COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/main_win.cc @ 1657:754f38f56b49

Last change on this file since 1657:754f38f56b49 was 1657:754f38f56b49, checked in by Akos Ladanyi, 14 years ago

removed the fit to window form the toolbar and the menu since its current implementation is broken

File size: 11.7 KB
Line 
1#include "main_win.h"
2#include "guipixbufs.h"
3
4MainWin::MainWin() :
5  mapwin("Map Setup", mapstorage, gd_canvas),
6  newmapwin("Creating new map", gd_canvas), gd_canvas(mapstorage, mapwin, (Gtk::Window *)this)
7{
8  set_title ("unsaved file - " + prog_name);
9  set_default_size(WIN_WIDTH,WIN_HEIGHT);
10  add(vbox);
11
12
13  // custom icons for the toolbar
14
15  Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
16 
17  Glib::RefPtr<Gdk::Pixbuf> p_move_pixbuf = Gdk::Pixbuf::create_from_inline(
18      2328, gui_icons_move);
19  Glib::RefPtr<Gdk::Pixbuf> p_addnode_pixbuf = Gdk::Pixbuf::create_from_inline(
20      2328, gui_icons_addnode);
21  Glib::RefPtr<Gdk::Pixbuf> p_addlink_pixbuf = Gdk::Pixbuf::create_from_inline(
22      2328, gui_icons_addlink);
23  Glib::RefPtr<Gdk::Pixbuf> p_delete_pixbuf = Gdk::Pixbuf::create_from_inline(
24      2328, gui_icons_delete);
25  Glib::RefPtr<Gdk::Pixbuf> p_editlink_pixbuf = Gdk::Pixbuf::create_from_inline(
26      2328, gui_icons_editlink);
27  Glib::RefPtr<Gdk::Pixbuf> p_editnode_pixbuf = Gdk::Pixbuf::create_from_inline(
28      2328, gui_icons_editnode);
29  Glib::RefPtr<Gdk::Pixbuf> p_newmap_pixbuf = Gdk::Pixbuf::create_from_inline(
30      2328, gui_icons_newmap);
31
32  Gtk::IconSource move_icon_source;
33  move_icon_source.set_pixbuf(p_move_pixbuf);
34  Gtk::IconSet move_icon_set;
35  move_icon_set.add_source(move_icon_source);
36  p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set);
37
38  Gtk::IconSource addnode_icon_source;
39  addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
40  Gtk::IconSet addnode_icon_set;
41  addnode_icon_set.add_source(addnode_icon_source);
42  p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set);
43
44  Gtk::IconSource addlink_icon_source;
45  addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
46  Gtk::IconSet addlink_icon_set;
47  addlink_icon_set.add_source(addlink_icon_source);
48  p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set);
49
50  Gtk::IconSource delete_icon_source;
51  delete_icon_source.set_pixbuf(p_delete_pixbuf);
52  Gtk::IconSet delete_icon_set;
53  delete_icon_set.add_source(delete_icon_source);
54  p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set);
55
56  Gtk::IconSource editlink_icon_source;
57  editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
58  Gtk::IconSet editlink_icon_set;
59  editlink_icon_set.add_source(editlink_icon_source);
60  p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set);
61
62  Gtk::IconSource editnode_icon_source;
63  editnode_icon_source.set_pixbuf(p_editnode_pixbuf);
64  Gtk::IconSet editnode_icon_set;
65  editnode_icon_set.add_source(editnode_icon_source);
66  p_icon_factory->add(Gtk::StockID("gd-editnode"), editnode_icon_set);
67
68  Gtk::IconSource newmap_icon_source;
69  newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
70  Gtk::IconSet newmap_icon_set;
71  newmap_icon_set.add_source(newmap_icon_source);
72  p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set);
73
74  p_icon_factory->add_default();
75 
76
77  ag=Gtk::ActionGroup::create();
78
79  ag->add( Gtk::Action::create("FileMenu", "_File") );
80  ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
81      sigc::mem_fun(*this, &MainWin::newFile));
82  ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
83      sigc::mem_fun(*this, &MainWin::openFile));
84  ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
85      sigc::mem_fun(*this, &MainWin::saveFile));
86  ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
87      sigc::mem_fun(*this, &MainWin::saveFileAs));
88  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
89      sigc::mem_fun(*this, &MainWin::close));
90  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
91      sigc::mem_fun(*this, &MainWin::hide));
92
93  ag->add( Gtk::Action::create("ViewMenu", "_View") );
94  ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
95      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomIn));
96  ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
97      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomOut));
98  ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
99      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomFit));
100  ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
101      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoom100));
102 
103  ag->add( Gtk::Action::create("ShowMenu", "_Show") );
104  ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
105      sigc::mem_fun(this->mapwin, &MapWin::show));
106
107  Gtk::RadioAction::Group tool_group;
108  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), "Move"),
109      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) );
110  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), "Create node"),
111      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 1) );
112  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), "Create edge"),
113      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 2) );
114  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), "Delete"),
115      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 3) );
116
117  ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), "Edit edge map"),
118      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 4) );
119  ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), "Edit node map"),
120      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 5) );
121
122  ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
123      sigc::mem_fun ( this->newmapwin, &NewMapWin::show ) );
124
125  uim=Gtk::UIManager::create();
126  uim->insert_action_group(ag);
127  add_accel_group(uim->get_accel_group());
128
129  /*
130      "      <menuitem action='ViewZoomFit' />"
131      "    <toolitem action='ViewZoomFit' />"
132   */
133
134  try
135  {
136
137    Glib::ustring ui_info =
138      "<ui>"
139      "  <menubar name='MenuBar'>"
140      "    <menu action='FileMenu'>"
141      "      <menuitem action='FileNew'/>"
142      "      <menuitem action='FileOpen'/>"
143      "      <menuitem action='FileSave'/>"
144      "      <menuitem action='FileSaveAs'/>"
145      "      <menuitem action='Close'/>"
146      "      <menuitem action='Quit'/>"
147      "    </menu>"
148      "    <menu action='ViewMenu'>"
149      "      <menuitem action='ViewZoomIn' />"
150      "      <menuitem action='ViewZoomOut' />"
151      "      <menuitem action='ViewZoom100' />"
152      "    </menu>"
153      "    <menu action='ShowMenu'>"
154      "      <menuitem action='ShowMaps'/>"
155      "    </menu>"
156      "  </menubar>"
157      "  <toolbar name='ToolBar'>"
158      "    <toolitem action='FileNew' />"
159      "    <toolitem action='FileOpen' />"
160      "    <toolitem action='FileSave' />"
161      "    <toolitem action='Close' />"
162      "    <separator />"
163      "    <toolitem action='ViewZoomIn' />"
164      "    <toolitem action='ViewZoomOut' />"
165      "    <toolitem action='ViewZoom100' />"
166      "    <separator />"
167      "    <toolitem action='MoveItem' />"
168      "    <toolitem action='CreateNode' />"
169      "    <toolitem action='CreateEdge' />"
170      "    <toolitem action='EraseItem' />"
171      "    <toolitem action='EditEdgeMap' />"
172      "    <toolitem action='EditNodeMap' />"
173      "    <separator />"
174      "    <toolitem action='AddMap' />"
175      "  </toolbar>"
176      "</ui>";
177
178    uim->add_ui_from_string(ui_info);
179
180  }
181  catch(const Glib::Error& ex)
182  {
183    std::cerr << "building menus failed: " <<  ex.what();
184  }
185
186  Gtk::Widget* menubar = uim->get_widget("/MenuBar");
187  if (menubar){
188    vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
189  }
190
191  Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
192  if (toolbar)
193  {
194    static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
195    vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
196  }
197
198  Gtk::ScrolledWindow* pScrolledWindow = manage(new Gtk::ScrolledWindow());
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
208void 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  mapwin.update();
215  set_title(Glib::filename_display_basename(file) + " - " + prog_name);
216}
217
218void MainWin::newFile()
219{
220  if (mapstorage.modified)
221  {
222    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
223        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
224    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
225    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
226    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
227    switch (mdialog.run())
228    {
229      case Gtk::RESPONSE_CANCEL:
230        return;
231      case Gtk::RESPONSE_REJECT:
232        break;
233      case Gtk::RESPONSE_ACCEPT:
234        saveFile();
235        break;
236    }
237  }
238  gd_canvas.clear();
239  mapstorage.clear();
240  mapwin.update();
241  set_title("unsaved file - " + prog_name);
242}
243
244void MainWin::openFile()
245{
246  if (mapstorage.modified)
247  {
248    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
249        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
250    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
251    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
252    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
253    switch (mdialog.run())
254    {
255      case Gtk::RESPONSE_CANCEL:
256        return;
257      case Gtk::RESPONSE_REJECT:
258        break;
259      case Gtk::RESPONSE_ACCEPT:
260        saveFile();
261        break;
262    }
263  }
264  Gtk::FileChooserDialog fcdialog("Open File");
265  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
266  fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
267  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
268  {
269    gd_canvas.clear();
270    mapstorage.clear();
271    Glib::ustring filename = fcdialog.get_filename();
272    if (!mapstorage.readFromFile(filename))
273    {
274      mapstorage.file_name = filename;
275      mapstorage.modified = false;
276      gd_canvas.drawGraph();
277      mapwin.update();
278      set_title(Glib::filename_display_basename(filename) + " - " + prog_name);
279    }
280  }
281}
282
283void MainWin::saveFile()
284{
285  if (mapstorage.file_name == "") {
286    saveFileAs();
287  }
288  else
289  {
290    mapstorage.writeToFile(mapstorage.file_name);
291    mapstorage.modified = false;
292    set_title(Glib::filename_display_basename(mapstorage.file_name) + " - " +
293        prog_name);
294  }
295}
296
297void MainWin::saveFileAs()
298{
299  Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
300  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
301  fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
302  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
303  {
304    Glib::ustring filename = fcdialog.get_filename();
305    mapstorage.file_name = filename;
306    mapstorage.writeToFile(filename);
307    mapstorage.modified = false;
308    set_title(Glib::filename_display_basename(filename) + " - " + prog_name);
309  }
310}
311
312void MainWin::close()
313{
314  if (mapstorage.modified)
315  {
316    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
317        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
318    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
319    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
320    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
321    switch (mdialog.run())
322    {
323      case Gtk::RESPONSE_CANCEL:
324        return;
325      case Gtk::RESPONSE_REJECT:
326        break;
327      case Gtk::RESPONSE_ACCEPT:
328        saveFile();
329        break;
330    }
331  }
332  gd_canvas.clear();
333  mapstorage.clear();
334  mapwin.update();
335  set_title("unsaved file - " + prog_name);
336}
Note: See TracBrowser for help on using the repository browser.