/[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 2541 by schoenebeck, Wed Apr 23 16:49:05 2014 UTC revision 2715 by schoenebeck, Tue Jan 20 18:48:15 2015 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (C) 2006-2014 Andreas Persson   * Copyright (C) 2006-2015 Andreas Persson
3   *   *
4   * This program is free software; you can redistribute it and/or   * This program is free software; you can redistribute it and/or
5   * modify it under the terms of the GNU General Public License as   * modify it under the terms of the GNU General Public License as
# Line 40  Line 40 
40    
41  #include <stdio.h>  #include <stdio.h>
42  #include <sndfile.h>  #include <sndfile.h>
43    #include <assert.h>
44    
45  #include "mainwindow.h"  #include "mainwindow.h"
46  #include "Settings.h"  #include "Settings.h"
47    #include "CombineInstrumentsDialog.h"
48    #include "scripteditor.h"
49    #include "scriptslots.h"
50    #include "ReferencesView.h"
51  #include "../../gfx/status_attached.xpm"  #include "../../gfx/status_attached.xpm"
52  #include "../../gfx/status_detached.xpm"  #include "../../gfx/status_detached.xpm"
53    
54    
55  MainWindow::MainWindow() :  MainWindow::MainWindow() :
56        m_DimRegionChooser(*this),
57      dimreg_label(_("Changes apply to:")),      dimreg_label(_("Changes apply to:")),
58      dimreg_all_regions(_("all regions")),      dimreg_all_regions(_("all regions")),
59      dimreg_all_dimregs(_("all dimension splits")),      dimreg_all_dimregs(_("all dimension splits")),
# Line 76  MainWindow::MainWindow() : Line 82  MainWindow::MainWindow() :
82      m_ScrolledWindowSamples.add(m_TreeViewSamples);      m_ScrolledWindowSamples.add(m_TreeViewSamples);
83      m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
84    
85        m_ScrolledWindowScripts.add(m_TreeViewScripts);
86        m_ScrolledWindowScripts.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
87    
88    
89      m_TreeViewNotebook.set_size_request(300);      m_TreeViewNotebook.set_size_request(300);
90    
# Line 96  MainWindow::MainWindow() : Line 105  MainWindow::MainWindow() :
105    
106      m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, _("Samples"));      m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, _("Samples"));
107      m_TreeViewNotebook.append_page(m_ScrolledWindow, _("Instruments"));      m_TreeViewNotebook.append_page(m_ScrolledWindow, _("Instruments"));
108        m_TreeViewNotebook.append_page(m_ScrolledWindowScripts, _("Scripts"));
109    
110      actionGroup = Gtk::ActionGroup::create();      actionGroup = Gtk::ActionGroup::create();
111    
# Line 128  MainWindow::MainWindow() : Line 137  MainWindow::MainWindow() :
137                       sigc::mem_fun(                       sigc::mem_fun(
138                           *this, &MainWindow::show_instr_props));                           *this, &MainWindow::show_instr_props));
139      actionGroup->add(Gtk::Action::create("MidiRules",      actionGroup->add(Gtk::Action::create("MidiRules",
140                                           _("_Midi Rules")),                                           _("_Midi Rules...")),
141                       sigc::mem_fun(                       sigc::mem_fun(
142                           *this, &MainWindow::show_midi_rules));                           *this, &MainWindow::show_midi_rules));
143        actionGroup->add(Gtk::Action::create("ScriptSlots",
144                                             _("_Script Slots...")),
145                         sigc::mem_fun(
146                             *this, &MainWindow::show_script_slots));
147      actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),      actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),
148                       sigc::mem_fun(                       sigc::mem_fun(
149                           *this, &MainWindow::on_action_quit));                           *this, &MainWindow::on_action_quit));
150      actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument")));      actionGroup->add(
151            Gtk::Action::create("MenuSample", _("_Sample")),
152            sigc::mem_fun(*this, &MainWindow::show_samples_tab)
153        );
154        actionGroup->add(
155            Gtk::Action::create("MenuInstrument", _("_Instrument")),
156            sigc::mem_fun(*this, &MainWindow::show_intruments_tab)
157        );
158        actionGroup->add(
159            Gtk::Action::create("MenuScript", _("S_cript")),
160            sigc::mem_fun(*this, &MainWindow::show_scripts_tab)
161        );
162        actionGroup->add(Gtk::Action::create("AllInstruments", _("_Select")));
163    
164      actionGroup->add(Gtk::Action::create("MenuEdit", _("_Edit")));      actionGroup->add(Gtk::Action::create("MenuEdit", _("_Edit")));
165    
# Line 193  MainWindow::MainWindow() : Line 217  MainWindow::MainWindow() :
217          sigc::mem_fun(*this, &MainWindow::on_action_warn_user_on_extensions)          sigc::mem_fun(*this, &MainWindow::on_action_warn_user_on_extensions)
218      );      );
219    
220        toggle_action =
221            Gtk::ToggleAction::create("SyncSamplerInstrumentSelection", _("Synchronize sampler's instrument selection"));
222        toggle_action->set_active(Settings::singleton()->syncSamplerInstrumentSelection);
223        actionGroup->add(
224            toggle_action,
225            sigc::mem_fun(*this, &MainWindow::on_action_sync_sampler_instrument_selection)
226        );
227    
228    
229        actionGroup->add(Gtk::Action::create("MenuTools", _("_Tools")));
230    
231        actionGroup->add(
232            Gtk::Action::create("CombineInstruments", _("_Combine Instruments...")),
233            sigc::mem_fun(*this, &MainWindow::on_action_combine_instruments)
234        );
235    
236        actionGroup->add(
237            Gtk::Action::create("MergeFiles", _("_Merge Files...")),
238            sigc::mem_fun(*this, &MainWindow::on_action_merge_files)
239        );
240    
241    
242      // sample right-click popup actions      // sample right-click popup actions
243      actionGroup->add(      actionGroup->add(
# Line 212  MainWindow::MainWindow() : Line 257  MainWindow::MainWindow() :
257          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
258      );      );
259      actionGroup->add(      actionGroup->add(
260            Gtk::Action::create("ShowSampleRefs", _("Show References...")),
261            sigc::mem_fun(*this, &MainWindow::on_action_view_references)
262        );
263        actionGroup->add(
264            Gtk::Action::create("ReplaceSample",
265                                _("Replace Sample...")),
266            sigc::mem_fun(*this, &MainWindow::on_action_replace_sample)
267        );
268        actionGroup->add(
269          Gtk::Action::create("ReplaceAllSamplesInAllGroups",          Gtk::Action::create("ReplaceAllSamplesInAllGroups",
270                              _("Replace All Samples in All Groups...")),                              _("Replace All Samples in All Groups...")),
271          sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups)          sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups)
272      );      );
273        
274        // script right-click popup actions
275        actionGroup->add(
276            Gtk::Action::create("AddScriptGroup", _("Add _Group")),
277            sigc::mem_fun(*this, &MainWindow::on_action_add_script_group)
278        );
279        actionGroup->add(
280            Gtk::Action::create("AddScript", _("Add _Script")),
281            sigc::mem_fun(*this, &MainWindow::on_action_add_script)
282        );
283        actionGroup->add(
284            Gtk::Action::create("EditScript", _("_Edit Script...")),
285            sigc::mem_fun(*this, &MainWindow::on_action_edit_script)
286        );
287        actionGroup->add(
288            Gtk::Action::create("RemoveScript", Gtk::Stock::REMOVE),
289            sigc::mem_fun(*this, &MainWindow::on_action_remove_script)
290        );
291    
292      uiManager = Gtk::UIManager::create();      uiManager = Gtk::UIManager::create();
293      uiManager->insert_action_group(actionGroup);      uiManager->insert_action_group(actionGroup);
# Line 240  MainWindow::MainWindow() : Line 312  MainWindow::MainWindow() :
312          "      <menuitem action='CopySampleTune'/>"          "      <menuitem action='CopySampleTune'/>"
313          "      <menuitem action='CopySampleLoop'/>"          "      <menuitem action='CopySampleLoop'/>"
314          "    </menu>"          "    </menu>"
315            "    <menu action='MenuSample'>"
316            "      <menuitem action='SampleProperties'/>"
317            "      <menuitem action='AddGroup'/>"
318            "      <menuitem action='AddSample'/>"
319            "      <menuitem action='ShowSampleRefs'/>"
320            "      <menuitem action='ReplaceSample' />"
321            "      <menuitem action='ReplaceAllSamplesInAllGroups' />"
322            "      <separator/>"
323            "      <menuitem action='RemoveSample'/>"
324            "    </menu>"
325          "    <menu action='MenuInstrument'>"          "    <menu action='MenuInstrument'>"
326            "      <menu action='AllInstruments'>"
327            "      </menu>"
328            "      <separator/>"
329            "      <menuitem action='InstrProperties'/>"
330            "      <menuitem action='MidiRules'/>"
331            "      <menuitem action='ScriptSlots'/>"
332            "      <menuitem action='AddInstrument'/>"
333            "      <menuitem action='DupInstrument'/>"
334            "      <separator/>"
335            "      <menuitem action='RemoveInstrument'/>"
336            "    </menu>"
337            "    <menu action='MenuScript'>"
338            "      <menuitem action='AddScriptGroup'/>"
339            "      <menuitem action='AddScript'/>"
340            "      <menuitem action='EditScript'/>"
341            "      <separator/>"
342            "      <menuitem action='RemoveScript'/>"
343          "    </menu>"          "    </menu>"
344          "    <menu action='MenuView'>"          "    <menu action='MenuView'>"
345          "      <menuitem action='Statusbar'/>"          "      <menuitem action='Statusbar'/>"
346          "    </menu>"          "    </menu>"
347            "    <menu action='MenuTools'>"
348            "      <menuitem action='CombineInstruments'/>"
349            "      <menuitem action='MergeFiles'/>"
350            "    </menu>"
351          "    <menu action='MenuSettings'>"          "    <menu action='MenuSettings'>"
352          "      <menuitem action='WarnUserOnExtensions'/>"          "      <menuitem action='WarnUserOnExtensions'/>"
353            "      <menuitem action='SyncSamplerInstrumentSelection'/>"
354          "    </menu>"          "    </menu>"
355          "    <menu action='MenuHelp'>"          "    <menu action='MenuHelp'>"
356          "      <menuitem action='About'/>"          "      <menuitem action='About'/>"
# Line 255  MainWindow::MainWindow() : Line 359  MainWindow::MainWindow() :
359          "  <popup name='PopupMenu'>"          "  <popup name='PopupMenu'>"
360          "    <menuitem action='InstrProperties'/>"          "    <menuitem action='InstrProperties'/>"
361          "    <menuitem action='MidiRules'/>"          "    <menuitem action='MidiRules'/>"
362            "    <menuitem action='ScriptSlots'/>"
363          "    <menuitem action='AddInstrument'/>"          "    <menuitem action='AddInstrument'/>"
364          "    <menuitem action='DupInstrument'/>"          "    <menuitem action='DupInstrument'/>"
365          "    <separator/>"          "    <separator/>"
# Line 264  MainWindow::MainWindow() : Line 369  MainWindow::MainWindow() :
369          "    <menuitem action='SampleProperties'/>"          "    <menuitem action='SampleProperties'/>"
370          "    <menuitem action='AddGroup'/>"          "    <menuitem action='AddGroup'/>"
371          "    <menuitem action='AddSample'/>"          "    <menuitem action='AddSample'/>"
372            "    <menuitem action='ShowSampleRefs'/>"
373            "    <menuitem action='ReplaceSample' />"
374          "    <menuitem action='ReplaceAllSamplesInAllGroups' />"          "    <menuitem action='ReplaceAllSamplesInAllGroups' />"
375          "    <separator/>"          "    <separator/>"
376          "    <menuitem action='RemoveSample'/>"          "    <menuitem action='RemoveSample'/>"
377          "  </popup>"          "  </popup>"
378            "  <popup name='ScriptPopupMenu'>"
379            "    <menuitem action='AddScriptGroup'/>"
380            "    <menuitem action='AddScript'/>"
381            "    <menuitem action='EditScript'/>"
382            "    <separator/>"
383            "    <menuitem action='RemoveScript'/>"
384            "  </popup>"
385          "</ui>";          "</ui>";
386      uiManager->add_ui_from_string(ui_info);      uiManager->add_ui_from_string(ui_info);
387    
# Line 297  MainWindow::MainWindow() : Line 411  MainWindow::MainWindow() :
411              uiManager->get_widget("/MenuBar/MenuSettings/WarnUserOnExtensions"));              uiManager->get_widget("/MenuBar/MenuSettings/WarnUserOnExtensions"));
412          item->set_tooltip_text(_("If checked, a warning will be shown whenever you try to use a feature which is based on a LinuxSampler extension ontop of the original gig format, which would not work with the Gigasampler/GigaStudio application."));          item->set_tooltip_text(_("If checked, a warning will be shown whenever you try to use a feature which is based on a LinuxSampler extension ontop of the original gig format, which would not work with the Gigasampler/GigaStudio application."));
413      }      }
414        {
415            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
416                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
417            item->set_tooltip_text(_("If checked, the sampler's current instrument will automatically be switched whenever another instrument was selected in gigedit (only available in live-mode)."));
418        }
419        {
420            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
421                uiManager->get_widget("/MenuBar/MenuTools/CombineInstruments"));
422            item->set_tooltip_text(_("Create combi sounds out of individual sounds of this .gig file."));
423        }
424        {
425            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
426                uiManager->get_widget("/MenuBar/MenuTools/MergeFiles"));
427            item->set_tooltip_text(_("Add instruments and samples of other .gig files to this .gig file."));
428        }
429    
430    
431      instrument_menu = static_cast<Gtk::MenuItem*>(      instrument_menu = static_cast<Gtk::MenuItem*>(
432          uiManager->get_widget("/MenuBar/MenuInstrument"))->get_submenu();          uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments"))->get_submenu();
433    
434      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
435      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
# Line 325  MainWindow::MainWindow() : Line 455  MainWindow::MainWindow() :
455      // Create the Tree model:      // Create the Tree model:
456      m_refTreeModel = Gtk::ListStore::create(m_Columns);      m_refTreeModel = Gtk::ListStore::create(m_Columns);
457      m_TreeView.set_model(m_refTreeModel);      m_TreeView.set_model(m_refTreeModel);
458      m_TreeView.set_tooltip_text(_("Right click here for actions on instruments & MIDI Rules."));      m_TreeView.set_tooltip_text(_("Right click here for actions on instruments & MIDI Rules. Drag & drop to change the order of instruments."));
459      instrument_name_connection = m_refTreeModel->signal_row_changed().connect(      instrument_name_connection = m_refTreeModel->signal_row_changed().connect(
460          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
461      );      );
# Line 333  MainWindow::MainWindow() : Line 463  MainWindow::MainWindow() :
463      // Add the TreeView's view columns:      // Add the TreeView's view columns:
464      m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);      m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);
465      m_TreeView.set_headers_visible(false);      m_TreeView.set_headers_visible(false);
466        
467        // establish drag&drop within the instrument tree view, allowing to reorder
468        // the sequence of instruments within the gig file
469        {
470            std::vector<Gtk::TargetEntry> drag_target_instrument;
471            drag_target_instrument.push_back(Gtk::TargetEntry("gig::Instrument"));
472            m_TreeView.drag_source_set(drag_target_instrument);
473            m_TreeView.drag_dest_set(drag_target_instrument);
474            m_TreeView.signal_drag_begin().connect(
475                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_begin)
476            );
477            m_TreeView.signal_drag_data_get().connect(
478                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_data_get)
479            );
480            m_TreeView.signal_drag_data_received().connect(
481                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drop_drag_data_received)
482            );
483        }
484    
485      // create samples treeview (including its data model)      // create samples treeview (including its data model)
486      m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);      m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);
487      m_TreeViewSamples.set_model(m_refSamplesTreeModel);      m_TreeViewSamples.set_model(m_refSamplesTreeModel);
488      m_TreeViewSamples.set_tooltip_text(_("To actually use a sample, drag it from this list view to \"Sample\" -> \"Sample:\" on the region's settings pane on the right.\n\nRight click here for more actions on samples."));      m_TreeViewSamples.set_tooltip_text(_("To actually use a sample, drag it from this list view to \"Sample\" -> \"Sample:\" on the region's settings pane on the right.\n\nRight click here for more actions on samples."));
489      // m_TreeViewSamples.set_reorderable();      // m_TreeViewSamples.set_reorderable();
490      m_TreeViewSamples.append_column_editable("Samples", m_SamplesModel.m_col_name);      m_TreeViewSamples.append_column_editable(_("Name"), m_SamplesModel.m_col_name);
491      m_TreeViewSamples.set_headers_visible(false);      m_TreeViewSamples.append_column(_("Referenced"), m_SamplesModel.m_col_refcount);
492        {
493            Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(0);
494            Gtk::CellRendererText* cellrenderer =
495                dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell());
496            column->add_attribute(
497                cellrenderer->property_foreground(), m_SamplesModel.m_color
498            );
499        }
500        {
501            Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(1);
502            Gtk::CellRendererText* cellrenderer =
503                dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell());
504            column->add_attribute(
505                cellrenderer->property_foreground(), m_SamplesModel.m_color
506            );
507        }
508        m_TreeViewSamples.set_headers_visible(true);
509      m_TreeViewSamples.signal_button_press_event().connect_notify(      m_TreeViewSamples.signal_button_press_event().connect_notify(
510          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
511      );      );
# Line 348  MainWindow::MainWindow() : Line 513  MainWindow::MainWindow() :
513          sigc::mem_fun(*this, &MainWindow::sample_name_changed)          sigc::mem_fun(*this, &MainWindow::sample_name_changed)
514      );      );
515    
516        // create scripts treeview (including its data model)
517        m_refScriptsTreeModel = ScriptsTreeStore::create(m_ScriptsModel);
518        m_TreeViewScripts.set_model(m_refScriptsTreeModel);
519        m_TreeViewScripts.set_tooltip_text(_(
520            "Use CTRL + double click for editing a script."
521            "\n\n"
522            "Note: instrument scripts are a LinuxSampler extension of the gig "
523            "format. This feature will not work with the GigaStudio software!"
524        ));
525        // m_TreeViewScripts.set_reorderable();
526        m_TreeViewScripts.append_column_editable("Samples", m_ScriptsModel.m_col_name);
527        m_TreeViewScripts.set_headers_visible(false);
528        m_TreeViewScripts.signal_button_press_event().connect_notify(
529            sigc::mem_fun(*this, &MainWindow::on_script_treeview_button_release)
530        );
531        //FIXME: why the heck does this double click signal_row_activated() only fire while CTRL key is pressed ?
532        m_TreeViewScripts.signal_row_activated().connect(
533            sigc::mem_fun(*this, &MainWindow::script_double_clicked)
534        );
535        m_refScriptsTreeModel->signal_row_changed().connect(
536            sigc::mem_fun(*this, &MainWindow::script_name_changed)
537        );
538    
539        // establish drag&drop between scripts tree view and ScriptSlots window
540        std::vector<Gtk::TargetEntry> drag_target_gig_script;
541        drag_target_gig_script.push_back(Gtk::TargetEntry("gig::Script"));
542        m_TreeViewScripts.drag_source_set(drag_target_gig_script);
543        m_TreeViewScripts.signal_drag_begin().connect(
544            sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_begin)
545        );
546        m_TreeViewScripts.signal_drag_data_get().connect(
547            sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_data_get)
548        );
549    
550      // establish drag&drop between samples tree view and dimension region 'Sample' text entry      // establish drag&drop between samples tree view and dimension region 'Sample' text entry
551      std::vector<Gtk::TargetEntry> drag_target_gig_sample;      std::vector<Gtk::TargetEntry> drag_target_gig_sample;
552      drag_target_gig_sample.push_back(Gtk::TargetEntry("gig::Sample"));      drag_target_gig_sample.push_back(Gtk::TargetEntry("gig::Sample"));
# Line 381  MainWindow::MainWindow() : Line 580  MainWindow::MainWindow() :
580          dimreg_changed_signal.make_slot());          dimreg_changed_signal.make_slot());
581      dimreg_edit.signal_sample_ref_changed().connect(      dimreg_edit.signal_sample_ref_changed().connect(
582          sample_ref_changed_signal.make_slot());          sample_ref_changed_signal.make_slot());
583        sample_ref_changed_signal.connect(
584            sigc::mem_fun(*this, &MainWindow::on_sample_ref_changed)
585        );
586        samples_to_be_removed_signal.connect(
587            sigc::mem_fun(*this, &MainWindow::on_samples_to_be_removed)
588        );
589    
590        dimreg_edit.signal_select_sample().connect(
591            sigc::mem_fun(*this, &MainWindow::select_sample)
592        );
593    
594      m_RegionChooser.signal_instrument_struct_to_be_changed().connect(      m_RegionChooser.signal_instrument_struct_to_be_changed().connect(
595          sigc::hide(          sigc::hide(
# Line 422  MainWindow::MainWindow() : Line 631  MainWindow::MainWindow() :
631    
632      // start with a new gig file by default      // start with a new gig file by default
633      on_action_file_new();      on_action_file_new();
634    
635        // select 'Instruments' tab by default
636        // (gtk allows this only if the tab childs are visible, thats why it's here)
637        m_TreeViewNotebook.set_current_page(1);
638  }  }
639    
640  MainWindow::~MainWindow()  MainWindow::~MainWindow()
# Line 502  void MainWindow::dimreg_all_dimregs_togg Line 715  void MainWindow::dimreg_all_dimregs_togg
715  void MainWindow::dimreg_changed()  void MainWindow::dimreg_changed()
716  {  {
717      update_dimregs();      update_dimregs();
718      dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());      dimreg_edit.set_dim_region(m_DimRegionChooser.get_main_dimregion());
719  }  }
720    
721  void MainWindow::on_sel_change()  void MainWindow::on_sel_change()
# Line 518  void MainWindow::on_sel_change() Line 731  void MainWindow::on_sel_change()
731      }      }
732    
733      m_RegionChooser.set_instrument(get_instrument());      m_RegionChooser.set_instrument(get_instrument());
734    
735        if (Settings::singleton()->syncSamplerInstrumentSelection) {
736            switch_sampler_instrument_signal.emit(get_instrument());
737        }
738  }  }
739    
740  void loader_progress_callback(gig::progress_t* progress)  void loader_progress_callback(gig::progress_t* progress)
# Line 538  void Loader::progress_callback(float fra Line 755  void Loader::progress_callback(float fra
755  void Loader::thread_function()  void Loader::thread_function()
756  {  {
757      printf("thread_function self=%x\n", Glib::Threads::Thread::self());      printf("thread_function self=%x\n", Glib::Threads::Thread::self());
758      printf("Start %s\n", filename);      printf("Start %s\n", filename.c_str());
759      RIFF::File* riff = new RIFF::File(filename);      try {
760      gig = new gig::File(riff);          RIFF::File* riff = new RIFF::File(filename);
761      gig::progress_t progress;          gig = new gig::File(riff);
762      progress.callback = loader_progress_callback;          gig::progress_t progress;
763      progress.custom = this;          progress.callback = loader_progress_callback;
764            progress.custom = this;
765      gig->GetInstrument(0, &progress);  
766      printf("End\n");          gig->GetInstrument(0, &progress);
767      finished_dispatcher();          printf("End\n");
768            finished_dispatcher();
769        } catch (RIFF::Exception e) {
770            error_message = e.Message;
771            error_dispatcher.emit();
772        } catch (...) {
773            error_message = _("Unknown exception occurred");
774            error_dispatcher.emit();
775        }
776  }  }
777    
778  Loader::Loader(const char* filename)  Loader::Loader(const char* filename)
779      : filename(filename), thread(0)      : filename(filename), thread(0), progress(0.f)
780  {  {
781  }  }
782    
# Line 585  Glib::Dispatcher& Loader::signal_finishe Line 810  Glib::Dispatcher& Loader::signal_finishe
810      return finished_dispatcher;      return finished_dispatcher;
811  }  }
812    
813  LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)  Glib::Dispatcher& Loader::signal_error()
814    {
815        return error_dispatcher;
816    }
817    
818    void saver_progress_callback(gig::progress_t* progress)
819    {
820        Saver* saver = static_cast<Saver*>(progress->custom);
821        saver->progress_callback(progress->factor);
822    }
823    
824    void Saver::progress_callback(float fraction)
825    {
826        {
827            Glib::Threads::Mutex::Lock lock(progressMutex);
828            progress = fraction;
829        }
830        progress_dispatcher.emit();
831    }
832    
833    void Saver::thread_function()
834    {
835        printf("thread_function self=%x\n", Glib::Threads::Thread::self());
836        printf("Start %s\n", filename.c_str());
837        try {
838            gig::progress_t progress;
839            progress.callback = saver_progress_callback;
840            progress.custom = this;
841    
842            // if no filename was provided, that means "save", if filename was provided means "save as"
843            if (filename.empty()) {
844                gig->Save(&progress);
845            } else {
846                gig->Save(filename, &progress);
847            }
848    
849            printf("End\n");
850            finished_dispatcher.emit();
851        } catch (RIFF::Exception e) {
852            error_message = e.Message;
853            error_dispatcher.emit();
854        } catch (...) {
855            error_message = _("Unknown exception occurred");
856            error_dispatcher.emit();
857        }
858    }
859    
860    Saver::Saver(gig::File* file, Glib::ustring filename)
861        : gig(file), filename(filename), thread(0), progress(0.f)
862    {
863    }
864    
865    void Saver::launch()
866    {
867    #ifdef OLD_THREADS
868        thread = Glib::Thread::create(sigc::mem_fun(*this, &Saver::thread_function), true);
869    #else
870        thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Saver::thread_function));
871    #endif
872        printf("launch thread=%x\n", thread);
873    }
874    
875    float Saver::get_progress()
876    {
877        float res;
878        {
879            Glib::Threads::Mutex::Lock lock(progressMutex);
880            res = progress;
881        }
882        return res;
883    }
884    
885    Glib::Dispatcher& Saver::signal_progress()
886    {
887        return progress_dispatcher;
888    }
889    
890    Glib::Dispatcher& Saver::signal_finished()
891    {
892        return finished_dispatcher;
893    }
894    
895    Glib::Dispatcher& Saver::signal_error()
896    {
897        return error_dispatcher;
898    }
899    
900    ProgressDialog::ProgressDialog(const Glib::ustring& title, Gtk::Window& parent)
901      : Gtk::Dialog(title, parent, true)      : Gtk::Dialog(title, parent, true)
902  {  {
903      get_vbox()->pack_start(progressBar);      get_vbox()->pack_start(progressBar);
904      show_all_children();      show_all_children();
905        resize(600,50);
906  }  }
907    
908  // Clear all GUI elements / controls. This method is typically called  // Clear all GUI elements / controls. This method is typically called
# Line 600  void MainWindow::__clear() { Line 913  void MainWindow::__clear() {
913      // clear the samples and instruments tree views      // clear the samples and instruments tree views
914      m_refTreeModel->clear();      m_refTreeModel->clear();
915      m_refSamplesTreeModel->clear();      m_refSamplesTreeModel->clear();
916        m_refScriptsTreeModel->clear();
917      // remove all entries from "Instrument" menu      // remove all entries from "Instrument" menu
918      while (!instrument_menu->get_children().empty()) {      while (!instrument_menu->get_children().empty()) {
919          remove_instrument_from_menu(0);          remove_instrument_from_menu(0);
# Line 610  void MainWindow::__clear() { Line 924  void MainWindow::__clear() {
924      set_file_is_shared(false);      set_file_is_shared(false);
925  }  }
926    
927    void MainWindow::__refreshEntireGUI() {
928        // clear the samples and instruments tree views
929        m_refTreeModel->clear();
930        m_refSamplesTreeModel->clear();
931        m_refScriptsTreeModel->clear();
932        // remove all entries from "Instrument" menu
933        while (!instrument_menu->get_children().empty()) {
934            remove_instrument_from_menu(0);
935        }
936    
937        if (!this->file) return;
938    
939        load_gig(
940            this->file, this->file->pInfo->Name.c_str(), this->file_is_shared
941        );
942    }
943    
944  void MainWindow::on_action_file_new()  void MainWindow::on_action_file_new()
945  {  {
946      if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;      if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
# Line 640  bool MainWindow::close_confirmation_dial Line 971  bool MainWindow::close_confirmation_dial
971      dialog.set_default_response(Gtk::RESPONSE_YES);      dialog.set_default_response(Gtk::RESPONSE_YES);
972      int response = dialog.run();      int response = dialog.run();
973      dialog.hide();      dialog.hide();
974      if (response == Gtk::RESPONSE_YES) return file_save();  
975      return response != Gtk::RESPONSE_CANCEL;      // user decided to exit app without saving
976        if (response == Gtk::RESPONSE_NO) return true;
977    
978        // user cancelled dialog, thus don't close app
979        if (response == Gtk::RESPONSE_CANCEL) return false;
980    
981        // TODO: the following return valid is disabled and hard coded instead for
982        // now, due to the fact that saving with progress bar is now implemented
983        // asynchronously, as a result the app does not close automatically anymore
984        // after saving the file has completed
985        //
986        //   if (response == Gtk::RESPONSE_YES) return file_save();
987        //   return response != Gtk::RESPONSE_CANCEL;
988        //
989        if (response == Gtk::RESPONSE_YES) file_save();
990        return false; // always prevent closing the app for now (see comment above)
991  }  }
992    
993  bool MainWindow::leaving_shared_mode_dialog() {  bool MainWindow::leaving_shared_mode_dialog() {
# Line 692  void MainWindow::on_action_file_open() Line 1038  void MainWindow::on_action_file_open()
1038  void MainWindow::load_file(const char* name)  void MainWindow::load_file(const char* name)
1039  {  {
1040      __clear();      __clear();
1041      load_dialog = new LoadDialog(_("Loading..."), *this);  
1042      load_dialog->show_all();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1043      loader = new Loader(strdup(name));          _("Loading") +  Glib::ustring(" '") +
1044            Glib::filename_display_basename(name) + "' ...",
1045            *this
1046        );
1047        progress_dialog->show_all();
1048        loader = new Loader(name); //FIXME: memory leak!
1049      loader->signal_progress().connect(      loader->signal_progress().connect(
1050          sigc::mem_fun(*this, &MainWindow::on_loader_progress));          sigc::mem_fun(*this, &MainWindow::on_loader_progress));
1051      loader->signal_finished().connect(      loader->signal_finished().connect(
1052          sigc::mem_fun(*this, &MainWindow::on_loader_finished));          sigc::mem_fun(*this, &MainWindow::on_loader_finished));
1053        loader->signal_error().connect(
1054            sigc::mem_fun(*this, &MainWindow::on_loader_error));
1055      loader->launch();      loader->launch();
1056  }  }
1057    
# Line 714  void MainWindow::load_instrument(gig::In Line 1067  void MainWindow::load_instrument(gig::In
1067      // load the instrument      // load the instrument
1068      gig::File* pFile = (gig::File*) instr->GetParent();      gig::File* pFile = (gig::File*) instr->GetParent();
1069      load_gig(pFile, 0 /*file name*/, true /*shared instrument*/);      load_gig(pFile, 0 /*file name*/, true /*shared instrument*/);
1070      //TODO: automatically select the given instrument      // automatically select the given instrument
1071        int i = 0;
1072        for (gig::Instrument* instrument = pFile->GetFirstInstrument(); instrument;
1073             instrument = pFile->GetNextInstrument(), ++i)
1074        {
1075            if (instrument == instr) {
1076                // select item in "instruments" tree view
1077                m_TreeView.get_selection()->select(Gtk::TreePath(ToString(i)));
1078                // make sure the selected item in the "instruments" tree view is
1079                // visible (scroll to it)
1080                m_TreeView.scroll_to_row(Gtk::TreePath(ToString(i)));
1081                // select item in instrument menu
1082                {
1083                    const std::vector<Gtk::Widget*> children =
1084                        instrument_menu->get_children();
1085                    static_cast<Gtk::RadioMenuItem*>(children[i])->set_active();
1086                }
1087                // update region chooser and dimension region chooser
1088                m_RegionChooser.set_instrument(instr);
1089                break;
1090            }
1091        }
1092  }  }
1093    
1094  void MainWindow::on_loader_progress()  void MainWindow::on_loader_progress()
1095  {  {
1096      load_dialog->set_fraction(loader->get_progress());      progress_dialog->set_fraction(loader->get_progress());
1097  }  }
1098    
1099  void MainWindow::on_loader_finished()  void MainWindow::on_loader_finished()
1100  {  {
1101      printf("Loader finished!\n");      printf("Loader finished!\n");
1102      printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());      printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());
1103      load_gig(loader->gig, loader->filename);      load_gig(loader->gig, loader->filename.c_str());
1104      load_dialog->hide();      progress_dialog->hide();
1105    }
1106    
1107    void MainWindow::on_loader_error()
1108    {
1109        Glib::ustring txt = _("Could not load file: ") + loader->error_message;
1110        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1111        msg.run();
1112        progress_dialog->hide();
1113  }  }
1114    
1115  void MainWindow::on_action_file_save()  void MainWindow::on_action_file_save()
# Line 766  bool MainWindow::file_save() Line 1148  bool MainWindow::file_save()
1148    
1149      std::cout << "Saving file\n" << std::flush;      std::cout << "Saving file\n" << std::flush;
1150      file_structure_to_be_changed_signal.emit(this->file);      file_structure_to_be_changed_signal.emit(this->file);
1151      try {  
1152          file->Save();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1153          if (file_is_changed) {          _("Saving") +  Glib::ustring(" '") +
1154              set_title(get_title().substr(1));          Glib::filename_display_basename(this->filename) + "' ...",
1155              file_is_changed = false;          *this
1156          }      );
1157      } catch (RIFF::Exception e) {      progress_dialog->show_all();
1158          file_structure_changed_signal.emit(this->file);      saver = new Saver(this->file); //FIXME: memory leak!
1159          Glib::ustring txt = _("Could not save file: ") + e.Message;      saver->signal_progress().connect(
1160          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);          sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1161          msg.run();      saver->signal_finished().connect(
1162          return false;          sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1163      }      saver->signal_error().connect(
1164      std::cout << "Saving file done\n" << std::flush;          sigc::mem_fun(*this, &MainWindow::on_saver_error));
1165        saver->launch();
1166    
1167        return true;
1168    }
1169    
1170    void MainWindow::on_saver_progress()
1171    {
1172        progress_dialog->set_fraction(saver->get_progress());
1173    }
1174    
1175    void MainWindow::on_saver_error()
1176    {
1177        file_structure_changed_signal.emit(this->file);
1178        Glib::ustring txt = _("Could not save file: ") + saver->error_message;
1179        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1180        msg.run();
1181    }
1182    
1183    void MainWindow::on_saver_finished()
1184    {
1185        this->file = saver->gig;
1186        this->filename = saver->filename;
1187        current_gig_dir = Glib::path_get_dirname(filename);
1188        set_title(Glib::filename_display_basename(filename));
1189        file_has_name = true;
1190        file_is_changed = false;
1191        std::cout << "Saving file done. Importing queued samples now ...\n" << std::flush;
1192      __import_queued_samples();      __import_queued_samples();
1193        std::cout << "Importing queued samples done.\n" << std::flush;
1194    
1195      file_structure_changed_signal.emit(this->file);      file_structure_changed_signal.emit(this->file);
1196      return true;  
1197        __refreshEntireGUI();
1198        progress_dialog->hide();
1199  }  }
1200    
1201  void MainWindow::on_action_file_save_as()  void MainWindow::on_action_file_save_as()
# Line 847  bool MainWindow::file_save_as() Line 1260  bool MainWindow::file_save_as()
1260      descriptionArea.show_all();      descriptionArea.show_all();
1261    
1262      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1263          file_structure_to_be_changed_signal.emit(this->file);          std::string filename = dialog.get_filename();
1264          try {          if (!Glib::str_has_suffix(filename, ".gig")) {
1265              std::string filename = dialog.get_filename();              filename += ".gig";
             if (!Glib::str_has_suffix(filename, ".gig")) {  
                 filename += ".gig";  
             }  
             printf("filename=%s\n", filename.c_str());  
             file->Save(filename);  
             this->filename = filename;  
             current_gig_dir = Glib::path_get_dirname(filename);  
             set_title(Glib::filename_display_basename(filename));  
             file_has_name = true;  
             file_is_changed = false;  
         } catch (RIFF::Exception e) {  
             file_structure_changed_signal.emit(this->file);  
             Glib::ustring txt = _("Could not save file: ") + e.Message;  
             Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);  
             msg.run();  
             return false;  
1266          }          }
1267          __import_queued_samples();          printf("filename=%s\n", filename.c_str());
1268          file_structure_changed_signal.emit(this->file);  
1269            progress_dialog = new ProgressDialog( //FIXME: memory leak!
1270                _("Saving") +  Glib::ustring(" '") +
1271                Glib::filename_display_basename(filename) + "' ...",
1272                *this
1273            );
1274            progress_dialog->show_all();
1275    
1276            saver = new Saver(file, filename); //FIXME: memory leak!
1277            saver->signal_progress().connect(
1278                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1279            saver->signal_finished().connect(
1280                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1281            saver->signal_error().connect(
1282                sigc::mem_fun(*this, &MainWindow::on_saver_error));
1283            saver->launch();
1284    
1285          return true;          return true;
1286      }      }
1287      return false;      return false;
# Line 979  void MainWindow::on_action_warn_user_on_ Line 1392  void MainWindow::on_action_warn_user_on_
1392          !Settings::singleton()->warnUserOnExtensions;          !Settings::singleton()->warnUserOnExtensions;
1393  }  }
1394    
1395    void MainWindow::on_action_sync_sampler_instrument_selection() {
1396        Settings::singleton()->syncSamplerInstrumentSelection =
1397            !Settings::singleton()->syncSamplerInstrumentSelection;
1398    }
1399    
1400  void MainWindow::on_action_help_about()  void MainWindow::on_action_help_about()
1401  {  {
1402      Gtk::AboutDialog dialog;      Gtk::AboutDialog dialog;
# Line 988  void MainWindow::on_action_help_about() Line 1406  void MainWindow::on_action_help_about()
1406      dialog.set_name("Gigedit");      dialog.set_name("Gigedit");
1407  #endif  #endif
1408      dialog.set_version(VERSION);      dialog.set_version(VERSION);
1409      dialog.set_copyright("Copyright (C) 2006-2014 Andreas Persson");      dialog.set_copyright("Copyright (C) 2006-2015 Andreas Persson");
1410      const std::string sComment =      const std::string sComment =
1411          _("Built " __DATE__ "\nUsing ") +          _("Built " __DATE__ "\nUsing ") +
1412          ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +          ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +
1413          _(          _(
1414              "Gigedit is released under the GNU General Public License.\n"              "Gigedit is released under the GNU General Public License.\n"
1415              "\n"              "\n"
1416              "Please notice that this is still a very young instrument editor. "              "This program is distributed WITHOUT ANY WARRANTY; So better "
1417              "So better backup your Gigasampler files before editing them with "              "backup your Gigasampler/GigaStudio files before editing them with "
1418              "this application.\n"              "this application.\n"
1419              "\n"              "\n"
1420              "Please report bugs to: http://bugs.linuxsampler.org"              "Please report bugs to: http://bugs.linuxsampler.org"
# Line 1008  void MainWindow::on_action_help_about() Line 1426  void MainWindow::on_action_help_about()
1426  }  }
1427    
1428  PropDialog::PropDialog()  PropDialog::PropDialog()
1429      : eName(_("Name")),      : eFileFormat(_("File Format")),
1430          eName(_("Name")),
1431        eCreationDate(_("Creation date")),        eCreationDate(_("Creation date")),
1432        eComments(_("Comments")),        eComments(_("Comments")),
1433        eProduct(_("Product")),        eProduct(_("Product")),
# Line 1025  PropDialog::PropDialog() Line 1444  PropDialog::PropDialog()
1444        eCommissioned(_("Commissioned")),        eCommissioned(_("Commissioned")),
1445        eSubject(_("Subject")),        eSubject(_("Subject")),
1446        quitButton(Gtk::Stock::CLOSE),        quitButton(Gtk::Stock::CLOSE),
1447        table(2, 1)        table(2, 1),
1448          m_file(NULL)
1449  {  {
1450      set_title(_("File Properties"));      set_title(_("File Properties"));
1451      eName.set_width_chars(50);      eName.set_width_chars(50);
# Line 1047  PropDialog::PropDialog() Line 1467  PropDialog::PropDialog()
1467      connect(eCommissioned, &DLS::Info::Commissioned);      connect(eCommissioned, &DLS::Info::Commissioned);
1468      connect(eSubject, &DLS::Info::Subject);      connect(eSubject, &DLS::Info::Subject);
1469    
1470        table.add(eFileFormat);
1471      table.add(eName);      table.add(eName);
1472      table.add(eCreationDate);      table.add(eCreationDate);
1473      table.add(eComments);      table.add(eComments);
# Line 1077  PropDialog::PropDialog() Line 1498  PropDialog::PropDialog()
1498      quitButton.grab_focus();      quitButton.grab_focus();
1499      quitButton.signal_clicked().connect(      quitButton.signal_clicked().connect(
1500          sigc::mem_fun(*this, &PropDialog::hide));          sigc::mem_fun(*this, &PropDialog::hide));
1501        eFileFormat.signal_value_changed().connect(
1502            sigc::mem_fun(*this, &PropDialog::onFileFormatChanged));
1503    
1504      quitButton.show();      quitButton.show();
1505      vbox.show();      vbox.show();
1506      show_all_children();      show_all_children();
1507  }  }
1508    
1509    void PropDialog::set_file(gig::File* file)
1510    {
1511        m_file = file;
1512    
1513        // update file format version combo box
1514        const std::string sGiga = "Gigasampler/GigaStudio v";
1515        const int major = file->pVersion->major;
1516        std::vector<std::string> txts;
1517        std::vector<int> values;
1518        txts.push_back(sGiga + "2"); values.push_back(2);
1519        txts.push_back(sGiga + "3/v4"); values.push_back(3);
1520        if (major != 2 && major != 3) {
1521            txts.push_back(sGiga + ToString(major)); values.push_back(major);
1522        }
1523        std::vector<const char*> texts;
1524        for (int i = 0; i < txts.size(); ++i) texts.push_back(txts[i].c_str());
1525        texts.push_back(NULL); values.push_back(0);
1526        eFileFormat.set_choices(&texts[0], &values[0]);
1527        eFileFormat.set_value(major);
1528    }
1529    
1530    void PropDialog::onFileFormatChanged() {
1531        const int major = eFileFormat.get_value();
1532        if (m_file) m_file->pVersion->major = major;
1533    }
1534    
1535  void PropDialog::set_info(DLS::Info* info)  void PropDialog::set_info(DLS::Info* info)
1536  {  {
1537      update(info);      update(info);
# Line 1214  void MainWindow::file_changed() Line 1663  void MainWindow::file_changed()
1663      }      }
1664  }  }
1665    
1666    void MainWindow::updateSampleRefCountMap(gig::File* gig) {
1667        sample_ref_count.clear();
1668        
1669        if (!gig) return;
1670    
1671        for (gig::Instrument* instrument = gig->GetFirstInstrument(); instrument;
1672             instrument = gig->GetNextInstrument())
1673        {
1674            for (gig::Region* rgn = instrument->GetFirstRegion(); rgn;
1675                 rgn = instrument->GetNextRegion())
1676            {
1677                for (int i = 0; i < 256; ++i) {
1678                    if (!rgn->pDimensionRegions[i]) continue;
1679                    if (rgn->pDimensionRegions[i]->pSample) {
1680                        sample_ref_count[rgn->pDimensionRegions[i]->pSample]++;
1681                    }
1682                }
1683            }
1684        }
1685    }
1686    
1687  void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument)  void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument)
1688  {  {
1689      file = 0;      file = 0;
# Line 1224  void MainWindow::load_gig(gig::File* gig Line 1694  void MainWindow::load_gig(gig::File* gig
1694      file_has_name = filename;      file_has_name = filename;
1695      file_is_changed = false;      file_is_changed = false;
1696    
1697        propDialog.set_file(gig);
1698      propDialog.set_info(gig->pInfo);      propDialog.set_info(gig->pInfo);
1699    
1700      instrument_name_connection.block();      instrument_name_connection.block();
# Line 1239  void MainWindow::load_gig(gig::File* gig Line 1710  void MainWindow::load_gig(gig::File* gig
1710          add_instrument_to_menu(name);          add_instrument_to_menu(name);
1711      }      }
1712      instrument_name_connection.unblock();      instrument_name_connection.unblock();
1713      uiManager->get_widget("/MenuBar/MenuInstrument")->show();      uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments")->show();
1714    
1715        updateSampleRefCountMap(gig);
1716    
1717      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1718          if (group->Name != "") {          if (group->Name != "") {
# Line 1257  void MainWindow::load_gig(gig::File* gig Line 1730  void MainWindow::load_gig(gig::File* gig
1730                      gig_to_utf8(sample->pInfo->Name);                      gig_to_utf8(sample->pInfo->Name);
1731                  rowSample[m_SamplesModel.m_col_sample] = sample;                  rowSample[m_SamplesModel.m_col_sample] = sample;
1732                  rowSample[m_SamplesModel.m_col_group]  = NULL;                  rowSample[m_SamplesModel.m_col_group]  = NULL;
1733                    int refcount = sample_ref_count.count(sample) ? sample_ref_count[sample] : 0;
1734                    rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
1735                    rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
1736              }              }
1737          }          }
1738      }      }
1739        
1740        for (int i = 0; gig->GetScriptGroup(i); ++i) {
1741            gig::ScriptGroup* group = gig->GetScriptGroup(i);
1742    
1743            Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
1744            Gtk::TreeModel::Row rowGroup = *iterGroup;
1745            rowGroup[m_ScriptsModel.m_col_name]   = gig_to_utf8(group->Name);
1746            rowGroup[m_ScriptsModel.m_col_group]  = group;
1747            rowGroup[m_ScriptsModel.m_col_script] = NULL;
1748            for (int s = 0; group->GetScript(s); ++s) {
1749                gig::Script* script = group->GetScript(s);
1750    
1751                Gtk::TreeModel::iterator iterScript =
1752                    m_refScriptsTreeModel->append(rowGroup.children());
1753                Gtk::TreeModel::Row rowScript = *iterScript;
1754                rowScript[m_ScriptsModel.m_col_name] = gig_to_utf8(script->Name);
1755                rowScript[m_ScriptsModel.m_col_script] = script;
1756                rowScript[m_ScriptsModel.m_col_group]  = NULL;
1757            }
1758        }
1759        // unfold all sample groups & script groups by default
1760        m_TreeViewSamples.expand_all();
1761        m_TreeViewScripts.expand_all();
1762    
1763      file = gig;      file = gig;
1764    
# Line 1328  void MainWindow::show_midi_rules() Line 1827  void MainWindow::show_midi_rules()
1827      }      }
1828  }  }
1829    
1830    void MainWindow::show_script_slots() {
1831        if (!file) return;
1832        // get selected instrument
1833        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1834        Gtk::TreeModel::iterator it = sel->get_selected();
1835        if (!it) return;
1836        Gtk::TreeModel::Row row = *it;
1837        gig::Instrument* instrument = row[m_Columns.m_col_instr];
1838        if (!instrument) return;
1839    
1840        ScriptSlots* window = new ScriptSlots;
1841        window->setInstrument(instrument);
1842        //window->reparent(*this);
1843        window->show();
1844    }
1845    
1846  void MainWindow::on_action_view_status_bar() {  void MainWindow::on_action_view_status_bar() {
1847      Gtk::CheckMenuItem* item =      Gtk::CheckMenuItem* item =
1848          dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/Statusbar"));          dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/Statusbar"));
# Line 1375  void MainWindow::on_button_release(GdkEv Line 1890  void MainWindow::on_button_release(GdkEv
1890          show_instr_props();          show_instr_props();
1891      } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {      } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1892          // gig v2 files have no midi rules          // gig v2 files have no midi rules
1893            const bool bEnabled = !(file->pVersion && file->pVersion->major == 2);
1894            static_cast<Gtk::MenuItem*>(
1895                uiManager->get_widget("/MenuBar/MenuInstrument/MidiRules"))->set_sensitive(
1896                    bEnabled
1897                );
1898          static_cast<Gtk::MenuItem*>(          static_cast<Gtk::MenuItem*>(
1899              uiManager->get_widget("/PopupMenu/MidiRules"))->set_sensitive(              uiManager->get_widget("/PopupMenu/MidiRules"))->set_sensitive(
1900                  !(file->pVersion && file->pVersion->major == 2));                  bEnabled
1901                );
1902          popup_menu->popup(button->button, button->time);          popup_menu->popup(button->button, button->time);
1903      }      }
1904  }  }
# Line 1397  void MainWindow::on_instrument_selection Line 1918  void MainWindow::on_instrument_selection
1918      }      }
1919  }  }
1920    
1921    void MainWindow::select_instrument(gig::Instrument* instrument) {
1922        if (!instrument) return;
1923    
1924        Glib::RefPtr<Gtk::TreeModel> model = m_TreeView.get_model();
1925        for (int i = 0; i < model->children().size(); ++i) {
1926            Gtk::TreeModel::Row row = model->children()[i];
1927            if (row[m_Columns.m_col_instr] == instrument) {
1928                // select and show the respective instrument in the list view
1929                show_intruments_tab();
1930                m_TreeView.get_selection()->select(model->children()[i]);
1931                Gtk::TreePath path(
1932                    m_TreeView.get_selection()->get_selected()
1933                );
1934                m_TreeView.scroll_to_row(path);
1935                on_sel_change(); // the regular instrument selection change callback
1936            }
1937        }
1938    }
1939    
1940    /// Returns true if requested dimension region was successfully selected and scrolled to in the list view, false on error.
1941    bool MainWindow::select_dimension_region(gig::DimensionRegion* dimRgn) {
1942        gig::Region* pRegion = (gig::Region*) dimRgn->GetParent();
1943        gig::Instrument* pInstrument = (gig::Instrument*) pRegion->GetParent();
1944    
1945        Glib::RefPtr<Gtk::TreeModel> model = m_TreeView.get_model();
1946        for (int i = 0; i < model->children().size(); ++i) {
1947            Gtk::TreeModel::Row row = model->children()[i];
1948            if (row[m_Columns.m_col_instr] == pInstrument) {
1949                // select and show the respective instrument in the list view
1950                show_intruments_tab();
1951                m_TreeView.get_selection()->select(model->children()[i]);
1952                Gtk::TreePath path(
1953                    m_TreeView.get_selection()->get_selected()
1954                );
1955                m_TreeView.scroll_to_row(path);
1956                on_sel_change(); // the regular instrument selection change callback
1957    
1958                // select respective region in the region selector
1959                m_RegionChooser.set_region(pRegion);
1960    
1961                // select and show the respective dimension region in the editor
1962                //update_dimregs();
1963                if (!m_DimRegionChooser.select_dimregion(dimRgn)) return false;
1964                //dimreg_edit.set_dim_region(dimRgn);
1965    
1966                return true;
1967            }
1968        }
1969    
1970        return false;
1971    }
1972    
1973    void MainWindow::select_sample(gig::Sample* sample) {
1974        Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
1975        for (int g = 0; g < model->children().size(); ++g) {
1976            Gtk::TreeModel::Row rowGroup = model->children()[g];
1977            for (int s = 0; s < rowGroup.children().size(); ++s) {
1978                Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
1979                if (rowSample[m_SamplesModel.m_col_sample] == sample) {
1980                    show_samples_tab();
1981                    m_TreeViewSamples.get_selection()->select(rowGroup.children()[s]);
1982                    Gtk::TreePath path(
1983                        m_TreeViewSamples.get_selection()->get_selected()
1984                    );
1985                    m_TreeViewSamples.scroll_to_row(path);
1986                    return;
1987                }
1988            }
1989        }
1990    }
1991    
1992  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
1993      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1994          Gtk::Menu* sample_popup =          Gtk::Menu* sample_popup =
# Line 1411  void MainWindow::on_sample_treeview_butt Line 2003  void MainWindow::on_sample_treeview_butt
2003              group_selected  = row[m_SamplesModel.m_col_group];              group_selected  = row[m_SamplesModel.m_col_group];
2004              sample_selected = row[m_SamplesModel.m_col_sample];              sample_selected = row[m_SamplesModel.m_col_sample];
2005          }          }
2006            
2007                
2008          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
2009              set_sensitive(group_selected || sample_selected);              set_sensitive(group_selected || sample_selected);
2010          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
2011              set_sensitive(group_selected || sample_selected);              set_sensitive(group_selected || sample_selected);
2012          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
2013              set_sensitive(file);              set_sensitive(file);
2014            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/ShowSampleRefs"))->
2015                set_sensitive(sample_selected);
2016          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
2017              set_sensitive(group_selected || sample_selected);              set_sensitive(group_selected || sample_selected);
2018          // show sample popup          // show sample popup
2019          sample_popup->popup(button->button, button->time);          sample_popup->popup(button->button, button->time);
2020    
2021            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/SampleProperties"))->
2022                set_sensitive(group_selected || sample_selected);
2023            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddSample"))->
2024                set_sensitive(group_selected || sample_selected);
2025            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddGroup"))->
2026                set_sensitive(file);
2027            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/ShowSampleRefs"))->
2028                set_sensitive(sample_selected);
2029            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/RemoveSample"))->
2030                set_sensitive(group_selected || sample_selected);
2031      }      }
2032  }  }
2033    
2034    void MainWindow::on_script_treeview_button_release(GdkEventButton* button) {
2035        if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
2036            Gtk::Menu* script_popup =
2037                dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/ScriptPopupMenu"));
2038            // update enabled/disabled state of sample popup items
2039            Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2040            Gtk::TreeModel::iterator it = sel->get_selected();
2041            bool group_selected  = false;
2042            bool script_selected = false;
2043            if (it) {
2044                Gtk::TreeModel::Row row = *it;
2045                group_selected  = row[m_ScriptsModel.m_col_group];
2046                script_selected = row[m_ScriptsModel.m_col_script];
2047            }
2048            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScript"))->
2049                set_sensitive(group_selected || script_selected);
2050            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScriptGroup"))->
2051                set_sensitive(file);
2052            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/EditScript"))->
2053                set_sensitive(script_selected);    
2054            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/RemoveScript"))->
2055                set_sensitive(group_selected || script_selected);
2056            // show sample popup
2057            script_popup->popup(button->button, button->time);
2058    
2059            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScript"))->
2060                set_sensitive(group_selected || script_selected);
2061            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScriptGroup"))->
2062                set_sensitive(file);
2063            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/EditScript"))->
2064                set_sensitive(script_selected);    
2065            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/RemoveScript"))->
2066                set_sensitive(group_selected || script_selected);
2067        }
2068    }
2069    
2070  Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu(  Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu(
2071      const Glib::ustring& name, int position) {      const Glib::ustring& name, int position) {
# Line 1571  void MainWindow::on_action_sample_proper Line 2213  void MainWindow::on_action_sample_proper
2213      msg.run();      msg.run();
2214  }  }
2215    
2216    void MainWindow::on_action_add_script_group() {
2217        static int __script_indexer = 0;
2218        if (!file) return;
2219        gig::ScriptGroup* group = file->AddScriptGroup();
2220        group->Name = gig_from_utf8(_("Unnamed Group"));
2221        if (__script_indexer) group->Name += " " + ToString(__script_indexer);
2222        __script_indexer++;
2223        // update sample tree view
2224        Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
2225        Gtk::TreeModel::Row rowGroup = *iterGroup;
2226        rowGroup[m_ScriptsModel.m_col_name] = gig_to_utf8(group->Name);
2227        rowGroup[m_ScriptsModel.m_col_script] = NULL;
2228        rowGroup[m_ScriptsModel.m_col_group] = group;
2229        file_changed();
2230    }
2231    
2232    void MainWindow::on_action_add_script() {
2233        if (!file) return;
2234        // get selected group
2235        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2236        Gtk::TreeModel::iterator it = sel->get_selected();
2237        if (!it) return;
2238        Gtk::TreeModel::Row row = *it;
2239        gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2240        if (!group) { // not a group, but a script is selected (probably)
2241            gig::Script* script = row[m_ScriptsModel.m_col_script];
2242            if (!script) return;
2243            it = row.parent(); // resolve parent (that is the script's group)
2244            if (!it) return;
2245            row = *it;
2246            group = row[m_ScriptsModel.m_col_group];
2247            if (!group) return;
2248        }
2249    
2250        // add a new script to the .gig file
2251        gig::Script* script = group->AddScript();    
2252        Glib::ustring name = _("Unnamed Script");
2253        script->Name = gig_from_utf8(name);
2254    
2255        // add script to the tree view
2256        Gtk::TreeModel::iterator iterScript =
2257            m_refScriptsTreeModel->append(row.children());
2258        Gtk::TreeModel::Row rowScript = *iterScript;
2259        rowScript[m_ScriptsModel.m_col_name] = name;
2260        rowScript[m_ScriptsModel.m_col_script] = script;
2261        rowScript[m_ScriptsModel.m_col_group]  = NULL;
2262    
2263        // unfold group of new script item in treeview
2264        Gtk::TreeModel::Path path(iterScript);
2265        m_TreeViewScripts.expand_to_path(path);
2266    }
2267    
2268    void MainWindow::on_action_edit_script() {
2269        if (!file) return;
2270        // get selected script
2271        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2272        Gtk::TreeModel::iterator it = sel->get_selected();
2273        if (!it) return;
2274        Gtk::TreeModel::Row row = *it;
2275        gig::Script* script = row[m_ScriptsModel.m_col_script];
2276        if (!script) return;
2277    
2278        ScriptEditor* editor = new ScriptEditor;
2279        editor->setScript(script);
2280        //editor->reparent(*this);
2281        editor->show();
2282    }
2283    
2284    void MainWindow::on_action_remove_script() {
2285        if (!file) return;
2286        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2287        Gtk::TreeModel::iterator it = sel->get_selected();
2288        if (it) {
2289            Gtk::TreeModel::Row row = *it;
2290            gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2291            gig::Script* script     = row[m_ScriptsModel.m_col_script];
2292            Glib::ustring name      = row[m_ScriptsModel.m_col_name];
2293            try {
2294                // remove script group or script from the gig file
2295                if (group) {
2296                    // notify everybody that we're going to remove these samples
2297    //TODO:         scripts_to_be_removed_signal.emit(members);
2298                    // delete the group in the .gig file including the
2299                    // samples that belong to the group
2300                    file->DeleteScriptGroup(group);
2301                    // notify that we're done with removal
2302    //TODO:         scripts_removed_signal.emit();
2303                    file_changed();
2304                } else if (script) {
2305                    // notify everybody that we're going to remove this sample
2306    //TODO:         std::list<gig::Script*> lscripts;
2307    //TODO:         lscripts.push_back(script);
2308    //TODO:         scripts_to_be_removed_signal.emit(lscripts);
2309                    // remove sample from the .gig file
2310                    script->GetGroup()->DeleteScript(script);
2311                    // notify that we're done with removal
2312    //TODO:         scripts_removed_signal.emit();
2313                    dimreg_changed();
2314                    file_changed();
2315                }
2316                // remove respective row(s) from samples tree view
2317                m_refScriptsTreeModel->erase(it);
2318            } catch (RIFF::Exception e) {
2319                // pretend we're done with removal (i.e. to avoid dead locks)
2320    //TODO:     scripts_removed_signal.emit();
2321                // show error message
2322                Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2323                msg.run();
2324            }
2325        }
2326    }
2327    
2328  void MainWindow::on_action_add_group() {  void MainWindow::on_action_add_group() {
2329      static int __sample_indexer = 0;      static int __sample_indexer = 0;
2330      if (!file) return;      if (!file) return;
# Line 1587  void MainWindow::on_action_add_group() { Line 2341  void MainWindow::on_action_add_group() {
2341      file_changed();      file_changed();
2342  }  }
2343    
2344    void MainWindow::on_action_replace_sample() {
2345        add_or_replace_sample(true);
2346    }
2347    
2348  void MainWindow::on_action_add_sample() {  void MainWindow::on_action_add_sample() {
2349        add_or_replace_sample(false);
2350    }
2351    
2352    void MainWindow::add_or_replace_sample(bool replace) {
2353      if (!file) return;      if (!file) return;
2354      // get selected group  
2355        // get selected group (and probably selected sample)
2356      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2357      Gtk::TreeModel::iterator it = sel->get_selected();      Gtk::TreeModel::iterator it = sel->get_selected();
2358      if (!it) return;      if (!it) return;
2359      Gtk::TreeModel::Row row = *it;      Gtk::TreeModel::Row row = *it;
2360        gig::Sample* sample = NULL;
2361      gig::Group* group = row[m_SamplesModel.m_col_group];      gig::Group* group = row[m_SamplesModel.m_col_group];
2362      if (!group) { // not a group, but a sample is selected (probably)      if (!group) { // not a group, but a sample is selected (probably)
2363          gig::Sample* sample = row[m_SamplesModel.m_col_sample];          if (replace) sample = row[m_SamplesModel.m_col_sample];
2364          if (!sample) return;          if (!row[m_SamplesModel.m_col_sample]) return;
2365          it = row.parent(); // resolve parent (that is the sample's group)          it = row.parent(); // resolve parent (that is the sample's group)
2366          if (!it) return;          if (!it) return;
2367          row = *it;          if (!replace) row = *it;
2368          group = row[m_SamplesModel.m_col_group];          group = (*it)[m_SamplesModel.m_col_group];
2369          if (!group) return;          if (!group) return;
2370      }      }
2371        if (replace && !sample) return;
2372    
2373      // show 'browse for file' dialog      // show 'browse for file' dialog
2374      Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));      Gtk::FileChooserDialog dialog(*this, replace ? _("Replace Sample with") : _("Add Sample(s)"));
2375      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2376      dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);      dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
2377      dialog.set_select_multiple(true);      dialog.set_select_multiple(!replace); // allow multi audio file selection only when adding new samples, does not make sense when replacing a specific sample
2378    
2379      // matches all file types supported by libsndfile      // matches all file types supported by libsndfile
2380  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
# Line 1679  void MainWindow::on_action_add_sample() Line 2445  void MainWindow::on_action_add_sample()
2445                          sf_close(hFile); // close sound file                          sf_close(hFile); // close sound file
2446                          throw std::string(_("format not supported")); // unsupported subformat (yet?)                          throw std::string(_("format not supported")); // unsupported subformat (yet?)
2447                  }                  }
2448                  // add a new sample to the .gig file                  // add a new sample to the .gig file (if adding is requested actually)
2449                  gig::Sample* sample = file->AddSample();                  if (!replace) sample = file->AddSample();
2450                  // file name without path                  // file name without path
2451                  Glib::ustring filename = Glib::filename_display_basename(*iter);                  Glib::ustring filename = Glib::filename_display_basename(*iter);
2452                  // remove file extension if there is one                  // remove file extension if there is one
# Line 1731  void MainWindow::on_action_add_sample() Line 2497  void MainWindow::on_action_add_sample()
2497                  // physically when File::Save() is called)                  // physically when File::Save() is called)
2498                  sample->Resize(info.frames);                  sample->Resize(info.frames);
2499                  // make sure sample is part of the selected group                  // make sure sample is part of the selected group
2500                  group->AddSample(sample);                  if (!replace) group->AddSample(sample);
2501                  // schedule that physical resize and sample import                  // schedule that physical resize and sample import
2502                  // (data copying), performed when "Save" is requested                  // (data copying), performed when "Save" is requested
2503                  SampleImportItem sched_item;                  SampleImportItem sched_item;
# Line 1739  void MainWindow::on_action_add_sample() Line 2505  void MainWindow::on_action_add_sample()
2505                  sched_item.sample_path = *iter;                  sched_item.sample_path = *iter;
2506                  m_SampleImportQueue.push_back(sched_item);                  m_SampleImportQueue.push_back(sched_item);
2507                  // add sample to the tree view                  // add sample to the tree view
2508                  Gtk::TreeModel::iterator iterSample =                  if (replace) {
2509                      m_refSamplesTreeModel->append(row.children());                      row[m_SamplesModel.m_col_name] = gig_to_utf8(sample->pInfo->Name);
2510                  Gtk::TreeModel::Row rowSample = *iterSample;                  } else {
2511                  rowSample[m_SamplesModel.m_col_name] =                      Gtk::TreeModel::iterator iterSample =
2512                      gig_to_utf8(sample->pInfo->Name);                          m_refSamplesTreeModel->append(row.children());
2513                  rowSample[m_SamplesModel.m_col_sample] = sample;                      Gtk::TreeModel::Row rowSample = *iterSample;
2514                  rowSample[m_SamplesModel.m_col_group]  = NULL;                      rowSample[m_SamplesModel.m_col_name] =
2515                            gig_to_utf8(sample->pInfo->Name);
2516                        rowSample[m_SamplesModel.m_col_sample] = sample;
2517                        rowSample[m_SamplesModel.m_col_group]  = NULL;
2518                    }
2519                  // close sound file                  // close sound file
2520                  sf_close(hFile);                  sf_close(hFile);
2521                  file_changed();                  file_changed();
# Line 1756  void MainWindow::on_action_add_sample() Line 2526  void MainWindow::on_action_add_sample()
2526          }          }
2527          // 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
2528          if (!error_files.empty()) {          if (!error_files.empty()) {
2529              Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files;              Glib::ustring txt =
2530                    (replace
2531                        ? _("Failed to replace sample with:\n")
2532                        : _("Could not add the following sample(s):\n"))
2533                    + error_files;
2534              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2535              msg.run();              msg.run();
2536          }          }
# Line 1940  void MainWindow::on_action_remove_sample Line 2714  void MainWindow::on_action_remove_sample
2714      }      }
2715  }  }
2716    
2717    // see comment on on_sample_treeview_drag_begin()
2718    void MainWindow::on_scripts_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2719    {
2720        first_call_to_drag_data_get = true;
2721    }
2722    
2723    void MainWindow::on_scripts_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2724                                                       Gtk::SelectionData& selection_data, guint, guint)
2725    {
2726        if (!first_call_to_drag_data_get) return;
2727        first_call_to_drag_data_get = false;
2728    
2729        // get selected script
2730        gig::Script* script = NULL;
2731        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2732        Gtk::TreeModel::iterator it = sel->get_selected();
2733        if (it) {
2734            Gtk::TreeModel::Row row = *it;
2735            script = row[m_ScriptsModel.m_col_script];
2736        }
2737        // pass the gig::Script as pointer
2738        selection_data.set(selection_data.get_target(), 0/*unused*/,
2739                           (const guchar*)&script,
2740                           sizeof(script)/*length of data in bytes*/);
2741    }
2742    
2743    // see comment on on_sample_treeview_drag_begin()
2744    void MainWindow::on_instruments_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2745    {
2746        first_call_to_drag_data_get = true;
2747    }
2748    
2749    void MainWindow::on_instruments_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2750                                                           Gtk::SelectionData& selection_data, guint, guint)
2751    {
2752        if (!first_call_to_drag_data_get) return;
2753        first_call_to_drag_data_get = false;
2754    
2755        // get selected source instrument
2756        gig::Instrument* src = NULL;
2757        {
2758            Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
2759            Gtk::TreeModel::iterator it = sel->get_selected();
2760            if (it) {
2761                Gtk::TreeModel::Row row = *it;
2762                src = row[m_Columns.m_col_instr];
2763            }
2764        }
2765        if (!src) return;
2766    
2767        // pass the source gig::Instrument as pointer
2768        selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&src,
2769                           sizeof(src)/*length of data in bytes*/);
2770    }
2771    
2772    void MainWindow::on_instruments_treeview_drop_drag_data_received(
2773        const Glib::RefPtr<Gdk::DragContext>& context, int x, int y,
2774        const Gtk::SelectionData& selection_data, guint, guint time)
2775    {
2776        gig::Instrument* src = *((gig::Instrument**) selection_data.get_data());
2777        if (!src || selection_data.get_length() != sizeof(gig::Instrument*))
2778            return;
2779    
2780        gig::Instrument* dst = NULL;
2781        {
2782            Gtk::TreeModel::Path path;
2783            const bool found = m_TreeView.get_path_at_pos(x, y, path);
2784            if (!found) return;
2785    
2786            Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path);
2787            if (!iter) return;
2788            Gtk::TreeModel::Row row = *iter;
2789            dst = row[m_Columns.m_col_instr];
2790        }
2791        if (!dst) return;
2792    
2793        //printf("dragdrop received src=%s dst=%s\n", src->pInfo->Name.c_str(), dst->pInfo->Name.c_str());
2794        src->MoveTo(dst);
2795        __refreshEntireGUI();
2796        select_instrument(src);
2797    }
2798    
2799  // For some reason drag_data_get gets called two times for each  // For some reason drag_data_get gets called two times for each
2800  // drag'n'drop (at least when target is an Entry). This work-around  // drag'n'drop (at least when target is an Entry). This work-around
2801  // makes sure the code in drag_data_get and drop_drag_data_received is  // makes sure the code in drag_data_get and drop_drag_data_received is
# Line 1998  void MainWindow::on_sample_label_drop_dr Line 2854  void MainWindow::on_sample_label_drop_dr
2854          bool channels_changed = false;          bool channels_changed = false;
2855          if (sample->Channels == 1 && stereo_dimension) {          if (sample->Channels == 1 && stereo_dimension) {
2856              // remove the samplechannel dimension              // remove the samplechannel dimension
2857    /* commented out, because it makes it impossible building up an instrument from scratch using two separate L/R samples
2858              region->DeleteDimension(stereo_dimension);              region->DeleteDimension(stereo_dimension);
2859              channels_changed = true;              channels_changed = true;
2860              region_changed();              region_changed();
2861    */
2862          }          }
2863          dimreg_edit.set_sample(          dimreg_edit.set_sample(
2864              sample,              sample,
# Line 2066  void MainWindow::sample_name_changed(con Line 2924  void MainWindow::sample_name_changed(con
2924      }      }
2925  }  }
2926    
2927    void MainWindow::script_name_changed(const Gtk::TreeModel::Path& path,
2928                                         const Gtk::TreeModel::iterator& iter) {
2929        if (!iter) return;
2930        Gtk::TreeModel::Row row = *iter;
2931        Glib::ustring name      = row[m_ScriptsModel.m_col_name];
2932        gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2933        gig::Script* script     = row[m_ScriptsModel.m_col_script];
2934        gig::String gigname(gig_from_utf8(name));
2935        if (group) {
2936            if (group->Name != gigname) {
2937                group->Name = gigname;
2938                printf("script group name changed\n");
2939                file_changed();
2940            }
2941        } else if (script) {
2942            if (script->Name != gigname) {
2943                script->Name = gigname;
2944                printf("script name changed\n");
2945                file_changed();
2946            }
2947        }
2948    }
2949    
2950    void MainWindow::script_double_clicked(const Gtk::TreeModel::Path& path,
2951                                           Gtk::TreeViewColumn* column)
2952    {
2953        Gtk::TreeModel::iterator iter = m_refScriptsTreeModel->get_iter(path);
2954        if (!iter) return;
2955        Gtk::TreeModel::Row row = *iter;
2956        gig::Script* script = row[m_ScriptsModel.m_col_script];
2957        if (!script) return;
2958    
2959        ScriptEditor* editor = new ScriptEditor;
2960        editor->setScript(script);
2961        //editor->reparent(*this);
2962        editor->show();
2963    }
2964    
2965  void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,  void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
2966                                           const Gtk::TreeModel::iterator& iter) {                                           const Gtk::TreeModel::iterator& iter) {
2967      if (!iter) return;      if (!iter) return;
# Line 2100  void MainWindow::instrument_name_changed Line 2996  void MainWindow::instrument_name_changed
2996      }      }
2997  }  }
2998    
2999    void MainWindow::on_action_combine_instruments() {
3000        CombineInstrumentsDialog* d = new CombineInstrumentsDialog(*this, file);
3001        d->show_all();
3002        d->resize(500, 400);
3003        d->run();
3004        if (d->fileWasChanged()) {
3005            // update GUI with new instrument just created
3006            add_instrument(d->newCombinedInstrument());
3007        }
3008        delete d;
3009    }
3010    
3011    void MainWindow::on_action_view_references() {
3012        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
3013        Gtk::TreeModel::iterator it = sel->get_selected();
3014        if (!it) return;
3015        Gtk::TreeModel::Row row = *it;
3016        gig::Sample* sample = row[m_SamplesModel.m_col_sample];
3017        if (!sample) return;
3018    
3019        ReferencesView* d = new ReferencesView(*this);
3020        d->setSample(sample);
3021        d->dimension_region_selected.connect(
3022            sigc::mem_fun(*this, &MainWindow::select_dimension_region)
3023        );
3024        d->show_all();
3025        d->resize(500, 400);
3026        d->run();
3027        delete d;
3028    }
3029    
3030    void MainWindow::mergeFiles(const std::vector<std::string>& filenames) {
3031        struct _Source {
3032            std::vector<RIFF::File*> riffs;
3033            std::vector<gig::File*> gigs;
3034            
3035            ~_Source() {
3036                for (int k = 0; k < gigs.size(); ++k) delete gigs[k];
3037                for (int k = 0; k < riffs.size(); ++k) delete riffs[k];
3038                riffs.clear();
3039                gigs.clear();
3040            }
3041        } sources;
3042    
3043        if (filenames.empty())
3044            throw RIFF::Exception(_("No files selected, so nothing done."));
3045    
3046        // first open all input files (to avoid output file corruption)
3047        int i;
3048        try {
3049            for (i = 0; i < filenames.size(); ++i) {
3050                const std::string& filename = filenames[i];
3051                printf("opening file=%s\n", filename.c_str());
3052    
3053                RIFF::File* riff = new RIFF::File(filename);
3054                sources.riffs.push_back(riff);
3055    
3056                gig::File* gig = new gig::File(riff);
3057                sources.gigs.push_back(gig);
3058            }
3059        } catch (RIFF::Exception e) {
3060            throw RIFF::Exception(
3061                _("Error occurred while opening '") +
3062                filenames[i] +
3063                "': " +
3064                e.Message
3065            );
3066        } catch (...) {
3067            throw RIFF::Exception(
3068                _("Unknown exception occurred while opening '") +
3069                filenames[i] + "'"
3070            );
3071        }
3072    
3073        // now merge the opened .gig files to the main .gig file currently being
3074        // open in gigedit
3075        try {
3076            for (i = 0; i < filenames.size(); ++i) {
3077                const std::string& filename = filenames[i];
3078                printf("merging file=%s\n", filename.c_str());
3079                assert(i < sources.gigs.size());
3080    
3081                this->file->AddContentOf(sources.gigs[i]);
3082            }
3083        } catch (RIFF::Exception e) {
3084            throw RIFF::Exception(
3085                _("Error occurred while merging '") +
3086                filenames[i] +
3087                "': " +
3088                e.Message
3089            );
3090        } catch (...) {
3091            throw RIFF::Exception(
3092                _("Unknown exception occurred while merging '") +
3093                filenames[i] + "'"
3094            );
3095        }
3096    
3097        // Finally save gig file persistently to disk ...
3098        //NOTE: requires that this gig file already has a filename !
3099        {
3100            std::cout << "Saving file\n" << std::flush;
3101            file_structure_to_be_changed_signal.emit(this->file);
3102    
3103            progress_dialog = new ProgressDialog( //FIXME: memory leak!
3104                _("Saving") +  Glib::ustring(" '") +
3105                Glib::filename_display_basename(this->filename) + "' ...",
3106                *this
3107            );
3108            progress_dialog->show_all();
3109            saver = new Saver(this->file); //FIXME: memory leak!
3110            saver->signal_progress().connect(
3111                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
3112            saver->signal_finished().connect(
3113                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
3114            saver->signal_error().connect(
3115                sigc::mem_fun(*this, &MainWindow::on_saver_error));
3116            saver->launch();
3117        }
3118    }
3119    
3120    void MainWindow::on_action_merge_files() {
3121        if (this->file->GetFileName().empty()) {
3122            Glib::ustring txt = _(
3123                "You seem to have a new .gig file open that has not been saved "
3124                "yet. You must save it somewhere before starting to merge it with "
3125                "other .gig files though, because during the merge operation the "
3126                "other files' sample data must be written on file level to the "
3127                "target .gig file."
3128            );
3129            Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
3130            msg.run();
3131            return;
3132        }
3133    
3134        Gtk::FileChooserDialog dialog(*this, _("Merge .gig files"));
3135        dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
3136        dialog.add_button(_("Merge"), Gtk::RESPONSE_OK);
3137        dialog.set_default_response(Gtk::RESPONSE_CANCEL);
3138    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
3139        Gtk::FileFilter filter;
3140        filter.add_pattern("*.gig");
3141    #else
3142        Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
3143        filter->add_pattern("*.gig");
3144    #endif
3145        dialog.set_filter(filter);
3146        if (current_gig_dir != "") {
3147            dialog.set_current_folder(current_gig_dir);
3148        }
3149        dialog.set_select_multiple(true);
3150    
3151        // show warning in the file picker dialog
3152        Gtk::HBox descriptionArea;
3153        descriptionArea.set_spacing(15);
3154        Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
3155        descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
3156    #if GTKMM_MAJOR_VERSION < 3
3157        view::WrapLabel description;
3158    #else
3159        Gtk::Label description;
3160        description.set_line_wrap();
3161    #endif
3162        description.set_markup(_(
3163            "\nSelect at least one .gig file that shall be merged to the .gig file "
3164            "currently being open in gigedit.\n\n"
3165            "<b>Please Note:</b> Merging with other files will modify your "
3166            "currently open .gig file on file level! And be aware that the current "
3167            "merge algorithm does not detect duplicate samples yet. So if you are "
3168            "merging files which are using equivalent sample data, those "
3169            "equivalent samples will currently be treated as separate samples and "
3170            "will accordingly be stored separately in the target .gig file!"
3171        ));
3172        descriptionArea.pack_start(description);
3173        dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
3174        descriptionArea.show_all();
3175    
3176        if (dialog.run() == Gtk::RESPONSE_OK) {
3177            printf("on_action_merge_files self=%x\n", Glib::Threads::Thread::self());
3178            std::vector<std::string> filenames = dialog.get_filenames();
3179    
3180            // merge the selected files to the currently open .gig file
3181            try {
3182                mergeFiles(filenames);
3183            } catch (RIFF::Exception e) {
3184                Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
3185                msg.run();
3186            }
3187    
3188            // update GUI
3189            __refreshEntireGUI();        
3190        }
3191    }
3192    
3193  void MainWindow::set_file_is_shared(bool b) {  void MainWindow::set_file_is_shared(bool b) {
3194      this->file_is_shared = b;      this->file_is_shared = b;
3195    
# Line 2114  void MainWindow::set_file_is_shared(bool Line 3204  void MainWindow::set_file_is_shared(bool
3204              Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)              Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)
3205          );          );
3206      }      }
3207    
3208        {
3209            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
3210                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
3211            if (item) item->set_sensitive(b);
3212        }
3213    }
3214    
3215    void MainWindow::on_sample_ref_count_incremented(gig::Sample* sample, int offset) {
3216        if (!sample) return;
3217        sample_ref_count[sample] += offset;
3218        const int refcount = sample_ref_count[sample];
3219    
3220        Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
3221        for (int g = 0; g < model->children().size(); ++g) {
3222            Gtk::TreeModel::Row rowGroup = model->children()[g];
3223            for (int s = 0; s < rowGroup.children().size(); ++s) {
3224                Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
3225                if (rowSample[m_SamplesModel.m_col_sample] != sample) continue;
3226                rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
3227                rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
3228            }
3229        }
3230    }
3231    
3232    void MainWindow::on_sample_ref_changed(gig::Sample* oldSample, gig::Sample* newSample) {
3233        on_sample_ref_count_incremented(oldSample, -1);
3234        on_sample_ref_count_incremented(newSample, +1);
3235    }
3236    
3237    void MainWindow::on_samples_to_be_removed(std::list<gig::Sample*> samples) {
3238        // just in case a new sample is added later with exactly the same memory
3239        // address, which would lead to incorrect refcount if not deleted here
3240        for (std::list<gig::Sample*>::const_iterator it = samples.begin();
3241             it != samples.end(); ++it)
3242        {
3243            sample_ref_count.erase(*it);
3244        }
3245    }
3246    
3247    void MainWindow::show_samples_tab() {
3248        m_TreeViewNotebook.set_current_page(0);
3249    }
3250    
3251    void MainWindow::show_intruments_tab() {
3252        m_TreeViewNotebook.set_current_page(1);
3253    }
3254    
3255    void MainWindow::show_scripts_tab() {
3256        m_TreeViewNotebook.set_current_page(2);
3257  }  }
3258    
3259  sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_to_be_changed() {  sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_to_be_changed() {
# Line 2171  sigc::signal<void, int/*key*/, int/*velo Line 3311  sigc::signal<void, int/*key*/, int/*velo
3311  sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {  sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {
3312      return m_RegionChooser.signal_keyboard_key_released();      return m_RegionChooser.signal_keyboard_key_released();
3313  }  }
3314    
3315    sigc::signal<void, gig::Instrument*>& MainWindow::signal_switch_sampler_instrument() {
3316        return switch_sampler_instrument_signal;
3317    }

Legend:
Removed from v.2541  
changed lines
  Added in v.2715

  ViewVC Help
Powered by ViewVC