/[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 2325 - (hide annotations) (download)
Sun Mar 4 09:01:40 2012 UTC (8 years, 4 months ago) by persson
File size: 71797 byte(s)
* added support for new glibmm threads API

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

  ViewVC Help
Powered by ViewVC