/[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 2772 - (hide annotations) (download)
Thu Jun 11 20:29:22 2015 UTC (8 years, 10 months ago) by schoenebeck
File size: 129684 byte(s)
* Fixed file name disappearing from title bar on certain actions.
* Added new menu item "View"->"Refresh All".
* Added new menu item "Samples"->"Remove Unused Samples".

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

  ViewVC Help
Powered by ViewVC