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