/[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 1799 by persson, Sat Dec 6 13:49:26 2008 UTC revision 3109 by schoenebeck, Sun Feb 12 16:35:03 2017 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (C) 2006 - 2008 Andreas Persson   * Copyright (C) 2006-2017 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 20  Line 20 
20  #include <iostream>  #include <iostream>
21  #include <cstring>  #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>  #include <gtkmm/toggleaction.h>
41    #if GTKMM_MAJOR_VERSION < 3
42  #include "wrapLabel.hh"  #include "wrapLabel.hh"
   
 #include "global.h"  
   
 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2  
 #define ABOUT_DIALOG  
 #include <gtkmm/aboutdialog.h>  
43  #endif  #endif
44    
45  #if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION < 6) || GLIBMM_MAJOR_VERSION < 2  #include "global.h"
46  namespace Glib {  #include "compat.h"
 Glib::ustring filename_display_basename(const std::string& filename)  
 {  
     gchar* gstr = g_path_get_basename(filename.c_str());  
     Glib::ustring str(gstr);  
     g_free(gstr);  
     return Glib::filename_to_utf8(str);  
 }  
 }  
 #endif  
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"  #include "../../gfx/status_attached.xpm"
59  #include "../../gfx/status_detached.xpm"  #include "../../gfx/status_detached.xpm"
60    #include "gfx/builtinpix.h"
 template<class T> inline std::string ToString(T o) {  
     std::stringstream ss;  
     ss << o;  
     return ss.str();  
 }  
   
 Table::Table(int x, int y) : Gtk::Table(x, y), rowno(0) {  }  
   
 void Table::add(BoolEntry& boolentry)  
 {  
     attach(boolentry.widget, 0, 2, rowno, rowno + 1,  
            Gtk::FILL, Gtk::SHRINK);  
     rowno++;  
 }  
   
 void Table::add(BoolEntryPlus6& boolentry)  
 {  
     attach(boolentry.widget, 0, 2, rowno, rowno + 1,  
            Gtk::FILL, Gtk::SHRINK);  
     rowno++;  
 }  
   
 void Table::add(LabelWidget& prop)  
 {  
     attach(prop.label, 1, 2, rowno, rowno + 1,  
            Gtk::FILL, Gtk::SHRINK);  
     attach(prop.widget, 2, 3, rowno, rowno + 1,  
            Gtk::EXPAND | Gtk::FILL, Gtk::SHRINK);  
     rowno++;  
 }  
61    
62  MainWindow::MainWindow() :  MainWindow::MainWindow() :
63        m_DimRegionChooser(*this),
64      dimreg_label(_("Changes apply to:")),      dimreg_label(_("Changes apply to:")),
65      dimreg_all_regions(_("all regions")),      dimreg_all_regions(_("all regions")),
66      dimreg_all_dimregs(_("all dimension splits")),      dimreg_all_dimregs(_("all dimension splits")),
67      dimreg_stereo(_("both channels"))      dimreg_stereo(_("both channels")),
68        labelLegend(_("Legend:")),
69        labelNoSample(_(" No Sample")),
70        labelMissingSample(_(" Missing some Sample(s)")),
71        labelLooped(_(" Looped")),
72        labelSomeLoops(_(" Some Loop(s)"))
73  {  {
74        loadBuiltInPix();
75    
76  //    set_border_width(5);  //    set_border_width(5);
77  //    set_default_size(400, 200);  //    set_default_size(400, 200);
78    
   
79      add(m_VBox);      add(m_VBox);
80    
81      // Handle selection      // Handle selection
82      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      m_TreeView.get_selection()->signal_changed().connect(
     tree_sel_ref->signal_changed().connect(  
83          sigc::mem_fun(*this, &MainWindow::on_sel_change));          sigc::mem_fun(*this, &MainWindow::on_sel_change));
84    
85      // m_TreeView.set_reorderable();      // m_TreeView.set_reorderable();
# Line 116  MainWindow::MainWindow() : Line 95  MainWindow::MainWindow() :
95      m_ScrolledWindowSamples.add(m_TreeViewSamples);      m_ScrolledWindowSamples.add(m_TreeViewSamples);
96      m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
97    
98        m_ScrolledWindowScripts.add(m_TreeViewScripts);
99        m_ScrolledWindowScripts.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
100    
101    
102      m_TreeViewNotebook.set_size_request(300);      m_TreeViewNotebook.set_size_request(300);
103    
# Line 127  MainWindow::MainWindow() : Line 109  MainWindow::MainWindow() :
109      dimreg_hbox.add(dimreg_stereo);      dimreg_hbox.add(dimreg_stereo);
110      dimreg_vbox.add(dimreg_edit);      dimreg_vbox.add(dimreg_edit);
111      dimreg_vbox.pack_start(dimreg_hbox, Gtk::PACK_SHRINK);      dimreg_vbox.pack_start(dimreg_hbox, Gtk::PACK_SHRINK);
112      m_HPaned.add2(dimreg_vbox);      {
113            legend_hbox.add(labelLegend);
114    
115            imageNoSample.set(redDot);
116            imageNoSample.set_alignment(Gtk::ALIGN_END);
117            labelNoSample.set_alignment(Gtk::ALIGN_START);
118            legend_hbox.add(imageNoSample);
119            legend_hbox.add(labelNoSample);
120    
121            imageMissingSample.set(yellowDot);
122            imageMissingSample.set_alignment(Gtk::ALIGN_END);
123            labelMissingSample.set_alignment(Gtk::ALIGN_START);
124            legend_hbox.add(imageMissingSample);
125            legend_hbox.add(labelMissingSample);
126    
127            imageLooped.set(blackLoop);
128            imageLooped.set_alignment(Gtk::ALIGN_END);
129            labelLooped.set_alignment(Gtk::ALIGN_START);
130            legend_hbox.add(imageLooped);
131            legend_hbox.add(labelLooped);
132    
133            imageSomeLoops.set(grayLoop);
134            imageSomeLoops.set_alignment(Gtk::ALIGN_END);
135            labelSomeLoops.set_alignment(Gtk::ALIGN_START);
136            legend_hbox.add(imageSomeLoops);
137            legend_hbox.add(labelSomeLoops);
138    
139      m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");          legend_hbox.show_all_children();
140      m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");      }
141        dimreg_vbox.pack_start(legend_hbox, Gtk::PACK_SHRINK);
142        m_HPaned.add2(dimreg_vbox);
143    
144        dimreg_label.set_tooltip_text(_("To automatically apply your changes above globally to the entire instrument, check all 3 check boxes on the right."));
145        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."));
146        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."));
147        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)."));
148    
149        m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, _("Samples"));
150        m_TreeViewNotebook.append_page(m_ScrolledWindow, _("Instruments"));
151        m_TreeViewNotebook.append_page(m_ScrolledWindowScripts, _("Scripts"));
152    
153      actionGroup = Gtk::ActionGroup::create();      actionGroup = Gtk::ActionGroup::create();
154    
155      actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));      actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
156      actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW),      actionGroup->add(Gtk::Action::create("New", _("_New")),
157                         Gtk::AccelKey("<control>n"),
158                       sigc::mem_fun(                       sigc::mem_fun(
159                           *this, &MainWindow::on_action_file_new));                           *this, &MainWindow::on_action_file_new));
160      Glib::RefPtr<Gtk::Action> action =      actionGroup->add(Gtk::Action::create("Open", _("_Open...")),
161          Gtk::Action::create("Open", Gtk::Stock::OPEN);                       Gtk::AccelKey("<control>o"),
     action->property_label() = action->property_label() + "...";  
     actionGroup->add(action,  
162                       sigc::mem_fun(                       sigc::mem_fun(
163                           *this, &MainWindow::on_action_file_open));                           *this, &MainWindow::on_action_file_open));
164      actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE),      actionGroup->add(Gtk::Action::create("Save", _("_Save")),
165                         Gtk::AccelKey("<control>s"),
166                       sigc::mem_fun(                       sigc::mem_fun(
167                           *this, &MainWindow::on_action_file_save));                           *this, &MainWindow::on_action_file_save));
168      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,  
169                       Gtk::AccelKey("<shift><control>s"),                       Gtk::AccelKey("<shift><control>s"),
170                       sigc::mem_fun(                       sigc::mem_fun(
171                           *this, &MainWindow::on_action_file_save_as));                           *this, &MainWindow::on_action_file_save_as));
172      actionGroup->add(Gtk::Action::create("Properties",      actionGroup->add(Gtk::Action::create("Properties",
173                                           Gtk::Stock::PROPERTIES),                                           _("_Properties")),
174                       sigc::mem_fun(                       sigc::mem_fun(
175                           *this, &MainWindow::on_action_file_properties));                           *this, &MainWindow::on_action_file_properties));
176      actionGroup->add(Gtk::Action::create("InstrProperties",      actionGroup->add(Gtk::Action::create("InstrProperties",
177                                           Gtk::Stock::PROPERTIES),                                           _("_Properties")),
178                       sigc::mem_fun(                       sigc::mem_fun(
179                           *this, &MainWindow::show_instr_props));                           *this, &MainWindow::show_instr_props));
180      actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),      actionGroup->add(Gtk::Action::create("MidiRules",
181                                             _("_Midi Rules...")),
182                         sigc::mem_fun(
183                             *this, &MainWindow::show_midi_rules));
184        actionGroup->add(Gtk::Action::create("ScriptSlots",
185                                             _("_Script Slots...")),
186                         sigc::mem_fun(
187                             *this, &MainWindow::show_script_slots));
188        actionGroup->add(Gtk::Action::create("Quit", _("_Quit")),
189                         Gtk::AccelKey("<control>q"),
190                       sigc::mem_fun(                       sigc::mem_fun(
191                           *this, &MainWindow::on_action_quit));                           *this, &MainWindow::on_action_quit));
192      actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument")));      actionGroup->add(
193            Gtk::Action::create("MenuSample", _("_Sample")),
194            sigc::mem_fun(*this, &MainWindow::show_samples_tab)
195        );
196        actionGroup->add(
197            Gtk::Action::create("MenuInstrument", _("_Instrument")),
198            sigc::mem_fun(*this, &MainWindow::show_intruments_tab)
199        );
200        actionGroup->add(
201            Gtk::Action::create("MenuScript", _("S_cript")),
202            sigc::mem_fun(*this, &MainWindow::show_scripts_tab)
203        );
204        actionGroup->add(Gtk::Action::create("AllInstruments", _("_Select")));
205    
206        actionGroup->add(Gtk::Action::create("MenuEdit", _("_Edit")));
207    
     actionGroup->add(Gtk::Action::create("MenuView", _("_View")));  
208      Glib::RefPtr<Gtk::ToggleAction> toggle_action =      Glib::RefPtr<Gtk::ToggleAction> toggle_action =
209            Gtk::ToggleAction::create("CopySampleUnity", _("Copy Sample's _Unity Note"));
210        toggle_action->set_active(true);
211        actionGroup->add(toggle_action);
212    
213        toggle_action =
214            Gtk::ToggleAction::create("CopySampleTune", _("Copy Sample's _Fine Tune"));
215        toggle_action->set_active(true);
216        actionGroup->add(toggle_action);
217    
218        toggle_action =
219            Gtk::ToggleAction::create("CopySampleLoop", _("Copy Sample's _Loop Points"));
220        toggle_action->set_active(true);
221        actionGroup->add(toggle_action);
222    
223    
224        actionGroup->add(Gtk::Action::create("MenuView", _("_View")));
225        toggle_action =
226          Gtk::ToggleAction::create("Statusbar", _("_Statusbar"));          Gtk::ToggleAction::create("Statusbar", _("_Statusbar"));
227      toggle_action->set_active(true);      toggle_action->set_active(true);
228      actionGroup->add(toggle_action,      actionGroup->add(toggle_action,
229                       sigc::mem_fun(                       sigc::mem_fun(
230                           *this, &MainWindow::on_action_view_status_bar));                           *this, &MainWindow::on_action_view_status_bar));
231    
232      action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);      toggle_action =
233      actionGroup->add(Gtk::Action::create("MenuHelp",          Gtk::ToggleAction::create("AutoRestoreWinDim", _("_Auto Restore Window Dimension"));
234                                           action->property_label()));      toggle_action->set_active(Settings::singleton()->autoRestoreWindowDimension);
235  #ifdef ABOUT_DIALOG      actionGroup->add(toggle_action,
236      actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT),                       sigc::mem_fun(
237                             *this, &MainWindow::on_auto_restore_win_dim));
238    
239        toggle_action =
240            Gtk::ToggleAction::create("SaveWithTemporaryFile", _("Save with _temporary file"));
241        toggle_action->set_active(Settings::singleton()->saveWithTemporaryFile);
242        actionGroup->add(toggle_action,
243                         sigc::mem_fun(
244                             *this, &MainWindow::on_save_with_temporary_file));
245    
246        actionGroup->add(
247            Gtk::Action::create("RefreshAll", _("_Refresh All")),
248            sigc::mem_fun(*this, &MainWindow::on_action_refresh_all)
249        );                
250    
251        actionGroup->add(Gtk::Action::create("MenuHelp", _("_Help")));
252        actionGroup->add(Gtk::Action::create("About", _("_About")),
253                       sigc::mem_fun(                       sigc::mem_fun(
254                           *this, &MainWindow::on_action_help_about));                           *this, &MainWindow::on_action_help_about));
 #endif  
255      actionGroup->add(      actionGroup->add(
256          Gtk::Action::create("AddInstrument", _("Add _Instrument")),          Gtk::Action::create("AddInstrument", _("Add _Instrument")),
257          sigc::mem_fun(*this, &MainWindow::on_action_add_instrument)          sigc::mem_fun(*this, &MainWindow::on_action_add_instrument)
258      );      );
259      actionGroup->add(      actionGroup->add(
260          Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE),          Gtk::Action::create("DupInstrument", _("_Duplicate Instrument")),
261            sigc::mem_fun(*this, &MainWindow::on_action_duplicate_instrument)
262        );
263        actionGroup->add(
264            Gtk::Action::create("RemoveInstrument", _("_Remove")),
265          sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument)          sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument)
266      );      );
267    
268    
269        actionGroup->add(Gtk::Action::create("MenuSettings", _("_Settings")));
270        
271        toggle_action =
272            Gtk::ToggleAction::create("WarnUserOnExtensions", _("Show warning on format _extensions"));
273        toggle_action->set_active(Settings::singleton()->warnUserOnExtensions);
274        actionGroup->add(
275            toggle_action,
276            sigc::mem_fun(*this, &MainWindow::on_action_warn_user_on_extensions)
277        );
278    
279        toggle_action =
280            Gtk::ToggleAction::create("SyncSamplerInstrumentSelection", _("Synchronize sampler's instrument selection"));
281        toggle_action->set_active(Settings::singleton()->syncSamplerInstrumentSelection);
282        actionGroup->add(
283            toggle_action,
284            sigc::mem_fun(*this, &MainWindow::on_action_sync_sampler_instrument_selection)
285        );
286    
287        toggle_action =
288            Gtk::ToggleAction::create("MoveRootNoteWithRegionMoved", _("Move root note with region moved"));
289        toggle_action->set_active(Settings::singleton()->moveRootNoteWithRegionMoved);
290        actionGroup->add(
291            toggle_action,
292            sigc::mem_fun(*this, &MainWindow::on_action_move_root_note_with_region_moved)
293        );
294    
295    
296        actionGroup->add(Gtk::Action::create("MenuTools", _("_Tools")));
297    
298        actionGroup->add(
299            Gtk::Action::create("CombineInstruments", _("_Combine Instruments...")),
300            sigc::mem_fun(*this, &MainWindow::on_action_combine_instruments)
301        );
302    
303        actionGroup->add(
304            Gtk::Action::create("MergeFiles", _("_Merge Files...")),
305            sigc::mem_fun(*this, &MainWindow::on_action_merge_files)
306        );
307    
308    
309      // sample right-click popup actions      // sample right-click popup actions
310      actionGroup->add(      actionGroup->add(
311          Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES),          Gtk::Action::create("SampleProperties", _("_Properties")),
312          sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)          sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)
313      );      );
314      actionGroup->add(      actionGroup->add(
# Line 207  MainWindow::MainWindow() : Line 320  MainWindow::MainWindow() :
320          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)          sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
321      );      );
322      actionGroup->add(      actionGroup->add(
323          Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),          Gtk::Action::create("RemoveSample", _("_Remove")),
324          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
325      );      );
326      actionGroup->add(      actionGroup->add(
327            Gtk::Action::create("RemoveUnusedSamples", _("Remove _Unused Samples")),
328            sigc::mem_fun(*this, &MainWindow::on_action_remove_unused_samples)
329        );
330        actionGroup->add(
331            Gtk::Action::create("ShowSampleRefs", _("Show References...")),
332            sigc::mem_fun(*this, &MainWindow::on_action_view_references)
333        );
334        actionGroup->add(
335            Gtk::Action::create("ReplaceSample",
336                                _("Replace Sample...")),
337            sigc::mem_fun(*this, &MainWindow::on_action_replace_sample)
338        );
339        actionGroup->add(
340          Gtk::Action::create("ReplaceAllSamplesInAllGroups",          Gtk::Action::create("ReplaceAllSamplesInAllGroups",
341                              _("Replace All Samples in All Groups...")),                              _("Replace All Samples in All Groups...")),
342          sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups)          sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups)
343      );      );
344        
345        // script right-click popup actions
346        actionGroup->add(
347            Gtk::Action::create("AddScriptGroup", _("Add _Group")),
348            sigc::mem_fun(*this, &MainWindow::on_action_add_script_group)
349        );
350        actionGroup->add(
351            Gtk::Action::create("AddScript", _("Add _Script")),
352            sigc::mem_fun(*this, &MainWindow::on_action_add_script)
353        );
354        actionGroup->add(
355            Gtk::Action::create("EditScript", _("_Edit Script...")),
356            sigc::mem_fun(*this, &MainWindow::on_action_edit_script)
357        );
358        actionGroup->add(
359            Gtk::Action::create("RemoveScript", _("_Remove")),
360            sigc::mem_fun(*this, &MainWindow::on_action_remove_script)
361        );
362    
363      uiManager = Gtk::UIManager::create();      uiManager = Gtk::UIManager::create();
364      uiManager->insert_action_group(actionGroup);      uiManager->insert_action_group(actionGroup);
# Line 234  MainWindow::MainWindow() : Line 378  MainWindow::MainWindow() :
378          "      <separator/>"          "      <separator/>"
379          "      <menuitem action='Quit'/>"          "      <menuitem action='Quit'/>"
380          "    </menu>"          "    </menu>"
381            "    <menu action='MenuEdit'>"
382            "      <menuitem action='CopySampleUnity'/>"
383            "      <menuitem action='CopySampleTune'/>"
384            "      <menuitem action='CopySampleLoop'/>"
385            "    </menu>"
386            "    <menu action='MenuSample'>"
387            "      <menuitem action='SampleProperties'/>"
388            "      <menuitem action='AddGroup'/>"
389            "      <menuitem action='AddSample'/>"
390            "      <menuitem action='ShowSampleRefs'/>"
391            "      <menuitem action='ReplaceSample' />"
392            "      <menuitem action='ReplaceAllSamplesInAllGroups' />"
393            "      <separator/>"
394            "      <menuitem action='RemoveSample'/>"
395            "      <menuitem action='RemoveUnusedSamples'/>"
396            "    </menu>"
397          "    <menu action='MenuInstrument'>"          "    <menu action='MenuInstrument'>"
398            "      <menu action='AllInstruments'>"
399            "      </menu>"
400            "      <separator/>"
401            "      <menuitem action='InstrProperties'/>"
402            "      <menuitem action='MidiRules'/>"
403            "      <menuitem action='ScriptSlots'/>"
404            "      <menuitem action='AddInstrument'/>"
405            "      <menuitem action='DupInstrument'/>"
406            "      <separator/>"
407            "      <menuitem action='RemoveInstrument'/>"
408            "    </menu>"
409            "    <menu action='MenuScript'>"
410            "      <menuitem action='AddScriptGroup'/>"
411            "      <menuitem action='AddScript'/>"
412            "      <menuitem action='EditScript'/>"
413            "      <separator/>"
414            "      <menuitem action='RemoveScript'/>"
415          "    </menu>"          "    </menu>"
416          "    <menu action='MenuView'>"          "    <menu action='MenuView'>"
417          "      <menuitem action='Statusbar'/>"          "      <menuitem action='Statusbar'/>"
418            "      <menuitem action='AutoRestoreWinDim'/>"
419            "      <separator/>"
420            "      <menuitem action='RefreshAll'/>"
421            "    </menu>"
422            "    <menu action='MenuTools'>"
423            "      <menuitem action='CombineInstruments'/>"
424            "      <menuitem action='MergeFiles'/>"
425            "    </menu>"
426            "    <menu action='MenuSettings'>"
427            "      <menuitem action='WarnUserOnExtensions'/>"
428            "      <menuitem action='SyncSamplerInstrumentSelection'/>"
429            "      <menuitem action='MoveRootNoteWithRegionMoved'/>"
430            "      <menuitem action='SaveWithTemporaryFile'/>"
431          "    </menu>"          "    </menu>"
 #ifdef ABOUT_DIALOG  
