COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/main_win.cc @ 1857:2e3a4481901e

Last change on this file since 1857:2e3a4481901e was 1856:3f0558065bcd, checked in by Hegyi Péter, 15 years ago

Notebook tabs can be closed.

File size: 11.8 KB
RevLine 
[1606]1#include "main_win.h"
[1659]2#include "icons/guipixbufs.h"
[1442]3
[1849]4MainWin::MainWin()
[1442]5{
[1849]6  set_title ("no file");
[1442]7  set_default_size(WIN_WIDTH,WIN_HEIGHT);
8  add(vbox);
9
[1634]10  // custom icons for the toolbar
11  Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
12 
[1635]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);
[1634]27
28  Gtk::IconSource move_icon_source;
[1635]29  move_icon_source.set_pixbuf(p_move_pixbuf);
[1634]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;
[1635]35  addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
[1634]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;
[1635]41  addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
[1634]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;
[1635]47  delete_icon_source.set_pixbuf(p_delete_pixbuf);
[1634]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;
[1635]53  editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
[1634]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;
[1635]59  editnode_icon_source.set_pixbuf(p_editnode_pixbuf);
[1634]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;
[1635]65  newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
[1634]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 
[1442]72  ag=Gtk::ActionGroup::create();
73
74  ag->add( Gtk::Action::create("FileMenu", "_File") );
[1849]75  ag->add( Gtk::Action::create("FileNewTab", "New _Tab"),
76      sigc::mem_fun(*this, &MainWin::newTab));
[1856]77  ag->add( Gtk::Action::create("FileCloseTab", "_Close Tab"),
78      sigc::mem_fun(*this, &MainWin::closeTab));
[1442]79  ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
80      sigc::mem_fun(*this, &MainWin::newFile));
81  ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
82      sigc::mem_fun(*this, &MainWin::openFile));
83  ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
84      sigc::mem_fun(*this, &MainWin::saveFile));
85  ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
86      sigc::mem_fun(*this, &MainWin::saveFileAs));
[1606]87  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
88      sigc::mem_fun(*this, &MainWin::close));
89  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
[1598]90      sigc::mem_fun(*this, &MainWin::hide));
[1442]91
92  ag->add( Gtk::Action::create("ViewMenu", "_View") );
93  ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
[1849]94      sigc::mem_fun(*this, &MainWin::zoomIn));
[1442]95  ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
[1849]96      sigc::mem_fun(*this, &MainWin::zoomOut));
[1442]97  ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
[1849]98      sigc::mem_fun(*this, &MainWin::zoomFit));
[1442]99  ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
[1849]100      sigc::mem_fun(*this, &MainWin::zoom100));
[1442]101 
102  ag->add( Gtk::Action::create("ShowMenu", "_Show") );
103  ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
[1838]104           sigc::mem_fun(*this, &MainWin::createMapWin));
[1442]105
[1606]106  Gtk::RadioAction::Group tool_group;
[1634]107  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), "Move"),
[1849]108      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 0) );
[1634]109  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), "Create node"),
[1849]110      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 1) );
[1634]111  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), "Create edge"),
[1849]112      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 2) );
[1634]113  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), "Delete"),
[1849]114      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 3) );
[1634]115
116  ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), "Edit edge map"),
[1849]117      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 4) );
[1634]118  ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), "Edit node map"),
[1849]119      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 5) );
[1634]120
121  ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
[1849]122      sigc::mem_fun ( *this , &MainWin::createNewMapWin ) );
[1502]123
[1442]124  uim=Gtk::UIManager::create();
125  uim->insert_action_group(ag);
126  add_accel_group(uim->get_accel_group());
127
128  try
129  {
130
131    Glib::ustring ui_info =
132      "<ui>"
133      "  <menubar name='MenuBar'>"
134      "    <menu action='FileMenu'>"
[1849]135      "      <menuitem action='FileNewTab'/>"
[1856]136      "      <menuitem action='FileCloseTab'/>"
[1442]137      "      <menuitem action='FileNew'/>"
138      "      <menuitem action='FileOpen'/>"
139      "      <menuitem action='FileSave'/>"
140      "      <menuitem action='FileSaveAs'/>"
[1606]141      "      <menuitem action='Close'/>"
142      "      <menuitem action='Quit'/>"
[1442]143      "    </menu>"
144      "    <menu action='ViewMenu'>"
145      "      <menuitem action='ViewZoomIn' />"
146      "      <menuitem action='ViewZoomOut' />"
147      "      <menuitem action='ViewZoom100' />"
[1777]148      "      <menuitem action='ViewZoomFit' />"
[1442]149      "    </menu>"
150      "    <menu action='ShowMenu'>"
151      "      <menuitem action='ShowMaps'/>"
152      "    </menu>"
153      "  </menubar>"
154      "  <toolbar name='ToolBar'>"
155      "    <toolitem action='FileNew' />"
156      "    <toolitem action='FileOpen' />"
157      "    <toolitem action='FileSave' />"
[1606]158      "    <toolitem action='Close' />"
[1442]159      "    <separator />"
160      "    <toolitem action='ViewZoomIn' />"
161      "    <toolitem action='ViewZoomOut' />"
162      "    <toolitem action='ViewZoom100' />"
[1777]163      "    <toolitem action='ViewZoomFit' />"
[1502]164      "    <separator />"
[1606]165      "    <toolitem action='MoveItem' />"
[1502]166      "    <toolitem action='CreateNode' />"
167      "    <toolitem action='CreateEdge' />"
168      "    <toolitem action='EraseItem' />"
[1550]169      "    <toolitem action='EditEdgeMap' />"
170      "    <toolitem action='EditNodeMap' />"
[1634]171      "    <separator />"
[1592]172      "    <toolitem action='AddMap' />"
[1442]173      "  </toolbar>"
174      "</ui>";
175
176    uim->add_ui_from_string(ui_info);
177
178  }
179  catch(const Glib::Error& ex)
180  {
181    std::cerr << "building menus failed: " <<  ex.what();
182  }
183
184  Gtk::Widget* menubar = uim->get_widget("/MenuBar");
185  if (menubar){
186    vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
187  }
188
189  Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
190  if (toolbar)
191  {
192    static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
193    vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
194  }
195
[1601]196  tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
197  tooltips.enable();
[1442]198
[1849]199  active_tab=-1;
200  notebook.signal_switch_page().connect(sigc::mem_fun(*this, &MainWin::onChangeTab));
201
202  vbox.pack_start(notebook);
203 
[1442]204  show_all_children();
205}
206
[1849]207void MainWin::set_tabtitle(std::string name)
[1616]208{
[1849]209  tabnames[active_tab]=name;
210  set_title(tabnames[active_tab] + " - " + prog_name);
211  notebook.set_tab_label_text((Widget&)*(tabs[active_tab]), tabnames[active_tab]);
212}
213
214void MainWin::readFile(const std::string & filename)
215{
216  newTab();
217  tabs[active_tab]->readFile(filename);
218}
219
220void MainWin::newTab()
221{
222  int size=tabs.size();
223  tabs.resize(size+1);
224  tabnames.resize(size+1);
225  active_tab=size;
226  tabs[active_tab]=new NoteBookTab();
227  tabnames[active_tab]="unsaved file";
228  tabs[active_tab]->signal_title_ch().connect(sigc::mem_fun(*this, &MainWin::set_tabtitle));
229  tabs[active_tab]->signal_newmap_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinAfterSignal));
230  tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
231  notebook.append_page((Gtk::Widget&)(*(tabs[active_tab])));
232  notebook.set_current_page(size);
233  set_tabtitle(tabnames[active_tab]);
234}
235
[1856]236void MainWin::closeTab()
237{
238  if(active_tab!=-1)
239    {
240      //tabs vector will be decreased with the deleted value
241      int size=tabs.size();
242      if(size>1)
243        {
244          for(int i=active_tab+1;i<size;i++)
245            {
246              tabs[i-1]=tabs[i];
247            }
248        }
249
250      //if size==1 resize will delete the only element
251      tabs.resize(size-1);
252
253      int old_active_tab=active_tab;
254      notebook.remove_page(active_tab);
255
256      //If the first tab was active, upon delete notebook
257      //will first switch one tab upper and not lower like
258      //in the case, when not the first tab was active.
259      //But after deletion it will become the first tab,
260      //and this should be registrated in tabs vector,
261      //as well.
262      if(old_active_tab==0)
263        {
264          onChangeTab(NULL,0);
265        }
266
267      //if this was the last page in notebook, there is
268      //no active_tab now
269      if(size==1)
270        {
271          active_tab=-1;
272        }
273    }
274}
275
[1849]276void MainWin::onChangeTab(GtkNotebookPage* page, guint page_num)
277{
278  page=page;
279  active_tab=page_num;
280  tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
281  set_title(tabnames[active_tab]);
282}
283
284void MainWin::onCloseTab()
285{
[1616]286}
287
[1442]288void MainWin::newFile()
289{
[1849]290  if(active_tab!=-1)
[1606]291    {
[1849]292      tabs[active_tab]->newFile();
[1606]293    }
[1442]294}
[1849]295 
[1442]296void MainWin::openFile()
297{
[1849]298  if(active_tab!=-1)
[1606]299    {
[1849]300      tabs[active_tab]->openFile();
[1606]301    }
[1442]302}
[1849]303 
[1442]304void MainWin::saveFile()
305{
[1849]306  if(active_tab!=-1)
307    {
308      tabs[active_tab]->saveFile();
309    }
[1442]310}
[1849]311 
[1442]312void MainWin::saveFileAs()
313{
[1849]314  if(active_tab!=-1)
315    {
316      tabs[active_tab]->saveFileAs();
317    }
[1442]318}
[1849]319 
[1606]320void MainWin::close()
321{
[1849]322  if(active_tab!=-1)
[1606]323    {
[1849]324      tabs[active_tab]->close();
[1838]325    }
[1837]326}
327
[1849]328void MainWin::zoomIn()
[1837]329{
[1849]330  if(active_tab!=-1)
[1838]331    {
[1849]332      tabs[active_tab]->gd_canvas->zoomIn();
333    }
334}
335
336void MainWin::zoomOut()
337{
338  if(active_tab!=-1)
339    {
340      tabs[active_tab]->gd_canvas->zoomOut();
341    }
342}
343
344void MainWin::zoomFit()
345{
346  if(active_tab!=-1)
347    {
348      tabs[active_tab]->gd_canvas->zoomFit();
349    }
350}
351
352void MainWin::zoom100()
353{
354  if(active_tab!=-1)
355    {
356      tabs[active_tab]->gd_canvas->zoom100();
[1838]357    }
[1837]358}
[1838]359
360void MainWin::createMapWin()
361{
[1849]362  if(active_tab!=-1)
[1838]363    {
[1849]364      tabs[active_tab]->createMapWin(tabnames[active_tab]);
[1838]365    }
366}
367
[1849]368void MainWin::changeEditorialTool(int tool)
[1838]369{
[1849]370  active_tool=tool;
371  if(active_tab!=-1)
372    {
373      tabs[active_tab]->gd_canvas->changeEditorialTool(tool);
374    }
[1838]375}
[1849]376
377void MainWin::createNewMapWin()
378{
379  if(active_tab!=-1)
380    {
381      NewMapWin * nmw=new NewMapWin("Create New Map - "+tabnames[active_tab], *(tabs[active_tab]));
382      nmw->show();
383    }
384}
385
386void MainWin::createNewMapWinAfterSignal(NoteBookTab * nbt, bool itisedge)
387{
388  std::vector<NoteBookTab*>::iterator nbti=tabs.begin();
389  int i=0;
390  for(;nbti!=tabs.end();nbti++)
391    {
392      if(*nbti!=nbt)
393        {
394          i++;
395        }
396      else
397        {
398          continue;
399        }
400    }
401  NewMapWin * nmw=new NewMapWin("Create New Map - "+tabnames[i], *nbt, itisedge, false);
402  nmw->run();
403}
Note: See TracBrowser for help on using the repository browser.