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