432          "    <menu action='MenuHelp'>"          "    <menu action='MenuHelp'>"
433          "      <menuitem action='About'/>"          "      <menuitem action='About'/>"
434          "    </menu>"          "    </menu>"
 #endif  
435          "  </menubar>"          "  </menubar>"
436          "  <popup name='PopupMenu'>"          "  <popup name='PopupMenu'>"
437          "    <menuitem action='InstrProperties'/>"          "    <menuitem action='InstrProperties'/>"
438            "    <menuitem action='MidiRules'/>"
439            "    <menuitem action='ScriptSlots'/>"
440          "    <menuitem action='AddInstrument'/>"          "    <menuitem action='AddInstrument'/>"
441            "    <menuitem action='DupInstrument'/>"
442          "    <separator/>"          "    <separator/>"
443          "    <menuitem action='RemoveInstrument'/>"          "    <menuitem action='RemoveInstrument'/>"
444          "  </popup>"          "  </popup>"
# Line 255  MainWindow::MainWindow() : Line 446  MainWindow::MainWindow() :
446          "    <menuitem action='SampleProperties'/>"          "    <menuitem action='SampleProperties'/>"
447          "    <menuitem action='AddGroup'/>"          "    <menuitem action='AddGroup'/>"
448          "    <menuitem action='AddSample'/>"          "    <menuitem action='AddSample'/>"
449          "    <menuitem action='ReplaceAllSamplesInAllGroups' />"          "    <menuitem action='ShowSampleRefs'/>"
450            "    <menuitem action='ReplaceSample' />"
451            "    <menuitem action='ReplaceAllSamplesInAllGroups' />"
452          "    <separator/>"          "    <separator/>"
453          "    <menuitem action='RemoveSample'/>"          "    <menuitem action='RemoveSample'/>"
454            "    <menuitem action='RemoveUnusedSamples'/>"
455            "  </popup>"
456            "  <popup name='ScriptPopupMenu'>"
457            "    <menuitem action='AddScriptGroup'/>"
458            "    <menuitem action='AddScript'/>"
459            "    <menuitem action='EditScript'/>"
460            "    <separator/>"
461            "    <menuitem action='RemoveScript'/>"
462          "  </popup>"          "  </popup>"
463          "</ui>";          "</ui>";
464      uiManager->add_ui_from_string(ui_info);      uiManager->add_ui_from_string(ui_info);
465    
466      popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));      popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));
467        
468        // Set tooltips for menu items (for some reason, setting a tooltip on the
469        // respective Gtk::Action objects above will simply be ignored, no matter
470        // if using Gtk::Action::set_tooltip() or passing the tooltip string on
471        // Gtk::Action::create()).
472        {
473            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
474                uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity"));
475            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."));
476        }
477        {
478            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
479                uiManager->get_widget("/MenuBar/MenuEdit/CopySampleTune"));
480            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."));
481        }
482        {
483            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
484                uiManager->get_widget("/MenuBar/MenuEdit/CopySampleLoop"));
485            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."));
486        }
487        {
488            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
489                uiManager->get_widget("/MenuBar/MenuSettings/WarnUserOnExtensions"));
490            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."));
491        }
492        {
493            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
494                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
495            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)."));
496        }
497        {
498            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
499                uiManager->get_widget("/MenuBar/MenuSettings/MoveRootNoteWithRegionMoved"));
500            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."));
501        }
502        {
503            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
504                uiManager->get_widget("/MenuBar/MenuSample/RemoveUnusedSamples"));
505            item->set_tooltip_text(_("Removes all samples that are not referenced by any instrument (i.e. red ones)."));
506            // copy tooltip to popup menu
507            Gtk::MenuItem* item2 = dynamic_cast<Gtk::MenuItem*>(
508                uiManager->get_widget("/SamplePopupMenu/RemoveUnusedSamples"));
509            item2->set_tooltip_text(item->get_tooltip_text());
510        }
511        {
512            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
513                uiManager->get_widget("/MenuBar/MenuView/RefreshAll"));
514            item->set_tooltip_text(_("Reloads the currently open gig file and updates the entire graphical user interface."));
515        }
516        {
517            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
518                uiManager->get_widget("/MenuBar/MenuView/AutoRestoreWinDim"));
519            item->set_tooltip_text(_("If checked, size and position of all windows will be saved and automatically restored next time."));
520        }
521        {
522            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
523                uiManager->get_widget("/MenuBar/MenuTools/CombineInstruments"));
524            item->set_tooltip_text(_("Create combi sounds out of individual sounds of this .gig file."));
525        }
526        {
527            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
528                uiManager->get_widget("/MenuBar/MenuTools/MergeFiles"));
529            item->set_tooltip_text(_("Add instruments and samples of other .gig files to this .gig file."));
530        }
531    
532    
533        instrument_menu = static_cast<Gtk::MenuItem*>(
534            uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments"))->get_submenu();
535    
536      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
537      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);      m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
# Line 272  MainWindow::MainWindow() : Line 541  MainWindow::MainWindow() :
541      m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);      m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);
542      m_VBox.pack_start(m_StatusBar, Gtk::PACK_SHRINK);      m_VBox.pack_start(m_StatusBar, Gtk::PACK_SHRINK);
543    
544        set_file_is_shared(false);
545    
546      // Status Bar:      // Status Bar:
547      m_StatusBar.pack_start(m_AttachedStateLabel, Gtk::PACK_SHRINK);      m_StatusBar.pack_start(m_AttachedStateLabel, Gtk::PACK_SHRINK);
548      m_StatusBar.pack_start(m_AttachedStateImage, Gtk::PACK_SHRINK);      m_StatusBar.pack_start(m_AttachedStateImage, Gtk::PACK_SHRINK);
# Line 286  MainWindow::MainWindow() : Line 557  MainWindow::MainWindow() :
557      // Create the Tree model:      // Create the Tree model:
558      m_refTreeModel = Gtk::ListStore::create(m_Columns);      m_refTreeModel = Gtk::ListStore::create(m_Columns);
559      m_TreeView.set_model(m_refTreeModel);      m_TreeView.set_model(m_refTreeModel);
560      m_refTreeModel->signal_row_changed().connect(      m_TreeView.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
561        m_TreeView.set_tooltip_text(_("Right click here for actions on instruments & MIDI Rules. Drag & drop to change the order of instruments."));
562        instrument_name_connection = m_refTreeModel->signal_row_changed().connect(
563          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)          sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
564      );      );
565    
566      // Add the TreeView's view columns:      // Add the TreeView's view columns:
567      m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);      m_TreeView.append_column(_("Nr"), m_Columns.m_col_nr);
568      m_TreeView.set_headers_visible(false);      m_TreeView.append_column_editable(_("Instrument"), m_Columns.m_col_name);
569        m_TreeView.set_headers_visible(true);
570        
571        // establish drag&drop within the instrument tree view, allowing to reorder
572        // the sequence of instruments within the gig file
573        {
574            std::vector<Gtk::TargetEntry> drag_target_instrument;
575            drag_target_instrument.push_back(Gtk::TargetEntry("gig::Instrument"));
576            m_TreeView.drag_source_set(drag_target_instrument);
577            m_TreeView.drag_dest_set(drag_target_instrument);
578            m_TreeView.signal_drag_begin().connect(
579                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_begin)
580            );
581            m_TreeView.signal_drag_data_get().connect(
582                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_data_get)
583            );
584            m_TreeView.signal_drag_data_received().connect(
585                sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drop_drag_data_received)
586            );
587        }
588    
589      // create samples treeview (including its data model)      // create samples treeview (including its data model)
590      m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);      m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);
591      m_TreeViewSamples.set_model(m_refSamplesTreeModel);      m_TreeViewSamples.set_model(m_refSamplesTreeModel);
592        m_TreeViewSamples.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
593        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."));
594      // m_TreeViewSamples.set_reorderable();      // m_TreeViewSamples.set_reorderable();
595      m_TreeViewSamples.append_column_editable("Samples", m_SamplesModel.m_col_name);      m_TreeViewSamples.append_column_editable(_("Name"), m_SamplesModel.m_col_name);
596      m_TreeViewSamples.set_headers_visible(false);      m_TreeViewSamples.append_column(_("Referenced"), m_SamplesModel.m_col_refcount);
597        {
598            Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(0);
599            Gtk::CellRendererText* cellrenderer =
600                dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell());
601            column->add_attribute(
602                cellrenderer->property_foreground(), m_SamplesModel.m_color
603            );
604        }
605        {
606            Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(1);
607            Gtk::CellRendererText* cellrenderer =
608                dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell());
609            column->add_attribute(
610                cellrenderer->property_foreground(), m_SamplesModel.m_color
611            );
612        }
613        m_TreeViewSamples.set_headers_visible(true);
614      m_TreeViewSamples.signal_button_press_event().connect_notify(      m_TreeViewSamples.signal_button_press_event().connect_notify(
615          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
616      );      );
# Line 307  MainWindow::MainWindow() : Line 618  MainWindow::MainWindow() :
618          sigc::mem_fun(*this, &MainWindow::sample_name_changed)          sigc::mem_fun(*this, &MainWindow::sample_name_changed)
619      );      );
620    
621        // create scripts treeview (including its data model)
622        m_refScriptsTreeModel = ScriptsTreeStore::create(m_ScriptsModel);
623        m_TreeViewScripts.set_model(m_refScriptsTreeModel);
624        m_TreeViewScripts.set_tooltip_text(_(
625            "Use CTRL + double click for editing a script."
626            "\n\n"
627            "Note: instrument scripts are a LinuxSampler extension of the gig "
628            "format. This feature will not work with the GigaStudio software!"
629        ));
630        // m_TreeViewScripts.set_reorderable();
631        m_TreeViewScripts.append_column_editable("Samples", m_ScriptsModel.m_col_name);
632        m_TreeViewScripts.set_headers_visible(false);
633        m_TreeViewScripts.signal_button_press_event().connect_notify(
634            sigc::mem_fun(*this, &MainWindow::on_script_treeview_button_release)
635        );
636        //FIXME: why the heck does this double click signal_row_activated() only fire while CTRL key is pressed ?
637        m_TreeViewScripts.signal_row_activated().connect(
638            sigc::mem_fun(*this, &MainWindow::script_double_clicked)
639        );
640        m_refScriptsTreeModel->signal_row_changed().connect(
641            sigc::mem_fun(*this, &MainWindow::script_name_changed)
642        );
643    
644        // establish drag&drop between scripts tree view and ScriptSlots window
645        std::vector<Gtk::TargetEntry> drag_target_gig_script;
646        drag_target_gig_script.push_back(Gtk::TargetEntry("gig::Script"));
647        m_TreeViewScripts.drag_source_set(drag_target_gig_script);
648        m_TreeViewScripts.signal_drag_begin().connect(
649            sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_begin)
650        );
651        m_TreeViewScripts.signal_drag_data_get().connect(
652            sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_data_get)
653        );
654    
655      // 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
656      std::list<Gtk::TargetEntry> drag_target_gig_sample;      std::vector<Gtk::TargetEntry> drag_target_gig_sample;
657      drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") );      drag_target_gig_sample.push_back(Gtk::TargetEntry("gig::Sample"));
658      m_TreeViewSamples.drag_source_set(drag_target_gig_sample);      m_TreeViewSamples.drag_source_set(drag_target_gig_sample);
659      m_TreeViewSamples.signal_drag_begin().connect(      m_TreeViewSamples.signal_drag_begin().connect(
660          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_begin)          sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_begin)
# Line 325  MainWindow::MainWindow() : Line 670  MainWindow::MainWindow() :
670          sigc::hide(sigc::mem_fun(*this, &MainWindow::file_changed)));          sigc::hide(sigc::mem_fun(*this, &MainWindow::file_changed)));
671      m_RegionChooser.signal_instrument_changed().connect(      m_RegionChooser.signal_instrument_changed().connect(
672          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::mem_fun(*this, &MainWindow::file_changed));
673        m_RegionChooser.signal_instrument_changed().connect(
674            sigc::mem_fun(*this, &MainWindow::region_changed));
675      m_DimRegionChooser.signal_region_changed().connect(      m_DimRegionChooser.signal_region_changed().connect(
676          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::mem_fun(*this, &MainWindow::file_changed));
677      instrumentProps.signal_instrument_changed().connect(      instrumentProps.signal_changed().connect(
678          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::mem_fun(*this, &MainWindow::file_changed));
679      propDialog.signal_info_changed().connect(      propDialog.signal_changed().connect(
680            sigc::mem_fun(*this, &MainWindow::file_changed));
681        midiRules.signal_changed().connect(
682          sigc::mem_fun(*this, &MainWindow::file_changed));          sigc::mem_fun(*this, &MainWindow::file_changed));
683    
684      dimreg_edit.signal_dimreg_to_be_changed().connect(      dimreg_edit.signal_dimreg_to_be_changed().connect(
# Line 338  MainWindow::MainWindow() : Line 687  MainWindow::MainWindow() :
687          dimreg_changed_signal.make_slot());          dimreg_changed_signal.make_slot());
688      dimreg_edit.signal_sample_ref_changed().connect(      dimreg_edit.signal_sample_ref_changed().connect(
689          sample_ref_changed_signal.make_slot());          sample_ref_changed_signal.make_slot());
690        sample_ref_changed_signal.connect(
691            sigc::mem_fun(*this, &MainWindow::on_sample_ref_changed)
692        );
693        samples_to_be_removed_signal.connect(
694            sigc::mem_fun(*this, &MainWindow::on_samples_to_be_removed)
695        );
696    
697        dimreg_edit.signal_select_sample().connect(
698            sigc::mem_fun(*this, &MainWindow::select_sample)
699        );
700    
701      m_RegionChooser.signal_instrument_struct_to_be_changed().connect(      m_RegionChooser.signal_instrument_struct_to_be_changed().connect(
702          sigc::hide(          sigc::hide(
# Line 374  MainWindow::MainWindow() : Line 733  MainWindow::MainWindow() :
733    
734      file = 0;      file = 0;
735      file_is_changed = false;      file_is_changed = false;
     set_file_is_shared(false);  
736    
737      show_all_children();      show_all_children();
738    
739      // start with a new gig file by default      // start with a new gig file by default
740      on_action_file_new();      on_action_file_new();
741    
742        // select 'Instruments' tab by default
743        // (gtk allows this only if the tab childs are visible, thats why it's here)
744        m_TreeViewNotebook.set_current_page(1);
745  }  }
746    
747  MainWindow::~MainWindow()  MainWindow::~MainWindow()
# Line 405  void MainWindow::region_changed() Line 767  void MainWindow::region_changed()
767  gig::Instrument* MainWindow::get_instrument()  gig::Instrument* MainWindow::get_instrument()
768  {  {
769      gig::Instrument* instrument = 0;      gig::Instrument* instrument = 0;
770      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      std::vector<Gtk::TreeModel::Path> rows = m_TreeView.get_selection()->get_selected_rows();
771        if (rows.empty()) return NULL;
772      Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();      Gtk::TreeModel::const_iterator it = m_refTreeModel->get_iter(rows[0]);
773      if (it) {      if (it) {
774          Gtk::TreeModel::Row row = *it;          Gtk::TreeModel::Row row = *it;
775          instrument = row[m_Columns.m_col_instr];          instrument = row[m_Columns.m_col_instr];
# Line 450  void MainWindow::update_dimregs() Line 812  void MainWindow::update_dimregs()
812              add_region_to_dimregs(region, stereo, all_dimregs);              add_region_to_dimregs(region, stereo, all_dimregs);
813          }          }
814      }      }
815    
816        m_DimRegionChooser.setModifyAllRegions(all_regions);
817        m_DimRegionChooser.setModifyAllDimensionRegions(all_dimregs);
818        m_DimRegionChooser.setModifyBothChannels(stereo);
819  }  }
820    
821  void MainWindow::dimreg_all_dimregs_toggled()  void MainWindow::dimreg_all_dimregs_toggled()
# Line 461  void MainWindow::dimreg_all_dimregs_togg Line 827  void MainWindow::dimreg_all_dimregs_togg
827  void MainWindow::dimreg_changed()  void MainWindow::dimreg_changed()
828  {  {
829      update_dimregs();      update_dimregs();
830      dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());      dimreg_edit.set_dim_region(m_DimRegionChooser.get_main_dimregion());
831  }  }
832    
833  void MainWindow::on_sel_change()  void MainWindow::on_sel_change()
834  {  {
835        // select item in instrument menu
836        std::vector<Gtk::TreeModel::Path> rows = m_TreeView.get_selection()->get_selected_rows();
837        if (!rows.empty()) {
838            Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(rows[0]);
839            if (it) {
840                Gtk::TreePath path(it);
841                int index = path[0];
842                const std::vector<Gtk::Widget*> children =
843                    instrument_menu->get_children();
844                static_cast<Gtk::RadioMenuItem*>(children[index])->set_active();
845            }
846        }
847    
848      m_RegionChooser.set_instrument(get_instrument());      m_RegionChooser.set_instrument(get_instrument());
849    
850        if (Settings::singleton()->syncSamplerInstrumentSelection) {
851            switch_sampler_instrument_signal.emit(get_instrument());
852        }
853  }  }
854    
855  void loader_progress_callback(gig::progress_t* progress)  void loader_progress_callback(gig::progress_t* progress)
# Line 478  void loader_progress_callback(gig::progr Line 861  void loader_progress_callback(gig::progr
861  void Loader::progress_callback(float fraction)  void Loader::progress_callback(float fraction)
862  {  {
863      {      {
864          Glib::Mutex::Lock lock(progressMutex);          Glib::Threads::Mutex::Lock lock(progressMutex);
865          progress = fraction;          progress = fraction;
866      }      }
867      progress_dispatcher();      progress_dispatcher();
868  }  }
869    
870    #if defined(WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
871    // make sure stack is 16-byte aligned for SSE instructions
872    __attribute__((force_align_arg_pointer))
873    #endif
874  void Loader::thread_function()  void Loader::thread_function()
875  {  {
876      printf("thread_function self=%x\n", Glib::Thread::self());      printf("thread_function self=%p\n",
877      printf("Start %s\n", filename);             static_cast<void*>(Glib::Threads::Thread::self()));
878      RIFF::File* riff = new RIFF::File(filename);      printf("Start %s\n", filename.c_str());
879      gig = new gig::File(riff);      try {
880      gig::progress_t progress;          RIFF::File* riff = new RIFF::File(filename);
881      progress.callback = loader_progress_callback;          gig = new gig::File(riff);
882      progress.custom = this;          gig::progress_t progress;
883            progress.callback = loader_progress_callback;
884      gig->GetInstrument(0, &progress);          progress.custom = this;
885      printf("End\n");  
886      finished_dispatcher();          gig->GetInstrument(0, &progress);
887            printf("End\n");
888            finished_dispatcher();
889        } catch (RIFF::Exception e) {
890            error_message = e.Message;
891            error_dispatcher.emit();
892        } catch (...) {
893            error_message = _("Unknown exception occurred");
894            error_dispatcher.emit();
895        }
896  }  }
897    
898  Loader::Loader(const char* filename)  Loader::Loader(const char* filename)
899      : thread(0), filename(filename)      : filename(filename), gig(0), thread(0), progress(0.f)
900  {  {
901  }  }
902    
903  void Loader::launch()  void Loader::launch()
904  {  {
905    #ifdef OLD_THREADS
906      thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);      thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
907      printf("launch thread=%x\n", thread);  #else
908        thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Loader::thread_function));
909    #endif
910        printf("launch thread=%p\n", static_cast<void*>(thread));
911  }  }
912    
913  float Loader::get_progress()  float Loader::get_progress()
914  {  {
915      float res;      float res;
916      {      {
917          Glib::Mutex::Lock lock(progressMutex);          Glib::Threads::Mutex::Lock lock(progressMutex);
918          res = progress;          res = progress;
919      }      }
920      return res;      return res;
# Line 530  Glib::Dispatcher& Loader::signal_finishe Line 930  Glib::Dispatcher& Loader::signal_finishe
930      return finished_dispatcher;      return finished_dispatcher;
931  }  }
932    
933  LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)  Glib::Dispatcher& Loader::signal_error()
934    {
935        return error_dispatcher;
936    }
937    
938    void saver_progress_callback(gig::progress_t* progress)
939    {
940        Saver* saver = static_cast<Saver*>(progress->custom);
941        saver->progress_callback(progress->factor);
942    }
943    
944    void Saver::progress_callback(float fraction)
945    {
946        {
947            Glib::Threads::Mutex::Lock lock(progressMutex);
948            progress = fraction;
949        }
950        progress_dispatcher.emit();
951    }
952    
953    #if defined(WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
954    // make sure stack is 16-byte aligned for SSE instructions
955    __attribute__((force_align_arg_pointer))
956    #endif
957    void Saver::thread_function()
958    {
959        printf("thread_function self=%p\n",
960               static_cast<void*>(Glib::Threads::Thread::self()));
961        printf("Start %s\n", filename.c_str());
962        try {
963            gig::progress_t progress;
964            progress.callback = saver_progress_callback;
965            progress.custom = this;
966    
967            // if no filename was provided, that means "save", if filename was provided means "save as"
968            if (filename.empty()) {
969                if (!Settings::singleton()->saveWithTemporaryFile) {
970                    // save directly over the existing .gig file
971                    // (requires less disk space than solution below
972                    // but may be slower)
973                    gig->Save(&progress);
974                } else {
975                    // save the file as separate temporary file first,
976                    // then move the saved file over the old file
977                    // (may result in performance speedup during save)
978                    String tmpname = filename + ".TMP";
979                    gig->Save(tmpname, &progress);
980                    #if defined(WIN32)
981                    if (!DeleteFile(filename.c_str())) {
982                        throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file).");
983                    }
984                    #else // POSIX ...
985                    if (unlink(filename.c_str())) {
986                        throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file): " + String(strerror(errno)));
987                    }
988                    #endif
989                    if (rename(tmpname.c_str(), filename.c_str())) {
990                        #if defined(WIN32)
991                        throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file).");
992                        #else
993                        throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file): " + String(strerror(errno)));
994                        #endif
995                    }
996                }
997            } else {
998                gig->Save(filename, &progress);
999            }
1000    
1001            printf("End\n");
1002            finished_dispatcher.emit();
1003        } catch (RIFF::Exception e) {
1004            error_message = e.Message;
1005            error_dispatcher.emit();
1006        } catch (...) {
1007            error_message = _("Unknown exception occurred");
1008            error_dispatcher.emit();
1009        }
1010    }
1011    
1012    Saver::Saver(gig::File* file, Glib::ustring filename)
1013        : gig(file), filename(filename), thread(0), progress(0.f)
1014    {
1015    }
1016    
1017    void Saver::launch()
1018    {
1019    #ifdef OLD_THREADS
1020        thread = Glib::Thread::create(sigc::mem_fun(*this, &Saver::thread_function), true);
1021    #else
1022        thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Saver::thread_function));
1023    #endif
1024        printf("launch thread=%p\n", static_cast<void*>(thread));
1025    }
1026    
1027    float Saver::get_progress()
1028    {
1029        float res;
1030        {
1031            Glib::Threads::Mutex::Lock lock(progressMutex);
1032            res = progress;
1033        }
1034        return res;
1035    }
1036    
1037    Glib::Dispatcher& Saver::signal_progress()
1038    {
1039        return progress_dispatcher;
1040    }
1041    
1042    Glib::Dispatcher& Saver::signal_finished()
1043    {
1044        return finished_dispatcher;
1045    }
1046    
1047    Glib::Dispatcher& Saver::signal_error()
1048    {
1049        return error_dispatcher;
1050    }
1051    
1052    ProgressDialog::ProgressDialog(const Glib::ustring& title, Gtk::Window& parent)
1053      : Gtk::Dialog(title, parent, true)      : Gtk::Dialog(title, parent, true)
1054  {  {
1055      get_vbox()->pack_start(progressBar);      get_vbox()->pack_start(progressBar);
1056      show_all_children();      show_all_children();
1057        resize(600,50);
1058  }  }
1059    
1060  // Clear all GUI elements / controls. This method is typically called  // Clear all GUI elements / controls. This method is typically called
1061  // 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.
1062  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();  
1063      // forget all samples that ought to be imported      // forget all samples that ought to be imported
1064      m_SampleImportQueue.clear();      m_SampleImportQueue.clear();
1065      // clear the samples and instruments tree views      // clear the samples and instruments tree views
1066      m_refTreeModel->clear();      m_refTreeModel->clear();
1067      m_refSamplesTreeModel->clear();      m_refSamplesTreeModel->clear();
1068        m_refScriptsTreeModel->clear();
1069        // remove all entries from "Instrument" menu
1070        while (!instrument_menu->get_children().empty()) {
1071            remove_instrument_from_menu(0);
1072        }
1073      // free libgig's gig::File instance      // free libgig's gig::File instance
1074      if (file && !file_is_shared) delete file;      if (file && !file_is_shared) delete file;
1075      file = NULL;      file = NULL;
1076      set_file_is_shared(false);      set_file_is_shared(false);
1077  }  }
1078    
1079    void MainWindow::__refreshEntireGUI() {
1080        // clear the samples and instruments tree views
1081        m_refTreeModel->clear();
1082        m_refSamplesTreeModel->clear();
1083        m_refScriptsTreeModel->clear();
1084        // remove all entries from "Instrument" menu
1085        while (!instrument_menu->get_children().empty()) {
1086            remove_instrument_from_menu(0);
1087        }
1088    
1089        if (!this->file) return;
1090    
1091        load_gig(
1092            this->file, this->file->pInfo->Name.c_str(), this->file_is_shared
1093        );
1094    }
1095    
1096  void MainWindow::on_action_file_new()  void MainWindow::on_action_file_new()
1097  {  {
1098      if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;      if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
# Line 571  void MainWindow::on_action_file_new() Line 1105  void MainWindow::on_action_file_new()
1105      gig::File* pFile = new gig::File;      gig::File* pFile = new gig::File;
1106      // already add one new instrument by default      // already add one new instrument by default
1107      gig::Instrument* pInstrument = pFile->AddInstrument();      gig::Instrument* pInstrument = pFile->AddInstrument();
1108      pInstrument->pInfo->Name = "Unnamed Instrument";      pInstrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument"));
1109      // update GUI with that new gig::File      // update GUI with that new gig::File
1110      load_gig(pFile, 0 /*no file name yet*/);      load_gig(pFile, 0 /*no file name yet*/);
1111  }  }
# Line 582  bool MainWindow::close_confirmation_dial Line 1116  bool MainWindow::close_confirmation_dial
1116                                   Glib::filename_display_basename(filename).c_str());                                   Glib::filename_display_basename(filename).c_str());
1117      Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);      Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
1118      g_free(msg);      g_free(msg);
 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2  
