main_win.cc
author alpar
Mon, 24 Oct 2005 08:12:26 +0000
branchgui
changeset 84 09b09efab0a1
parent 76 76f4f2ae0e7d
child 87 b44281e4cca7
permissions -rw-r--r--
Doxyfile for documenting glemon's architecture.
ladanyi@53
     1
#include "main_win.h"
alpar@76
     2
#include "icons/guipixbufs.h"
ladanyi@6
     3
ladanyi@68
     4
MainWin::MainWin() :
hegyi@82
     5
  newmapwin("Creating new map", gd_canvas),
hegyi@82
     6
  mapwin("Map Setup", mapstorage, gd_canvas, newmapwin),
hegyi@82
     7
  gd_canvas(mapstorage, mapwin, (Gtk::Window *)this)
ladanyi@6
     8
{
ladanyi@68
     9
  set_title ("unsaved file - " + prog_name);
ladanyi@6
    10
  set_default_size(WIN_WIDTH,WIN_HEIGHT);
ladanyi@6
    11
  add(vbox);
ladanyi@6
    12
ladanyi@60
    13
ladanyi@60
    14
  // custom icons for the toolbar
ladanyi@60
    15
ladanyi@60
    16
  Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
ladanyi@60
    17
 
ladanyi@61
    18
  Glib::RefPtr<Gdk::Pixbuf> p_move_pixbuf = Gdk::Pixbuf::create_from_inline(
ladanyi@61
    19
      2328, gui_icons_move);
ladanyi@61
    20
  Glib::RefPtr<Gdk::Pixbuf> p_addnode_pixbuf = Gdk::Pixbuf::create_from_inline(
ladanyi@61
    21
      2328, gui_icons_addnode);
ladanyi@61
    22
  Glib::RefPtr<Gdk::Pixbuf> p_addlink_pixbuf = Gdk::Pixbuf::create_from_inline(
ladanyi@61
    23
      2328, gui_icons_addlink);
ladanyi@61
    24
  Glib::RefPtr<Gdk::Pixbuf> p_delete_pixbuf = Gdk::Pixbuf::create_from_inline(
ladanyi@61
    25
      2328, gui_icons_delete);
ladanyi@61
    26
  Glib::RefPtr<Gdk::Pixbuf> p_editlink_pixbuf = Gdk::Pixbuf::create_from_inline(
ladanyi@61
    27
      2328, gui_icons_editlink);
ladanyi@61
    28
  Glib::RefPtr<Gdk::Pixbuf> p_editnode_pixbuf = Gdk::Pixbuf::create_from_inline(
ladanyi@61
    29
      2328, gui_icons_editnode);
ladanyi@61
    30
  Glib::RefPtr<Gdk::Pixbuf> p_newmap_pixbuf = Gdk::Pixbuf::create_from_inline(
ladanyi@61
    31
      2328, gui_icons_newmap);
ladanyi@60
    32
ladanyi@60
    33
  Gtk::IconSource move_icon_source;
ladanyi@61
    34
  move_icon_source.set_pixbuf(p_move_pixbuf);
ladanyi@60
    35
  Gtk::IconSet move_icon_set;
ladanyi@60
    36
  move_icon_set.add_source(move_icon_source);
ladanyi@60
    37
  p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set);
ladanyi@60
    38
ladanyi@60
    39
  Gtk::IconSource addnode_icon_source;
ladanyi@61
    40
  addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
ladanyi@60
    41
  Gtk::IconSet addnode_icon_set;
ladanyi@60
    42
  addnode_icon_set.add_source(addnode_icon_source);
ladanyi@60
    43
  p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set);
ladanyi@60
    44
ladanyi@60
    45
  Gtk::IconSource addlink_icon_source;
ladanyi@61
    46
  addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
ladanyi@60
    47
  Gtk::IconSet addlink_icon_set;
ladanyi@60
    48
  addlink_icon_set.add_source(addlink_icon_source);
ladanyi@60
    49
  p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set);
ladanyi@60
    50
