gui/main_win.cc
author ladanyi
Sun, 07 Aug 2005 16:35:15 +0000
changeset 1615 b42385baaf3a
parent 1614 350c1d8bb7cc
child 1616 78704393218d
permissions -rw-r--r--
compile with gtkmm 2.4
     1 #include "main_win.h"
     2 
     3 MainWin::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 
   136 void MainWin::newFile()
   137 {
   138   if (mapstorage.modified)
   139   {
   140     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
   141         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   142     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   143     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   144     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   145     switch (mdialog.run())
   146     {
   147       case Gtk::RESPONSE_CANCEL:
   148         return;
   149       case Gtk::RESPONSE_REJECT:
   150         break;
   151       case Gtk::RESPONSE_ACCEPT:
   152         saveFile();
   153         break;
   154     }
   155   }
   156   gd_canvas.clear();
   157   mapstorage.clear();
   158   mapwin.update();
   159 }
   160 
   161 void MainWin::openFile()
   162 {
   163   if (mapstorage.modified)
   164   {
   165     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true, 
   166         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   167     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   168     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   169     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   170     switch (mdialog.run())
   171     {
   172       case Gtk::RESPONSE_CANCEL:
   173         return;
   174       case Gtk::RESPONSE_REJECT:
   175         break;
   176       case Gtk::RESPONSE_ACCEPT:
   177         saveFile();
   178         break;
   179     }
   180   }
   181   gd_canvas.clear();
   182   mapstorage.clear();
   183   Gtk::FileChooserDialog fcdialog("Open File");
   184   fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   185   fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
   186   if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
   187   {
   188     Glib::ustring filename = fcdialog.get_filename();
   189     mapstorage.readFromFile(filename);
   190     mapstorage.file_name = filename;
   191     mapstorage.modified = false;
   192     gd_canvas.drawGraph();
   193     mapwin.update();
   194   }
   195 }
   196 
   197 void MainWin::saveFile()
   198 {
   199   if (mapstorage.file_name == "") {
   200     saveFileAs();
   201   }
   202   else
   203   {
   204     mapstorage.writeToFile(mapstorage.file_name);
   205     mapstorage.modified = false;
   206   }
   207 }
   208 
   209 void MainWin::saveFileAs()
   210 {
   211   Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
   212   fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   213   fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   214   if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
   215   {
   216     Glib::ustring filename = fcdialog.get_filename();
   217     mapstorage.file_name = filename;
   218     mapstorage.writeToFile(filename);
   219     mapstorage.modified = false;
   220   }
   221 }
   222 
   223 void MainWin::close()
   224 {
   225   if (mapstorage.modified)
   226   {
   227     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
   228         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   229     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   230     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   231     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   232     switch (mdialog.run())
   233     {
   234       case Gtk::RESPONSE_CANCEL:
   235         return;
   236       case Gtk::RESPONSE_REJECT:
   237         break;
   238       case Gtk::RESPONSE_ACCEPT:
   239         saveFile();
   240         break;
   241     }
   242   }
   243   gd_canvas.clear();
   244   mapstorage.clear();
   245   mapwin.update();
   246 }