/[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 1262 by persson, Sun Jul 22 15:07:08 2007 UTC revision 2845 by persson, Sun Sep 20 10:18:22 2015 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (C) 2006, 2007 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 17  Line 17 
17   * 02110-1301 USA.   * 02110-1301 USA.
18   */   */
19    
 #include <libintl.h>  
20  #include <iostream>  #include <iostream>
21    #include <cstring>
22    
23    #include <glibmmconfig.h>
24    // threads.h must be included first to be able to build with
25    // G_DISABLE_DEPRECATED
26    #if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION == 31 && GLIBMM_MICRO_VERSION >= 2) || \
27        (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION > 31) || GLIBMM_MAJOR_VERSION > 2
28    #include <glibmm/threads.h>
29    #endif
30    
31    #include <glibmm/convert.h>
32    #include <glibmm/dispatcher.h>
33    #include <glibmm/miscutils.h>
34    #include <glibmm/stringutils.h>
35    #include <gtkmm/aboutdialog.h>
36  #include <gtkmm/filechooserdialog.h>  #include <gtkmm/filechooserdialog.h>
37  #include <gtkmm/messagedialog.h>  #include <gtkmm/messagedialog.h>
 #include <gtkmm/stock.h>  
38  #include <gtkmm/targetentry.h>  #include <gtkmm/targetentry.h>
39  #include <gtkmm/main.h>  #include <gtkmm/main.h>
40    #include <gtkmm/toggleaction.h>
41  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2  #if GTKMM_MAJOR_VERSION < 3
42  #define ABOUT_DIALOG  #include "wrapLabel.hh"
 #include <gtkmm/aboutdialog.h>  
43  #endif  #endif
44    
45    #include "global.h"
46    #include "compat.h"
47    
48  #include <stdio.h>  #include <stdio.h>
49  #include <sndfile.h>  #include <sndfile.h>
50    #include <assert.h>
51    
52  #include "mainwindow.h"  #include "mainwindow.h"
53    #include "Settings.h"
54    #include "CombineInstrumentsDialog.h"
55    #include "scripteditor.h"
56    #include "scriptslots.h"
57    #include "ReferencesView.h"
58    #include "../../gfx/status_attached.xpm"
59    #include "../../gfx/status_detached.xpm"
60    
 #define _(String) gettext(String)  
   
 template<class T> inline std::string ToString(T o) {  
     std::stringstream ss;  
     ss << o;  
     return ss.str();  
 }  
61    
62  MainWindow::MainWindow()  MainWindow::MainWindow() :
63        m_DimRegionChooser(*this),
64        dimreg_label(_("Changes apply to:")),
65        dimreg_all_regions(_("all regions")),
66        dimreg_all_dimregs(_("all dimension splits")),
67        dimreg_stereo(_("both channels"))
68  {  {
69  //    set_border_width(5);  //    set_border_width(5);
70  //    set_default_size(400, 200);  //    set_default_size(400, 200);
# Line 53  MainWindow::MainWindow() Line 73  MainWindow::MainWindow()
73      add(m_VBox);      add(m_VBox);
74    
75      // Handle selection      // Handle selection
76      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      m_TreeView.get_selection()->signal_changed().connect(
     tree_sel_ref->signal_changed().connect(  
77          sigc::mem_fun(*this, &MainWindow::on_sel_change));          sigc::mem_fun(*this, &MainWindow::on_sel_change));
78    
79      // m_TreeView.set_reorderable();      // m_TreeView.set_reorderable();
# Line 70  MainWindow::MainWindow() Line 89  MainWindow::MainWindow()
89      m_ScrolledWindowSamples.add(m_TreeViewSamples);      m_ScrolledWindowSamples.add(m_TreeViewSamples);
90      m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
91    
92        m_ScrolledWindowScripts.add(m_TreeViewScripts);
93        m_ScrolledWindowScripts.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
94    
95    
96      m_TreeViewNotebook.set_size_request(300);      m_TreeViewNotebook.set_size_request(300);
97    
98      m_HPaned.add1(m_TreeViewNotebook);      m_HPaned.add1(m_TreeViewNotebook);
99      m_HPaned.add2(dimreg_edit);      dimreg_hbox.add(dimreg_label);
100        dimreg_hbox.add(dimreg_all_regions);
101        dimreg_hbox.add(dimreg_all_dimregs);
102      m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");      dimreg_stereo.set_active();
103      m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");      dimreg_hbox.add(dimreg_stereo);
104        dimreg_vbox.add(dimreg_edit);
105        dimreg_vbox.pack_start(dimreg_hbox, Gtk::PACK_SHRINK);
106        m_HPaned.add2(dimreg_vbox);
107    
108        dimreg_label.set_tooltip_text(_("To automatically apply your changes above globally to the entire instrument, check all 3 check boxes on the right."));
109        dimreg_all_regions.set_tooltip_text(_("If checked: all changes you perform above will automatically be applied to all regions of this instrument as well."));
110        dimreg_all_dimregs.set_tooltip_text(_("If checked: all changes you perform above will automatically be applied as well to all dimension splits of the region selected below."));
111        dimreg_stereo.set_tooltip_text(_("If checked: all changes you perform above will automatically be applied to both audio channel splits (only if a \"stereo\" dimension is defined below)."));
112    
113        m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, _("Samples"));
114        m_TreeViewNotebook.append_page(m_ScrolledWindow, _("Instruments"));
115        m_TreeViewNotebook.append_page(m_ScrolledWindowScripts, _("Scripts"));
116    
117      actionGroup = Gtk::ActionGroup::create();      actionGroup = Gtk::ActionGroup::create();
118    
119      actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));      actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
120      actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW),      actionGroup->add(Gtk::Action::create("New", _("_New")),
121                         Gtk::AccelKey("<control>n"),
122                       sigc::mem_fun(                       sigc::mem_fun(
123                           *this, &MainWindow::on_action_file_new));                           *this, &MainWindow::on_action_file_new));
124      Glib::RefPtr<Gtk::Action> action =      actionGroup->add(Gtk::Action::create("Open", _("_Open...")),
125          Gtk::Action::create("Open", Gtk::Stock::OPEN);                       Gtk::AccelKey("<control>o"),
     action->property_label() = action->property_label() + "...";  
     actionGroup->add(action,  
126                       sigc::mem_fun(                       sigc::mem_fun(
127                           *this, &MainWindow::on_action_file_open));                           *this, &MainWindow::on_action_file_open));
128      actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE),      actionGroup->add(Gtk::Action::create("Save", _("_Save")),
129                         Gtk::AccelKey("<control>s"),
130                       sigc::mem_fun(                       sigc::mem_fun(
131                           *this, &MainWindow::on_action_file_save));                           *this, &MainWindow::on_action_file_save));
132      action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS);      actionGroup->add(Gtk::Action::create("SaveAs", _("Save _As...")),
     action->property_label() = action->property_label() + "...";  
     actionGroup->add(action,  
133                       Gtk::AccelKey("<shift><control>s"),                       Gtk::AccelKey("<shift><control>s"),
134                       sigc::mem_fun(                       sigc::mem_fun(
135                           *this, &MainWindow::on_action_file_save_as));                           *this, &MainWindow::on_action_file_save_as));
136      actionGroup->add(Gtk::Action::create("Properties",      actionGroup->add(Gtk::Action::create("Properties",
137                                           Gtk::Stock::PROPERTIES),                                           _("_Properties")),
138                       sigc::mem_fun(                       sigc::mem_fun(
139                           *this, &MainWindow::on_action_file_properties));                           *this, &MainWindow::on_action_file_properties));
140      actionGroup->add(Gtk::Action::create("InstrProperties",      actionGroup->add(Gtk::Action::create("InstrProperties",
141                                           Gtk::Stock::PROPERTIES),                                           _("_Properties")),
142                       sigc::mem_fun(                       sigc::mem_fun(
143                           *this, &MainWindow::show_instr_props));                           *this, &MainWindow::show_instr_props));
144      actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),      actionGroup->add(Gtk::Action::create("MidiRules",
145                                             _("_Midi Rules...")),
146                         sigc::mem_fun(
147                             *this, &MainWindow::show_midi_rules));
148        actionGroup->add(Gtk::Action::create("ScriptSlots",
149                                             _("_Script Slots...")),
150                         sigc::mem_fun(
151                             *this, &MainWindow::show_script_slots));
152        actionGroup->add(Gtk::Action::create("Quit", _("_Quit")),
153                         Gtk::AccelKey("<control>q"),
154                       sigc::mem_fun(                       sigc::mem_fun(
155                           *this, &MainWindow::on_action_quit));                           *this, &MainWindow::on_action_quit));
156      actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument")));      actionGroup->add(
157            Gtk::Action::create("MenuSample", _("_Sample")),
158            sigc::mem_fun(*this, &MainWindow::show_samples_tab)
159        );
160        actionGroup->add(
161            Gtk::Action::create("MenuInstrument", _("_Instrument")),
162            sigc::mem_fun(*this, &MainWindow::show_intruments_tab)
163        );
164        actionGroup->add(
165            Gtk::Action::create("MenuScript", _("S_cript")),
166            sigc::mem_fun(*this, &MainWindow::show_scripts_tab)
167        );
168        actionGroup->add(Gtk::Action::create("AllInstruments", _("_Select")));
169    
170        actionGroup->add(Gtk::Action::create("MenuEdit", _("_Edit")));
171    
172      action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);      Glib::RefPtr<Gtk::ToggleAction> toggle_action =
173      actionGroup->add(Gtk::Action::create("MenuHelp",          Gtk::ToggleAction::create("CopySampleUnity", _("Copy Sample's _Unity Note"));
174                                           action->property_label()));      toggle_action->set_active(true);
175  #ifdef ABOUT_DIALOG      actionGroup->add(toggle_action);
176      actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT),  
177        toggle_action =
178            Gtk::ToggleAction::create("CopySampleTune", _("Copy Sample's _Fine Tune"));
179        toggle_action->set_active(true);
180        actionGroup->add(toggle_action);
181    
182        toggle_action =
183            Gtk::ToggleAction::create("CopySampleLoop", _("Copy Sample's _Loop Points"));
184        toggle_action->set_active(true);
185        actionGroup->add(toggle_action);
186    
187    
188        actionGroup->add(Gtk::Action::create("MenuView", _("_View")));
189        toggle_action =
190            Gtk::ToggleAction::create("Statusbar", _("_Statusbar"));
191        toggle_action->set_active(true);
192        actionGroup->add(toggle_action,
193                         sigc::mem_fun(
194                             *this, &MainWindow::on_action_view_status_bar));
195        actionGroup->add(
196            Gtk::Action::create("RefreshAll", _("_Refresh All")),
197            sigc::mem_fun(*this, &MainWindow::on_action_refresh_all)
198        );                
199    
200        actionGroup->add(Gtk::Action::create("MenuHelp", _("_Help")));
201        actionGroup->add(Gtk::Action::create("About", _("_About")),
202                       sigc::mem_fun(                       sigc::mem_fun(
203                           *this, &MainWindow::on_action_help_about));                           *this, &MainWindow::on_action_help_about));
 #endif  
204      actionGroup->add(      actionGroup->add(
205          Gtk::Action::create("AddInstrument", _("Add _Instrument")),          Gtk::Action::create("AddInstrument", _("Add _Instrument")),
206          sigc::mem_fun(*this, &MainWindow::on_action_add_instrument)          sigc::mem_fun(*this, &MainWindow::on_action_add_instrument)
207      );      );
208      actionGroup->add(      actionGroup->add(
209          Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE),          Gtk::Action::create("DupInstrument", _("_Duplicate Instrument")),
210            sigc::mem_fun(*this, &MainWindow::on_action_duplicate_instrument)
211        );
212        actionGroup->add(
213            Gtk::Action::create("RemoveInstrument", _("_Remove")),
214          sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument)          sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument)
215      );      );
216    
217    
218        actionGroup->add(Gtk::Action::create("MenuSettings", _("_Settings")));
219        
220        toggle_action =
221            Gtk::ToggleAction::create("WarnUserOnExtensions", _("Show warning on format _extensions"));
222        toggle_action->set_active(Settings::singleton()->warnUserOnExtensions);
223        actionGroup->add(
224            toggle_action,
225            sigc::mem_fun(*this, &MainWindow::on_action_warn_user_on_extensions)
226        );
227    
228        toggle_action =
229            Gtk::ToggleAction::create("SyncSamplerInstrumentSelection", _("Synchronize sampler's instrument selection"));
230        toggle_action->set_active(Settings::singleton()->syncSamplerInstrumentSelection);
231        actionGroup->add(
232            toggle_action,
233            sigc::mem_fun(*this, &MainWindow::on_action_sync_sampler_instrument_selection)
234        );
235    
236        toggle_action =
237            Gtk::ToggleAction::create("MoveRootNoteWithRegionMoved", _("Move root note with region moved"));
238        toggle_action->set_active(Settings::singleton()->moveRootNoteWithRegionMoved);
239        actionGroup->add(
240            toggle_action,
241            sigc::mem_fun(*this, &MainWindow::on_action_move_root_note_with_region_moved)
242        );
243    
244    
245        actionGroup->add(Gtk::Action::create("MenuTools", _("_Tools")));
246    
247        actionGroup->add(
248            Gtk::Action::create("CombineInstruments", _("_Combine Instruments...")),
249            sigc::mem_fun(*this, &MainWindow::on_action_combine_instruments)
250        );
251    
252        actionGroup->add(
253            Gtk::Action::create("MergeFiles", _("_Merge Files...")),
254            sigc::mem_fun(*this, &MainWindow::on_action_merge_files)
255        );
256    
257    
258      // sample right-click popup actions      // sample right-click popup actions
259      actionGroup->add(      actionGroup->add(
260          Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES),          Gtk::Action::create("SampleProperties", _("_Properties")),
261          sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)          sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)
262      );      );
263      actionGroup->add(      actionGroup->add(
# Line 142  MainWindow::MainWindow() Line 265  MainWindow::MainWindow()
265          sigc::mem_fun(*this, &MainWindow::on_action_add_group)          sigc::mem_fun(*this, &MainWindow::on_action_add_group)
266      );      );
267      actionGroup->add(      actionGroup->add(
268          Gtk::Action::create("AddSample", _("Add _Sample(s)")),          Gtk::Action::create("AddSample", _("Add _Sample(s)...")),
269          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
270      );      );
271      actionGroup->add(      actionGroup->add(
272          Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),          Gtk::Action::create("RemoveSample", _("_Remove")),
273          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
274      );      );
275        actionGroup->add(
276            Gtk::Action::create("RemoveUnusedSamples", _("Remove _Unused Samples")),
277            sigc::mem_fun(*this, &MainWindow::on_action_remove_unused_samples)
278        );
279        actionGroup->add(
280            Gtk::Action::create("ShowSampleRefs", _("Show References...")),
281            sigc::mem_fun(*this, &MainWindow::on_action_view_references)
282        );
283        actionGroup->add(
284            Gtk::Action::create("ReplaceSample",
285                                _("Replace Sample...")),
286            sigc::mem_fun(*this, &MainWindow::on_action_replace_sample)
287        );
288        actionGroup->add(
289            Gtk::Action::create("ReplaceAllSamplesInAllGroups",
290                                _("Replace All Samples in All Groups...")),
291            sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups)
292        );
293        
294        // script right-click popup actions
295        actionGroup->add(
296            Gtk::Action::create("AddScriptGroup", _("Add _Group")),
297            sigc::mem_fun(*this, &MainWindow::on_action_add_script_group)
298        );
299        actionGroup->add(
300            Gtk::Action::create("AddScript", _("Add _Script")),
301            sigc::mem_fun(*this, &MainWindow::on_action_add_script)
302        );
303        actionGroup->add(
304            Gtk::Action::create("EditScript", _("_Edit Script...")),
305            sigc::mem_fun(*this, &MainWindow::on_action_edit_script)
306        );
307        actionGroup->add(
308            Gtk::Action::create("RemoveScript", _("_Remove")),
309            sigc::mem_fun(*this, &MainWindow::on_action_remove_script)
310        );
311    
312      uiManager = Gtk::UIManager::create();      uiManager = Gtk::UIManager::create();
313      uiManager->insert_action_group(actionGroup);      uiManager->insert_action_group(actionGroup);
# Line 168  MainWindow::MainWindow() Line 327  MainWindow::MainWindow()
327          "      <separator/>"          "      <separator/>"
328          "      <menuitem action='Quit'/>"          "      <menuitem action='Quit'/>"
329          "    </menu>"          "    </menu>"
330            "    <menu action='MenuEdit'>"
331            "      <menuitem action='CopySampleUnity'/>"
332            "      <menuitem action='CopySampleTune'/>"
333            "      <menuitem action='CopySampleLoop'/>"
334            "    </menu>"
335            "    <menu action='MenuSample'>"
336            "      <menuitem action='SampleProperties'/>"
337            "      <menuitem action='AddGroup'/>"
338            "      <menuitem action='AddSample'/>"
339            "      <menuitem action='ShowSampleRefs'/>"
340            "      <menuitem action='ReplaceSample' />"
341            "      <menuitem action='ReplaceAllSamplesInAllGroups' />"
342            "      <separator/>"
343            "      <menuitem action='RemoveSample'/>"
344            "      <menuitem action='RemoveUnusedSamples'/>"
345            "    </menu>"
346          "    <menu action='MenuInstrument'>"          "    <menu action='MenuInstrument'>"
347            "      <menu action='AllInstruments'>"
348            "      </menu>"
349            "      <separator/>"
350            "      <menuitem action='InstrProperties'/>"
351            "      <menuitem action='MidiRules'/>"
352            "      <menuitem action='ScriptSlots'/>"
353            "      <menuitem action='AddInstrument'/>"
354            "      <menuitem action='DupInstrument'/>"
355            "      <separator/>"
356            "      <menuitem action='RemoveInstrument'/>"
357            "    </menu>"
358            "    <menu action='MenuScript'>"
359            "      <menuitem action='AddScriptGroup'/>"
360            "      <menuitem action='AddScript'/>"
361            "      <menuitem action='EditScript'/>"
362            "      <separator/>"
363            "      <menuitem action='RemoveScript'/>"
364            "    </menu>"
365            "    <menu action='MenuView'>"
366            "      <menuitem action='Statusbar'/>"
367            "      <separator/>"
368            "      <menuitem action='RefreshAll'/>"
369            "    </menu>"
370            "    <menu action='MenuTools'>"
371            "      <menuitem action='CombineInstruments'/>"
372            "      <menuitem action='MergeFiles'/>"
373            "    </menu>"
374            "    <menu action='MenuSettings'>"
375            "      <menuitem action='WarnUserOnExtensions'/>"
376            "      <menuitem action='SyncSamplerInstrumentSelection'/>"
377            "      <menuitem action='MoveRootNoteWithRegionMoved'/>"
378          "    </menu>"          "    </menu>"
 #ifdef ABOUT_DIALOG  
379          "    <menu action='MenuHelp'>"          "    <menu action='MenuHelp'>"
380          "      <menuitem action='About'/>"          "      <menuitem action='About'/>"
381          "    </menu>"          "    </menu>"
 #endif  
