gui/main_win.cc
author alpar
Mon, 01 Aug 2005 21:16:08 +0000
changeset 1610 893dacc1866c
parent 1601 70d169a8313a
child 1614 350c1d8bb7cc
permissions -rw-r--r--
A default LP solver is defined in lp.h
ladanyi@1606
     1
#include "main_win.h"
ladanyi@1442
     2
ladanyi@1606
     3
MainWin::MainWin(const std::string& title) :
ladanyi@1606
     4
  mapwin("Map Setup", mapstorage, gd_canvas),
ladanyi@1606
     5
  newmapwin("Creating new map", gd_canvas), gd_canvas(mapstorage, mapwin)
ladanyi@1442
     6
{
hegyi@1592
     7
ladanyi@1442
     8
  set_title (title);
ladanyi@1442
     9
  set_default_size(WIN_WIDTH,WIN_HEIGHT);
ladanyi@1442
    10
  add(vbox);
ladanyi@1442
    11
ladanyi@1442
    12
  ag=Gtk::ActionGroup::create();
ladanyi@1442
    13
ladanyi@1442
    14
  ag->add( Gtk::Action::create("FileMenu", "_File") );
ladanyi@1442
    15
  ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
ladanyi@1442
    16
      sigc::mem_fun(*this, &MainWin::newFile));
ladanyi@1442
    17
  ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
ladanyi@1442
    18
      sigc::mem_fun(*this, &MainWin::openFile));
ladanyi@1442
    19
  ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
ladanyi@1442
    20
      sigc::mem_fun(*this, &MainWin::saveFile));
ladanyi@1442
    21
  ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
ladanyi@1442
    22
      sigc::mem_fun(*this, &MainWin::saveFileAs));
ladanyi@1606
    23
  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
ladanyi@1606
    24
      sigc::mem_fun(*this, &MainWin::close));
ladanyi@1606
    25
  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
hegyi@1598
    26
      sigc::mem_fun(*this, &MainWin::hide));
ladanyi@1442
    27
ladanyi@1442
    28
  ag->add( Gtk::Action::create("ViewMenu", "_View") );
ladanyi@1442
    29
  ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
ladanyi@1442
    30
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomIn));
ladanyi@1442
    31
  ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
ladanyi@1442
    32
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomOut));
ladanyi@1442
    33
  ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
ladanyi@1442
    34
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomFit));
ladanyi@1442
    35
  ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
ladanyi@1442
    36
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoom100));
ladanyi@1442
    37
  
ladanyi@1442
    38
  ag->add( Gtk::Action::create("ShowMenu", "_Show") );
ladanyi@1442
    39
  ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
hegyi@1598
    40
      sigc::mem_fun(this->mapwin, &MapWin::show));
ladanyi@1442
    41
ladanyi@1606
    42
  Gtk::RadioAction::Group tool_group;
ladanyi@1606
    43
  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::Stock::CONVERT, "Move"),
ladanyi@1606
    44
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) );
ladanyi@1606
    45
  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::Stock::NO, "Create node"),
hegyi@1502
    46
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 1) );
ladanyi@1606
    47
  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::Stock::REMOVE, "Create edge"),
hegyi@1502
    48
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 2) );
ladanyi@1606
    49
  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::Stock::DELETE, "Delete"),
hegyi@1502
    50
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 3) );
hegyi@1550
    51
  ag->add( Gtk::Action::create("EditEdgeMap", Gtk::Stock::PROPERTIES),
hegyi@1550
    52
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 4) );
hegyi@1550
    53
  ag->add( Gtk::Action::create("EditNodeMap", Gtk::Stock::PREFERENCES),
hegyi@1550
    54
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 5) );
hegyi@1592
    55
  ag->add( Gtk::Action::create("AddMap", Gtk::Stock::NEW),
hegyi@1592
    56
      sigc::mem_fun ( this->newmapwin, &NewMapWin::show ) );
hegyi@1502
    57
ladanyi@1442
    58
  uim=Gtk::UIManager::create();
ladanyi@1442
    59
  uim->insert_action_group(ag);
ladanyi@1442
    60
  add_accel_group(uim->get_accel_group());
ladanyi@1442
    61
ladanyi@1442
    62
  try
