/[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 2683 - (hide annotations) (download)
Mon Dec 29 16:30:21 2014 UTC (9 years, 3 months ago) by schoenebeck
File size: 116967 byte(s)
* Fixed potential crash if exception was thrown while opening a gig file.
* Added progress bar while saving a gig file.

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

  ViewVC Help
Powered by ViewVC