COIN-OR::LEMON - Graph Library

Changeset 1606:dc4ea2010dee in lemon-0.x for gui/main_win.cc


Ignore:
Timestamp:
07/29/05 14:01:37 (15 years ago)
Author:
Akos Ladanyi
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2111
Message:

added support for saving files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gui/main_win.cc

    r1601 r1606  
    1 #include <main_win.h>
    2 
    3 MainWin::MainWin(const std::string& title, Graph & graph, CoordinatesMap & cm,
    4     MapStorage & ms):mapwin("Map Setup", ms, gd_canvas),newmapwin("Creating new map",gd_canvas),gd_canvas(graph, cm, ms, &mapwin)
     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)
    56{
    67
     
    2021  ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
    2122      sigc::mem_fun(*this, &MainWin::saveFileAs));
    22   ag->add( Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
     23  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
     24      sigc::mem_fun(*this, &MainWin::close));
     25  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
    2326      sigc::mem_fun(*this, &MainWin::hide));
    2427
     
    3740      sigc::mem_fun(this->mapwin, &MapWin::show));
    3841
    39   ag->add( Gtk::Action::create("CreateNode", Gtk::Stock::NO),
     42  Gtk::RadioAction::Group tool_group;
     43  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::Stock::CONVERT, "Move"),
     44      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) );
     45  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::Stock::NO, "Create node"),
    4046      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 1) );
    41   ag->add( Gtk::Action::create("CreateEdge", Gtk::Stock::REMOVE),
     47  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::Stock::REMOVE, "Create edge"),
    4248      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 2) );
    43   ag->add( Gtk::Action::create("EraseItem", Gtk::Stock::DELETE),
     49  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::Stock::DELETE, "Delete"),
    4450      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 3) );
    45   ag->add( Gtk::Action::create("MoveItem", Gtk::Stock::CONVERT),
    46       sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) );
    4751  ag->add( Gtk::Action::create("EditEdgeMap", Gtk::Stock::PROPERTIES),
    4852      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 4) );
     
    6771      "      <menuitem action='FileSave'/>"
    6872      "      <menuitem action='FileSaveAs'/>"
    69       "      <menuitem action='FileQuit'/>"
     73      "      <menuitem action='Close'/>"
     74      "      <menuitem action='Quit'/>"
    7075      "    </menu>"
    7176      "    <menu action='ViewMenu'>"
     
    8388      "    <toolitem action='FileOpen' />"
    8489      "    <toolitem action='FileSave' />"
     90      "    <toolitem action='Close' />"
    8591      "    <separator />"
    8692      "    <toolitem action='ViewZoomIn' />"
     
    8995      "    <toolitem action='ViewZoom100' />"
    9096      "    <separator />"
     97      "    <toolitem action='MoveItem' />"
    9198      "    <toolitem action='CreateNode' />"
    9299      "    <toolitem action='CreateEdge' />"
    93100      "    <toolitem action='EraseItem' />"
    94       "    <toolitem action='MoveItem' />"
    95101      "    <toolitem action='EditEdgeMap' />"
    96102      "    <toolitem action='EditNodeMap' />"
     
    131137void MainWin::newFile()
    132138{
    133   std::cerr << "MainWin::newFile(): not yet implemented" << std::endl;
     139  if (mapstorage.modified)
     140  {
     141    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
     142        Gtk::BUTTONS_NONE);
     143    mdialog.set_message("<b>Save changes before closing?</b>", true);
     144    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
     145    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
     146    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
     147    switch (mdialog.run())
     148    {
     149      case Gtk::RESPONSE_CANCEL:
     150        return;
     151      case Gtk::RESPONSE_REJECT:
     152        break;
     153      case Gtk::RESPONSE_ACCEPT:
     154        saveFile();
     155        break;
     156    }
     157  }
     158  gd_canvas.clear();
     159  mapstorage.clear();
     160  mapwin.update();
    134161}
    135162
    136163void MainWin::openFile()
    137164{
    138   std::cerr << "MainWin::openFile(): not yet implemented" << std::endl;
     165  if (mapstorage.modified)
     166  {
     167    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
     168        Gtk::BUTTONS_NONE);
     169    mdialog.set_message("<b>Save changes before closing?</b>", true);
     170    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
     171    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
     172    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
     173    switch (mdialog.run())
     174    {
     175      case Gtk::RESPONSE_CANCEL:
     176        return;
     177      case Gtk::RESPONSE_REJECT:
     178        break;
     179      case Gtk::RESPONSE_ACCEPT:
     180        saveFile();
     181        break;
     182    }
     183  }
     184  gd_canvas.clear();
     185  mapstorage.clear();
     186  Gtk::FileChooserDialog fcdialog("Open File");
     187  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
     188  fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
     189  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
     190  {
     191    Glib::ustring filename = fcdialog.get_filename();
     192    mapstorage.readFromFile(filename);
     193    mapstorage.file_name = filename;
     194    mapstorage.modified = false;
     195    gd_canvas.drawGraph();
     196    mapwin.update();
     197  }
    139198}
    140199
    141200void MainWin::saveFile()
    142201{
    143   std::cerr << "MainWin::saveFile(): not yet implemented" << std::endl;
     202  if (mapstorage.file_name == "") {
     203    saveFileAs();
     204  }
     205  else
     206  {
     207    mapstorage.writeToFile(mapstorage.file_name);
     208    mapstorage.modified = false;
     209  }
    144210}
    145211
    146212void MainWin::saveFileAs()
    147213{
    148   std::cerr << "MainWin::saveFileAs(): not yet implemented" << std::endl;
    149 }
     214  Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
     215  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
     216  fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
     217  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
     218  {
     219    Glib::ustring filename = fcdialog.get_filename();
     220    mapstorage.file_name = filename;
     221    mapstorage.writeToFile(filename);
     222    mapstorage.modified = false;
     223  }
     224}
     225
     226void MainWin::close()
     227{
     228  if (mapstorage.modified)
     229  {
     230    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
     231        Gtk::BUTTONS_NONE);
     232    mdialog.set_message("<b>Save changes before closing?</b>", true);
     233    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
     234    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
     235    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
     236    switch (mdialog.run())
     237    {
     238      case Gtk::RESPONSE_CANCEL:
     239        return;
     240      case Gtk::RESPONSE_REJECT:
     241        break;
     242      case Gtk::RESPONSE_ACCEPT:
     243        saveFile();
     244        break;
     245    }
     246  }
     247  gd_canvas.clear();
     248  mapstorage.clear();
     249  mapwin.update();
     250}
Note: See TracChangeset for help on using the changeset viewer.