/[svn]/gigedit/trunk/src/gigedit/mainwindow.cpp
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2423 by persson, Sun Feb 24 15:19:39 2013 UTC revision 2442 by persson, Sun Apr 14 07:29:59 2013 UTC
# Line 30  Line 30 
30  #include <gtkmm/stock.h>  #include <gtkmm/stock.h>
31  #include <gtkmm/targetentry.h>  #include <gtkmm/targetentry.h>
32  #include <gtkmm/main.h>  #include <gtkmm/main.h>
 #include <gtkmm/radiomenuitem.h>  
33  #include <gtkmm/toggleaction.h>  #include <gtkmm/toggleaction.h>
34  #if GTKMM_MAJOR_VERSION < 3  #if GTKMM_MAJOR_VERSION < 3
35  #include "wrapLabel.hh"  #include "wrapLabel.hh"
# Line 47  Line 46 
46  #include "../../gfx/status_attached.xpm"  #include "../../gfx/status_attached.xpm"
47  #include "../../gfx/status_detached.xpm"  #include "../../gfx/status_detached.xpm"
48    
 template<class T> inline std::string ToString(T o) {  
     std::stringstream ss;  
     ss << o;  
     return ss.str();  
 }  
   
49    
50  MainWindow::MainWindow() :  MainWindow::MainWindow() :
51      dimreg_label(_("Changes apply to:")),      dimreg_label(_("Changes apply to:")),
# Line 67  MainWindow::MainWindow() : Line 60  MainWindow::MainWindow() :
60      add(m_VBox);      add(m_VBox);
61    
62      // Handle selection      // Handle selection
63      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      m_TreeView.get_selection()->signal_changed().connect(
     tree_sel_ref->signal_changed().connect(  
64          sigc::mem_fun(*this, &MainWindow::on_sel_change));          sigc::mem_fun(*this, &MainWindow::on_sel_change));
65    
66      // m_TreeView.set_reorderable();      // m_TreeView.set_reorderable();
# Line 224  MainWindow::MainWindow() : Line 216  MainWindow::MainWindow() :
216          "    <menuitem action='SampleProperties'/>"          "    <menuitem action='SampleProperties'/>"
217          "    <menuitem action='AddGroup'/>"          "    <menuitem action='AddGroup'/>"
218          "    <menuitem action='AddSample'/>"          "    <menuitem action='AddSample'/>"
219          "    <menuitem action='ReplaceAllSamplesInAllGroups' />"          "    <menuitem action='ReplaceAllSamplesInAllGroups' />"
220          "    <separator/>"          "    <separator/>"
221          "    <menuitem action='RemoveSample'/>"          "    <menuitem action='RemoveSample'/>"
222          "  </popup>"          "  </popup>"
# Line 233  MainWindow::MainWindow() : Line 225  MainWindow::MainWindow() :
225    
226      popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));      popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));
227    
228        instrument_menu = static_cast<Gtk::MenuItem*>(
229            uiManager->get_widget("/MenuBar/MenuInstrument"))->get_submenu();
230    
231      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
232      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
233      m_VBox.pack_start(m_HPaned);      m_VBox.pack_start(m_HPaned);
# Line 257  MainWindow::MainWindow() : Line 252  MainWindow::MainWindow() :
252      // Create the Tree model:      // Create the Tree model:
253      m_refTreeModel = Gtk::ListStore::create(m_Columns);      m_refTreeModel = Gtk::ListStore::create(m_Columns);
254      m_TreeView.set_model(m_refTreeModel);      m_TreeView.set_model(m_refTreeModel);
255      m_refTreeModel->signal_row_changed().connect(      instrument_name_connection = m_refTreeModel->signal_row_changed().connect(
256          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
257      );      );
258    
# Line 375  void MainWindow::region_changed() Line 370  void MainWindow::region_changed()
370  gig::Instrument* MainWindow::get_instrument()  gig::Instrument* MainWindow::get_instrument()
371  {  {
372      gig::Instrument* instrument = 0;      gig::Instrument* instrument = 0;
373      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      Gtk::TreeModel::const_iterator it =
374            m_TreeView.get_selection()->get_selected();
     Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();  
375      if (it) {      if (it) {
376          Gtk::TreeModel::Row row = *it;          Gtk::TreeModel::Row row = *it;
377          instrument = row[m_Columns.m_col_instr];          instrument = row[m_Columns.m_col_instr];
# Line 436  void MainWindow::dimreg_changed() Line 430  void MainWindow::dimreg_changed()
430    
431  void MainWindow::on_sel_change()  void MainWindow::on_sel_change()
432  {  {
433        // select item in instrument menu
434        Gtk::TreeModel::iterator it = m_TreeView.get_selection()->get_selected();
435        if (it) {
436            Gtk::TreePath path(it);
437            int index = path[0];
438            const std::vector<Gtk::Widget*> children =
439                instrument_menu->get_children();
440            static_cast<Gtk::RadioMenuItem*>(children[index])->set_active();
441        }
442    
443      m_RegionChooser.set_instrument(get_instrument());      m_RegionChooser.set_instrument(get_instrument());
444  }  }
445    
# Line 514  LoadDialog::LoadDialog(const Glib::ustri Line 518  LoadDialog::LoadDialog(const Glib::ustri
518  // Clear all GUI elements / controls. This method is typically called  // Clear all GUI elements / controls. This method is typically called
519  // before a new .gig file is to be created or to be loaded.  // before a new .gig file is to be created or to be loaded.
520  void MainWindow::__clear() {  void MainWindow::__clear() {
     // remove all entries from "Instrument" menu  
     Gtk::MenuItem* instrument_menu =  
         dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));  
     instrument_menu->hide();  
     Gtk::Menu* menu = instrument_menu->get_submenu();  
     while (menu->get_children().size()) {  
         Gtk::Widget* child = *menu->get_children().begin();  
         menu->remove(*child);  
         delete child;  
     }  
521      // forget all samples that ought to be imported      // forget all samples that ought to be imported
522      m_SampleImportQueue.clear();      m_SampleImportQueue.clear();
523      // clear the samples and instruments tree views      // clear the samples and instruments tree views
524      m_refTreeModel->clear();      m_refTreeModel->clear();
525      m_refSamplesTreeModel->clear();      m_refSamplesTreeModel->clear();
526        // remove all entries from "Instrument" menu
527        while (!instrument_menu->get_children().empty()) {
528            remove_instrument_from_menu(0);
529        }
530      // free libgig's gig::File instance      // free libgig's gig::File instance
531      if (file && !file_is_shared) delete file;      if (file && !file_is_shared) delete file;
532      file = NULL;      file = NULL;
# Line 880  void MainWindow::__import_queued_samples Line 878  void MainWindow::__import_queued_samples
878              m_SampleImportQueue.erase(cur);              m_SampleImportQueue.erase(cur);
879          } catch (std::string what) {          } catch (std::string what) {
880              // remember the files that made trouble (and their cause)              // remember the files that made trouble (and their cause)
881              if (error_files.size()) error_files += "\n";              if (!error_files.empty()) error_files += "\n";
882              error_files += (*iter).sample_path += " (" + what + ")";              error_files += (*iter).sample_path += " (" + what + ")";
883              ++iter;              ++iter;
884          }          }
885      }      }
886      // show error message box when some sample(s) could not be imported      // show error message box when some sample(s) could not be imported
887      if (error_files.size()) {      if (!error_files.empty()) {
888          Glib::ustring txt = _("Could not import the following sample(s):\n") + error_files;          Glib::ustring txt = _("Could not import the following sample(s):\n") + error_files;
889          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
890          msg.run();          msg.run();
# Line 1126  void MainWindow::load_gig(gig::File* gig Line 1124  void MainWindow::load_gig(gig::File* gig
1124    
1125      propDialog.set_info(gig->pInfo);      propDialog.set_info(gig->pInfo);
1126    
1127      Gtk::MenuItem* instrument_menu =      instrument_name_connection.block();
         dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));  
   
     int instrument_index = 0;  
     Gtk::RadioMenuItem::Group instrument_group;  
