/[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 2423 - (hide annotations) (download)
Sun Feb 24 15:19:39 2013 UTC (11 years, 1 month ago) by persson
File size: 71107 byte(s)
* code refactoring: created a PropEdit class for property editor
  windows, moved Table class from mainwindow to paramedit
* minor gui tweaks: made note entry fields a bit wider, set a minimum
  width for scales
* bug fix: avoid stale information in the instrument properties window
  when a new file is loaded or the instrument is removed

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

  ViewVC Help
Powered by ViewVC