/[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 2445 - (hide annotations) (download)
Sun Apr 28 06:07:22 2013 UTC (10 years, 11 months ago) by persson
File size: 74395 byte(s)
* fixed name entry field in instrument properties window

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

  ViewVC Help
Powered by ViewVC