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