382          "  </menubar>"          "  </menubar>"
383          "  <popup name='PopupMenu'>"          "  <popup name='PopupMenu'>"
384          "    <menuitem action='InstrProperties'/>"          "    <menuitem action='InstrProperties'/>"
385            "    <menuitem action='MidiRules'/>"
386            "    <menuitem action='ScriptSlots'/>"
387          "    <menuitem action='AddInstrument'/>"          "    <menuitem action='AddInstrument'/>"
388            "    <menuitem action='DupInstrument'/>"
389          "    <separator/>"          "    <separator/>"
390          "    <menuitem action='RemoveInstrument'/>"          "    <menuitem action='RemoveInstrument'/>"
391          "  </popup>"          "  </popup>"
# Line 186  MainWindow::MainWindow() Line 393  MainWindow::MainWindow()
393          "    <menuitem action='SampleProperties'/>"          "    <menuitem action='SampleProperties'/>"
394          "    <menuitem action='AddGroup'/>"          "    <menuitem action='AddGroup'/>"
395          "    <menuitem action='AddSample'/>"          "    <menuitem action='AddSample'/>"
396            "    <menuitem action='ShowSampleRefs'/>"
397            "    <menuitem action='ReplaceSample' />"
398            "    <menuitem action='ReplaceAllSamplesInAllGroups' />"
399          "    <separator/>"          "    <separator/>"
400          "    <menuitem action='RemoveSample'/>"          "    <menuitem action='RemoveSample'/>"
401            "    <menuitem action='RemoveUnusedSamples'/>"
402            "  </popup>"
403            "  <popup name='ScriptPopupMenu'>"
404            "    <menuitem action='AddScriptGroup'/>"
405            "    <menuitem action='AddScript'/>"
406            "    <menuitem action='EditScript'/>"
407            "    <separator/>"
408            "    <menuitem action='RemoveScript'/>"
409          "  </popup>"          "  </popup>"
410          "</ui>";          "</ui>";
411      uiManager->add_ui_from_string(ui_info);      uiManager->add_ui_from_string(ui_info);
412    
413      popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));      popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));
414        
415        // Set tooltips for menu items (for some reason, setting a tooltip on the
416        // respective Gtk::Action objects above will simply be ignored, no matter
417        // if using Gtk::Action::set_tooltip() or passing the tooltip string on
418        // Gtk::Action::create()).
419        {
420            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
421                uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity"));
422            item->set_tooltip_text(_("Used when dragging a sample to a region's sample reference field. You may disable this for example if you want to replace an existing sample in a region with a new sample, but don't want that the region's current unity note setting will be altered by this action."));
423        }
424        {
425            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
426                uiManager->get_widget("/MenuBar/MenuEdit/CopySampleTune"));
427            item->set_tooltip_text(_("Used when dragging a sample to a region's sample reference field. You may disable this for example if you want to replace an existing sample in a region with a new sample, but don't want that the region's current sample playback tuning will be altered by this action."));
428        }
429        {
430            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
431                uiManager->get_widget("/MenuBar/MenuEdit/CopySampleLoop"));
432            item->set_tooltip_text(_("Used when dragging a sample to a region's sample reference field. You may disable this for example if you want to replace an existing sample in a region with a new sample, but don't want that the region's current loop informations to be altered by this action."));
433        }
434        {
435            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
436                uiManager->get_widget("/MenuBar/MenuSettings/WarnUserOnExtensions"));
437            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."));
438        }
439        {
440            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
441                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
442            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)."));
443        }
444        {
445            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
446                uiManager->get_widget("/MenuBar/MenuSettings/MoveRootNoteWithRegionMoved"));
447            item->set_tooltip_text(_("If checked, and when a region is moved by dragging it around on the virtual keyboard, the keybord position dependent pitch will move exactly with the amount of semi tones the region was moved around."));
448        }
449        {
450            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
451                uiManager->get_widget("/MenuBar/MenuSample/RemoveUnusedSamples"));
452            item->set_tooltip_text(_("Removes all samples that are not referenced by any instrument (i.e. red ones)."));
453            // copy tooltip to popup menu
454            Gtk::MenuItem* item2 = dynamic_cast<Gtk::MenuItem*>(
455                uiManager->get_widget("/SamplePopupMenu/RemoveUnusedSamples"));
456            item2->set_tooltip_text(item->get_tooltip_text());
457        }
458        {
459            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
460                uiManager->get_widget("/MenuBar/MenuView/RefreshAll"));
461            item->set_tooltip_text(_("Reloads the currently open gig file and updates the entire graphical user interface."));
462        }
463        {
464            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
465                uiManager->get_widget("/MenuBar/MenuTools/CombineInstruments"));
466            item->set_tooltip_text(_("Create combi sounds out of individual sounds of this .gig file."));
467        }
468        {
469            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
470                uiManager->get_widget("/MenuBar/MenuTools/MergeFiles"));
471            item->set_tooltip_text(_("Add instruments and samples of other .gig files to this .gig file."));
472        }
473    
474    
475        instrument_menu = static_cast<Gtk::MenuItem*>(
476            uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments"))->get_submenu();
477    
478      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
479      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
480      m_VBox.pack_start(m_HPaned);      m_VBox.pack_start(m_HPaned);
481      m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK);      m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK);
482        m_VBox.pack_start(m_RegionChooser.m_VirtKeybPropsBox, Gtk::PACK_SHRINK);
483      m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);      m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);
484        m_VBox.pack_start(m_StatusBar, Gtk::PACK_SHRINK);
485    
486        set_file_is_shared(false);
487    
488        // Status Bar:
489        m_StatusBar.pack_start(m_AttachedStateLabel, Gtk::PACK_SHRINK);
490        m_StatusBar.pack_start(m_AttachedStateImage, Gtk::PACK_SHRINK);
491        m_StatusBar.show();
492    
493      m_RegionChooser.signal_region_selected().connect(      m_RegionChooser.signal_region_selected().connect(
494          sigc::mem_fun(*this, &MainWindow::region_changed) );          sigc::mem_fun(*this, &MainWindow::region_changed) );
# Line 209  MainWindow::MainWindow() Line 499  MainWindow::MainWindow()
499      // Create the Tree model:      // Create the Tree model:
500      m_refTreeModel = Gtk::ListStore::create(m_Columns);      m_refTreeModel = Gtk::ListStore::create(m_Columns);
501      m_TreeView.set_model(m_refTreeModel);      m_TreeView.set_model(m_refTreeModel);
502      m_refTreeModel->signal_row_changed().connect(      m_TreeView.set_tooltip_text(_("Right click here for actions on instruments & MIDI Rules. Drag & drop to change the order of instruments."));
503        instrument_name_connection = m_refTreeModel->signal_row_changed().connect(
504          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
505      );      );
506    
507      // Add the TreeView's view columns:      // Add the TreeView's view columns:
508      m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);      m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);
509      m_TreeView.set_headers_visible(false);      m_TreeView.set_headers_visible(false);
510        
511        // establish drag&drop within the instrument tree view, allowing to reorder
512        // the sequence of instruments within the gig file
513        {
514            std::vector<Gtk::TargetEntry> drag_target_instrument;
515            drag_target_instrument.push_back(Gtk::TargetEntry("gig::Instrument"));
516            m_TreeView.drag_source_set(drag_target_instrument);
517            m_TreeView.drag_dest_set(drag_target_instrument);
518            m_TreeView.signal_drag_begin().connect(
519                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_begin)
520            );
521            m_TreeView.signal_drag_data_get().connect(
522                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_data_get)
523            );
524            m_TreeView.signal_drag_data_received().connect(
525                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drop_drag_data_received)
526            );
527        }
528    
529      // create samples treeview (including its data model)      // create samples treeview (including its data model)
530      m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);      m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);
531      m_TreeViewSamples.set_model(m_refSamplesTreeModel);      m_TreeViewSamples.set_model(m_refSamplesTreeModel);
532        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."));
533      // m_TreeViewSamples.set_reorderable();      // m_TreeViewSamples.set_reorderable();
534      m_TreeViewSamples.append_column_editable("Samples", m_SamplesModel.m_col_name);      m_TreeViewSamples.append_column_editable(_("Name"), m_SamplesModel.m_col_name);
535      m_TreeViewSamples.set_headers_visible(false);      m_TreeViewSamples.append_column(_("Referenced"), m_SamplesModel.m_col_refcount);
536        {
537            Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(0);
538            Gtk::CellRendererText* cellrenderer =
539                dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell());
540            column->add_attribute(
541                cellrenderer->property_foreground(), m_SamplesModel.m_color
542            );
543        }
544        {
545            Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(1);
546            Gtk::CellRendererText* cellrenderer =
547                dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell());
548            column->add_attribute(
549                cellrenderer->property_foreground(), m_SamplesModel.m_color
550            );
551        }
552        m_TreeViewSamples.set_headers_visible(true);
553      m_TreeViewSamples.signal_button_press_event().connect_notify(      m_TreeViewSamples.signal_button_press_event().connect_notify(
554          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
555      );      );
# Line 230  MainWindow::MainWindow() Line 557  MainWindow::MainWindow()
557          sigc::mem_fun(*this, &MainWindow::sample_name_changed)          sigc::mem_fun(*this, &MainWindow::sample_name_changed)
558      );      );
559    
560        // create scripts treeview (including its data model)
561        m_refScriptsTreeModel = ScriptsTreeStore::create(m_ScriptsModel);
562        m_TreeViewScripts.set_model(m_refScriptsTreeModel);
563        m_TreeViewScripts.set_tooltip_text(_(
564            "Use CTRL + double click for editing a script."
565            "\n\n"
566            "Note: instrument scripts are a LinuxSampler extension of the gig "
567            "format. This feature will not work with the GigaStudio software!"
568        ));
569        // m_TreeViewScripts.set_reorderable();
570        m_TreeViewScripts.append_column_editable("Samples", m_ScriptsModel.m_col_name);
571        m_TreeViewScripts.set_headers_visible(false);
572        m_TreeViewScripts.signal_button_press_event().connect_notify(
573            sigc::mem_fun(*this, &MainWindow::on_script_treeview_button_release)
574        );
575        //FIXME: why the heck does this double click signal_row_activated() only fire while CTRL key is pressed ?
576        m_TreeViewScripts.signal_row_activated().connect(
577            sigc::mem_fun(*this, &MainWindow::script_double_clicked)
578        );
579        m_refScriptsTreeModel->signal_row_changed().connect(
580            sigc::mem_fun(*this, &MainWindow::script_name_changed)
581        );
582    
583        // establish drag&drop between scripts tree view and ScriptSlots window
584        std::vector<Gtk::TargetEntry> drag_target_gig_script;
585        drag_target_gig_script.push_back(Gtk::TargetEntry("gig::Script"));
586        m_TreeViewScripts.drag_source_set(drag_target_gig_script);
587        m_TreeViewScripts.signal_drag_begin().connect(
588            sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_begin)
589        );
590        m_TreeViewScripts.signal_drag_data_get().connect(
591            sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_data_get)
592        );
593    
594      // 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
595      std::list<Gtk::TargetEntry> drag_target_gig_sample;      std::vector<Gtk::TargetEntry> drag_target_gig_sample;
596      drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") );      drag_target_gig_sample.push_back(Gtk::TargetEntry("gig::Sample"));
597      m_TreeViewSamples.drag_source_set(drag_target_gig_sample);      m_TreeViewSamples.drag_source_set(drag_target_gig_sample);
598        m_TreeViewSamples.signal_drag_begin().connect(
599            sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_begin)
600        );
601      m_TreeViewSamples.signal_drag_data_get().connect(      m_TreeViewSamples.signal_drag_data_get().connect(
602          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get)          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get)
603      );      );
# Line 242  MainWindow::MainWindow() Line 606  MainWindow::MainWindow()
606          sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received)          sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received)
607      );      );
608      dimreg_edit.signal_dimreg_changed().connect(      dimreg_edit.signal_dimreg_changed().connect(
609          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::hide(sigc::mem_fun(*this, &MainWindow::file_changed)));
610      m_RegionChooser.signal_instrument_changed().connect(      m_RegionChooser.signal_instrument_changed().connect(
611          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::mem_fun(*this, &MainWindow::file_changed));
612        m_RegionChooser.signal_instrument_changed().connect(
613            sigc::mem_fun(*this, &MainWindow::region_changed));
614      m_DimRegionChooser.signal_region_changed().connect(      m_DimRegionChooser.signal_region_changed().connect(
615          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::mem_fun(*this, &MainWindow::file_changed));
616      instrumentProps.signal_instrument_changed().connect(      instrumentProps.signal_changed().connect(
617            sigc::mem_fun(*this, &MainWindow::file_changed));
618        propDialog.signal_changed().connect(
619            sigc::mem_fun(*this, &MainWindow::file_changed));
620        midiRules.signal_changed().connect(
621          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::mem_fun(*this, &MainWindow::file_changed));
622    
623        dimreg_edit.signal_dimreg_to_be_changed().connect(
624            dimreg_to_be_changed_signal.make_slot());
625        dimreg_edit.signal_dimreg_changed().connect(
626            dimreg_changed_signal.make_slot());
627        dimreg_edit.signal_sample_ref_changed().connect(
628            sample_ref_changed_signal.make_slot());
629        sample_ref_changed_signal.connect(
630            sigc::mem_fun(*this, &MainWindow::on_sample_ref_changed)
631        );
632        samples_to_be_removed_signal.connect(
633            sigc::mem_fun(*this, &MainWindow::on_samples_to_be_removed)
634        );
635    
636        dimreg_edit.signal_select_sample().connect(
637            sigc::mem_fun(*this, &MainWindow::select_sample)
638        );
639    
640        m_RegionChooser.signal_instrument_struct_to_be_changed().connect(
641            sigc::hide(
642                sigc::bind(
643                    file_structure_to_be_changed_signal.make_slot(),
644                    sigc::ref(this->file)
645                )
646            )
647        );
648        m_RegionChooser.signal_instrument_struct_changed().connect(
649            sigc::hide(
650                sigc::bind(
651                    file_structure_changed_signal.make_slot(),
652                    sigc::ref(this->file)
653                )
654            )
655        );
656        m_RegionChooser.signal_region_to_be_changed().connect(
657            region_to_be_changed_signal.make_slot());
658        m_RegionChooser.signal_region_changed_signal().connect(
659            region_changed_signal.make_slot());
660    
661        note_on_signal.connect(
662            sigc::mem_fun(m_RegionChooser, &RegionChooser::on_note_on_event));
663        note_off_signal.connect(
664            sigc::mem_fun(m_RegionChooser, &RegionChooser::on_note_off_event));
665    
666        dimreg_all_regions.signal_toggled().connect(
667            sigc::mem_fun(*this, &MainWindow::update_dimregs));
668        dimreg_all_dimregs.signal_toggled().connect(
669            sigc::mem_fun(*this, &MainWindow::dimreg_all_dimregs_toggled));
670        dimreg_stereo.signal_toggled().connect(
671            sigc::mem_fun(*this, &MainWindow::update_dimregs));
672    
673      file = 0;      file = 0;
674      file_is_changed = false;      file_is_changed = false;
675    
676      show_all_children();      show_all_children();
677    
678        // start with a new gig file by default
679        on_action_file_new();
680    
681        // select 'Instruments' tab by default
682        // (gtk allows this only if the tab childs are visible, thats why it's here)
683        m_TreeViewNotebook.set_current_page(1);
684  }  }
685    
686  MainWindow::~MainWindow()  MainWindow::~MainWindow()
# Line 261  MainWindow::~MainWindow() Line 689  MainWindow::~MainWindow()
689    
690  bool MainWindow::on_delete_event(GdkEventAny* event)  bool MainWindow::on_delete_event(GdkEventAny* event)
691  {  {
692      return file_is_changed && !close_confirmation_dialog();      return !file_is_shared && file_is_changed && !close_confirmation_dialog();
693  }  }
694    
695  void MainWindow::on_action_quit()  void MainWindow::on_action_quit()
696  {  {
697      if (file_is_changed && !close_confirmation_dialog()) return;      if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
698      hide();      hide();
699  }  }
700    
# Line 275  void MainWindow::region_changed() Line 703  void MainWindow::region_changed()
703      m_DimRegionChooser.set_region(m_RegionChooser.get_region());      m_DimRegionChooser.set_region(m_RegionChooser.get_region());
704  }  }
705    
706    gig::Instrument* MainWindow::get_instrument()
707    {
708        gig::Instrument* instrument = 0;
709        Gtk::TreeModel::const_iterator it =
710            m_TreeView.get_selection()->get_selected();
711        if (it) {
712            Gtk::TreeModel::Row row = *it;
713            instrument = row[m_Columns.m_col_instr];
714        }
715        return instrument;
716    }
717    
718    void MainWindow::add_region_to_dimregs(gig::Region* region, bool stereo, bool all_dimregs)
719    {
720        if (all_dimregs) {
721            for (int i = 0 ; i < region->DimensionRegions ; i++) {
722                if (region->pDimensionRegions[i]) {
723                    dimreg_edit.dimregs.insert(region->pDimensionRegions[i]);
724                }
725            }
726        } else {
727            m_DimRegionChooser.get_dimregions(region, stereo, dimreg_edit.dimregs);
728        }
729    }
730    
731    void MainWindow::update_dimregs()
732    {
733        dimreg_edit.dimregs.clear();
734        bool all_regions = dimreg_all_regions.get_active();
735        bool stereo = dimreg_stereo.get_active();
736        bool all_dimregs = dimreg_all_dimregs.get_active();
737    
738        if (all_regions) {
739            gig::Instrument* instrument = get_instrument();
740            if (instrument) {
741                for (gig::Region* region = instrument->GetFirstRegion() ;
742                     region ;
743                     region = instrument->GetNextRegion()) {
744                    add_region_to_dimregs(region, stereo, all_dimregs);
745                }
746            }
747        } else {
748            gig::Region* region = m_RegionChooser.get_region();
749            if (region) {
750                add_region_to_dimregs(region, stereo, all_dimregs);
751            }
752        }
753    }
754    
755    void MainWindow::dimreg_all_dimregs_toggled()
756    {
757        dimreg_stereo.set_sensitive(!dimreg_all_dimregs.get_active());
758        update_dimregs();
759    }
760    
761  void MainWindow::dimreg_changed()  void MainWindow::dimreg_changed()
762  {  {
763      dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());      update_dimregs();
764        dimreg_edit.set_dim_region(m_DimRegionChooser.get_main_dimregion());
765  }  }
766    
767  void MainWindow::on_sel_change()  void MainWindow::on_sel_change()
768  {  {
769      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      // select item in instrument menu
770        Gtk::TreeModel::iterator it = m_TreeView.get_selection()->get_selected();
     Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();  
771      if (it) {      if (it) {
772          Gtk::TreeModel::Row row = *it;          Gtk::TreePath path(it);
773          std::cout << row[m_Columns.m_col_name] << std::endl;          int index = path[0];
774            const std::vector<Gtk::Widget*> children =
775                instrument_menu->get_children();
776            static_cast<Gtk::RadioMenuItem*>(children[index])->set_active();
777        }
778    
779          m_RegionChooser.set_instrument(row[m_Columns.m_col_instr]);      m_RegionChooser.set_instrument(get_instrument());
780      } else {  
781          m_RegionChooser.set_instrument(0);      if (Settings::singleton()->syncSamplerInstrumentSelection) {
782            switch_sampler_instrument_signal.emit(get_instrument());
783      }      }
784  }  }
785    
# Line 304  void loader_progress_callback(gig::progr Line 792  void loader_progress_callback(gig::progr
792  void Loader::progress_callback(float fraction)  void Loader::progress_callback(float fraction)
793  {  {
794      {      {
795          Glib::Mutex::Lock lock(progressMutex);          Glib::Threads::Mutex::Lock lock(progressMutex);
796          progress = fraction;          progress = fraction;
797      }      }
798      progress_dispatcher();      progress_dispatcher();
# Line 312  void Loader::progress_callback(float fra Line 800  void Loader::progress_callback(float fra
800    
801  void Loader::thread_function()  void Loader::thread_function()
802  {  {
803      printf("thread_function self=%x\n", Glib::Thread::self());      printf("thread_function self=%p\n",
804      printf("Start %s\n", filename);             static_cast<void*>(Glib::Threads::Thread::self()));
805      RIFF::File* riff = new RIFF::File(filename);      printf("Start %s\n", filename.c_str());
806      gig = new gig::File(riff);      try {
807      gig::progress_t progress;          RIFF::File* riff = new RIFF::File(filename);
808      progress.callback = loader_progress_callback;          gig = new gig::File(riff);
809      progress.custom = this;          gig::progress_t progress;
810            progress.callback = loader_progress_callback;
811      gig->GetInstrument(0, &progress);          progress.custom = this;
812      printf("End\n");  
813      finished_dispatcher();          gig->GetInstrument(0, &progress);
814            printf("End\n");
815            finished_dispatcher();
816        } catch (RIFF::Exception e) {
817            error_message = e.Message;
818            error_dispatcher.emit();
819        } catch (...) {
820            error_message = _("Unknown exception occurred");
821            error_dispatcher.emit();
822        }
823  }  }
824    
825  Loader::Loader(const char* filename)  Loader::Loader(const char* filename)
826      : thread(0), filename(filename)      : filename(filename), gig(0), thread(0), progress(0.f)
827  {  {
828  }  }
829    
830  void Loader::launch()  void Loader::launch()
831  {  {
832    #ifdef OLD_THREADS
833      thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);      thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
834      printf("launch thread=%x\n", thread);  #else
835        thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Loader::thread_function));
836    #endif
837        printf("launch thread=%p\n", static_cast<void*>(thread));
838  }  }
839    
840  float Loader::get_progress()  float Loader::get_progress()
841  {  {
842      float res;      float res;
843      {      {
844          Glib::Mutex::Lock lock(progressMutex);          Glib::Threads::Mutex::Lock lock(progressMutex);
845          res = progress;          res = progress;
846      }      }
847      return res;      return res;
# Line 356  Glib::Dispatcher& Loader::signal_finishe Line 857  Glib::Dispatcher& Loader::signal_finishe
857      return finished_dispatcher;      return finished_dispatcher;
858  }  }
859    
860  LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)  Glib::Dispatcher& Loader::signal_error()
861    {
862        return error_dispatcher;
863    }
864    
865    void saver_progress_callback(gig::progress_t* progress)
866    {
867        Saver* saver = static_cast<Saver*>(progress->custom);
868        saver->progress_callback(progress->factor);
869    }
870    
871    void Saver::progress_callback(float fraction)
872    {
873        {
874            Glib::Threads::Mutex::Lock lock(progressMutex);
875            progress = fraction;
876        }
877        progress_dispatcher.emit();
878    }
879    
880    void Saver::thread_function()
881    {
882        printf("thread_function self=%p\n",
883               static_cast<void*>(Glib::Threads::Thread::self()));
884        printf("Start %s\n", filename.c_str());
885        try {
886            gig::progress_t progress;
887            progress.callback = saver_progress_callback;
888            progress.custom = this;
889    
890            // if no filename was provided, that means "save", if filename was provided means "save as"
891            if (filename.empty()) {
892                gig->Save(&progress);
893            } else {
894                gig->Save(filename, &progress);
895            }
896    
897            printf("End\n");
898            finished_dispatcher.emit();
899        } catch (RIFF::Exception e) {
900            error_message = e.Message;
901            error_dispatcher.emit();
902        } catch (...) {
903            error_message = _("Unknown exception occurred");
904            error_dispatcher.emit();
905        }
906    }
907    
908    Saver::Saver(gig::File* file, Glib::ustring filename)
909        : gig(file), filename(filename), thread(0), progress(0.f)
910    {
911    }
912    
913    void Saver::launch()
914    {
915    #ifdef OLD_THREADS
916        thread = Glib::Thread::create(sigc::mem_fun(*this, &Saver::thread_function), true);
917    #else
918        thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Saver::thread_function));
919    #endif
920        printf("launch thread=%p\n", static_cast<void*>(thread));
921    }
922    
923    float Saver::get_progress()
924    {
925        float res;
926        {
927            Glib::Threads::Mutex::Lock lock(progressMutex);
928            res = progress;
929        }
930        return res;
931    }
932    
933    Glib::Dispatcher& Saver::signal_progress()
934    {
935        return progress_dispatcher;
936    }
937    
938    Glib::Dispatcher& Saver::signal_finished()
939    {
940        return finished_dispatcher;
941    }
942    
943    Glib::Dispatcher& Saver::signal_error()
944    {
945        return error_dispatcher;
946    }
947    
948    ProgressDialog::ProgressDialog(const Glib::ustring& title, Gtk::Window& parent)
949      : Gtk::Dialog(title, parent, true)      : Gtk::Dialog(title, parent, true)
950  {  {
951      get_vbox()->pack_start(progressBar);      get_vbox()->pack_start(progressBar);
952      show_all_children();      show_all_children();
953        resize(600,50);
954  }  }
955    
956  // Clear all GUI elements / controls. This method is typically called  // Clear all GUI elements / controls. This method is typically called
957  // before a new .gig file is to be created or to be loaded.  // before a new .gig file is to be created or to be loaded.
958  void MainWindow::__clear() {  void MainWindow::__clear() {
     // remove all entries from "Instrument" menu  
     Gtk::MenuItem* instrument_menu =  
         dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));  
     instrument_menu->hide();  
     for (int i = 0; i < instrument_menu->get_submenu()->items().size(); i++) {  
         delete &instrument_menu->get_submenu()->items()[i];  
     }  
     instrument_menu->get_submenu()->items().clear();  