ladanyi@60
    51
  Gtk::IconSource delete_icon_source;
ladanyi@61
    52
  delete_icon_source.set_pixbuf(p_delete_pixbuf);
ladanyi@60
    53
  Gtk::IconSet delete_icon_set;
ladanyi@60
    54
  delete_icon_set.add_source(delete_icon_source);
ladanyi@60
    55
  p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set);
ladanyi@60
    56
ladanyi@60
    57
  Gtk::IconSource editlink_icon_source;
ladanyi@61
    58
  editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
ladanyi@60
    59
  Gtk::IconSet editlink_icon_set;
ladanyi@60
    60
  editlink_icon_set.add_source(editlink_icon_source);
ladanyi@60
    61
  p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set);
ladanyi@60
    62
ladanyi@60
    63
  Gtk::IconSource editnode_icon_source;
ladanyi@61
    64
  editnode_icon_source.set_pixbuf(p_editnode_pixbuf);
ladanyi@60
    65
  Gtk::IconSet editnode_icon_set;
ladanyi@60
    66
  editnode_icon_set.add_source(editnode_icon_source);
ladanyi@60
    67
  p_icon_factory->add(Gtk::StockID("gd-editnode"), editnode_icon_set);
ladanyi@60
    68
ladanyi@60
    69
  Gtk::IconSource newmap_icon_source;
ladanyi@61
    70
  newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
ladanyi@60
    71
  Gtk::IconSet newmap_icon_set;
ladanyi@60
    72
  newmap_icon_set.add_source(newmap_icon_source);
ladanyi@60
    73
  p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set);
ladanyi@60
    74
ladanyi@60
    75
  p_icon_factory->add_default();
ladanyi@60
    76
  
ladanyi@60
    77
ladanyi@6
    78
  ag=Gtk::ActionGroup::create();
ladanyi@6
    79
ladanyi@6
    80
  ag->add( Gtk::Action::create("FileMenu", "_File") );
ladanyi@6
    81
  ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
ladanyi@6
    82
      sigc::mem_fun(*this, &MainWin::newFile));
ladanyi@6
    83
  ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
ladanyi@6
    84
      sigc::mem_fun(*this, &MainWin::openFile));
ladanyi@6
    85
  ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
ladanyi@6
    86
      sigc::mem_fun(*this, &MainWin::saveFile));
ladanyi@6
    87
  ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
ladanyi@6
    88
      sigc::mem_fun(*this, &MainWin::saveFileAs));
ladanyi@53
    89
  ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
ladanyi@53
    90
      sigc::mem_fun(*this, &MainWin::close));
ladanyi@53
    91
  ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
hegyi@47
    92
      sigc::mem_fun(*this, &MainWin::hide));
ladanyi@6
    93
ladanyi@6
    94
  ag->add( Gtk::Action::create("ViewMenu", "_View") );
ladanyi@6
    95
  ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
ladanyi@6
    96
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomIn));
ladanyi@6
    97
  ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
ladanyi@6
    98
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomOut));
ladanyi@6
    99
  ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
ladanyi@6
   100
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoomFit));
ladanyi@6
   101
  ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
ladanyi@6
   102
      sigc::mem_fun(this->gd_canvas, &GraphDisplayerCanvas::zoom100));
ladanyi@6
   103
  
ladanyi@6
   104
  ag->add( Gtk::Action::create("ShowMenu", "_Show") );
ladanyi@6
   105
  ag->add( Gtk::Action::create("ShowMaps", "_Maps"),
hegyi@47
   106
      sigc::mem_fun(this->mapwin, &MapWin::show));
ladanyi@6
   107
ladanyi@53
   108
  Gtk::RadioAction::Group tool_group;
ladanyi@60
   109
  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), "Move"),
ladanyi@53
   110
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 0) );
ladanyi@60
   111
  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), "Create node"),
hegyi@22
   112
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 1) );
ladanyi@60
   113
  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), "Create edge"),
hegyi@22
   114
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 2) );
ladanyi@60
   115
  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), "Delete"),
