COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/main_win.cc @ 2052:c54ce7a5f4e7

Last change on this file since 2052:c54ce7a5f4e7 was 2052:c54ce7a5f4e7, checked in by Akos Ladanyi, 15 years ago

some reorganization

File size: 14.1 KB
RevLine 
[2012]1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
[1606]5#include "main_win.h"
[1862]6#include "guipixbufs.h"
[1442]7
[2052]8#include "i18n.h"
[2012]9
[1849]10MainWin::MainWin()
[1442]11{
[1849]12  set_title ("no file");
[1442]13  set_default_size(WIN_WIDTH,WIN_HEIGHT);
14  add(vbox);
15
[1634]16  // custom icons for the toolbar
17  Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
18 
[1635]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);
[1634]33
34  Gtk::IconSource move_icon_source;
[1635]35  move_icon_source.set_pixbuf(p_move_pixbuf);
[1634]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;
[1635]41  addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
[1634]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;
[1635]47  addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
[1634]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;
[1635]53  delete_icon_source.set_pixbuf(p_delete_pixbuf);
[1634]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;
[1635]59  editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
[1634]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;
[1635]65  editnode_icon_source.set_pixbuf(p_editnode_pixbuf);
[1634]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;
[1635]71  newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
[1634]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 
[1442]78  ag=Gtk::ActionGroup::create();
79
[2012]80  ag->add( Gtk::Action::create("FileMenu", _("_File")) );
[1869]81  ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
[1849]82      sigc::mem_fun(*this, &MainWin::newTab));
[1442]83  ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
84      sigc::mem_fun(*this, &MainWin::openFile));
[2012]85  ag->add( Gtk::Action::create("FileClearTab", _("Clear Tab")),
[1869]86      sigc::mem_fun(*this, &MainWin::newFile));
[1442]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));
[1606]91  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
[1869]92      sigc::mem_fun(*this, &MainWin::closeTab));
[1606]93  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
[1598]94      sigc::mem_fun(*this, &MainWin::hide));
[1442]95
[2012]96  ag->add( Gtk::Action::create("ViewMenu", _("_View")) );
[1442]97  ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
[1849]98      sigc::mem_fun(*this, &MainWin::zoomIn));
[1442]99  ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
[1849]100      sigc::mem_fun(*this, &MainWin::zoomOut));
[1442]101  ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
[1849]102      sigc::mem_fun(*this, &MainWin::zoomFit));
[1442]103  ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
[1849]104      sigc::mem_fun(*this, &MainWin::zoom100));
[1442]105 
[2012]106  ag->add( Gtk::Action::create("ShowMenu", _("_Show")) );
107  ag->add( Gtk::Action::create("ShowMaps", _("_Maps")),
[1838]108           sigc::mem_fun(*this, &MainWin::createMapWin));
[1442]109
[2012]110  ag->add( Gtk::Action::create("AlgoMenu", _("_Algorithms")) );
111  ag->add( Gtk::Action::create("AlgoGeneral", _("_General")),
[1871]112           sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 0) );
[2012]113  ag->add( Gtk::Action::create("AlgoKruskal", _("_Kruskal")),
[1876]114           sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 1) );
[1871]115
[1606]116  Gtk::RadioAction::Group tool_group;
[2012]117  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), _("Move")),
[1849]118      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 0) );
[2012]119  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), _("Create node")),
[1849]120      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 1) );
[2012]121  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), _("Create edge")),
[1849]122      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 2) );
[2012]123  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), _("Delete")),
[1849]124      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 3) );
[1634]125
[2012]126  ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), _("Edit edge map")),
[1849]127      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 4) );
[2012]128  ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), _("Edit node map")),
[1849]129      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 5) );
[1634]130
131  ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
[1849]132      sigc::mem_fun ( *this , &MainWin::createNewMapWin ) );
[1502]133
[1442]134  uim=Gtk::UIManager::create();
135  uim->insert_action_group(ag);
136  add_accel_group(uim->get_accel_group());
137
138  try
139  {
140
141    Glib::ustring ui_info =
142      "<ui>"
143      "  <menubar name='MenuBar'>"
144      "    <menu action='FileMenu'>"
145      "      <menuitem action='FileNew'/>"
146      "      <menuitem action='FileOpen'/>"
[1869]147      "      <menuitem action='FileClearTab'/>"
[1442]148      "      <menuitem action='FileSave'/>"
149      "      <menuitem action='FileSaveAs'/>"
[1606]150      "      <menuitem action='Close'/>"
151      "      <menuitem action='Quit'/>"
[1442]152      "    </menu>"
153      "    <menu action='ViewMenu'>"
154      "      <menuitem action='ViewZoomIn' />"
155      "      <menuitem action='ViewZoomOut' />"
156      "      <menuitem action='ViewZoom100' />"
[1777]157      "      <menuitem action='ViewZoomFit' />"
[1442]158      "    </menu>"
159      "    <menu action='ShowMenu'>"
160      "      <menuitem action='ShowMaps'/>"
161      "    </menu>"
[1871]162      "    <menu action='AlgoMenu'>"
163      "      <menuitem action='AlgoGeneral'/>"
[1876]164      "      <menuitem action='AlgoKruskal'/>"
[1871]165      "    </menu>"
[1442]166      "  </menubar>"
167      "  <toolbar name='ToolBar'>"
168      "    <toolitem action='FileNew' />"
169      "    <toolitem action='FileOpen' />"
170      "    <toolitem action='FileSave' />"
[1606]171      "    <toolitem action='Close' />"
[1442]172      "    <separator />"
173      "    <toolitem action='ViewZoomIn' />"
174      "    <toolitem action='ViewZoomOut' />"
175      "    <toolitem action='ViewZoom100' />"
[1777]176      "    <toolitem action='ViewZoomFit' />"
[1502]177      "    <separator />"
[1606]178      "    <toolitem action='MoveItem' />"
[1502]179      "    <toolitem action='CreateNode' />"
180      "    <toolitem action='CreateEdge' />"
181      "    <toolitem action='EraseItem' />"
[1550]182      "    <toolitem action='EditEdgeMap' />"
183      "    <toolitem action='EditNodeMap' />"
[1634]184      "    <separator />"
[1592]185      "    <toolitem action='AddMap' />"
[1442]186      "  </toolbar>"
187      "</ui>";
188
189    uim->add_ui_from_string(ui_info);
190
191  }
192  catch(const Glib::Error& ex)
193  {
194    std::cerr << "building menus failed: " <<  ex.what();
195  }
196
197  Gtk::Widget* menubar = uim->get_widget("/MenuBar");
198  if (menubar){
199    vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
200  }
201
202  Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
203  if (toolbar)
204  {
205    static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
206    vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
207  }
208
[1601]209  tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
210  tooltips.enable();
[1442]211
[1849]212  active_tab=-1;
213  notebook.signal_switch_page().connect(sigc::mem_fun(*this, &MainWin::onChangeTab));
214
[1877]215  active_tool = MOVE;
216
[1849]217  vbox.pack_start(notebook);
218 
[1442]219  show_all_children();
220}
221
[1849]222void MainWin::set_tabtitle(std::string name)
[1616]223{
[1872]224  if(strinst.find(name)==strinst.end())
225    {
226      tabnames[active_tab]=name;
227      strinst[name]=1;
228    }
229  else
230    {
231      strinst[name]++;
232      std::ostringstream o;
233      o << strinst[name];
234      tabnames[active_tab]=name+" - "+o.str();
235    }
[1849]236  set_title(tabnames[active_tab] + " - " + prog_name);
237  notebook.set_tab_label_text((Widget&)*(tabs[active_tab]), tabnames[active_tab]);
[1876]238  updateAlgoWinTabs();
[1849]239}
240
241void MainWin::readFile(const std::string & filename)
242{
243  newTab();
244  tabs[active_tab]->readFile(filename);
245}
246
247void MainWin::newTab()
248{
249  int size=tabs.size();
250  tabs.resize(size+1);
251  tabnames.resize(size+1);
252  active_tab=size;
253  tabs[active_tab]=new NoteBookTab();
254  tabs[active_tab]->signal_title_ch().connect(sigc::mem_fun(*this, &MainWin::set_tabtitle));
255  tabs[active_tab]->signal_newmap_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinAfterSignal));
256  tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
257  notebook.append_page((Gtk::Widget&)(*(tabs[active_tab])));
258  notebook.set_current_page(size);
[2012]259  set_tabtitle(_("unsaved file"));
[1876]260  updateAlgoWinTabs();
[1849]261}
262
[1856]263void MainWin::closeTab()
264{
265  if(active_tab!=-1)
266    {
[1869]267      if (tabs[active_tab]->mapstorage.modified)
268        {
[2012]269          Gtk::MessageDialog mdialog(_("<b>Save changes before closing?</b>"), true,
[1869]270                                     Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
[2012]271          mdialog.add_button(_("Close file _without Saving"), Gtk::RESPONSE_REJECT);
[1869]272          mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
273          mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
274          switch (mdialog.run())
275            {
276            case Gtk::RESPONSE_CANCEL:
277              return;
278            case Gtk::RESPONSE_REJECT:
279              break;
280            case Gtk::RESPONSE_ACCEPT:
281              tabs[active_tab]->saveFile();
282              break;
283            }
284        }
[1856]285      //tabs vector will be decreased with the deleted value
286      int size=tabs.size();
287      if(size>1)
288        {
289          for(int i=active_tab+1;i<size;i++)
290            {
[1871]291              tabnames[i-1]=tabnames[i];
[1856]292              tabs[i-1]=tabs[i];
293            }
294        }
295      //if size==1 resize will delete the only element
296      tabs.resize(size-1);
[1871]297      tabnames.resize(size-1);
[1856]298
299      int old_active_tab=active_tab;
300      notebook.remove_page(active_tab);
301
302      //If the first tab was active, upon delete notebook
303      //will first switch one tab upper and not lower like
304      //in the case, when not the first tab was active.
305      //But after deletion it will become the first tab,
306      //and this should be registrated in tabs vector,
307      //as well.
[1871]308      if((old_active_tab==0)&&(size!=1))
[1856]309        {
310          onChangeTab(NULL,0);
311        }
312
313      //if this was the last page in notebook, there is
314      //no active_tab now
315      if(size==1)
316        {
317          active_tab=-1;
318        }
[1871]319
[1876]320      updateAlgoWinTabs();
[1856]321    }
322}
323
[1849]324void MainWin::onChangeTab(GtkNotebookPage* page, guint page_num)
325{
326  page=page;
327  active_tab=page_num;
328  tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
329  set_title(tabnames[active_tab]);
330}
331
[1442]332void MainWin::newFile()
333{
[1849]334  if(active_tab!=-1)
[1606]335    {
[1849]336      tabs[active_tab]->newFile();
[1606]337    }
[1442]338}
[1849]339 
[1442]340void MainWin::openFile()
341{
[1849]342  if(active_tab!=-1)
[1606]343    {
[1849]344      tabs[active_tab]->openFile();
[1606]345    }
[1442]346}
[1849]347 
[1442]348void MainWin::saveFile()
349{
[1849]350  if(active_tab!=-1)
351    {
352      tabs[active_tab]->saveFile();
353    }
[1442]354}
[1849]355 
[1442]356void MainWin::saveFileAs()
357{
[1849]358  if(active_tab!=-1)
359    {
360      tabs[active_tab]->saveFileAs();
361    }
[1442]362}
[1849]363 
[1606]364void MainWin::close()
365{
[1849]366  if(active_tab!=-1)
[1606]367    {
[1849]368      tabs[active_tab]->close();
[1838]369    }
[1837]370}
371
[1849]372void MainWin::zoomIn()
[1837]373{
[1849]374  if(active_tab!=-1)
[1838]375    {
[1849]376      tabs[active_tab]->gd_canvas->zoomIn();
377    }
378}
379
380void MainWin::zoomOut()
381{
382  if(active_tab!=-1)
383    {
384      tabs[active_tab]->gd_canvas->zoomOut();
385    }
386}
387
388void MainWin::zoomFit()
389{
390  if(active_tab!=-1)
391    {
392      tabs[active_tab]->gd_canvas->zoomFit();
393    }
394}
395
396void MainWin::zoom100()
397{
398  if(active_tab!=-1)
399    {
400      tabs[active_tab]->gd_canvas->zoom100();
[1838]401    }
[1837]402}
[1838]403
404void MainWin::createMapWin()
405{
[1849]406  if(active_tab!=-1)
[1838]407    {
[1849]408      tabs[active_tab]->createMapWin(tabnames[active_tab]);
[1838]409    }
410}
411
[1871]412void MainWin::createAlgoWin(int algoid)
413{
[1878]414  AlgoWin * aw=new AlgoWin(algoid, tabnames);
[1871]415  aw->signal_closing().connect(sigc::mem_fun(*this, &MainWin::deRegisterAlgoWin));
[1876]416  aw->signal_maplist_needed().connect(sigc::mem_fun(*this, &MainWin::updateAlgoWinMaps));
[1884]417  aw->signal_newmapwin_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinTabString));
[1871]418  aws.insert(aw);
419  aw->show();
420}
421
[1876]422void MainWin::updateAlgoWinTabs()
[1871]423{
424  std::set< AlgoWin* >::iterator awsi=aws.begin();
425  for(;awsi!=aws.end();awsi++)
426    {
427      (*awsi)->update_tablist(tabnames);
428    }
429}
430
[1878]431void MainWin::updateAlgoWinMaps(AlgoWin * awp, std::string tabname)
432{
433  int i=0;
434  for(;(i<(int)tabnames.size())&&(tabnames[i]!=tabname);i++)
435    {
436    }
437  awp->update_maplist(&(tabs[i]->mapstorage));
438}
439
440void MainWin::deRegisterAlgoWin(AlgoWin * awp)
441{
442  aws.erase(awp);
443}
444
[1849]445void MainWin::changeEditorialTool(int tool)
[1838]446{
[1849]447  active_tool=tool;
448  if(active_tab!=-1)
449    {
450      tabs[active_tab]->gd_canvas->changeEditorialTool(tool);
451    }
[1838]452}
[1849]453
454void MainWin::createNewMapWin()
455{
456  if(active_tab!=-1)
457    {
[2012]458      NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[active_tab], *(tabs[active_tab]));
[1849]459      nmw->show();
460    }
461}
462
[1884]463void MainWin::createNewMapWinTabString(std::string tabname, bool itisedge)
464{
465  int i=0;
[1890]466  for(;((i<(int)tabnames.size())&&(tabnames[i]!=tabname));i++)
[1884]467    {
468    }
469  createNewMapWinAfterSignal(tabs[i], itisedge);
470}
471
[1849]472void MainWin::createNewMapWinAfterSignal(NoteBookTab * nbt, bool itisedge)
473{
474  std::vector<NoteBookTab*>::iterator nbti=tabs.begin();
475  int i=0;
476  for(;nbti!=tabs.end();nbti++)
477    {
478      if(*nbti!=nbt)
479        {
480          i++;
481        }
482      else
483        {
484          continue;
485        }
486    }
[2012]487  NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[i], *nbt, itisedge, false);
[1849]488  nmw->run();
489}
Note: See TracBrowser for help on using the repository browser.