ladanyi@1442
    63
  {
ladanyi@1442
    64
ladanyi@1442
    65
    Glib::ustring ui_info =
ladanyi@1442
    66
      "<ui>"
ladanyi@1442
    67
      "  <menubar name='MenuBar'>"
ladanyi@1442
    68
      "    <menu action='FileMenu'>"
ladanyi@1442
    69
      "      <menuitem action='FileNew'/>"
ladanyi@1442
    70
      "      <menuitem action='FileOpen'/>"
ladanyi@1442
    71
      "      <menuitem action='FileSave'/>"
ladanyi@1442
    72
      "      <menuitem action='FileSaveAs'/>"
ladanyi@1606
    73
      "      <menuitem action='Close'/>"
ladanyi@1606
    74
      "      <menuitem action='Quit'/>"
ladanyi@1442
    75
      "    </menu>"
ladanyi@1442
    76
      "    <menu action='ViewMenu'>"
ladanyi@1442
    77
      "      <menuitem action='ViewZoomIn' />"
ladanyi@1442
    78
      "      <menuitem action='ViewZoomOut' />"
ladanyi@1442
    79
      "      <menuitem action='ViewZoomFit' />"
ladanyi@1442
    80
      "      <menuitem action='ViewZoom100' />"
ladanyi@1442
    81
      "    </menu>"
ladanyi@1442
    82
      "    <menu action='ShowMenu'>"
ladanyi@1442
    83
      "      <menuitem action='ShowMaps'/>"
ladanyi@1442
    84
      "    </menu>"
ladanyi@1442
    85
      "  </menubar>"
ladanyi@1442
    86
      "  <toolbar name='ToolBar'>"
ladanyi@1442
    87
      "    <toolitem action='FileNew' />"
ladanyi@1442
    88
      "    <toolitem action='FileOpen' />"
ladanyi@1442
    89
      "    <toolitem action='FileSave' />"
ladanyi@1606
    90
      "    <toolitem action='Close' />"
ladanyi@1442
    91
      "    <separator />"
ladanyi@1442
    92
      "    <toolitem action='ViewZoomIn' />"
ladanyi@1442
    93
      "    <toolitem action='ViewZoomOut' />"
ladanyi@1442
    94
      "    <toolitem action='ViewZoomFit' />"
ladanyi@1442
    95
      "    <toolitem action='ViewZoom100' />"
hegyi@1502
    96
      "    <separator />"
ladanyi@1606
    97
      "    <toolitem action='MoveItem' />"
hegyi@1502
    98
      "    <toolitem action='CreateNode' />"
hegyi@1502
    99
      "    <toolitem action='CreateEdge' />"
hegyi@1502
   100
      "    <toolitem action='EraseItem' />"
hegyi@1550
   101
      "    <toolitem action='EditEdgeMap' />"
hegyi@1550
   102
      "    <toolitem action='EditNodeMap' />"
hegyi@1592
   103
      "    <toolitem action='AddMap' />"
ladanyi@1442
   104
      "  </toolbar>"
ladanyi@1442
   105
      "</ui>";
ladanyi@1442
   106
ladanyi@1442
   107
    uim->add_ui_from_string(ui_info);
ladanyi@1442
   108
ladanyi@1442
   109
  }
ladanyi@1442
   110
  catch(const Glib::Error& ex)
ladanyi@1442
   111
  {
ladanyi@1442
   112
    std::cerr << "building menus failed: " <<  ex.what();
ladanyi@1442
   113
  }
ladanyi@1442
   114
ladanyi@1442
   115
  Gtk::Widget* menubar = uim->get_widget("/MenuBar");
ladanyi@1442
   116
  if (menubar){
ladanyi@1442
   117
    vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
ladanyi@1442
   118
  }
ladanyi@1442
   119
ladanyi@1442
   120
  Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
ladanyi@1442
   121
  if (toolbar)
ladanyi@1442
   122
  {
ladanyi@1442
   123
    static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
ladanyi@1442
   124
    vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
ladanyi@1442
   125
  }
ladanyi@1442
   126
ladanyi@1442
   127
  Gtk::ScrolledWindow* pScrolledWindow = manage(new Gtk::ScrolledWindow());
ladanyi@1442
   128
  pScrolledWindow->add(gd_canvas);
ladanyi@1442
   129
  vbox.pack_start(*pScrolledWindow);
hegyi@1601
   130
hegyi@1601
   131
  tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
hegyi@1601
   132
  tooltips.enable();
ladanyi@1442
   133
ladanyi@1442
   134
  show_all_children();
ladanyi@1442
   135
}
ladanyi@1442
   136
ladanyi@1442
   137
void MainWin::newFile()
ladanyi@1442
   138
{
ladanyi@1606
   139
  if (mapstorage.modified)
ladanyi@1606
   140
  {
ladanyi@1606
   141
    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
ladanyi@1606
   142
        Gtk::BUTTONS_NONE);
ladanyi@1606
   143
    mdialog.set_message("<b>Save changes before closing?</b>", true);
ladanyi@1606
   144
    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
ladanyi@1606
   145
    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@1606
   146
    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@1606
   147
    switch (mdialog.run())
ladanyi@1606
   148
    {
ladanyi@1606
   149
      case Gtk::RESPONSE_CANCEL:
ladanyi@1606
   150
        return;
ladanyi@1606
   151
      case Gtk::RESPONSE_REJECT:
ladanyi@1606
   152
        break;
ladanyi@1606
   153
      case Gtk::RESPONSE_ACCEPT:
ladanyi@1606
   154
        saveFile();
ladanyi@1606
   155
        break;
ladanyi@1606
   156
    }
ladanyi@1606
   157
  }