hegyi@22
   116
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 3) );
ladanyi@60
   117
ladanyi@60
   118
  ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), "Edit edge map"),
hegyi@32
   119
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 4) );
ladanyi@60
   120
  ag->add( Gtk::RadioAction::create(tool_group, "EditNodeMap", Gtk::StockID("gd-editnode"), "Edit node map"),
hegyi@32
   121
      sigc::bind( sigc::mem_fun ( this->gd_canvas, &GraphDisplayerCanvas::changeEditorialTool ), 5) );
ladanyi@60
   122
ladanyi@60
   123
  ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
hegyi@41
   124
      sigc::mem_fun ( this->newmapwin, &NewMapWin::show ) );
hegyi@22
   125
ladanyi@6
   126
  uim=Gtk::UIManager::create();
ladanyi@6
   127
  uim->insert_action_group(ag);
ladanyi@6
   128
  add_accel_group(uim->get_accel_group());
ladanyi@6
   129
ladanyi@74
   130
  /*
ladanyi@74
   131
      "      <menuitem action='ViewZoomFit' />"
ladanyi@74
   132
      "    <toolitem action='ViewZoomFit' />"
ladanyi@74
   133
   */
ladanyi@74
   134
ladanyi@6
   135
  try
ladanyi@6
   136
  {
ladanyi@6
   137
ladanyi@6
   138
    Glib::ustring ui_info =
ladanyi@6
   139
      "<ui>"
ladanyi@6
   140
      "  <menubar name='MenuBar'>"
ladanyi@6
   141
      "    <menu action='FileMenu'>"
ladanyi@6
   142
      "      <menuitem action='FileNew'/>"
ladanyi@6
   143
      "      <menuitem action='FileOpen'/>"
ladanyi@6
   144
      "      <menuitem action='FileSave'/>"
ladanyi@6
   145
      "      <menuitem action='FileSaveAs'/>"
ladanyi@53
   146
      "      <menuitem action='Close'/>"
ladanyi@53
   147
      "      <menuitem action='Quit'/>"
ladanyi@6
   148
      "    </menu>"
ladanyi@6
   149
      "    <menu action='ViewMenu'>"
ladanyi@6
   150
      "      <menuitem action='ViewZoomIn' />"
ladanyi@6
   151
      "      <menuitem action='ViewZoomOut' />"
ladanyi@6
   152
      "      <menuitem action='ViewZoom100' />"
ladanyi@6
   153
      "    </menu>"
ladanyi@6
   154
      "    <menu action='ShowMenu'>"
ladanyi@6
   155
      "      <menuitem action='ShowMaps'/>"
ladanyi@6
   156
      "    </menu>"
ladanyi@6
   157
      "  </menubar>"
ladanyi@6
   158
      "  <toolbar name='ToolBar'>"
ladanyi@6
   159
      "    <toolitem action='FileNew' />"
ladanyi@6
   160
      "    <toolitem action='FileOpen' />"
ladanyi@6
   161
      "    <toolitem action='FileSave' />"
ladanyi@53
   162
      "    <toolitem action='Close' />"
ladanyi@6
   163
      "    <separator />"
ladanyi@6
   164
      "    <toolitem action='ViewZoomIn' />"
ladanyi@6
   165
      "    <toolitem action='ViewZoomOut' />"
ladanyi@6
   166
      "    <toolitem action='ViewZoom100' />"
hegyi@22
   167
      "    <separator />"
ladanyi@53
   168
      "    <toolitem action='MoveItem' />"
hegyi@22
   169
      "    <toolitem action='CreateNode' />"
hegyi@22
   170
      "    <toolitem action='CreateEdge' />"
hegyi@22
   171
      "    <toolitem action='EraseItem' />"
hegyi@32
   172
      "    <toolitem action='EditEdgeMap' />"
hegyi@32
   173
      "    <toolitem action='EditNodeMap' />"
ladanyi@60
   174
      "    <separator />"
hegyi@41
   175
      "    <toolitem action='AddMap' />"
ladanyi@6
   176
      "  </toolbar>"
ladanyi@6
   177
      "</ui>";
ladanyi@6
   178
ladanyi@6
   179
    uim->add_ui_from_string(ui_info);
ladanyi@6
   180
ladanyi@6
   181
  }
