/[svn]/gigedit/trunk/src/gigedit/mainwindow.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3134 - (show annotations) (download)
Fri Apr 28 12:41:12 2017 UTC (6 years, 11 months ago) by schoenebeck
File size: 143824 byte(s)
* Implemented selecting multiple dimension region zones by keyboard with
  keyboard accelerators Alt+Shift+Left and Alt+Shift+Right.
* Bumped version (1.0.0.svn31).

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

  ViewVC Help
Powered by ViewVC