gui/main_win.cc
author hegyi
Tue, 29 Nov 2005 19:31:58 +0000
changeset 1837 8dd6160ff699
parent 1823 cb082cdf3667
child 1838 b61682f0ee96
permissions -rw-r--r--
Structure of GUI is now more clear-cut than before.
     1 #include "main_win.h"
     2 #include "icons/guipixbufs.h"
     3 
     4 MainWin::MainWin()
     5 {
     6   mapwin=new MapWin("Map Setup", mapstorage.getEdgeMapList(), mapstorage.getNodeMapList(), *this);
     7   gd_canvas=new GraphDisplayerCanvas(*this);
     8 
     9   set_title ("unsaved file - " + prog_name);
    10   set_default_size(WIN_WIDTH,WIN_HEIGHT);
    11   add(vbox);
    12 
    13   //connecting signals - controller character
    14   mapstorage.signal_prop_ch().connect(sigc::mem_fun(*gd_canvas, &GraphDisplayerCanvas::propertyChange));
    15 
    16   // custom icons for the toolbar
    17 
    18   Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
    19  
    20   Glib::RefPtr<Gdk::Pixbuf> p_move_pixbuf = Gdk::Pixbuf::create_from_inline(
    21       2328, gui_icons_move);
    22   Glib::RefPtr<Gdk::Pixbuf> p_addnode_pixbuf = Gdk::Pixbuf::create_from_inline(
    23       2328, gui_icons_addnode);
    24   Glib::RefPtr<Gdk::Pixbuf> p_addlink_pixbuf = Gdk::Pixbuf::create_from_inline(
    25       2328, gui_icons_addlink);
    26   Glib::RefPtr<Gdk::Pixbuf> p_delete_pixbuf = Gdk::Pixbuf::create_from_inline(
    27       2328, gui_icons_delete);
    28   Glib::RefPtr<Gdk::Pixbuf> p_editlink_pixbuf = Gdk::Pixbuf::create_from_inline(
    29       2328, gui_icons_editlink);
    30   Glib::RefPtr<Gdk::Pixbuf> p_editnode_pixbuf = Gdk::Pixbuf::create_from_inline(
    31       2328, gui_icons_editnode);
    32   Glib::RefPtr<Gdk::Pixbuf> p_newmap_pixbuf = Gdk::Pixbuf::create_from_inline(
    33       2328, gui_icons_newmap);
    34 
    35   Gtk::IconSource move_icon_source;
    36   move_icon_source.set_pixbuf(p_move_pixbuf);
    37   Gtk::IconSet move_icon_set;
    38   move_icon_set.add_source(move_icon_source);
    39   p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set);
    40 
    41   Gtk::IconSource addnode_icon_source;
    42   addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
    43   Gtk::IconSet addnode_icon_set;
    44   addnode_icon_set.add_source(addnode_icon_source);
    45   p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set);
    46 
    47   Gtk::IconSource addlink_icon_source;
    48   addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
    49   Gtk::IconSet addlink_icon_set;
    50   addlink_icon_set.add_source(addlink_icon_source);
    51   p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set);
    52 
    53   Gtk::IconSource delete_icon_source;
    54   delete_icon_source.set_pixbuf(p_delete_pixbuf);
    55   Gtk::IconSet delete_icon_set;
    56   delete_icon_set.add_source(delete_icon_source);
    57   p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set);
    58 
    59   Gtk::IconSource editlink_icon_source;
    60   editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
    61   Gtk::IconSet editlink_icon_set;
    62   editlink_icon_set.add_source(editlink_icon_source);
    63   p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set);
    64 
    65   Gtk::IconSource editnode_icon_source;
    66   editnode_icon_source.set_pixbuf(p_editnode_pixbuf);
    67   Gtk::IconSet editnode_icon_set;
    68   editnode_icon_set.add_source(editnode_icon_source);
    69   p_icon_factory->add(Gtk::StockID("gd-editnode"), editnode_icon_set);
    70 
    71   Gtk::IconSource newmap_icon_source;
    72   newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
    73   Gtk::IconSet newmap_icon_set;
    74   newmap_icon_set.add_source(newmap_icon_source);
    75   p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set);
    76 
    77   p_icon_factory->add_default();
    78   
    79 
    80   ag=Gtk::ActionGroup::create();
    81 
    82   ag->add( Gtk::Action::create("FileMenu", "_File") );
    83   ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
    84       sigc::mem_fun(*this, &MainWin::newFile));
    85   ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
    86       sigc::mem_fun(*this, &MainWin::openFile));
    87   ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
    88       sigc::mem_fun(*this, &MainWin::saveFile));
    89   ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
    90       sigc::mem_fun(*this, &MainWin::saveFileAs));
    91   ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
    92       sigc::mem_fun(*this, &MainWin::close));
    93   ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
    94       sigc::mem_fun(*this, &MainWin::hide));
    95 
    96   ag->add( Gtk::Action::create("ViewMenu", "_View") );
    97   ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
    98       sigc::mem_fun(*(this->gd_canvas), &GraphDisplayerCanvas::zoomIn));
    99   ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
   100       sigc::mem_fun(*(this->gd_canvas), &GraphDisplayerCanvas::zoomOut));
   101   ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
   102       sigc::mem_fun(*(this->gd_canvas), &GraphDisplayerCanvas::zoomFit));
   103   ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
   104       sigc::mem_fun(*(this->gd_canvas), &GraphDisplayerCanvas::zoom100));
   105   
   106   ag->add( Gtk::Action::create("ShowMenu", "_Show") );
   107   ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
   108       sigc::mem_fun(*(this->mapwin), &MapWin::show));
   109 
   110   Gtk::RadioAction::Group tool_group;
   111   ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), "Move"),
   112       sigc::bind( sigc::mem_fun ( *(this->gd_canvas), &GraphDisplayerCanvas::changeEditorialTool ), 0) );
   113   ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), "Create node"),
   114       sigc::bind( sigc::mem_fun ( *(this->gd_canvas), &GraphDisplayerCanvas::changeEditorialTool ), 1) );
   115   ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), "Create edge"),
   116       sigc::bind( sigc::mem_fun ( *(this->gd_canvas), &GraphDisplayerCanvas::changeEditorialTool ), 2) );
   117   ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), "Delete"),
   118       sigc::bind( sigc::mem_fun ( *(this->gd_canvas), &GraphDisplayerCanvas::changeEditorialTool ), 3) );
   119 
   120   ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), "Edit edge map"),
   121       sigc::bind( sigc::mem_fun ( *(this->gd_canvas), &GraphDisplayerCanvas::changeEditorialTool ), 4) );
   122   ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), "Edit node map"),
   123       sigc::bind( sigc::mem_fun ( *(this->gd_canvas), &GraphDisplayerCanvas::changeEditorialTool ), 5) );
   124 
   125   ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
   126       sigc::mem_fun (new NewMapWin("NewMapWin", *this), &NewMapWin::show ) );
   127 
   128   uim=Gtk::UIManager::create();
   129   uim->insert_action_group(ag);
   130   add_accel_group(uim->get_accel_group());
   131 
   132   try
   133   {
   134 
   135     Glib::ustring ui_info =
   136       "<ui>"
   137       "  <menubar name='MenuBar'>"
   138       "    <menu action='FileMenu'>"
   139       "      <menuitem action='FileNew'/>"
   140       "      <menuitem action='FileOpen'/>"
   141       "      <menuitem action='FileSave'/>"
   142       "      <menuitem action='FileSaveAs'/>"
   143       "      <menuitem action='Close'/>"
   144       "      <menuitem action='Quit'/>"
   145       "    </menu>"
   146       "    <menu action='ViewMenu'>"
   147       "      <menuitem action='ViewZoomIn' />"
   148       "      <menuitem action='ViewZoomOut' />"
   149       "      <menuitem action='ViewZoom100' />"
   150       "      <menuitem action='ViewZoomFit' />"
   151       "    </menu>"
   152       "    <menu action='ShowMenu'>"
   153       "      <menuitem action='ShowMaps'/>"
   154       "    </menu>"
   155       "  </menubar>"
   156       "  <toolbar name='ToolBar'>"
   157       "    <toolitem action='FileNew' />"
   158       "    <toolitem action='FileOpen' />"
   159       "    <toolitem action='FileSave' />"
   160       "    <toolitem action='Close' />"
   161       "    <separator />"
   162       "    <toolitem action='ViewZoomIn' />"
   163       "    <toolitem action='ViewZoomOut' />"
   164       "    <toolitem action='ViewZoom100' />"
   165       "    <toolitem action='ViewZoomFit' />"
   166       "    <separator />"
   167       "    <toolitem action='MoveItem' />"
   168       "    <toolitem action='CreateNode' />"
   169       "    <toolitem action='CreateEdge' />"
   170       "    <toolitem action='EraseItem' />"
   171       "    <toolitem action='EditEdgeMap' />"
   172       "    <toolitem action='EditNodeMap' />"
   173       "    <separator />"
   174       "    <toolitem action='AddMap' />"
   175       "  </toolbar>"
   176       "</ui>";
   177 
   178     uim->add_ui_from_string(ui_info);
   179 
   180   }
   181   catch(const Glib::Error& ex)
   182   {
   183     std::cerr << "building menus failed: " <<  ex.what();
   184   }
   185 
   186   Gtk::Widget* menubar = uim->get_widget("/MenuBar");
   187   if (menubar){
   188     vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
   189   }
   190 
   191   Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
   192   if (toolbar)
   193   {
   194     static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
   195     vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
   196   }
   197 
   198   Gtk::ScrolledWindow* pScrolledWindow = manage(new Gtk::ScrolledWindow());
   199   pScrolledWindow->set_shadow_type(Gtk::SHADOW_IN);
   200   pScrolledWindow->add(*gd_canvas);
   201   vbox.pack_start(*pScrolledWindow);
   202 
   203   tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
   204   tooltips.enable();
   205 
   206   show_all_children();
   207 }
   208 
   209 void MainWin::readFile(const std::string &file)
   210 {
   211   mapstorage.readFromFile(file);
   212   mapstorage.file_name = file;
   213   mapstorage.modified = false;
   214   gd_canvas->drawGraph();
   215   mapwin->update(mapstorage.getEdgeMapList(), mapstorage.getNodeMapList());
   216   set_title(Glib::filename_display_basename(file) + " - " + prog_name);
   217 }
   218 
   219 void MainWin::newFile()
   220 {
   221   if (mapstorage.modified)
   222   {
   223     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
   224         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   225     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   226     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   227     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   228     switch (mdialog.run())
   229     {
   230       case Gtk::RESPONSE_CANCEL:
   231         return;
   232       case Gtk::RESPONSE_REJECT:
   233         break;
   234       case Gtk::RESPONSE_ACCEPT:
   235         saveFile();
   236         break;
   237     }
   238   }
   239   gd_canvas->clear();
   240   mapstorage.clear();
   241   mapwin->update(mapstorage.getEdgeMapList(), mapstorage.getNodeMapList());
   242   set_title("unsaved file - " + prog_name);
   243 }
   244 
   245 void MainWin::openFile()
   246 {
   247   if (mapstorage.modified)
   248   {
   249     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true, 
   250         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   251     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   252     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   253     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   254     switch (mdialog.run())
   255     {
   256       case Gtk::RESPONSE_CANCEL:
   257         return;
   258       case Gtk::RESPONSE_REJECT:
   259         break;
   260       case Gtk::RESPONSE_ACCEPT:
   261         saveFile();
   262         break;
   263     }
   264   }
   265   Gtk::FileChooserDialog fcdialog("Open File");
   266   fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   267   fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
   268   if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
   269   {
   270     gd_canvas->clear();
   271     mapstorage.clear();
   272     Glib::ustring filename = fcdialog.get_filename();
   273     if (!mapstorage.readFromFile(filename))
   274     {
   275       mapstorage.file_name = filename;
   276       mapstorage.modified = false;
   277       gd_canvas->drawGraph();
   278       mapwin->update(mapstorage.getEdgeMapList(), mapstorage.getNodeMapList());
   279       set_title(Glib::filename_display_basename(filename) + " - " + prog_name);
   280     }
   281   }
   282 }
   283 
   284 void MainWin::saveFile()
   285 {
   286   if (mapstorage.file_name == "") {
   287     saveFileAs();
   288   }
   289   else
   290   {
   291     mapstorage.writeToFile(mapstorage.file_name);
   292     mapstorage.modified = false;
   293     set_title(Glib::filename_display_basename(mapstorage.file_name) + " - " +
   294         prog_name);
   295   }
   296 }
   297 
   298 void MainWin::saveFileAs()
   299 {
   300   Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
   301   fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   302   fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   303   if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
   304   {
   305     Glib::ustring filename = fcdialog.get_filename();
   306     mapstorage.file_name = filename;
   307     mapstorage.writeToFile(filename);
   308     mapstorage.modified = false;
   309     set_title(Glib::filename_display_basename(filename) + " - " + prog_name);
   310   }
   311 }
   312 
   313 void MainWin::close()
   314 {
   315   if (mapstorage.modified)
   316   {
   317     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
   318         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   319     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   320     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   321     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   322     switch (mdialog.run())
   323     {
   324       case Gtk::RESPONSE_CANCEL:
   325         return;
   326       case Gtk::RESPONSE_REJECT:
   327         break;
   328       case Gtk::RESPONSE_ACCEPT:
   329         saveFile();
   330         break;
   331     }
   332   }
   333   gd_canvas->clear();
   334   mapstorage.clear();
   335   mapwin->update(mapstorage.getEdgeMapList(), mapstorage.getNodeMapList());
   336   set_title("unsaved file - " + prog_name);
   337 }
   338 
   339 void MainWin::propertyChange(bool itisedge, int prop, std::string mapname)
   340 {
   341   mapstorage.changeActiveMap(itisedge, prop, mapname);
   342 }
   343 
   344 void MainWin::popupNewMapWin(bool itisedge, int prop)
   345 {
   346   prop=prop;
   347   (new NewMapWin("NewMapWin", *this, itisedge, false))->run();
   348 }
   349 
   350 std::string MainWin::getActiveEdgeMap(int prop)
   351 {
   352   return mapstorage.getActiveEdgeMap(prop);
   353 }
   354 
   355 std::string MainWin::getActiveNodeMap(int prop)
   356 {
   357   return mapstorage.getActiveNodeMap(prop);
   358 }
   359 
   360 void MainWin::registerNewEdgeMap(std::string mapname)
   361 {
   362   mapwin->registerNewEdgeMap(mapname);
   363 }
   364 
   365 void MainWin::registerNewNodeMap(std::string mapname)
   366 {
   367   mapwin->registerNewNodeMap(mapname);
   368 }