959      // forget all samples that ought to be imported      // forget all samples that ought to be imported
960      m_SampleImportQueue.clear();      m_SampleImportQueue.clear();
961      // clear the samples and instruments tree views      // clear the samples and instruments tree views
962      m_refTreeModel->clear();      m_refTreeModel->clear();
963      m_refSamplesTreeModel->clear();      m_refSamplesTreeModel->clear();
964        m_refScriptsTreeModel->clear();
965        // remove all entries from "Instrument" menu
966        while (!instrument_menu->get_children().empty()) {
967            remove_instrument_from_menu(0);
968        }
969      // free libgig's gig::File instance      // free libgig's gig::File instance
970      if (file) {      if (file && !file_is_shared) delete file;
971          delete file;      file = NULL;
972          file = NULL;      set_file_is_shared(false);
973    }
974    
975    void MainWindow::__refreshEntireGUI() {
976        // clear the samples and instruments tree views
977        m_refTreeModel->clear();
978        m_refSamplesTreeModel->clear();
979        m_refScriptsTreeModel->clear();
980        // remove all entries from "Instrument" menu
981        while (!instrument_menu->get_children().empty()) {
982            remove_instrument_from_menu(0);
983      }      }
984    
985        if (!this->file) return;
986    
987        load_gig(
988            this->file, this->file->pInfo->Name.c_str(), this->file_is_shared
989        );
990  }  }
991    
992  void MainWindow::on_action_file_new()  void MainWindow::on_action_file_new()
993  {  {
994      if (file_is_changed && !close_confirmation_dialog()) return;      if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
995    
996        if (file_is_shared && !leaving_shared_mode_dialog()) return;
997    
998      // clear all GUI elements      // clear all GUI elements
999      __clear();      __clear();
# Line 396  void MainWindow::on_action_file_new() Line 1001  void MainWindow::on_action_file_new()
1001      gig::File* pFile = new gig::File;      gig::File* pFile = new gig::File;
1002      // already add one new instrument by default      // already add one new instrument by default
1003      gig::Instrument* pInstrument = pFile->AddInstrument();      gig::Instrument* pInstrument = pFile->AddInstrument();
1004      pInstrument->pInfo->Name = "Unnamed Instrument";      pInstrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument"));
1005      // update GUI with that new gig::File      // update GUI with that new gig::File
1006      load_gig(pFile, 0 /*no file name yet*/);      load_gig(pFile, 0 /*no file name yet*/);
1007  }  }
# Line 409  bool MainWindow::close_confirmation_dial Line 1014  bool MainWindow::close_confirmation_dial
1014      g_free(msg);      g_free(msg);
1015      dialog.set_secondary_text(_("If you close without saving, your changes will be lost."));      dialog.set_secondary_text(_("If you close without saving, your changes will be lost."));
1016      dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);      dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);
1017      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1018      dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES);      dialog.add_button(file_has_name ? _("_Save") : _("Save _As"), Gtk::RESPONSE_YES);
1019      dialog.set_default_response(Gtk::RESPONSE_YES);      dialog.set_default_response(Gtk::RESPONSE_YES);
1020      int response = dialog.run();      int response = dialog.run();
1021      if (response == Gtk::RESPONSE_YES) return file_save();      dialog.hide();
1022      return response != Gtk::RESPONSE_CANCEL;  
1023        // user decided to exit app without saving
1024        if (response == Gtk::RESPONSE_NO) return true;
1025    
1026        // user cancelled dialog, thus don't close app
1027        if (response == Gtk::RESPONSE_CANCEL) return false;
1028    
1029        // TODO: the following return valid is disabled and hard coded instead for
1030        // now, due to the fact that saving with progress bar is now implemented
1031        // asynchronously, as a result the app does not close automatically anymore
1032        // after saving the file has completed
1033        //
1034        //   if (response == Gtk::RESPONSE_YES) return file_save();
1035        //   return response != Gtk::RESPONSE_CANCEL;
1036        //
1037        if (response == Gtk::RESPONSE_YES) file_save();
1038        return false; // always prevent closing the app for now (see comment above)
1039    }
1040    
1041    bool MainWindow::leaving_shared_mode_dialog() {
1042        Glib::ustring msg = _("Detach from sampler and proceed working stand-alone?");
1043        Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
1044        dialog.set_secondary_text(
1045            _("If you proceed to work on another instrument file, it won't be "
1046              "used by the sampler until you tell the sampler explicitly to "
1047              "load it."));
1048        dialog.add_button(_("_Yes, Detach"), Gtk::RESPONSE_YES);
1049        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1050        dialog.set_default_response(Gtk::RESPONSE_CANCEL);
1051        int response = dialog.run();
1052        dialog.hide();
1053        return response == Gtk::RESPONSE_YES;
1054  }  }
1055    
1056  void MainWindow::on_action_file_open()  void MainWindow::on_action_file_open()
1057  {  {
1058      if (file_is_changed && !close_confirmation_dialog()) return;      if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
1059    
1060        if (file_is_shared && !leaving_shared_mode_dialog()) return;
1061    
1062      Gtk::FileChooserDialog dialog(*this, _("Open file"));      Gtk::FileChooserDialog dialog(*this, _("Open file"));
1063      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1064      dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);      dialog.add_button(_("_Open"), Gtk::RESPONSE_OK);
1065      dialog.set_default_response(Gtk::RESPONSE_OK);      dialog.set_default_response(Gtk::RESPONSE_OK);
1066    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
1067      Gtk::FileFilter filter;      Gtk::FileFilter filter;
1068      filter.add_pattern("*.gig");      filter.add_pattern("*.gig");
1069    #else
1070        Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
1071        filter->add_pattern("*.gig");
1072    #endif
1073      dialog.set_filter(filter);      dialog.set_filter(filter);
1074      if (current_dir != "") {      if (current_gig_dir != "") {
1075          dialog.set_current_folder(current_dir);          dialog.set_current_folder(current_gig_dir);
1076      }      }
1077      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1078          std::string filename = dialog.get_filename();          std::string filename = dialog.get_filename();
1079          printf("filename=%s\n", filename.c_str());          printf("filename=%s\n", filename.c_str());
1080          __clear();          printf("on_action_file_open self=%p\n",
1081          printf("on_action_file_open self=%x\n", Glib::Thread::self());                 static_cast<void*>(Glib::Threads::Thread::self()));
1082          load_file(filename.c_str());          load_file(filename.c_str());
1083          current_dir = Glib::path_get_dirname(filename);          current_gig_dir = Glib::path_get_dirname(filename);
1084      }      }
1085  }  }
1086    
1087  void MainWindow::load_file(const char* name)  void MainWindow::load_file(const char* name)
1088  {  {
1089      load_dialog = new LoadDialog("Loading...", *this);      __clear();
1090      load_dialog->show_all();  
1091      loader = new Loader(strdup(name));      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1092            _("Loading") +  Glib::ustring(" '") +
1093            Glib::filename_display_basename(name) + "' ...",
1094            *this
1095        );
1096        progress_dialog->show_all();
1097        loader = new Loader(name); //FIXME: memory leak!
1098      loader->signal_progress().connect(      loader->signal_progress().connect(
1099          sigc::mem_fun(*this, &MainWindow::on_loader_progress));          sigc::mem_fun(*this, &MainWindow::on_loader_progress));
1100      loader->signal_finished().connect(      loader->signal_finished().connect(
1101          sigc::mem_fun(*this, &MainWindow::on_loader_finished));          sigc::mem_fun(*this, &MainWindow::on_loader_finished));
1102        loader->signal_error().connect(
1103            sigc::mem_fun(*this, &MainWindow::on_loader_error));
1104      loader->launch();      loader->launch();
1105  }  }
1106    
# Line 460  void MainWindow::load_instrument(gig::In Line 1111  void MainWindow::load_instrument(gig::In
1111          msg.run();          msg.run();
1112          Gtk::Main::quit();          Gtk::Main::quit();
1113      }      }
1114        // clear all GUI elements
1115        __clear();
1116        // load the instrument
1117      gig::File* pFile = (gig::File*) instr->GetParent();      gig::File* pFile = (gig::File*) instr->GetParent();
1118      load_gig(pFile, 0 /*file name*/);      load_gig(pFile, 0 /*file name*/, true /*shared instrument*/);
1119      //TODO: automatically select the given instrument      // automatically select the given instrument
1120        int i = 0;
1121        for (gig::Instrument* instrument = pFile->GetFirstInstrument(); instrument;
1122             instrument = pFile->GetNextInstrument(), ++i)
1123        {
1124            if (instrument == instr) {
1125                // select item in "instruments" tree view
1126                m_TreeView.get_selection()->select(Gtk::TreePath(ToString(i)));
1127                // make sure the selected item in the "instruments" tree view is
1128                // visible (scroll to it)
1129                m_TreeView.scroll_to_row(Gtk::TreePath(ToString(i)));
1130                // select item in instrument menu
1131                {
1132                    const std::vector<Gtk::Widget*> children =
1133                        instrument_menu->get_children();
1134                    static_cast<Gtk::RadioMenuItem*>(children[i])->set_active();
1135                }
1136                // update region chooser and dimension region chooser
1137                m_RegionChooser.set_instrument(instr);
1138                break;
1139            }
1140        }
1141  }  }
1142    
1143  void MainWindow::on_loader_progress()  void MainWindow::on_loader_progress()
1144  {  {
1145      load_dialog->set_fraction(loader->get_progress());      progress_dialog->set_fraction(loader->get_progress());
1146  }  }
1147    
1148  void MainWindow::on_loader_finished()  void MainWindow::on_loader_finished()
1149  {  {
1150      printf("Loader finished!\n");      printf("Loader finished!\n");
1151      printf("on_loader_finished self=%x\n", Glib::Thread::self());      printf("on_loader_finished self=%p\n",
1152      load_gig(loader->gig, loader->filename);             static_cast<void*>(Glib::Threads::Thread::self()));
1153      load_dialog->hide();      load_gig(loader->gig, loader->filename.c_str());
1154        progress_dialog->hide();
1155    }
1156    
1157    void MainWindow::on_loader_error()
1158    {
1159        Glib::ustring txt = _("Could not load file: ") + loader->error_message;
1160        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1161        msg.run();
1162        progress_dialog->hide();
1163  }  }
1164    
1165  void MainWindow::on_action_file_save()  void MainWindow::on_action_file_save()
# Line 483  void MainWindow::on_action_file_save() Line 1167  void MainWindow::on_action_file_save()
1167      file_save();      file_save();
1168  }  }
1169    
1170  bool MainWindow::file_save()  bool MainWindow::check_if_savable()
1171  {  {
1172      if (!file) return false;      if (!file) return false;
     if (!file_has_name) return file_save_as();  
1173    
1174      std::cout << "Saving file\n" << std::flush;      if (!file->GetFirstSample()) {
1175      try {          Gtk::MessageDialog(*this, _("The file could not be saved "
1176          file->Save();                                      "because it contains no samples"),
1177          if (file_is_changed) {                             false, Gtk::MESSAGE_ERROR).run();
             set_title(get_title().substr(1));  
             file_is_changed = false;  
         }  
     } catch (RIFF::Exception e) {  
         Glib::ustring txt = "Could not save file: " + e.Message;  
         Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);  
         msg.run();  
1178          return false;          return false;
1179      }      }
1180      std::cout << "Saving file done\n" << std::flush;  
1181      __import_queued_samples();      for (gig::Instrument* instrument = file->GetFirstInstrument() ; instrument ;
1182             instrument = file->GetNextInstrument()) {
1183            if (!instrument->GetFirstRegion()) {
1184                Gtk::MessageDialog(*this, _("The file could not be saved "
1185                                            "because there are instruments "
1186                                            "that have no regions"),
1187                                   false, Gtk::MESSAGE_ERROR).run();
1188                return false;
1189            }
1190        }
1191      return true;      return true;
1192  }  }
1193    
1194    bool MainWindow::file_save()
1195    {
1196        if (!check_if_savable()) return false;
1197        if (!file_is_shared && !file_has_name) return file_save_as();
1198    
1199        std::cout << "Saving file\n" << std::flush;
1200        file_structure_to_be_changed_signal.emit(this->file);
1201    
1202        progress_dialog = new ProgressDialog( //FIXME: memory leak!
1203            _("Saving") +  Glib::ustring(" '") +
1204            Glib::filename_display_basename(this->filename) + "' ...",
1205            *this
1206        );
1207        progress_dialog->show_all();
1208        saver = new Saver(this->file); //FIXME: memory leak!
1209        saver->signal_progress().connect(
1210            sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1211        saver->signal_finished().connect(
1212            sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1213        saver->signal_error().connect(
1214            sigc::mem_fun(*this, &MainWindow::on_saver_error));
1215        saver->launch();
1216    
1217        return true;
1218    }
1219    
1220    void MainWindow::on_saver_progress()
1221    {
1222        progress_dialog->set_fraction(saver->get_progress());
1223    }
1224    
1225    void MainWindow::on_saver_error()
1226    {
1227        file_structure_changed_signal.emit(this->file);
1228        Glib::ustring txt = _("Could not save file: ") + saver->error_message;
1229        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1230        msg.run();
1231    }
1232    
1233    void MainWindow::on_saver_finished()
1234    {
1235        this->file = saver->gig;
1236        this->filename = saver->filename;
1237        current_gig_dir = Glib::path_get_dirname(filename);
1238        set_title(Glib::filename_display_basename(filename));
1239        file_has_name = true;
1240        file_is_changed = false;
1241        std::cout << "Saving file done. Importing queued samples now ...\n" << std::flush;
1242        __import_queued_samples();
1243        std::cout << "Importing queued samples done.\n" << std::flush;
1244    
1245        file_structure_changed_signal.emit(this->file);
1246    
1247        __refreshEntireGUI();
1248        progress_dialog->hide();
1249    }
1250    
1251  void MainWindow::on_action_file_save_as()  void MainWindow::on_action_file_save_as()
1252  {  {
1253        if (!check_if_savable()) return;
1254      file_save_as();      file_save_as();
1255  }  }
1256    
1257  bool MainWindow::file_save_as()  bool MainWindow::file_save_as()
1258  {  {
1259      if (!file) return false;      Gtk::FileChooserDialog dialog(*this, _("Save As"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1260      Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1261      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Save"), Gtk::RESPONSE_OK);
     dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);  
1262      dialog.set_default_response(Gtk::RESPONSE_OK);      dialog.set_default_response(Gtk::RESPONSE_OK);
   
 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 8) || GTKMM_MAJOR_VERSION > 2  
1263      dialog.set_do_overwrite_confirmation();      dialog.set_do_overwrite_confirmation();
1264      // TODO: an overwrite dialog for gtkmm < 2.8  
1265  #endif  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
1266      Gtk::FileFilter filter;      Gtk::FileFilter filter;
1267      filter.add_pattern("*.gig");      filter.add_pattern("*.gig");
1268    #else
1269        Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
1270        filter->add_pattern("*.gig");
1271    #endif
1272      dialog.set_filter(filter);      dialog.set_filter(filter);
1273    
1274      if (Glib::path_is_absolute(filename)) {      // set initial dir and filename of the Save As dialog
1275          dialog.set_filename(filename);      // and prepare that initial filename as a copy of the gig
1276      } else if (current_dir != "") {      {
1277          dialog.set_current_folder(current_dir);          std::string basename = Glib::path_get_basename(filename);
1278            std::string dir = Glib::path_get_dirname(filename);
1279            basename = std::string(_("copy_of_")) + basename;
1280            Glib::ustring copyFileName = Glib::build_filename(dir, basename);
1281            if (Glib::path_is_absolute(filename)) {
1282                dialog.set_filename(copyFileName);
1283            } else {
1284                if (current_gig_dir != "") dialog.set_current_folder(current_gig_dir);
1285            }
1286            dialog.set_current_name(Glib::filename_display_basename(copyFileName));
1287      }      }
1288      dialog.set_current_name(Glib::filename_display_basename(filename));  
1289        // show warning in the dialog
1290        Gtk::HBox descriptionArea;
1291        descriptionArea.set_spacing(15);
1292        Gtk::Image warningIcon;
1293        warningIcon.set_from_icon_name("dialog-warning",
1294                                       Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
1295        descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
1296    #if GTKMM_MAJOR_VERSION < 3
1297        view::WrapLabel description;
1298    #else
1299        Gtk::Label description;
1300        description.set_line_wrap();
1301    #endif
1302        description.set_markup(
1303            _("\n<b>CAUTION:</b> You <b>MUST</b> use the "
1304              "<span style=\"italic\">\"Save\"</span> dialog instead of "
1305              "<span style=\"italic\">\"Save As...\"</span> if you want to save "
1306              "to the same .gig file. Using "
1307              "<span style=\"italic\">\"Save As...\"</span> for writing to the "
1308              "same .gig file will end up in corrupted sample wave data!\n")
1309        );
1310        descriptionArea.pack_start(description);
1311        dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
1312        descriptionArea.show_all();
1313    
1314      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1315          try {          std::string filename = dialog.get_filename();
1316              std::string filename = dialog.get_filename();          if (!Glib::str_has_suffix(filename, ".gig")) {
1317              if (!Glib::str_has_suffix(filename, ".gig")) {              filename += ".gig";
                 filename += ".gig";  
             }  
             printf("filename=%s\n", filename.c_str());  
             file->Save(filename);  
             this->filename = filename;  
             current_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) {  
             Glib::ustring txt = "Could not save file: " + e.Message;  
             Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);  
             msg.run();  
             return false;  
1318          }          }
1319          __import_queued_samples();          printf("filename=%s\n", filename.c_str());
1320    
1321            progress_dialog = new ProgressDialog( //FIXME: memory leak!
1322                _("Saving") +  Glib::ustring(" '") +
1323                Glib::filename_display_basename(filename) + "' ...",
1324                *this
1325            );
1326            progress_dialog->show_all();
1327    
1328            saver = new Saver(file, filename); //FIXME: memory leak!
1329            saver->signal_progress().connect(
1330                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1331            saver->signal_finished().connect(
1332                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1333            saver->signal_error().connect(
1334                sigc::mem_fun(*this, &MainWindow::on_saver_error));
1335            saver->launch();
1336    
1337          return true;          return true;
1338      }      }
1339      return false;      return false;
# Line 563  bool MainWindow::file_save_as() Line 1343  bool MainWindow::file_save_as()
1343  void MainWindow::__import_queued_samples() {  void MainWindow::__import_queued_samples() {
1344      std::cout << "Starting sample import\n" << std::flush;      std::cout << "Starting sample import\n" << std::flush;
1345      Glib::ustring error_files;      Glib::ustring error_files;
1346      printf("Samples to import: %d\n", m_SampleImportQueue.size());      printf("Samples to import: %d\n", int(m_SampleImportQueue.size()));
1347      for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();      for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1348           iter != m_SampleImportQueue.end(); ) {           iter != m_SampleImportQueue.end(); ) {
1349          printf("Importing sample %s\n",(*iter).sample_path.c_str());          printf("Importing sample %s\n",(*iter).sample_path.c_str());
1350          SF_INFO info;          SF_INFO info;
1351          info.format = 0;          info.format = 0;
1352          SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);          SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
1353            sf_command(hFile, SFC_SET_SCALE_FLOAT_INT_READ, 0, SF_TRUE);
1354          try {          try {
1355              if (!hFile) throw std::string("could not open file");              if (!hFile) throw std::string(_("could not open file"));
1356              // determine sample's bit depth              // determine sample's bit depth
1357              int bitdepth;              int bitdepth;
1358              switch (info.format & 0xff) {              switch (info.format & 0xff) {
1359                  case SF_FORMAT_PCM_S8:                  case SF_FORMAT_PCM_S8:
                     bitdepth = 16; // we simply convert to 16 bit for now  
                     break;  
1360                  case SF_FORMAT_PCM_16:                  case SF_FORMAT_PCM_16:
1361                    case SF_FORMAT_PCM_U8:
1362                      bitdepth = 16;                      bitdepth = 16;
1363                      break;                      break;
1364                  case SF_FORMAT_PCM_24:                  case SF_FORMAT_PCM_24:
                     bitdepth = 32; // we simply convert to 32 bit for now  
                     break;  
1365                  case SF_FORMAT_PCM_32:                  case SF_FORMAT_PCM_32:
                     bitdepth = 32;  
                     break;  
                 case SF_FORMAT_PCM_U8:  
                     bitdepth = 16; // we simply convert to 16 bit for now  
                     break;  
1366                  case SF_FORMAT_FLOAT:                  case SF_FORMAT_FLOAT:
                     bitdepth = 32;  
                     break;  
1367                  case SF_FORMAT_DOUBLE:                  case SF_FORMAT_DOUBLE:
1368                      bitdepth = 32; // I guess we will always truncate this to 32 bit                      bitdepth = 24;
1369                      break;                      break;
1370                  default:                  default:
1371                      sf_close(hFile); // close sound file                      sf_close(hFile); // close sound file
1372                      throw std::string("format not supported"); // unsupported subformat (yet?)                      throw std::string(_("format not supported")); // unsupported subformat (yet?)
1373              }              }
1374              // allocate appropriate copy buffer (TODO: for now we copy  
1375              // it in one piece, might be tough for very long samples)              const int bufsize = 10000;
             // and copy sample data into buffer  
             int8_t* buffer = NULL;  
1376              switch (bitdepth) {              switch (bitdepth) {
1377                  case 16:                  case 16: {
1378                      buffer = new int8_t[2 * info.channels * info.frames];                      short* buffer = new short[bufsize * info.channels];
1379                      // libsndfile does the conversion for us (if needed)                      sf_count_t cnt = info.frames;
1380                      sf_readf_short(hFile, (short*) buffer, info.frames);                      while (cnt) {
1381                            // libsndfile does the conversion for us (if needed)
1382                            int n = sf_readf_short(hFile, buffer, bufsize);
1383                            // write from buffer directly (physically) into .gig file
1384                            iter->gig_sample->Write(buffer, n);
1385                            cnt -= n;
1386                        }
1387                        delete[] buffer;
1388                      break;                      break;
1389                  case 32:                  }
1390                      buffer = new int8_t[4 * info.channels * info.frames];                  case 24: {
1391                      // libsndfile does the conversion for us (if needed)                      int* srcbuf = new int[bufsize * info.channels];
1392                      sf_readf_int(hFile, (int*) buffer, info.frames);                      uint8_t* dstbuf = new uint8_t[bufsize * 3 * info.channels];
1393                        sf_count_t cnt = info.frames;
1394                        while (cnt) {
1395                            // libsndfile returns 32 bits, convert to 24
1396                            int n = sf_readf_int(hFile, srcbuf, bufsize);
1397                            int j = 0;
1398                            for (int i = 0 ; i < n * info.channels ; i++) {
1399                                dstbuf[j++] = srcbuf[i] >> 8;
1400                                dstbuf[j++] = srcbuf[i] >> 16;
1401                                dstbuf[j++] = srcbuf[i] >> 24;
1402                            }
1403                            // write from buffer directly (physically) into .gig file
1404                            iter->gig_sample->Write(dstbuf, n);
1405                            cnt -= n;
1406                        }
1407                        delete[] srcbuf;
1408                        delete[] dstbuf;
1409                      break;                      break;
1410                    }
1411              }              }
             // write from buffer directly (physically) into .gig file  
             (*iter).gig_sample->Write(buffer, info.frames);  
1412              // cleanup              // cleanup
1413              sf_close(hFile);              sf_close(hFile);
1414              delete[] buffer;              // let the sampler re-cache the sample if needed
1415                sample_changed_signal.emit(iter->gig_sample);
1416              // on success we remove the sample from the import queue,              // on success we remove the sample from the import queue,
1417              // otherwise keep it, maybe it works the next time ?              // otherwise keep it, maybe it works the next time ?
1418              std::list<SampleImportItem>::iterator cur = iter;              std::list<SampleImportItem>::iterator cur = iter;
# Line 628  void MainWindow::__import_queued_samples Line 1420  void MainWindow::__import_queued_samples
1420              m_SampleImportQueue.erase(cur);              m_SampleImportQueue.erase(cur);
1421          } catch (std::string what) {          } catch (std::string what) {
1422              // remember the files that made trouble (and their cause)              // remember the files that made trouble (and their cause)
1423              if (error_files.size()) error_files += "\n";              if (!error_files.empty()) error_files += "\n";
1424              error_files += (*iter).sample_path += " (" + what + ")";              error_files += (*iter).sample_path += " (" + what + ")";
1425              ++iter;              ++iter;
1426          }          }
1427      }      }
1428      // show error message box when some sample(s) could not be imported      // show error message box when some sample(s) could not be imported
1429      if (error_files.size()) {      if (!error_files.empty()) {
1430          Glib::ustring txt = "Could not import the following sample(s):\n" + error_files;          Glib::ustring txt = _("Could not import the following sample(s):\n") + error_files;
1431          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1432          msg.run();          msg.run();
1433      }      }
# Line 647  void MainWindow::on_action_file_properti Line 1439  void MainWindow::on_action_file_properti
1439      propDialog.deiconify();      propDialog.deiconify();
1440  }  }
1441    
1442    void MainWindow::on_action_warn_user_on_extensions() {
1443        Settings::singleton()->warnUserOnExtensions =
1444            !Settings::singleton()->warnUserOnExtensions;
1445    }
1446    
1447    void MainWindow::on_action_sync_sampler_instrument_selection() {
1448        Settings::singleton()->syncSamplerInstrumentSelection =
1449            !Settings::singleton()->syncSamplerInstrumentSelection;
1450    }
1451    
1452    void MainWindow::on_action_move_root_note_with_region_moved() {
1453        Settings::singleton()->moveRootNoteWithRegionMoved =
1454            !Settings::singleton()->moveRootNoteWithRegionMoved;
1455    }
1456    
1457  void MainWindow::on_action_help_about()  void MainWindow::on_action_help_about()
1458  {  {
 #ifdef ABOUT_DIALOG  
1459      Gtk::AboutDialog dialog;      Gtk::AboutDialog dialog;
1460    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 12) || GTKMM_MAJOR_VERSION > 2
1461        dialog.set_program_name("Gigedit");
1462    #else
1463        dialog.set_name("Gigedit");
1464    #endif
1465      dialog.set_version(VERSION);      dialog.set_version(VERSION);
1466        dialog.set_copyright("Copyright (C) 2006-2015 Andreas Persson");
1467        const std::string sComment =
1468            _("Built " __DATE__ "\nUsing ") +
1469            ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +
1470            _(
1471                "Gigedit is released under the GNU General Public License.\n"
1472                "\n"
1473                "This program is distributed WITHOUT ANY WARRANTY; So better "
1474                "backup your Gigasampler/GigaStudio files before editing them with "
1475                "this application.\n"
1476                "\n"
1477                "Please report bugs to: http://bugs.linuxsampler.org"
1478            );
1479        dialog.set_comments(sComment.c_str());
1480        dialog.set_website("http://www.linuxsampler.org");
1481        dialog.set_website_label("http://www.linuxsampler.org");
1482      dialog.run();      dialog.run();
 #endif  
1483  }  }
1484    
1485  PropDialog::PropDialog()  PropDialog::PropDialog()
1486      : table(2,1)      : eFileFormat(_("File Format")),
1487  {        eName(_("Name")),
1488          eCreationDate(_("Creation date")),
1489          eComments(_("Comments")),
1490          eProduct(_("Product")),
1491          eCopyright(_("Copyright")),
1492          eArtists(_("Artists")),
1493          eGenre(_("Genre")),
1494          eKeywords(_("Keywords")),
1495          eEngineer(_("Engineer")),
1496          eTechnician(_("Technician")),
1497          eSoftware(_("Software")),
1498          eMedium(_("Medium")),
1499          eSource(_("Source")),
1500          eSourceForm(_("Source form")),
1501          eCommissioned(_("Commissioned")),
1502          eSubject(_("Subject")),
1503          quitButton(_("_Close"), true),
1504          table(2, 1),
1505          m_file(NULL)
1506    {
1507        set_title(_("File Properties"));
1508        eName.set_width_chars(50);
1509    
1510        connect(eName, &DLS::Info::Name);
1511        connect(eCreationDate, &DLS::Info::CreationDate);
1512        connect(eComments, &DLS::Info::Comments);
1513        connect(eProduct, &DLS::Info::Product);
1514        connect(eCopyright, &DLS::Info::Copyright);
1515        connect(eArtists, &DLS::Info::Artists);
1516        connect(eGenre, &DLS::Info::Genre);
1517        connect(eKeywords, &DLS::Info::Keywords);
1518        connect(eEngineer, &DLS::Info::Engineer);
1519        connect(eTechnician, &DLS::Info::Technician);
1520        connect(eSoftware, &DLS::Info::Software);
1521        connect(eMedium, &DLS::Info::Medium);
1522        connect(eSource, &DLS::Info::Source);
1523        connect(eSourceForm, &DLS::Info::SourceForm);
1524        connect(eCommissioned, &DLS::Info::Commissioned);
1525        connect(eSubject, &DLS::Info::Subject);
1526    
1527        table.add(eFileFormat);
1528        table.add(eName);
1529        table.add(eCreationDate);
1530        table.add(eComments);
1531        table.add(eProduct);
1532        table.add(eCopyright);
1533        table.add(eArtists);
1534        table.add(eGenre);
1535        table.add(eKeywords);
1536        table.add(eEngineer);
1537        table.add(eTechnician);
1538        table.add(eSoftware);
1539        table.add(eMedium);
1540        table.add(eSource);
1541        table.add(eSourceForm);
1542        table.add(eCommissioned);
1543        table.add(eSubject);
1544    
1545      table.set_col_spacings(5);      table.set_col_spacings(5);
1546      const char* propLabels[] = {      add(vbox);
1547          "Name:",      table.set_border_width(5);
1548          "CreationDate:",      vbox.add(table);
1549          "Comments:", // TODO: multiline      vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
1550          "Product:",      buttonBox.set_layout(Gtk::BUTTONBOX_END);
1551          "Copyright:",      buttonBox.set_border_width(5);
1552          "Artists:",      buttonBox.show();
1553          "Genre:",      buttonBox.pack_start(quitButton);
1554          "Keywords:",      quitButton.set_can_default();
1555          "Engineer:",      quitButton.grab_focus();
1556          "Technician:",      quitButton.signal_clicked().connect(
1557          "Software:", // TODO: readonly          sigc::mem_fun(*this, &PropDialog::hide));
1558          "Medium:",      eFileFormat.signal_value_changed().connect(
1559          "Source:",          sigc::mem_fun(*this, &PropDialog::onFileFormatChanged));
         "SourceForm:",  
         "Commissioned:",  
         "Subject:"  
     };  
     for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {  
         label[i].set_text(propLabels[i]);  
         label[i].set_alignment(Gtk::ALIGN_LEFT);  
         table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);  
         table.attach(entry[i], 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND,  
                      Gtk::SHRINK);  
     }  
1560    
1561      add(table);      quitButton.show();
1562      // add_button(Gtk::Stock::CANCEL, 0);      vbox.show();
     // add_button(Gtk::Stock::OK, 1);  
1563      show_all_children();      show_all_children();
1564  }  }
1565    
1566    void PropDialog::set_file(gig::File* file)
1567    {
1568        m_file = file;
1569    
1570        // update file format version combo box
1571        const std::string sGiga = "Gigasampler/GigaStudio v";
1572        const int major = file->pVersion->major;
1573        std::vector<std::string> txts;
1574        std::vector<int> values;
1575        txts.push_back(sGiga + "2"); values.push_back(2);
1576        txts.push_back(sGiga + "3/v4"); values.push_back(3);
1577        if (major != 2 && major != 3) {
1578            txts.push_back(sGiga + ToString(major)); values.push_back(major);
1579        }
1580        std::vector<const char*> texts;
1581        for (int i = 0; i < txts.size(); ++i) texts.push_back(txts[i].c_str());
1582        texts.push_back(NULL); values.push_back(0);
1583        eFileFormat.set_choices(&texts[0], &values[0]);
1584        eFileFormat.set_value(major);
1585    }
1586    
1587    void PropDialog::onFileFormatChanged() {
1588        const int major = eFileFormat.get_value();
1589        if (m_file) m_file->pVersion->major = major;
1590    }
1591    
1592  void PropDialog::set_info(DLS::Info* info)  void PropDialog::set_info(DLS::Info* info)
1593  {  {
1594      entry[0].set_text(info->Name);      update(info);
1595      entry[1].set_text(info->CreationDate);  }
1596      entry[2].set_text(Glib::convert(info->Comments, "UTF-8", "ISO-8859-1"));  
1597      entry[3].set_text(info->Product);  
1598      entry[4].set_text(info->Copyright);  void InstrumentProps::set_Name(const gig::String& name)
1599      entry[5].set_text(info->Artists);  {
1600      entry[6].set_text(info->Genre);      m->pInfo->Name = name;
1601      entry[7].set_text(info->Keywords);  }
1602      entry[8].set_text(info->Engineer);  
1603      entry[9].set_text(info->Technician);  void InstrumentProps::update_name()
1604      entry[10].set_text(info->Software);  {
1605      entry[11].set_text(info->Medium);      update_model++;
1606      entry[12].set_text(info->Source);      eName.set_value(m->pInfo->Name);
1607      entry[13].set_text(info->SourceForm);      update_model--;
1608      entry[14].set_text(info->Commissioned);  }
1609      entry[15].set_text(info->Subject);  
1610  }  void InstrumentProps::set_IsDrum(bool value)
   
 void InstrumentProps::add_prop(BoolEntry& boolentry)  
 {  
     table.attach(boolentry.widget, 0, 2, rowno, rowno + 1,  
                  Gtk::FILL, Gtk::SHRINK);  
     rowno++;  
     boolentry.signal_changed_by_user().connect(instrument_changed.make_slot());  
 }  
   
 void InstrumentProps::add_prop(BoolEntryPlus6& boolentry)  
 {  
     table.attach(boolentry.widget, 0, 2, rowno, rowno + 1,  
                  Gtk::FILL, Gtk::SHRINK);  
     rowno++;  
     boolentry.signal_changed_by_user().connect(instrument_changed.make_slot());  
 }  
   
 void InstrumentProps::add_prop(LabelWidget& prop)  
 {  
     table.attach(prop.label, 0, 1, rowno, rowno + 1,  
                  Gtk::FILL, Gtk::SHRINK);  
     table.attach(prop.widget, 1, 2, rowno, rowno + 1,  
                  Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);  
     rowno++;  
     prop.signal_changed_by_user().connect(instrument_changed.make_slot());  
 }  
   
 InstrumentProps::InstrumentProps()  
     : table(2,1),  
       quitButton(Gtk::Stock::CLOSE),  
       eName("Name"),  
       eIsDrum("Is drum"),  
       eMIDIBank("MIDI bank", 0, 16383),  
       eMIDIProgram("MIDI program"),  
       eAttenuation("Attenuation", 0, 96, 0, 1),  
       eGainPlus6("Gain +6dB", eAttenuation, -6),  
       eEffectSend("Effect send", 0, 65535),  
       eFineTune("Fine tune", -8400, 8400),  
       ePitchbendRange("Pitchbend range", 0, 12),  
       ePianoReleaseMode("Piano release mode"),  
       eDimensionKeyRangeLow("Dimension key range low"),  
       eDimensionKeyRangeHigh("Dimension key range high")  
