/[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 1436 - (show annotations) (download)
Mon Oct 15 15:29:58 2007 UTC (12 years, 4 months ago) by schoenebeck
File size: 59688 byte(s)
* updated "About" dialog a bit

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

  ViewVC Help
Powered by ViewVC