/[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 2773 - (hide annotations) (download)
Fri Jun 12 17:57:52 2015 UTC (8 years, 9 months ago) by schoenebeck
File size: 130797 byte(s)
* Region Chooser: when moving a region around automatically move the
  keyboard position dependent pitch accordingly (can be turned off with
  "Settings"->"Move root key with region moved").

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

  ViewVC Help
Powered by ViewVC