main_win.cc
author alpar
Wed, 04 Jan 2006 13:31:59 +0000
branchgui
changeset 105 9e78d14fd0ba
parent 103 3a263e57e1d9
child 106 853dd852abc7
permissions -rw-r--r--
Happy new year to LEMON
     1 #include "main_win.h"
     2 #include "guipixbufs.h"
     3 
     4 MainWin::MainWin()
     5 {
     6   set_title ("no file");
     7   set_default_size(WIN_WIDTH,WIN_HEIGHT);
     8   add(vbox);
     9 
    10   // custom icons for the toolbar
    11   Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
    12  
    13   Glib::RefPtr<Gdk::Pixbuf> p_move_pixbuf = Gdk::Pixbuf::create_from_inline(
    14       2328, gui_icons_move);
    15   Glib::RefPtr<Gdk::Pixbuf> p_addnode_pixbuf = Gdk::Pixbuf::create_from_inline(
    16       2328, gui_icons_addnode);
    17   Glib::RefPtr<Gdk::Pixbuf> p_addlink_pixbuf = Gdk::Pixbuf::create_from_inline(
    18       2328, gui_icons_addlink);
    19   Glib::RefPtr<Gdk::Pixbuf> p_delete_pixbuf = Gdk::Pixbuf::create_from_inline(
    20       2328, gui_icons_delete);
    21   Glib::RefPtr<Gdk::Pixbuf> p_editlink_pixbuf = Gdk::Pixbuf::create_from_inline(
    22       2328, gui_icons_editlink);
    23   Glib::RefPtr<Gdk::Pixbuf> p_editnode_pixbuf = Gdk::Pixbuf::create_from_inline(
    24       2328, gui_icons_editnode);
    25   Glib::RefPtr<Gdk::Pixbuf> p_newmap_pixbuf = Gdk::Pixbuf::create_from_inline(
    26       2328, gui_icons_newmap);
    27 
    28   Gtk::IconSource move_icon_source;
    29   move_icon_source.set_pixbuf(p_move_pixbuf);
    30   Gtk::IconSet move_icon_set;
    31   move_icon_set.add_source(move_icon_source);
    32   p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set);
    33 
    34   Gtk::IconSource addnode_icon_source;
    35   addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
    36   Gtk::IconSet addnode_icon_set;
    37   addnode_icon_set.add_source(addnode_icon_source);
    38   p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set);
    39 
    40   Gtk::IconSource addlink_icon_source;
    41   addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
    42   Gtk::IconSet addlink_icon_set;
    43   addlink_icon_set.add_source(addlink_icon_source);
    44   p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set);
    45 
    46   Gtk::IconSource delete_icon_source;
    47   delete_icon_source.set_pixbuf(p_delete_pixbuf);
    48   Gtk::IconSet delete_icon_set;
    49   delete_icon_set.add_source(delete_icon_source);
    50   p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set);
    51 
    52   Gtk::IconSource editlink_icon_source;
    53   editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
    54   Gtk::IconSet editlink_icon_set;
    55   editlink_icon_set.add_source(editlink_icon_source);
    56   p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set);
    57 
    58   Gtk::IconSource editnode_icon_source;
    59   editnode_icon_source.set_pixbuf(p_editnode_pixbuf);
    60   Gtk::IconSet editnode_icon_set;
    61   editnode_icon_set.add_source(editnode_icon_source);
    62   p_icon_factory->add(Gtk::StockID("gd-editnode"), editnode_icon_set);
    63 
    64   Gtk::IconSource newmap_icon_source;
    65   newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
    66   Gtk::IconSet newmap_icon_set;
    67   newmap_icon_set.add_source(newmap_icon_source);
    68   p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set);
    69 
    70   p_icon_factory->add_default();
    71   
    72   ag=Gtk::ActionGroup::create();
    73 
    74   ag->add( Gtk::Action::create("FileMenu", "_File") );
    75   ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
    76       sigc::mem_fun(*this, &MainWin::newTab));
    77   ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
    78       sigc::mem_fun(*this, &MainWin::openFile));
    79   ag->add( Gtk::Action::create("FileClearTab", "Clear Tab"),
    80       sigc::mem_fun(*this, &MainWin::newFile));
    81   ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
    82       sigc::mem_fun(*this, &MainWin::saveFile));
    83   ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
    84       sigc::mem_fun(*this, &MainWin::saveFileAs));
    85   ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
    86       sigc::mem_fun(*this, &MainWin::closeTab));
    87   ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
    88       sigc::mem_fun(*this, &MainWin::hide));
    89 
    90   ag->add( Gtk::Action::create("ViewMenu", "_View") );
    91   ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
    92       sigc::mem_fun(*this, &MainWin::zoomIn));
    93   ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
    94       sigc::mem_fun(*this, &MainWin::zoomOut));
    95   ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
    96       sigc::mem_fun(*this, &MainWin::zoomFit));
    97   ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
    98       sigc::mem_fun(*this, &MainWin::zoom100));
    99   
   100   ag->add( Gtk::Action::create("ShowMenu", "_Show") );
   101   ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
   102 	   sigc::mem_fun(*this, &MainWin::createMapWin));
   103 
   104   ag->add( Gtk::Action::create("AlgoMenu", "_Algorithms") );
   105   ag->add( Gtk::Action::create("AlgoGeneral", "_General"),
   106 	   sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 0) );
   107 
   108   Gtk::RadioAction::Group tool_group;
   109   ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), "Move"),
   110       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 0) );
   111   ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), "Create node"),
   112       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 1) );
   113   ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), "Create edge"),
   114       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 2) );
   115   ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), "Delete"),
   116       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 3) );
   117 
   118   ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), "Edit edge map"),
   119       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 4) );
   120   ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), "Edit node map"),
   121       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 5) );
   122 
   123   ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
   124       sigc::mem_fun ( *this , &MainWin::createNewMapWin ) );
   125 
   126   uim=Gtk::UIManager::create();
   127   uim->insert_action_group(ag);
   128   add_accel_group(uim->get_accel_group());
   129 
   130   try
   131   {
   132 
   133     Glib::ustring ui_info =
   134       "<ui>"
   135       "  <menubar name='MenuBar'>"
   136       "    <menu action='FileMenu'>"
   137       "      <menuitem action='FileNew'/>"
   138       "      <menuitem action='FileOpen'/>"
   139       "      <menuitem action='FileClearTab'/>"
   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       "    <menu action='AlgoMenu'>"
   155       "      <menuitem action='AlgoGeneral'/>"
   156       "    </menu>"
   157       "  </menubar>"
   158       "  <toolbar name='ToolBar'>"
   159       "    <toolitem action='FileNew' />"
   160       "    <toolitem action='FileOpen' />"
   161       "    <toolitem action='FileSave' />"
   162       "    <toolitem action='Close' />"
   163       "    <separator />"
   164       "    <toolitem action='ViewZoomIn' />"
   165       "    <toolitem action='ViewZoomOut' />"
   166       "    <toolitem action='ViewZoom100' />"
   167       "    <toolitem action='ViewZoomFit' />"
   168       "    <separator />"
   169       "    <toolitem action='MoveItem' />"
   170       "    <toolitem action='CreateNode' />"
   171       "    <toolitem action='CreateEdge' />"
   172       "    <toolitem action='EraseItem' />"
   173       "    <toolitem action='EditEdgeMap' />"
   174       "    <toolitem action='EditNodeMap' />"
   175       "    <separator />"
   176       "    <toolitem action='AddMap' />"
   177       "  </toolbar>"
   178       "</ui>";
   179 
   180     uim->add_ui_from_string(ui_info);
   181 
   182   }
   183   catch(const Glib::Error& ex)
   184   {
   185     std::cerr << "building menus failed: " <<  ex.what();
   186   }
   187 
   188   Gtk::Widget* menubar = uim->get_widget("/MenuBar");
   189   if (menubar){
   190     vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
   191   }
   192 
   193   Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
   194   if (toolbar)
   195   {
   196     static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
   197     vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
   198   }
   199 
   200   tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
   201   tooltips.enable();
   202 
   203   active_tab=-1;
   204   notebook.signal_switch_page().connect(sigc::mem_fun(*this, &MainWin::onChangeTab));
   205 
   206   vbox.pack_start(notebook);
   207   
   208   show_all_children();
   209 }
   210 
   211 void MainWin::set_tabtitle(std::string name)
   212 {
   213   if(strinst.find(name)==strinst.end())
   214     {
   215       tabnames[active_tab]=name;
   216       strinst[name]=1;
   217     }
   218   else
   219     {
   220       strinst[name]++;
   221       std::ostringstream o;
   222       o << strinst[name];
   223       tabnames[active_tab]=name+" - "+o.str();
   224     }
   225   set_title(tabnames[active_tab] + " - " + prog_name);
   226   notebook.set_tab_label_text((Widget&)*(tabs[active_tab]), tabnames[active_tab]);
   227   updateAlgoWins();
   228 }
   229 
   230 void MainWin::readFile(const std::string & filename)
   231 {
   232   newTab();
   233   tabs[active_tab]->readFile(filename);
   234 }
   235 
   236 void MainWin::newTab()
   237 {
   238   int size=tabs.size();
   239   tabs.resize(size+1);
   240   tabnames.resize(size+1);
   241   active_tab=size;
   242   tabs[active_tab]=new NoteBookTab();
   243   tabs[active_tab]->signal_title_ch().connect(sigc::mem_fun(*this, &MainWin::set_tabtitle));
   244   tabs[active_tab]->signal_newmap_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinAfterSignal));
   245   tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
   246   notebook.append_page((Gtk::Widget&)(*(tabs[active_tab])));
   247   notebook.set_current_page(size);
   248   set_tabtitle("unsaved file");
   249   updateAlgoWins();
   250 }
   251 
   252 void MainWin::closeTab()
   253 {
   254   if(active_tab!=-1)
   255     {
   256       if (tabs[active_tab]->mapstorage.modified)
   257 	{
   258 	  Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true, 
   259 				     Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   260 	  mdialog.add_button("Close file _without Saving", Gtk::RESPONSE_REJECT);
   261 	  mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   262 	  mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   263 	  switch (mdialog.run())
   264 	    {
   265 	    case Gtk::RESPONSE_CANCEL:
   266 	      return;
   267 	    case Gtk::RESPONSE_REJECT:
   268 	      break;
   269 	    case Gtk::RESPONSE_ACCEPT:
   270 	      tabs[active_tab]->saveFile();
   271 	      break;
   272 	    }
   273 	}
   274       //tabs vector will be decreased with the deleted value
   275       int size=tabs.size();
   276       if(size>1)
   277 	{
   278 	  for(int i=active_tab+1;i<size;i++)
   279 	    {
   280 	      tabnames[i-1]=tabnames[i];
   281 	      tabs[i-1]=tabs[i];
   282 	    }
   283 	}
   284       //if size==1 resize will delete the only element
   285       tabs.resize(size-1);
   286       tabnames.resize(size-1);
   287 
   288       int old_active_tab=active_tab;
   289       notebook.remove_page(active_tab);
   290 
   291       //If the first tab was active, upon delete notebook
   292       //will first switch one tab upper and not lower like
   293       //in the case, when not the first tab was active.
   294       //But after deletion it will become the first tab,
   295       //and this should be registrated in tabs vector,
   296       //as well.
   297       if((old_active_tab==0)&&(size!=1))
   298 	{
   299 	  onChangeTab(NULL,0);
   300 	}
   301 
   302       //if this was the last page in notebook, there is
   303       //no active_tab now
   304       if(size==1)
   305 	{
   306 	  active_tab=-1;
   307 	}
   308 
   309       updateAlgoWins();
   310     }
   311 }
   312 
   313 void MainWin::onChangeTab(GtkNotebookPage* page, guint page_num)
   314 {
   315   page=page;
   316   active_tab=page_num;
   317   tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
   318   set_title(tabnames[active_tab]);
   319 }
   320 
   321 void MainWin::onCloseTab()
   322 {
   323 }
   324 
   325 void MainWin::newFile()
   326 {
   327   if(active_tab!=-1)
   328     {
   329       tabs[active_tab]->newFile();
   330     }
   331 }
   332  
   333 void MainWin::openFile()
   334 {
   335   if(active_tab!=-1)
   336     {
   337       tabs[active_tab]->openFile();
   338     }
   339 }
   340  
   341 void MainWin::saveFile()
   342 {
   343   if(active_tab!=-1)
   344     {
   345       tabs[active_tab]->saveFile();
   346     }
   347 }
   348  
   349 void MainWin::saveFileAs()
   350 {
   351   if(active_tab!=-1)
   352     {
   353       tabs[active_tab]->saveFileAs();
   354     }
   355 }
   356  
   357 void MainWin::close()
   358 {
   359   if(active_tab!=-1)
   360     {
   361       tabs[active_tab]->close();
   362     }
   363 }
   364 
   365 void MainWin::zoomIn()
   366 {
   367   if(active_tab!=-1)
   368     {
   369       tabs[active_tab]->gd_canvas->zoomIn();
   370     }
   371 }
   372 
   373 void MainWin::zoomOut()
   374 {
   375   if(active_tab!=-1)
   376     {
   377       tabs[active_tab]->gd_canvas->zoomOut();
   378     }
   379 }
   380 
   381 void MainWin::zoomFit()
   382 {
   383   if(active_tab!=-1)
   384     {
   385       tabs[active_tab]->gd_canvas->zoomFit();
   386     }
   387 }
   388 
   389 void MainWin::zoom100()
   390 {
   391   if(active_tab!=-1)
   392     {
   393       tabs[active_tab]->gd_canvas->zoom100();
   394     }
   395 }
   396 
   397 void MainWin::createMapWin()
   398 {
   399   if(active_tab!=-1)
   400     {
   401       tabs[active_tab]->createMapWin(tabnames[active_tab]);
   402     }
   403 }
   404 
   405 void MainWin::createAlgoWin(int algoid)
   406 {
   407   AlgoWin * aw=new AlgoWin(algoid, tabnames);
   408   aw->signal_closing().connect(sigc::mem_fun(*this, &MainWin::deRegisterAlgoWin));
   409   aws.insert(aw);
   410   aw->show();
   411 }
   412 
   413 void MainWin::deRegisterAlgoWin(AlgoWin * awp)
   414 {
   415   aws.erase(awp);
   416 }
   417 
   418 void MainWin::updateAlgoWins()
   419 {
   420   std::set< AlgoWin* >::iterator awsi=aws.begin();
   421   for(;awsi!=aws.end();awsi++)
   422     {
   423       (*awsi)->update_tablist(tabnames);
   424     }
   425 }
   426 
   427 void MainWin::changeEditorialTool(int tool)
   428 {
   429   active_tool=tool;
   430   if(active_tab!=-1)
   431     {
   432       tabs[active_tab]->gd_canvas->changeEditorialTool(tool);
   433     }
   434 }
   435 
   436 void MainWin::createNewMapWin()
   437 {
   438   if(active_tab!=-1)
   439     {
   440       NewMapWin * nmw=new NewMapWin("Create New Map - "+tabnames[active_tab], *(tabs[active_tab]));
   441       nmw->show();
   442     }
   443 }
   444 
   445 void MainWin::createNewMapWinAfterSignal(NoteBookTab * nbt, bool itisedge)
   446 {
   447   std::vector<NoteBookTab*>::iterator nbti=tabs.begin();
   448   int i=0;
   449   for(;nbti!=tabs.end();nbti++)
   450     {
   451       if(*nbti!=nbt)
   452 	{
   453 	  i++;
   454 	}
   455       else
   456 	{
   457 	  continue;
   458 	}
   459     }
   460   NewMapWin * nmw=new NewMapWin("Create New Map - "+tabnames[i], *nbt, itisedge, false);
   461   nmw->run();
   462 }