/[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 2626 - (hide annotations) (download)
Thu Jun 12 15:12:00 2014 UTC (9 years, 9 months ago) by schoenebeck
File size: 110343 byte(s)
* Dimension Region Selector: implemented multi zone selection by press and
  hold CTRL key.
* Dimension Region Selector: show bottom and upper limit values for all
  dimension zones, not just the currently selected ones.
* Dimension Region Selector: Fixed drawing issues when zone sizes were
  altered by mouse drag and labels being displayed at wrong positions.

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

  ViewVC Help
Powered by ViewVC