/[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 2395 - (show annotations) (download)
Mon Jan 7 23:35:08 2013 UTC (11 years, 2 months ago) by schoenebeck
File size: 73388 byte(s)
* added "Duplicate Instrument" function

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

  ViewVC Help
Powered by ViewVC