Dijkstra in GUI - and the body...
6 #include "guipixbufs.h"
12 set_title ("no file");
13 set_default_size(WIN_WIDTH,WIN_HEIGHT);
17 // custom icons for the toolbar
18 Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
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_newmap_pixbuf = Gdk::Pixbuf::create_from_inline(
31 2328, gui_icons_newmap);
33 Gtk::IconSource move_icon_source;
34 move_icon_source.set_pixbuf(p_move_pixbuf);
35 Gtk::IconSet move_icon_set;
36 move_icon_set.add_source(move_icon_source);
37 p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set);
39 Gtk::IconSource addnode_icon_source;
40 addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
41 Gtk::IconSet addnode_icon_set;
42 addnode_icon_set.add_source(addnode_icon_source);
43 p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set);
45 Gtk::IconSource addlink_icon_source;
46 addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
47 Gtk::IconSet addlink_icon_set;
48 addlink_icon_set.add_source(addlink_icon_source);
49 p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set);
51 Gtk::IconSource delete_icon_source;
52 delete_icon_source.set_pixbuf(p_delete_pixbuf);
53 Gtk::IconSet delete_icon_set;
54 delete_icon_set.add_source(delete_icon_source);
55 p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set);
57 Gtk::IconSource editlink_icon_source;
58 editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
59 Gtk::IconSet editlink_icon_set;
60 editlink_icon_set.add_source(editlink_icon_source);
61 p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set);
63 Gtk::IconSource newmap_icon_source;
64 newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
65 Gtk::IconSet newmap_icon_set;
66 newmap_icon_set.add_source(newmap_icon_source);
67 p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set);
69 p_icon_factory->add_default();
71 ag=Gtk::ActionGroup::create();
73 ag->add( Gtk::Action::create("FileMenu", _("_File")) );
74 ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
75 sigc::mem_fun(*this, &MainWin::newTab));
76 ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
77 sigc::mem_fun(*this, &MainWin::openFile));
78 ag->add( Gtk::Action::create("FileClearTab", _("Clear Tab")),
79 sigc::mem_fun(*this, &MainWin::newFile));
80 ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
81 sigc::mem_fun(*this, &MainWin::saveFile));
82 ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
83 sigc::mem_fun(*this, &MainWin::saveFileAs));
84 ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
85 sigc::mem_fun(*this, &MainWin::closeTab));
86 ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
87 sigc::mem_fun(*this, &MainWin::hide));
89 ag->add( Gtk::Action::create("ViewMenu", _("_View")) );
90 ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
91 sigc::mem_fun(*this, &MainWin::zoomIn));
92 ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
93 sigc::mem_fun(*this, &MainWin::zoomOut));
94 ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
95 sigc::mem_fun(*this, &MainWin::zoomFit));
96 ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
97 sigc::mem_fun(*this, &MainWin::zoom100));
99 ag->add( Gtk::Action::create("ShowMenu", _("_Show")) );
100 ag->add( Gtk::Action::create("ShowMaps", _("_Maps")),
101 sigc::mem_fun(*this, &MainWin::createMapWin));
102 ag->add( Gtk::Action::create("ShowDesign", _("_Design")),
103 sigc::mem_fun(*this, &MainWin::createDesignWin));
105 ag->add( Gtk::Action::create("AlgoMenu", _("_Algorithms")) );
106 ag->add( Gtk::Action::create("AlgoGeneral", _("_General")),
107 sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 0) );
108 ag->add( Gtk::Action::create("AlgoKruskal", _("_Kruskal")),
109 sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 1) );
110 ag->add( Gtk::Action::create("AlgoDijkstra", _("_Dijkstra")),
111 sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 2) );
113 Gtk::RadioAction::Group tool_group;
114 ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), _("Move")),
115 sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 0) );
116 ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), _("Create node")),
117 sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 1) );
118 ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), _("Create edge")),
119 sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 2) );
120 ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), _("Delete")),
121 sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 3) );
123 ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), _("Edit edge map")),
124 sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 4) );
126 ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
127 sigc::mem_fun ( *this , &MainWin::createNewMapWin ) );
129 ag->add( Gtk::Action::create("DesignGraph", Gtk::Stock::REFRESH),
130 sigc::mem_fun ( *this , &MainWin::reDesignGraph ) );
132 uim=Gtk::UIManager::create();
133 uim->insert_action_group(ag);
134 add_accel_group(uim->get_accel_group());
139 Glib::ustring ui_info =
141 " <menubar name='MenuBar'>"
142 " <menu action='FileMenu'>"
143 " <menuitem action='FileNew'/>"
144 " <menuitem action='FileOpen'/>"
145 " <menuitem action='FileClearTab'/>"
146 " <menuitem action='FileSave'/>"
147 " <menuitem action='FileSaveAs'/>"
148 " <menuitem action='Close'/>"
149 " <menuitem action='Quit'/>"
151 " <menu action='ViewMenu'>"
152 " <menuitem action='ViewZoomIn' />"
153 " <menuitem action='ViewZoomOut' />"
154 " <menuitem action='ViewZoom100' />"
155 " <menuitem action='ViewZoomFit' />"
157 " <menu action='ShowMenu'>"
158 " <menuitem action='ShowMaps'/>"
159 " <menuitem action='ShowDesign'/>"
161 " <menu action='AlgoMenu'>"
162 " <menuitem action='AlgoGeneral'/>"
163 " <menuitem action='AlgoKruskal'/>"
164 " <menuitem action='AlgoDijkstra'/>"
167 " <toolbar name='ToolBar'>"
168 " <toolitem action='FileNew' />"
169 " <toolitem action='FileOpen' />"
170 " <toolitem action='FileSave' />"
171 " <toolitem action='Close' />"
173 " <toolitem action='ViewZoomIn' />"
174 " <toolitem action='ViewZoomOut' />"
175 " <toolitem action='ViewZoom100' />"
176 " <toolitem action='ViewZoomFit' />"
178 " <toolitem action='MoveItem' />"
179 " <toolitem action='CreateNode' />"
180 " <toolitem action='CreateEdge' />"
181 " <toolitem action='EraseItem' />"
182 " <toolitem action='EditEdgeMap' />"
184 " <toolitem action='AddMap' />"
185 " <toolitem action='DesignGraph' />"
189 uim->add_ui_from_string(ui_info);
192 catch(const Glib::Error& ex)
194 std::cerr << "building menus failed: " << ex.what();
197 Gtk::Widget* menubar = uim->get_widget("/MenuBar");
199 //vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
200 table.attach(*menubar, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
203 Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
206 static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
207 //hbox.pack_start(*toolbar, Gtk::PACK_EXPAND_WIDGET);
209 table.attach(*toolbar, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
213 table2.set_row_spacings(10);
214 table2.set_col_spacings(5);
216 auto_scale = new Gtk::CheckButton("Autoscale");
217 auto_scale->set_active(false);
218 auto_scale->signal_toggled().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
219 table2.attach(*auto_scale, 0,2,0,1);
221 Gtk::Label * width_label= new Gtk::Label("Edge Width:");
222 table2.attach(*width_label, 0,1,1,2);
224 Gtk::Adjustment * adjustment_width=new Gtk::Adjustment(20, 1, 200, 5, 10);
226 edge_width = new Gtk::SpinButton(*adjustment_width, 5,0);
227 edge_width->signal_value_changed().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
228 table2.attach(*edge_width, 1,2,1,2);
230 Gtk::Label * radius_label= new Gtk::Label("Node Radius:");
231 table2.attach(*radius_label, 2,3,1,2);
233 Gtk::Adjustment * adjustment_radius=new Gtk::Adjustment(20, 0, 500, 5, 10);
235 radius_size = new Gtk::SpinButton(*adjustment_radius, 5,0);
236 radius_size->signal_value_changed().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
237 table2.attach(*radius_size, 3,4,1,2);
239 zoom_track = new Gtk::CheckButton("Zoom tracking");
240 zoom_track->set_active(false);
241 zoom_track->signal_toggled().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
242 table2.attach(*zoom_track, 2,4,0,1);
245 table.attach(table2, 1, 2, 0, 2, Gtk::SHRINK, Gtk::SHRINK);
247 tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
251 notebook.signal_switch_page().connect(sigc::mem_fun(*this, &MainWin::onChangeTab));
255 table.attach(notebook,0,2,2,3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL);
260 void MainWin::set_tabtitle(std::string name)
262 if(strinst.find(name)==strinst.end())
264 tabnames[active_tab]=name;
270 std::ostringstream o;
272 tabnames[active_tab]=name+" - "+o.str();
274 set_title(tabnames[active_tab] + " - " + prog_name);
275 notebook.set_tab_label_text((Widget&)*(tabs[active_tab]), tabnames[active_tab]);
279 void MainWin::readFile(const std::string & filename)
282 tabs[active_tab]->readFile(filename);
285 void MainWin::newTab()
287 int size=tabs.size();
289 tabnames.resize(size+1);
291 tabs[active_tab]=new NoteBookTab();
292 tabs[active_tab]->signal_title_ch().connect(sigc::mem_fun(*this, &MainWin::set_tabtitle));
293 tabs[active_tab]->signal_newmap_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinAfterSignal));
294 tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
295 notebook.append_page((Gtk::Widget&)(*(tabs[active_tab])));
296 notebook.set_current_page(size);
297 set_tabtitle(_("unsaved file"));
301 void MainWin::closeTab()
305 if (tabs[active_tab]->mapstorage.modified)
307 Gtk::MessageDialog mdialog(_("<b>Save changes before closing?</b>"), true,
308 Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
309 mdialog.add_button(_("Close file _without Saving"), Gtk::RESPONSE_REJECT);
310 mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
311 mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
312 switch (mdialog.run())
314 case Gtk::RESPONSE_CANCEL:
316 case Gtk::RESPONSE_REJECT:
318 case Gtk::RESPONSE_ACCEPT:
319 tabs[active_tab]->saveFile();
323 //tabs vector will be decreased with the deleted value
324 int size=tabs.size();
327 for(int i=active_tab+1;i<size;i++)
329 tabnames[i-1]=tabnames[i];
333 //if size==1 resize will delete the only element
335 tabnames.resize(size-1);
337 int old_active_tab=active_tab;
338 notebook.remove_page(active_tab);
340 //If the first tab was active, upon delete notebook
341 //will first switch one tab upper and not lower like
342 //in the case, when not the first tab was active.
343 //But after deletion it will become the first tab,
344 //and this should be registrated in tabs vector,
346 if((old_active_tab==0)&&(size!=1))
351 //if this was the last page in notebook, there is
362 void MainWin::onChangeTab(GtkNotebookPage* page, guint page_num)
366 tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
367 set_title(tabnames[active_tab]);
372 tabs[active_tab]->getView(autoscale, zoomtrack, width, radius);
373 edge_width->set_value(width);
374 radius_size->set_value(radius);
375 zoom_track->set_active(zoomtrack);
376 auto_scale->set_active(autoscale);
380 void MainWin::newFile()
384 tabs[active_tab]->newFile();
388 void MainWin::openFile()
394 tabs[active_tab]->openFile();
397 void MainWin::saveFile()
401 tabs[active_tab]->saveFile();
405 void MainWin::saveFileAs()
409 tabs[active_tab]->saveFileAs();
413 void MainWin::close()
417 tabs[active_tab]->close();
421 void MainWin::zoomIn()
425 tabs[active_tab]->gd_canvas->zoomIn();
429 void MainWin::zoomOut()
433 tabs[active_tab]->gd_canvas->zoomOut();
437 void MainWin::zoomFit()
441 tabs[active_tab]->gd_canvas->zoomFit();
445 void MainWin::zoom100()
449 tabs[active_tab]->gd_canvas->zoom100();
453 void MainWin::createMapWin()
457 tabs[active_tab]->createMapWin(tabnames[active_tab]);
461 void MainWin::createDesignWin()
465 tabs[active_tab]->createDesignWin(tabnames[active_tab]);
469 void MainWin::createAlgoWin(int algoid)
471 AlgoWin * aw=new AlgoWin(algoid, tabnames);
472 aw->signal_closing().connect(sigc::mem_fun(*this, &MainWin::deRegisterAlgoWin));
473 aw->signal_maplist_needed().connect(sigc::mem_fun(*this, &MainWin::updateAlgoWinMaps));
474 aw->signal_newmapwin_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinTabString));
479 void MainWin::updateAlgoWinTabs()
481 std::set< AlgoWin* >::iterator awsi=aws.begin();
482 for(;awsi!=aws.end();awsi++)
484 (*awsi)->update_tablist(tabnames);
488 void MainWin::updateAlgoWinMaps(AlgoWin * awp, std::string tabname)
491 for(;(i<(int)tabnames.size())&&(tabnames[i]!=tabname);i++)
494 awp->update_maplist(&(tabs[i]->mapstorage));
497 void MainWin::deRegisterAlgoWin(AlgoWin * awp)
502 void MainWin::changeEditorialTool(int tool)
507 tabs[active_tab]->gd_canvas->changeEditorialTool(tool);
511 void MainWin::createNewMapWin()
515 NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[active_tab], *(tabs[active_tab]));
520 void MainWin::createNewMapWinTabString(std::string tabname, bool itisedge)
523 for(;((i<(int)tabnames.size())&&(tabnames[i]!=tabname));i++)
526 createNewMapWinAfterSignal(tabs[i], itisedge);
529 void MainWin::createNewMapWinAfterSignal(NoteBookTab * nbt, bool itisedge)
531 std::vector<NoteBookTab*>::iterator nbti=tabs.begin();
533 for(;nbti!=tabs.end();nbti++)
544 NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[i], *nbt, itisedge, false);
549 void MainWin::nodeViewChanged()
551 double width=edge_width->get_value();
552 double radius=radius_size->get_value();
553 bool zoomtrack=zoom_track->get_active();
554 bool autoscale=auto_scale->get_active();
555 tabs[active_tab]->setView(autoscale, zoomtrack, width, radius);
558 void MainWin::reDesignGraph()
560 tabs[active_tab]->reDesignGraph();