/[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 1533 - (show annotations) (download)
Sat Dec 1 10:21:07 2007 UTC (16 years, 4 months ago) by persson
File size: 62190 byte(s)
* parameter edits can now be applied to multiple regions and dimension
  regions simultaneously - three checkboxes were added that select
  if changes apply to all regions and/or all dimension regions

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

  ViewVC Help
Powered by ViewVC