1611  {  {
1612      set_title("Instrument properties");      m->IsDrum = value;
1613    }
1614    
1615    void InstrumentProps::set_MIDIBank(uint16_t value)
1616    {
1617        m->MIDIBank = value;
1618    }
1619    
1620    void InstrumentProps::set_MIDIProgram(uint32_t value)
1621    {
1622        m->MIDIProgram = value;
1623    }
1624    
1625    InstrumentProps::InstrumentProps() :
1626        quitButton(_("_Close"), true),
1627        table(2,1),
1628        eName(_("Name")),
1629        eIsDrum(_("Is drum")),
1630        eMIDIBank(_("MIDI bank"), 0, 16383),
1631        eMIDIProgram(_("MIDI program")),
1632        eAttenuation(_("Attenuation"), 0, 96, 0, 1),
1633        eGainPlus6(_("Gain +6dB"), eAttenuation, -6),
1634        eEffectSend(_("Effect send"), 0, 65535),
1635        eFineTune(_("Fine tune"), -8400, 8400),
1636        ePitchbendRange(_("Pitchbend range"), 0, 12),
1637        ePianoReleaseMode(_("Piano release mode")),
1638        eDimensionKeyRangeLow(_("Keyswitching range low")),
1639        eDimensionKeyRangeHigh(_("Keyswitching range high"))
1640    {
1641        set_title(_("Instrument Properties"));
1642    
1643        eDimensionKeyRangeLow.set_tip(
1644            _("start of the keyboard area which should switch the "
1645              "\"keyswitching\" dimension")
1646        );
1647        eDimensionKeyRangeHigh.set_tip(
1648            _("end of the keyboard area which should switch the "
1649              "\"keyswitching\" dimension")
1650        );
1651    
1652        connect(eName, &InstrumentProps::set_Name);
1653        connect(eIsDrum, &InstrumentProps::set_IsDrum);
1654        connect(eMIDIBank, &InstrumentProps::set_MIDIBank);
1655        connect(eMIDIProgram, &InstrumentProps::set_MIDIProgram);
1656        connect(eAttenuation, &gig::Instrument::Attenuation);
1657        connect(eGainPlus6, &gig::Instrument::Attenuation);
1658        connect(eEffectSend, &gig::Instrument::EffectSend);
1659        connect(eFineTune, &gig::Instrument::FineTune);
1660        connect(ePitchbendRange, &gig::Instrument::PitchbendRange);
1661        connect(ePianoReleaseMode, &gig::Instrument::PianoReleaseMode);
1662        connect(eDimensionKeyRangeLow, eDimensionKeyRangeHigh,
1663                &gig::Instrument::DimensionKeyRange);
1664    
1665        eName.signal_value_changed().connect(sig_name_changed.make_slot());
1666    
     rowno = 0;  
1667      table.set_col_spacings(5);      table.set_col_spacings(5);
1668    
1669      add_prop(eName);      table.add(eName);
1670      add_prop(eIsDrum);      table.add(eIsDrum);
1671      add_prop(eMIDIBank);      table.add(eMIDIBank);
1672      add_prop(eMIDIProgram);      table.add(eMIDIProgram);
1673      add_prop(eAttenuation);      table.add(eAttenuation);
1674      add_prop(eGainPlus6);      table.add(eGainPlus6);
1675      add_prop(eEffectSend);      table.add(eEffectSend);
1676      add_prop(eFineTune);      table.add(eFineTune);
1677      add_prop(ePitchbendRange);      table.add(ePitchbendRange);
1678      add_prop(ePianoReleaseMode);      table.add(ePianoReleaseMode);
1679      add_prop(eDimensionKeyRangeLow);      table.add(eDimensionKeyRangeLow);
1680      add_prop(eDimensionKeyRangeHigh);      table.add(eDimensionKeyRangeHigh);
   
     eDimensionKeyRangeLow.signal_changed_by_user().connect(  
         sigc::mem_fun(*this, &InstrumentProps::key_range_low_changed));  
     eDimensionKeyRangeHigh.signal_changed_by_user().connect(  
         sigc::mem_fun(*this, &InstrumentProps::key_range_high_changed));  
1681    
1682      add(vbox);      add(vbox);
1683      table.set_border_width(5);      table.set_border_width(5);
# Line 786  InstrumentProps::InstrumentProps() Line 1688  InstrumentProps::InstrumentProps()
1688      buttonBox.set_border_width(5);      buttonBox.set_border_width(5);
1689      buttonBox.show();      buttonBox.show();
1690      buttonBox.pack_start(quitButton);      buttonBox.pack_start(quitButton);
1691      quitButton.set_flags(Gtk::CAN_DEFAULT);      quitButton.set_can_default();
1692      quitButton.grab_focus();      quitButton.grab_focus();
1693    
1694      quitButton.signal_clicked().connect(      quitButton.signal_clicked().connect(
# Line 799  InstrumentProps::InstrumentProps() Line 1701  InstrumentProps::InstrumentProps()
1701    
1702  void InstrumentProps::set_instrument(gig::Instrument* instrument)  void InstrumentProps::set_instrument(gig::Instrument* instrument)
1703  {  {
1704      eName.set_ptr(&instrument->pInfo->Name);      update(instrument);
     eIsDrum.set_ptr(&instrument->IsDrum);  
     eMIDIBank.set_ptr(&instrument->MIDIBank);  
     eMIDIProgram.set_ptr(&instrument->MIDIProgram);  
     eAttenuation.set_ptr(&instrument->Attenuation);  
     eGainPlus6.set_ptr(&instrument->Attenuation);  
     eEffectSend.set_ptr(&instrument->EffectSend);  
     eFineTune.set_ptr(&instrument->FineTune);  
     ePitchbendRange.set_ptr(&instrument->PitchbendRange);  
     ePianoReleaseMode.set_ptr(&instrument->PianoReleaseMode);  
     eDimensionKeyRangeLow.set_ptr(0);  
     eDimensionKeyRangeHigh.set_ptr(0);  
     eDimensionKeyRangeLow.set_ptr(&instrument->DimensionKeyRange.low);  
     eDimensionKeyRangeHigh.set_ptr(&instrument->DimensionKeyRange.high);  
 }  
   
 void InstrumentProps::key_range_low_changed()  
 {  
     double l = eDimensionKeyRangeLow.get_value();  
     double h = eDimensionKeyRangeHigh.get_value();  
     if (h < l) eDimensionKeyRangeHigh.set_value(l);  
 }  
1705    
1706  void InstrumentProps::key_range_high_changed()      update_model++;
1707  {      eName.set_value(instrument->pInfo->Name);
1708      double l = eDimensionKeyRangeLow.get_value();      eIsDrum.set_value(instrument->IsDrum);
1709      double h = eDimensionKeyRangeHigh.get_value();      eMIDIBank.set_value(instrument->MIDIBank);
1710      if (h < l) eDimensionKeyRangeLow.set_value(h);      eMIDIProgram.set_value(instrument->MIDIProgram);
1711        update_model--;
1712  }  }
1713    
 sigc::signal<void> InstrumentProps::signal_instrument_changed()  
 {  
     return instrument_changed;  
 }  
1714    
1715  void MainWindow::file_changed()  void MainWindow::file_changed()
1716  {  {
# Line 842  void MainWindow::file_changed() Line 1720  void MainWindow::file_changed()
1720      }      }
1721  }  }
1722    
1723  void MainWindow::load_gig(gig::File* gig, const char* filename)  void MainWindow::updateSampleRefCountMap(gig::File* gig) {
1724        sample_ref_count.clear();
1725        
1726        if (!gig) return;
1727    
1728        for (gig::Instrument* instrument = gig->GetFirstInstrument(); instrument;
1729             instrument = gig->GetNextInstrument())
1730        {
1731            for (gig::Region* rgn = instrument->GetFirstRegion(); rgn;
1732                 rgn = instrument->GetNextRegion())
1733            {
1734                for (int i = 0; i < 256; ++i) {
1735                    if (!rgn->pDimensionRegions[i]) continue;
1736                    if (rgn->pDimensionRegions[i]->pSample) {
1737                        sample_ref_count[rgn->pDimensionRegions[i]->pSample]++;
1738                    }
1739                }
1740            }
1741        }
1742    }
1743    
1744    void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument)
1745  {  {
1746      file = 0;      file = 0;
1747        set_file_is_shared(isSharedInstrument);
1748    
1749      this->filename = filename ? filename : _("Unsaved Gig File");      this->filename =
1750            (filename && strlen(filename) > 0) ?
1751                filename : (!gig->GetFileName().empty()) ?
1752                    gig->GetFileName() : _("Unsaved Gig File");
1753      set_title(Glib::filename_display_basename(this->filename));      set_title(Glib::filename_display_basename(this->filename));
1754      file_has_name = filename;      file_has_name = filename;
1755      file_is_changed = false;      file_is_changed = false;
1756    
1757        propDialog.set_file(gig);
1758      propDialog.set_info(gig->pInfo);      propDialog.set_info(gig->pInfo);
1759    
1760      Gtk::MenuItem* instrument_menu =      instrument_name_connection.block();
         dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));  
   
     int instrument_index = 0;  
     Gtk::RadioMenuItem::Group instrument_group;  
1761      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
1762           instrument = gig->GetNextInstrument()) {           instrument = gig->GetNextInstrument()) {
1763            Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
1764    
1765          Gtk::TreeModel::iterator iter = m_refTreeModel->append();          Gtk::TreeModel::iterator iter = m_refTreeModel->append();
1766          Gtk::TreeModel::Row row = *iter;          Gtk::TreeModel::Row row = *iter;
1767          row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();          row[m_Columns.m_col_name] = name;
1768          row[m_Columns.m_col_instr] = instrument;          row[m_Columns.m_col_instr] = instrument;
1769          // create a menu item for this instrument  
1770          Gtk::RadioMenuItem* item =          add_instrument_to_menu(name);
             new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());  
         instrument_menu->get_submenu()->append(*item);  
         item->signal_activate().connect(  
             sigc::bind(  
                 sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),  
                 instrument_index  
             )  
         );  
         instrument_index++;  
