/[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 2621 - (show annotations) (download)
Wed Jun 11 16:46:27 2014 UTC (9 years, 9 months ago) by schoenebeck
File size: 105943 byte(s)
* Samples tree view on main window: display the amount of
  references for each sample and show the respective sample
  in gray color in case it is not referenced at all.

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

  ViewVC Help
Powered by ViewVC