/[svn]/gigedit/trunk/src/gigedit/mainwindow.cpp
ViewVC logotype

Annotation of /gigedit/trunk/src/gigedit/mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1382 - (hide annotations) (download)
Thu Oct 4 23:29:22 2007 UTC (16 years, 5 months ago) by schoenebeck
File size: 57506 byte(s)
* fixed a crash when selecting 'New' or opening another file while being
  attached to the sampler
* don't allow the user to remove an instrument while being attached to the
  sampler, because it wouldn't end well ;-)
* don't show a 'file modified' confirmation dialog when closing/quitting
  the application and being attached to the sampler, since the
  modifications won't be lost until the sampler was quit
* while beng attached to the sampler, show the user an information dialog
  when he selects 'New' or 'Open', since this will detach gigedit from the
  sampler
* updated German translation

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

  ViewVC Help
Powered by ViewVC