/[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 1411 - (hide annotations) (download)
Fri Oct 12 17:46:29 2007 UTC (16 years, 5 months ago) by schoenebeck
File size: 58259 byte(s)
* added status bar to the bottom of main window (independent area on the
  right shows whether gigedit is running stand-alone or attached to
  LinuxSampler)
* minor cosmetical fix in dimension manager widget
* regionchooser.cpp: temporary fix for a crash which occured when gigedit
  was compiled with CXXFLAGS="-g"

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

  ViewVC Help
Powered by ViewVC