/[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 3340 - (hide annotations) (download)
Mon Jul 31 11:20:18 2017 UTC (6 years, 7 months ago) by schoenebeck
File size: 165318 byte(s)
* Added search filter field which allows to quickly filter the instrument
  list by case insensitive search tokens.
* Fix: navigating between dimension zones by keyboard did sometimes not
  work after switching from an instrument with different dimension types.
* Fix: Sample references view dialog had some odd behaviors, like it did not
  respond to mouse scroll events for instance.
* Bumped version (1.0.0.svn62).

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

  ViewVC Help
Powered by ViewVC