ladanyi@6
   182
  catch(const Glib::Error& ex)
ladanyi@6
   183
  {
ladanyi@6
   184
    std::cerr << "building menus failed: " <<  ex.what();
ladanyi@6
   185
  }
ladanyi@6
   186
ladanyi@6
   187
  Gtk::Widget* menubar = uim->get_widget("/MenuBar");
ladanyi@6
   188
  if (menubar){
ladanyi@6
   189
    vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
ladanyi@6
   190
  }
ladanyi@6
   191
ladanyi@6
   192
  Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
ladanyi@6
   193
  if (toolbar)
ladanyi@6
   194
  {
ladanyi@6
   195
    static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
ladanyi@6
   196
    vbox.pack_start(*toolbar, Gtk::PACK_SHRINK);
ladanyi@6
   197
  }
ladanyi@6
   198
ladanyi@6
   199
  Gtk::ScrolledWindow* pScrolledWindow = manage(new Gtk::ScrolledWindow());
ladanyi@6
   200
  pScrolledWindow->add(gd_canvas);
ladanyi@6
   201
  vbox.pack_start(*pScrolledWindow);
hegyi@50
   202
hegyi@50
   203
  tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
hegyi@50
   204
  tooltips.enable();
ladanyi@6
   205
ladanyi@6
   206
  show_all_children();
ladanyi@6
   207
}
ladanyi@6
   208
ladanyi@57
   209
void MainWin::readFile(const std::string &file)
ladanyi@57
   210
{
ladanyi@57
   211
  mapstorage.readFromFile(file);
ladanyi@57
   212
  mapstorage.file_name = file;
ladanyi@57
   213
  mapstorage.modified = false;
ladanyi@57
   214
  gd_canvas.drawGraph();
ladanyi@57
   215
  mapwin.update();
ladanyi@68
   216
  set_title(Glib::filename_display_basename(file) + " - " + prog_name);
ladanyi@57
   217
}
ladanyi@57
   218
ladanyi@6
   219
void MainWin::newFile()
ladanyi@6
   220
{
ladanyi@53
   221
  if (mapstorage.modified)
ladanyi@53
   222
  {
ladanyi@56
   223
    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
ladanyi@56
   224
        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
ladanyi@53
   225
    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
ladanyi@53
   226
    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@53
   227
    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@53
   228
    switch (mdialog.run())
ladanyi@53
   229
    {
ladanyi@53
   230
      case Gtk::RESPONSE_CANCEL:
ladanyi@53
   231
        return;
ladanyi@53
   232
      case Gtk::RESPONSE_REJECT:
ladanyi@53
   233
        break;
ladanyi@53
   234
      case Gtk::RESPONSE_ACCEPT:
ladanyi@53
   235
        saveFile();
ladanyi@53
   236
        break;
ladanyi@53
   237
    }
ladanyi@53
   238
  }
ladanyi@53
   239
  gd_canvas.clear();
ladanyi@53
   240
  mapstorage.clear();
ladanyi@53
   241
  mapwin.update();
ladanyi@68
   242
  set_title("unsaved file - " + prog_name);
ladanyi@6
   243
}
ladanyi@6
   244
ladanyi@6
   245
