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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2845 - (hide annotations) (download)
Sun Sep 20 10:18:22 2015 UTC (8 years, 6 months ago) by persson
File size: 131040 byte(s)
* avoid using gtk stock items, as they are deprecated in gtk 3.10

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

  ViewVC Help
Powered by ViewVC