ladanyi@1606
   158
  gd_canvas.clear();
ladanyi@1606
   159
  mapstorage.clear();
ladanyi@1606
   160
  mapwin.update();
ladanyi@1442
   161
}
ladanyi@1442
   162
ladanyi@1442
   163
void MainWin::openFile()
ladanyi@1442
   164
{
ladanyi@1606
   165
  if (mapstorage.modified)
ladanyi@1606
   166
  {
ladanyi@1606
   167
    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
ladanyi@1606
   168
        Gtk::BUTTONS_NONE);
ladanyi@1606
   169
    mdialog.set_message("<b>Save changes before closing?</b>", true);
ladanyi@1606
   170
    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
ladanyi@1606
   171
    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@1606
   172
    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@1606
   173
    switch (mdialog.run())
ladanyi@1606
   174
    {
ladanyi@1606
   175
      case Gtk::RESPONSE_CANCEL:
ladanyi@1606
   176
        return;
ladanyi@1606
   177
      case Gtk::RESPONSE_REJECT:
ladanyi@1606
   178
        break;
ladanyi@1606
   179
      case Gtk::RESPONSE_ACCEPT:
ladanyi@1606
   180
        saveFile();
ladanyi@1606
   181
        break;
ladanyi@1606
   182
    }
ladanyi@1606
   183
  }
ladanyi@1606
   184
  gd_canvas.clear();
ladanyi@1606
   185
  mapstorage.clear();
ladanyi@1606
   186
  Gtk::FileChooserDialog fcdialog("Open File");
ladanyi@1606
   187
  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@1606
   188
  fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
ladanyi@1606
   189
  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
ladanyi@1606
   190
  {
ladanyi@1606
   191
    Glib::ustring filename = fcdialog.get_filename();
ladanyi@1606
   192
    mapstorage.readFromFile(filename);
ladanyi@1606
   193
    mapstorage.file_name = filename;
ladanyi@1606
   194
    mapstorage.modified = false;
ladanyi@1606
   195
    gd_canvas.drawGraph();
ladanyi@1606
   196
    mapwin.update();
ladanyi@1606
   197
  }
ladanyi@1442
   198
}
ladanyi@1442
   199
ladanyi@1442
   200
void MainWin::saveFile()
ladanyi@1442
   201
{
ladanyi@1606
   202
  if (mapstorage.file_name == "") {
ladanyi@1606
   203
    saveFileAs();
ladanyi@1606
   204
  }
ladanyi@1606
   205
  else
ladanyi@1606
   206
  {
ladanyi@1606
   207
    mapstorage.writeToFile(mapstorage.file_name);
ladanyi@1606
   208
    mapstorage.modified = false;
ladanyi@1606
   209
  }
ladanyi@1442
   210
}
ladanyi@1442
   211
ladanyi@1442
   212
void MainWin::saveFileAs()
ladanyi@1442
   213
{
ladanyi@1606
   214
  Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
ladanyi@1606
   215
  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@1606
   216
  fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@1606
   217
  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
ladanyi@1606
   218
  {
ladanyi@1606
   219
    Glib::ustring filename = fcdialog.get_filename();
ladanyi@1606
   220
    mapstorage.file_name = filename;
ladanyi@1606
   221
    mapstorage.writeToFile(filename);
ladanyi@1606
   222
    mapstorage.modified = false;
ladanyi@1606
   223
  }
ladanyi@1442
   224
}
ladanyi@1606
   225
ladanyi@1606
   226
void MainWin::close()
ladanyi@1606
   227
{
ladanyi@1606
   228
  if (mapstorage.modified)
ladanyi@1606
   229
  {
ladanyi@1606
   230
    Gtk::MessageDialog mdialog("", false, Gtk::MESSAGE_WARNING,
ladanyi@1606
   231
        Gtk::BUTTONS_NONE);
ladanyi@1606
   232
    mdialog.set_message("<b>Save changes before closing?</b>", true);
ladanyi@1606
   233
    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
ladanyi@1606
   234
    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@1606
   235
    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@1606
   236
    switch (mdialog.run())
ladanyi@1606
   237
    {
ladanyi@1606
   238
      case Gtk::RESPONSE_CANCEL:
ladanyi@1606
   239
        return;
ladanyi@1606
   240
      case Gtk::RESPONSE_REJECT:
ladanyi@1606
   241
        break;
ladanyi@1606
   242
      case Gtk::RESPONSE_ACCEPT:
ladanyi@1606
   243
        saveFile();
ladanyi@1606
   244
        break;
ladanyi@1606
   245
    }
ladanyi@1606
   246
  }
ladanyi@1606
   247
  gd_canvas.clear();
ladanyi@1606
   248
  mapstorage.clear();
ladanyi@1606
   249
  mapwin.update();
ladanyi@1606
   250
}