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