#include "main_win.h" #include "icons/guipixbufs.h" MainWin::MainWin() : mapwin("Map Setup", mapstorage, gd_canvas), newmapwin("Creating new map", gd_canvas), gd_canvas(mapstorage, mapwin, (Gtk::Window *)this) { set_title ("unsaved file - " + prog_name); set_default_size(WIN_WIDTH,WIN_HEIGHT); add(vbox); // custom icons for the toolbar Glib::RefPtr p_icon_factory = Gtk::IconFactory::create(); Glib::RefPtr p_move_pixbuf = Gdk::Pixbuf::create_from_inline( 2328, gui_icons_move); Glib::RefPtr p_addnode_pixbuf = Gdk::Pixbuf::create_from_inline( 2328, gui_icons_addnode); Glib::RefPtr p_addlink_pixbuf = Gdk::Pixbuf::create_from_inline( 2328, gui_icons_addlink); Glib::RefPtr p_delete_pixbuf = Gdk::Pixbuf::create_from_inline( 2328, gui_icons_delete); Glib::RefPtr p_editlink_pixbuf = Gdk::Pixbuf::create_from_inline( 2328, gui_icons_editlink); Glib::RefPtr p_editnode_pixbuf = Gdk::Pixbuf::create_from_inline( 2328, gui_icons_editnode); Glib::RefPtr p_newmap_pixbuf = Gdk::Pixbuf::create_from_inline( 2328, gui_icons_newmap); Gtk::IconSource move_icon_source; move_icon_source.set_pixbuf(p_move_pixbuf); Gtk::IconSet move_icon_set; move_icon_set.add_source(move_icon_source); p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set); Gtk::IconSource addnode_icon_source; addnode_icon_source.set_pixbuf(p_addnode_pixbuf); Gtk::IconSet addnode_icon_set; addnode_icon_set.add_source(addnode_icon_source); p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set); Gtk::IconSource addlink_icon_source; addlink_icon_source.set_pixbuf(p_addlink_pixbuf); Gtk::IconSet addlink_icon_set; addlink_icon_set.add_source(addlink_icon_source); p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set); Gtk::IconSource delete_icon_source; delete_icon_source.set_pixbuf(p_delete_pixbuf); Gtk::IconSet delete_icon_set; delete_icon_set.add_source(delete_icon_source); p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set); Gtk::IconSource editlink_icon_source; editlink_icon_source.set_pixbuf(p_editlink_pixbuf); Gtk::IconSet editlink_icon_set; editlink_icon_set.add_source(editlink_icon_source); p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set); Gtk::IconSource editnode_icon_source; editnode_icon_source.set_pixbuf(p_editnode_pixbuf); Gtk::IconSet editnode_icon_set; editnode_icon_set.add_source(editnode_icon_source); p_icon_factory->add(Gtk::StockID("gd-editnode"), editnode_icon_set); Gtk::IconSource newmap_icon_source; newmap_icon_source.set_pixbuf(p_newmap_pixbuf); Gtk::IconSet newmap_icon_set; newmap_icon_set.add_source(newmap_icon_source); p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set); p_icon_factory->add_default(); ag=Gtk::ActionGroup::create(); ag->add( Gtk::Action::create("FileMenu", "_File") ); ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW), sigc::mem_fun(*this, &MainWin::newFile)); ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN), sigc::mem_fun(*this, &MainWin::openFile)); ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE), sigc::mem_fun(*this, &MainWin::saveFile)); ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS), sigc::mem_fun(*this, &MainWin::saveFileAs)); ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE), sigc::mem_fun(*this, &MainWin::close)); ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT), sigc::mem_fun(*this, &MainWin::hide)); ag->add( Gtk::Action::create("ViewMenu", "_View") ); ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN), sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomIn)); ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT), sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomOut)); ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT), sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomFit)); ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100), sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoom100)); ag->add( Gtk::Action::create("ShowMenu", "_Show") ); ag->add( Gtk::Action::create("ShowMaps", "_Maps"), sigc::mem_fun(this->mapwin, &MapWin::show)); Gtk::RadioAction::Group tool_group; ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), "Move"), sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) ); ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), "Create node"), sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 1) ); ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), "Create edge"), sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 2) ); ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), "Delete"), sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 3) ); ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), "Edit edge map"), sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 4) ); ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), "Edit node map"), sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 5) ); ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")), sigc::mem_fun ( this->newmapwin, &NewMapWin::show ) ); uim=Gtk::UIManager::create(); uim->insert_action_group(ag); add_accel_group(uim->get_accel_group()); /* " " " " */ try { Glib::ustring ui_info = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; uim->add_ui_from_string(ui_info); } catch(const Glib::Error& ex) { std::cerr << "building menus failed: " << ex.what(); } Gtk::Widget* menubar = uim->get_widget("/MenuBar"); if (menubar){ vbox.pack_start(*menubar, Gtk::PACK_SHRINK); } Gtk::Widget* toolbar = uim->get_widget("/ToolBar"); if (toolbar) { static_cast(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS); vbox.pack_start(*toolbar, Gtk::PACK_SHRINK); } Gtk::ScrolledWindow* pScrolledWindow = manage(new Gtk::ScrolledWindow()); pScrolledWindow->add(gd_canvas); vbox.pack_start(*pScrolledWindow); tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node"); tooltips.enable(); show_all_children(); } void MainWin::readFile(const std::string &file) { mapstorage.readFromFile(file); mapstorage.file_name = file; mapstorage.modified = false; gd_canvas.drawGraph(); mapwin.update(); set_title(Glib::filename_display_basename(file) + " - " + prog_name); } void MainWin::newFile() { if (mapstorage.modified) { Gtk::MessageDialog mdialog("Save changes before closing?", true, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT); mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT); switch (mdialog.run()) { case Gtk::RESPONSE_CANCEL: return; case Gtk::RESPONSE_REJECT: break; case Gtk::RESPONSE_ACCEPT: saveFile(); break; } } gd_canvas.clear(); mapstorage.clear(); mapwin.update(); set_title("unsaved file - " + prog_name); } void MainWin::openFile() { if (mapstorage.modified) { Gtk::MessageDialog mdialog("Save changes before closing?", true, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT); mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT); switch (mdialog.run()) { case Gtk::RESPONSE_CANCEL: return; case Gtk::RESPONSE_REJECT: break; case Gtk::RESPONSE_ACCEPT: saveFile(); break; } } Gtk::FileChooserDialog fcdialog("Open File"); fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT); if (fcdialog.run() == Gtk::RESPONSE_ACCEPT) { gd_canvas.clear(); mapstorage.clear(); Glib::ustring filename = fcdialog.get_filename(); if (!mapstorage.readFromFile(filename)) { mapstorage.file_name = filename; mapstorage.modified = false; gd_canvas.drawGraph(); mapwin.update(); set_title(Glib::filename_display_basename(filename) + " - " + prog_name); } } } void MainWin::saveFile() { if (mapstorage.file_name == "") { saveFileAs(); } else { mapstorage.writeToFile(mapstorage.file_name); mapstorage.modified = false; set_title(Glib::filename_display_basename(mapstorage.file_name) + " - " + prog_name); } } void MainWin::saveFileAs() { Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE); fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT); if (fcdialog.run() == Gtk::RESPONSE_ACCEPT) { Glib::ustring filename = fcdialog.get_filename(); mapstorage.file_name = filename; mapstorage.writeToFile(filename); mapstorage.modified = false; set_title(Glib::filename_display_basename(filename) + " - " + prog_name); } } void MainWin::close() { if (mapstorage.modified) { Gtk::MessageDialog mdialog("Save changes before closing?", true, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT); mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT); switch (mdialog.run()) { case Gtk::RESPONSE_CANCEL: return; case Gtk::RESPONSE_REJECT: break; case Gtk::RESPONSE_ACCEPT: saveFile(); break; } } gd_canvas.clear(); mapstorage.clear(); mapwin.update(); set_title("unsaved file - " + prog_name); }