/[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 2541 - (show annotations) (download)
Wed Apr 23 16:49:05 2014 UTC (4 years, 5 months ago) by schoenebeck
File size: 81986 byte(s)
* Mark all recently added new leverage controllers (which are an
  unofficial gig sound format extension, only understood by
  LinuxSampler so far, not by GSt) with a "[EXT]" tag and show
  the user a warning dialog in case he tries to use one of those
  new controller types (warning can be switched off from menu).

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

  ViewVC Help
Powered by ViewVC