/[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 3144 - (show annotations) (download)
Wed May 3 19:00:11 2017 UTC (6 years, 10 months ago) by schoenebeck
File size: 149017 byte(s)
* Implemented copy & paste of DimensionRegion (with Alt+c and Alt+v).
* Bumped version (1.0.0.svn32).

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

  ViewVC Help
Powered by ViewVC