COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/main_win.cc @ 1614:350c1d8bb7cc

Last change on this file since 1614:350c1d8bb7cc was 1614:350c1d8bb7cc, checked in by Hegyi Péter, 14 years ago

Alpar had the key, focus can be set in the window class. But it is not enough, the focused widget has to be activated, as well! Was a hard task to find out... By the way, two compilation warnings are removed.

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