COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/main_win.cc @ 1634:910b1bcb7d05

Last change on this file since 1634:910b1bcb7d05 was 1634:910b1bcb7d05, checked in by Akos Ladanyi, 15 years ago

Toolbar has custom icons now. Works only when installed.

File size: 11.5 KB
Line 
1#include "main_win.h"
2
3MainWin::MainWin(const std::string& title) :
4  mapwin("Map Setup", mapstorage, gd_canvas),
5  newmapwin("Creating new map", gd_canvas), gd_canvas(mapstorage, mapwin, (Gtk::Window *)this)
6{
7  set_title (title);
8  set_default_size(WIN_WIDTH,WIN_HEIGHT);
9  add(vbox);
10
11
12  // custom icons for the toolbar
13
14  Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
15 
16  std::vector<std::string> icon_path;
17  icon_path.push_back(DATADIR);
18  icon_path.push_back("pixmaps");
19  icon_path.push_back("gd");
20
21  std::vector<std::string> move_icon_path(icon_path);
22  move_icon_path.push_back("move.png");
23  Gtk::IconSource move_icon_source;
24  move_icon_source.set_filename(Glib::build_filename(move_icon_path));
25  Gtk::IconSet move_icon_set;
26  move_icon_set.add_source(move_icon_source);
27  p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set);
28
29  std::vector<std::string> addnode_icon_path(icon_path);
30  addnode_icon_path.push_back("addnode.png");
31  Gtk::IconSource addnode_icon_source;
32  addnode_icon_source.set_filename(Glib::build_filename(addnode_icon_path));
33  Gtk::IconSet addnode_icon_set;
34  addnode_icon_set.add_source(addnode_icon_source);
35  p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set);
36
37  std::vector<std::string> addlink_icon_path(icon_path);
38  addlink_icon_path.push_back("addlink.png");
39  Gtk::IconSource addlink_icon_source;
40  addlink_icon_source.set_filename(Glib::build_filename(addlink_icon_path));
41  Gtk::IconSet addlink_icon_set;
42  addlink_icon_set.add_source(addlink_icon_source);
43  p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set);
44
45  std::vector<std::string> delete_icon_path(icon_path);
46  delete_icon_path.push_back("delete.png");
47  Gtk::IconSource delete_icon_source;
48  delete_icon_source.set_filename(Glib::build_filename(delete_icon_path));
49  Gtk::IconSet delete_icon_set;
50  delete_icon_set.add_source(delete_icon_source);
51  p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set);
52
53  std::vector<std::string> editlink_icon_path(icon_path);
54  editlink_icon_path.push_back("editlink.png");
55  Gtk::IconSource editlink_icon_source;
56  editlink_icon_source.set_filename(Glib::build_filename(editlink_icon_path));
57  Gtk::IconSet editlink_icon_set;
58  editlink_icon_set.add_source(editlink_icon_source);
59  p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set);
60
61  std::vector<std::string> editnode_icon_path(icon_path);
62  editnode_icon_path.push_back("editnode.png");
63  Gtk::IconSource editnode_icon_source;
64  editnode_icon_source.set_filename(Glib::build_filename(editnode_icon_path));
65  Gtk::IconSet editnode_icon_set;
66  editnode_icon_set.add_source(editnode_icon_source);
67  p_icon_factory->add(Gtk::StockID("gd-editnode"), editnode_icon_set);
68
69  std::vector<std::string> newmap_icon_path(icon_path);
70  newmap_icon_path.push_back("newmap.png");
71  Gtk::IconSource newmap_icon_source;
72  newmap_icon_source.set_filename(Glib::build_filename(newmap_icon_path));
73  Gtk::IconSet newmap_icon_set;
74  newmap_icon_set.add_source(newmap_icon_source);
75  p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set);
76
77  p_icon_factory->add_default();
78 
79
80  ag=Gtk::ActionGroup::create();
81
82  ag->add( Gtk::Action::create("FileMenu", "_File") );
83  ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
84      sigc::mem_fun(*this, &MainWin::newFile));
85  ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
86      sigc::mem_fun(*this, &MainWin::openFile));
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));
91  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
92      sigc::mem_fun(*this, &MainWin::close));
93  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
94      sigc::mem_fun(*this, &MainWin::hide));
95
96  ag->add( Gtk::Action::create("ViewMenu", "_View") );
97  ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
98      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomIn));
99  ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
100      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomOut));
101  ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
102      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomFit));
103  ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
104      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoom100));
105 
106  ag->add( Gtk::Action::create("ShowMenu", "_Show") );
107  ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
108      sigc::mem_fun(this->mapwin, &MapWin::show));
109
110  Gtk::RadioAction::Group tool_group;
111  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), "Move"),
112      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) );
113  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), "Create node"),
114      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 1) );
115  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), "Create edge"),
116      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 2) );
117  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), "Delete"),
118      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 3) );
119
120  ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), "Edit edge map"),
121      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 4) );
122  ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), "Edit node map"),
123      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 5) );
124
125  ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
126      sigc::mem_fun ( this->newmapwin, &NewMapWin::show ) );
127
128  uim=Gtk::UIManager::create();
129  uim->insert_action_group(ag);
130  add_accel_group(uim->get_accel_group());
131
132  try
133  {
134
135    Glib::ustring ui_info =
136      "<ui>"
137      "  <menubar name='MenuBar'>"
138      "    <menu action='FileMenu'>"
139      "      <menuitem action='FileNew'/>"
140      "      <menuitem action='FileOpen'/>"
141      "      <menuitem action='FileSave'/>"
142      "      <menuitem action='FileSaveAs'/>"
143      "      <menuitem action='Close'/>"
144      "      <menuitem action='Quit'/>"
145      "    </menu>"
146      "    <menu action='ViewMenu'>"
147      "      <menuitem action='ViewZoomIn' />"
148      "      <menuitem action='ViewZoomOut' />"
149      "      <menuitem action='ViewZoomFit' />"
150      "      <menuitem action='ViewZoom100' />"
151      "    </menu>"
152      "    <menu action='ShowMenu'>"
153      "      <menuitem action='ShowMaps'/>"
154      "    </menu>"
155      "  </menubar>"
156      "  <toolbar name='ToolBar'>"
157      "    <toolitem action='FileNew' />"
158      "    <toolitem action='FileOpen' />"
159      "    <toolitem action='FileSave' />"
160      "    <toolitem action='Close' />"
161      "    <separator />"
162      "    <toolitem action='ViewZoomIn' />"
163      "    <toolitem action='ViewZoomOut' />"
164      "    <toolitem action='ViewZoomFit' />"
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}
216
217void MainWin::newFile()
218{
219  if (mapstorage.modified)
220  {
221    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
222        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
223    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
224    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
225    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
226    switch (mdialog.run())
227    {
228      case Gtk::RESPONSE_CANCEL:
229        return;
230      case Gtk::RESPONSE_REJECT:
231        break;
232      case Gtk::RESPONSE_ACCEPT:
233        saveFile();
234        break;
235    }
236  }
237  gd_canvas.clear();
238  mapstorage.clear();
239  mapwin.update();
240}
241
242void MainWin::openFile()
243{
244  if (mapstorage.modified)
245  {
246    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
247        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
248    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
249    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
250    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
251    switch (mdialog.run())
252    {
253      case Gtk::RESPONSE_CANCEL:
254        return;
255      case Gtk::RESPONSE_REJECT:
256        break;
257      case Gtk::RESPONSE_ACCEPT:
258        saveFile();
259        break;
260    }
261  }
262  gd_canvas.clear();
263  mapstorage.clear();
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    Glib::ustring filename = fcdialog.get_filename();
270    mapstorage.readFromFile(filename);
271    mapstorage.file_name = filename;
272    mapstorage.modified = false;
273    gd_canvas.drawGraph();
274    mapwin.update();
275  }
276}
277
278void MainWin::saveFile()
279{
280  if (mapstorage.file_name == "") {
281    saveFileAs();
282  }
283  else
284  {
285    mapstorage.writeToFile(mapstorage.file_name);
286    mapstorage.modified = false;
287  }
288}
289
290void MainWin::saveFileAs()
291{
292  Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
293  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
294  fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
295  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
296  {
297    Glib::ustring filename = fcdialog.get_filename();
298    mapstorage.file_name = filename;
299    mapstorage.writeToFile(filename);
300    mapstorage.modified = false;
301  }
302}
303
304void MainWin::close()
305{
306  if (mapstorage.modified)
307  {
308    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
309        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
310    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
311    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
312    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
313    switch (mdialog.run())
314    {
315      case Gtk::RESPONSE_CANCEL:
316        return;
317      case Gtk::RESPONSE_REJECT:
318        break;
319      case Gtk::RESPONSE_ACCEPT:
320        saveFile();
321        break;
322    }
323  }
324  gd_canvas.clear();
325  mapstorage.clear();
326  mapwin.update();
327}
Note: See TracBrowser for help on using the repository browser.