gui/main_win.cc
author alpar
Tue, 16 Aug 2005 16:44:57 +0000
changeset 1630 f67737f5727a
parent 1615 b42385baaf3a
child 1634 910b1bcb7d05
permissions -rw-r--r--
Doc changes:
- True and False got documented
- Graph "developper interface" documentation switched off
- minor fix in graph_to_eps_demo.cc
     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::readFile(const std::string &file)
   137 {
   138   mapstorage.readFromFile(file);
   139   mapstorage.file_name = file;
   140   mapstorage.modified = false;
   141   gd_canvas.drawGraph();
   142   mapwin.update();
   143 }
   144 
   145 void MainWin::newFile()
   146 {
   147   if (mapstorage.modified)
   148   {
   149     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
   150         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   151     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   152     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   153     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   154     switch (mdialog.run())
   155     {
   156       case Gtk::RESPONSE_CANCEL:
   157         return;
   158       case Gtk::RESPONSE_REJECT:
   159         break;
   160       case Gtk::RESPONSE_ACCEPT:
   161         saveFile();
   162         break;
   163     }
   164   }
   165   gd_canvas.clear();
   166   mapstorage.clear();
   167   mapwin.update();
   168 }
   169 
   170 void MainWin::openFile()
   171 {
   172   if (mapstorage.modified)
   173   {
   174     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true, 
   175         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   176     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   177     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   178     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   179     switch (mdialog.run())
   180     {
   181       case Gtk::RESPONSE_CANCEL:
   182         return;
   183       case Gtk::RESPONSE_REJECT:
   184         break;
   185       case Gtk::RESPONSE_ACCEPT:
   186         saveFile();
   187         break;
   188     }
   189   }
   190   gd_canvas.clear();
   191   mapstorage.clear();
   192   Gtk::FileChooserDialog fcdialog("Open File");
   193   fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   194   fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
   195   if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
   196   {
   197     Glib::ustring filename = fcdialog.get_filename();
   198     mapstorage.readFromFile(filename);
   199     mapstorage.file_name = filename;
   200     mapstorage.modified = false;
   201     gd_canvas.drawGraph();
   202     mapwin.update();
   203   }
   204 }
   205 
   206 void MainWin::saveFile()
   207 {
   208   if (mapstorage.file_name == "") {
   209     saveFileAs();
   210   }
   211   else
   212   {
   213     mapstorage.writeToFile(mapstorage.file_name);
   214     mapstorage.modified = false;
   215   }
   216 }
   217 
   218 void MainWin::saveFileAs()
   219 {
   220   Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
   221   fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   222   fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   223   if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
   224   {
   225     Glib::ustring filename = fcdialog.get_filename();
   226     mapstorage.file_name = filename;
   227     mapstorage.writeToFile(filename);
   228     mapstorage.modified = false;
   229   }
   230 }
   231 
   232 void MainWin::close()
   233 {
   234   if (mapstorage.modified)
   235   {
   236     Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
   237         Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   238     mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
   239     mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   240     mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   241     switch (mdialog.run())
   242     {
   243       case Gtk::RESPONSE_CANCEL:
   244         return;
   245       case Gtk::RESPONSE_REJECT:
   246         break;
   247       case Gtk::RESPONSE_ACCEPT:
   248         saveFile();
   249         break;
   250     }
   251   }
   252   gd_canvas.clear();
   253   mapstorage.clear();
   254   mapwin.update();
   255 }