/[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 2666 - (hide annotations) (download)
Mon Jul 7 15:01:01 2014 UTC (9 years, 8 months ago) by schoenebeck
File size: 112020 byte(s)
* Fixed endless loop when deleting a sample.

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 1225 printf("Start %s\n", filename);
712     RIFF::File* riff = new RIFF::File(filename);
713     gig = new gig::File(riff);
714     gig::progress_t progress;
715     progress.callback = loader_progress_callback;
716     progress.custom = this;
717    
718     gig->GetInstrument(0, &progress);
719     printf("End\n");
720     finished_dispatcher();
721     }
722    
723     Loader::Loader(const char* filename)
724 persson 2151 : filename(filename), thread(0)
725 schoenebeck 1225 {
726     }
727    
728     void Loader::launch()
729     {
730 persson 2332 #ifdef OLD_THREADS
731     thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
732     #else
733     thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Loader::thread_function));
734     #endif
735 schoenebeck 1225 printf("launch thread=%x\n", thread);
736     }
737    
738     float Loader::get_progress()
739     {
740     float res;
741     {
742 persson 2325 Glib::Threads::Mutex::Lock lock(progressMutex);
743 schoenebeck 1225 res = progress;
744     }
745     return res;
746     }
747    
748     Glib::Dispatcher& Loader::signal_progress()
749     {
750     return progress_dispatcher;
751     }
752    
753     Glib::Dispatcher& Loader::signal_finished()
754     {
755     return finished_dispatcher;
756     }
757    
758     LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)
759     : Gtk::Dialog(title, parent, true)
760     {
761     get_vbox()->pack_start(progressBar);
762     show_all_children();
763     }
764    
765     // Clear all GUI elements / controls. This method is typically called
766     // before a new .gig file is to be created or to be loaded.
767     void MainWindow::__clear() {
768     // forget all samples that ought to be imported
769     m_SampleImportQueue.clear();
770     // clear the samples and instruments tree views
771     m_refTreeModel->clear();
772     m_refSamplesTreeModel->clear();
773 schoenebeck 2615 m_refScriptsTreeModel->clear();
774 persson 2442 // remove all entries from "Instrument" menu
775     while (!instrument_menu->get_children().empty()) {
776     remove_instrument_from_menu(0);
777     }
778 schoenebeck 1225 // free libgig's gig::File instance
779 schoenebeck 1382 if (file && !file_is_shared) delete file;
780     file = NULL;
781 schoenebeck 1411 set_file_is_shared(false);
782 schoenebeck 1225 }
783    
784 schoenebeck 2553 void MainWindow::__refreshEntireGUI() {
785     // clear the samples and instruments tree views
786     m_refTreeModel->clear();
787     m_refSamplesTreeModel->clear();
788 schoenebeck 2615 m_refScriptsTreeModel->clear();
789 schoenebeck 2553 // remove all entries from "Instrument" menu
790     while (!instrument_menu->get_children().empty()) {
791     remove_instrument_from_menu(0);
792     }
793    
794     if (!this->file) return;
795    
796     load_gig(
797     this->file, this->file->pInfo->Name.c_str(), this->file_is_shared
798     );
799     }
800    
801 schoenebeck 1225 void MainWindow::on_action_file_new()
802     {
803 schoenebeck 1382 if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
804 persson 1261
805 schoenebeck 1382 if (file_is_shared && !leaving_shared_mode_dialog()) return;
806    
807 schoenebeck 1225 // clear all GUI elements
808     __clear();
809     // create a new .gig file (virtually yet)
810     gig::File* pFile = new gig::File;
811     // already add one new instrument by default
812     gig::Instrument* pInstrument = pFile->AddInstrument();
813 persson 2446 pInstrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument"));
814 schoenebeck 1225 // update GUI with that new gig::File
815 persson 1261 load_gig(pFile, 0 /*no file name yet*/);
816 schoenebeck 1225 }
817    
818 persson 1261 bool MainWindow::close_confirmation_dialog()
819     {
820     gchar* msg = g_strdup_printf(_("Save changes to \"%s\" before closing?"),
821     Glib::filename_display_basename(filename).c_str());
822     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
823     g_free(msg);
824     dialog.set_secondary_text(_("If you close without saving, your changes will be lost."));
825     dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);
826     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
827     dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES);
828     dialog.set_default_response(Gtk::RESPONSE_YES);
829     int response = dialog.run();
830 persson 1303 dialog.hide();
831 persson 1261 if (response == Gtk::RESPONSE_YES) return file_save();
832     return response != Gtk::RESPONSE_CANCEL;
833     }
834    
835 schoenebeck 1382 bool MainWindow::leaving_shared_mode_dialog() {
836     Glib::ustring msg = _("Detach from sampler and proceed working stand-alone?");
837     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
838     dialog.set_secondary_text(
839     _("If you proceed to work on another instrument file, it won't be "
840     "used by the sampler until you tell the sampler explicitly to "
841 persson 2151 "load it."));
842 schoenebeck 1382 dialog.add_button(_("_Yes, Detach"), Gtk::RESPONSE_YES);
843     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
844     dialog.set_default_response(Gtk::RESPONSE_CANCEL);
845     int response = dialog.run();
846     dialog.hide();
847     return response == Gtk::RESPONSE_YES;
848     }
849    
850 schoenebeck 1225 void MainWindow::on_action_file_open()
851     {
852 schoenebeck 1382 if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
853 persson 1261
854 schoenebeck 1382 if (file_is_shared && !leaving_shared_mode_dialog()) return;
855    
856 schoenebeck 1225 Gtk::FileChooserDialog dialog(*this, _("Open file"));
857     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
858     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
859 persson 1261 dialog.set_default_response(Gtk::RESPONSE_OK);
860 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
861 schoenebeck 1225 Gtk::FileFilter filter;
862     filter.add_pattern("*.gig");
863 persson 2169 #else
864     Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
865     filter->add_pattern("*.gig");
866     #endif
867 schoenebeck 1225 dialog.set_filter(filter);
868 persson 1725 if (current_gig_dir != "") {
869     dialog.set_current_folder(current_gig_dir);
870 persson 1261 }
871 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
872 persson 1261 std::string filename = dialog.get_filename();
873     printf("filename=%s\n", filename.c_str());
874 persson 2325 printf("on_action_file_open self=%x\n", Glib::Threads::Thread::self());
875 persson 1261 load_file(filename.c_str());
876 persson 1725 current_gig_dir = Glib::path_get_dirname(filename);
877 schoenebeck 1225 }
878     }
879    
880     void MainWindow::load_file(const char* name)
881     {
882 persson 1303 __clear();
883 persson 1831 load_dialog = new LoadDialog(_("Loading..."), *this);
884 schoenebeck 1225 load_dialog->show_all();
885     loader = new Loader(strdup(name));
886     loader->signal_progress().connect(
887     sigc::mem_fun(*this, &MainWindow::on_loader_progress));
888     loader->signal_finished().connect(
889     sigc::mem_fun(*this, &MainWindow::on_loader_finished));
890     loader->launch();
891     }
892    
893     void MainWindow::load_instrument(gig::Instrument* instr) {
894     if (!instr) {
895     Glib::ustring txt = "Provided instrument is NULL!\n";
896     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
897     msg.run();
898     Gtk::Main::quit();
899     }
900 schoenebeck 1328 // clear all GUI elements
901     __clear();
902     // load the instrument
903 schoenebeck 1225 gig::File* pFile = (gig::File*) instr->GetParent();
904 schoenebeck 1382 load_gig(pFile, 0 /*file name*/, true /*shared instrument*/);
905 schoenebeck 2664 // automatically select the given instrument
906     int i = 0;
907     for (gig::Instrument* instrument = pFile->GetFirstInstrument(); instrument;
908     instrument = pFile->GetNextInstrument(), ++i)
909     {
910     if (instrument == instr) {
911     // select item in "instruments" tree view
912     m_TreeView.get_selection()->select(Gtk::TreePath(ToString(i)));
913     // make sure the selected item in the "instruments" tree view is
914     // visible (scroll to it)
915     m_TreeView.scroll_to_row(Gtk::TreePath(ToString(i)));
916     // select item in instrument menu
917     {
918     const std::vector<Gtk::Widget*> children =
919     instrument_menu->get_children();
920     static_cast<Gtk::RadioMenuItem*>(children[i])->set_active();
921     }
922     // update region chooser and dimension region chooser
923     m_RegionChooser.set_instrument(instr);
924     break;
925     }
926     }
927 schoenebeck 1225 }
928    
929     void MainWindow::on_loader_progress()
930     {
931     load_dialog->set_fraction(loader->get_progress());
932     }
933    
934     void MainWindow::on_loader_finished()
935     {
936     printf("Loader finished!\n");
937 persson 2325 printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());
938 schoenebeck 1225 load_gig(loader->gig, loader->filename);
939     load_dialog->hide();
940     }
941    
942     void MainWindow::on_action_file_save()
943     {
944 persson 1261 file_save();
945     }
946    
947 persson 1303 bool MainWindow::check_if_savable()
948     {
949     if (!file) return false;
950    
951     if (!file->GetFirstSample()) {
952     Gtk::MessageDialog(*this, _("The file could not be saved "
953     "because it contains no samples"),
954     false, Gtk::MESSAGE_ERROR).run();
955     return false;
956     }
957    
958     for (gig::Instrument* instrument = file->GetFirstInstrument() ; instrument ;
959     instrument = file->GetNextInstrument()) {
960     if (!instrument->GetFirstRegion()) {
961     Gtk::MessageDialog(*this, _("The file could not be saved "
962     "because there are instruments "
963     "that have no regions"),
964     false, Gtk::MESSAGE_ERROR).run();
965     return false;
966     }
967     }
968     return true;
969     }
970    
971 persson 1261 bool MainWindow::file_save()
972     {
973 persson 1303 if (!check_if_savable()) return false;
974 schoenebeck 1382 if (!file_is_shared && !file_has_name) return file_save_as();
975 persson 1261
976 schoenebeck 1225 std::cout << "Saving file\n" << std::flush;
977 schoenebeck 1322 file_structure_to_be_changed_signal.emit(this->file);
978 schoenebeck 1225 try {
979     file->Save();
980 persson 1261 if (file_is_changed) {
981     set_title(get_title().substr(1));
982     file_is_changed = false;
983     }
984 schoenebeck 1225 } catch (RIFF::Exception e) {
985 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
986 schoenebeck 1382 Glib::ustring txt = _("Could not save file: ") + e.Message;
987 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
988     msg.run();
989 persson 1261 return false;
990 schoenebeck 1225 }
991     std::cout << "Saving file done\n" << std::flush;
992     __import_queued_samples();
993 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
994 persson 1261 return true;
995 schoenebeck 1225 }
996    
997     void MainWindow::on_action_file_save_as()
998     {
999 persson 1303 if (!check_if_savable()) return;
1000 persson 1261 file_save_as();
1001     }
1002    
1003     bool MainWindow::file_save_as()
1004     {
1005     Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1006 schoenebeck 1225 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1007     dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
1008 persson 1261 dialog.set_default_response(Gtk::RESPONSE_OK);
1009 persson 2151 dialog.set_do_overwrite_confirmation();
1010 persson 1261
1011 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
1012 schoenebeck 1225 Gtk::FileFilter filter;
1013     filter.add_pattern("*.gig");
1014 persson 2169 #else
1015     Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
1016     filter->add_pattern("*.gig");
1017     #endif
1018 schoenebeck 1225 dialog.set_filter(filter);
1019 persson 1261
1020 schoenebeck 1679 // set initial dir and filename of the Save As dialog
1021     // and prepare that initial filename as a copy of the gig
1022     {
1023     std::string basename = Glib::path_get_basename(filename);
1024     std::string dir = Glib::path_get_dirname(filename);
1025 persson 1831 basename = std::string(_("copy_of_")) + basename;
1026 schoenebeck 1679 Glib::ustring copyFileName = Glib::build_filename(dir, basename);
1027     if (Glib::path_is_absolute(filename)) {
1028     dialog.set_filename(copyFileName);
1029     } else {
1030 persson 1725 if (current_gig_dir != "") dialog.set_current_folder(current_gig_dir);
1031 schoenebeck 1679 }
1032     dialog.set_current_name(Glib::filename_display_basename(copyFileName));
1033 persson 1261 }
1034    
1035 schoenebeck 1679 // show warning in the dialog
1036     Gtk::HBox descriptionArea;
1037     descriptionArea.set_spacing(15);
1038     Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
1039     descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
1040 persson 2344 #if GTKMM_MAJOR_VERSION < 3
1041 persson 1799 view::WrapLabel description;
1042 persson 2344 #else
1043     Gtk::Label description;
1044     description.set_line_wrap();
1045     #endif
1046 schoenebeck 1679 description.set_markup(
1047     _("\n<b>CAUTION:</b> You <b>MUST</b> use the "
1048     "<span style=\"italic\">\"Save\"</span> dialog instead of "
1049     "<span style=\"italic\">\"Save As...\"</span> if you want to save "
1050     "to the same .gig file. Using "
1051     "<span style=\"italic\">\"Save As...\"</span> for writing to the "
1052     "same .gig file will end up in corrupted sample wave data!\n")
1053     );
1054 persson 1799 descriptionArea.pack_start(description);
1055 schoenebeck 1679 dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
1056 persson 1799 descriptionArea.show_all();
1057 schoenebeck 1679
1058 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
1059 schoenebeck 1322 file_structure_to_be_changed_signal.emit(this->file);
1060 schoenebeck 1225 try {
1061 persson 1261 std::string filename = dialog.get_filename();
1062     if (!Glib::str_has_suffix(filename, ".gig")) {
1063     filename += ".gig";
1064     }
1065     printf("filename=%s\n", filename.c_str());
1066     file->Save(filename);
1067     this->filename = filename;
1068 persson 1725 current_gig_dir = Glib::path_get_dirname(filename);
1069 persson 1261 set_title(Glib::filename_display_basename(filename));
1070     file_has_name = true;
1071     file_is_changed = false;
1072 schoenebeck 1225 } catch (RIFF::Exception e) {
1073 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
1074 schoenebeck 1382 Glib::ustring txt = _("Could not save file: ") + e.Message;
1075 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1076     msg.run();
1077 persson 1261 return false;
1078 schoenebeck 1225 }
1079     __import_queued_samples();
1080 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
1081 persson 1261 return true;
1082 schoenebeck 1225 }
1083 persson 1261 return false;
1084 schoenebeck 1225 }
1085    
1086     // actually write the sample(s)' data to the gig file
1087     void MainWindow::__import_queued_samples() {
1088     std::cout << "Starting sample import\n" << std::flush;
1089     Glib::ustring error_files;
1090     printf("Samples to import: %d\n", m_SampleImportQueue.size());
1091     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1092     iter != m_SampleImportQueue.end(); ) {
1093     printf("Importing sample %s\n",(*iter).sample_path.c_str());
1094     SF_INFO info;
1095     info.format = 0;
1096     SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
1097 persson 2398 sf_command(hFile, SFC_SET_SCALE_FLOAT_INT_READ, 0, SF_TRUE);
1098 schoenebeck 1225 try {
1099 persson 1831 if (!hFile) throw std::string(_("could not open file"));
1100 schoenebeck 1225 // determine sample's bit depth
1101     int bitdepth;
1102     switch (info.format & 0xff) {
1103     case SF_FORMAT_PCM_S8:
1104     case SF_FORMAT_PCM_16:
1105 persson 1265 case SF_FORMAT_PCM_U8:
1106 schoenebeck 1225 bitdepth = 16;
1107     break;
1108     case SF_FORMAT_PCM_24:
1109     case SF_FORMAT_PCM_32:
1110     case SF_FORMAT_FLOAT:
1111     case SF_FORMAT_DOUBLE:
1112 persson 1265 bitdepth = 24;
1113 schoenebeck 1225 break;
1114     default:
1115     sf_close(hFile); // close sound file
1116 persson 1831 throw std::string(_("format not supported")); // unsupported subformat (yet?)
1117 schoenebeck 1225 }
1118 persson 1265
1119     const int bufsize = 10000;
1120 schoenebeck 1225 switch (bitdepth) {
1121 persson 1265 case 16: {
1122     short* buffer = new short[bufsize * info.channels];
1123     sf_count_t cnt = info.frames;
1124     while (cnt) {
1125     // libsndfile does the conversion for us (if needed)
1126     int n = sf_readf_short(hFile, buffer, bufsize);
1127     // write from buffer directly (physically) into .gig file
1128     iter->gig_sample->Write(buffer, n);
1129     cnt -= n;
1130     }
1131     delete[] buffer;
1132 schoenebeck 1225 break;
1133 persson 1265 }
1134     case 24: {
1135     int* srcbuf = new int[bufsize * info.channels];
1136     uint8_t* dstbuf = new uint8_t[bufsize * 3 * info.channels];
1137     sf_count_t cnt = info.frames;
1138     while (cnt) {
1139     // libsndfile returns 32 bits, convert to 24
1140     int n = sf_readf_int(hFile, srcbuf, bufsize);
1141     int j = 0;
1142     for (int i = 0 ; i < n * info.channels ; i++) {
1143     dstbuf[j++] = srcbuf[i] >> 8;
1144     dstbuf[j++] = srcbuf[i] >> 16;
1145     dstbuf[j++] = srcbuf[i] >> 24;
1146     }
1147     // write from buffer directly (physically) into .gig file
1148     iter->gig_sample->Write(dstbuf, n);
1149     cnt -= n;
1150     }
1151     delete[] srcbuf;
1152     delete[] dstbuf;
1153 schoenebeck 1225 break;
1154 persson 1265 }
1155 schoenebeck 1225 }
1156     // cleanup
1157     sf_close(hFile);
1158 schoenebeck 1853 // let the sampler re-cache the sample if needed
1159     sample_changed_signal.emit(iter->gig_sample);
1160 schoenebeck 1225 // on success we remove the sample from the import queue,
1161     // otherwise keep it, maybe it works the next time ?
1162     std::list<SampleImportItem>::iterator cur = iter;
1163     ++iter;
1164     m_SampleImportQueue.erase(cur);
1165     } catch (std::string what) {
1166     // remember the files that made trouble (and their cause)
1167 persson 2442 if (!error_files.empty()) error_files += "\n";
1168 schoenebeck 1225 error_files += (*iter).sample_path += " (" + what + ")";
1169     ++iter;
1170     }
1171     }
1172     // show error message box when some sample(s) could not be imported
1173 persson 2442 if (!error_files.empty()) {
1174 schoenebeck 1382 Glib::ustring txt = _("Could not import the following sample(s):\n") + error_files;
1175 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1176     msg.run();
1177     }
1178     }
1179    
1180     void MainWindow::on_action_file_properties()
1181     {
1182     propDialog.show();
1183     propDialog.deiconify();
1184     }
1185    
1186 schoenebeck 2541 void MainWindow::on_action_warn_user_on_extensions() {
1187     Settings::singleton()->warnUserOnExtensions =
1188     !Settings::singleton()->warnUserOnExtensions;
1189     }
1190    
1191 schoenebeck 1225 void MainWindow::on_action_help_about()
1192     {
1193     Gtk::AboutDialog dialog;
1194 persson 1959 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 12) || GTKMM_MAJOR_VERSION > 2
1195     dialog.set_program_name("Gigedit");
1196     #else
1197     dialog.set_name("Gigedit");
1198     #endif
1199 schoenebeck 1225 dialog.set_version(VERSION);
1200 persson 2507 dialog.set_copyright("Copyright (C) 2006-2014 Andreas Persson");
1201 schoenebeck 2476 const std::string sComment =
1202     _("Built " __DATE__ "\nUsing ") +
1203     ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +
1204     _(
1205     "Gigedit is released under the GNU General Public License.\n"
1206     "\n"
1207 schoenebeck 2627 "This program is distributed WITHOUT ANY WARRANTY; So better "
1208     "backup your Gigasampler/GigaStudio files before editing them with "
1209 schoenebeck 2476 "this application.\n"
1210     "\n"
1211     "Please report bugs to: http://bugs.linuxsampler.org"
1212     );
1213     dialog.set_comments(sComment.c_str());
1214 schoenebeck 1436 dialog.set_website("http://www.linuxsampler.org");
1215     dialog.set_website_label("http://www.linuxsampler.org");
1216 schoenebeck 1225 dialog.run();
1217     }
1218    
1219     PropDialog::PropDialog()
1220 schoenebeck 2560 : eFileFormat(_("File Format")),
1221     eName(_("Name")),
1222 persson 1831 eCreationDate(_("Creation date")),
1223     eComments(_("Comments")),
1224     eProduct(_("Product")),
1225     eCopyright(_("Copyright")),
1226     eArtists(_("Artists")),
1227     eGenre(_("Genre")),
1228     eKeywords(_("Keywords")),
1229     eEngineer(_("Engineer")),
1230     eTechnician(_("Technician")),
1231     eSoftware(_("Software")),
1232     eMedium(_("Medium")),
1233     eSource(_("Source")),
1234     eSourceForm(_("Source form")),
1235     eCommissioned(_("Commissioned")),
1236     eSubject(_("Subject")),
1237 persson 1582 quitButton(Gtk::Stock::CLOSE),
1238 schoenebeck 2560 table(2, 1),
1239     m_file(NULL)
1240 schoenebeck 1225 {
1241 persson 1831 set_title(_("File Properties"));
1242 persson 1582 eName.set_width_chars(50);
1243    
1244     connect(eName, &DLS::Info::Name);
1245     connect(eCreationDate, &DLS::Info::CreationDate);
1246     connect(eComments, &DLS::Info::Comments);
1247     connect(eProduct, &DLS::Info::Product);
1248     connect(eCopyright, &DLS::Info::Copyright);
1249     connect(eArtists, &DLS::Info::Artists);
1250     connect(eGenre, &DLS::Info::Genre);
1251     connect(eKeywords, &DLS::Info::Keywords);
1252     connect(eEngineer, &DLS::Info::Engineer);
1253     connect(eTechnician, &DLS::Info::Technician);
1254     connect(eSoftware, &DLS::Info::Software);
1255     connect(eMedium, &DLS::Info::Medium);
1256     connect(eSource, &DLS::Info::Source);
1257     connect(eSourceForm, &DLS::Info::SourceForm);
1258     connect(eCommissioned, &DLS::Info::Commissioned);
1259     connect(eSubject, &DLS::Info::Subject);
1260    
1261 schoenebeck 2560 table.add(eFileFormat);
1262 persson 1582 table.add(eName);
1263     table.add(eCreationDate);
1264     table.add(eComments);
1265     table.add(eProduct);
1266     table.add(eCopyright);
1267     table.add(eArtists);
1268     table.add(eGenre);
1269     table.add(eKeywords);
1270     table.add(eEngineer);
1271     table.add(eTechnician);
1272     table.add(eSoftware);
1273     table.add(eMedium);
1274     table.add(eSource);
1275     table.add(eSourceForm);
1276     table.add(eCommissioned);
1277     table.add(eSubject);
1278    
1279 schoenebeck 1225 table.set_col_spacings(5);
1280 persson 1582 add(vbox);
1281     table.set_border_width(5);
1282     vbox.add(table);
1283     vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
1284     buttonBox.set_layout(Gtk::BUTTONBOX_END);
1285     buttonBox.set_border_width(5);
1286     buttonBox.show();
1287     buttonBox.pack_start(quitButton);
1288 persson 2151 quitButton.set_can_default();
1289 persson 1582 quitButton.grab_focus();
1290     quitButton.signal_clicked().connect(
1291     sigc::mem_fun(*this, &PropDialog::hide));
1292 schoenebeck 2560 eFileFormat.signal_value_changed().connect(
1293     sigc::mem_fun(*this, &PropDialog::onFileFormatChanged));
1294 schoenebeck 1225
1295 persson 1582 quitButton.show();
1296     vbox.show();
1297 schoenebeck 1225 show_all_children();
1298     }
1299    
1300 schoenebeck 2560 void PropDialog::set_file(gig::File* file)
1301     {
1302     m_file = file;
1303    
1304     // update file format version combo box
1305     const std::string sGiga = "Gigasampler/GigaStudio v";
1306     const int major = file->pVersion->major;
1307     std::vector<std::string> txts;
1308     std::vector<int> values;
1309     txts.push_back(sGiga + "2"); values.push_back(2);
1310 schoenebeck 2566 txts.push_back(sGiga + "3/v4"); values.push_back(3);
1311 schoenebeck 2560 if (major != 2 && major != 3) {
1312     txts.push_back(sGiga + ToString(major)); values.push_back(major);
1313     }
1314     std::vector<const char*> texts;
1315     for (int i = 0; i < txts.size(); ++i) texts.push_back(txts[i].c_str());
1316 persson 2579 texts.push_back(NULL); values.push_back(0);
1317 schoenebeck 2560 eFileFormat.set_choices(&texts[0], &values[0]);
1318     eFileFormat.set_value(major);
1319     }
1320    
1321     void PropDialog::onFileFormatChanged() {
1322     const int major = eFileFormat.get_value();
1323     if (m_file) m_file->pVersion->major = major;
1324     }
1325    
1326 schoenebeck 1225 void PropDialog::set_info(DLS::Info* info)
1327     {
1328 persson 2423 update(info);
1329 schoenebeck 1225 }
1330    
1331 persson 1582
1332 persson 2445 void InstrumentProps::set_Name(const gig::String& name)
1333     {
1334     m->pInfo->Name = name;
1335     }
1336    
1337     void InstrumentProps::update_name()
1338     {
1339     update_model++;
1340     eName.set_value(m->pInfo->Name);
1341     update_model--;
1342     }
1343    
1344 persson 1460 void InstrumentProps::set_IsDrum(bool value)
1345     {
1346 persson 2423 m->IsDrum = value;
1347 persson 1460 }
1348    
1349     void InstrumentProps::set_MIDIBank(uint16_t value)
1350     {
1351 persson 2423 m->MIDIBank = value;
1352 persson 1460 }
1353    
1354     void InstrumentProps::set_MIDIProgram(uint32_t value)
1355     {
1356 persson 2423 m->MIDIProgram = value;
1357 persson 1460 }
1358    
1359 persson 2423 InstrumentProps::InstrumentProps() :
1360     quitButton(Gtk::Stock::CLOSE),
1361     table(2,1),
1362     eName(_("Name")),
1363     eIsDrum(_("Is drum")),
1364     eMIDIBank(_("MIDI bank"), 0, 16383),
1365     eMIDIProgram(_("MIDI program")),
1366     eAttenuation(_("Attenuation"), 0, 96, 0, 1),
1367     eGainPlus6(_("Gain +6dB"), eAttenuation, -6),
1368     eEffectSend(_("Effect send"), 0, 65535),
1369     eFineTune(_("Fine tune"), -8400, 8400),
1370     ePitchbendRange(_("Pitchbend range"), 0, 12),
1371     ePianoReleaseMode(_("Piano release mode")),
1372     eDimensionKeyRangeLow(_("Keyswitching range low")),
1373     eDimensionKeyRangeHigh(_("Keyswitching range high"))
1374 persson 1460 {
1375 persson 1831 set_title(_("Instrument Properties"));
1376 schoenebeck 1225
1377 schoenebeck 1656 eDimensionKeyRangeLow.set_tip(
1378     _("start of the keyboard area which should switch the "
1379     "\"keyswitching\" dimension")
1380     );
1381     eDimensionKeyRangeHigh.set_tip(
1382     _("end of the keyboard area which should switch the "
1383     "\"keyswitching\" dimension")
1384     );
1385    
1386 persson 2445 connect(eName, &InstrumentProps::set_Name);
1387 persson 1460 connect(eIsDrum, &InstrumentProps::set_IsDrum);
1388     connect(eMIDIBank, &InstrumentProps::set_MIDIBank);
1389     connect(eMIDIProgram, &InstrumentProps::set_MIDIProgram);
1390     connect(eAttenuation, &gig::Instrument::Attenuation);
1391     connect(eGainPlus6, &gig::Instrument::Attenuation);
1392     connect(eEffectSend, &gig::Instrument::EffectSend);
1393     connect(eFineTune, &gig::Instrument::FineTune);
1394     connect(ePitchbendRange, &gig::Instrument::PitchbendRange);
1395     connect(ePianoReleaseMode, &gig::Instrument::PianoReleaseMode);
1396 persson 2423 connect(eDimensionKeyRangeLow, eDimensionKeyRangeHigh,
1397     &gig::Instrument::DimensionKeyRange);
1398 persson 1460
1399 persson 2445 eName.signal_value_changed().connect(sig_name_changed.make_slot());
1400    
1401 schoenebeck 1225 table.set_col_spacings(5);
1402    
1403 persson 1582 table.add(eName);
1404     table.add(eIsDrum);
1405     table.add(eMIDIBank);
1406     table.add(eMIDIProgram);
1407     table.add(eAttenuation);
1408     table.add(eGainPlus6);
1409     table.add(eEffectSend);
1410     table.add(eFineTune);
1411     table.add(ePitchbendRange);
1412     table.add(ePianoReleaseMode);
1413     table.add(eDimensionKeyRangeLow);
1414     table.add(eDimensionKeyRangeHigh);
1415 schoenebeck 1225
1416     add(vbox);
1417     table.set_border_width(5);
1418     vbox.pack_start(table);
1419     table.show();
1420     vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
1421     buttonBox.set_layout(Gtk::BUTTONBOX_END);
1422     buttonBox.set_border_width(5);
1423     buttonBox.show();
1424     buttonBox.pack_start(quitButton);
1425 persson 2151 quitButton.set_can_default();
1426 schoenebeck 1225 quitButton.grab_focus();
1427    
1428     quitButton.signal_clicked().connect(
1429     sigc::mem_fun(*this, &InstrumentProps::hide));
1430    
1431     quitButton.show();
1432     vbox.show();
1433     show_all_children();
1434     }
1435    
1436     void InstrumentProps::set_instrument(gig::Instrument* instrument)
1437     {
1438 persson 2423 update(instrument);
1439 persson 1460
1440     update_model++;
1441 persson 2445 eName.set_value(instrument->pInfo->Name);
1442 persson 1460 eIsDrum.set_value(instrument->IsDrum);
1443     eMIDIBank.set_value(instrument->MIDIBank);
1444     eMIDIProgram.set_value(instrument->MIDIProgram);
1445     update_model--;
1446 schoenebeck 1225 }
1447    
1448    
1449 persson 1261 void MainWindow::file_changed()
1450     {
1451     if (file && !file_is_changed) {
1452     set_title("*" + get_title());
1453     file_is_changed = true;
1454 schoenebeck 1225 }
1455 persson 1261 }
1456 schoenebeck 1225
1457 schoenebeck 2621 void MainWindow::updateSampleRefCountMap(gig::File* gig) {
1458     sample_ref_count.clear();
1459    
1460     if (!gig) return;
1461    
1462     for (gig::Instrument* instrument = gig->GetFirstInstrument(); instrument;
1463     instrument = gig->GetNextInstrument())
1464     {
1465     for (gig::Region* rgn = instrument->GetFirstRegion(); rgn;
1466     rgn = instrument->GetNextRegion())
1467     {
1468     for (int i = 0; i < 256; ++i) {
1469     if (!rgn->pDimensionRegions[i]) continue;
1470     if (rgn->pDimensionRegions[i]->pSample) {
1471     sample_ref_count[rgn->pDimensionRegions[i]->pSample]++;
1472     }
1473     }
1474     }
1475     }
1476     }
1477    
1478 schoenebeck 1382 void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument)
1479 persson 1261 {
1480     file = 0;
1481 schoenebeck 1411 set_file_is_shared(isSharedInstrument);
1482 persson 1261
1483     this->filename = filename ? filename : _("Unsaved Gig File");
1484     set_title(Glib::filename_display_basename(this->filename));
1485     file_has_name = filename;
1486     file_is_changed = false;
1487    
1488 schoenebeck 2560 propDialog.set_file(gig);
1489 schoenebeck 1225 propDialog.set_info(gig->pInfo);
1490    
1491 persson 2442 instrument_name_connection.block();
1492 schoenebeck 1225 for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
1493     instrument = gig->GetNextInstrument()) {
1494 persson 2446 Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
1495 persson 2442
1496 schoenebeck 1225 Gtk::TreeModel::iterator iter = m_refTreeModel->append();
1497     Gtk::TreeModel::Row row = *iter;
1498 persson 2442 row[m_Columns.m_col_name] = name;
1499 schoenebeck 1225 row[m_Columns.m_col_instr] = instrument;
1500 persson 2442
1501     add_instrument_to_menu(name);
1502 schoenebeck 1225 }
1503 persson 2442 instrument_name_connection.unblock();
1504 schoenebeck 2625 uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments")->show();
1505 schoenebeck 1225
1506 schoenebeck 2621 updateSampleRefCountMap(gig);
1507    
1508 schoenebeck 1225 for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1509     if (group->Name != "") {
1510     Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1511     Gtk::TreeModel::Row rowGroup = *iterGroup;
1512 persson 2446 rowGroup[m_SamplesModel.m_col_name] = gig_to_utf8(group->Name);
1513 schoenebeck 1225 rowGroup[m_SamplesModel.m_col_group] = group;
1514     rowGroup[m_SamplesModel.m_col_sample] = NULL;
1515     for (gig::Sample* sample = group->GetFirstSample();
1516     sample; sample = group->GetNextSample()) {
1517     Gtk::TreeModel::iterator iterSample =
1518     m_refSamplesTreeModel->append(rowGroup.children());
1519     Gtk::TreeModel::Row rowSample = *iterSample;
1520 persson 2446 rowSample[m_SamplesModel.m_col_name] =
1521     gig_to_utf8(sample->pInfo->Name);
1522 schoenebeck 1225 rowSample[m_SamplesModel.m_col_sample] = sample;
1523     rowSample[m_SamplesModel.m_col_group] = NULL;
1524 schoenebeck 2621 int refcount = sample_ref_count.count(sample) ? sample_ref_count[sample] : 0;
1525     rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
1526 schoenebeck 2625 rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
1527 schoenebeck 1225 }
1528     }
1529     }
1530 schoenebeck 2604
1531     for (int i = 0; gig->GetScriptGroup(i); ++i) {
1532     gig::ScriptGroup* group = gig->GetScriptGroup(i);
1533 schoenebeck 1225
1534 schoenebeck 2604 Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
1535     Gtk::TreeModel::Row rowGroup = *iterGroup;
1536     rowGroup[m_ScriptsModel.m_col_name] = gig_to_utf8(group->Name);
1537     rowGroup[m_ScriptsModel.m_col_group] = group;
1538     rowGroup[m_ScriptsModel.m_col_script] = NULL;
1539     for (int s = 0; group->GetScript(s); ++s) {
1540     gig::Script* script = group->GetScript(s);
1541    
1542     Gtk::TreeModel::iterator iterScript =
1543     m_refScriptsTreeModel->append(rowGroup.children());
1544     Gtk::TreeModel::Row rowScript = *iterScript;
1545     rowScript[m_ScriptsModel.m_col_name] = gig_to_utf8(script->Name);
1546     rowScript[m_ScriptsModel.m_col_script] = script;
1547     rowScript[m_ScriptsModel.m_col_group] = NULL;
1548     }
1549     }
1550 schoenebeck 2624 // unfold all sample groups & script groups by default
1551     m_TreeViewSamples.expand_all();
1552 schoenebeck 2604 m_TreeViewScripts.expand_all();
1553    
1554 persson 1261 file = gig;
1555    
1556 schoenebeck 1225 // select the first instrument
1557 persson 2442 m_TreeView.get_selection()->select(Gtk::TreePath("0"));
1558 persson 2423
1559 persson 2445 instr_props_set_instrument();
1560 persson 2507 gig::Instrument* instrument = get_instrument();
1561     if (instrument) {
1562     midiRules.set_instrument(instrument);
1563     }
1564 persson 2445 }
1565    
1566     bool MainWindow::instr_props_set_instrument()
1567     {
1568     instrumentProps.signal_name_changed().clear();
1569    
1570     Gtk::TreeModel::const_iterator it =
1571     m_TreeView.get_selection()->get_selected();
1572     if (it) {
1573     Gtk::TreeModel::Row row = *it;
1574     gig::Instrument* instrument = row[m_Columns.m_col_instr];
1575    
1576 persson 2423 instrumentProps.set_instrument(instrument);
1577 persson 2445
1578     // make sure instrument tree is updated when user changes the
1579     // instrument name in instrument properties window
1580     instrumentProps.signal_name_changed().connect(
1581     sigc::bind(
1582     sigc::mem_fun(*this,
1583     &MainWindow::instr_name_changed_by_instr_props),
1584     it));
1585     } else {
1586     instrumentProps.hide();
1587 persson 2423 }
1588 persson 2445 return it;
1589 schoenebeck 1225 }
1590    
1591     void MainWindow::show_instr_props()
1592     {
1593 persson 2445 if (instr_props_set_instrument()) {
1594 persson 1533 instrumentProps.show();
1595     instrumentProps.deiconify();
1596 schoenebeck 1225 }
1597     }
1598    
1599 persson 2445 void MainWindow::instr_name_changed_by_instr_props(Gtk::TreeModel::iterator& it)
1600     {
1601     Gtk::TreeModel::Row row = *it;
1602     Glib::ustring name = row[m_Columns.m_col_name];
1603    
1604     gig::Instrument* instrument = row[m_Columns.m_col_instr];
1605 persson 2446 Glib::ustring gigname(gig_to_utf8(instrument->pInfo->Name));
1606     if (gigname != name) {
1607     row[m_Columns.m_col_name] = gigname;
1608 persson 2445 }
1609     }
1610    
1611 persson 2507 void MainWindow::show_midi_rules()
1612     {
1613     if (gig::Instrument* instrument = get_instrument())
1614     {
1615     midiRules.set_instrument(instrument);
1616     midiRules.show();
1617     midiRules.deiconify();
1618     }
1619     }
1620    
1621 schoenebeck 2610 void MainWindow::show_script_slots() {
1622     if (!file) return;
1623     // get selected instrument
1624     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1625     Gtk::TreeModel::iterator it = sel->get_selected();
1626     if (!it) return;
1627     Gtk::TreeModel::Row row = *it;
1628     gig::Instrument* instrument = row[m_Columns.m_col_instr];
1629     if (!instrument) return;
1630    
1631     ScriptSlots* window = new ScriptSlots;
1632     window->setInstrument(instrument);
1633     //window->reparent(*this);
1634     window->show();
1635     }
1636    
1637 schoenebeck 1415 void MainWindow::on_action_view_status_bar() {
1638     Gtk::CheckMenuItem* item =
1639     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/Statusbar"));
1640     if (!item) {
1641     std::cerr << "/MenuBar/MenuView/Statusbar == NULL\n";
1642     return;
1643     }
1644     if (item->get_active()) m_StatusBar.show();
1645     else m_StatusBar.hide();
1646     }
1647    
1648 schoenebeck 2464 bool MainWindow::is_copy_samples_unity_note_enabled() const {
1649     Gtk::CheckMenuItem* item =
1650     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity"));
1651     if (!item) {
1652     std::cerr << "/MenuBar/MenuEdit/CopySampleUnity == NULL\n";
1653     return true;
1654     }
1655     return item->get_active();
1656     }
1657    
1658     bool MainWindow::is_copy_samples_fine_tune_enabled() const {
1659     Gtk::CheckMenuItem* item =
1660     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleTune"));
1661     if (!item) {
1662     std::cerr << "/MenuBar/MenuEdit/CopySampleTune == NULL\n";
1663     return true;
1664     }
1665     return item->get_active();
1666     }
1667    
1668     bool MainWindow::is_copy_samples_loop_enabled() const {
1669     Gtk::CheckMenuItem* item =
1670     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleLoop"));
1671     if (!item) {
1672     std::cerr << "/MenuBar/MenuEdit/CopySampleLoop == NULL\n";
1673     return true;
1674     }
1675     return item->get_active();
1676     }
1677    
1678 schoenebeck 1225 void MainWindow::on_button_release(GdkEventButton* button)
1679     {
1680     if (button->type == GDK_2BUTTON_PRESS) {
1681     show_instr_props();
1682     } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1683 persson 2507 // gig v2 files have no midi rules
1684 schoenebeck 2625 const bool bEnabled = !(file->pVersion && file->pVersion->major == 2);
1685 persson 2507 static_cast<Gtk::MenuItem*>(
1686 schoenebeck 2625 uiManager->get_widget("/MenuBar/MenuInstrument/MidiRules"))->set_sensitive(
1687     bEnabled
1688     );
1689     static_cast<Gtk::MenuItem*>(
1690 persson 2507 uiManager->get_widget("/PopupMenu/MidiRules"))->set_sensitive(
1691 schoenebeck 2625 bEnabled
1692     );
1693 schoenebeck 1225 popup_menu->popup(button->button, button->time);
1694     }
1695     }
1696    
1697 persson 2442 void MainWindow::on_instrument_selection_change(Gtk::RadioMenuItem* item) {
1698     if (item->get_active()) {
1699     const std::vector<Gtk::Widget*> children =
1700     instrument_menu->get_children();
1701     std::vector<Gtk::Widget*>::const_iterator it =
1702     find(children.begin(), children.end(), item);
1703     if (it != children.end()) {
1704     int index = it - children.begin();
1705     m_TreeView.get_selection()->select(Gtk::TreePath(ToString(index)));
1706    
1707     m_RegionChooser.set_instrument(file->GetInstrument(index));
1708     }
1709     }
1710 schoenebeck 1225 }
1711    
1712     void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
1713     if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1714     Gtk::Menu* sample_popup =
1715     dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
1716     // update enabled/disabled state of sample popup items
1717     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1718     Gtk::TreeModel::iterator it = sel->get_selected();
1719     bool group_selected = false;
1720     bool sample_selected = false;
1721     if (it) {
1722     Gtk::TreeModel::Row row = *it;
1723     group_selected = row[m_SamplesModel.m_col_group];
1724     sample_selected = row[m_SamplesModel.m_col_sample];
1725     }
1726 schoenebeck 2625
1727    
1728 schoenebeck 1225 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
1729     set_sensitive(group_selected || sample_selected);
1730     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
1731     set_sensitive(group_selected || sample_selected);
1732     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
1733     set_sensitive(file);
1734 schoenebeck 2624 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/ShowSampleRefs"))->
1735     set_sensitive(sample_selected);
1736 schoenebeck 1225 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
1737     set_sensitive(group_selected || sample_selected);
1738     // show sample popup
1739     sample_popup->popup(button->button, button->time);
1740 schoenebeck 2625
1741     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/SampleProperties"))->
1742     set_sensitive(group_selected || sample_selected);
1743     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddSample"))->
1744     set_sensitive(group_selected || sample_selected);
1745     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddGroup"))->
1746     set_sensitive(file);
1747     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/ShowSampleRefs"))->
1748     set_sensitive(sample_selected);
1749     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/RemoveSample"))->
1750     set_sensitive(group_selected || sample_selected);
1751 schoenebeck 1225 }
1752     }
1753    
1754 schoenebeck 2604 void MainWindow::on_script_treeview_button_release(GdkEventButton* button) {
1755     if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1756     Gtk::Menu* script_popup =
1757     dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/ScriptPopupMenu"));
1758     // update enabled/disabled state of sample popup items
1759     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
1760     Gtk::TreeModel::iterator it = sel->get_selected();
1761     bool group_selected = false;
1762     bool script_selected = false;
1763     if (it) {
1764     Gtk::TreeModel::Row row = *it;
1765     group_selected = row[m_ScriptsModel.m_col_group];
1766     script_selected = row[m_ScriptsModel.m_col_script];
1767     }
1768     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScript"))->
1769     set_sensitive(group_selected || script_selected);
1770     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScriptGroup"))->
1771     set_sensitive(file);
1772     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/EditScript"))->
1773     set_sensitive(script_selected);
1774     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/RemoveScript"))->
1775     set_sensitive(group_selected || script_selected);
1776     // show sample popup
1777     script_popup->popup(button->button, button->time);
1778 schoenebeck 2625
1779     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScript"))->
1780     set_sensitive(group_selected || script_selected);
1781     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScriptGroup"))->
1782     set_sensitive(file);
1783     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/EditScript"))->
1784     set_sensitive(script_selected);
1785     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/RemoveScript"))->
1786     set_sensitive(group_selected || script_selected);
1787 schoenebeck 2604 }
1788     }
1789 persson 2442
1790     Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu(
1791     const Glib::ustring& name, int position) {
1792    
1793     Gtk::RadioMenuItem::Group instrument_group;
1794     const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
1795     if (!children.empty()) {
1796     instrument_group =
1797     static_cast<Gtk::RadioMenuItem*>(children[0])->get_group();
1798     }
1799     Gtk::RadioMenuItem* item =
1800     new Gtk::RadioMenuItem(instrument_group, name);
1801     if (position < 0) {
1802     instrument_menu->append(*item);
1803     } else {
1804     instrument_menu->insert(*item, position);
1805     }
1806     item->show();
1807     item->signal_activate().connect(
1808     sigc::bind(
1809     sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
1810     item));
1811     return item;
1812     }
1813    
1814     void MainWindow::remove_instrument_from_menu(int index) {
1815     const std::vector<Gtk::Widget*> children =
1816     instrument_menu->get_children();
1817     Gtk::Widget* child = children[index];
1818     instrument_menu->remove(*child);
1819     delete child;
1820     }
1821    
1822     void MainWindow::add_instrument(gig::Instrument* instrument) {
1823 persson 2446 const Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
1824 persson 2442
1825     // update instrument tree view
1826     instrument_name_connection.block();
1827     Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
1828     Gtk::TreeModel::Row rowInstr = *iterInstr;
1829     rowInstr[m_Columns.m_col_name] = name;
1830     rowInstr[m_Columns.m_col_instr] = instrument;
1831     instrument_name_connection.unblock();
1832    
1833     add_instrument_to_menu(name);
1834    
1835     m_TreeView.get_selection()->select(iterInstr);
1836    
1837     file_changed();
1838     }
1839    
1840 schoenebeck 1225 void MainWindow::on_action_add_instrument() {
1841     static int __instrument_indexer = 0;
1842     if (!file) return;
1843     gig::Instrument* instrument = file->AddInstrument();
1844     __instrument_indexer++;
1845 persson 2446 instrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument ") +
1846     ToString(__instrument_indexer));
1847 persson 2442
1848     add_instrument(instrument);
1849 schoenebeck 1225 }
1850    
1851 schoenebeck 2395 void MainWindow::on_action_duplicate_instrument() {
1852     if (!file) return;
1853 persson 2442
1854 schoenebeck 2395 // retrieve the currently selected instrument
1855     // (being the original instrument to be duplicated)
1856     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1857     Gtk::TreeModel::iterator itSelection = sel->get_selected();
1858     if (!itSelection) return;
1859     Gtk::TreeModel::Row row = *itSelection;
1860     gig::Instrument* instrOrig = row[m_Columns.m_col_instr];
1861     if (!instrOrig) return;
1862 persson 2442
1863 schoenebeck 2395 // duplicate the orginal instrument
1864     gig::Instrument* instrNew = file->AddDuplicateInstrument(instrOrig);
1865     instrNew->pInfo->Name =
1866 persson 2446 instrOrig->pInfo->Name +
1867     gig_from_utf8(Glib::ustring(" (") + _("Copy") + ")");
1868 persson 2442
1869     add_instrument(instrNew);
1870 schoenebeck 2395 }
1871    
1872 schoenebeck 1225 void MainWindow::on_action_remove_instrument() {
1873     if (!file) return;
1874 schoenebeck 1382 if (file_is_shared) {
1875     Gtk::MessageDialog msg(
1876     *this,
1877     _("You cannot delete an instrument from this file, since it's "
1878     "currently used by the sampler."),
1879     false, Gtk::MESSAGE_INFO
1880     );
1881     msg.run();
1882     return;
1883     }
1884    
1885 schoenebeck 1225 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1886     Gtk::TreeModel::iterator it = sel->get_selected();
1887     if (it) {
1888     Gtk::TreeModel::Row row = *it;
1889     gig::Instrument* instr = row[m_Columns.m_col_instr];
1890     try {
1891 persson 2442 Gtk::TreePath path(it);
1892     int index = path[0];
1893    
1894 schoenebeck 1225 // remove instrument from the gig file
1895     if (instr) file->DeleteInstrument(instr);
1896 persson 1261 file_changed();
1897 persson 2423
1898 persson 2442 remove_instrument_from_menu(index);
1899    
1900     // remove row from instruments tree view
1901     m_refTreeModel->erase(it);
1902    
1903     #if GTKMM_MAJOR_VERSION < 3
1904     // select another instrument (in gtk3 this is done
1905     // automatically)
1906     if (!m_refTreeModel->children().empty()) {
1907     if (index == m_refTreeModel->children().size()) {
1908     index--;
1909     }
1910     m_TreeView.get_selection()->select(
1911     Gtk::TreePath(ToString(index)));
1912     }
1913     #endif
1914 persson 2445 instr_props_set_instrument();
1915 persson 2507 instr = get_instrument();
1916     if (instr) {
1917     midiRules.set_instrument(instr);
1918     } else {
1919     midiRules.hide();
1920     }
1921 schoenebeck 1225 } catch (RIFF::Exception e) {
1922     Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1923     msg.run();
1924     }
1925     }
1926     }
1927    
1928     void MainWindow::on_action_sample_properties() {
1929     //TODO: show a dialog where the selected sample's properties can be edited
1930     Gtk::MessageDialog msg(
1931 persson 1831 *this, _("Sorry, yet to be implemented!"), false, Gtk::MESSAGE_INFO
1932 schoenebeck 1225 );
1933     msg.run();
1934     }
1935    
1936 schoenebeck 2604 void MainWindow::on_action_add_script_group() {
1937     static int __script_indexer = 0;
1938     if (!file) return;
1939     gig::ScriptGroup* group = file->AddScriptGroup();
1940     group->Name = gig_from_utf8(_("Unnamed Group"));
1941     if (__script_indexer) group->Name += " " + ToString(__script_indexer);
1942     __script_indexer++;
1943     // update sample tree view
1944     Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
1945     Gtk::TreeModel::Row rowGroup = *iterGroup;
1946     rowGroup[m_ScriptsModel.m_col_name] = gig_to_utf8(group->Name);
1947     rowGroup[m_ScriptsModel.m_col_script] = NULL;
1948     rowGroup[m_ScriptsModel.m_col_group] = group;
1949     file_changed();
1950     }
1951    
1952     void MainWindow::on_action_add_script() {
1953     if (!file) return;
1954     // get selected group
1955     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
1956     Gtk::TreeModel::iterator it = sel->get_selected();
1957     if (!it) return;
1958     Gtk::TreeModel::Row row = *it;
1959     gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
1960     if (!group) { // not a group, but a script is selected (probably)
1961     gig::Script* script = row[m_ScriptsModel.m_col_script];
1962     if (!script) return;
1963     it = row.parent(); // resolve parent (that is the script's group)
1964     if (!it) return;
1965     row = *it;
1966     group = row[m_ScriptsModel.m_col_group];
1967     if (!group) return;
1968     }
1969    
1970     // add a new script to the .gig file
1971     gig::Script* script = group->AddScript();
1972     Glib::ustring name = _("Unnamed Script");
1973     script->Name = gig_from_utf8(name);
1974    
1975     // add script to the tree view
1976     Gtk::TreeModel::iterator iterScript =
1977     m_refScriptsTreeModel->append(row.children());
1978     Gtk::TreeModel::Row rowScript = *iterScript;
1979     rowScript[m_ScriptsModel.m_col_name] = name;
1980     rowScript[m_ScriptsModel.m_col_script] = script;
1981     rowScript[m_ScriptsModel.m_col_group] = NULL;
1982    
1983     // unfold group of new script item in treeview
1984     Gtk::TreeModel::Path path(iterScript);
1985     m_TreeViewScripts.expand_to_path(path);
1986     }
1987    
1988     void MainWindow::on_action_edit_script() {
1989     if (!file) return;
1990     // get selected script
1991     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
1992     Gtk::TreeModel::iterator it = sel->get_selected();
1993     if (!it) return;
1994     Gtk::TreeModel::Row row = *it;
1995     gig::Script* script = row[m_ScriptsModel.m_col_script];
1996     if (!script) return;
1997    
1998     ScriptEditor* editor = new ScriptEditor;
1999     editor->setScript(script);
2000     //editor->reparent(*this);
2001     editor->show();
2002     }
2003    
2004     void MainWindow::on_action_remove_script() {
2005     if (!file) return;
2006     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2007     Gtk::TreeModel::iterator it = sel->get_selected();
2008     if (it) {
2009     Gtk::TreeModel::Row row = *it;
2010     gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2011     gig::Script* script = row[m_ScriptsModel.m_col_script];
2012     Glib::ustring name = row[m_ScriptsModel.m_col_name];
2013     try {
2014     // remove script group or script from the gig file
2015     if (group) {
2016     // notify everybody that we're going to remove these samples
2017     //TODO: scripts_to_be_removed_signal.emit(members);
2018     // delete the group in the .gig file including the
2019     // samples that belong to the group
2020     file->DeleteScriptGroup(group);
2021     // notify that we're done with removal
2022     //TODO: scripts_removed_signal.emit();
2023     file_changed();
2024     } else if (script) {
2025     // notify everybody that we're going to remove this sample
2026     //TODO: std::list<gig::Script*> lscripts;
2027     //TODO: lscripts.push_back(script);
2028     //TODO: scripts_to_be_removed_signal.emit(lscripts);
2029     // remove sample from the .gig file
2030     script->GetGroup()->DeleteScript(script);
2031     // notify that we're done with removal
2032     //TODO: scripts_removed_signal.emit();
2033     dimreg_changed();
2034     file_changed();
2035     }
2036     // remove respective row(s) from samples tree view
2037     m_refScriptsTreeModel->erase(it);
2038     } catch (RIFF::Exception e) {
2039     // pretend we're done with removal (i.e. to avoid dead locks)
2040     //TODO: scripts_removed_signal.emit();
2041     // show error message
2042     Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2043     msg.run();
2044     }
2045     }
2046     }
2047    
2048 schoenebeck 1225 void MainWindow::on_action_add_group() {
2049     static int __sample_indexer = 0;
2050     if (!file) return;
2051     gig::Group* group = file->AddGroup();
2052 persson 2446 group->Name = gig_from_utf8(_("Unnamed Group"));
2053 schoenebeck 1225 if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
2054     __sample_indexer++;
2055     // update sample tree view
2056     Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
2057     Gtk::TreeModel::Row rowGroup = *iterGroup;
2058 persson 2446 rowGroup[m_SamplesModel.m_col_name] = gig_to_utf8(group->Name);
2059 schoenebeck 1225 rowGroup[m_SamplesModel.m_col_sample] = NULL;
2060     rowGroup[m_SamplesModel.m_col_group] = group;
2061 persson 1261 file_changed();
2062 schoenebeck 1225 }
2063    
2064     void MainWindow::on_action_add_sample() {
2065     if (!file) return;
2066     // get selected group
2067     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2068     Gtk::TreeModel::iterator it = sel->get_selected();
2069     if (!it) return;
2070     Gtk::TreeModel::Row row = *it;
2071     gig::Group* group = row[m_SamplesModel.m_col_group];
2072     if (!group) { // not a group, but a sample is selected (probably)
2073     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2074     if (!sample) return;
2075     it = row.parent(); // resolve parent (that is the sample's group)
2076     if (!it) return;
2077     row = *it;
2078     group = row[m_SamplesModel.m_col_group];
2079     if (!group) return;
2080     }
2081     // show 'browse for file' dialog
2082     Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
2083     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2084     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
2085     dialog.set_select_multiple(true);
2086 persson 2169
2087     // matches all file types supported by libsndfile
2088     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2089     Gtk::FileFilter soundfilter;
2090     #else
2091     Glib::RefPtr<Gtk::FileFilter> soundfilter = Gtk::FileFilter::create();
2092     #endif
2093 persson 1262 const char* const supportedFileTypes[] = {
2094 schoenebeck 1225 "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
2095     "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
2096     "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
2097     "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
2098     "*.caf", "*.CAF", NULL
2099     };
2100 persson 2169 const char* soundfiles = _("Sound Files");
2101     const char* allfiles = _("All Files");
2102     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2103 schoenebeck 1225 for (int i = 0; supportedFileTypes[i]; i++)
2104     soundfilter.add_pattern(supportedFileTypes[i]);
2105 persson 2169 soundfilter.set_name(soundfiles);
2106    
2107     // matches every file
2108     Gtk::FileFilter allpassfilter;
2109 schoenebeck 1225 allpassfilter.add_pattern("*.*");
2110 persson 2169 allpassfilter.set_name(allfiles);
2111     #else
2112     for (int i = 0; supportedFileTypes[i]; i++)
2113     soundfilter->add_pattern(supportedFileTypes[i]);
2114     soundfilter->set_name(soundfiles);
2115    
2116     // matches every file
2117     Glib::RefPtr<Gtk::FileFilter> allpassfilter = Gtk::FileFilter::create();
2118     allpassfilter->add_pattern("*.*");
2119     allpassfilter->set_name(allfiles);
2120     #endif
2121 schoenebeck 1225 dialog.add_filter(soundfilter);
2122     dialog.add_filter(allpassfilter);
2123 persson 1725 if (current_sample_dir != "") {
2124     dialog.set_current_folder(current_sample_dir);
2125     }
2126 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
2127 persson 1725 current_sample_dir = dialog.get_current_folder();
2128 schoenebeck 1225 Glib::ustring error_files;
2129 persson 2169 std::vector<std::string> filenames = dialog.get_filenames();
2130     for (std::vector<std::string>::iterator iter = filenames.begin();
2131 schoenebeck 1225 iter != filenames.end(); ++iter) {
2132     printf("Adding sample %s\n",(*iter).c_str());
2133     // use libsndfile to retrieve file informations
2134     SF_INFO info;
2135     info.format = 0;
2136     SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
2137     try {
2138 persson 1831 if (!hFile) throw std::string(_("could not open file"));
2139 schoenebeck 1225 int bitdepth;
2140     switch (info.format & 0xff) {
2141     case SF_FORMAT_PCM_S8:
2142     case SF_FORMAT_PCM_16:
2143 persson 1265 case SF_FORMAT_PCM_U8:
2144 schoenebeck 1225 bitdepth = 16;
2145     break;
2146     case SF_FORMAT_PCM_24:
2147     case SF_FORMAT_PCM_32:
2148     case SF_FORMAT_FLOAT:
2149     case SF_FORMAT_DOUBLE:
2150 persson 1265 bitdepth = 24;
2151 schoenebeck 1225 break;
2152     default:
2153     sf_close(hFile); // close sound file
2154 persson 1831 throw std::string(_("format not supported")); // unsupported subformat (yet?)
2155 schoenebeck 1225 }
2156     // add a new sample to the .gig file
2157     gig::Sample* sample = file->AddSample();
2158     // file name without path
2159 persson 1262 Glib::ustring filename = Glib::filename_display_basename(*iter);
2160     // remove file extension if there is one
2161     for (int i = 0; supportedFileTypes[i]; i++) {
2162     if (Glib::str_has_suffix(filename, supportedFileTypes[i] + 1)) {
2163     filename.erase(filename.length() - strlen(supportedFileTypes[i] + 1));
2164     break;
2165     }
2166     }
2167 persson 2446 sample->pInfo->Name = gig_from_utf8(filename);
2168 schoenebeck 1225 sample->Channels = info.channels;
2169     sample->BitDepth = bitdepth;
2170     sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
2171     sample->SamplesPerSecond = info.samplerate;
2172 persson 1265 sample->AverageBytesPerSecond = sample->FrameSize * sample->SamplesPerSecond;
2173     sample->BlockAlign = sample->FrameSize;
2174     sample->SamplesTotal = info.frames;
2175    
2176     SF_INSTRUMENT instrument;
2177     if (sf_command(hFile, SFC_GET_INSTRUMENT,
2178     &instrument, sizeof(instrument)) != SF_FALSE)
2179     {
2180     sample->MIDIUnityNote = instrument.basenote;
2181 schoenebeck 2466 sample->FineTune = instrument.detune;
2182 persson 1265
2183     if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) {
2184     sample->Loops = 1;
2185    
2186     switch (instrument.loops[0].mode) {
2187     case SF_LOOP_FORWARD:
2188     sample->LoopType = gig::loop_type_normal;
2189     break;
2190     case SF_LOOP_BACKWARD:
2191     sample->LoopType = gig::loop_type_backward;
2192     break;
2193     case SF_LOOP_ALTERNATING:
2194     sample->LoopType = gig::loop_type_bidirectional;
2195     break;
2196     }
2197     sample->LoopStart = instrument.loops[0].start;
2198     sample->LoopEnd = instrument.loops[0].end;
2199     sample->LoopPlayCount = instrument.loops[0].count;
2200     sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1;
2201     }
2202     }
2203    
2204 schoenebeck 1225 // schedule resizing the sample (which will be done
2205     // physically when File::Save() is called)
2206     sample->Resize(info.frames);
2207     // make sure sample is part of the selected group
2208     group->AddSample(sample);
2209     // schedule that physical resize and sample import
2210     // (data copying), performed when "Save" is requested
2211     SampleImportItem sched_item;
2212     sched_item.gig_sample = sample;
2213     sched_item.sample_path = *iter;
2214     m_SampleImportQueue.push_back(sched_item);
2215     // add sample to the tree view
2216     Gtk::TreeModel::iterator iterSample =
2217     m_refSamplesTreeModel->append(row.children());
2218     Gtk::TreeModel::Row rowSample = *iterSample;
2219 persson 2446 rowSample[m_SamplesModel.m_col_name] =
2220     gig_to_utf8(sample->pInfo->Name);
2221 schoenebeck 1225 rowSample[m_SamplesModel.m_col_sample] = sample;
2222     rowSample[m_SamplesModel.m_col_group] = NULL;
2223     // close sound file
2224     sf_close(hFile);
2225 persson 1261 file_changed();
2226 schoenebeck 1225 } catch (std::string what) { // remember the files that made trouble (and their cause)
2227 persson 2442 if (!error_files.empty()) error_files += "\n";
2228 schoenebeck 1225 error_files += *iter += " (" + what + ")";
2229     }
2230     }
2231     // show error message box when some file(s) could not be opened / added
2232 persson 2442 if (!error_files.empty()) {
2233 schoenebeck 1382 Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files;
2234 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2235     msg.run();
2236     }
2237     }
2238     }
2239    
2240 schoenebeck 1673 void MainWindow::on_action_replace_all_samples_in_all_groups()
2241     {
2242     if (!file) return;
2243     Gtk::FileChooserDialog dialog(*this, _("Select Folder"),
2244     Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
2245 persson 2442 const char* str =
2246 schoenebeck 1673 _("This is a very specific function. It tries to replace all samples "
2247 persson 1799 "in the current gig file by samples located in the chosen "
2248     "directory.\n\n"
2249     "It works like this: For each sample in the gig file, it tries to "
2250 schoenebeck 1673 "find a sample file in the selected directory with the same name as "
2251 persson 1799 "the sample in the gig file. Optionally, you can add a filename "
2252     "extension below, which will be added to the filename expected to be "
2253 schoenebeck 1673 "found. That is, assume you have a gig file with a sample called "
2254     "'Snare', if you enter '.wav' below (like it's done by default), it "
2255 persson 1799 "expects to find a sample file called 'Snare.wav' and will replace "
2256     "the sample in the gig file accordingly. If you don't need an "
2257     "extension, blank the field below. Any gig sample where no "
2258     "appropriate sample file could be found will be reported and left "
2259 persson 2344 "untouched.\n");
2260     #if GTKMM_MAJOR_VERSION < 3
2261     view::WrapLabel description(str);
2262     #else
2263     Gtk::Label description(str);
2264     description.set_line_wrap();
2265     #endif
2266 schoenebeck 1673 Gtk::HBox entryArea;
2267 persson 2169 Gtk::Label entryLabel( _("Add filename extension: "), Gtk::ALIGN_START);
2268 schoenebeck 1673 Gtk::Entry postfixEntryBox;
2269     postfixEntryBox.set_text(".wav");
2270     entryArea.pack_start(entryLabel);
2271     entryArea.pack_start(postfixEntryBox);
2272     dialog.get_vbox()->pack_start(description, Gtk::PACK_SHRINK);
2273     dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK);
2274     description.show();
2275 persson 1799 entryArea.show_all();
2276 schoenebeck 1673 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2277     dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
2278     dialog.set_select_multiple(false);
2279 persson 1725 if (current_sample_dir != "") {
2280     dialog.set_current_folder(current_sample_dir);
2281     }
2282 schoenebeck 1673 if (dialog.run() == Gtk::RESPONSE_OK)
2283     {
2284 persson 1725 current_sample_dir = dialog.get_current_folder();
2285 schoenebeck 1673 Glib::ustring error_files;
2286 persson 2169 std::string folder = dialog.get_filename();
2287 schoenebeck 1673 for (gig::Sample* sample = file->GetFirstSample();
2288     sample; sample = file->GetNextSample())
2289     {
2290     std::string filename =
2291 persson 2446 folder + G_DIR_SEPARATOR_S +
2292     Glib::filename_from_utf8(gig_to_utf8(sample->pInfo->Name) +
2293     postfixEntryBox.get_text());
2294 schoenebeck 1673 SF_INFO info;
2295     info.format = 0;
2296     SNDFILE* hFile = sf_open(filename.c_str(), SFM_READ, &info);
2297     try
2298     {
2299 persson 1831 if (!hFile) throw std::string(_("could not open file"));
2300 schoenebeck 1673 int bitdepth;
2301     switch (info.format & 0xff) {
2302     case SF_FORMAT_PCM_S8:
2303     case SF_FORMAT_PCM_16:
2304     case SF_FORMAT_PCM_U8:
2305     bitdepth = 16;
2306     break;
2307     case SF_FORMAT_PCM_24:
2308     case SF_FORMAT_PCM_32:
2309     case SF_FORMAT_FLOAT:
2310     case SF_FORMAT_DOUBLE:
2311     bitdepth = 24;
2312     break;
2313     default:
2314     sf_close(hFile);
2315 persson 1831 throw std::string(_("format not supported"));
2316 schoenebeck 1673 }
2317     SampleImportItem sched_item;
2318     sched_item.gig_sample = sample;
2319     sched_item.sample_path = filename;
2320     m_SampleImportQueue.push_back(sched_item);
2321     sf_close(hFile);
2322     file_changed();
2323     }
2324     catch (std::string what)
2325     {
2326 persson 2442 if (!error_files.empty()) error_files += "\n";
2327 persson 2446 error_files += Glib::filename_to_utf8(filename) +
2328     " (" + what + ")";
2329 schoenebeck 1673 }
2330     }
2331     // show error message box when some file(s) could not be opened / added
2332 persson 2442 if (!error_files.empty()) {
2333 schoenebeck 1673 Glib::ustring txt =
2334     _("Could not replace the following sample(s):\n") + error_files;
2335     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2336     msg.run();
2337     }
2338     }
2339     }
2340    
2341 schoenebeck 1225 void MainWindow::on_action_remove_sample() {
2342     if (!file) return;
2343     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2344     Gtk::TreeModel::iterator it = sel->get_selected();
2345     if (it) {
2346     Gtk::TreeModel::Row row = *it;
2347     gig::Group* group = row[m_SamplesModel.m_col_group];
2348     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2349     Glib::ustring name = row[m_SamplesModel.m_col_name];
2350     try {
2351     // remove group or sample from the gig file
2352     if (group) {
2353 persson 2446 // temporarily remember the samples that belong to
2354 schoenebeck 1225 // that group (we need that to clean the queue)
2355     std::list<gig::Sample*> members;
2356     for (gig::Sample* pSample = group->GetFirstSample();
2357     pSample; pSample = group->GetNextSample()) {
2358     members.push_back(pSample);
2359     }
2360 schoenebeck 1322 // notify everybody that we're going to remove these samples
2361     samples_to_be_removed_signal.emit(members);
2362 schoenebeck 1225 // delete the group in the .gig file including the
2363     // samples that belong to the group
2364     file->DeleteGroup(group);
2365 schoenebeck 1322 // notify that we're done with removal
2366     samples_removed_signal.emit();
2367 schoenebeck 1225 // if sample(s) were just previously added, remove
2368     // them from the import queue
2369     for (std::list<gig::Sample*>::iterator member = members.begin();
2370     member != members.end(); ++member) {
2371     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
2372     iter != m_SampleImportQueue.end(); ++iter) {
2373     if ((*iter).gig_sample == *member) {
2374     printf("Removing previously added sample '%s' from group '%s'\n",
2375     (*iter).sample_path.c_str(), name.c_str());
2376     m_SampleImportQueue.erase(iter);
2377     break;
2378     }
2379     }
2380     }
2381 persson 1261 file_changed();
2382 schoenebeck 1225 } else if (sample) {
2383 schoenebeck 1322 // notify everybody that we're going to remove this sample
2384     std::list<gig::Sample*> lsamples;
2385     lsamples.push_back(sample);
2386     samples_to_be_removed_signal.emit(lsamples);
2387 schoenebeck 1225 // remove sample from the .gig file
2388     file->DeleteSample(sample);
2389 schoenebeck 1322 // notify that we're done with removal
2390     samples_removed_signal.emit();
2391 schoenebeck 1225 // if sample was just previously added, remove it from
2392     // the import queue
2393     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
2394     iter != m_SampleImportQueue.end(); ++iter) {
2395     if ((*iter).gig_sample == sample) {
2396     printf("Removing previously added sample '%s'\n",
2397     (*iter).sample_path.c_str());
2398     m_SampleImportQueue.erase(iter);
2399     break;
2400     }
2401     }
2402 persson 1303 dimreg_changed();
2403 persson 1261 file_changed();
2404 schoenebeck 1225 }
2405     // remove respective row(s) from samples tree view
2406     m_refSamplesTreeModel->erase(it);
2407     } catch (RIFF::Exception e) {
2408 schoenebeck 1322 // pretend we're done with removal (i.e. to avoid dead locks)
2409     samples_removed_signal.emit();
2410     // show error message
2411 schoenebeck 1225 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2412     msg.run();
2413     }
2414     }
2415     }
2416    
2417 schoenebeck 2610 // see comment on on_sample_treeview_drag_begin()
2418     void MainWindow::on_scripts_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2419     {
2420     first_call_to_drag_data_get = true;
2421     }
2422    
2423     void MainWindow::on_scripts_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2424     Gtk::SelectionData& selection_data, guint, guint)
2425     {
2426     if (!first_call_to_drag_data_get) return;
2427     first_call_to_drag_data_get = false;
2428    
2429     // get selected script
2430     gig::Script* script = NULL;
2431     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2432     Gtk::TreeModel::iterator it = sel->get_selected();
2433     if (it) {
2434     Gtk::TreeModel::Row row = *it;
2435     script = row[m_ScriptsModel.m_col_script];
2436     }
2437     // pass the gig::Script as pointer
2438     selection_data.set(selection_data.get_target(), 0/*unused*/,
2439     (const guchar*)&script,
2440     sizeof(script)/*length of data in bytes*/);
2441     }
2442    
2443 persson 1303 // For some reason drag_data_get gets called two times for each
2444     // drag'n'drop (at least when target is an Entry). This work-around
2445     // makes sure the code in drag_data_get and drop_drag_data_received is
2446     // only executed once, as drag_begin only gets called once.
2447     void MainWindow::on_sample_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2448     {
2449     first_call_to_drag_data_get = true;
2450     }
2451    
2452 schoenebeck 1225 void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2453     Gtk::SelectionData& selection_data, guint, guint)
2454     {
2455 persson 1303 if (!first_call_to_drag_data_get) return;
2456     first_call_to_drag_data_get = false;
2457    
2458 schoenebeck 1225 // get selected sample
2459     gig::Sample* sample = NULL;
2460     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2461     Gtk::TreeModel::iterator it = sel->get_selected();
2462     if (it) {
2463     Gtk::TreeModel::Row row = *it;
2464     sample = row[m_SamplesModel.m_col_sample];
2465     }
2466     // pass the gig::Sample as pointer
2467     selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
2468     sizeof(sample)/*length of data in bytes*/);
2469     }
2470    
2471     void MainWindow::on_sample_label_drop_drag_data_received(
2472     const Glib::RefPtr<Gdk::DragContext>& context, int, int,
2473     const Gtk::SelectionData& selection_data, guint, guint time)
2474     {
2475     gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
2476    
2477 persson 1265 if (sample && selection_data.get_length() == sizeof(gig::Sample*)) {
2478 persson 1303 std::cout << "Drop received sample \"" <<
2479     sample->pInfo->Name << "\"" << std::endl;
2480     // drop success
2481     context->drop_reply(true, time);
2482    
2483 schoenebeck 1322 //TODO: we should better move most of the following code to DimRegionEdit::set_sample()
2484    
2485     // notify everybody that we're going to alter the region
2486     gig::Region* region = m_RegionChooser.get_region();
2487     region_to_be_changed_signal.emit(region);
2488    
2489 persson 1303 // find the samplechannel dimension
2490     gig::dimension_def_t* stereo_dimension = 0;
2491     for (int i = 0 ; i < region->Dimensions ; i++) {
2492     if (region->pDimensionDefinitions[i].dimension ==
2493     gig::dimension_samplechannel) {
2494     stereo_dimension = &region->pDimensionDefinitions[i];
2495     break;
2496     }
2497 schoenebeck 1225 }
2498 persson 1303 bool channels_changed = false;
2499     if (sample->Channels == 1 && stereo_dimension) {
2500     // remove the samplechannel dimension
2501 schoenebeck 2550 /* commented out, because it makes it impossible building up an instrument from scratch using two separate L/R samples
2502 persson 1303 region->DeleteDimension(stereo_dimension);
2503     channels_changed = true;
2504     region_changed();
2505 schoenebeck 2550 */
2506 persson 1303 }
2507 schoenebeck 2464 dimreg_edit.set_sample(
2508     sample,
2509     is_copy_samples_unity_note_enabled(),
2510     is_copy_samples_fine_tune_enabled(),
2511     is_copy_samples_loop_enabled()
2512     );
2513 persson 1303
2514     if (sample->Channels == 2 && !stereo_dimension) {
2515     // add samplechannel dimension
2516     gig::dimension_def_t dim;
2517     dim.dimension = gig::dimension_samplechannel;
2518     dim.bits = 1;
2519     dim.zones = 2;
2520     region->AddDimension(&dim);
2521     channels_changed = true;
2522     region_changed();
2523     }
2524     if (channels_changed) {
2525     // unmap all samples with wrong number of channels
2526     // TODO: maybe there should be a warning dialog for this
2527     for (int i = 0 ; i < region->DimensionRegions ; i++) {
2528     gig::DimensionRegion* d = region->pDimensionRegions[i];
2529     if (d->pSample && d->pSample->Channels != sample->Channels) {
2530 schoenebeck 1322 gig::Sample* oldref = d->pSample;
2531     d->pSample = NULL;
2532     sample_ref_changed_signal.emit(oldref, NULL);
2533 persson 1303 }
2534     }
2535     }
2536    
2537 schoenebeck 1322 // notify we're done with altering
2538     region_changed_signal.emit(region);
2539    
2540 persson 1460 file_changed();
2541    
2542 persson 1303 return;
2543 schoenebeck 1225 }
2544     // drop failed
2545     context->drop_reply(false, time);
2546     }
2547    
2548     void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path,
2549     const Gtk::TreeModel::iterator& iter) {
2550     if (!iter) return;
2551     Gtk::TreeModel::Row row = *iter;
2552     Glib::ustring name = row[m_SamplesModel.m_col_name];
2553     gig::Group* group = row[m_SamplesModel.m_col_group];
2554     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2555 persson 2446 gig::String gigname(gig_from_utf8(name));
2556 schoenebeck 1225 if (group) {
2557 persson 2446 if (group->Name != gigname) {
2558     group->Name = gigname;
2559 persson 1261 printf("group name changed\n");
2560     file_changed();
2561     }
2562 schoenebeck 1225 } else if (sample) {
2563 persson 2446 if (sample->pInfo->Name != gigname) {
2564     sample->pInfo->Name = gigname;
2565 persson 1261 printf("sample name changed\n");
2566     file_changed();
2567     }
2568 schoenebeck 1225 }
2569     }
2570    
2571 schoenebeck 2604 void MainWindow::script_name_changed(const Gtk::TreeModel::Path& path,
2572     const Gtk::TreeModel::iterator& iter) {
2573     if (!iter) return;
2574     Gtk::TreeModel::Row row = *iter;
2575     Glib::ustring name = row[m_ScriptsModel.m_col_name];
2576     gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2577     gig::Script* script = row[m_ScriptsModel.m_col_script];
2578     gig::String gigname(gig_from_utf8(name));
2579     if (group) {
2580     if (group->Name != gigname) {
2581     group->Name = gigname;
2582     printf("script group name changed\n");
2583     file_changed();
2584     }
2585     } else if (script) {
2586     if (script->Name != gigname) {
2587     script->Name = gigname;
2588     printf("script name changed\n");
2589     file_changed();
2590     }
2591     }
2592     }
2593    
2594 schoenebeck 2644 void MainWindow::script_double_clicked(const Gtk::TreeModel::Path& path,
2595     Gtk::TreeViewColumn* column)
2596     {
2597     Gtk::TreeModel::iterator iter = m_refScriptsTreeModel->get_iter(path);
2598     if (!iter) return;
2599     Gtk::TreeModel::Row row = *iter;
2600     gig::Script* script = row[m_ScriptsModel.m_col_script];
2601     if (!script) return;
2602    
2603     ScriptEditor* editor = new ScriptEditor;
2604     editor->setScript(script);
2605     //editor->reparent(*this);
2606     editor->show();
2607     }
2608    
2609 schoenebeck 1225 void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
2610     const Gtk::TreeModel::iterator& iter) {
2611     if (!iter) return;
2612     Gtk::TreeModel::Row row = *iter;
2613     Glib::ustring name = row[m_Columns.m_col_name];
2614 persson 2442
2615     // change name in instrument menu
2616     int index = path[0];
2617     const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
2618     if (index < children.size()) {
2619     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 16) || GTKMM_MAJOR_VERSION > 2
2620     static_cast<Gtk::RadioMenuItem*>(children[index])->set_label(name);
2621     #else
2622     remove_instrument_from_menu(index);
2623     Gtk::RadioMenuItem* item = add_instrument_to_menu(name, index);
2624     item->set_active();
2625     #endif
2626     }
2627    
2628     // change name in gig
2629 schoenebeck 1225 gig::Instrument* instrument = row[m_Columns.m_col_instr];
2630 persson 2446 gig::String gigname(gig_from_utf8(name));
2631     if (instrument && instrument->pInfo->Name != gigname) {
2632     instrument->pInfo->Name = gigname;
2633 persson 2445
2634     // change name in the instrument properties window
2635     if (instrumentProps.get_instrument() == instrument) {
2636     instrumentProps.update_name();
2637     }
2638    
2639 persson 1261 file_changed();
2640     }
2641 schoenebeck 1225 }
2642 schoenebeck 1322
2643 schoenebeck 2548 void MainWindow::on_action_combine_instruments() {
2644     CombineInstrumentsDialog* d = new CombineInstrumentsDialog(*this, file);
2645     d->show_all();
2646     d->resize(500, 400);
2647     d->run();
2648     if (d->fileWasChanged()) {
2649     // update GUI with new instrument just created
2650     add_instrument(d->newCombinedInstrument());
2651     }
2652     delete d;
2653     }
2654    
2655 schoenebeck 2624 void MainWindow::on_action_view_references() {
2656     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2657     Gtk::TreeModel::iterator it = sel->get_selected();
2658     if (!it) return;
2659     Gtk::TreeModel::Row row = *it;
2660     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2661     if (!sample) return;
2662    
2663     ReferencesView* d = new ReferencesView(*this);
2664     d->setSample(sample);
2665     d->show_all();
2666     d->resize(500, 400);
2667     d->run();
2668     delete d;
2669     }
2670    
2671 schoenebeck 2553 void MainWindow::mergeFiles(const std::vector<std::string>& filenames) {
2672     struct _Source {
2673     std::vector<RIFF::File*> riffs;
2674     std::vector<gig::File*> gigs;
2675    
2676     ~_Source() {
2677     for (int k = 0; k < gigs.size(); ++k) delete gigs[k];
2678     for (int k = 0; k < riffs.size(); ++k) delete riffs[k];
2679     riffs.clear();
2680     gigs.clear();
2681     }
2682     } sources;
2683    
2684     if (filenames.empty())
2685     throw RIFF::Exception(_("No files selected, so nothing done."));
2686    
2687     // first open all input files (to avoid output file corruption)
2688     int i;
2689     try {
2690     for (i = 0; i < filenames.size(); ++i) {
2691     const std::string& filename = filenames[i];
2692     printf("opening file=%s\n", filename.c_str());
2693    
2694     RIFF::File* riff = new RIFF::File(filename);
2695     sources.riffs.push_back(riff);
2696    
2697     gig::File* gig = new gig::File(riff);
2698     sources.gigs.push_back(gig);
2699     }
2700     } catch (RIFF::Exception e) {
2701     throw RIFF::Exception(
2702     _("Error occurred while opening '") +
2703     filenames[i] +
2704     "': " +
2705     e.Message
2706     );
2707     } catch (...) {
2708     throw RIFF::Exception(
2709     _("Unknown exception occurred while opening '") +
2710     filenames[i] + "'"
2711     );
2712     }
2713    
2714     // now merge the opened .gig files to the main .gig file currently being
2715     // open in gigedit
2716     try {
2717     for (i = 0; i < filenames.size(); ++i) {
2718     const std::string& filename = filenames[i];
2719     printf("merging file=%s\n", filename.c_str());
2720     assert(i < sources.gigs.size());
2721    
2722     this->file->AddContentOf(sources.gigs[i]);
2723     }
2724     } catch (RIFF::Exception e) {
2725     throw RIFF::Exception(
2726     _("Error occurred while merging '") +
2727     filenames[i] +
2728     "': " +
2729     e.Message
2730     );
2731     } catch (...) {
2732     throw RIFF::Exception(
2733     _("Unknown exception occurred while merging '") +
2734     filenames[i] + "'"
2735     );
2736     }
2737    
2738     // Note: requires that this file already has a filename !
2739     this->file->Save();
2740     }
2741    
2742     void MainWindow::on_action_merge_files() {
2743     if (this->file->GetFileName().empty()) {
2744     Glib::ustring txt = _(
2745     "You seem to have a new .gig file open that has not been saved "
2746     "yet. You must save it somewhere before starting to merge it with "
2747     "other .gig files though, because during the merge operation the "
2748     "other files' sample data must be written on file level to the "
2749     "target .gig file."
2750     );
2751     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2752     msg.run();
2753     return;
2754     }
2755    
2756     Gtk::FileChooserDialog dialog(*this, _("Merge .gig files"));
2757     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2758     dialog.add_button(_("Merge"), Gtk::RESPONSE_OK);
2759     dialog.set_default_response(Gtk::RESPONSE_CANCEL);
2760     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2761     Gtk::FileFilter filter;
2762     filter.add_pattern("*.gig");
2763     #else
2764     Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
2765     filter->add_pattern("*.gig");
2766     #endif
2767     dialog.set_filter(filter);
2768     if (current_gig_dir != "") {
2769     dialog.set_current_folder(current_gig_dir);
2770     }
2771     dialog.set_select_multiple(true);
2772    
2773     // show warning in the file picker dialog
2774     Gtk::HBox descriptionArea;
2775     descriptionArea.set_spacing(15);
2776     Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
2777     descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
2778     #if GTKMM_MAJOR_VERSION < 3
2779     view::WrapLabel description;
2780     #else
2781     Gtk::Label description;
2782     description.set_line_wrap();
2783     #endif
2784     description.set_markup(_(
2785     "\nSelect at least one .gig file that shall be merged to the .gig file "
2786     "currently being open in gigedit.\n\n"
2787     "<b>Please Note:</b> Merging with other files will modify your "
2788     "currently open .gig file on file level! And be aware that the current "
2789     "merge algorithm does not detect duplicate samples yet. So if you are "
2790     "merging files which are using equivalent sample data, those "
2791     "equivalent samples will currently be treated as separate samples and "
2792     "will accordingly be stored separately in the target .gig file!"
2793     ));
2794     descriptionArea.pack_start(description);
2795     dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
2796     descriptionArea.show_all();
2797    
2798     if (dialog.run() == Gtk::RESPONSE_OK) {
2799     printf("on_action_merge_files self=%x\n", Glib::Threads::Thread::self());
2800     std::vector<std::string> filenames = dialog.get_filenames();
2801    
2802     // merge the selected files to the currently open .gig file
2803     try {
2804     mergeFiles(filenames);
2805     } catch (RIFF::Exception e) {
2806     Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
2807     msg.run();
2808     }
2809    
2810     // update GUI
2811     __refreshEntireGUI();
2812     }
2813     }
2814    
2815 schoenebeck 1411 void MainWindow::set_file_is_shared(bool b) {
2816     this->file_is_shared = b;
2817    
2818     if (file_is_shared) {
2819     m_AttachedStateLabel.set_label(_("live-mode"));
2820     m_AttachedStateImage.set(
2821     Gdk::Pixbuf::create_from_xpm_data(status_attached_xpm)
2822     );
2823     } else {
2824     m_AttachedStateLabel.set_label(_("stand-alone"));
2825     m_AttachedStateImage.set(
2826     Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)
2827     );
2828     }
2829     }
2830    
2831 schoenebeck 2621 void MainWindow::on_sample_ref_count_incremented(gig::Sample* sample, int offset) {
2832     if (!sample) return;
2833     sample_ref_count[sample] += offset;
2834     const int refcount = sample_ref_count[sample];
2835    
2836     Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
2837     for (int g = 0; g < model->children().size(); ++g) {
2838     Gtk::TreeModel::Row rowGroup = model->children()[g];
2839     for (int s = 0; s < rowGroup.children().size(); ++s) {
2840     Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
2841     if (rowSample[m_SamplesModel.m_col_sample] != sample) continue;
2842     rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
2843 schoenebeck 2625 rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
2844 schoenebeck 2621 }
2845     }
2846     }
2847    
2848     void MainWindow::on_sample_ref_changed(gig::Sample* oldSample, gig::Sample* newSample) {
2849     on_sample_ref_count_incremented(oldSample, -1);
2850     on_sample_ref_count_incremented(newSample, +1);
2851     }
2852    
2853     void MainWindow::on_samples_to_be_removed(std::list<gig::Sample*> samples) {
2854     // just in case a new sample is added later with exactly the same memory
2855     // address, which would lead to incorrect refcount if not deleted here
2856     for (std::list<gig::Sample*>::const_iterator it = samples.begin();
2857 schoenebeck 2666 it != samples.end(); ++it)
2858 schoenebeck 2621 {
2859     sample_ref_count.erase(*it);
2860     }
2861     }
2862    
2863 schoenebeck 2625 void MainWindow::show_samples_tab() {
2864     m_TreeViewNotebook.set_current_page(0);
2865     }
2866    
2867     void MainWindow::show_intruments_tab() {
2868     m_TreeViewNotebook.set_current_page(1);
2869     }
2870    
2871     void MainWindow::show_scripts_tab() {
2872     m_TreeViewNotebook.set_current_page(2);
2873     }
2874    
2875 schoenebeck 1339 sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_to_be_changed() {
2876 schoenebeck 1322 return file_structure_to_be_changed_signal;
2877     }
2878    
2879 schoenebeck 1339 sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_changed() {
2880 schoenebeck 1322 return file_structure_changed_signal;
2881     }
2882    
2883 schoenebeck 1339 sigc::signal<void, std::list<gig::Sample*> >& MainWindow::signal_samples_to_be_removed() {
2884 schoenebeck 1322 return samples_to_be_removed_signal;
2885     }
2886    
2887 schoenebeck 1339 sigc::signal<void>& MainWindow::signal_samples_removed() {
2888 schoenebeck 1322 return samples_removed_signal;
2889     }
2890    
2891 schoenebeck 1339 sigc::signal<void, gig::Region*>& MainWindow::signal_region_to_be_changed() {
2892 schoenebeck 1322 return region_to_be_changed_signal;
2893     }
2894    
2895 schoenebeck 1339 sigc::signal<void, gig::Region*>& MainWindow::signal_region_changed() {
2896 schoenebeck 1322 return region_changed_signal;
2897     }
2898    
2899 schoenebeck 1853 sigc::signal<void, gig::Sample*>& MainWindow::signal_sample_changed() {
2900     return sample_changed_signal;
2901     }
2902    
2903 schoenebeck 1339 sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& MainWindow::signal_sample_ref_changed() {
2904 schoenebeck 1322 return sample_ref_changed_signal;
2905     }
2906    
2907 schoenebeck 1339 sigc::signal<void, gig::DimensionRegion*>& MainWindow::signal_dimreg_to_be_changed() {
2908 schoenebeck 1322 return dimreg_to_be_changed_signal;
2909     }
2910    
2911 schoenebeck 1339 sigc::signal<void, gig::DimensionRegion*>& MainWindow::signal_dimreg_changed() {
2912 schoenebeck 1322 return dimreg_changed_signal;
2913     }
2914 schoenebeck 1654
2915     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_note_on() {
2916     return note_on_signal;
2917     }
2918    
2919     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_note_off() {
2920     return note_off_signal;
2921     }
2922 schoenebeck 1660
2923     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_hit() {
2924     return m_RegionChooser.signal_keyboard_key_hit();
2925     }
2926    
2927     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {
2928     return m_RegionChooser.signal_keyboard_key_released();
2929     }

  ViewVC Help
Powered by ViewVC