1119      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."));
 #endif  
1120      dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);      dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);
1121      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1122      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);
1123      dialog.set_default_response(Gtk::RESPONSE_YES);      dialog.set_default_response(Gtk::RESPONSE_YES);
1124      int response = dialog.run();      int response = dialog.run();
1125      dialog.hide();      dialog.hide();
1126      if (response == Gtk::RESPONSE_YES) return file_save();  
1127      return response != Gtk::RESPONSE_CANCEL;      // user decided to exit app without saving
1128        if (response == Gtk::RESPONSE_NO) return true;
1129    
1130        // user cancelled dialog, thus don't close app
1131        if (response == Gtk::RESPONSE_CANCEL) return false;
1132    
1133        // TODO: the following return valid is disabled and hard coded instead for
1134        // now, due to the fact that saving with progress bar is now implemented
1135        // asynchronously, as a result the app does not close automatically anymore
1136        // after saving the file has completed
1137        //
1138        //   if (response == Gtk::RESPONSE_YES) return file_save();
1139        //   return response != Gtk::RESPONSE_CANCEL;
1140        //
1141        if (response == Gtk::RESPONSE_YES) file_save();
1142        return false; // always prevent closing the app for now (see comment above)
1143  }  }
1144    
1145  bool MainWindow::leaving_shared_mode_dialog() {  bool MainWindow::leaving_shared_mode_dialog() {
1146      Glib::ustring msg = _("Detach from sampler and proceed working stand-alone?");      Glib::ustring msg = _("Detach from sampler and proceed working stand-alone?");
1147      Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);      Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2  
1148      dialog.set_secondary_text(      dialog.set_secondary_text(
1149          _("If you proceed to work on another instrument file, it won't be "          _("If you proceed to work on another instrument file, it won't be "
1150            "used by the sampler until you tell the sampler explicitly to "            "used by the sampler until you tell the sampler explicitly to "
1151            "load it.")            "load it."));
    );  
 #endif  
1152      dialog.add_button(_("_Yes, Detach"), Gtk::RESPONSE_YES);      dialog.add_button(_("_Yes, Detach"), Gtk::RESPONSE_YES);
1153      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1154      dialog.set_default_response(Gtk::RESPONSE_CANCEL);      dialog.set_default_response(Gtk::RESPONSE_CANCEL);
1155      int response = dialog.run();      int response = dialog.run();
1156      dialog.hide();      dialog.hide();
# Line 620  void MainWindow::on_action_file_open() Line 1164  void MainWindow::on_action_file_open()
1164      if (file_is_shared && !leaving_shared_mode_dialog()) return;      if (file_is_shared && !leaving_shared_mode_dialog()) return;
1165    
1166      Gtk::FileChooserDialog dialog(*this, _("Open file"));      Gtk::FileChooserDialog dialog(*this, _("Open file"));
1167      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1168      dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);      dialog.add_button(_("_Open"), Gtk::RESPONSE_OK);
1169      dialog.set_default_response(Gtk::RESPONSE_OK);      dialog.set_default_response(Gtk::RESPONSE_OK);
1170    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
1171      Gtk::FileFilter filter;      Gtk::FileFilter filter;
1172      filter.add_pattern("*.gig");      filter.add_pattern("*.gig");
1173    #else
1174        Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
1175        filter->add_pattern("*.gig");
1176    #endif
1177      dialog.set_filter(filter);      dialog.set_filter(filter);
1178      if (current_gig_dir != "") {      if (current_gig_dir != "") {
1179          dialog.set_current_folder(current_gig_dir);          dialog.set_current_folder(current_gig_dir);
# Line 632  void MainWindow::on_action_file_open() Line 1181  void MainWindow::on_action_file_open()
1181      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1182          std::string filename = dialog.get_filename();          std::string filename = dialog.get_filename();
1183          printf("filename=%s\n", filename.c_str());          printf("filename=%s\n", filename.c_str());
1184          printf("on_action_file_open self=%x\n", Glib::Thread::self());          printf("on_action_file_open self=%p\n",
1185                   static_cast<void*>(Glib::Threads::Thread::self()));
1186          load_file(filename.c_str());          load_file(filename.c_str());
1187          current_gig_dir = Glib::path_get_dirname(filename);          current_gig_dir = Glib::path_get_dirname(filename);
1188      }      }
# Line 641  void MainWindow::on_action_file_open() Line 1191  void MainWindow::on_action_file_open()
1191  void MainWindow::load_file(const char* name)  void MainWindow::load_file(const char* name)
1192  {  {
1193      __clear();      __clear();
1194      load_dialog = new LoadDialog("Loading...", *this);  
1195      load_dialog->show_all();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1196      loader = new Loader(strdup(name));          _("Loading") +  Glib::ustring(" '") +
1197            Glib::filename_display_basename(name) + "' ...",
1198            *this
1199        );
1200        progress_dialog->show_all();
1201        loader = new Loader(name); //FIXME: memory leak!
1202      loader->signal_progress().connect(      loader->signal_progress().connect(
1203          sigc::mem_fun(*this, &MainWindow::on_loader_progress));          sigc::mem_fun(*this, &MainWindow::on_loader_progress));
1204      loader->signal_finished().connect(      loader->signal_finished().connect(
1205          sigc::mem_fun(*this, &MainWindow::on_loader_finished));          sigc::mem_fun(*this, &MainWindow::on_loader_finished));
1206        loader->signal_error().connect(
1207            sigc::mem_fun(*this, &MainWindow::on_loader_error));
1208      loader->launch();      loader->launch();
1209  }  }
1210    
# Line 663  void MainWindow::load_instrument(gig::In Line 1220  void MainWindow::load_instrument(gig::In
1220      // load the instrument      // load the instrument
1221      gig::File* pFile = (gig::File*) instr->GetParent();      gig::File* pFile = (gig::File*) instr->GetParent();
1222      load_gig(pFile, 0 /*file name*/, true /*shared instrument*/);      load_gig(pFile, 0 /*file name*/, true /*shared instrument*/);
1223      //TODO: automatically select the given instrument      // automatically select the given instrument
1224        int i = 0;
1225        for (gig::Instrument* instrument = pFile->GetFirstInstrument(); instrument;
1226             instrument = pFile->GetNextInstrument(), ++i)
1227        {
1228            if (instrument == instr) {
1229                // select item in "instruments" tree view
1230                m_TreeView.get_selection()->select(Gtk::TreePath(ToString(i)));
1231                // make sure the selected item in the "instruments" tree view is
1232                // visible (scroll to it)
1233                m_TreeView.scroll_to_row(Gtk::TreePath(ToString(i)));
1234                // select item in instrument menu
1235                {
1236                    const std::vector<Gtk::Widget*> children =
1237                        instrument_menu->get_children();
1238                    static_cast<Gtk::RadioMenuItem*>(children[i])->set_active();
1239                }
1240                // update region chooser and dimension region chooser
1241                m_RegionChooser.set_instrument(instr);
1242                break;
1243            }
1244        }
1245  }  }
1246    
1247  void MainWindow::on_loader_progress()  void MainWindow::on_loader_progress()
1248  {  {
1249      load_dialog->set_fraction(loader->get_progress());      progress_dialog->set_fraction(loader->get_progress());
1250  }  }
1251    
1252  void MainWindow::on_loader_finished()  void MainWindow::on_loader_finished()
1253  {  {
1254      printf("Loader finished!\n");      printf("Loader finished!\n");
1255      printf("on_loader_finished self=%x\n", Glib::Thread::self());      printf("on_loader_finished self=%p\n",
1256      load_gig(loader->gig, loader->filename);             static_cast<void*>(Glib::Threads::Thread::self()));
1257      load_dialog->hide();      load_gig(loader->gig, loader->filename.c_str());
1258        progress_dialog->hide();
1259    }
1260    
1261    void MainWindow::on_loader_error()
1262    {
1263        Glib::ustring txt = _("Could not load file: ") + loader->error_message;
1264        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1265        msg.run();
1266        progress_dialog->hide();
1267  }  }
1268    
1269  void MainWindow::on_action_file_save()  void MainWindow::on_action_file_save()
# Line 715  bool MainWindow::file_save() Line 1302  bool MainWindow::file_save()
1302    
1303      std::cout << "Saving file\n" << std::flush;      std::cout << "Saving file\n" << std::flush;
1304      file_structure_to_be_changed_signal.emit(this->file);      file_structure_to_be_changed_signal.emit(this->file);
1305      try {  
1306          file->Save();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1307          if (file_is_changed) {          _("Saving") +  Glib::ustring(" '") +
1308              set_title(get_title().substr(1));          Glib::filename_display_basename(this->filename) + "' ...",
1309              file_is_changed = false;          *this
1310          }      );
1311      } catch (RIFF::Exception e) {      progress_dialog->show_all();
1312          file_structure_changed_signal.emit(this->file);      saver = new Saver(this->file); //FIXME: memory leak!
1313          Glib::ustring txt = _("Could not save file: ") + e.Message;      saver->signal_progress().connect(
1314          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);          sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1315          msg.run();      saver->signal_finished().connect(
1316          return false;          sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1317      }      saver->signal_error().connect(
1318      std::cout << "Saving file done\n" << std::flush;          sigc::mem_fun(*this, &MainWindow::on_saver_error));
1319        saver->launch();
1320    
1321        return true;
1322    }
1323    
1324    void MainWindow::on_saver_progress()
1325    {
1326        progress_dialog->set_fraction(saver->get_progress());
1327    }
1328    
1329    void MainWindow::on_saver_error()
1330    {
1331        file_structure_changed_signal.emit(this->file);
1332        Glib::ustring txt = _("Could not save file: ") + saver->error_message;
1333        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1334        msg.run();
1335    }
1336    
1337    void MainWindow::on_saver_finished()
1338    {
1339        this->file = saver->gig;
1340        this->filename = saver->filename;
1341        current_gig_dir = Glib::path_get_dirname(filename);
1342        set_title(Glib::filename_display_basename(filename));
1343        file_has_name = true;
1344        file_is_changed = false;
1345        std::cout << "Saving file done. Importing queued samples now ...\n" << std::flush;
1346      __import_queued_samples();      __import_queued_samples();
1347        std::cout << "Importing queued samples done.\n" << std::flush;
1348    
1349      file_structure_changed_signal.emit(this->file);      file_structure_changed_signal.emit(this->file);
1350      return true;  
1351        __refreshEntireGUI();
1352        progress_dialog->hide();
1353  }  }
1354    
1355  void MainWindow::on_action_file_save_as()  void MainWindow::on_action_file_save_as()
# Line 742  void MainWindow::on_action_file_save_as( Line 1360  void MainWindow::on_action_file_save_as(
1360    
1361  bool MainWindow::file_save_as()  bool MainWindow::file_save_as()
1362  {  {
1363      Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE);      Gtk::FileChooserDialog dialog(*this, _("Save As"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1364      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
1365      dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);      dialog.add_button(_("_Save"), Gtk::RESPONSE_OK);
1366      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  
1367      dialog.set_do_overwrite_confirmation();      dialog.set_do_overwrite_confirmation();
1368      // TODO: an overwrite dialog for gtkmm < 2.8  
1369  #endif  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
1370      Gtk::FileFilter filter;      Gtk::FileFilter filter;
1371      filter.add_pattern("*.gig");      filter.add_pattern("*.gig");
1372    #else
1373        Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
1374        filter->add_pattern("*.gig");
1375    #endif
1376      dialog.set_filter(filter);      dialog.set_filter(filter);
1377    
1378      // set initial dir and filename of the Save As dialog      // set initial dir and filename of the Save As dialog
# Line 760  bool MainWindow::file_save_as() Line 1380  bool MainWindow::file_save_as()
1380      {      {
1381          std::string basename = Glib::path_get_basename(filename);          std::string basename = Glib::path_get_basename(filename);
1382          std::string dir = Glib::path_get_dirname(filename);          std::string dir = Glib::path_get_dirname(filename);
1383          basename = std::string("copy_of_") + basename;          basename = std::string(_("copy_of_")) + basename;
1384          Glib::ustring copyFileName = Glib::build_filename(dir, basename);          Glib::ustring copyFileName = Glib::build_filename(dir, basename);
1385          if (Glib::path_is_absolute(filename)) {          if (Glib::path_is_absolute(filename)) {
1386              dialog.set_filename(copyFileName);              dialog.set_filename(copyFileName);
# Line 773  bool MainWindow::file_save_as() Line 1393  bool MainWindow::file_save_as()
1393      // show warning in the dialog      // show warning in the dialog
1394      Gtk::HBox descriptionArea;      Gtk::HBox descriptionArea;
1395      descriptionArea.set_spacing(15);      descriptionArea.set_spacing(15);
1396      Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));      Gtk::Image warningIcon;
1397        warningIcon.set_from_icon_name("dialog-warning",
1398                                       Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
1399      descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);      descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
1400    #if GTKMM_MAJOR_VERSION < 3
1401      view::WrapLabel description;      view::WrapLabel description;
1402    #else
1403        Gtk::Label description;
1404        description.set_line_wrap();
1405    #endif
1406      description.set_markup(      description.set_markup(
1407          _("\n<b>CAUTION:</b> You <b>MUST</b> use the "          _("\n<b>CAUTION:</b> You <b>MUST</b> use the "
1408            "<span style=\"italic\">\"Save\"</span> dialog instead of "            "<span style=\"italic\">\"Save\"</span> dialog instead of "
# Line 789  bool MainWindow::file_save_as() Line 1416  bool MainWindow::file_save_as()
1416      descriptionArea.show_all();      descriptionArea.show_all();
1417    
1418      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1419          file_structure_to_be_changed_signal.emit(this->file);          std::string filename = dialog.get_filename();
1420          try {          if (!Glib::str_has_suffix(filename, ".gig")) {
1421              std::string filename = dialog.get_filename();              filename += ".gig";
             if (!Glib::str_has_suffix(filename, ".gig")) {  
                 filename += ".gig";  
             }  
             printf("filename=%s\n", filename.c_str());  
             file->Save(filename);  
             this->filename = filename;  
             current_gig_dir = Glib::path_get_dirname(filename);  
             set_title(Glib::filename_display_basename(filename));  
             file_has_name = true;  
             file_is_changed = false;  
         } catch (RIFF::Exception e) {  
             file_structure_changed_signal.emit(this->file);  
             Glib::ustring txt = _("Could not save file: ") + e.Message;  
             Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);  
             msg.run();  
             return false;  
1422          }          }
1423          __import_queued_samples();          printf("filename=%s\n", filename.c_str());
1424          file_structure_changed_signal.emit(this->file);  
1425            progress_dialog = new ProgressDialog( //FIXME: memory leak!
1426                _("Saving") +  Glib::ustring(" '") +
1427                Glib::filename_display_basename(filename) + "' ...",
1428                *this
1429            );
1430            progress_dialog->show_all();
1431    
1432            saver = new Saver(file, filename); //FIXME: memory leak!
1433            saver->signal_progress().connect(
1434                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1435            saver->signal_finished().connect(
1436                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1437            saver->signal_error().connect(
1438                sigc::mem_fun(*this, &MainWindow::on_saver_error));
1439            saver->launch();
1440    
1441          return true;          return true;
1442      }      }
1443      return false;      return false;
# Line 820  bool MainWindow::file_save_as() Line 1447  bool MainWindow::file_save_as()
1447  void MainWindow::__import_queued_samples() {  void MainWindow::__import_queued_samples() {
1448      std::cout << "Starting sample import\n" << std::flush;      std::cout << "Starting sample import\n" << std::flush;
1449      Glib::ustring error_files;      Glib::ustring error_files;
1450      printf("Samples to import: %d\n", m_SampleImportQueue.size());      printf("Samples to import: %d\n", int(m_SampleImportQueue.size()));
1451      for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();      for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1452           iter != m_SampleImportQueue.end(); ) {           iter != m_SampleImportQueue.end(); ) {
1453          printf("Importing sample %s\n",(*iter).sample_path.c_str());          printf("Importing sample %s\n",(*iter).sample_path.c_str());
1454          SF_INFO info;          SF_INFO info;
1455          info.format = 0;          info.format = 0;
1456          SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);          SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
1457            sf_command(hFile, SFC_SET_SCALE_FLOAT_INT_READ, 0, SF_TRUE);
1458          try {          try {
1459              if (!hFile) throw std::string("could not open file");              if (!hFile) throw std::string(_("could not open file"));
1460              // determine sample's bit depth              // determine sample's bit depth
1461              int bitdepth;              int bitdepth;
1462              switch (info.format & 0xff) {              switch (info.format & 0xff) {
# Line 845  void MainWindow::__import_queued_samples Line 1473  void MainWindow::__import_queued_samples
1473                      break;                      break;
1474                  default:                  default:
1475                      sf_close(hFile); // close sound file                      sf_close(hFile); // close sound file
1476                      throw std::string("format not supported"); // unsupported subformat (yet?)                      throw std::string(_("format not supported")); // unsupported subformat (yet?)
1477              }              }
1478    
1479              const int bufsize = 10000;              const int bufsize = 10000;
# Line 887  void MainWindow::__import_queued_samples Line 1515  void MainWindow::__import_queued_samples
1515              }              }
1516              // cleanup              // cleanup
1517              sf_close(hFile);              sf_close(hFile);
1518                // let the sampler re-cache the sample if needed
1519                sample_changed_signal.emit(iter->gig_sample);
1520              // on success we remove the sample from the import queue,              // on success we remove the sample from the import queue,
1521              // otherwise keep it, maybe it works the next time ?              // otherwise keep it, maybe it works the next time ?
1522              std::list<SampleImportItem>::iterator cur = iter;              std::list<SampleImportItem>::iterator cur = iter;
# Line 894  void MainWindow::__import_queued_samples Line 1524  void MainWindow::__import_queued_samples
1524              m_SampleImportQueue.erase(cur);              m_SampleImportQueue.erase(cur);
1525          } catch (std::string what) {          } catch (std::string what) {
1526              // remember the files that made trouble (and their cause)              // remember the files that made trouble (and their cause)
1527              if (error_files.size()) error_files += "\n";              if (!error_files.empty()) error_files += "\n";
1528              error_files += (*iter).sample_path += " (" + what + ")";              error_files += (*iter).sample_path += " (" + what + ")";
1529              ++iter;              ++iter;
1530          }          }
1531      }      }
1532      // show error message box when some sample(s) could not be imported      // show error message box when some sample(s) could not be imported
1533      if (error_files.size()) {      if (!error_files.empty()) {
1534          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;
1535          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1536          msg.run();          msg.run();
# Line 913  void MainWindow::on_action_file_properti Line 1543  void MainWindow::on_action_file_properti
1543      propDialog.deiconify();      propDialog.deiconify();
1544  }  }
1545    
1546    void MainWindow::on_action_warn_user_on_extensions() {
1547        Settings::singleton()->warnUserOnExtensions =
1548            !Settings::singleton()->warnUserOnExtensions;
1549    }
1550    
1551    void MainWindow::on_action_sync_sampler_instrument_selection() {
1552        Settings::singleton()->syncSamplerInstrumentSelection =
1553            !Settings::singleton()->syncSamplerInstrumentSelection;
1554    }
1555    
1556    void MainWindow::on_action_move_root_note_with_region_moved() {
1557        Settings::singleton()->moveRootNoteWithRegionMoved =
1558            !Settings::singleton()->moveRootNoteWithRegionMoved;
1559    }
1560    
1561  void MainWindow::on_action_help_about()  void MainWindow::on_action_help_about()
1562  {  {
 #ifdef ABOUT_DIALOG  
1563      Gtk::AboutDialog dialog;      Gtk::AboutDialog dialog;
1564    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 12) || GTKMM_MAJOR_VERSION > 2
1565        dialog.set_program_name("Gigedit");
1566    #else
1567        dialog.set_name("Gigedit");
1568    #endif
1569      dialog.set_version(VERSION);      dialog.set_version(VERSION);
1570      dialog.set_copyright("Copyright (C) 2006,2007 Andreas Persson");      dialog.set_copyright("Copyright (C) 2006-2017 Andreas Persson");
1571      dialog.set_comments(      const std::string sComment =
1572          "Released under the GNU General Public License.\n"          _("Built " __DATE__ "\nUsing ") +
1573          "\n"          ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +
1574          "Please notice that this is still a very young instrument editor. "          _(
1575          "So better backup your Gigasampler files before editing them with "              "Gigedit is released under the GNU General Public License.\n"
1576          "this application.\n"              "\n"
1577          "\n"              "This program is distributed WITHOUT ANY WARRANTY; So better "
1578          "Please report bugs to: http://bugs.linuxsampler.org"              "backup your Gigasampler/GigaStudio files before editing them with "
1579      );              "this application.\n"
1580                "\n"
1581                "Please report bugs to: http://bugs.linuxsampler.org"
1582            );
1583        dialog.set_comments(sComment.c_str());
1584      dialog.set_website("http://www.linuxsampler.org");      dialog.set_website("http://www.linuxsampler.org");
1585      dialog.set_website_label("http://www.linuxsampler.org");      dialog.set_website_label("http://www.linuxsampler.org");
1586      dialog.run();      dialog.run();
 #endif  
1587  }  }
1588    
1589  PropDialog::PropDialog()  PropDialog::PropDialog()
1590      : table(2,1),      : eFileFormat(_("File Format")),
1591        eName("Name"),        eName(_("Name")),
1592        eCreationDate("Creation date"),        eCreationDate(_("Creation date")),
1593        eComments("Comments"),        eComments(_("Comments")),
1594        eProduct("Product"),        eProduct(_("Product")),
1595        eCopyright("Copyright"),        eCopyright(_("Copyright")),
1596        eArtists("Artists"),        eArtists(_("Artists")),
1597        eGenre("Genre"),        eGenre(_("Genre")),
1598        eKeywords("Keywords"),        eKeywords(_("Keywords")),
1599        eEngineer("Engineer"),        eEngineer(_("Engineer")),
1600        eTechnician("Technician"),        eTechnician(_("Technician")),
1601        eSoftware("Software"),        eSoftware(_("Software")),
1602        eMedium("Medium"),        eMedium(_("Medium")),
1603        eSource("Source"),        eSource(_("Source")),
1604        eSourceForm("Source form"),        eSourceForm(_("Source form")),
1605        eCommissioned("Commissioned"),        eCommissioned(_("Commissioned")),
1606        eSubject("Subject"),        eSubject(_("Subject")),
1607        quitButton(Gtk::Stock::CLOSE),        quitButton(_("_Close"), true),
1608        update_model(0)        table(2, 1),
1609          m_file(NULL)
1610  {  {
1611      set_title("File Properties");      set_title(_("File Properties"));
1612      eName.set_width_chars(50);      eName.set_width_chars(50);
1613    
1614      connect(eName, &DLS::Info::Name);      connect(eName, &DLS::Info::Name);
# Line 975  PropDialog::PropDialog() Line 1628  PropDialog::PropDialog()
1628      connect(eCommissioned, &DLS::Info::Commissioned);      connect(eCommissioned, &DLS::Info::Commissioned);
1629      connect(eSubject, &DLS::Info::Subject);      connect(eSubject, &DLS::Info::Subject);
1630    
1631        table.add(eFileFormat);
1632      table.add(eName);      table.add(eName);
1633      table.add(eCreationDate);      table.add(eCreationDate);
1634      table.add(eComments);      table.add(eComments);
# Line 1001  PropDialog::PropDialog() Line 1655  PropDialog::PropDialog()
1655      buttonBox.set_border_width(5);      buttonBox.set_border_width(5);
1656      buttonBox.show();      buttonBox.show();
1657      buttonBox.pack_start(quitButton);      buttonBox.pack_start(quitButton);
1658      quitButton.set_flags(Gtk::CAN_DEFAULT);      quitButton.set_can_default();
1659      quitButton.grab_focus();      quitButton.grab_focus();
1660      quitButton.signal_clicked().connect(      quitButton.signal_clicked().connect(
1661          sigc::mem_fun(*this, &PropDialog::hide));          sigc::mem_fun(*this, &PropDialog::hide));
1662        eFileFormat.signal_value_changed().connect(
1663            sigc::mem_fun(*this, &PropDialog::onFileFormatChanged));
1664    
1665      quitButton.show();      quitButton.show();
1666      vbox.show();      vbox.show();
1667      show_all_children();      show_all_children();
1668  }  }
1669    
1670  void PropDialog::set_info(DLS::Info* info)  void PropDialog::set_file(gig::File* file)
1671  {  {
1672      this->info = info;      m_file = file;
1673      update_model++;  
1674      eName.set_value(info->Name);      // update file format version combo box
1675      eCreationDate.set_value(info->CreationDate);      const std::string sGiga = "Gigasampler/GigaStudio v";
1676      eComments.set_value(info->Comments);      const int major = file->pVersion->major;
1677      eProduct.set_value(info->Product);      std::vector<std::string> txts;
1678      eCopyright.set_value(info->Copyright);      std::vector<int> values;
1679      eArtists.set_value(info->Artists);      txts.push_back(sGiga + "2"); values.push_back(2);
1680      eGenre.set_value(info->Genre);      txts.push_back(sGiga + "3/v4"); values.push_back(3);
1681      eKeywords.set_value(info->Keywords);      if (major != 2 && major != 3) {
1682      eEngineer.set_value(info->Engineer);          txts.push_back(sGiga + ToString(major)); values.push_back(major);
1683      eTechnician.set_value(info->Technician);      }
1684      eSoftware.set_value(info->Software);      std::vector<const char*> texts;
1685      eMedium.set_value(info->Medium);      for (int i = 0; i < txts.size(); ++i) texts.push_back(txts[i].c_str());
1686      eSource.set_value(info->Source);      texts.push_back(NULL); values.push_back(0);
1687      eSourceForm.set_value(info->SourceForm);      eFileFormat.set_choices(&texts[0], &values[0]);
1688      eCommissioned.set_value(info->Commissioned);      eFileFormat.set_value(major);
1689      eSubject.set_value(info->Subject);  }
1690      update_model--;  
1691    void PropDialog::onFileFormatChanged() {
1692        const int major = eFileFormat.get_value();
1693        if (m_file) m_file->pVersion->major = major;
1694  }  }
1695    
1696  sigc::signal<void>& PropDialog::signal_info_changed()  void PropDialog::set_info(DLS::Info* info)
1697  {  {
1698      return info_changed;      update(info);
1699  }  }
1700    
1701  void InstrumentProps::set_IsDrum(bool value)  
1702    void InstrumentProps::set_Name(const gig::String& name)
1703  {  {
1704      instrument->IsDrum = value;      m->pInfo->Name = name;
1705  }  }
1706    
1707  void InstrumentProps::set_MIDIBank(uint16_t value)  void InstrumentProps::update_name()
1708  {  {
1709      instrument->MIDIBank = value;      update_model++;
1710        eName.set_value(m->pInfo->Name);
1711        update_model--;
1712  }  }
1713    
1714  void InstrumentProps::set_MIDIProgram(uint32_t value)  void InstrumentProps::set_IsDrum(bool value)
1715  {  {
1716      instrument->MIDIProgram = value;      m->IsDrum = value;
1717  }  }
1718    
1719  void InstrumentProps::set_DimensionKeyRange_low(uint8_t value)  void InstrumentProps::set_MIDIBank(uint16_t value)
1720  {  {
1721      instrument->DimensionKeyRange.low = value;      m->MIDIBank = value;
     if (value > instrument->DimensionKeyRange.high) {  
         eDimensionKeyRangeHigh.set_value(value);  
     }  
1722  }  }
1723    
1724  void InstrumentProps::set_DimensionKeyRange_high(uint8_t value)  void InstrumentProps::set_MIDIProgram(uint32_t value)
1725  {  {
1726      instrument->DimensionKeyRange.high = value;      m->MIDIProgram = value;
     if (value < instrument->DimensionKeyRange.low) {  
         eDimensionKeyRangeLow.set_value(value);  
     }  
1727  }  }
1728    
1729  InstrumentProps::InstrumentProps()  InstrumentProps::InstrumentProps() :
1730      : table(2,1),      quitButton(_("_Close"), true),
1731        quitButton(Gtk::Stock::CLOSE),      table(2,1),
1732        eName("Name"),      eName(_("Name")),
1733        eIsDrum("Is drum"),      eIsDrum(_("Is drum")),
1734        eMIDIBank("MIDI bank", 0, 16383),      eMIDIBank(_("MIDI bank"), 0, 16383),
1735        eMIDIProgram("MIDI program"),      eMIDIProgram(_("MIDI program")),
1736        eAttenuation("Attenuation", 0, 96, 0, 1),      eAttenuation(_("Attenuation"), 0, 96, 0, 1),
1737        eGainPlus6("Gain +6dB", eAttenuation, -6),      eGainPlus6(_("Gain +6dB"), eAttenuation, -6),
1738        eEffectSend("Effect send", 0, 65535),      eEffectSend(_("Effect send"), 0, 65535),
1739        eFineTune("Fine tune", -8400, 8400),      eFineTune(_("Fine tune"), -8400, 8400),
1740        ePitchbendRange("Pitchbend range", 0, 12),      ePitchbendRange(_("Pitchbend range"), 0, 12),
1741        ePianoReleaseMode("Piano release mode"),      ePianoReleaseMode(_("Piano release mode")),
1742        eDimensionKeyRangeLow("Keyswitching range low"),      eDimensionKeyRangeLow(_("Keyswitching range low")),
1743        eDimensionKeyRangeHigh("Keyswitching range high"),      eDimensionKeyRangeHigh(_("Keyswitching range high"))
       update_model(0)  
1744  {  {
1745      set_title("Instrument Properties");      set_title(_("Instrument Properties"));
1746    
1747      eDimensionKeyRangeLow.set_tip(      eDimensionKeyRangeLow.set_tip(
1748          _("start of the keyboard area which should switch the "          _("start of the keyboard area which should switch the "
# Line 1098  InstrumentProps::InstrumentProps() Line 1753  InstrumentProps::InstrumentProps()
1753            "\"keyswitching\" dimension")            "\"keyswitching\" dimension")
1754      );      );
1755    
1756        connect(eName, &InstrumentProps::set_Name);
1757      connect(eIsDrum, &InstrumentProps::set_IsDrum);      connect(eIsDrum, &InstrumentProps::set_IsDrum);
1758      connect(eMIDIBank, &InstrumentProps::set_MIDIBank);      connect(eMIDIBank, &InstrumentProps::set_MIDIBank);
1759      connect(eMIDIProgram, &InstrumentProps::set_MIDIProgram);      connect(eMIDIProgram, &InstrumentProps::set_MIDIProgram);
# Line 1107  InstrumentProps::InstrumentProps() Line 1763  InstrumentProps::InstrumentProps()
1763      connect(eFineTune, &gig::Instrument::FineTune);      connect(eFineTune, &gig::Instrument::FineTune);
1764      connect(ePitchbendRange, &gig::Instrument::PitchbendRange);      connect(ePitchbendRange, &gig::Instrument::PitchbendRange);
1765      connect(ePianoReleaseMode, &gig::Instrument::PianoReleaseMode);      connect(ePianoReleaseMode, &gig::Instrument::PianoReleaseMode);
1766      connect(eDimensionKeyRangeLow,      connect(eDimensionKeyRangeLow, eDimensionKeyRangeHigh,
1767              &InstrumentProps::set_DimensionKeyRange_low);              &gig::Instrument::DimensionKeyRange);
1768      connect(eDimensionKeyRangeHigh,  
1769              &InstrumentProps::set_DimensionKeyRange_high);      eName.signal_value_changed().connect(sig_name_changed.make_slot());
1770    
1771      table.set_col_spacings(5);      table.set_col_spacings(5);
1772    
# Line 1136  InstrumentProps::InstrumentProps() Line 1792  InstrumentProps::InstrumentProps()
1792      buttonBox.set_border_width(5);      buttonBox.set_border_width(5);
1793      buttonBox.show();      buttonBox.show();
1794      buttonBox.pack_start(quitButton);      buttonBox.pack_start(quitButton);
1795      quitButton.set_flags(Gtk::CAN_DEFAULT);      quitButton.set_can_default();
1796      quitButton.grab_focus();      quitButton.grab_focus();
1797    
1798      quitButton.signal_clicked().connect(      quitButton.signal_clicked().connect(
# Line 1149  InstrumentProps::InstrumentProps() Line 1805  InstrumentProps::InstrumentProps()
1805    
1806  void InstrumentProps::set_instrument(gig::Instrument* instrument)  void InstrumentProps::set_instrument(gig::Instrument* instrument)
1807  {  {
1808      this->instrument = instrument;      update(instrument);
1809    
1810      update_model++;      update_model++;
1811      eName.set_value(instrument->pInfo->Name);      eName.set_value(instrument->pInfo->Name);
1812      eIsDrum.set_value(instrument->IsDrum);      eIsDrum.set_value(instrument->IsDrum);
1813      eMIDIBank.set_value(instrument->MIDIBank);      eMIDIBank.set_value(instrument->MIDIBank);
1814      eMIDIProgram.set_value(instrument->MIDIProgram);      eMIDIProgram.set_value(instrument->MIDIProgram);
     eAttenuation.set_value(instrument->Attenuation);  
     eGainPlus6.set_value(instrument->Attenuation);  
     eEffectSend.set_value(instrument->EffectSend);  
     eFineTune.set_value(instrument->FineTune);  
     ePitchbendRange.set_value(instrument->PitchbendRange);  
     ePianoReleaseMode.set_value(instrument->PianoReleaseMode);  
     eDimensionKeyRangeLow.set_value(instrument->DimensionKeyRange.low);  
     eDimensionKeyRangeHigh.set_value(instrument->DimensionKeyRange.high);  
1815      update_model--;      update_model--;
1816  }  }
1817    
 sigc::signal<void>& InstrumentProps::signal_instrument_changed()  
 {  
     return instrument_changed;  
 }  
1818    
1819  void MainWindow::file_changed()  void MainWindow::file_changed()
1820  {  {
# Line 1180  void MainWindow::file_changed() Line 1824  void MainWindow::file_changed()
1824      }      }
1825  }  }
1826    
1827    void MainWindow::updateSampleRefCountMap(gig::File* gig) {
1828        sample_ref_count.clear();
1829        
1830        if (!gig) return;
1831    
1832        for (gig::Instrument* instrument = gig->GetFirstInstrument(); instrument;
1833             instrument = gig->GetNextInstrument())
1834        {
1835            for (gig::Region* rgn = instrument->GetFirstRegion(); rgn;
1836                 rgn = instrument->GetNextRegion())
1837            {
1838                for (int i = 0; i < 256; ++i) {
1839                    if (!rgn->pDimensionRegions[i]) continue;
1840                    if (rgn->pDimensionRegions[i]->pSample) {
1841                        sample_ref_count[rgn->pDimensionRegions[i]->pSample]++;
1842                    }
1843                }
1844            }
1845        }
1846    }
1847    
1848  void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument)  void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument)
1849  {  {
1850      file = 0;      file = 0;
1851      set_file_is_shared(isSharedInstrument);      set_file_is_shared(isSharedInstrument);
1852    
1853      this->filename = filename ? filename : _("Unsaved Gig File");      this->filename =
1854            (filename && strlen(filename) > 0) ?
1855                filename : (!gig->GetFileName().empty()) ?
1856                    gig->GetFileName() : _("Unsaved Gig File");
1857      set_title(Glib::filename_display_basename(this->filename));      set_title(Glib::filename_display_basename(this->filename));
1858      file_has_name = filename;      file_has_name = filename;
1859      file_is_changed = false;      file_is_changed = false;
1860    
1861        propDialog.set_file(gig);
1862      propDialog.set_info(gig->pInfo);      propDialog.set_info(gig->pInfo);
1863    
1864      Gtk::MenuItem* instrument_menu =      instrument_name_connection.block();
1865          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));      int index = 0;
   
     int instrument_index = 0;  
     Gtk::RadioMenuItem::Group instrument_group;  
1866      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;      for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
1867           instrument = gig->GetNextInstrument()) {           instrument = gig->GetNextInstrument(), ++index) {
1868            Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
1869    
1870          Gtk::TreeModel::iterator iter = m_refTreeModel->append();          Gtk::TreeModel::iterator iter = m_refTreeModel->append();
1871          Gtk::TreeModel::Row row = *iter;          Gtk::TreeModel::Row row = *iter;
1872          row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();          row[m_Columns.m_col_nr] = index;
1873            row[m_Columns.m_col_name] = name;
1874          row[m_Columns.m_col_instr] = instrument;          row[m_Columns.m_col_instr] = instrument;
1875          // create a menu item for this instrument  
1876          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++;  
1877      }      }
1878      instrument_menu->show();      instrument_name_connection.unblock();
1879      instrument_menu->get_submenu()->show_all_children();      uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments")->show();
1880    
1881        updateSampleRefCountMap(gig);
1882    
1883      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {      for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1884          if (group->Name != "") {          if (group->Name != "") {
1885              Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();              Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1886              Gtk::TreeModel::Row rowGroup = *iterGroup;              Gtk::TreeModel::Row rowGroup = *iterGroup;
1887              rowGroup[m_SamplesModel.m_col_name]   = group->Name.c_str();              rowGroup[m_SamplesModel.m_col_name]   = gig_to_utf8(group->Name);
1888              rowGroup[m_SamplesModel.m_col_group]  = group;              rowGroup[m_SamplesModel.m_col_group]  = group;
1889              rowGroup[m_SamplesModel.m_col_sample] = NULL;              rowGroup[m_SamplesModel.m_col_sample] = NULL;
1890              for (gig::Sample* sample = group->GetFirstSample();              for (gig::Sample* sample = group->GetFirstSample();
# Line 1230  void MainWindow::load_gig(gig::File* gig Line 1892  void MainWindow::load_gig(gig::File* gig
1892                  Gtk::TreeModel::iterator iterSample =                  Gtk::TreeModel::iterator iterSample =
1893                      m_refSamplesTreeModel->append(rowGroup.children());                      m_refSamplesTreeModel->append(rowGroup.children());
1894                  Gtk::TreeModel::Row rowSample = *iterSample;                  Gtk::TreeModel::Row rowSample = *iterSample;
1895                  rowSample[m_SamplesModel.m_col_name]   = sample->pInfo->Name.c_str();                  rowSample[m_SamplesModel.m_col_name] =
1896                        gig_to_utf8(sample->pInfo->Name);
1897                  rowSample[m_SamplesModel.m_col_sample] = sample;                  rowSample[m_SamplesModel.m_col_sample] = sample;
1898                  rowSample[m_SamplesModel.m_col_group]  = NULL;                  rowSample[m_SamplesModel.m_col_group]  = NULL;
1899                    int refcount = sample_ref_count.count(sample) ? sample_ref_count[sample] : 0;
1900                    rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
1901                    rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
1902              }              }
1903          }          }
1904      }      }
1905        
1906        for (int i = 0; gig->GetScriptGroup(i); ++i) {
1907            gig::ScriptGroup* group = gig->GetScriptGroup(i);
1908    
1909            Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
1910            Gtk::TreeModel::Row rowGroup = *iterGroup;
1911            rowGroup[m_ScriptsModel.m_col_name]   = gig_to_utf8(group->Name);
1912            rowGroup[m_ScriptsModel.m_col_group]  = group;
1913            rowGroup[m_ScriptsModel.m_col_script] = NULL;
1914            for (int s = 0; group->GetScript(s); ++s) {
1915                gig::Script* script = group->GetScript(s);
1916    
1917                Gtk::TreeModel::iterator iterScript =
1918                    m_refScriptsTreeModel->append(rowGroup.children());
1919                Gtk::TreeModel::Row rowScript = *iterScript;
1920                rowScript[m_ScriptsModel.m_col_name] = gig_to_utf8(script->Name);
1921                rowScript[m_ScriptsModel.m_col_script] = script;
1922                rowScript[m_ScriptsModel.m_col_group]  = NULL;
1923            }
1924        }
1925        // unfold all sample groups & script groups by default
1926        m_TreeViewSamples.expand_all();
1927        m_TreeViewScripts.expand_all();
1928    
1929      file = gig;      file = gig;
1930    
1931      // select the first instrument      // select the first instrument
1932      Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();      m_TreeView.get_selection()->select(Gtk::TreePath("0"));
1933      tree_sel_ref->select(Gtk::TreePath("0"));  
1934        instr_props_set_instrument();
1935        gig::Instrument* instrument = get_instrument();
1936        if (instrument) {
1937            midiRules.set_instrument(instrument);
1938        }
1939  }  }
1940    
1941  void MainWindow::show_instr_props()  bool MainWindow::instr_props_set_instrument()
1942  {  {
1943      gig::Instrument* instrument = get_instrument();      instrumentProps.signal_name_changed().clear();
1944      if (instrument)  
1945      {      std::vector<Gtk::TreeModel::Path> rows = m_TreeView.get_selection()->get_selected_rows();
1946        if (rows.empty()) {
1947            instrumentProps.hide();
1948            return false;
1949        }
1950        Gtk::TreeModel::const_iterator it = m_refTreeModel->get_iter(rows[0]);
1951        if (it) {
1952            Gtk::TreeModel::Row row = *it;
1953            gig::Instrument* instrument = row[m_Columns.m_col_instr];
1954    
1955          instrumentProps.set_instrument(instrument);          instrumentProps.set_instrument(instrument);
1956    
1957            // make sure instrument tree is updated when user changes the
1958            // instrument name in instrument properties window
1959            instrumentProps.signal_name_changed().connect(
1960                sigc::bind(
1961                    sigc::mem_fun(*this,
1962                                  &MainWindow::instr_name_changed_by_instr_props),
1963                    it));
1964        } else {
1965            instrumentProps.hide();
1966        }
1967        return it;
1968    }
1969    
1970    void MainWindow::show_instr_props()
1971    {
1972        if (instr_props_set_instrument()) {
1973          instrumentProps.show();          instrumentProps.show();
1974          instrumentProps.deiconify();          instrumentProps.deiconify();
1975      }      }
1976  }  }
1977    
1978    void MainWindow::instr_name_changed_by_instr_props(Gtk::TreeModel::iterator& it)
1979    {
1980        Gtk::TreeModel::Row row = *it;
1981        Glib::ustring name = row[m_Columns.m_col_name];
1982    
1983        gig::Instrument* instrument = row[m_Columns.m_col_instr];
1984        Glib::ustring gigname(gig_to_utf8(instrument->pInfo->Name));
1985        if (gigname != name) {
1986            row[m_Columns.m_col_name] = gigname;
1987        }
1988    }
1989    
1990    void MainWindow::show_midi_rules()
1991    {
1992        if (gig::Instrument* instrument = get_instrument())
1993        {
1994            midiRules.set_instrument(instrument);
1995            midiRules.show();
1996            midiRules.deiconify();
1997        }
1998    }
1999    
2000    void MainWindow::show_script_slots() {
2001        if (!file) return;
2002        // get selected instrument
2003        std::vector<Gtk::TreeModel::Path> rows = m_TreeView.get_selection()->get_selected_rows();
2004        if (rows.empty()) return;
2005        Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(rows[0]);
2006        if (!it) return;
2007        Gtk::TreeModel::Row row = *it;
2008        gig::Instrument* instrument = row[m_Columns.m_col_instr];
2009        if (!instrument) return;
2010    
2011        ScriptSlots* window = new ScriptSlots;
2012        window->setInstrument(instrument);
2013        //window->reparent(*this);
2014        window->show();
2015    }
2016    
2017    void MainWindow::on_action_refresh_all() {
2018        __refreshEntireGUI();
2019    }
2020    
2021  void MainWindow::on_action_view_status_bar() {  void MainWindow::on_action_view_status_bar() {
2022      Gtk::CheckMenuItem* item =      Gtk::CheckMenuItem* item =
2023          dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/Statusbar"));          dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/Statusbar"));
# Line 1266  void MainWindow::on_action_view_status_b Line 2029  void MainWindow::on_action_view_status_b
2029      else                    m_StatusBar.hide();      else                    m_StatusBar.hide();
2030  }  }
2031    
2032    void MainWindow::on_auto_restore_win_dim() {
2033        Gtk::CheckMenuItem* item =
2034            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/AutoRestoreWinDim"));
2035        if (!item) {
2036            std::cerr << "/MenuBar/MenuView/AutoRestoreWinDim == NULL\n";
2037            return;
2038        }
2039        Settings::singleton()->autoRestoreWindowDimension = item->get_active();
2040    }
2041    
2042    void MainWindow::on_save_with_temporary_file() {
2043        Gtk::CheckMenuItem* item =
2044            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuSettings/SaveWithTemporaryFile"));
2045        if (!item) {
2046            std::cerr << "/MenuBar/MenuSettings/SaveWithTemporaryFile == NULL\n";
2047            return;
2048        }
2049        Settings::singleton()->saveWithTemporaryFile = item->get_active();
2050    }
2051    
2052    bool MainWindow::is_copy_samples_unity_note_enabled() const {
2053        Gtk::CheckMenuItem* item =
2054            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity"));
2055        if (!item) {
2056            std::cerr << "/MenuBar/MenuEdit/CopySampleUnity == NULL\n";
2057            return true;
2058        }
2059        return item->get_active();
2060    }
2061    
2062    bool MainWindow::is_copy_samples_fine_tune_enabled() const {
2063        Gtk::CheckMenuItem* item =
2064            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleTune"));
2065        if (!item) {
2066            std::cerr << "/MenuBar/MenuEdit/CopySampleTune == NULL\n";
2067            return true;
2068        }
2069        return item->get_active();
2070    }
2071    
2072    bool MainWindow::is_copy_samples_loop_enabled() const {
2073        Gtk::CheckMenuItem* item =
2074            dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleLoop"));
2075        if (!item) {
2076            std::cerr << "/MenuBar/MenuEdit/CopySampleLoop == NULL\n";
2077            return true;
2078        }
2079        return item->get_active();
2080    }
2081    
2082  void MainWindow::on_button_release(GdkEventButton* button)  void MainWindow::on_button_release(GdkEventButton* button)
2083  {  {
2084      if (button->type == GDK_2BUTTON_PRESS) {      if (button->type == GDK_2BUTTON_PRESS) {
2085          show_instr_props();          show_instr_props();
2086      } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {      } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
2087            // gig v2 files have no midi rules
2088            const bool bEnabled = !(file->pVersion && file->pVersion->major == 2);
2089            static_cast<Gtk::MenuItem*>(
2090                uiManager->get_widget("/MenuBar/MenuInstrument/MidiRules"))->set_sensitive(
2091                    bEnabled
2092                );
2093            static_cast<Gtk::MenuItem*>(
2094                uiManager->get_widget("/PopupMenu/MidiRules"))->set_sensitive(
2095                    bEnabled
2096                );
2097          popup_menu->popup(button->button, button->time);          popup_menu->popup(button->button, button->time);
2098      }      }
2099  }  }
2100    
2101  void MainWindow::on_instrument_selection_change(int index) {  void MainWindow::on_instrument_selection_change(Gtk::RadioMenuItem* item) {
2102      m_RegionChooser.set_instrument(file->GetInstrument(index));      if (item->get_active()) {
2103            const std::vector<Gtk::Widget*> children =
2104                instrument_menu->get_children();
2105            std::vector<Gtk::Widget*>::const_iterator it =
2106                find(children.begin(), children.end(), item);
2107            if (it != children.end()) {
2108                int index = it - children.begin();
2109                m_TreeView.get_selection()->select(Gtk::TreePath(ToString(index)));
2110    
2111                m_RegionChooser.set_instrument(file->GetInstrument(index));
2112            }
2113        }
2114    }
2115    
2116    void MainWindow::select_instrument(gig::Instrument* instrument) {
2117        if (!instrument) return;
2118    
2119        Glib::RefPtr<Gtk::TreeModel> model = m_TreeView.get_model();
2120        for (int i = 0; i < model->children().size(); ++i) {
2121            Gtk::TreeModel::Row row = model->children()[i];
2122            if (row[m_Columns.m_col_instr] == instrument) {
2123                // select and show the respective instrument in the list view
2124                show_intruments_tab();
2125                m_TreeView.get_selection()->unselect_all();
2126                m_TreeView.get_selection()->select(model->children()[i]);
2127                std::vector<Gtk::TreeModel::Path> rows =
2128                    m_TreeView.get_selection()->get_selected_rows();
2129                if (!rows.empty())
2130                    m_TreeView.scroll_to_row(rows[0]);
2131                on_sel_change(); // the regular instrument selection change callback
2132            }
2133        }
2134    }
2135    
2136    /// Returns true if requested dimension region was successfully selected and scrolled to in the list view, false on error.
2137    bool MainWindow::select_dimension_region(gig::DimensionRegion* dimRgn) {
2138        gig::Region* pRegion = (gig::Region*) dimRgn->GetParent();
2139        gig::Instrument* pInstrument = (gig::Instrument*) pRegion->GetParent();
2140    
2141        Glib::RefPtr<Gtk::TreeModel> model = m_TreeView.get_model();
2142        for (int i = 0; i < model->children().size(); ++i) {
2143            Gtk::TreeModel::Row row = model->children()[i];
2144            if (row[m_Columns.m_col_instr] == pInstrument) {
2145                // select and show the respective instrument in the list view
2146                show_intruments_tab();
2147                m_TreeView.get_selection()->unselect_all();
2148                m_TreeView.get_selection()->select(model->children()[i]);
2149                std::vector<Gtk::TreeModel::Path> rows =
2150                    m_TreeView.get_selection()->get_selected_rows();
2151                if (!rows.empty())
2152                    m_TreeView.scroll_to_row(rows[0]);
2153                on_sel_change(); // the regular instrument selection change callback
2154    
2155                // select respective region in the region selector
2156                m_RegionChooser.set_region(pRegion);
2157    
2158                // select and show the respective dimension region in the editor
2159                //update_dimregs();
2160                if (!m_DimRegionChooser.select_dimregion(dimRgn)) return false;
2161                //dimreg_edit.set_dim_region(dimRgn);
2162    
2163                return true;
2164            }
2165        }
2166    
2167        return false;
2168    }
2169    
2170    void MainWindow::select_sample(gig::Sample* sample) {
2171        Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
2172        for (int g = 0; g < model->children().size(); ++g) {
2173            Gtk::TreeModel::Row rowGroup = model->children()[g];
2174            for (int s = 0; s < rowGroup.children().size(); ++s) {
2175                Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
2176                if (rowSample[m_SamplesModel.m_col_sample] == sample) {
2177                    show_samples_tab();
2178                    m_TreeViewSamples.get_selection()->unselect_all();
2179                    m_TreeViewSamples.get_selection()->select(rowGroup.children()[s]);
2180                    std::vector<Gtk::TreeModel::Path> rows =
2181                        m_TreeViewSamples.get_selection()->get_selected_rows();
2182                    if (rows.empty()) return;
2183                    m_TreeViewSamples.scroll_to_row(rows[0]);
2184                    return;
2185                }
2186            }
2187        }
2188  }  }
2189    
2190  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
2191      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
2192            // by default if Ctrl keys is pressed down, then a mouse right-click
2193            // does not select the respective row, so we must assure this
2194            // programmatically ...
2195            /*{
2196                Gtk::TreeModel::Path path;
2197                Gtk::TreeViewColumn* pColumn = NULL;
2198                int cellX, cellY;
2199                bool bSuccess = m_TreeViewSamples.get_path_at_pos(
2200                    (int)button->x, (int)button->y,
2201                    path, pColumn, cellX, cellY
2202                );
2203                if (bSuccess) {
2204                    if (m_TreeViewSamples.get_selection()->count_selected_rows() <= 0) {
2205                        printf("not selected !!!\n");
2206                        m_TreeViewSamples.get_selection()->select(path);
2207                    }
2208                }
2209            }*/
2210    
2211          Gtk::Menu* sample_popup =          Gtk::Menu* sample_popup =
2212              dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));              dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
2213          // update enabled/disabled state of sample popup items          // update enabled/disabled state of sample popup items
2214          Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();          Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2215          Gtk::TreeModel::iterator it = sel->get_selected();          std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
2216          bool group_selected  = false;          const int n = rows.size();
2217          bool sample_selected = false;          int nGroups  = 0;
2218          if (it) {          int nSamples = 0;
2219            for (int r = 0; r < n; ++r) {
2220                Gtk::TreeModel::iterator it = m_refSamplesTreeModel->get_iter(rows[r]);
2221                if (!it) continue;
2222              Gtk::TreeModel::Row row = *it;              Gtk::TreeModel::Row row = *it;
2223              group_selected  = row[m_SamplesModel.m_col_group];              if (row[m_SamplesModel.m_col_group]) nGroups++;
2224              sample_selected = row[m_SamplesModel.m_col_sample];              if (row[m_SamplesModel.m_col_sample]) nSamples++;
2225          }          }
2226    
2227          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
2228              set_sensitive(group_selected || sample_selected);              set_sensitive(n == 1);
2229          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
2230              set_sensitive(group_selected || sample_selected);              set_sensitive(n);
2231          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
2232              set_sensitive(file);              set_sensitive(file);
2233            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/ShowSampleRefs"))->
2234                set_sensitive(nSamples == 1);
2235          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->          dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
2236              set_sensitive(group_selected || sample_selected);              set_sensitive(n);
2237          // show sample popup          // show sample popup
2238          sample_popup->popup(button->button, button->time);          sample_popup->popup(button->button, button->time);
2239    
2240            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/SampleProperties"))->
2241                set_sensitive(n == 1);
2242            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddSample"))->
2243                set_sensitive(n);
2244            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddGroup"))->
2245                set_sensitive(file);
2246            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/ShowSampleRefs"))->
2247                set_sensitive(nSamples == 1);
2248            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/RemoveSample"))->
2249                set_sensitive(n);
2250      }      }
2251  }  }
2252    
2253  void MainWindow::on_action_add_instrument() {  void MainWindow::on_script_treeview_button_release(GdkEventButton* button) {
2254      static int __instrument_indexer = 0;      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
2255      if (!file) return;          Gtk::Menu* script_popup =
2256      gig::Instrument* instrument = file->AddInstrument();              dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/ScriptPopupMenu"));
2257      __instrument_indexer++;          // update enabled/disabled state of sample popup items
2258      instrument->pInfo->Name =          Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2259          "Unnamed Instrument " + ToString(__instrument_indexer);          Gtk::TreeModel::iterator it = sel->get_selected();
2260            bool group_selected  = false;
2261            bool script_selected = false;
2262            if (it) {
2263                Gtk::TreeModel::Row row = *it;
2264                group_selected  = row[m_ScriptsModel.m_col_group];
2265                script_selected = row[m_ScriptsModel.m_col_script];
2266            }
2267            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScript"))->
2268                set_sensitive(group_selected || script_selected);
2269            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScriptGroup"))->
2270                set_sensitive(file);
2271            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/EditScript"))->
2272                set_sensitive(script_selected);    
2273            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/RemoveScript"))->
2274                set_sensitive(group_selected || script_selected);
2275            // show sample popup
2276            script_popup->popup(button->button, button->time);
2277    
2278            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScript"))->
2279                set_sensitive(group_selected || script_selected);
2280            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScriptGroup"))->
2281                set_sensitive(file);
2282            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/EditScript"))->
2283                set_sensitive(script_selected);    
2284            dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/RemoveScript"))->
2285                set_sensitive(group_selected || script_selected);
2286        }
2287    }
2288    
2289    Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu(
2290        const Glib::ustring& name, int position) {
2291    
2292        Gtk::RadioMenuItem::Group instrument_group;
2293        const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
2294        if (!children.empty()) {
2295            instrument_group =
2296                static_cast<Gtk::RadioMenuItem*>(children[0])->get_group();
2297        }
2298        Gtk::RadioMenuItem* item =
2299            new Gtk::RadioMenuItem(instrument_group, name);
2300        if (position < 0) {
2301            instrument_menu->append(*item);
2302        } else {
2303            instrument_menu->insert(*item, position);
2304        }
2305        item->show();
2306        item->signal_activate().connect(
2307            sigc::bind(
2308                sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
2309                item));
2310        return item;
2311    }
2312    
2313    void MainWindow::remove_instrument_from_menu(int index) {
2314        const std::vector<Gtk::Widget*> children =
2315            instrument_menu->get_children();
2316        Gtk::Widget* child = children[index];
2317        instrument_menu->remove(*child);
2318        delete child;
2319    }
2320    
2321    void MainWindow::add_instrument(gig::Instrument* instrument) {
2322        const Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
2323    
2324      // update instrument tree view      // update instrument tree view
2325        instrument_name_connection.block();
2326      Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();      Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
2327      Gtk::TreeModel::Row rowInstr = *iterInstr;      Gtk::TreeModel::Row rowInstr = *iterInstr;
2328      rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();      rowInstr[m_Columns.m_col_nr] = m_refTreeModel->children().size() - 1;
2329        rowInstr[m_Columns.m_col_name] = name;
2330      rowInstr[m_Columns.m_col_instr] = instrument;      rowInstr[m_Columns.m_col_instr] = instrument;
2331        instrument_name_connection.unblock();
2332    
2333        add_instrument_to_menu(name);
2334    
2335        m_TreeView.get_selection()->select(iterInstr);
2336    
2337      file_changed();      file_changed();
2338  }  }
2339    
2340    void MainWindow::on_action_add_instrument() {
2341        static int __instrument_indexer = 0;
2342        if (!file) return;
2343        gig::Instrument* instrument = file->AddInstrument();
2344        __instrument_indexer++;
2345        instrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument ") +
2346                                                ToString(__instrument_indexer));
2347    
2348        add_instrument(instrument);
2349    }
2350    
2351    void MainWindow::on_action_duplicate_instrument() {
2352        if (!file) return;
2353    
2354        // retrieve the currently selected instrument
2355        // (being the original instrument to be duplicated)
2356        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
2357        std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
2358        for (int r = 0; r < rows.size(); ++r) {
2359            Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(rows[r]);
2360            if (it) {
2361                Gtk::TreeModel::Row row = *it;
2362                gig::Instrument* instrOrig = row[m_Columns.m_col_instr];
2363                if (instrOrig) {
2364                    // duplicate the orginal instrument
2365                    gig::Instrument* instrNew = file->AddDuplicateInstrument(instrOrig);
2366                    instrNew->pInfo->Name =
2367                        instrOrig->pInfo->Name +
2368                        gig_from_utf8(Glib::ustring(" (") + _("Copy") + ")");
2369    
2370                    add_instrument(instrNew);
2371                }
2372            }
2373        }
2374    }
2375    
2376  void MainWindow::on_action_remove_instrument() {  void MainWindow::on_action_remove_instrument() {
2377      if (!file) return;      if (!file) return;
2378      if (file_is_shared) {      if (file_is_shared) {
# Line 1335  void MainWindow::on_action_remove_instru Line 2387  void MainWindow::on_action_remove_instru
2387      }      }
2388    
2389      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
2390      Gtk::TreeModel::iterator it = sel->get_selected();      std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
2391      if (it) {      for (int r = rows.size() - 1; r >= 0; --r) {
2392            Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(rows[r]);
2393            if (!it) continue;
2394          Gtk::TreeModel::Row row = *it;          Gtk::TreeModel::Row row = *it;
2395          gig::Instrument* instr = row[m_Columns.m_col_instr];          gig::Instrument* instr = row[m_Columns.m_col_instr];
2396          try {          try {
2397                Gtk::TreePath path(it);
2398                int index = path[0];
2399    
2400              // remove instrument from the gig file              // remove instrument from the gig file
2401              if (instr) file->DeleteInstrument(instr);              if (instr) file->DeleteInstrument(instr);
             // remove respective row from instruments tree view  
             m_refTreeModel->erase(it);  
2402              file_changed();              file_changed();
2403    
2404                remove_instrument_from_menu(index);
2405    
2406                // remove row from instruments tree view
2407                m_refTreeModel->erase(it);
2408                // update "Nr" column of all instrument rows
2409                {
2410                    int index = 0;
2411                    for (Gtk::TreeModel::iterator it = m_refTreeModel->children().begin();
2412                         it != m_refTreeModel->children().end(); ++it, ++index)
2413                    {
2414                        Gtk::TreeModel::Row row = *it;
2415                        row[m_Columns.m_col_nr] = index;
2416                    }
2417                }
2418    
2419    #if GTKMM_MAJOR_VERSION < 3
2420                // select another instrument (in gtk3 this is done
2421                // automatically)
2422                if (!m_refTreeModel->children().empty()) {
2423                    if (index == m_refTreeModel->children().size()) {
2424                        index--;
2425                    }
2426                    m_TreeView.get_selection()->select(
2427                        Gtk::TreePath(ToString(index)));
2428                }
2429    #endif
2430                instr_props_set_instrument();
2431                instr = get_instrument();
2432                if (instr) {
2433                    midiRules.set_instrument(instr);
2434                } else {
2435                    midiRules.hide();
2436                }
2437          } catch (RIFF::Exception e) {          } catch (RIFF::Exception e) {
2438              Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2439              msg.run();              msg.run();
# Line 1355  void MainWindow::on_action_remove_instru Line 2444  void MainWindow::on_action_remove_instru
2444  void MainWindow::on_action_sample_properties() {  void MainWindow::on_action_sample_properties() {
2445      //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
2446      Gtk::MessageDialog msg(      Gtk::MessageDialog msg(
2447          *this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO          *this, _("Sorry, yet to be implemented!"), false, Gtk::MESSAGE_INFO
2448      );      );
2449      msg.run();      msg.run();
2450  }  }
2451    
2452    void MainWindow::on_action_add_script_group() {
2453        static int __script_indexer = 0;
2454        if (!file) return;
2455        gig::ScriptGroup* group = file->AddScriptGroup();
2456        group->Name = gig_from_utf8(_("Unnamed Group"));
2457        if (__script_indexer) group->Name += " " + ToString(__script_indexer);
2458        __script_indexer++;
2459        // update sample tree view
2460        Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
2461        Gtk::TreeModel::Row rowGroup = *iterGroup;
2462        rowGroup[m_ScriptsModel.m_col_name] = gig_to_utf8(group->Name);
2463        rowGroup[m_ScriptsModel.m_col_script] = NULL;
2464        rowGroup[m_ScriptsModel.m_col_group] = group;
2465        file_changed();
2466    }
2467    
2468    void MainWindow::on_action_add_script() {
2469        if (!file) return;
2470        // get selected group
2471        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2472        Gtk::TreeModel::iterator it = sel->get_selected();
2473        if (!it) return;
2474        Gtk::TreeModel::Row row = *it;
2475        gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2476        if (!group) { // not a group, but a script is selected (probably)
2477            gig::Script* script = row[m_ScriptsModel.m_col_script];
2478            if (!script) return;
2479            it = row.parent(); // resolve parent (that is the script's group)
2480            if (!it) return;
2481            row = *it;
2482            group = row[m_ScriptsModel.m_col_group];
2483            if (!group) return;
2484        }
2485    
2486        // add a new script to the .gig file
2487        gig::Script* script = group->AddScript();    
2488        Glib::ustring name = _("Unnamed Script");
2489        script->Name = gig_from_utf8(name);
2490    
2491        // add script to the tree view
2492        Gtk::TreeModel::iterator iterScript =
2493            m_refScriptsTreeModel->append(row.children());
2494        Gtk::TreeModel::Row rowScript = *iterScript;
2495        rowScript[m_ScriptsModel.m_col_name] = name;
2496        rowScript[m_ScriptsModel.m_col_script] = script;
2497        rowScript[m_ScriptsModel.m_col_group]  = NULL;
2498    
2499        // unfold group of new script item in treeview
2500        Gtk::TreeModel::Path path(iterScript);
2501        m_TreeViewScripts.expand_to_path(path);
2502    }
2503    
2504    void MainWindow::on_action_edit_script() {
2505        if (!file) return;
2506        // get selected script
2507        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2508        Gtk::TreeModel::iterator it = sel->get_selected();
2509        if (!it) return;
2510        Gtk::TreeModel::Row row = *it;
2511        gig::Script* script = row[m_ScriptsModel.m_col_script];
2512        if (!script) return;
2513    
2514        ScriptEditor* editor = new ScriptEditor;
2515        editor->signal_script_to_be_changed.connect(
2516            signal_script_to_be_changed.make_slot()
2517        );
2518        editor->signal_script_changed.connect(
2519            signal_script_changed.make_slot()
2520        );
2521        editor->setScript(script);
2522        //editor->reparent(*this);
2523        editor->show();
2524    }
2525    
2526    void MainWindow::on_action_remove_script() {
2527        if (!file) return;
2528        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2529        Gtk::TreeModel::iterator it = sel->get_selected();
2530        if (it) {
2531            Gtk::TreeModel::Row row = *it;
2532            gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2533            gig::Script* script     = row[m_ScriptsModel.m_col_script];
2534            Glib::ustring name      = row[m_ScriptsModel.m_col_name];
2535            try {
2536                // remove script group or script from the gig file
2537                if (group) {
2538                    // notify everybody that we're going to remove these samples
2539    //TODO:         scripts_to_be_removed_signal.emit(members);
2540                    // delete the group in the .gig file including the
2541                    // samples that belong to the group
2542                    file->DeleteScriptGroup(group);
2543                    // notify that we're done with removal
2544    //TODO:         scripts_removed_signal.emit();
2545                    file_changed();
2546                } else if (script) {
2547                    // notify everybody that we're going to remove this sample
2548    //TODO:         std::list<gig::Script*> lscripts;
2549    //TODO:         lscripts.push_back(script);
2550    //TODO:         scripts_to_be_removed_signal.emit(lscripts);
2551                    // remove sample from the .gig file
2552                    script->GetGroup()->DeleteScript(script);
2553                    // notify that we're done with removal
2554    //TODO:         scripts_removed_signal.emit();
2555                    dimreg_changed();
2556                    file_changed();
2557                }
2558                // remove respective row(s) from samples tree view
2559                m_refScriptsTreeModel->erase(it);
2560            } catch (RIFF::Exception e) {
2561                // pretend we're done with removal (i.e. to avoid dead locks)
2562    //TODO:     scripts_removed_signal.emit();
2563                // show error message
2564                Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2565                msg.run();
2566            }
2567        }
2568    }
2569    
2570  void MainWindow::on_action_add_group() {  void MainWindow::on_action_add_group() {
2571      static int __sample_indexer = 0;      static int __sample_indexer = 0;
2572      if (!file) return;      if (!file) return;
2573      gig::Group* group = file->AddGroup();      gig::Group* group = file->AddGroup();
2574      group->Name = "Unnamed Group";      group->Name = gig_from_utf8(_("Unnamed Group"));
2575      if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);      if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
2576      __sample_indexer++;      __sample_indexer++;
2577      // update sample tree view      // update sample tree view
2578      Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();      Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
2579      Gtk::TreeModel::Row rowGroup = *iterGroup;      Gtk::TreeModel::Row rowGroup = *iterGroup;
2580      rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();      rowGroup[m_SamplesModel.m_col_name] = gig_to_utf8(group->Name);
2581      rowGroup[m_SamplesModel.m_col_sample] = NULL;      rowGroup[m_SamplesModel.m_col_sample] = NULL;
2582      rowGroup[m_SamplesModel.m_col_group] = group;      rowGroup[m_SamplesModel.m_col_group] = group;
2583      file_changed();      file_changed();
2584  }  }
2585    
2586    void MainWindow::on_action_replace_sample() {
2587        add_or_replace_sample(true);
2588    }
2589    
2590  void MainWindow::on_action_add_sample() {  void MainWindow::on_action_add_sample() {
2591        add_or_replace_sample(false);
2592    }
2593    
2594    void MainWindow::add_or_replace_sample(bool replace) {
2595      if (!file) return;      if (!file) return;
2596      // get selected group  
2597        // get selected group (and probably selected sample)
2598      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2599      Gtk::TreeModel::iterator it = sel->get_selected();      std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
2600        if (rows.empty()) return;
2601        Gtk::TreeModel::iterator it = m_refSamplesTreeModel->get_iter(rows[0]);
2602      if (!it) return;      if (!it) return;
2603      Gtk::TreeModel::Row row = *it;      Gtk::TreeModel::Row row = *it;
2604        gig::Sample* sample = NULL;
2605      gig::Group* group = row[m_SamplesModel.m_col_group];      gig::Group* group = row[m_SamplesModel.m_col_group];
2606      if (!group) { // not a group, but a sample is selected (probably)      if (!group) { // not a group, but a sample is selected (probably)
2607          gig::Sample* sample = row[m_SamplesModel.m_col_sample];          if (replace) sample = row[m_SamplesModel.m_col_sample];
2608          if (!sample) return;          if (!row[m_SamplesModel.m_col_sample]) return;
2609          it = row.parent(); // resolve parent (that is the sample's group)          it = row.parent(); // resolve parent (that is the sample's group)
2610          if (!it) return;          if (!it) return;
2611          row = *it;          if (!replace) row = *it;
2612          group = row[m_SamplesModel.m_col_group];          group = (*it)[m_SamplesModel.m_col_group];
2613          if (!group) return;          if (!group) return;
2614      }      }
2615        if (replace && !sample) return;
2616    
2617      // show 'browse for file' dialog      // show 'browse for file' dialog
2618      Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));      Gtk::FileChooserDialog dialog(*this, replace ? _("Replace Sample with") : _("Add Sample(s)"));
2619      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
2620      dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);      dialog.add_button(_("_Open"), Gtk::RESPONSE_OK);
2621      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
2622      Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile  
2623        // matches all file types supported by libsndfile
2624    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2625        Gtk::FileFilter soundfilter;
2626    #else
2627        Glib::RefPtr<Gtk::FileFilter> soundfilter = Gtk::FileFilter::create();
2628    #endif
2629      const char* const supportedFileTypes[] = {      const char* const supportedFileTypes[] = {
2630          "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",          "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
2631          "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",          "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
# Line 1406  void MainWindow::on_action_add_sample() Line 2633  void MainWindow::on_action_add_sample()
2633          "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",          "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
2634          "*.caf", "*.CAF", NULL          "*.caf", "*.CAF", NULL
2635      };      };
2636        const char* soundfiles = _("Sound Files");
2637        const char* allfiles = _("All Files");
2638    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2639      for (int i = 0; supportedFileTypes[i]; i++)      for (int i = 0; supportedFileTypes[i]; i++)
2640          soundfilter.add_pattern(supportedFileTypes[i]);          soundfilter.add_pattern(supportedFileTypes[i]);
2641      soundfilter.set_name("Sound Files");      soundfilter.set_name(soundfiles);
2642      Gtk::FileFilter allpassfilter; // matches every file  
2643        // matches every file
2644        Gtk::FileFilter allpassfilter;
2645      allpassfilter.add_pattern("*.*");      allpassfilter.add_pattern("*.*");
2646      allpassfilter.set_name("All Files");      allpassfilter.set_name(allfiles);
2647    #else
2648        for (int i = 0; supportedFileTypes[i]; i++)
2649            soundfilter->add_pattern(supportedFileTypes[i]);
2650        soundfilter->set_name(soundfiles);
2651    
2652        // matches every file
2653        Glib::RefPtr<Gtk::FileFilter> allpassfilter = Gtk::FileFilter::create();
2654        allpassfilter->add_pattern("*.*");
2655        allpassfilter->set_name(allfiles);
2656    #endif
2657      dialog.add_filter(soundfilter);      dialog.add_filter(soundfilter);
2658      dialog.add_filter(allpassfilter);      dialog.add_filter(allpassfilter);
2659      if (current_sample_dir != "") {      if (current_sample_dir != "") {
# Line 1420  void MainWindow::on_action_add_sample() Line 2662  void MainWindow::on_action_add_sample()
2662      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
2663          current_sample_dir = dialog.get_current_folder();          current_sample_dir = dialog.get_current_folder();
2664          Glib::ustring error_files;          Glib::ustring error_files;
2665          Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();          std::vector<std::string> filenames = dialog.get_filenames();
2666          for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();          for (std::vector<std::string>::iterator iter = filenames.begin();
2667               iter != filenames.end(); ++iter) {               iter != filenames.end(); ++iter) {
2668              printf("Adding sample %s\n",(*iter).c_str());              printf("Adding sample %s\n",(*iter).c_str());
2669              // use libsndfile to retrieve file informations              // use libsndfile to retrieve file informations
# Line 1429  void MainWindow::on_action_add_sample() Line 2671  void MainWindow::on_action_add_sample()
2671              info.format = 0;              info.format = 0;
2672              SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);              SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
2673              try {              try {
2674                  if (!hFile) throw std::string("could not open file");                  if (!hFile) throw std::string(_("could not open file"));
2675                  int bitdepth;                  int bitdepth;
2676                  switch (info.format & 0xff) {                  switch (info.format & 0xff) {
2677                      case SF_FORMAT_PCM_S8:                      case SF_FORMAT_PCM_S8:
# Line 1445  void MainWindow::on_action_add_sample() Line 2687  void MainWindow::on_action_add_sample()
2687                          break;                          break;
2688                      default:                      default:
2689                          sf_close(hFile); // close sound file                          sf_close(hFile); // close sound file
2690                          throw std::string("format not supported"); // unsupported subformat (yet?)                          throw std::string(_("format not supported")); // unsupported subformat (yet?)
2691                  }                  }
2692                  // add a new sample to the .gig file                  // add a new sample to the .gig file (if adding is requested actually)
2693                  gig::Sample* sample = file->AddSample();                  if (!replace) sample = file->AddSample();
2694                  // file name without path                  // file name without path
2695                  Glib::ustring filename = Glib::filename_display_basename(*iter);                  Glib::ustring filename = Glib::filename_display_basename(*iter);
2696                  // remove file extension if there is one                  // remove file extension if there is one
# Line 1458  void MainWindow::on_action_add_sample() Line 2700  void MainWindow::on_action_add_sample()
2700                          break;                          break;
2701                      }                      }
2702                  }                  }
2703                  sample->pInfo->Name = filename;                  sample->pInfo->Name = gig_from_utf8(filename);
2704                  sample->Channels = info.channels;                  sample->Channels = info.channels;
2705                  sample->BitDepth = bitdepth;                  sample->BitDepth = bitdepth;
2706                  sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;                  sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
# Line 1472  void MainWindow::on_action_add_sample() Line 2714  void MainWindow::on_action_add_sample()
2714                                 &instrument, sizeof(instrument)) != SF_FALSE)                                 &instrument, sizeof(instrument)) != SF_FALSE)
2715                  {                  {
2716                      sample->MIDIUnityNote = instrument.basenote;                      sample->MIDIUnityNote = instrument.basenote;
2717                        sample->FineTune      = instrument.detune;
2718    
 #if HAVE_SF_INSTRUMENT_LOOPS  
2719                      if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) {                      if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) {
2720                          sample->Loops = 1;                          sample->Loops = 1;
2721    
# Line 1493  void MainWindow::on_action_add_sample() Line 2735  void MainWindow::on_action_add_sample()
2735                          sample->LoopPlayCount = instrument.loops[0].count;                          sample->LoopPlayCount = instrument.loops[0].count;
2736                          sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1;                          sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1;
2737                      }                      }
 #endif  
