/[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 2550 - (show annotations) (download)
Wed May 14 01:31:30 2014 UTC (6 years, 11 months ago) by schoenebeck
File size: 83036 byte(s)
* Fixed various bugs regarding new "combine instruments" tool.
* Show a warning if user tries to combine instruments in old
  .gig v2 format.
* Don't auto remove stereo dimension if user drags a mono
  sample reference on DimensionRegion's sample reference.
* Select "Instruments" tab on app start by default.

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

  ViewVC Help
Powered by ViewVC