COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/main_win.cc @ 1606:dc4ea2010dee

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

added support for saving files

File size: 8.4 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)
6{
7
8  set_title (title);
9  set_default_size(WIN_WIDTH,WIN_HEIGHT);
10  add(vbox);
11
12  ag=Gtk::ActionGroup::create();
13
14  ag->add( Gtk::Action::create("FileMenu", "_File") );
15  ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
16      sigc::mem_fun(*this, &MainWin::newFile));
17  ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
18      sigc::mem_fun(*this, &MainWin::openFile));
19  ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
20      sigc::mem_fun(*this, &MainWin::saveFile));
21  ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
22      sigc::mem_fun(*this, &MainWin::saveFileAs));
23  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
24      sigc::mem_fun(*this, &MainWin::close));
25  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
26      sigc::mem_fun(*this, &MainWin::hide));
27
28  ag->add( Gtk::Action::create("ViewMenu", "_View") );
29  ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
30      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomIn));
31  ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
32      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomOut));
33  ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
34      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomFit));
35  ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
36      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoom100));
37 
38  ag->add( Gtk::Action::create("ShowMenu", "_Show") );
39  ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
40      sigc::mem_fun(this->mapwin, &MapWin::show));
41
42  Gtk::RadioAction::Group tool_group;
43  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::Stock::CONVERT, "Move"),
44      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) );
45  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::Stock::NO, "Create node"),
46      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 1) );
47  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::Stock::REMOVE, "Create edge"),
48      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 2) );
49  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::Stock::DELETE, "Delete"),
50      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 3) );
51  ag->add( Gtk::Action::create("EditEdgeMap", Gtk::Stock::PROPERTIES),
52      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 4) );
53  ag->add( Gtk::Action::create("EditNodeMap", Gtk::Stock::PREFERENCES),
54      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 5) );
55  ag->add( Gtk::Action::create("AddMap", Gtk::Stock::NEW),
56      sigc::mem_fun ( this->newmapwin, &NewMapWin::show ) );
57
58  uim=Gtk::UIManager::create();
59  uim->insert_action_group(ag);
60  add_accel_group(uim->get_accel_group());
61
62  try
63  {
64
65    Glib::ustring ui_info =
66      "<ui>"
67      "  <menubar name='MenuBar'>"
68      "    <menu action='FileMenu'>"
69      "      <menuitem action='FileNew'/>"
70      "      <menuitem action='FileOpen'/>"
71      "      <menuitem action='FileSave'/>"
72      "      <menuitem action='FileSaveAs'/>"
73      "      <menuitem action='Close'/>"
74      "      <menuitem action='Quit'/>"
75      "    </menu>"
76      "    <menu action='ViewMenu'>"
77      "      <menuitem action='ViewZoomIn' />"
78      "      <menuitem action='ViewZoomOut' />"
79      "      <menuitem action='ViewZoomFit' />"
80      "      <menuitem action='ViewZoom100' />"
81      "    </menu>"
82      "    <menu action='ShowMenu'>"
83      "      <menuitem action='ShowMaps'/>"
84      "    </menu>"
85      "  </menubar>"
86      "  <toolbar name='ToolBar'>"
87      "    <toolitem action='FileNew' />"
88      "    <toolitem action='FileOpen' />"
89      "    <toolitem action='FileSave' />"
90      "    <toolitem action='Close' />"
91      "    <separator />"
92      "    <toolitem action='ViewZoomIn' />"
93      "    <toolitem action='ViewZoomOut' />"
94      "    <toolitem action='ViewZoomFit' />"
95      "    <toolitem action='ViewZoom100' />"
96      "    <separator />"
97      "    <toolitem action='MoveItem' />"
98      "    <toolitem action='CreateNode' />"
99      "    <toolitem action='CreateEdge' />"
100      "    <toolitem action='EraseItem' />"
101      "    <toolitem action='EditEdgeMap' />"
102      "    <toolitem action='EditNodeMap' />"
103      "    <toolitem action='AddMap' />"
104      "  </toolbar>"
105      "</ui>";
106
107    uim->add_ui_from_string(ui_info);
108
109  }
110  catch(const Glib::Error& ex)
111  {
112    std::cerr << "building menus failed: " <<  ex.what();
113  }
114
115  Gtk::Widget* menubar = uim->get_widget("/MenuBar");
116  if (menubar){
117    vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
118  }
119
120  Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
121  if (toolbar)
122  {
123    static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
124    vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
125  }
126
127  Gtk::ScrolledWindow* pScrolledWindow = manage(new Gtk::ScrolledWindow());
128  pScrolledWindow->add(gd_canvas);
129  vbox.pack_start(*pScrolledWindow);
130
131  tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
132  tooltips.enable();
133
134  show_all_children();
135}
136
137void MainWin::newFile()
138{
139  if (mapstorage.modified)
140  {
141    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
142        Gtk::BUTTONS_NONE);
143    mdialog.set_message("<b>Save changes before closing?</b>", true);
144    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
145    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
146    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
147    switch (mdialog.run())
148    {
149      case Gtk::RESPONSE_CANCEL:
150        return;
151      case Gtk::RESPONSE_REJECT:
152        break;
153      case Gtk::RESPONSE_ACCEPT:
154        saveFile();
155        break;
156    }
157  }
158  gd_canvas.clear();
159  mapstorage.clear();
160  mapwin.update();
161}
162
163void MainWin::openFile()
164{
165  if (mapstorage.modified)
166  {
167    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
168        Gtk::BUTTONS_NONE);
169    mdialog.set_message("<b>Save changes before closing?</b>", true);
170    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
171    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
172    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
173    switch (mdialog.run())
174    {
175      case Gtk::RESPONSE_CANCEL:
176        return;
177      case Gtk::RESPONSE_REJECT:
178        break;
179      case Gtk::RESPONSE_ACCEPT:
180        saveFile();
181        break;
182    }
183  }
184  gd_canvas.clear();
185  mapstorage.clear();
186  Gtk::FileChooserDialog fcdialog("Open File");
187  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
188  fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
189  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
190  {
191    Glib::ustring filename = fcdialog.get_filename();
192    mapstorage.readFromFile(filename);
193    mapstorage.file_name = filename;
194    mapstorage.modified = false;
195    gd_canvas.drawGraph();
196    mapwin.update();
197  }
198}
199
200void MainWin::saveFile()
201{
202  if (mapstorage.file_name == "") {
203    saveFileAs();
204  }
205  else
206  {
207    mapstorage.writeToFile(mapstorage.file_name);
208    mapstorage.modified = false;
209  }
210}
211
212void MainWin::saveFileAs()
213{
214  Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
215  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
216  fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
217  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
218  {
219    Glib::ustring filename = fcdialog.get_filename();
220    mapstorage.file_name = filename;
221    mapstorage.writeToFile(filename);
222    mapstorage.modified = false;
223  }
224}
225
226void MainWin::close()
227{
228  if (mapstorage.modified)
229  {
230    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
231        Gtk::BUTTONS_NONE);
232    mdialog.set_message("<b>Save changes before closing?</b>", true);
233    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
234    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
235    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
236    switch (mdialog.run())
237    {
238      case Gtk::RESPONSE_CANCEL:
239        return;
240      case Gtk::RESPONSE_REJECT:
241        break;
242      case Gtk::RESPONSE_ACCEPT:
243        saveFile();
244        break;
245    }
246  }
247  gd_canvas.clear();
248  mapstorage.clear();
249  mapwin.update();
250}
Note: See TracBrowser for help on using the repository browser.