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