1128      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
1129           instrument = gig->GetNextInstrument()) {           instrument = gig->GetNextInstrument()) {
1130            Glib::ustring name(instrument->pInfo->Name);
1131    
1132          Gtk::TreeModel::iterator iter = m_refTreeModel->append();          Gtk::TreeModel::iterator iter = m_refTreeModel->append();
1133          Gtk::TreeModel::Row row = *iter;          Gtk::TreeModel::Row row = *iter;
1134          row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();          row[m_Columns.m_col_name] = name;
1135          row[m_Columns.m_col_instr] = instrument;          row[m_Columns.m_col_instr] = instrument;
1136          // create a menu item for this instrument  
1137          Gtk::RadioMenuItem* item =          add_instrument_to_menu(name);
             new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());  
         instrument_menu->get_submenu()->append(*item);  
         item->signal_activate().connect(  
             sigc::bind(  
                 sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),  
                 instrument_index  
             )  
         );  
         instrument_index++;  
1138      }      }
1139      instrument_menu->show();      instrument_name_connection.unblock();
1140      instrument_menu->get_submenu()->show_all_children();      uiManager->get_widget("/MenuBar/MenuInstrument")->show();
1141    
1142      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1143          if (group->Name != "") {          if (group->Name != "") {
# Line 1174  void MainWindow::load_gig(gig::File* gig Line 1161  void MainWindow::load_gig(gig::File* gig
1161      file = gig;      file = gig;
1162    
1163      // select the first instrument      // select the first instrument
1164      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      m_TreeView.get_selection()->select(Gtk::TreePath("0"));
     tree_sel_ref->select(Gtk::TreePath("0"));  
1165    
1166      gig::Instrument* instrument = get_instrument();      gig::Instrument* instrument = get_instrument();
1167      if (instrument) {      if (instrument) {
# Line 1214  void MainWindow::on_button_release(GdkEv Line 1200  void MainWindow::on_button_release(GdkEv
1200      }      }
1201  }  }
1202    
1203  void MainWindow::on_instrument_selection_change(int index) {  void MainWindow::on_instrument_selection_change(Gtk::RadioMenuItem* item) {
1204      m_RegionChooser.set_instrument(file->GetInstrument(index));      if (item->get_active()) {
1205            const std::vector<Gtk::Widget*> children =
1206                instrument_menu->get_children();
1207            std::vector<Gtk::Widget*>::const_iterator it =
1208                find(children.begin(), children.end(), item);
1209            if (it != children.end()) {
1210                int index = it - children.begin();
1211                m_TreeView.get_selection()->select(Gtk::TreePath(ToString(index)));
1212    
1213                m_RegionChooser.set_instrument(file->GetInstrument(index));
1214            }
1215        }
1216  }  }
1217    
1218  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
# Line 1245  void MainWindow::on_sample_treeview_butt Line 1242  void MainWindow::on_sample_treeview_butt
1242      }      }
1243  }  }
1244    
1245    
1246    Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu(
1247        const Glib::ustring& name, int position) {
1248    
1249        Gtk::RadioMenuItem::Group instrument_group;
1250        const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
1251        if (!children.empty()) {
1252            instrument_group =
1253                static_cast<Gtk::RadioMenuItem*>(children[0])->get_group();
1254        }
1255        Gtk::RadioMenuItem* item =
1256            new Gtk::RadioMenuItem(instrument_group, name);
1257        if (position < 0) {
1258            instrument_menu->append(*item);
1259        } else {
1260            instrument_menu->insert(*item, position);
1261        }
1262        item->show();
1263        item->signal_activate().connect(
1264            sigc::bind(
1265                sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
1266                item));
1267        return item;
1268    }
1269    
1270    void MainWindow::remove_instrument_from_menu(int index) {
1271        const std::vector<Gtk::Widget*> children =
1272            instrument_menu->get_children();
1273        Gtk::Widget* child = children[index];
1274        instrument_menu->remove(*child);
1275        delete child;
1276    }
1277    
1278    void MainWindow::add_instrument(gig::Instrument* instrument) {
1279        const char* name = instrument->pInfo->Name.c_str();
1280    
1281        // update instrument tree view
1282        instrument_name_connection.block();
1283        Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
1284        Gtk::TreeModel::Row rowInstr = *iterInstr;
1285        rowInstr[m_Columns.m_col_name] = name;
1286        rowInstr[m_Columns.m_col_instr] = instrument;
1287        instrument_name_connection.unblock();
1288    
1289        add_instrument_to_menu(name);
1290    
1291        m_TreeView.get_selection()->select(iterInstr);
1292    
1293        file_changed();
1294    }
1295    
1296  void MainWindow::on_action_add_instrument() {  void MainWindow::on_action_add_instrument() {
1297      static int __instrument_indexer = 0;      static int __instrument_indexer = 0;
1298      if (!file) return;      if (!file) return;
# Line 1252  void MainWindow::on_action_add_instrumen Line 1300  void MainWindow::on_action_add_instrumen
1300      __instrument_indexer++;      __instrument_indexer++;
1301      instrument->pInfo->Name =      instrument->pInfo->Name =
1302          _("Unnamed Instrument ") + ToString(__instrument_indexer);          _("Unnamed Instrument ") + ToString(__instrument_indexer);
1303      // update instrument tree view  
1304      Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();      add_instrument(instrument);
     Gtk::TreeModel::Row rowInstr = *iterInstr;  
     rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();  
     rowInstr[m_Columns.m_col_instr] = instrument;  
     file_changed();  
1305  }  }
1306    
1307  void MainWindow::on_action_duplicate_instrument() {  void MainWindow::on_action_duplicate_instrument() {
1308      if (!file) return;      if (!file) return;
1309        
1310      // retrieve the currently selected instrument      // retrieve the currently selected instrument
1311      // (being the original instrument to be duplicated)      // (being the original instrument to be duplicated)
1312      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
# Line 1271  void MainWindow::on_action_duplicate_ins Line 1315  void MainWindow::on_action_duplicate_ins
1315      Gtk::TreeModel::Row row = *itSelection;      Gtk::TreeModel::Row row = *itSelection;
1316      gig::Instrument* instrOrig = row[m_Columns.m_col_instr];      gig::Instrument* instrOrig = row[m_Columns.m_col_instr];
1317      if (!instrOrig) return;      if (!instrOrig) return;
1318        
1319      // duplicate the orginal instrument      // duplicate the orginal instrument
1320      gig::Instrument* instrNew = file->AddDuplicateInstrument(instrOrig);      gig::Instrument* instrNew = file->AddDuplicateInstrument(instrOrig);
1321      instrNew->pInfo->Name =      instrNew->pInfo->Name =
1322          instrOrig->pInfo->Name + " (" + _("Copy") + ")";          instrOrig->pInfo->Name + " (" + _("Copy") + ")";
1323            
1324      // update instrument tree view      add_instrument(instrNew);
     Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();  
     Gtk::TreeModel::Row rowInstr = *iterInstr;  
     rowInstr[m_Columns.m_col_name] = instrNew->pInfo->Name.c_str();  
     rowInstr[m_Columns.m_col_instr] = instrNew;  
     file_changed();  
1325  }  }
1326    
1327  void MainWindow::on_action_remove_instrument() {  void MainWindow::on_action_remove_instrument() {
# Line 1304  void MainWindow::on_action_remove_instru Line 1343  void MainWindow::on_action_remove_instru
1343          Gtk::TreeModel::Row row = *it;          Gtk::TreeModel::Row row = *it;
1344          gig::Instrument* instr = row[m_Columns.m_col_instr];          gig::Instrument* instr = row[m_Columns.m_col_instr];
1345          try {          try {
1346                Gtk::TreePath path(it);
1347                int index = path[0];
1348    
1349              // remove instrument from the gig file              // remove instrument from the gig file
1350              if (instr) file->DeleteInstrument(instr);              if (instr) file->DeleteInstrument(instr);
             // remove respective row from instruments tree view  
             m_refTreeModel->erase(it);  
1351              file_changed();              file_changed();
1352    
1353                remove_instrument_from_menu(index);
1354    
1355                // remove row from instruments tree view
1356                m_refTreeModel->erase(it);
1357    
1358    #if GTKMM_MAJOR_VERSION < 3
1359                // select another instrument (in gtk3 this is done
1360                // automatically)
1361                if (!m_refTreeModel->children().empty()) {
1362                    if (index == m_refTreeModel->children().size()) {
1363                        index--;
1364                    }
1365                    m_TreeView.get_selection()->select(
1366                        Gtk::TreePath(ToString(index)));
1367                }
1368    #endif
1369              instr = get_instrument();              instr = get_instrument();
1370              if (instr) {              if (instr) {
1371                  instrumentProps.set_instrument(instr);                  instrumentProps.set_instrument(instr);
# Line 1508  void MainWindow::on_action_add_sample() Line 1564  void MainWindow::on_action_add_sample()
1564                  sf_close(hFile);                  sf_close(hFile);
1565                  file_changed();                  file_changed();
1566              } catch (std::string what) { // remember the files that made trouble (and their cause)              } catch (std::string what) { // remember the files that made trouble (and their cause)
1567                  if (error_files.size()) error_files += "\n";                  if (!error_files.empty()) error_files += "\n";
1568                  error_files += *iter += " (" + what + ")";                  error_files += *iter += " (" + what + ")";
1569              }              }
1570          }          }
1571          // show error message box when some file(s) could not be opened / added          // show error message box when some file(s) could not be opened / added
1572          if (error_files.size()) {          if (!error_files.empty()) {
1573              Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files;              Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files;
1574              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1575              msg.run();              msg.run();
# Line 1526  void MainWindow::on_action_replace_all_s Line 1582  void MainWindow::on_action_replace_all_s
1582      if (!file) return;      if (!file) return;
1583      Gtk::FileChooserDialog dialog(*this, _("Select Folder"),      Gtk::FileChooserDialog dialog(*this, _("Select Folder"),
1584                                    Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);                                    Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
1585      const char* str =      const char* str =
1586          _("This is a very specific function. It tries to replace all samples "          _("This is a very specific function. It tries to replace all samples "
1587            "in the current gig file by samples located in the chosen "            "in the current gig file by samples located in the chosen "
1588            "directory.\n\n"            "directory.\n\n"
# Line 1606  void MainWindow::on_action_replace_all_s Line 1662  void MainWindow::on_action_replace_all_s
1662              }              }
1663              catch (std::string what)              catch (std::string what)
1664              {              {
1665                  if (error_files.size()) error_files += "\n";                  if (!error_files.empty()) error_files += "\n";
1666                      error_files += filename += " (" + what + ")";                      error_files += filename += " (" + what + ")";
1667              }              }
1668          }          }
1669          // show error message box when some file(s) could not be opened / added          // show error message box when some file(s) could not be opened / added
1670          if (error_files.size()) {          if (!error_files.empty()) {
1671              Glib::ustring txt =              Glib::ustring txt =
1672                  _("Could not replace the following sample(s):\n") + error_files;                  _("Could not replace the following sample(s):\n") + error_files;
1673              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
# Line 1821  void MainWindow::instrument_name_changed Line 1877  void MainWindow::instrument_name_changed
1877      if (!iter) return;      if (!iter) return;
1878      Gtk::TreeModel::Row row = *iter;      Gtk::TreeModel::Row row = *iter;
1879      Glib::ustring name = row[m_Columns.m_col_name];      Glib::ustring name = row[m_Columns.m_col_name];
1880    
1881        // change name in instrument menu
1882        int index = path[0];
1883        const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
1884        if (index < children.size()) {
1885    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 16) || GTKMM_MAJOR_VERSION > 2
1886            static_cast<Gtk::RadioMenuItem*>(children[index])->set_label(name);
1887    #else
1888            remove_instrument_from_menu(index);
1889            Gtk::RadioMenuItem* item = add_instrument_to_menu(name, index);
1890            item->set_active();
1891    #endif
1892        }
1893    
1894        // change name in gig
1895      gig::Instrument* instrument = row[m_Columns.m_col_instr];      gig::Instrument* instrument = row[m_Columns.m_col_instr];
1896      if (instrument && instrument->pInfo->Name != name.raw()) {      if (instrument && instrument->pInfo->Name != name.raw()) {
1897          instrument->pInfo->Name = name.raw();          instrument->pInfo->Name = name.raw();

Legend:
Removed from v.2423  
changed lines
  Added in v.2442

  ViewVC Help
Powered by ViewVC