void MainWin::openFile()
ladanyi@6
   246
{
ladanyi@53
   247
  if (mapstorage.modified)
ladanyi@53
   248
  {
ladanyi@56
   249
    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true, 
ladanyi@56
   250
        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
ladanyi@53
   251
    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
ladanyi@53
   252
    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@53
   253
    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@53
   254
    switch (mdialog.run())
ladanyi@53
   255
    {
ladanyi@53
   256
      case Gtk::RESPONSE_CANCEL:
ladanyi@53
   257
        return;
ladanyi@53
   258
      case Gtk::RESPONSE_REJECT:
ladanyi@53
   259
        break;
ladanyi@53
   260
      case Gtk::RESPONSE_ACCEPT:
ladanyi@53
   261
        saveFile();
ladanyi@53
   262
        break;
ladanyi@53
   263
    }
ladanyi@53
   264
  }
ladanyi@53
   265
  Gtk::FileChooserDialog fcdialog("Open File");
ladanyi@53
   266
  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@53
   267
  fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
ladanyi@53
   268
  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
ladanyi@53
   269
  {
ladanyi@68
   270
    gd_canvas.clear();
ladanyi@68
   271
    mapstorage.clear();
ladanyi@53
   272
    Glib::ustring filename = fcdialog.get_filename();
ladanyi@63
   273
    if (!mapstorage.readFromFile(filename))
ladanyi@63
   274
    {
ladanyi@63
   275
      mapstorage.file_name = filename;
ladanyi@63
   276
      mapstorage.modified = false;
ladanyi@63
   277
      gd_canvas.drawGraph();
ladanyi@63
   278
      mapwin.update();
ladanyi@68
   279
      set_title(Glib::filename_display_basename(filename) + " - " + prog_name);
ladanyi@63
   280
    }
ladanyi@53
   281
  }
ladanyi@6
   282
}
ladanyi@6
   283
ladanyi@6
   284
void MainWin::saveFile()
ladanyi@6
   285
{
ladanyi@53
   286
  if (mapstorage.file_name == "") {
ladanyi@53
   287
    saveFileAs();
ladanyi@53
   288
  }
ladanyi@53
   289
  else
ladanyi@53
   290
  {
ladanyi@53
   291
    mapstorage.writeToFile(mapstorage.file_name);
ladanyi@53
   292
    mapstorage.modified = false;
ladanyi@68
   293
    set_title(Glib::filename_display_basename(mapstorage.file_name) + " - " +
ladanyi@68
   294
        prog_name);
ladanyi@53
   295
  }
ladanyi@6
   296
}
ladanyi@6
   297
ladanyi@6
   298
void MainWin::saveFileAs()
ladanyi@6
   299
{
ladanyi@53
   300
  Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
ladanyi@53
   301
  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@53
   302
  fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@53
   303
  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
ladanyi@53
   304
  {
ladanyi@53
   305
    Glib::ustring filename = fcdialog.get_filename();
ladanyi@53
   306
    mapstorage.file_name = filename;
ladanyi@53
   307
    mapstorage.writeToFile(filename);
ladanyi@53
   308
    mapstorage.modified = false;
ladanyi@68
   309
    set_title(Glib::filename_display_basename(filename) + " - " + prog_name);
ladanyi@53
   310
  }
ladanyi@6
   311
}
ladanyi@53
   312
ladanyi@53
   313
void MainWin::close()
ladanyi@53
   314
{
ladanyi@53
   315
  if (mapstorage.modified)
ladanyi@53
   316
  {
ladanyi@56
   317
    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
ladanyi@56
   318
        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
ladanyi@53
   319
    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
ladanyi@53
   320
    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@53
   321
    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
ladanyi@53
   322
    switch (mdialog.run())
ladanyi@53
   323
    {
ladanyi@53
   324
      case Gtk::RESPONSE_CANCEL:
ladanyi@53
   325
        return;
ladanyi@53
   326
      case Gtk::RESPONSE_REJECT:
ladanyi@53
   327
        break;
ladanyi@53
   328
      case Gtk::RESPONSE_ACCEPT:
ladanyi@53
   329
        saveFile();
ladanyi@53
   330
        break;
ladanyi@53
   331
    }
ladanyi@53
   332
  }
ladanyi@53
   333
  gd_canvas.clear();
ladanyi@53
   334
  mapstorage.clear();
ladanyi@53
   335
  mapwin.update();
ladanyi@68
   336
  set_title("unsaved file - " + prog_name);
ladanyi@53
   337
}