2738                  }                  }
2739    
2740                  // schedule resizing the sample (which will be done                  // schedule resizing the sample (which will be done
2741                  // physically when File::Save() is called)                  // physically when File::Save() is called)
2742                  sample->Resize(info.frames);                  sample->Resize(info.frames);
2743                  // make sure sample is part of the selected group                  // make sure sample is part of the selected group
2744                  group->AddSample(sample);                  if (!replace) group->AddSample(sample);
2745                  // schedule that physical resize and sample import                  // schedule that physical resize and sample import
2746                  // (data copying), performed when "Save" is requested                  // (data copying), performed when "Save" is requested
2747                  SampleImportItem sched_item;                  SampleImportItem sched_item;
# Line 1508  void MainWindow::on_action_add_sample() Line 2749  void MainWindow::on_action_add_sample()
2749                  sched_item.sample_path = *iter;                  sched_item.sample_path = *iter;
2750                  m_SampleImportQueue.push_back(sched_item);                  m_SampleImportQueue.push_back(sched_item);
2751                  // add sample to the tree view                  // add sample to the tree view
2752                  Gtk::TreeModel::iterator iterSample =                  if (replace) {
2753                      m_refSamplesTreeModel->append(row.children());                      row[m_SamplesModel.m_col_name] = gig_to_utf8(sample->pInfo->Name);
2754                  Gtk::TreeModel::Row rowSample = *iterSample;                  } else {
2755                  rowSample[m_SamplesModel.m_col_name]   = filename;                      Gtk::TreeModel::iterator iterSample =
2756                  rowSample[m_SamplesModel.m_col_sample] = sample;                          m_refSamplesTreeModel->append(row.children());
2757                  rowSample[m_SamplesModel.m_col_group]  = NULL;                      Gtk::TreeModel::Row rowSample = *iterSample;
2758                        rowSample[m_SamplesModel.m_col_name] =
2759                            gig_to_utf8(sample->pInfo->Name);
2760                        rowSample[m_SamplesModel.m_col_sample] = sample;
2761                        rowSample[m_SamplesModel.m_col_group]  = NULL;
2762                    }
2763                  // close sound file                  // close sound file
2764                  sf_close(hFile);                  sf_close(hFile);
2765                  file_changed();                  file_changed();
2766              } 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)
2767                  if (error_files.size()) error_files += "\n";                  if (!error_files.empty()) error_files += "\n";
2768                  error_files += *iter += " (" + what + ")";                  error_files += *iter += " (" + what + ")";
2769              }              }
2770          }          }
2771          // 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
2772          if (error_files.size()) {          if (!error_files.empty()) {
2773              Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files;              Glib::ustring txt =
2774                    (replace
2775                        ? _("Failed to replace sample with:\n")
2776                        : _("Could not add the following sample(s):\n"))
2777                    + error_files;
2778              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2779              msg.run();              msg.run();
2780          }          }
# Line 1536  void MainWindow::on_action_replace_all_s Line 2786  void MainWindow::on_action_replace_all_s
2786      if (!file) return;      if (!file) return;
2787      Gtk::FileChooserDialog dialog(*this, _("Select Folder"),      Gtk::FileChooserDialog dialog(*this, _("Select Folder"),
2788                                    Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);                                    Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
2789      view::WrapLabel description(      const char* str =
2790          _("This is a very specific function. It tries to replace all samples "          _("This is a very specific function. It tries to replace all samples "
2791            "in the current gig file by samples located in the chosen "            "in the current gig file by samples located in the chosen "
2792            "directory.\n\n"            "directory.\n\n"
# Line 1550  void MainWindow::on_action_replace_all_s Line 2800  void MainWindow::on_action_replace_all_s
2800            "the sample in the gig file accordingly. If you don't need an "            "the sample in the gig file accordingly. If you don't need an "
2801            "extension, blank the field below. Any gig sample where no "            "extension, blank the field below. Any gig sample where no "
2802            "appropriate sample file could be found will be reported and left "            "appropriate sample file could be found will be reported and left "
2803            "untouched.\n")            "untouched.\n");
2804      );  #if GTKMM_MAJOR_VERSION < 3
2805        view::WrapLabel description(str);
2806    #else
2807        Gtk::Label description(str);
2808        description.set_line_wrap();
2809    #endif
2810      Gtk::HBox entryArea;      Gtk::HBox entryArea;
2811      Gtk::Label entryLabel( _("Add filename extension: "), Gtk::ALIGN_RIGHT);      Gtk::Label entryLabel( _("Add filename extension: "), Gtk::ALIGN_START);
2812      Gtk::Entry postfixEntryBox;      Gtk::Entry postfixEntryBox;
2813      postfixEntryBox.set_text(".wav");      postfixEntryBox.set_text(".wav");
2814      entryArea.pack_start(entryLabel);      entryArea.pack_start(entryLabel);
# Line 1562  void MainWindow::on_action_replace_all_s Line 2817  void MainWindow::on_action_replace_all_s
2817      dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK);      dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK);
2818      description.show();      description.show();
2819      entryArea.show_all();      entryArea.show_all();
2820      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);      dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
2821      dialog.add_button(_("Select"), Gtk::RESPONSE_OK);      dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
2822      dialog.set_select_multiple(false);      dialog.set_select_multiple(false);
2823      if (current_sample_dir != "") {      if (current_sample_dir != "") {
# Line 1572  void MainWindow::on_action_replace_all_s Line 2827  void MainWindow::on_action_replace_all_s
2827      {      {
2828          current_sample_dir = dialog.get_current_folder();          current_sample_dir = dialog.get_current_folder();
2829          Glib::ustring error_files;          Glib::ustring error_files;
2830          Glib::ustring folder = dialog.get_filename();          std::string folder = dialog.get_filename();
2831          for (gig::Sample* sample = file->GetFirstSample();          for (gig::Sample* sample = file->GetFirstSample();
2832               sample; sample = file->GetNextSample())               sample; sample = file->GetNextSample())
2833          {          {
2834              std::string filename =              std::string filename =
2835                  folder + G_DIR_SEPARATOR_S + sample->pInfo->Name +                  folder + G_DIR_SEPARATOR_S +
2836                  postfixEntryBox.get_text().raw();                  Glib::filename_from_utf8(gig_to_utf8(sample->pInfo->Name) +
2837                                             postfixEntryBox.get_text());
2838              SF_INFO info;              SF_INFO info;
2839              info.format = 0;              info.format = 0;
2840              SNDFILE* hFile = sf_open(filename.c_str(), SFM_READ, &info);              SNDFILE* hFile = sf_open(filename.c_str(), SFM_READ, &info);
2841              try              try
2842              {              {
2843                  if (!hFile) throw std::string("could not open file");                  if (!hFile) throw std::string(_("could not open file"));
                 int bitdepth;  
2844                  switch (info.format & 0xff) {                  switch (info.format & 0xff) {
2845                      case SF_FORMAT_PCM_S8:                      case SF_FORMAT_PCM_S8:
2846                      case SF_FORMAT_PCM_16:                      case SF_FORMAT_PCM_16:
2847                      case SF_FORMAT_PCM_U8:                      case SF_FORMAT_PCM_U8:
                         bitdepth = 16;  
                         break;  
2848                      case SF_FORMAT_PCM_24:                      case SF_FORMAT_PCM_24:
2849                      case SF_FORMAT_PCM_32:                      case SF_FORMAT_PCM_32:
2850                      case SF_FORMAT_FLOAT:                      case SF_FORMAT_FLOAT:
2851                      case SF_FORMAT_DOUBLE:                      case SF_FORMAT_DOUBLE:
                         bitdepth = 24;  
2852                          break;                          break;
2853                      default:                      default:
2854                          sf_close(hFile);                          sf_close(hFile);
2855                          throw std::string("format not supported");                          throw std::string(_("format not supported"));
2856                  }                  }
2857                  SampleImportItem sched_item;                  SampleImportItem sched_item;
2858                  sched_item.gig_sample  = sample;                  sched_item.gig_sample  = sample;
# Line 1611  void MainWindow::on_action_replace_all_s Line 2863  void MainWindow::on_action_replace_all_s
2863              }              }
2864              catch (std::string what)              catch (std::string what)
2865              {              {
2866                  if (error_files.size()) error_files += "\n";                  if (!error_files.empty()) error_files += "\n";
2867                      error_files += filename += " (" + what + ")";                  error_files += Glib::filename_to_utf8(filename) +
2868                        " (" + what + ")";
2869              }              }
2870          }          }
2871          // 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
2872          if (error_files.size()) {          if (!error_files.empty()) {
2873              Glib::ustring txt =              Glib::ustring txt =
2874                  _("Could not replace the following sample(s):\n") + error_files;                  _("Could not replace the following sample(s):\n") + error_files;
2875              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
# Line 1628  void MainWindow::on_action_replace_all_s Line 2881  void MainWindow::on_action_replace_all_s
2881  void MainWindow::on_action_remove_sample() {  void MainWindow::on_action_remove_sample() {
2882      if (!file) return;      if (!file) return;
2883      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2884      Gtk::TreeModel::iterator it = sel->get_selected();      std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
2885      if (it) {      for (int r = rows.size() - 1; r >= 0; --r) {
2886            Gtk::TreeModel::iterator it = m_refSamplesTreeModel->get_iter(rows[r]);
2887            if (!it) continue;
2888          Gtk::TreeModel::Row row = *it;          Gtk::TreeModel::Row row = *it;
2889          gig::Group* group   = row[m_SamplesModel.m_col_group];          gig::Group* group   = row[m_SamplesModel.m_col_group];
2890          gig::Sample* sample = row[m_SamplesModel.m_col_sample];          gig::Sample* sample = row[m_SamplesModel.m_col_sample];
# Line 1637  void MainWindow::on_action_remove_sample Line 2892  void MainWindow::on_action_remove_sample
2892          try {          try {
2893              // remove group or sample from the gig file              // remove group or sample from the gig file
2894              if (group) {              if (group) {
2895                  // temporarily remember the samples that bolong to                  // temporarily remember the samples that belong to
2896                  // that group (we need that to clean the queue)                  // that group (we need that to clean the queue)
2897                  std::list<gig::Sample*> members;                  std::list<gig::Sample*> members;
2898                  for (gig::Sample* pSample = group->GetFirstSample();                  for (gig::Sample* pSample = group->GetFirstSample();
# Line 1701  void MainWindow::on_action_remove_sample Line 2956  void MainWindow::on_action_remove_sample
2956      }      }
2957  }  }
2958    
2959    void MainWindow::on_action_remove_unused_samples() {
2960        if (!file) return;
2961    
2962        // collect all samples that are not referenced by any instrument
2963        std::list<gig::Sample*> lsamples;
2964        for (int iSample = 0; file->GetSample(iSample); ++iSample) {
2965            gig::Sample* sample = file->GetSample(iSample);
2966            bool isUsed = false;
2967            for (gig::Instrument* instrument = file->GetFirstInstrument(); instrument;
2968                                  instrument = file->GetNextInstrument())
2969            {
2970                for (gig::Region* rgn = instrument->GetFirstRegion(); rgn;
2971                                  rgn = instrument->GetNextRegion())
2972                {
2973                    for (int i = 0; i < 256; ++i) {
2974                        if (!rgn->pDimensionRegions[i]) continue;
2975                        if (rgn->pDimensionRegions[i]->pSample != sample) continue;
2976                        isUsed = true;
2977                        goto endOfRefSearch;
2978                    }
2979                }
2980            }
2981            endOfRefSearch:
2982            if (!isUsed) lsamples.push_back(sample);
2983        }
2984    
2985        if (lsamples.empty()) return;
2986    
2987        // notify everybody that we're going to remove these samples
2988        samples_to_be_removed_signal.emit(lsamples);
2989    
2990        // remove collected samples
2991        try {
2992            for (std::list<gig::Sample*>::iterator itSample = lsamples.begin();
2993                 itSample != lsamples.end(); ++itSample)
2994            {
2995                gig::Sample* sample = *itSample;
2996                // remove sample from the .gig file
2997                file->DeleteSample(sample);
2998                // if sample was just previously added, remove it fro the import queue
2999                for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
3000                     iter != m_SampleImportQueue.end(); ++iter)
3001                {
3002                    if ((*iter).gig_sample == sample) {
3003                        printf("Removing previously added sample '%s'\n",
3004                               (*iter).sample_path.c_str());
3005                        m_SampleImportQueue.erase(iter);
3006                        break;
3007                    }
3008                }
3009            }
3010        } catch (RIFF::Exception e) {
3011            // show error message
3012            Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
3013            msg.run();
3014        }
3015    
3016        // notify everybody that we're done with removal
3017        samples_removed_signal.emit();
3018    
3019        dimreg_changed();
3020        file_changed();
3021        __refreshEntireGUI();
3022    }
3023    
3024    // see comment on on_sample_treeview_drag_begin()
3025    void MainWindow::on_scripts_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
3026    {
3027        first_call_to_drag_data_get = true;
3028    }
3029    
3030    void MainWindow::on_scripts_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
3031                                                       Gtk::SelectionData& selection_data, guint, guint)
3032    {
3033        if (!first_call_to_drag_data_get) return;
3034        first_call_to_drag_data_get = false;
3035    
3036        // get selected script
3037        gig::Script* script = NULL;
3038        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
3039        Gtk::TreeModel::iterator it = sel->get_selected();
3040        if (it) {
3041            Gtk::TreeModel::Row row = *it;
3042            script = row[m_ScriptsModel.m_col_script];
3043        }
3044        // pass the gig::Script as pointer
3045        selection_data.set(selection_data.get_target(), 0/*unused*/,
3046                           (const guchar*)&script,
3047                           sizeof(script)/*length of data in bytes*/);
3048    }
3049    
3050    // see comment on on_sample_treeview_drag_begin()
3051    void MainWindow::on_instruments_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
3052    {
3053        first_call_to_drag_data_get = true;
3054    }
3055    
3056    void MainWindow::on_instruments_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
3057                                                           Gtk::SelectionData& selection_data, guint, guint)
3058    {
3059        if (!first_call_to_drag_data_get) return;
3060        first_call_to_drag_data_get = false;
3061    
3062        // get selected source instrument
3063        gig::Instrument* src = NULL;
3064        {
3065            Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
3066            std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
3067            if (!rows.empty()) {
3068                Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(rows[0]);
3069                if (it) {
3070                    Gtk::TreeModel::Row row = *it;
3071                    src = row[m_Columns.m_col_instr];
3072                }
3073            }
3074        }
3075        if (!src) return;
3076    
3077        // pass the source gig::Instrument as pointer
3078        selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&src,
3079                           sizeof(src)/*length of data in bytes*/);
3080    }
3081    
3082    void MainWindow::on_instruments_treeview_drop_drag_data_received(
3083        const Glib::RefPtr<Gdk::DragContext>& context, int x, int y,
3084        const Gtk::SelectionData& selection_data, guint, guint time)
3085    {
3086        gig::Instrument* src = *((gig::Instrument**) selection_data.get_data());
3087        if (!src || selection_data.get_length() != sizeof(gig::Instrument*))
3088            return;
3089    
3090        gig::Instrument* dst = NULL;
3091        {
3092            Gtk::TreeModel::Path path;
3093            const bool found = m_TreeView.get_path_at_pos(x, y, path);
3094            if (!found) return;
3095    
3096            Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path);
3097            if (!iter) return;
3098            Gtk::TreeModel::Row row = *iter;
3099            dst = row[m_Columns.m_col_instr];
3100        }
3101        if (!dst) return;
3102    
3103        //printf("dragdrop received src=%s dst=%s\n", src->pInfo->Name.c_str(), dst->pInfo->Name.c_str());
3104        src->MoveTo(dst);
3105        __refreshEntireGUI();
3106        select_instrument(src);
3107    }
3108    
3109  // For some reason drag_data_get gets called two times for each  // For some reason drag_data_get gets called two times for each
3110  // drag'n'drop (at least when target is an Entry). This work-around  // drag'n'drop (at least when target is an Entry). This work-around
3111  // makes sure the code in drag_data_get and drop_drag_data_received is  // makes sure the code in drag_data_get and drop_drag_data_received is
# Line 1719  void MainWindow::on_sample_treeview_drag Line 3124  void MainWindow::on_sample_treeview_drag
3124      // get selected sample      // get selected sample
3125      gig::Sample* sample = NULL;      gig::Sample* sample = NULL;
3126      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
3127      Gtk::TreeModel::iterator it = sel->get_selected();      std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
3128      if (it) {      if (!rows.empty()) {
3129          Gtk::TreeModel::Row row = *it;          Gtk::TreeModel::iterator it = m_refSamplesTreeModel->get_iter(rows[0]);
3130          sample = row[m_SamplesModel.m_col_sample];          if (it) {
3131                Gtk::TreeModel::Row row = *it;
3132                sample = row[m_SamplesModel.m_col_sample];
3133            }
3134      }      }
3135      // pass the gig::Sample as pointer      // pass the gig::Sample as pointer
3136      selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,      selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
# Line 1759  void MainWindow::on_sample_label_drop_dr Line 3167  void MainWindow::on_sample_label_drop_dr
3167          bool channels_changed = false;          bool channels_changed = false;
3168          if (sample->Channels == 1 && stereo_dimension) {          if (sample->Channels == 1 && stereo_dimension) {
3169              // remove the samplechannel dimension              // remove the samplechannel dimension
3170    /* commented out, because it makes it impossible building up an instrument from scratch using two separate L/R samples
3171              region->DeleteDimension(stereo_dimension);              region->DeleteDimension(stereo_dimension);
3172              channels_changed = true;              channels_changed = true;
3173              region_changed();              region_changed();
3174    */
3175          }          }
3176          dimreg_edit.set_sample(sample);          dimreg_edit.set_sample(
3177                sample,
3178                is_copy_samples_unity_note_enabled(),
3179                is_copy_samples_fine_tune_enabled(),
3180                is_copy_samples_loop_enabled()
3181            );
3182    
3183          if (sample->Channels == 2 && !stereo_dimension) {          if (sample->Channels == 2 && !stereo_dimension) {
3184              // add samplechannel dimension              // add samplechannel dimension
# Line 1806  void MainWindow::sample_name_changed(con Line 3221  void MainWindow::sample_name_changed(con
3221      Glib::ustring name  = row[m_SamplesModel.m_col_name];      Glib::ustring name  = row[m_SamplesModel.m_col_name];
3222      gig::Group* group   = row[m_SamplesModel.m_col_group];      gig::Group* group   = row[m_SamplesModel.m_col_group];
3223      gig::Sample* sample = row[m_SamplesModel.m_col_sample];      gig::Sample* sample = row[m_SamplesModel.m_col_sample];
3224        gig::String gigname(gig_from_utf8(name));
3225      if (group) {      if (group) {
3226          if (group->Name != name) {          if (group->Name != gigname) {
3227              group->Name = name;              group->Name = gigname;
3228              printf("group name changed\n");              printf("group name changed\n");
3229              file_changed();              file_changed();
3230          }          }
3231      } else if (sample) {      } else if (sample) {
3232          if (sample->pInfo->Name != name.raw()) {          if (sample->pInfo->Name != gigname) {
3233              sample->pInfo->Name = name.raw();              sample->pInfo->Name = gigname;
3234              printf("sample name changed\n");              printf("sample name changed\n");
3235              file_changed();              file_changed();
3236          }          }
3237      }      }
3238  }  }
3239    
3240    void MainWindow::script_name_changed(const Gtk::TreeModel::Path& path,
3241                                         const Gtk::TreeModel::iterator& iter) {
3242        if (!iter) return;
3243        Gtk::TreeModel::Row row = *iter;
3244        Glib::ustring name      = row[m_ScriptsModel.m_col_name];
3245        gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
3246        gig::Script* script     = row[m_ScriptsModel.m_col_script];
3247        gig::String gigname(gig_from_utf8(name));
3248        if (group) {
3249            if (group->Name != gigname) {
3250                group->Name = gigname;
3251                printf("script group name changed\n");
3252                file_changed();
3253            }
3254        } else if (script) {
3255            if (script->Name != gigname) {
3256                script->Name = gigname;
3257                printf("script name changed\n");
3258                file_changed();
3259            }
3260        }
3261    }
3262    
3263    void MainWindow::script_double_clicked(const Gtk::TreeModel::Path& path,
3264                                           Gtk::TreeViewColumn* column)
3265    {
3266        Gtk::TreeModel::iterator iter = m_refScriptsTreeModel->get_iter(path);
3267        if (!iter) return;
3268        Gtk::TreeModel::Row row = *iter;
3269        gig::Script* script = row[m_ScriptsModel.m_col_script];
3270        if (!script) return;
3271    
3272        ScriptEditor* editor = new ScriptEditor;
3273        editor->signal_script_to_be_changed.connect(
3274            signal_script_to_be_changed.make_slot()
3275        );
3276        editor->signal_script_changed.connect(
3277            signal_script_changed.make_slot()
3278        );
3279        editor->setScript(script);
3280        //editor->reparent(*this);
3281        editor->show();
3282    }
3283    
3284  void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,  void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
3285                                           const Gtk::TreeModel::iterator& iter) {                                           const Gtk::TreeModel::iterator& iter) {
3286      if (!iter) return;      if (!iter) return;
3287      Gtk::TreeModel::Row row = *iter;      Gtk::TreeModel::Row row = *iter;
3288      Glib::ustring name = row[m_Columns.m_col_name];      Glib::ustring name = row[m_Columns.m_col_name];
3289    
3290        // change name in instrument menu
3291        int index = path[0];
3292        const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
3293        if (index < children.size()) {
3294    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 16) || GTKMM_MAJOR_VERSION > 2
3295            static_cast<Gtk::RadioMenuItem*>(children[index])->set_label(name);
3296    #else
3297            remove_instrument_from_menu(index);
3298            Gtk::RadioMenuItem* item = add_instrument_to_menu(name, index);
3299            item->set_active();
3300    #endif
3301        }
3302    
3303        // change name in gig
3304      gig::Instrument* instrument = row[m_Columns.m_col_instr];      gig::Instrument* instrument = row[m_Columns.m_col_instr];
3305      if (instrument && instrument->pInfo->Name != name.raw()) {      gig::String gigname(gig_from_utf8(name));
3306          instrument->pInfo->Name = name.raw();      if (instrument && instrument->pInfo->Name != gigname) {
3307            instrument->pInfo->Name = gigname;
3308    
3309            // change name in the instrument properties window
3310            if (instrumentProps.get_instrument() == instrument) {
3311                instrumentProps.update_name();
3312            }
3313    
3314          file_changed();          file_changed();
3315      }      }
3316  }  }
3317    
3318    void MainWindow::on_action_combine_instruments() {
3319        CombineInstrumentsDialog* d = new CombineInstrumentsDialog(*this, file);
3320        d->show_all();
3321        d->resize(500, 400);
3322        d->run();
3323        if (d->fileWasChanged()) {
3324            // update GUI with new instrument just created
3325            add_instrument(d->newCombinedInstrument());
3326        }
3327        delete d;
3328    }
3329    
3330    void MainWindow::on_action_view_references() {
3331        Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
3332        std::vector<Gtk::TreeModel::Path> rows = sel->get_selected_rows();
3333        if (rows.empty()) return;
3334        Gtk::TreeModel::iterator it = m_refSamplesTreeModel->get_iter(rows[0]);
3335        if (!it) return;
3336        Gtk::TreeModel::Row row = *it;
3337        gig::Sample* sample = row[m_SamplesModel.m_col_sample];
3338        if (!sample) return;
3339    
3340        ReferencesView* d = new ReferencesView(*this);
3341        d->setSample(sample);
3342        d->dimension_region_selected.connect(
3343            sigc::mem_fun(*this, &MainWindow::select_dimension_region)
3344        );
3345        d->show_all();
3346        d->resize(500, 400);
3347        d->run();
3348        delete d;
3349    }
3350    
3351    void MainWindow::mergeFiles(const std::vector<std::string>& filenames) {
3352        struct _Source {
3353            std::vector<RIFF::File*> riffs;
3354            std::vector<gig::File*> gigs;
3355            
3356            ~_Source() {
3357                for (int k = 0; k < gigs.size(); ++k) delete gigs[k];
3358                for (int k = 0; k < riffs.size(); ++k) delete riffs[k];
3359                riffs.clear();
3360                gigs.clear();
3361            }
3362        } sources;
3363    
3364        if (filenames.empty())
3365            throw RIFF::Exception(_("No files selected, so nothing done."));
3366    
3367        // first open all input files (to avoid output file corruption)
3368        int i;
3369        try {
3370            for (i = 0; i < filenames.size(); ++i) {
3371                const std::string& filename = filenames[i];
3372                printf("opening file=%s\n", filename.c_str());
3373    
3374                RIFF::File* riff = new RIFF::File(filename);
3375                sources.riffs.push_back(riff);
3376    
3377                gig::File* gig = new gig::File(riff);
3378                sources.gigs.push_back(gig);
3379            }
3380        } catch (RIFF::Exception e) {
3381            throw RIFF::Exception(
3382                _("Error occurred while opening '") +
3383                filenames[i] +
3384                "': " +
3385                e.Message
3386            );
3387        } catch (...) {
3388            throw RIFF::Exception(
3389                _("Unknown exception occurred while opening '") +
3390                filenames[i] + "'"
3391            );
3392        }
3393    
3394        // now merge the opened .gig files to the main .gig file currently being
3395        // open in gigedit
3396        try {
3397            for (i = 0; i < filenames.size(); ++i) {
3398                const std::string& filename = filenames[i];
3399                printf("merging file=%s\n", filename.c_str());
3400                assert(i < sources.gigs.size());
3401    
3402                this->file->AddContentOf(sources.gigs[i]);
3403            }
3404        } catch (RIFF::Exception e) {
3405            throw RIFF::Exception(
3406                _("Error occurred while merging '") +
3407                filenames[i] +
3408                "': " +
3409                e.Message
3410            );
3411        } catch (...) {
3412            throw RIFF::Exception(
3413                _("Unknown exception occurred while merging '") +
3414                filenames[i] + "'"
3415            );
3416        }
3417    
3418        // Finally save gig file persistently to disk ...
3419        //NOTE: requires that this gig file already has a filename !
3420        {
3421            std::cout << "Saving file\n" << std::flush;
3422            file_structure_to_be_changed_signal.emit(this->file);
3423    
3424            progress_dialog = new ProgressDialog( //FIXME: memory leak!
3425                _("Saving") +  Glib::ustring(" '") +
3426                Glib::filename_display_basename(this->filename) + "' ...",
3427                *this
3428            );
3429            progress_dialog->show_all();
3430            saver = new Saver(this->file); //FIXME: memory leak!
3431            saver->signal_progress().connect(
3432                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
3433            saver->signal_finished().connect(
3434                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
3435            saver->signal_error().connect(
3436                sigc::mem_fun(*this, &MainWindow::on_saver_error));
3437            saver->launch();
3438        }
3439    }
3440    
3441    void MainWindow::on_action_merge_files() {
3442        if (this->file->GetFileName().empty()) {
3443            Glib::ustring txt = _(
3444                "You seem to have a new .gig file open that has not been saved "
3445                "yet. You must save it somewhere before starting to merge it with "
3446                "other .gig files though, because during the merge operation the "
3447                "other files' sample data must be written on file level to the "
3448                "target .gig file."
3449            );
3450            Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
3451            msg.run();
3452            return;
3453        }
3454    
3455        Gtk::FileChooserDialog dialog(*this, _("Merge .gig files"));
3456        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
3457        dialog.add_button(_("Merge"), Gtk::RESPONSE_OK);
3458        dialog.set_default_response(Gtk::RESPONSE_CANCEL);
3459    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
3460        Gtk::FileFilter filter;
3461        filter.add_pattern("*.gig");
3462    #else
3463        Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
3464        filter->add_pattern("*.gig");
3465    #endif
3466        dialog.set_filter(filter);
3467        if (current_gig_dir != "") {
3468            dialog.set_current_folder(current_gig_dir);
3469        }
3470        dialog.set_select_multiple(true);
3471    
3472        // show warning in the file picker dialog
3473        Gtk::HBox descriptionArea;
3474        descriptionArea.set_spacing(15);
3475        Gtk::Image warningIcon;
3476        warningIcon.set_from_icon_name("dialog-warning",
3477                                       Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
3478        descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
3479    #if GTKMM_MAJOR_VERSION < 3
3480        view::WrapLabel description;
3481    #else
3482        Gtk::Label description;
3483        description.set_line_wrap();
3484    #endif
3485        description.set_markup(_(
3486            "\nSelect at least one .gig file that shall be merged to the .gig file "
3487            "currently being open in gigedit.\n\n"
3488            "<b>Please Note:</b> Merging with other files will modify your "
3489            "currently open .gig file on file level! And be aware that the current "
3490            "merge algorithm does not detect duplicate samples yet. So if you are "
3491            "merging files which are using equivalent sample data, those "
3492            "equivalent samples will currently be treated as separate samples and "
3493            "will accordingly be stored separately in the target .gig file!"
3494        ));
3495        descriptionArea.pack_start(description);
3496        dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
3497        descriptionArea.show_all();
3498    
3499        if (dialog.run() == Gtk::RESPONSE_OK) {
3500            printf("on_action_merge_files self=%p\n",
3501                   static_cast<void*>(Glib::Threads::Thread::self()));
3502            std::vector<std::string> filenames = dialog.get_filenames();
3503    
3504            // merge the selected files to the currently open .gig file
3505            try {
3506                mergeFiles(filenames);
3507            } catch (RIFF::Exception e) {
3508                Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
3509                msg.run();
3510            }
3511    
3512            // update GUI
3513            __refreshEntireGUI();
3514        }
3515    }
3516    
3517  void MainWindow::set_file_is_shared(bool b) {  void MainWindow::set_file_is_shared(bool b) {
3518      this->file_is_shared = b;      this->file_is_shared = b;
3519    
# Line 1847  void MainWindow::set_file_is_shared(bool Line 3528  void MainWindow::set_file_is_shared(bool
3528              Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)              Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)
3529          );          );
3530      }      }
3531    
3532        {
3533            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
3534                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
3535            if (item) item->set_sensitive(b);
3536        }
3537    }
3538    
3539    void MainWindow::on_sample_ref_count_incremented(gig::Sample* sample, int offset) {
3540        if (!sample) return;
3541        sample_ref_count[sample] += offset;
3542        const int refcount = sample_ref_count[sample];
3543    
3544        Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
3545        for (int g = 0; g < model->children().size(); ++g) {
3546            Gtk::TreeModel::Row rowGroup = model->children()[g];
3547            for (int s = 0; s < rowGroup.children().size(); ++s) {
3548                Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
3549                if (rowSample[m_SamplesModel.m_col_sample] != sample) continue;
3550                rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
3551                rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
3552            }
3553        }
3554    }
3555    
3556    void MainWindow::on_sample_ref_changed(gig::Sample* oldSample, gig::Sample* newSample) {
3557        on_sample_ref_count_incremented(oldSample, -1);
3558        on_sample_ref_count_incremented(newSample, +1);
3559    }
3560    
3561    void MainWindow::on_samples_to_be_removed(std::list<gig::Sample*> samples) {
3562        // just in case a new sample is added later with exactly the same memory
3563        // address, which would lead to incorrect refcount if not deleted here
3564        for (std::list<gig::Sample*>::const_iterator it = samples.begin();
3565             it != samples.end(); ++it)
3566        {
3567            sample_ref_count.erase(*it);
3568        }
3569    }
3570    
3571    void MainWindow::show_samples_tab() {
3572        m_TreeViewNotebook.set_current_page(0);
3573    }
3574    
3575    void MainWindow::show_intruments_tab() {
3576        m_TreeViewNotebook.set_current_page(1);
3577    }
3578    
3579    void MainWindow::show_scripts_tab() {
3580        m_TreeViewNotebook.set_current_page(2);
3581  }  }
3582    
3583  sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_to_be_changed() {  sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_to_be_changed() {
# Line 1873  sigc::signal<void, gig::Region*>& MainWi Line 3604  sigc::signal<void, gig::Region*>& MainWi
3604      return region_changed_signal;      return region_changed_signal;
3605  }  }
3606    
3607    sigc::signal<void, gig::Sample*>& MainWindow::signal_sample_changed() {
3608        return sample_changed_signal;
3609    }
3610    
3611  sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& MainWindow::signal_sample_ref_changed() {  sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& MainWindow::signal_sample_ref_changed() {
3612      return sample_ref_changed_signal;      return sample_ref_changed_signal;
3613  }  }
# Line 1900  sigc::signal<void, int/*key*/, int/*velo Line 3635  sigc::signal<void, int/*key*/, int/*velo
3635  sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {  sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {
3636      return m_RegionChooser.signal_keyboard_key_released();      return m_RegionChooser.signal_keyboard_key_released();
3637  }  }
3638    
3639    sigc::signal<void, gig::Instrument*>& MainWindow::signal_switch_sampler_instrument() {
3640        return switch_sampler_instrument_signal;
3641    }

Legend:
Removed from v.1799  
changed lines
  Added in v.3109

  ViewVC Help
Powered by ViewVC