1771      }      }
1772      instrument_menu->show();      instrument_name_connection.unblock();
1773      instrument_menu->get_submenu()->show_all_children();      uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments")->show();
1774    
1775        updateSampleRefCountMap(gig);
1776    
1777      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1778          if (group->Name != "") {          if (group->Name != "") {
1779              Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();              Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1780              Gtk::TreeModel::Row rowGroup = *iterGroup;              Gtk::TreeModel::Row rowGroup = *iterGroup;
1781              rowGroup[m_SamplesModel.m_col_name]   = group->Name.c_str();              rowGroup[m_SamplesModel.m_col_name]   = gig_to_utf8(group->Name);
1782              rowGroup[m_SamplesModel.m_col_group]  = group;              rowGroup[m_SamplesModel.m_col_group]  = group;
1783              rowGroup[m_SamplesModel.m_col_sample] = NULL;              rowGroup[m_SamplesModel.m_col_sample] = NULL;
1784              for (gig::Sample* sample = group->GetFirstSample();              for (gig::Sample* sample = group->GetFirstSample();
# Line 891  void MainWindow::load_gig(gig::File* gig Line 1786  void MainWindow::load_gig(gig::File* gig
1786                  Gtk::TreeModel::iterator iterSample =                  Gtk::TreeModel::iterator iterSample =
1787                      m_refSamplesTreeModel->append(rowGroup.children());                      m_refSamplesTreeModel->append(rowGroup.children());
1788                  Gtk::TreeModel::Row rowSample = *iterSample;                  Gtk::TreeModel::Row rowSample = *iterSample;
1789                  rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();                  rowSample[m_SamplesModel.m_col_name] =
1790                        gig_to_utf8(sample->pInfo->Name);
1791                  rowSample[m_SamplesModel.m_col_sample] = sample;                  rowSample[m_SamplesModel.m_col_sample] = sample;
1792                  rowSample[m_SamplesModel.m_col_group]  = NULL;                  rowSample[m_SamplesModel.m_col_group]  = NULL;
1793                    int refcount = sample_ref_count.count(sample) ? sample_ref_count[sample] : 0;
1794                    rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
1795                    rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
1796              }              }
1797          }          }
1798      }      }
1799        
1800        for (int i = 0; gig->GetScriptGroup(i); ++i) {
1801            gig::ScriptGroup* group = gig->GetScriptGroup(i);
1802    
1803            Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
1804            Gtk::TreeModel::Row rowGroup = *iterGroup;
1805            rowGroup[m_ScriptsModel.m_col_name]   = gig_to_utf8(group->Name);
1806            rowGroup[m_ScriptsModel.m_col_group]  = group;
1807            rowGroup[m_ScriptsModel.m_col_script] = NULL;
1808            for (int s = 0; group->GetScript(s); ++s) {
1809                gig::Script* script = group->GetScript(s);
1810    
1811                Gtk::TreeModel::iterator iterScript =
1812                    m_refScriptsTreeModel->append(rowGroup.children());
1813                Gtk::TreeModel::Row rowScript = *iterScript;
1814                rowScript[m_ScriptsModel.m_col_name] = gig_to_utf8(script->Name);
1815                rowScript[m_ScriptsModel.m_col_script] = script;
1816                rowScript[m_ScriptsModel.m_col_group]  = NULL;
1817            }
1818        }
1819        // unfold all sample groups & script groups by default
1820        m_TreeViewSamples.expand_all();
1821        m_TreeViewScripts.expand_all();
1822    
1823      file = gig;      file = gig;
1824    
1825      // select the first instrument      // select the first instrument
1826      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      m_TreeView.get_selection()->select(Gtk::TreePath("0"));
1827      tree_sel_ref->select(Gtk::TreePath("0"));  
1828        instr_props_set_instrument();
1829        gig::Instrument* instrument = get_instrument();
1830        if (instrument) {
1831            midiRules.set_instrument(instrument);
1832        }
1833    }
1834    
1835    bool MainWindow::instr_props_set_instrument()
1836    {
1837        instrumentProps.signal_name_changed().clear();
1838    
1839        Gtk::TreeModel::const_iterator it =
1840            m_TreeView.get_selection()->get_selected();
1841        if (it) {
1842            Gtk::TreeModel::Row row = *it;
1843            gig::Instrument* instrument = row[m_Columns.m_col_instr];
1844    
1845            instrumentProps.set_instrument(instrument);
1846    
1847            // make sure instrument tree is updated when user changes the
1848            // instrument name in instrument properties window
1849            instrumentProps.signal_name_changed().connect(
1850                sigc::bind(
1851                    sigc::mem_fun(*this,
1852                                  &MainWindow::instr_name_changed_by_instr_props),
1853                    it));
1854        } else {
1855            instrumentProps.hide();
1856        }
1857        return it;
1858  }  }
1859    
1860  void MainWindow::show_instr_props()  void MainWindow::show_instr_props()
1861  {  {
1862      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      if (instr_props_set_instrument()) {
1863      Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();          instrumentProps.show();
1864      if (it)          instrumentProps.deiconify();
1865        }
1866    }
1867    
1868    void MainWindow::instr_name_changed_by_instr_props(Gtk::TreeModel::iterator& it)
1869    {
1870        Gtk::TreeModel::Row row = *it;
1871        Glib::ustring name = row[m_Columns.m_col_name];
1872    
1873        gig::Instrument* instrument = row[m_Columns.m_col_instr];
1874        Glib::ustring gigname(gig_to_utf8(instrument->pInfo->Name));
1875        if (gigname != name) {
1876            row[m_Columns.m_col_name] = gigname;
1877        }
1878    }
1879    
1880    void MainWindow::show_midi_rules()
1881    {
1882        if (gig::Instrument* instrument = get_instrument())
1883      {      {
1884          Gtk::TreeModel::Row row = *it;          midiRules.set_instrument(instrument);
1885          if (row[m_Columns.m_col_instr])          midiRules.show();
1886          {          midiRules.deiconify();
1887              instrumentProps.set_instrument(row[m_Columns.m_col_instr]);      }
1888              instrumentProps.show();  }
1889              instrumentProps.deiconify();  
1890          }  void MainWindow::show_script_slots() {
1891        if (!file) return;
1892        // get selected instrument
1893        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1894        Gtk::TreeModel::iterator it = sel->get_selected();
1895        if (!it) return;
1896        Gtk::TreeModel::Row row = *it;
1897        gig::Instrument* instrument = row[m_Columns.m_col_instr];
1898        if (!instrument) return;
1899    
1900        ScriptSlots* window = new ScriptSlots;
1901        window->setInstrument(instrument);
1902        //window->reparent(*this);
1903        window->show();
1904    }
1905    
1906    void MainWindow::on_action_refresh_all() {
1907        __refreshEntireGUI();
1908    }
1909    
1910    void MainWindow::on_action_view_status_bar() {
1911        Gtk::CheckMenuItem* item =
1912            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/Statusbar"));
1913        if (!item) {
1914            std::cerr << "/MenuBar/MenuView/Statusbar == NULL\n";
1915            return;
1916        }
1917        if (item->get_active()) m_StatusBar.show();
1918        else                    m_StatusBar.hide();
1919    }
1920    
1921    bool MainWindow::is_copy_samples_unity_note_enabled() const {
1922        Gtk::CheckMenuItem* item =
1923            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity"));
1924        if (!item) {
1925            std::cerr << "/MenuBar/MenuEdit/CopySampleUnity == NULL\n";
1926            return true;
1927      }      }
1928        return item->get_active();
1929    }
1930    
1931    bool MainWindow::is_copy_samples_fine_tune_enabled() const {
1932        Gtk::CheckMenuItem* item =
1933            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleTune"));
1934        if (!item) {
1935            std::cerr << "/MenuBar/MenuEdit/CopySampleTune == NULL\n";
1936            return true;
1937        }
1938        return item->get_active();
1939    }
1940    
1941    bool MainWindow::is_copy_samples_loop_enabled() const {
1942        Gtk::CheckMenuItem* item =
1943            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleLoop"));
1944        if (!item) {
1945            std::cerr << "/MenuBar/MenuEdit/CopySampleLoop == NULL\n";
1946            return true;
1947        }
1948        return item->get_active();
1949  }  }
1950    
1951  void MainWindow::on_button_release(GdkEventButton* button)  void MainWindow::on_button_release(GdkEventButton* button)
# Line 926  void MainWindow::on_button_release(GdkEv Line 1953  void MainWindow::on_button_release(GdkEv
1953      if (button->type == GDK_2BUTTON_PRESS) {      if (button->type == GDK_2BUTTON_PRESS) {
1954          show_instr_props();          show_instr_props();
1955      } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {      } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1956            // gig v2 files have no midi rules
1957            const bool bEnabled = !(file->pVersion && file->pVersion->major == 2);
1958            static_cast<Gtk::MenuItem*>(
1959                uiManager->get_widget("/MenuBar/MenuInstrument/MidiRules"))->set_sensitive(
1960                    bEnabled
1961                );
1962            static_cast<Gtk::MenuItem*>(
1963                uiManager->get_widget("/PopupMenu/MidiRules"))->set_sensitive(
1964                    bEnabled
1965                );
1966          popup_menu->popup(button->button, button->time);          popup_menu->popup(button->button, button->time);
1967      }      }
1968  }  }
1969    
1970  void MainWindow::on_instrument_selection_change(int index) {  void MainWindow::on_instrument_selection_change(Gtk::RadioMenuItem* item) {
1971      m_RegionChooser.set_instrument(file->GetInstrument(index));      if (item->get_active()) {
1972            const std::vector<Gtk::Widget*> children =
1973                instrument_menu->get_children();
1974            std::vector<Gtk::Widget*>::const_iterator it =
1975                find(children.begin(), children.end(), item);
1976            if (it != children.end()) {
1977                int index = it - children.begin();
1978                m_TreeView.get_selection()->select(Gtk::TreePath(ToString(index)));
1979    
1980                m_RegionChooser.set_instrument(file->GetInstrument(index));
1981            }
1982        }
1983    }
1984    
1985    void MainWindow::select_instrument(gig::Instrument* instrument) {
1986        if (!instrument) return;
1987    
1988        Glib::RefPtr<Gtk::TreeModel> model = m_TreeView.get_model();
1989        for (int i = 0; i < model->children().size(); ++i) {
1990            Gtk::TreeModel::Row row = model->children()[i];
1991            if (row[m_Columns.m_col_instr] == instrument) {
1992                // select and show the respective instrument in the list view
1993                show_intruments_tab();
1994                m_TreeView.get_selection()->select(model->children()[i]);
1995                Gtk::TreePath path(
1996                    m_TreeView.get_selection()->get_selected()
1997                );
1998                m_TreeView.scroll_to_row(path);
1999                on_sel_change(); // the regular instrument selection change callback
2000            }
2001        }
2002    }
2003    
2004    /// Returns true if requested dimension region was successfully selected and scrolled to in the list view, false on error.
2005    bool MainWindow::select_dimension_region(gig::DimensionRegion* dimRgn) {
2006        gig::Region* pRegion = (gig::Region*) dimRgn->GetParent();
2007        gig::Instrument* pInstrument = (gig::Instrument*) pRegion->GetParent();
2008    
2009        Glib::RefPtr<Gtk::TreeModel> model = m_TreeView.get_model();
2010        for (int i = 0; i < model->children().size(); ++i) {
2011            Gtk::TreeModel::Row row = model->children()[i];
2012            if (row[m_Columns.m_col_instr] == pInstrument) {
2013                // select and show the respective instrument in the list view
2014                show_intruments_tab();
2015                m_TreeView.get_selection()->select(model->children()[i]);
2016                Gtk::TreePath path(
2017                    m_TreeView.get_selection()->get_selected()
2018                );
2019                m_TreeView.scroll_to_row(path);
2020                on_sel_change(); // the regular instrument selection change callback
2021    
2022                // select respective region in the region selector
2023                m_RegionChooser.set_region(pRegion);
2024    
2025                // select and show the respective dimension region in the editor
2026                //update_dimregs();
2027                if (!m_DimRegionChooser.select_dimregion(dimRgn)) return false;
2028                //dimreg_edit.set_dim_region(dimRgn);
2029    
2030                return true;
2031            }
2032        }
2033    
2034        return false;
2035    }
2036    
2037    void MainWindow::select_sample(gig::Sample* sample) {
2038        Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
2039        for (int g = 0; g < model->children().size(); ++g) {
2040            Gtk::TreeModel::Row rowGroup = model->children()[g];
2041            for (int s = 0; s < rowGroup.children().size(); ++s) {
2042                Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
2043                if (rowSample[m_SamplesModel.m_col_sample] == sample) {
2044                    show_samples_tab();
2045                    m_TreeViewSamples.get_selection()->select(rowGroup.children()[s]);
2046                    Gtk::TreePath path(
2047                        m_TreeViewSamples.get_selection()->get_selected()
2048                    );
2049                    m_TreeViewSamples.scroll_to_row(path);
2050                    return;
2051                }
2052            }
2053        }
2054  }  }
2055    
2056  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
# Line 948  void MainWindow::on_sample_treeview_butt Line 2067  void MainWindow::on_sample_treeview_butt
2067              group_selected  = row[m_SamplesModel.m_col_group];              group_selected  = row[m_SamplesModel.m_col_group];
2068              sample_selected = row[m_SamplesModel.m_col_sample];              sample_selected = row[m_SamplesModel.m_col_sample];
2069          }          }
2070            
2071                
2072          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
2073              set_sensitive(group_selected || sample_selected);              set_sensitive(group_selected || sample_selected);
2074          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
2075              set_sensitive(group_selected || sample_selected);              set_sensitive(group_selected || sample_selected);
2076          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
2077              set_sensitive(file);              set_sensitive(file);
2078            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/ShowSampleRefs"))->
2079                set_sensitive(sample_selected);
2080          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
2081              set_sensitive(group_selected || sample_selected);              set_sensitive(group_selected || sample_selected);
2082          // show sample popup          // show sample popup
2083          sample_popup->popup(button->button, button->time);          sample_popup->popup(button->button, button->time);
2084    
2085            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/SampleProperties"))->
2086                set_sensitive(group_selected || sample_selected);
2087            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddSample"))->
2088                set_sensitive(group_selected || sample_selected);
2089            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddGroup"))->
2090                set_sensitive(file);
2091            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/ShowSampleRefs"))->
2092                set_sensitive(sample_selected);
2093            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/RemoveSample"))->
2094                set_sensitive(group_selected || sample_selected);
2095      }      }
2096  }  }
2097    
2098  void MainWindow::on_action_add_instrument() {  void MainWindow::on_script_treeview_button_release(GdkEventButton* button) {
2099      static int __instrument_indexer = 0;      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
2100      if (!file) return;          Gtk::Menu* script_popup =
2101      gig::Instrument* instrument = file->AddInstrument();              dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/ScriptPopupMenu"));
2102      __instrument_indexer++;          // update enabled/disabled state of sample popup items
2103      instrument->pInfo->Name =          Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2104          "Unnamed Instrument " + ToString(__instrument_indexer);          Gtk::TreeModel::iterator it = sel->get_selected();
2105            bool group_selected  = false;
2106            bool script_selected = false;
2107            if (it) {
2108                Gtk::TreeModel::Row row = *it;
2109                group_selected  = row[m_ScriptsModel.m_col_group];
2110                script_selected = row[m_ScriptsModel.m_col_script];
2111            }
2112            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScript"))->
2113                set_sensitive(group_selected || script_selected);
2114            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScriptGroup"))->
2115                set_sensitive(file);
2116            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/EditScript"))->
2117                set_sensitive(script_selected);    
2118            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/RemoveScript"))->
2119                set_sensitive(group_selected || script_selected);
2120            // show sample popup
2121            script_popup->popup(button->button, button->time);
2122    
2123            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScript"))->
2124                set_sensitive(group_selected || script_selected);
2125            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScriptGroup"))->
2126                set_sensitive(file);
2127            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/EditScript"))->
2128                set_sensitive(script_selected);    
2129            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/RemoveScript"))->
2130                set_sensitive(group_selected || script_selected);
2131        }
2132    }
2133    
2134    Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu(
2135        const Glib::ustring& name, int position) {
2136    
2137        Gtk::RadioMenuItem::Group instrument_group;
2138        const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
2139        if (!children.empty()) {
2140            instrument_group =
2141                static_cast<Gtk::RadioMenuItem*>(children[0])->get_group();
2142        }
2143        Gtk::RadioMenuItem* item =
2144            new Gtk::RadioMenuItem(instrument_group, name);
2145        if (position < 0) {
2146            instrument_menu->append(*item);
2147        } else {
2148            instrument_menu->insert(*item, position);
2149        }
2150        item->show();
2151        item->signal_activate().connect(
2152            sigc::bind(
2153                sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
2154                item));
2155        return item;
2156    }
2157    
2158    void MainWindow::remove_instrument_from_menu(int index) {
2159        const std::vector<Gtk::Widget*> children =
2160            instrument_menu->get_children();
2161        Gtk::Widget* child = children[index];
2162        instrument_menu->remove(*child);
2163        delete child;
2164    }
2165    
2166    void MainWindow::add_instrument(gig::Instrument* instrument) {
2167        const Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
2168    
2169      // update instrument tree view      // update instrument tree view
2170        instrument_name_connection.block();
2171      Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();      Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
2172      Gtk::TreeModel::Row rowInstr = *iterInstr;      Gtk::TreeModel::Row rowInstr = *iterInstr;
2173      rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();      rowInstr[m_Columns.m_col_name] = name;
2174      rowInstr[m_Columns.m_col_instr] = instrument;      rowInstr[m_Columns.m_col_instr] = instrument;
2175        instrument_name_connection.unblock();
2176    
2177        add_instrument_to_menu(name);
2178    
2179        m_TreeView.get_selection()->select(iterInstr);
2180    
2181      file_changed();      file_changed();
2182  }  }
2183    
2184    void MainWindow::on_action_add_instrument() {
2185        static int __instrument_indexer = 0;
2186        if (!file) return;
2187        gig::Instrument* instrument = file->AddInstrument();
2188        __instrument_indexer++;
2189        instrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument ") +
2190                                                ToString(__instrument_indexer));
2191    
2192        add_instrument(instrument);
2193    }
2194    
2195    void MainWindow::on_action_duplicate_instrument() {
2196        if (!file) return;
2197    
2198        // retrieve the currently selected instrument
2199        // (being the original instrument to be duplicated)
2200        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
2201        Gtk::TreeModel::iterator itSelection = sel->get_selected();
2202        if (!itSelection) return;
2203        Gtk::TreeModel::Row row = *itSelection;
2204        gig::Instrument* instrOrig = row[m_Columns.m_col_instr];
2205        if (!instrOrig) return;
2206    
2207        // duplicate the orginal instrument
2208        gig::Instrument* instrNew = file->AddDuplicateInstrument(instrOrig);
2209        instrNew->pInfo->Name =
2210            instrOrig->pInfo->Name +
2211            gig_from_utf8(Glib::ustring(" (") + _("Copy") + ")");
2212    
2213        add_instrument(instrNew);
2214    }
2215    
2216  void MainWindow::on_action_remove_instrument() {  void MainWindow::on_action_remove_instrument() {
2217      if (!file) return;      if (!file) return;
2218        if (file_is_shared) {
2219            Gtk::MessageDialog msg(
2220                *this,
2221                 _("You cannot delete an instrument from this file, since it's "
2222                   "currently used by the sampler."),
2223                 false, Gtk::MESSAGE_INFO
2224            );
2225            msg.run();
2226            return;
2227        }
2228    
2229      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
2230      Gtk::TreeModel::iterator it = sel->get_selected();      Gtk::TreeModel::iterator it = sel->get_selected();
2231      if (it) {      if (it) {
2232          Gtk::TreeModel::Row row = *it;          Gtk::TreeModel::Row row = *it;
2233          gig::Instrument* instr = row[m_Columns.m_col_instr];          gig::Instrument* instr = row[m_Columns.m_col_instr];
2234          try {          try {
2235                Gtk::TreePath path(it);
2236                int index = path[0];
2237    
2238              // remove instrument from the gig file              // remove instrument from the gig file
2239              if (instr) file->DeleteInstrument(instr);              if (instr) file->DeleteInstrument(instr);
             // remove respective row from instruments tree view  
             m_refTreeModel->erase(it);  
2240              file_changed();              file_changed();
2241    
2242                remove_instrument_from_menu(index);
2243    
2244                // remove row from instruments tree view
2245                m_refTreeModel->erase(it);
2246    
2247    #if GTKMM_MAJOR_VERSION < 3
2248                // select another instrument (in gtk3 this is done
2249                // automatically)
2250                if (!m_refTreeModel->children().empty()) {
2251                    if (index == m_refTreeModel->children().size()) {
2252                        index--;
2253                    }
2254                    m_TreeView.get_selection()->select(
2255                        Gtk::TreePath(ToString(index)));
2256                }
2257    #endif
2258                instr_props_set_instrument();
2259                instr = get_instrument();
2260                if (instr) {
2261                    midiRules.set_instrument(instr);
2262                } else {
2263                    midiRules.hide();
2264                }
2265          } catch (RIFF::Exception e) {          } catch (RIFF::Exception e) {
2266              Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2267              msg.run();              msg.run();
# Line 999  void MainWindow::on_action_remove_instru Line 2272  void MainWindow::on_action_remove_instru
2272  void MainWindow::on_action_sample_properties() {  void MainWindow::on_action_sample_properties() {
2273      //TODO: show a dialog where the selected sample's properties can be edited      //TODO: show a dialog where the selected sample's properties can be edited
2274      Gtk::MessageDialog msg(      Gtk::MessageDialog msg(
2275          *this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO          *this, _("Sorry, yet to be implemented!"), false, Gtk::MESSAGE_INFO
2276      );      );
2277      msg.run();      msg.run();
2278  }  }
2279    
2280    void MainWindow::on_action_add_script_group() {
2281        static int __script_indexer = 0;
2282        if (!file) return;
2283        gig::ScriptGroup* group = file->AddScriptGroup();
2284        group->Name = gig_from_utf8(_("Unnamed Group"));
2285        if (__script_indexer) group->Name += " " + ToString(__script_indexer);
2286        __script_indexer++;
2287        // update sample tree view
2288        Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
2289        Gtk::TreeModel::Row rowGroup = *iterGroup;
2290        rowGroup[m_ScriptsModel.m_col_name] = gig_to_utf8(group->Name);
2291        rowGroup[m_ScriptsModel.m_col_script] = NULL;
2292        rowGroup[m_ScriptsModel.m_col_group] = group;
2293        file_changed();
2294    }
2295    
2296    void MainWindow::on_action_add_script() {
2297        if (!file) return;
2298        // get selected group
2299        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2300        Gtk::TreeModel::iterator it = sel->get_selected();
2301        if (!it) return;
2302        Gtk::TreeModel::Row row = *it;
2303        gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2304        if (!group) { // not a group, but a script is selected (probably)
2305            gig::Script* script = row[m_ScriptsModel.m_col_script];
2306            if (!script) return;
2307            it = row.parent(); // resolve parent (that is the script's group)
2308            if (!it) return;
2309            row = *it;
2310            group = row[m_ScriptsModel.m_col_group];
2311            if (!group) return;
2312        }
2313    
2314        // add a new script to the .gig file
2315        gig::Script* script = group->AddScript();    
2316        Glib::ustring name = _("Unnamed Script");
2317        script->Name = gig_from_utf8(name);
2318    
2319        // add script to the tree view
2320        Gtk::TreeModel::iterator iterScript =
2321            m_refScriptsTreeModel->append(row.children());
2322        Gtk::TreeModel::Row rowScript = *iterScript;
2323        rowScript[m_ScriptsModel.m_col_name] = name;
2324        rowScript[m_ScriptsModel.m_col_script] = script;
2325        rowScript[m_ScriptsModel.m_col_group]  = NULL;
2326    
2327        // unfold group of new script item in treeview
2328        Gtk::TreeModel::Path path(iterScript);
2329        m_TreeViewScripts.expand_to_path(path);
2330    }
2331    
2332    void MainWindow::on_action_edit_script() {
2333        if (!file) return;
2334        // get selected script
2335        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2336        Gtk::TreeModel::iterator it = sel->get_selected();
2337        if (!it) return;
2338        Gtk::TreeModel::Row row = *it;
2339        gig::Script* script = row[m_ScriptsModel.m_col_script];
2340        if (!script) return;
2341    
2342        ScriptEditor* editor = new ScriptEditor;
2343        editor->setScript(script);
2344        //editor->reparent(*this);
2345        editor->show();
2346    }
2347    
2348    void MainWindow::on_action_remove_script() {
2349        if (!file) return;
2350        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2351        Gtk::TreeModel::iterator it = sel->get_selected();
2352        if (it) {
2353            Gtk::TreeModel::Row row = *it;
2354            gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2355            gig::Script* script     = row[m_ScriptsModel.m_col_script];
2356            Glib::ustring name      = row[m_ScriptsModel.m_col_name];
2357            try {
2358                // remove script group or script from the gig file
2359                if (group) {
2360                    // notify everybody that we're going to remove these samples
2361    //TODO:         scripts_to_be_removed_signal.emit(members);
2362                    // delete the group in the .gig file including the
2363                    // samples that belong to the group
2364                    file->DeleteScriptGroup(group);
2365                    // notify that we're done with removal
2366    //TODO:         scripts_removed_signal.emit();
2367                    file_changed();
2368                } else if (script) {
2369                    // notify everybody that we're going to remove this sample
2370    //TODO:         std::list<gig::Script*> lscripts;
2371    //TODO:         lscripts.push_back(script);
2372    //TODO:         scripts_to_be_removed_signal.emit(lscripts);
2373                    // remove sample from the .gig file
2374                    script->GetGroup()->DeleteScript(script);
2375                    // notify that we're done with removal
2376    //TODO:         scripts_removed_signal.emit();
2377                    dimreg_changed();
2378                    file_changed();
2379                }
2380                // remove respective row(s) from samples tree view
2381                m_refScriptsTreeModel->erase(it);
2382            } catch (RIFF::Exception e) {
2383                // pretend we're done with removal (i.e. to avoid dead locks)
2384    //TODO:     scripts_removed_signal.emit();
2385                // show error message
2386                Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2387                msg.run();
2388            }
2389        }
2390    }
2391    
2392  void MainWindow::on_action_add_group() {  void MainWindow::on_action_add_group() {
2393      static int __sample_indexer = 0;      static int __sample_indexer = 0;
2394      if (!file) return;      if (!file) return;
2395      gig::Group* group = file->AddGroup();      gig::Group* group = file->AddGroup();
2396      group->Name = "Unnamed Group";      group->Name = gig_from_utf8(_("Unnamed Group"));
2397      if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);      if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
2398      __sample_indexer++;      __sample_indexer++;
2399      // update sample tree view      // update sample tree view
2400      Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();      Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
2401      Gtk::TreeModel::Row rowGroup = *iterGroup;      Gtk::TreeModel::Row rowGroup = *iterGroup;
2402      rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();      rowGroup[m_SamplesModel.m_col_name] = gig_to_utf8(group->Name);
2403      rowGroup[m_SamplesModel.m_col_sample] = NULL;      rowGroup[m_SamplesModel.m_col_sample] = NULL;
2404      rowGroup[m_SamplesModel.m_col_group] = group;      rowGroup[m_SamplesModel.m_col_group] = group;
2405      file_changed();      file_changed();
2406  }  }
2407    
2408    void MainWindow::on_action_replace_sample() {
2409        add_or_replace_sample(true);
2410    }
2411    
2412  void MainWindow::on_action_add_sample() {  void MainWindow::on_action_add_sample() {
2413        add_or_replace_sample(false);
2414    }
2415    
2416    void MainWindow::add_or_replace_sample(bool replace) {
2417      if (!file) return;      if (!file) return;
2418      // get selected group  
2419        // get selected group (and probably selected sample)
2420      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2421      Gtk::TreeModel::iterator it = sel->get_selected();      Gtk::TreeModel::iterator it = sel->get_selected();
2422      if (!it) return;      if (!it) return;
2423      Gtk::TreeModel::Row row = *it;      Gtk::TreeModel::Row row = *it;
2424        gig::Sample* sample = NULL;
2425      gig::Group* group = row[m_SamplesModel.m_col_group];      gig::Group* group = row[m_SamplesModel.m_col_group];
2426      if (!group) { // not a group, but a sample is selected (probably)      if (!group) { // not a group, but a sample is selected (probably)
2427          gig::Sample* sample = row[m_SamplesModel.m_col_sample];          if (replace) sample = row[m_SamplesModel.m_col_sample];
2428          if (!sample) return;          if (!row[m_SamplesModel.m_col_sample]) return;
2429          it = row.parent(); // resolve parent (that is the sample's group)          it = row.parent(); // resolve parent (that is the sample's group)
2430          if (!it) return;          if (!it) return;
2431          row = *it;          if (!replace) row = *it;
2432          group = row[m_SamplesModel.m_col_group];          group = (*it)[m_SamplesModel.m_col_group];
2433          if (!group) return;          if (!group) return;
2434      }      }
2435        if (replace && !sample) return;
2436    
2437      // show 'browse for file' dialog      // show 'browse for file' dialog
2438      Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));      Gtk::FileChooserDialog dialog(*this, replace ? _("Replace Sample with") : _("Add Sample(s)"));
2439      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
2440      dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);      dialog.add_button(_("_Open"), Gtk::RESPONSE_OK);
2441      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
2442      Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile  
2443        // matches all file types supported by libsndfile
2444    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2445        Gtk::FileFilter soundfilter;
2446    #else
2447        Glib::RefPtr<Gtk::FileFilter> soundfilter = Gtk::FileFilter::create();
2448    #endif
2449      const char* const supportedFileTypes[] = {      const char* const supportedFileTypes[] = {
2450          "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",          "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
2451          "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",          "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
# Line 1050  void MainWindow::on_action_add_sample() Line 2453  void MainWindow::on_action_add_sample()
2453          "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",          "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
2454          "*.caf", "*.CAF", NULL          "*.caf", "*.CAF", NULL
2455      };      };
2456        const char* soundfiles = _("Sound Files");
2457        const char* allfiles = _("All Files");
2458    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2459      for (int i = 0; supportedFileTypes[i]; i++)      for (int i = 0; supportedFileTypes[i]; i++)
2460          soundfilter.add_pattern(supportedFileTypes[i]);          soundfilter.add_pattern(supportedFileTypes[i]);
2461      soundfilter.set_name("Sound Files");      soundfilter.set_name(soundfiles);
2462      Gtk::FileFilter allpassfilter; // matches every file  
2463        // matches every file
2464        Gtk::FileFilter allpassfilter;
2465      allpassfilter.add_pattern("*.*");      allpassfilter.add_pattern("*.*");
2466      allpassfilter.set_name("All Files");      allpassfilter.set_name(allfiles);
2467    #else
2468        for (int i = 0; supportedFileTypes[i]; i++)
2469            soundfilter->add_pattern(supportedFileTypes[i]);
2470        soundfilter->set_name(soundfiles);
2471    
2472        // matches every file
2473        Glib::RefPtr<Gtk::FileFilter> allpassfilter = Gtk::FileFilter::create();
2474        allpassfilter->add_pattern("*.*");
2475        allpassfilter->set_name(allfiles);
2476    #endif
2477      dialog.add_filter(soundfilter);      dialog.add_filter(soundfilter);
2478      dialog.add_filter(allpassfilter);      dialog.add_filter(allpassfilter);
2479        if (current_sample_dir != "") {
2480            dialog.set_current_folder(current_sample_dir);
2481        }
2482      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
2483            current_sample_dir = dialog.get_current_folder();
2484          Glib::ustring error_files;          Glib::ustring error_files;
2485          Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();          std::vector<std::string> filenames = dialog.get_filenames();
2486          for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();          for (std::vector<std::string>::iterator iter = filenames.begin();
2487               iter != filenames.end(); ++iter) {               iter != filenames.end(); ++iter) {
2488              printf("Adding sample %s\n",(*iter).c_str());              printf("Adding sample %s\n",(*iter).c_str());
2489              // use libsndfile to retrieve file informations              // use libsndfile to retrieve file informations
# Line 1069  void MainWindow::on_action_add_sample() Line 2491  void MainWindow::on_action_add_sample()
2491              info.format = 0;              info.format = 0;
2492              SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);              SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
2493              try {              try {
2494                  if (!hFile) throw std::string("could not open file");                  if (!hFile) throw std::string(_("could not open file"));
2495                  int bitdepth;                  int bitdepth;
2496                  switch (info.format & 0xff) {                  switch (info.format & 0xff) {
2497                      case SF_FORMAT_PCM_S8:                      case SF_FORMAT_PCM_S8:
                         bitdepth = 16; // we simply convert to 16 bit for now  
                         break;  
2498                      case SF_FORMAT_PCM_16:                      case SF_FORMAT_PCM_16:
2499                        case SF_FORMAT_PCM_U8:
2500                          bitdepth = 16;                          bitdepth = 16;
2501                          break;                          break;
2502                      case SF_FORMAT_PCM_24:                      case SF_FORMAT_PCM_24:
                         bitdepth = 32; // we simply convert to 32 bit for now  
                         break;  
2503                      case SF_FORMAT_PCM_32:                      case SF_FORMAT_PCM_32:
                         bitdepth = 32;  
                         break;  
                     case SF_FORMAT_PCM_U8:  
                         bitdepth = 16; // we simply convert to 16 bit for now  
                         break;  
2504                      case SF_FORMAT_FLOAT:                      case SF_FORMAT_FLOAT:
                         bitdepth = 32;  
                         break;  
2505                      case SF_FORMAT_DOUBLE:                      case SF_FORMAT_DOUBLE:
2506                          bitdepth = 32; // I guess we will always truncate this to 32 bit                          bitdepth = 24;
2507                          break;                          break;
2508                      default:                      default:
2509                          sf_close(hFile); // close sound file                          sf_close(hFile); // close sound file
2510                          throw std::string("format not supported"); // unsupported subformat (yet?)                          throw std::string(_("format not supported")); // unsupported subformat (yet?)
2511                  }                  }
2512                  // add a new sample to the .gig file                  // add a new sample to the .gig file (if adding is requested actually)
2513                  gig::Sample* sample = file->AddSample();                  if (!replace) sample = file->AddSample();
2514                  // file name without path                  // file name without path
2515                  Glib::ustring filename = Glib::filename_display_basename(*iter);                  Glib::ustring filename = Glib::filename_display_basename(*iter);
2516                  // remove file extension if there is one                  // remove file extension if there is one
# Line 1108  void MainWindow::on_action_add_sample() Line 2520  void MainWindow::on_action_add_sample()
2520                          break;                          break;
2521                      }                      }
2522                  }                  }
2523                  sample->pInfo->Name = filename;                  sample->pInfo->Name = gig_from_utf8(filename);
2524                  sample->Channels = info.channels;                  sample->Channels = info.channels;
2525                  sample->BitDepth = bitdepth;                  sample->BitDepth = bitdepth;
2526                  sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;                  sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
2527                  sample->SamplesPerSecond = info.samplerate;                  sample->SamplesPerSecond = info.samplerate;
2528                    sample->AverageBytesPerSecond = sample->FrameSize * sample->SamplesPerSecond;
2529                    sample->BlockAlign = sample->FrameSize;
2530                    sample->SamplesTotal = info.frames;
2531    
2532                    SF_INSTRUMENT instrument;
2533                    if (sf_command(hFile, SFC_GET_INSTRUMENT,
2534                                   &instrument, sizeof(instrument)) != SF_FALSE)
2535                    {
2536                        sample->MIDIUnityNote = instrument.basenote;
2537                        sample->FineTune      = instrument.detune;
2538    
2539                        if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) {
2540                            sample->Loops = 1;
2541    
2542                            switch (instrument.loops[0].mode) {
2543                            case SF_LOOP_FORWARD:
2544                                sample->LoopType = gig::loop_type_normal;
2545                                break;
2546                            case SF_LOOP_BACKWARD:
2547                                sample->LoopType = gig::loop_type_backward;
2548                                break;
2549                            case SF_LOOP_ALTERNATING:
2550                                sample->LoopType = gig::loop_type_bidirectional;
2551                                break;
2552                            }
2553                            sample->LoopStart = instrument.loops[0].start;
2554                            sample->LoopEnd = instrument.loops[0].end;
2555                            sample->LoopPlayCount = instrument.loops[0].count;
2556                            sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1;
2557                        }
2558                    }
2559    
2560                  // schedule resizing the sample (which will be done                  // schedule resizing the sample (which will be done
2561                  // physically when File::Save() is called)                  // physically when File::Save() is called)
2562                  sample->Resize(info.frames);                  sample->Resize(info.frames);
2563                  // make sure sample is part of the selected group                  // make sure sample is part of the selected group
2564                  group->AddSample(sample);                  if (!replace) group->AddSample(sample);
2565                  // schedule that physical resize and sample import                  // schedule that physical resize and sample import
2566                  // (data copying), performed when "Save" is requested                  // (data copying), performed when "Save" is requested
2567                  SampleImportItem sched_item;                  SampleImportItem sched_item;
# Line 1125  void MainWindow::on_action_add_sample() Line 2569  void MainWindow::on_action_add_sample()
2569                  sched_item.sample_path = *iter;                  sched_item.sample_path = *iter;
2570                  m_SampleImportQueue.push_back(sched_item);                  m_SampleImportQueue.push_back(sched_item);
2571                  // add sample to the tree view                  // add sample to the tree view
2572                  Gtk::TreeModel::iterator iterSample =                  if (replace) {
2573                      m_refSamplesTreeModel->append(row.children());                      row[m_SamplesModel.m_col_name] = gig_to_utf8(sample->pInfo->Name);
2574                  Gtk::TreeModel::Row rowSample = *iterSample;                  } else {
2575                  rowSample[m_SamplesModel.m_col_name]   = filename;                      Gtk::TreeModel::iterator iterSample =
2576                  rowSample[m_SamplesModel.m_col_sample] = sample;                          m_refSamplesTreeModel->append(row.children());
2577                  rowSample[m_SamplesModel.m_col_group]  = NULL;                      Gtk::TreeModel::Row rowSample = *iterSample;
2578                        rowSample[m_SamplesModel.m_col_name] =
2579                            gig_to_utf8(sample->pInfo->Name);
2580                        rowSample[m_SamplesModel.m_col_sample] = sample;
2581                        rowSample[m_SamplesModel.m_col_group]  = NULL;
2582                    }
2583                  // close sound file                  // close sound file
2584                  sf_close(hFile);                  sf_close(hFile);
2585                  file_changed();                  file_changed();
2586              } catch (std::string what) { // remember the files that made trouble (and their cause)              } catch (std::string what) { // remember the files that made trouble (and their cause)
2587                  if (error_files.size()) error_files += "\n";                  if (!error_files.empty()) error_files += "\n";
2588                  error_files += *iter += " (" + what + ")";                  error_files += *iter += " (" + what + ")";
2589              }              }
2590          }          }
2591          // 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
2592          if (error_files.size()) {          if (!error_files.empty()) {
2593              Glib::ustring txt = "Could not add the following sample(s):\n" + error_files;              Glib::ustring txt =
2594                    (replace
2595                        ? _("Failed to replace sample with:\n")
2596                        : _("Could not add the following sample(s):\n"))
2597                    + error_files;
2598                Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2599                msg.run();
2600            }
2601        }
2602    }
2603    
2604    void MainWindow::on_action_replace_all_samples_in_all_groups()
2605    {
2606        if (!file) return;
2607        Gtk::FileChooserDialog dialog(*this, _("Select Folder"),
2608                                      Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
2609        const char* str =
2610            _("This is a very specific function. It tries to replace all samples "
2611              "in the current gig file by samples located in the chosen "
2612              "directory.\n\n"
2613              "It works like this: For each sample in the gig file, it tries to "
2614              "find a sample file in the selected directory with the same name as "
2615              "the sample in the gig file. Optionally, you can add a filename "
2616              "extension below, which will be added to the filename expected to be "
2617              "found. That is, assume you have a gig file with a sample called "
2618              "'Snare', if you enter '.wav' below (like it's done by default), it "
2619              "expects to find a sample file called 'Snare.wav' and will replace "
2620              "the sample in the gig file accordingly. If you don't need an "
2621              "extension, blank the field below. Any gig sample where no "
2622              "appropriate sample file could be found will be reported and left "
2623              "untouched.\n");
2624    #if GTKMM_MAJOR_VERSION < 3
2625        view::WrapLabel description(str);
2626    #else
2627        Gtk::Label description(str);
2628        description.set_line_wrap();
2629    #endif
2630        Gtk::HBox entryArea;
2631        Gtk::Label entryLabel( _("Add filename extension: "), Gtk::ALIGN_START);
2632        Gtk::Entry postfixEntryBox;
2633        postfixEntryBox.set_text(".wav");
2634        entryArea.pack_start(entryLabel);
2635        entryArea.pack_start(postfixEntryBox);
2636        dialog.get_vbox()->pack_start(description, Gtk::PACK_SHRINK);
2637        dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK);
2638        description.show();
2639        entryArea.show_all();
2640        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
2641        dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
2642        dialog.set_select_multiple(false);
2643        if (current_sample_dir != "") {
2644            dialog.set_current_folder(current_sample_dir);
2645        }
2646        if (dialog.run() == Gtk::RESPONSE_OK)
2647        {
2648            current_sample_dir = dialog.get_current_folder();
2649            Glib::ustring error_files;
2650            std::string folder = dialog.get_filename();
2651            for (gig::Sample* sample = file->GetFirstSample();
2652                 sample; sample = file->GetNextSample())
2653            {
2654                std::string filename =
2655                    folder + G_DIR_SEPARATOR_S +
2656                    Glib::filename_from_utf8(gig_to_utf8(sample->pInfo->Name) +
2657                                             postfixEntryBox.get_text());
2658                SF_INFO info;
2659                info.format = 0;
2660                SNDFILE* hFile = sf_open(filename.c_str(), SFM_READ, &info);
2661                try
2662                {
2663                    if (!hFile) throw std::string(_("could not open file"));
2664                    switch (info.format & 0xff) {
2665                        case SF_FORMAT_PCM_S8:
2666                        case SF_FORMAT_PCM_16:
2667                        case SF_FORMAT_PCM_U8:
2668                        case SF_FORMAT_PCM_24:
2669                        case SF_FORMAT_PCM_32:
2670                        case SF_FORMAT_FLOAT:
2671                        case SF_FORMAT_DOUBLE:
2672                            break;
2673                        default:
2674                            sf_close(hFile);
2675                            throw std::string(_("format not supported"));
2676                    }
2677                    SampleImportItem sched_item;
2678                    sched_item.gig_sample  = sample;
2679                    sched_item.sample_path = filename;
2680                    m_SampleImportQueue.push_back(sched_item);
2681                    sf_close(hFile);
2682                    file_changed();
2683                }
2684                catch (std::string what)
2685                {
2686                    if (!error_files.empty()) error_files += "\n";
2687                    error_files += Glib::filename_to_utf8(filename) +
2688                        " (" + what + ")";
2689                }
2690            }
2691            // show error message box when some file(s) could not be opened / added
2692            if (!error_files.empty()) {
2693                Glib::ustring txt =
2694                    _("Could not replace the following sample(s):\n") + error_files;
2695              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2696              msg.run();              msg.run();
2697          }          }
# Line 1160  void MainWindow::on_action_remove_sample Line 2710  void MainWindow::on_action_remove_sample
2710          try {          try {
2711              // remove group or sample from the gig file              // remove group or sample from the gig file
2712              if (group) {              if (group) {
2713                  // temporarily remember the samples that bolong to                  // temporarily remember the samples that belong to
2714                  // that group (we need that to clean the queue)                  // that group (we need that to clean the queue)
2715                  std::list<gig::Sample*> members;                  std::list<gig::Sample*> members;
2716                  for (gig::Sample* pSample = group->GetFirstSample();                  for (gig::Sample* pSample = group->GetFirstSample();
2717                       pSample; pSample = group->GetNextSample()) {                       pSample; pSample = group->GetNextSample()) {
2718                      members.push_back(pSample);                      members.push_back(pSample);
2719                  }                  }
2720                    // notify everybody that we're going to remove these samples
2721                    samples_to_be_removed_signal.emit(members);
2722                  // delete the group in the .gig file including the                  // delete the group in the .gig file including the
2723                  // samples that belong to the group                  // samples that belong to the group
2724                  file->DeleteGroup(group);                  file->DeleteGroup(group);
2725                    // notify that we're done with removal
2726                    samples_removed_signal.emit();
2727                  // if sample(s) were just previously added, remove                  // if sample(s) were just previously added, remove
2728                  // them from the import queue                  // them from the import queue
2729                  for (std::list<gig::Sample*>::iterator member = members.begin();                  for (std::list<gig::Sample*>::iterator member = members.begin();
# Line 1186  void MainWindow::on_action_remove_sample Line 2740  void MainWindow::on_action_remove_sample
2740                  }                  }
2741                  file_changed();                  file_changed();
2742              } else if (sample) {              } else if (sample) {
2743                    // notify everybody that we're going to remove this sample
2744                    std::list<gig::Sample*> lsamples;
2745                    lsamples.push_back(sample);
2746                    samples_to_be_removed_signal.emit(lsamples);
2747                  // remove sample from the .gig file                  // remove sample from the .gig file
2748                  file->DeleteSample(sample);                  file->DeleteSample(sample);
2749                    // notify that we're done with removal
2750                    samples_removed_signal.emit();
2751                  // if sample was just previously added, remove it from                  // if sample was just previously added, remove it from
2752                  // the import queue                  // the import queue
2753                  for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();                  for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
# Line 1199  void MainWindow::on_action_remove_sample Line 2759  void MainWindow::on_action_remove_sample
2759                          break;                          break;
2760                      }                      }
2761                  }                  }
2762                    dimreg_changed();
2763                  file_changed();                  file_changed();
2764              }              }
2765              // remove respective row(s) from samples tree view              // remove respective row(s) from samples tree view
2766              m_refSamplesTreeModel->erase(it);              m_refSamplesTreeModel->erase(it);
2767          } catch (RIFF::Exception e) {          } catch (RIFF::Exception e) {
2768                // pretend we're done with removal (i.e. to avoid dead locks)
2769                samples_removed_signal.emit();
2770                // show error message
2771              Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2772              msg.run();              msg.run();
2773          }          }
2774      }      }
2775  }  }
2776    
2777    void MainWindow::on_action_remove_unused_samples() {
2778        if (!file) return;
2779    
2780        // collect all samples that are not referenced by any instrument
2781        std::list<gig::Sample*> lsamples;
2782        for (int iSample = 0; file->GetSample(iSample); ++iSample) {
2783            gig::Sample* sample = file->GetSample(iSample);
2784            bool isUsed = false;
2785            for (gig::Instrument* instrument = file->GetFirstInstrument(); instrument;
2786                                  instrument = file->GetNextInstrument())
2787            {
2788                for (gig::Region* rgn = instrument->GetFirstRegion(); rgn;
2789                                  rgn = instrument->GetNextRegion())
2790                {
2791                    for (int i = 0; i < 256; ++i) {
2792                        if (!rgn->pDimensionRegions[i]) continue;
2793                        if (rgn->pDimensionRegions[i]->pSample != sample) continue;
2794                        isUsed = true;
2795                        goto endOfRefSearch;
2796                    }
2797                }
2798            }
2799            endOfRefSearch:
2800            if (!isUsed) lsamples.push_back(sample);
2801        }
2802    
2803        if (lsamples.empty()) return;
2804    
2805        // notify everybody that we're going to remove these samples
2806        samples_to_be_removed_signal.emit(lsamples);
2807    
2808        // remove collected samples
2809        try {
2810            for (std::list<gig::Sample*>::iterator itSample = lsamples.begin();
2811                 itSample != lsamples.end(); ++itSample)
2812            {
2813                gig::Sample* sample = *itSample;
2814                // remove sample from the .gig file
2815                file->DeleteSample(sample);
2816                // if sample was just previously added, remove it fro the import queue
2817                for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
2818                     iter != m_SampleImportQueue.end(); ++iter)
2819                {
2820                    if ((*iter).gig_sample == sample) {
2821                        printf("Removing previously added sample '%s'\n",
2822                               (*iter).sample_path.c_str());
2823                        m_SampleImportQueue.erase(iter);
2824                        break;
2825                    }
2826                }
2827            }
2828        } catch (RIFF::Exception e) {
2829            // show error message
2830            Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2831            msg.run();
2832        }
2833    
2834        // notify everybody that we're done with removal
2835        samples_removed_signal.emit();
2836    
2837        dimreg_changed();
2838        file_changed();
2839        __refreshEntireGUI();
2840    }
2841    
2842    // see comment on on_sample_treeview_drag_begin()
2843    void MainWindow::on_scripts_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2844    {
2845        first_call_to_drag_data_get = true;
2846    }
2847    
2848    void MainWindow::on_scripts_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2849                                                       Gtk::SelectionData& selection_data, guint, guint)
2850    {
2851        if (!first_call_to_drag_data_get) return;
2852        first_call_to_drag_data_get = false;
2853    
2854        // get selected script
2855        gig::Script* script = NULL;
2856        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2857        Gtk::TreeModel::iterator it = sel->get_selected();
2858        if (it) {
2859            Gtk::TreeModel::Row row = *it;
2860            script = row[m_ScriptsModel.m_col_script];
2861        }
2862        // pass the gig::Script as pointer
2863        selection_data.set(selection_data.get_target(), 0/*unused*/,
2864                           (const guchar*)&script,
2865                           sizeof(script)/*length of data in bytes*/);
2866    }
2867    
2868    // see comment on on_sample_treeview_drag_begin()
2869    void MainWindow::on_instruments_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2870    {
2871        first_call_to_drag_data_get = true;
2872    }
2873    
2874    void MainWindow::on_instruments_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2875                                                           Gtk::SelectionData& selection_data, guint, guint)
2876    {
2877        if (!first_call_to_drag_data_get) return;
2878        first_call_to_drag_data_get = false;
2879    
2880        // get selected source instrument
2881        gig::Instrument* src = NULL;
2882        {
2883            Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
2884            Gtk::TreeModel::iterator it = sel->get_selected();
2885            if (it) {
2886                Gtk::TreeModel::Row row = *it;
2887                src = row[m_Columns.m_col_instr];
2888            }
2889        }
2890        if (!src) return;
2891    
2892        // pass the source gig::Instrument as pointer
2893        selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&src,
2894                           sizeof(src)/*length of data in bytes*/);
2895    }
2896    
2897    void MainWindow::on_instruments_treeview_drop_drag_data_received(
2898        const Glib::RefPtr<Gdk::DragContext>& context, int x, int y,
2899        const Gtk::SelectionData& selection_data, guint, guint time)
2900    {
2901        gig::Instrument* src = *((gig::Instrument**) selection_data.get_data());
2902        if (!src || selection_data.get_length() != sizeof(gig::Instrument*))
2903            return;
2904    
2905        gig::Instrument* dst = NULL;
2906        {
2907            Gtk::TreeModel::Path path;
2908            const bool found = m_TreeView.get_path_at_pos(x, y, path);
2909            if (!found) return;
2910    
2911            Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path);
2912            if (!iter) return;
2913            Gtk::TreeModel::Row row = *iter;
2914            dst = row[m_Columns.m_col_instr];
2915        }
2916        if (!dst) return;
2917    
2918        //printf("dragdrop received src=%s dst=%s\n", src->pInfo->Name.c_str(), dst->pInfo->Name.c_str());
2919        src->MoveTo(dst);
2920        __refreshEntireGUI();
2921        select_instrument(src);
2922    }
2923    
2924    // For some reason drag_data_get gets called two times for each
2925    // drag'n'drop (at least when target is an Entry). This work-around
2926    // makes sure the code in drag_data_get and drop_drag_data_received is
2927    // only executed once, as drag_begin only gets called once.
2928    void MainWindow::on_sample_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2929    {
2930        first_call_to_drag_data_get = true;
2931    }
2932    
2933  void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,  void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2934                                                    Gtk::SelectionData& selection_data, guint, guint)                                                    Gtk::SelectionData& selection_data, guint, guint)
2935  {  {
2936        if (!first_call_to_drag_data_get) return;
2937        first_call_to_drag_data_get = false;
2938    
2939      // get selected sample      // get selected sample
2940      gig::Sample* sample = NULL;      gig::Sample* sample = NULL;
2941      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
# Line 1230  void MainWindow::on_sample_label_drop_dr Line 2953  void MainWindow::on_sample_label_drop_dr
2953      const Glib::RefPtr<Gdk::DragContext>& context, int, int,      const Glib::RefPtr<Gdk::DragContext>& context, int, int,
2954      const Gtk::SelectionData& selection_data, guint, guint time)      const Gtk::SelectionData& selection_data, guint, guint time)
2955  {  {
     gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion();  
2956      gig::Sample* sample = *((gig::Sample**) selection_data.get_data());      gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
2957    
2958      if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) {      if (sample && selection_data.get_length() == sizeof(gig::Sample*)) {
2959          if (sample != dimregion->pSample) {          std::cout << "Drop received sample \"" <<
2960              dimregion->pSample = sample;              sample->pInfo->Name << "\"" << std::endl;
2961              dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str());          // drop success
2962              std::cout << "Drop received sample \"" <<          context->drop_reply(true, time);
2963                  dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl;  
2964              // drop success          //TODO: we should better move most of the following code to DimRegionEdit::set_sample()
2965              context->drop_reply(true, time);  
2966              file_changed();          // notify everybody that we're going to alter the region
2967              return;          gig::Region* region = m_RegionChooser.get_region();
2968            region_to_be_changed_signal.emit(region);
2969    
2970            // find the samplechannel dimension
2971            gig::dimension_def_t* stereo_dimension = 0;
2972            for (int i = 0 ; i < region->Dimensions ; i++) {
2973                if (region->pDimensionDefinitions[i].dimension ==
2974                    gig::dimension_samplechannel) {
2975                    stereo_dimension = &region->pDimensionDefinitions[i];
2976                    break;
2977                }
2978            }
2979            bool channels_changed = false;
2980            if (sample->Channels == 1 && stereo_dimension) {
2981                // remove the samplechannel dimension
2982    /* commented out, because it makes it impossible building up an instrument from scratch using two separate L/R samples
2983                region->DeleteDimension(stereo_dimension);
2984                channels_changed = true;
2985                region_changed();
2986    */
2987          }          }
2988            dimreg_edit.set_sample(
2989                sample,
2990                is_copy_samples_unity_note_enabled(),
2991                is_copy_samples_fine_tune_enabled(),
2992                is_copy_samples_loop_enabled()
2993            );
2994    
2995            if (sample->Channels == 2 && !stereo_dimension) {
2996                // add samplechannel dimension
2997                gig::dimension_def_t dim;
2998                dim.dimension = gig::dimension_samplechannel;
2999                dim.bits = 1;
3000                dim.zones = 2;
3001                region->AddDimension(&dim);
3002                channels_changed = true;
3003                region_changed();
3004            }
3005            if (channels_changed) {
3006                // unmap all samples with wrong number of channels
3007                // TODO: maybe there should be a warning dialog for this
3008                for (int i = 0 ; i < region->DimensionRegions ; i++) {
3009                    gig::DimensionRegion* d = region->pDimensionRegions[i];
3010                    if (d->pSample && d->pSample->Channels != sample->Channels) {
3011                        gig::Sample* oldref = d->pSample;
3012                        d->pSample = NULL;
3013                        sample_ref_changed_signal.emit(oldref, NULL);
3014                    }
3015                }
3016            }
3017    
3018            // notify we're done with altering
3019            region_changed_signal.emit(region);
3020    
3021            file_changed();
3022    
3023            return;
3024      }      }
3025      // drop failed      // drop failed
3026      context->drop_reply(false, time);      context->drop_reply(false, time);
# Line 1256  void MainWindow::sample_name_changed(con Line 3033  void MainWindow::sample_name_changed(con
3033      Glib::ustring name  = row[m_SamplesModel.m_col_name];      Glib::ustring name  = row[m_SamplesModel.m_col_name];
3034      gig::Group* group   = row[m_SamplesModel.m_col_group];      gig::Group* group   = row[m_SamplesModel.m_col_group];
3035      gig::Sample* sample = row[m_SamplesModel.m_col_sample];      gig::Sample* sample = row[m_SamplesModel.m_col_sample];
3036        gig::String gigname(gig_from_utf8(name));
3037      if (group) {      if (group) {
3038          if (group->Name != name) {          if (group->Name != gigname) {
3039              group->Name = name;              group->Name = gigname;
3040              printf("group name changed\n");              printf("group name changed\n");
3041              file_changed();              file_changed();
3042          }          }
3043      } else if (sample) {      } else if (sample) {
3044          if (sample->pInfo->Name != name.raw()) {          if (sample->pInfo->Name != gigname) {
3045              sample->pInfo->Name = name.raw();              sample->pInfo->Name = gigname;
3046              printf("sample name changed\n");              printf("sample name changed\n");
3047              file_changed();              file_changed();
3048          }          }
3049      }      }
3050  }  }
3051    
3052    void MainWindow::script_name_changed(const Gtk::TreeModel::Path& path,
3053                                         const Gtk::TreeModel::iterator& iter) {
3054        if (!iter) return;
3055        Gtk::TreeModel::Row row = *iter;
3056        Glib::ustring name      = row[m_ScriptsModel.m_col_name];
3057        gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
3058        gig::Script* script     = row[m_ScriptsModel.m_col_script];
3059        gig::String gigname(gig_from_utf8(name));
3060        if (group) {
3061            if (group->Name != gigname) {
3062                group->Name = gigname;
3063                printf("script group name changed\n");
3064                file_changed();
3065            }
3066        } else if (script) {
3067            if (script->Name != gigname) {
3068                script->Name = gigname;
3069                printf("script name changed\n");
3070                file_changed();
3071            }
3072        }
3073    }
3074    
3075    void MainWindow::script_double_clicked(const Gtk::TreeModel::Path& path,
3076                                           Gtk::TreeViewColumn* column)
3077    {
3078        Gtk::TreeModel::iterator iter = m_refScriptsTreeModel->get_iter(path);
3079        if (!iter) return;
3080        Gtk::TreeModel::Row row = *iter;
3081        gig::Script* script = row[m_ScriptsModel.m_col_script];
3082        if (!script) return;
3083    
3084        ScriptEditor* editor = new ScriptEditor;
3085        editor->setScript(script);
3086        //editor->reparent(*this);
3087        editor->show();
3088    }
3089    
3090  void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,  void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
3091                                           const Gtk::TreeModel::iterator& iter) {                                           const Gtk::TreeModel::iterator& iter) {
3092      if (!iter) return;      if (!iter) return;
3093      Gtk::TreeModel::Row row = *iter;      Gtk::TreeModel::Row row = *iter;
3094      Glib::ustring name = row[m_Columns.m_col_name];      Glib::ustring name = row[m_Columns.m_col_name];
3095    
3096        // change name in instrument menu
3097        int index = path[0];
3098        const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
3099        if (index < children.size()) {
3100    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 16) || GTKMM_MAJOR_VERSION > 2
3101            static_cast<Gtk::RadioMenuItem*>(children[index])->set_label(name);
3102    #else
3103            remove_instrument_from_menu(index);
3104            Gtk::RadioMenuItem* item = add_instrument_to_menu(name, index);
3105            item->set_active();
3106    #endif
3107        }
3108    
3109        // change name in gig
3110      gig::Instrument* instrument = row[m_Columns.m_col_instr];      gig::Instrument* instrument = row[m_Columns.m_col_instr];
3111      if (instrument && instrument->pInfo->Name != name.raw()) {      gig::String gigname(gig_from_utf8(name));
3112          instrument->pInfo->Name = name.raw();      if (instrument && instrument->pInfo->Name != gigname) {
3113            instrument->pInfo->Name = gigname;
3114    
3115            // change name in the instrument properties window
3116            if (instrumentProps.get_instrument() == instrument) {
3117                instrumentProps.update_name();
3118            }
3119    
3120          file_changed();          file_changed();
3121      }      }
3122  }  }
3123    
3124    void MainWindow::on_action_combine_instruments() {
3125        CombineInstrumentsDialog* d = new CombineInstrumentsDialog(*this, file);
3126        d->show_all();
3127        d->resize(500, 400);
3128        d->run();
3129        if (d->fileWasChanged()) {
3130            // update GUI with new instrument just created
3131            add_instrument(d->newCombinedInstrument());
3132        }
3133        delete d;
3134    }
3135    
3136    void MainWindow::on_action_view_references() {
3137        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
3138        Gtk::TreeModel::iterator it = sel->get_selected();
3139        if (!it) return;
3140        Gtk::TreeModel::Row row = *it;
3141        gig::Sample* sample = row[m_SamplesModel.m_col_sample];
3142        if (!sample) return;
3143    
3144        ReferencesView* d = new ReferencesView(*this);
3145        d->setSample(sample);
3146        d->dimension_region_selected.connect(
3147            sigc::mem_fun(*this, &MainWindow::select_dimension_region)
3148        );
3149        d->show_all();
3150        d->resize(500, 400);
3151        d->run();
3152        delete d;
3153    }
3154    
3155    void MainWindow::mergeFiles(const std::vector<std::string>& filenames) {
3156        struct _Source {
3157            std::vector<RIFF::File*> riffs;
3158            std::vector<gig::File*> gigs;
3159            
3160            ~_Source() {
3161                for (int k = 0; k < gigs.size(); ++k) delete gigs[k];
3162                for (int k = 0; k < riffs.size(); ++k) delete riffs[k];
3163                riffs.clear();
3164                gigs.clear();
3165            }
3166        } sources;
3167    
3168        if (filenames.empty())
3169            throw RIFF::Exception(_("No files selected, so nothing done."));
3170    
3171        // first open all input files (to avoid output file corruption)
3172        int i;
3173        try {
3174            for (i = 0; i < filenames.size(); ++i) {
3175                const std::string& filename = filenames[i];
3176                printf("opening file=%s\n", filename.c_str());
3177    
3178                RIFF::File* riff = new RIFF::File(filename);
3179                sources.riffs.push_back(riff);
3180    
3181                gig::File* gig = new gig::File(riff);
3182                sources.gigs.push_back(gig);
3183            }
3184        } catch (RIFF::Exception e) {
3185            throw RIFF::Exception(
3186                _("Error occurred while opening '") +
3187                filenames[i] +
3188                "': " +
3189                e.Message
3190            );
3191        } catch (...) {
3192            throw RIFF::Exception(
3193                _("Unknown exception occurred while opening '") +
3194                filenames[i] + "'"
3195            );
3196        }
3197    
3198        // now merge the opened .gig files to the main .gig file currently being
3199        // open in gigedit
3200        try {
3201            for (i = 0; i < filenames.size(); ++i) {
3202                const std::string& filename = filenames[i];
3203                printf("merging file=%s\n", filename.c_str());
3204                assert(i < sources.gigs.size());
3205    
3206                this->file->AddContentOf(sources.gigs[i]);
3207            }
3208        } catch (RIFF::Exception e) {
3209            throw RIFF::Exception(
3210                _("Error occurred while merging '") +
3211                filenames[i] +
3212                "': " +
3213                e.Message
3214            );
3215        } catch (...) {
3216            throw RIFF::Exception(
3217                _("Unknown exception occurred while merging '") +
3218                filenames[i] + "'"
3219            );
3220        }
3221    
3222        // Finally save gig file persistently to disk ...
3223        //NOTE: requires that this gig file already has a filename !
3224        {
3225            std::cout << "Saving file\n" << std::flush;
3226            file_structure_to_be_changed_signal.emit(this->file);
3227    
3228            progress_dialog = new ProgressDialog( //FIXME: memory leak!
3229                _("Saving") +  Glib::ustring(" '") +
3230                Glib::filename_display_basename(this->filename) + "' ...",
3231                *this
3232            );
3233            progress_dialog->show_all();
3234            saver = new Saver(this->file); //FIXME: memory leak!
3235            saver->signal_progress().connect(
3236                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
3237            saver->signal_finished().connect(
3238                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
3239            saver->signal_error().connect(
3240                sigc::mem_fun(*this, &MainWindow::on_saver_error));
3241            saver->launch();
3242        }
3243    }
3244    
3245    void MainWindow::on_action_merge_files() {
3246        if (this->file->GetFileName().empty()) {
3247            Glib::ustring txt = _(
3248                "You seem to have a new .gig file open that has not been saved "
3249                "yet. You must save it somewhere before starting to merge it with "
3250                "other .gig files though, because during the merge operation the "
3251                "other files' sample data must be written on file level to the "
3252                "target .gig file."
3253            );
3254            Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
3255            msg.run();
3256            return;
3257        }
3258    
3259        Gtk::FileChooserDialog dialog(*this, _("Merge .gig files"));
3260        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
3261        dialog.add_button(_("Merge"), Gtk::RESPONSE_OK);
3262        dialog.set_default_response(Gtk::RESPONSE_CANCEL);
3263    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
3264        Gtk::FileFilter filter;
3265        filter.add_pattern("*.gig");
3266    #else
3267        Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
3268        filter->add_pattern("*.gig");
3269    #endif
3270        dialog.set_filter(filter);
3271        if (current_gig_dir != "") {
3272            dialog.set_current_folder(current_gig_dir);
3273        }
3274        dialog.set_select_multiple(true);
3275    
3276        // show warning in the file picker dialog
3277        Gtk::HBox descriptionArea;
3278        descriptionArea.set_spacing(15);
3279        Gtk::Image warningIcon;
3280        warningIcon.set_from_icon_name("dialog-warning",
3281                                       Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
3282        descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
3283    #if GTKMM_MAJOR_VERSION < 3
3284        view::WrapLabel description;
3285    #else
3286        Gtk::Label description;
3287        description.set_line_wrap();
3288    #endif
3289        description.set_markup(_(
3290            "\nSelect at least one .gig file that shall be merged to the .gig file "
3291            "currently being open in gigedit.\n\n"
3292            "<b>Please Note:</b> Merging with other files will modify your "
3293            "currently open .gig file on file level! And be aware that the current "
3294            "merge algorithm does not detect duplicate samples yet. So if you are "
3295            "merging files which are using equivalent sample data, those "
3296            "equivalent samples will currently be treated as separate samples and "
3297            "will accordingly be stored separately in the target .gig file!"
3298        ));
3299        descriptionArea.pack_start(description);
3300        dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
3301        descriptionArea.show_all();
3302    
3303        if (dialog.run() == Gtk::RESPONSE_OK) {
3304            printf("on_action_merge_files self=%p\n",
3305                   static_cast<void*>(Glib::Threads::Thread::self()));
3306            std::vector<std::string> filenames = dialog.get_filenames();
3307    
3308            // merge the selected files to the currently open .gig file
3309            try {
3310                mergeFiles(filenames);
3311            } catch (RIFF::Exception e) {
3312                Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
3313                msg.run();
3314            }
3315    
3316            // update GUI
3317            __refreshEntireGUI();
3318        }
3319    }
3320    
3321    void MainWindow::set_file_is_shared(bool b) {
3322        this->file_is_shared = b;
3323    
3324        if (file_is_shared) {
3325            m_AttachedStateLabel.set_label(_("live-mode"));
3326            m_AttachedStateImage.set(
3327                Gdk::Pixbuf::create_from_xpm_data(status_attached_xpm)
3328            );
3329        } else {
3330            m_AttachedStateLabel.set_label(_("stand-alone"));
3331            m_AttachedStateImage.set(
3332                Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)
3333            );
3334        }
3335    
3336        {
3337            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
3338                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
3339            if (item) item->set_sensitive(b);
3340        }
3341    }
3342    
3343    void MainWindow::on_sample_ref_count_incremented(gig::Sample* sample, int offset) {
3344        if (!sample) return;
3345        sample_ref_count[sample] += offset;
3346        const int refcount = sample_ref_count[sample];
3347    
3348        Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
3349        for (int g = 0; g < model->children().size(); ++g) {
3350            Gtk::TreeModel::Row rowGroup = model->children()[g];
3351            for (int s = 0; s < rowGroup.children().size(); ++s) {
3352                Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
3353                if (rowSample[m_SamplesModel.m_col_sample] != sample) continue;
3354                rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
3355                rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
3356            }
3357        }
3358    }
3359    
3360    void MainWindow::on_sample_ref_changed(gig::Sample* oldSample, gig::Sample* newSample) {
3361        on_sample_ref_count_incremented(oldSample, -1);
3362        on_sample_ref_count_incremented(newSample, +1);
3363    }
3364    
3365    void MainWindow::on_samples_to_be_removed(std::list<gig::Sample*> samples) {
3366        // just in case a new sample is added later with exactly the same memory
3367        // address, which would lead to incorrect refcount if not deleted here
3368        for (std::list<gig::Sample*>::const_iterator it = samples.begin();
3369             it != samples.end(); ++it)
3370        {
3371            sample_ref_count.erase(*it);
3372        }
3373    }
3374    
3375    void MainWindow::show_samples_tab() {
3376        m_TreeViewNotebook.set_current_page(0);
3377    }
3378    
3379    void MainWindow::show_intruments_tab() {
3380        m_TreeViewNotebook.set_current_page(1);
3381    }
3382    
3383    void MainWindow::show_scripts_tab() {
3384        m_TreeViewNotebook.set_current_page(2);
3385    }
3386    
3387    sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_to_be_changed() {
3388        return file_structure_to_be_changed_signal;
3389    }
3390    
3391    sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_changed() {
3392        return file_structure_changed_signal;
3393    }
3394    
3395    sigc::signal<void, std::list<gig::Sample*> >& MainWindow::signal_samples_to_be_removed() {
3396        return samples_to_be_removed_signal;
3397    }
3398    
3399    sigc::signal<void>& MainWindow::signal_samples_removed() {
3400        return samples_removed_signal;
3401    }
3402    
3403    sigc::signal<void, gig::Region*>& MainWindow::signal_region_to_be_changed() {
3404        return region_to_be_changed_signal;
3405    }
3406    
3407    sigc::signal<void, gig::Region*>& MainWindow::signal_region_changed() {
3408        return region_changed_signal;
3409    }
3410    
3411    sigc::signal<void, gig::Sample*>& MainWindow::signal_sample_changed() {
3412        return sample_changed_signal;
3413    }
3414    
3415    sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& MainWindow::signal_sample_ref_changed() {
3416        return sample_ref_changed_signal;
3417    }
3418    
3419    sigc::signal<void, gig::DimensionRegion*>& MainWindow::signal_dimreg_to_be_changed() {
3420        return dimreg_to_be_changed_signal;
3421    }
3422    
3423    sigc::signal<void, gig::DimensionRegion*>& MainWindow::signal_dimreg_changed() {
3424        return dimreg_changed_signal;
3425    }
3426    
3427    sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_note_on() {
3428        return note_on_signal;
3429    }
3430    
3431    sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_note_off() {
3432        return note_off_signal;
3433    }
3434    
3435    sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_hit() {
3436        return m_RegionChooser.signal_keyboard_key_hit();
3437    }
3438    
3439    sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {
3440        return m_RegionChooser.signal_keyboard_key_released();
3441    }
3442    
3443    sigc::signal<void, gig::Instrument*>& MainWindow::signal_switch_sampler_instrument() {
3444        return switch_sampler_instrument_signal;
3445    }

Legend:
Removed from v.1262  
changed lines
  Added in v.2845

  ViewVC Help
Powered by ViewVC