/[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 2625 - (hide annotations) (download)
Wed Jun 11 21:22:24 2014 UTC (9 years, 9 months ago) by schoenebeck
File size: 110307 byte(s)
* Restructured main menu: duplicated all popup menus to the main menu,
  automatically switch to the right tree view tab when either selecting the
  "Sample", "Instrument" or "Script" main menu entry.
* Mark all not referenced samples in red color (instead of gray).

1 schoenebeck 1225 /*
2 persson 2507 * Copyright (C) 2006-2014 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 schoenebeck 2553 #include <assert.h>
44 schoenebeck 1225
45     #include "mainwindow.h"
46 schoenebeck 2541 #include "Settings.h"
47 schoenebeck 2548 #include "CombineInstrumentsDialog.h"
48 schoenebeck 2604 #include "scripteditor.h"
49 schoenebeck 2610 #include "scriptslots.h"
50 schoenebeck 2624 #include "ReferencesView.h"
51 schoenebeck 1411 #include "../../gfx/status_attached.xpm"
52     #include "../../gfx/status_detached.xpm"
53    
54 schoenebeck 1225
55 persson 1533 MainWindow::MainWindow() :
56     dimreg_label(_("Changes apply to:")),
57     dimreg_all_regions(_("all regions")),
58     dimreg_all_dimregs(_("all dimension splits")),
59     dimreg_stereo(_("both channels"))
60 schoenebeck 1225 {
61     // set_border_width(5);
62     // set_default_size(400, 200);
63    
64    
65     add(m_VBox);
66    
67     // Handle selection
68 persson 2442 m_TreeView.get_selection()->signal_changed().connect(
69 schoenebeck 1225 sigc::mem_fun(*this, &MainWindow::on_sel_change));
70    
71     // m_TreeView.set_reorderable();
72    
73     m_TreeView.signal_button_press_event().connect_notify(
74     sigc::mem_fun(*this, &MainWindow::on_button_release));
75    
76     // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
77     m_ScrolledWindow.add(m_TreeView);
78     // m_ScrolledWindow.set_size_request(200, 600);
79     m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
80    
81     m_ScrolledWindowSamples.add(m_TreeViewSamples);
82     m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
83    
84 schoenebeck 2604 m_ScrolledWindowScripts.add(m_TreeViewScripts);
85     m_ScrolledWindowScripts.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
86 schoenebeck 1225
87 schoenebeck 2604
88 schoenebeck 1225 m_TreeViewNotebook.set_size_request(300);
89    
90     m_HPaned.add1(m_TreeViewNotebook);
91 persson 1533 dimreg_hbox.add(dimreg_label);
92     dimreg_hbox.add(dimreg_all_regions);
93     dimreg_hbox.add(dimreg_all_dimregs);
94     dimreg_stereo.set_active();
95     dimreg_hbox.add(dimreg_stereo);
96     dimreg_vbox.add(dimreg_edit);
97 persson 1582 dimreg_vbox.pack_start(dimreg_hbox, Gtk::PACK_SHRINK);
98 persson 1533 m_HPaned.add2(dimreg_vbox);
99 schoenebeck 1225
100 schoenebeck 2536 dimreg_label.set_tooltip_text(_("To automatically apply your changes above globally to the entire instrument, check all 3 check boxes on the right."));
101     dimreg_all_regions.set_tooltip_text(_("If checked: all changes you perform above will automatically be applied to all regions of this instrument as well."));
102     dimreg_all_dimregs.set_tooltip_text(_("If checked: all changes you perform above will automatically be applied as well to all dimension splits of the region selected below."));
103     dimreg_stereo.set_tooltip_text(_("If checked: all changes you perform above will automatically be applied to both audio channel splits (only if a \"stereo\" dimension is defined below)."));
104 schoenebeck 1225
105 persson 1831 m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, _("Samples"));
106     m_TreeViewNotebook.append_page(m_ScrolledWindow, _("Instruments"));
107 schoenebeck 2604 m_TreeViewNotebook.append_page(m_ScrolledWindowScripts, _("Scripts"));
108 schoenebeck 1225
109     actionGroup = Gtk::ActionGroup::create();
110    
111     actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
112     actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW),
113     sigc::mem_fun(
114     *this, &MainWindow::on_action_file_new));
115     Glib::RefPtr<Gtk::Action> action =
116     Gtk::Action::create("Open", Gtk::Stock::OPEN);
117     action->property_label() = action->property_label() + "...";
118     actionGroup->add(action,
119     sigc::mem_fun(
120     *this, &MainWindow::on_action_file_open));
121     actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE),
122     sigc::mem_fun(
123     *this, &MainWindow::on_action_file_save));
124     action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS);
125     action->property_label() = action->property_label() + "...";
126     actionGroup->add(action,
127 persson 1261 Gtk::AccelKey("<shift><control>s"),
128 schoenebeck 1225 sigc::mem_fun(
129 persson 1261 *this, &MainWindow::on_action_file_save_as));
130 schoenebeck 1225 actionGroup->add(Gtk::Action::create("Properties",
131     Gtk::Stock::PROPERTIES),
132     sigc::mem_fun(
133     *this, &MainWindow::on_action_file_properties));
134     actionGroup->add(Gtk::Action::create("InstrProperties",
135     Gtk::Stock::PROPERTIES),
136     sigc::mem_fun(
137     *this, &MainWindow::show_instr_props));
138 persson 2507 actionGroup->add(Gtk::Action::create("MidiRules",
139 schoenebeck 2610 _("_Midi Rules...")),
140 persson 2507 sigc::mem_fun(
141     *this, &MainWindow::show_midi_rules));
142 schoenebeck 2610 actionGroup->add(Gtk::Action::create("ScriptSlots",
143     _("_Script Slots...")),
144     sigc::mem_fun(
145     *this, &MainWindow::show_script_slots));
146 schoenebeck 1225 actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),
147     sigc::mem_fun(
148 persson 1261 *this, &MainWindow::on_action_quit));
149 schoenebeck 2625 actionGroup->add(
150     Gtk::Action::create("MenuSample", _("_Sample")),
151     sigc::mem_fun(*this, &MainWindow::show_samples_tab)
152     );
153     actionGroup->add(
154     Gtk::Action::create("MenuInstrument", _("_Instrument")),
155     sigc::mem_fun(*this, &MainWindow::show_intruments_tab)
156     );
157     actionGroup->add(
158     Gtk::Action::create("MenuScript", _("S_cript")),
159     sigc::mem_fun(*this, &MainWindow::show_scripts_tab)
160     );
161     actionGroup->add(Gtk::Action::create("AllInstruments", _("_Select")));
162 schoenebeck 1225
163 schoenebeck 2464 actionGroup->add(Gtk::Action::create("MenuEdit", _("_Edit")));
164    
165     Glib::RefPtr<Gtk::ToggleAction> toggle_action =
166 schoenebeck 2536 Gtk::ToggleAction::create("CopySampleUnity", _("Copy Sample's _Unity Note"));
167 schoenebeck 2464 toggle_action->set_active(true);
168     actionGroup->add(toggle_action);
169    
170     toggle_action =
171     Gtk::ToggleAction::create("CopySampleTune", _("Copy Sample's _Fine Tune"));
172     toggle_action->set_active(true);
173     actionGroup->add(toggle_action);
174    
175     toggle_action =
176     Gtk::ToggleAction::create("CopySampleLoop", _("Copy Sample's _Loop Points"));
177     toggle_action->set_active(true);
178     actionGroup->add(toggle_action);
179    
180    
181 schoenebeck 1415 actionGroup->add(Gtk::Action::create("MenuView", _("_View")));
182 schoenebeck 2464 toggle_action =
183 schoenebeck 1415 Gtk::ToggleAction::create("Statusbar", _("_Statusbar"));
184     toggle_action->set_active(true);
185     actionGroup->add(toggle_action,
186     sigc::mem_fun(
187     *this, &MainWindow::on_action_view_status_bar));
188    
189 schoenebeck 1225 action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);
190     actionGroup->add(Gtk::Action::create("MenuHelp",
191     action->property_label()));
192     actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT),
193     sigc::mem_fun(
194     *this, &MainWindow::on_action_help_about));
195     actionGroup->add(
196     Gtk::Action::create("AddInstrument", _("Add _Instrument")),
197     sigc::mem_fun(*this, &MainWindow::on_action_add_instrument)
198     );
199     actionGroup->add(
200 schoenebeck 2395 Gtk::Action::create("DupInstrument", _("_Duplicate Instrument")),
201     sigc::mem_fun(*this, &MainWindow::on_action_duplicate_instrument)
202     );
203     actionGroup->add(
204 schoenebeck 1225 Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE),
205     sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument)
206     );
207    
208 schoenebeck 2541
209     actionGroup->add(Gtk::Action::create("MenuSettings", _("_Settings")));
210    
211     toggle_action =
212     Gtk::ToggleAction::create("WarnUserOnExtensions", _("Show warning on format _extensions"));
213     toggle_action->set_active(Settings::singleton()->warnUserOnExtensions);
214     actionGroup->add(
215     toggle_action,
216     sigc::mem_fun(*this, &MainWindow::on_action_warn_user_on_extensions)
217     );
218    
219    
220 schoenebeck 2548 actionGroup->add(Gtk::Action::create("MenuTools", _("_Tools")));
221    
222     actionGroup->add(
223     Gtk::Action::create("CombineInstruments", _("_Combine Instruments...")),
224     sigc::mem_fun(*this, &MainWindow::on_action_combine_instruments)
225     );
226    
227 schoenebeck 2553 actionGroup->add(
228     Gtk::Action::create("MergeFiles", _("_Merge Files...")),
229     sigc::mem_fun(*this, &MainWindow::on_action_merge_files)
230     );
231 schoenebeck 2548
232 schoenebeck 2553
233 schoenebeck 1225 // sample right-click popup actions
234     actionGroup->add(
235     Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES),
236     sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)
237     );
238     actionGroup->add(
239     Gtk::Action::create("AddGroup", _("Add _Group")),
240     sigc::mem_fun(*this, &MainWindow::on_action_add_group)
241     );
242     actionGroup->add(
243 persson 1799 Gtk::Action::create("AddSample", _("Add _Sample(s)...")),
244 schoenebeck 1225 sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
245     );
246     actionGroup->add(
247     Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),
248     sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
249     );
250 schoenebeck 1673 actionGroup->add(
251 schoenebeck 2624 Gtk::Action::create("ShowSampleRefs", _("Show References...")),
252     sigc::mem_fun(*this, &MainWindow::on_action_view_references)
253     );
254     actionGroup->add(
255 persson 1799 Gtk::Action::create("ReplaceAllSamplesInAllGroups",
256     _("Replace All Samples in All Groups...")),
257 schoenebeck 1673 sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups)
258     );
259 schoenebeck 2604
260     // script right-click popup actions
261     actionGroup->add(
262     Gtk::Action::create("AddScriptGroup", _("Add _Group")),
263     sigc::mem_fun(*this, &MainWindow::on_action_add_script_group)
264     );
265     actionGroup->add(
266     Gtk::Action::create("AddScript", _("Add _Script")),
267     sigc::mem_fun(*this, &MainWindow::on_action_add_script)
268     );
269     actionGroup->add(
270     Gtk::Action::create("EditScript", _("_Edit Script...")),
271     sigc::mem_fun(*this, &MainWindow::on_action_edit_script)
272     );
273     actionGroup->add(
274     Gtk::Action::create("RemoveScript", Gtk::Stock::REMOVE),
275     sigc::mem_fun(*this, &MainWindow::on_action_remove_script)
276     );
277 schoenebeck 1225
278     uiManager = Gtk::UIManager::create();
279     uiManager->insert_action_group(actionGroup);
280 persson 1261 add_accel_group(uiManager->get_accel_group());
281 schoenebeck 1225
282     Glib::ustring ui_info =
283     "<ui>"
284     " <menubar name='MenuBar'>"
285     " <menu action='MenuFile'>"
286     " <menuitem action='New'/>"
287     " <menuitem action='Open'/>"
288     " <separator/>"
289     " <menuitem action='Save'/>"
290     " <menuitem action='SaveAs'/>"
291     " <separator/>"
292     " <menuitem action='Properties'/>"
293     " <separator/>"
294     " <menuitem action='Quit'/>"
295     " </menu>"
296 schoenebeck 2464 " <menu action='MenuEdit'>"
297     " <menuitem action='CopySampleUnity'/>"
298     " <menuitem action='CopySampleTune'/>"
299     " <menuitem action='CopySampleLoop'/>"
300     " </menu>"
301 schoenebeck 2625 " <menu action='MenuSample'>"
302     " <menuitem action='SampleProperties'/>"
303     " <menuitem action='AddGroup'/>"
304     " <menuitem action='AddSample'/>"
305     " <menuitem action='ShowSampleRefs'/>"
306     " <menuitem action='ReplaceAllSamplesInAllGroups' />"
307     " <separator/>"
308     " <menuitem action='RemoveSample'/>"
309     " </menu>"
310 schoenebeck 1225 " <menu action='MenuInstrument'>"
311 schoenebeck 2625 " <menu action='AllInstruments'>"
312     " </menu>"
313     " <separator/>"
314     " <menuitem action='InstrProperties'/>"
315     " <menuitem action='MidiRules'/>"
316     " <menuitem action='ScriptSlots'/>"
317     " <menuitem action='AddInstrument'/>"
318     " <menuitem action='DupInstrument'/>"
319     " <separator/>"
320     " <menuitem action='RemoveInstrument'/>"
321 schoenebeck 1225 " </menu>"
322 schoenebeck 2625 " <menu action='MenuScript'>"
323     " <menuitem action='AddScriptGroup'/>"
324     " <menuitem action='AddScript'/>"
325     " <menuitem action='EditScript'/>"
326     " <separator/>"
327     " <menuitem action='RemoveScript'/>"
328     " </menu>"
329 schoenebeck 1415 " <menu action='MenuView'>"
330     " <menuitem action='Statusbar'/>"
331     " </menu>"
332 schoenebeck 2548 " <menu action='MenuTools'>"
333     " <menuitem action='CombineInstruments'/>"
334 schoenebeck 2553 " <menuitem action='MergeFiles'/>"
335 schoenebeck 2548 " </menu>"
336 schoenebeck 2541 " <menu action='MenuSettings'>"
337     " <menuitem action='WarnUserOnExtensions'/>"
338     " </menu>"
339 schoenebeck 1225 " <menu action='MenuHelp'>"
340     " <menuitem action='About'/>"
341     " </menu>"
342     " </menubar>"
343     " <popup name='PopupMenu'>"
344     " <menuitem action='InstrProperties'/>"
345 persson 2507 " <menuitem action='MidiRules'/>"
346 schoenebeck 2610 " <menuitem action='ScriptSlots'/>"
347 schoenebeck 1225 " <menuitem action='AddInstrument'/>"
348 schoenebeck 2395 " <menuitem action='DupInstrument'/>"
349 schoenebeck 1225 " <separator/>"
350     " <menuitem action='RemoveInstrument'/>"
351     " </popup>"
352     " <popup name='SamplePopupMenu'>"
353     " <menuitem action='SampleProperties'/>"
354     " <menuitem action='AddGroup'/>"
355     " <menuitem action='AddSample'/>"
356 schoenebeck 2624 " <menuitem action='ShowSampleRefs'/>"
357 persson 2442 " <menuitem action='ReplaceAllSamplesInAllGroups' />"
358 schoenebeck 1225 " <separator/>"
359     " <menuitem action='RemoveSample'/>"
360     " </popup>"
361 schoenebeck 2604 " <popup name='ScriptPopupMenu'>"
362     " <menuitem action='AddScriptGroup'/>"
363     " <menuitem action='AddScript'/>"
364     " <menuitem action='EditScript'/>"
365     " <separator/>"
366     " <menuitem action='RemoveScript'/>"
367     " </popup>"
368 schoenebeck 1225 "</ui>";
369     uiManager->add_ui_from_string(ui_info);
370    
371     popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));
372 schoenebeck 2536
373     // Set tooltips for menu items (for some reason, setting a tooltip on the
374     // respective Gtk::Action objects above will simply be ignored, no matter
375     // if using Gtk::Action::set_tooltip() or passing the tooltip string on
376     // Gtk::Action::create()).
377     {
378     Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
379     uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity"));
380     item->set_tooltip_text(_("Used when dragging a sample to a region's sample reference field. You may disable this for example if you want to replace an existing sample in a region with a new sample, but don't want that the region's current unity note setting will be altered by this action."));
381     }
382     {
383     Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
384     uiManager->get_widget("/MenuBar/MenuEdit/CopySampleTune"));
385     item->set_tooltip_text(_("Used when dragging a sample to a region's sample reference field. You may disable this for example if you want to replace an existing sample in a region with a new sample, but don't want that the region's current sample playback tuning will be altered by this action."));
386     }
387     {
388     Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
389     uiManager->get_widget("/MenuBar/MenuEdit/CopySampleLoop"));
390     item->set_tooltip_text(_("Used when dragging a sample to a region's sample reference field. You may disable this for example if you want to replace an existing sample in a region with a new sample, but don't want that the region's current loop informations to be altered by this action."));
391     }
392 schoenebeck 2541 {
393     Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
394     uiManager->get_widget("/MenuBar/MenuSettings/WarnUserOnExtensions"));
395     item->set_tooltip_text(_("If checked, a warning will be shown whenever you try to use a feature which is based on a LinuxSampler extension ontop of the original gig format, which would not work with the Gigasampler/GigaStudio application."));
396     }
397 schoenebeck 2553 {
398     Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
399     uiManager->get_widget("/MenuBar/MenuTools/CombineInstruments"));
400     item->set_tooltip_text(_("Create combi sounds out of individual sounds of this .gig file."));
401     }
402     {
403     Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
404     uiManager->get_widget("/MenuBar/MenuTools/MergeFiles"));
405     item->set_tooltip_text(_("Add instruments and samples of other .gig files to this .gig file."));
406     }
407 schoenebeck 1225
408 schoenebeck 2553
409 persson 2442 instrument_menu = static_cast<Gtk::MenuItem*>(
410 schoenebeck 2625 uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments"))->get_submenu();
411 persson 2442
412 schoenebeck 1225 Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
413     m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
414     m_VBox.pack_start(m_HPaned);
415     m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK);
416 schoenebeck 1661 m_VBox.pack_start(m_RegionChooser.m_VirtKeybPropsBox, Gtk::PACK_SHRINK);
417 schoenebeck 1225 m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);
418 schoenebeck 1411 m_VBox.pack_start(m_StatusBar, Gtk::PACK_SHRINK);
419 schoenebeck 1225
420 persson 2246 set_file_is_shared(false);
421    
422 schoenebeck 1411 // Status Bar:
423     m_StatusBar.pack_start(m_AttachedStateLabel, Gtk::PACK_SHRINK);
424     m_StatusBar.pack_start(m_AttachedStateImage, Gtk::PACK_SHRINK);
425     m_StatusBar.show();
426    
427 persson 1261 m_RegionChooser.signal_region_selected().connect(
428 schoenebeck 1225 sigc::mem_fun(*this, &MainWindow::region_changed) );
429 persson 1261 m_DimRegionChooser.signal_dimregion_selected().connect(
430 schoenebeck 1225 sigc::mem_fun(*this, &MainWindow::dimreg_changed) );
431    
432    
433     // Create the Tree model:
434     m_refTreeModel = Gtk::ListStore::create(m_Columns);
435     m_TreeView.set_model(m_refTreeModel);
436 schoenebeck 2536 m_TreeView.set_tooltip_text(_("Right click here for actions on instruments & MIDI Rules."));
437 persson 2442 instrument_name_connection = m_refTreeModel->signal_row_changed().connect(
438 schoenebeck 1225 sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
439     );
440    
441     // Add the TreeView's view columns:
442     m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);
443     m_TreeView.set_headers_visible(false);
444    
445     // create samples treeview (including its data model)
446     m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);
447     m_TreeViewSamples.set_model(m_refSamplesTreeModel);
448 schoenebeck 2536 m_TreeViewSamples.set_tooltip_text(_("To actually use a sample, drag it from this list view to \"Sample\" -> \"Sample:\" on the region's settings pane on the right.\n\nRight click here for more actions on samples."));
449 schoenebeck 1225 // m_TreeViewSamples.set_reorderable();
450 schoenebeck 2621 m_TreeViewSamples.append_column_editable(_("Name"), m_SamplesModel.m_col_name);
451     m_TreeViewSamples.append_column(_("Referenced"), m_SamplesModel.m_col_refcount);
452     {
453     Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(0);
454     Gtk::CellRendererText* cellrenderer =
455     dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell_renderer());
456     column->add_attribute(
457     cellrenderer->property_foreground(), m_SamplesModel.m_color
458     );
459     }
460     {
461     Gtk::TreeViewColumn* column = m_TreeViewSamples.get_column(1);
462     Gtk::CellRendererText* cellrenderer =
463     dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell_renderer());
464     column->add_attribute(
465     cellrenderer->property_foreground(), m_SamplesModel.m_color
466     );
467     }
468     m_TreeViewSamples.set_headers_visible(true);
469 schoenebeck 1225 m_TreeViewSamples.signal_button_press_event().connect_notify(
470     sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
471     );
472     m_refSamplesTreeModel->signal_row_changed().connect(
473     sigc::mem_fun(*this, &MainWindow::sample_name_changed)
474     );
475    
476 schoenebeck 2604 // create scripts treeview (including its data model)
477     m_refScriptsTreeModel = ScriptsTreeStore::create(m_ScriptsModel);
478     m_TreeViewScripts.set_model(m_refScriptsTreeModel);
479     m_TreeViewScripts.set_tooltip_text(_(
480     "Note: instrument scripts are a LinuxSampler extension of the gig "
481     "format. This feature will not work with the GigaStudio software!"
482     ));
483     // m_TreeViewScripts.set_reorderable();
484     m_TreeViewScripts.append_column_editable("Samples", m_ScriptsModel.m_col_name);
485     m_TreeViewScripts.set_headers_visible(false);
486     m_TreeViewScripts.signal_button_press_event().connect_notify(
487     sigc::mem_fun(*this, &MainWindow::on_script_treeview_button_release)
488     );
489     m_refScriptsTreeModel->signal_row_changed().connect(
490     sigc::mem_fun(*this, &MainWindow::script_name_changed)
491     );
492    
493 schoenebeck 2610 // establish drag&drop between scripts tree view and ScriptSlots window
494     std::vector<Gtk::TargetEntry> drag_target_gig_script;
495     drag_target_gig_script.push_back(Gtk::TargetEntry("gig::Script"));
496     m_TreeViewScripts.drag_source_set(drag_target_gig_script);
497     m_TreeViewScripts.signal_drag_begin().connect(
498     sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_begin)
499     );
500     m_TreeViewScripts.signal_drag_data_get().connect(
501     sigc::mem_fun(*this, &MainWindow::on_scripts_treeview_drag_data_get)
502     );
503    
504 schoenebeck 1225 // establish drag&drop between samples tree view and dimension region 'Sample' text entry
505 persson 2169 std::vector<Gtk::TargetEntry> drag_target_gig_sample;
506     drag_target_gig_sample.push_back(Gtk::TargetEntry("gig::Sample"));
507 schoenebeck 1225 m_TreeViewSamples.drag_source_set(drag_target_gig_sample);
508 persson 1303 m_TreeViewSamples.signal_drag_begin().connect(
509     sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_begin)
510     );
511 schoenebeck 1225 m_TreeViewSamples.signal_drag_data_get().connect(
512     sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get)
513     );
514     dimreg_edit.wSample->drag_dest_set(drag_target_gig_sample);
515     dimreg_edit.wSample->signal_drag_data_received().connect(
516     sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received)
517     );
518 persson 1261 dimreg_edit.signal_dimreg_changed().connect(
519 schoenebeck 1322 sigc::hide(sigc::mem_fun(*this, &MainWindow::file_changed)));
520 persson 1261 m_RegionChooser.signal_instrument_changed().connect(
521     sigc::mem_fun(*this, &MainWindow::file_changed));
522     m_DimRegionChooser.signal_region_changed().connect(
523     sigc::mem_fun(*this, &MainWindow::file_changed));
524 persson 2423 instrumentProps.signal_changed().connect(
525 persson 1261 sigc::mem_fun(*this, &MainWindow::file_changed));
526 persson 2423 propDialog.signal_changed().connect(
527 persson 1582 sigc::mem_fun(*this, &MainWindow::file_changed));
528 persson 2507 midiRules.signal_changed().connect(
529     sigc::mem_fun(*this, &MainWindow::file_changed));
530 schoenebeck 1322
531     dimreg_edit.signal_dimreg_to_be_changed().connect(
532     dimreg_to_be_changed_signal.make_slot());
533     dimreg_edit.signal_dimreg_changed().connect(
534     dimreg_changed_signal.make_slot());
535     dimreg_edit.signal_sample_ref_changed().connect(
536     sample_ref_changed_signal.make_slot());
537 schoenebeck 2621 sample_ref_changed_signal.connect(
538     sigc::mem_fun(*this, &MainWindow::on_sample_ref_changed)
539     );
540     samples_to_be_removed_signal.connect(
541     sigc::mem_fun(*this, &MainWindow::on_samples_to_be_removed)
542     );
543 schoenebeck 1322
544     m_RegionChooser.signal_instrument_struct_to_be_changed().connect(
545     sigc::hide(
546     sigc::bind(
547     file_structure_to_be_changed_signal.make_slot(),
548     sigc::ref(this->file)
549     )
550     )
551     );
552     m_RegionChooser.signal_instrument_struct_changed().connect(
553     sigc::hide(
554     sigc::bind(
555     file_structure_changed_signal.make_slot(),
556     sigc::ref(this->file)
557     )
558     )
559     );
560     m_RegionChooser.signal_region_to_be_changed().connect(
561     region_to_be_changed_signal.make_slot());
562     m_RegionChooser.signal_region_changed_signal().connect(
563     region_changed_signal.make_slot());
564    
565 schoenebeck 1654 note_on_signal.connect(
566     sigc::mem_fun(m_RegionChooser, &RegionChooser::on_note_on_event));
567     note_off_signal.connect(
568     sigc::mem_fun(m_RegionChooser, &RegionChooser::on_note_off_event));
569    
570 persson 1533 dimreg_all_regions.signal_toggled().connect(
571     sigc::mem_fun(*this, &MainWindow::update_dimregs));
572     dimreg_all_dimregs.signal_toggled().connect(
573     sigc::mem_fun(*this, &MainWindow::dimreg_all_dimregs_toggled));
574     dimreg_stereo.signal_toggled().connect(
575     sigc::mem_fun(*this, &MainWindow::update_dimregs));
576    
577 schoenebeck 1225 file = 0;
578 persson 1261 file_is_changed = false;
579 schoenebeck 1225
580     show_all_children();
581 schoenebeck 1300
582     // start with a new gig file by default
583     on_action_file_new();
584 schoenebeck 2550
585     // select 'Instruments' tab by default
586     // (gtk allows this only if the tab childs are visible, thats why it's here)
587     m_TreeViewNotebook.set_current_page(1);
588 schoenebeck 1225 }
589    
590     MainWindow::~MainWindow()
591     {
592     }
593    
594 persson 1261 bool MainWindow::on_delete_event(GdkEventAny* event)
595     {
596 schoenebeck 1382 return !file_is_shared && file_is_changed && !close_confirmation_dialog();
597 persson 1261 }
598    
599     void MainWindow::on_action_quit()
600     {
601 schoenebeck 1382 if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
602 persson 1261 hide();
603     }
604    
605 schoenebeck 1225 void MainWindow::region_changed()
606     {
607     m_DimRegionChooser.set_region(m_RegionChooser.get_region());
608     }
609    
610 persson 1533 gig::Instrument* MainWindow::get_instrument()
611 schoenebeck 1225 {
612 persson 1533 gig::Instrument* instrument = 0;
613 persson 2442 Gtk::TreeModel::const_iterator it =
614     m_TreeView.get_selection()->get_selected();
615 schoenebeck 1225 if (it) {
616     Gtk::TreeModel::Row row = *it;
617 persson 1533 instrument = row[m_Columns.m_col_instr];
618     }
619     return instrument;
620     }
621 schoenebeck 1225
622 persson 1533 void MainWindow::add_region_to_dimregs(gig::Region* region, bool stereo, bool all_dimregs)
623     {
624     if (all_dimregs) {
625     for (int i = 0 ; i < region->DimensionRegions ; i++) {
626     if (region->pDimensionRegions[i]) {
627     dimreg_edit.dimregs.insert(region->pDimensionRegions[i]);
628     }
629     }
630 schoenebeck 1225 } else {
631 persson 1533 m_DimRegionChooser.get_dimregions(region, stereo, dimreg_edit.dimregs);
632 schoenebeck 1225 }
633     }
634    
635 persson 1533 void MainWindow::update_dimregs()
636     {
637     dimreg_edit.dimregs.clear();
638     bool all_regions = dimreg_all_regions.get_active();
639     bool stereo = dimreg_stereo.get_active();
640     bool all_dimregs = dimreg_all_dimregs.get_active();
641    
642     if (all_regions) {
643     gig::Instrument* instrument = get_instrument();
644     if (instrument) {
645     for (gig::Region* region = instrument->GetFirstRegion() ;
646     region ;
647     region = instrument->GetNextRegion()) {
648     add_region_to_dimregs(region, stereo, all_dimregs);
649     }
650     }
651     } else {
652     gig::Region* region = m_RegionChooser.get_region();
653     if (region) {
654     add_region_to_dimregs(region, stereo, all_dimregs);
655     }
656     }
657     }
658    
659     void MainWindow::dimreg_all_dimregs_toggled()
660     {
661     dimreg_stereo.set_sensitive(!dimreg_all_dimregs.get_active());
662     update_dimregs();
663     }
664    
665     void MainWindow::dimreg_changed()
666     {
667     update_dimregs();
668     dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());
669     }
670    
671     void MainWindow::on_sel_change()
672     {
673 persson 2442 // select item in instrument menu
674     Gtk::TreeModel::iterator it = m_TreeView.get_selection()->get_selected();
675     if (it) {
676     Gtk::TreePath path(it);
677     int index = path[0];
678     const std::vector<Gtk::Widget*> children =
679     instrument_menu->get_children();
680     static_cast<Gtk::RadioMenuItem*>(children[index])->set_active();
681     }
682    
683 persson 1533 m_RegionChooser.set_instrument(get_instrument());
684     }
685    
686 schoenebeck 1225 void loader_progress_callback(gig::progress_t* progress)
687     {
688     Loader* loader = static_cast<Loader*>(progress->custom);
689     loader->progress_callback(progress->factor);
690     }
691    
692     void Loader::progress_callback(float fraction)
693     {
694     {
695 persson 2325 Glib::Threads::Mutex::Lock lock(progressMutex);
696 schoenebeck 1225 progress = fraction;
697     }
698     progress_dispatcher();
699     }
700    
701     void Loader::thread_function()
702     {
703 persson 2325 printf("thread_function self=%x\n", Glib::Threads::Thread::self());
704 schoenebeck 1225 printf("Start %s\n", filename);
705     RIFF::File* riff = new RIFF::File(filename);
706     gig = new gig::File(riff);
707     gig::progress_t progress;
708     progress.callback = loader_progress_callback;
709     progress.custom = this;
710    
711     gig->GetInstrument(0, &progress);
712     printf("End\n");
713     finished_dispatcher();
714     }
715    
716     Loader::Loader(const char* filename)
717 persson 2151 : filename(filename), thread(0)
718 schoenebeck 1225 {
719     }
720    
721     void Loader::launch()
722     {
723 persson 2332 #ifdef OLD_THREADS
724     thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
725     #else
726     thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Loader::thread_function));
727     #endif
728 schoenebeck 1225 printf("launch thread=%x\n", thread);
729     }
730    
731     float Loader::get_progress()
732     {
733     float res;
734     {
735 persson 2325 Glib::Threads::Mutex::Lock lock(progressMutex);
736 schoenebeck 1225 res = progress;
737     }
738     return res;
739     }
740    
741     Glib::Dispatcher& Loader::signal_progress()
742     {
743     return progress_dispatcher;
744     }
745    
746     Glib::Dispatcher& Loader::signal_finished()
747     {
748     return finished_dispatcher;
749     }
750    
751     LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)
752     : Gtk::Dialog(title, parent, true)
753     {
754     get_vbox()->pack_start(progressBar);
755     show_all_children();
756     }
757    
758     // Clear all GUI elements / controls. This method is typically called
759     // before a new .gig file is to be created or to be loaded.
760     void MainWindow::__clear() {
761     // forget all samples that ought to be imported
762     m_SampleImportQueue.clear();
763     // clear the samples and instruments tree views
764     m_refTreeModel->clear();
765     m_refSamplesTreeModel->clear();
766 schoenebeck 2615 m_refScriptsTreeModel->clear();
767 persson 2442 // remove all entries from "Instrument" menu
768     while (!instrument_menu->get_children().empty()) {
769     remove_instrument_from_menu(0);
770     }
771 schoenebeck 1225 // free libgig's gig::File instance
772 schoenebeck 1382 if (file && !file_is_shared) delete file;
773     file = NULL;
774 schoenebeck 1411 set_file_is_shared(false);
775 schoenebeck 1225 }
776    
777 schoenebeck 2553 void MainWindow::__refreshEntireGUI() {
778     // clear the samples and instruments tree views
779     m_refTreeModel->clear();
780     m_refSamplesTreeModel->clear();
781 schoenebeck 2615 m_refScriptsTreeModel->clear();
782 schoenebeck 2553 // remove all entries from "Instrument" menu
783     while (!instrument_menu->get_children().empty()) {
784     remove_instrument_from_menu(0);
785     }
786    
787     if (!this->file) return;
788    
789     load_gig(
790     this->file, this->file->pInfo->Name.c_str(), this->file_is_shared
791     );
792     }
793    
794 schoenebeck 1225 void MainWindow::on_action_file_new()
795     {
796 schoenebeck 1382 if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
797 persson 1261
798 schoenebeck 1382 if (file_is_shared && !leaving_shared_mode_dialog()) return;
799    
800 schoenebeck 1225 // clear all GUI elements
801     __clear();
802     // create a new .gig file (virtually yet)
803     gig::File* pFile = new gig::File;
804     // already add one new instrument by default
805     gig::Instrument* pInstrument = pFile->AddInstrument();
806 persson 2446 pInstrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument"));
807 schoenebeck 1225 // update GUI with that new gig::File
808 persson 1261 load_gig(pFile, 0 /*no file name yet*/);
809 schoenebeck 1225 }
810    
811 persson 1261 bool MainWindow::close_confirmation_dialog()
812     {
813     gchar* msg = g_strdup_printf(_("Save changes to \"%s\" before closing?"),
814     Glib::filename_display_basename(filename).c_str());
815     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
816     g_free(msg);
817     dialog.set_secondary_text(_("If you close without saving, your changes will be lost."));
818     dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO);
819     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
820     dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES);
821     dialog.set_default_response(Gtk::RESPONSE_YES);
822     int response = dialog.run();
823 persson 1303 dialog.hide();
824 persson 1261 if (response == Gtk::RESPONSE_YES) return file_save();
825     return response != Gtk::RESPONSE_CANCEL;
826     }
827    
828 schoenebeck 1382 bool MainWindow::leaving_shared_mode_dialog() {
829     Glib::ustring msg = _("Detach from sampler and proceed working stand-alone?");
830     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
831     dialog.set_secondary_text(
832     _("If you proceed to work on another instrument file, it won't be "
833     "used by the sampler until you tell the sampler explicitly to "
834 persson 2151 "load it."));
835 schoenebeck 1382 dialog.add_button(_("_Yes, Detach"), Gtk::RESPONSE_YES);
836     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
837     dialog.set_default_response(Gtk::RESPONSE_CANCEL);
838     int response = dialog.run();
839     dialog.hide();
840     return response == Gtk::RESPONSE_YES;
841     }
842    
843 schoenebeck 1225 void MainWindow::on_action_file_open()
844     {
845 schoenebeck 1382 if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return;
846 persson 1261
847 schoenebeck 1382 if (file_is_shared && !leaving_shared_mode_dialog()) return;
848    
849 schoenebeck 1225 Gtk::FileChooserDialog dialog(*this, _("Open file"));
850     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
851     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
852 persson 1261 dialog.set_default_response(Gtk::RESPONSE_OK);
853 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
854 schoenebeck 1225 Gtk::FileFilter filter;
855     filter.add_pattern("*.gig");
856 persson 2169 #else
857     Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
858     filter->add_pattern("*.gig");
859     #endif
860 schoenebeck 1225 dialog.set_filter(filter);
861 persson 1725 if (current_gig_dir != "") {
862     dialog.set_current_folder(current_gig_dir);
863 persson 1261 }
864 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
865 persson 1261 std::string filename = dialog.get_filename();
866     printf("filename=%s\n", filename.c_str());
867 persson 2325 printf("on_action_file_open self=%x\n", Glib::Threads::Thread::self());
868 persson 1261 load_file(filename.c_str());
869 persson 1725 current_gig_dir = Glib::path_get_dirname(filename);
870 schoenebeck 1225 }
871     }
872    
873     void MainWindow::load_file(const char* name)
874     {
875 persson 1303 __clear();
876 persson 1831 load_dialog = new LoadDialog(_("Loading..."), *this);
877 schoenebeck 1225 load_dialog->show_all();
878     loader = new Loader(strdup(name));
879     loader->signal_progress().connect(
880     sigc::mem_fun(*this, &MainWindow::on_loader_progress));
881     loader->signal_finished().connect(
882     sigc::mem_fun(*this, &MainWindow::on_loader_finished));
883     loader->launch();
884     }
885    
886     void MainWindow::load_instrument(gig::Instrument* instr) {
887     if (!instr) {
888     Glib::ustring txt = "Provided instrument is NULL!\n";
889     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
890     msg.run();
891     Gtk::Main::quit();
892     }
893 schoenebeck 1328 // clear all GUI elements
894     __clear();
895     // load the instrument
896 schoenebeck 1225 gig::File* pFile = (gig::File*) instr->GetParent();
897 schoenebeck 1382 load_gig(pFile, 0 /*file name*/, true /*shared instrument*/);
898 schoenebeck 1225 //TODO: automatically select the given instrument
899     }
900    
901     void MainWindow::on_loader_progress()
902     {
903     load_dialog->set_fraction(loader->get_progress());
904     }
905    
906     void MainWindow::on_loader_finished()
907     {
908     printf("Loader finished!\n");
909 persson 2325 printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());
910 schoenebeck 1225 load_gig(loader->gig, loader->filename);
911     load_dialog->hide();
912     }
913    
914     void MainWindow::on_action_file_save()
915     {
916 persson 1261 file_save();
917     }
918    
919 persson 1303 bool MainWindow::check_if_savable()
920     {
921     if (!file) return false;
922    
923     if (!file->GetFirstSample()) {
924     Gtk::MessageDialog(*this, _("The file could not be saved "
925     "because it contains no samples"),
926     false, Gtk::MESSAGE_ERROR).run();
927     return false;
928     }
929    
930     for (gig::Instrument* instrument = file->GetFirstInstrument() ; instrument ;
931     instrument = file->GetNextInstrument()) {
932     if (!instrument->GetFirstRegion()) {
933     Gtk::MessageDialog(*this, _("The file could not be saved "
934     "because there are instruments "
935     "that have no regions"),
936     false, Gtk::MESSAGE_ERROR).run();
937     return false;
938     }
939     }
940     return true;
941     }
942    
943 persson 1261 bool MainWindow::file_save()
944     {
945 persson 1303 if (!check_if_savable()) return false;
946 schoenebeck 1382 if (!file_is_shared && !file_has_name) return file_save_as();
947 persson 1261
948 schoenebeck 1225 std::cout << "Saving file\n" << std::flush;
949 schoenebeck 1322 file_structure_to_be_changed_signal.emit(this->file);
950 schoenebeck 1225 try {
951     file->Save();
952 persson 1261 if (file_is_changed) {
953     set_title(get_title().substr(1));
954     file_is_changed = false;
955     }
956 schoenebeck 1225 } catch (RIFF::Exception e) {
957 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
958 schoenebeck 1382 Glib::ustring txt = _("Could not save file: ") + e.Message;
959 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
960     msg.run();
961 persson 1261 return false;
962 schoenebeck 1225 }
963     std::cout << "Saving file done\n" << std::flush;
964     __import_queued_samples();
965 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
966 persson 1261 return true;
967 schoenebeck 1225 }
968    
969     void MainWindow::on_action_file_save_as()
970     {
971 persson 1303 if (!check_if_savable()) return;
972 persson 1261 file_save_as();
973     }
974    
975     bool MainWindow::file_save_as()
976     {
977     Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE);
978 schoenebeck 1225 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
979     dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
980 persson 1261 dialog.set_default_response(Gtk::RESPONSE_OK);
981 persson 2151 dialog.set_do_overwrite_confirmation();
982 persson 1261
983 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
984 schoenebeck 1225 Gtk::FileFilter filter;
985     filter.add_pattern("*.gig");
986 persson 2169 #else
987     Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
988     filter->add_pattern("*.gig");
989     #endif
990 schoenebeck 1225 dialog.set_filter(filter);
991 persson 1261
992 schoenebeck 1679 // set initial dir and filename of the Save As dialog
993     // and prepare that initial filename as a copy of the gig
994     {
995     std::string basename = Glib::path_get_basename(filename);
996     std::string dir = Glib::path_get_dirname(filename);
997 persson 1831 basename = std::string(_("copy_of_")) + basename;
998 schoenebeck 1679 Glib::ustring copyFileName = Glib::build_filename(dir, basename);
999     if (Glib::path_is_absolute(filename)) {
1000     dialog.set_filename(copyFileName);
1001     } else {
1002 persson 1725 if (current_gig_dir != "") dialog.set_current_folder(current_gig_dir);
1003 schoenebeck 1679 }
1004     dialog.set_current_name(Glib::filename_display_basename(copyFileName));
1005 persson 1261 }
1006    
1007 schoenebeck 1679 // show warning in the dialog
1008     Gtk::HBox descriptionArea;
1009     descriptionArea.set_spacing(15);
1010     Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
1011     descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
1012 persson 2344 #if GTKMM_MAJOR_VERSION < 3
1013 persson 1799 view::WrapLabel description;
1014 persson 2344 #else
1015     Gtk::Label description;
1016     description.set_line_wrap();
1017     #endif
1018 schoenebeck 1679 description.set_markup(
1019     _("\n<b>CAUTION:</b> You <b>MUST</b> use the "
1020     "<span style=\"italic\">\"Save\"</span> dialog instead of "
1021     "<span style=\"italic\">\"Save As...\"</span> if you want to save "
1022     "to the same .gig file. Using "
1023     "<span style=\"italic\">\"Save As...\"</span> for writing to the "
1024     "same .gig file will end up in corrupted sample wave data!\n")
1025     );
1026 persson 1799 descriptionArea.pack_start(description);
1027 schoenebeck 1679 dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
1028 persson 1799 descriptionArea.show_all();
1029 schoenebeck 1679
1030 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
1031 schoenebeck 1322 file_structure_to_be_changed_signal.emit(this->file);
1032 schoenebeck 1225 try {
1033 persson 1261 std::string filename = dialog.get_filename();
1034     if (!Glib::str_has_suffix(filename, ".gig")) {
1035     filename += ".gig";
1036     }
1037     printf("filename=%s\n", filename.c_str());
1038     file->Save(filename);
1039     this->filename = filename;
1040 persson 1725 current_gig_dir = Glib::path_get_dirname(filename);
1041 persson 1261 set_title(Glib::filename_display_basename(filename));
1042     file_has_name = true;
1043     file_is_changed = false;
1044 schoenebeck 1225 } catch (RIFF::Exception e) {
1045 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
1046 schoenebeck 1382 Glib::ustring txt = _("Could not save file: ") + e.Message;
1047 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1048     msg.run();
1049 persson 1261 return false;
1050 schoenebeck 1225 }
1051     __import_queued_samples();
1052 schoenebeck 1322 file_structure_changed_signal.emit(this->file);
1053 persson 1261 return true;
1054 schoenebeck 1225 }
1055 persson 1261 return false;
1056 schoenebeck 1225 }
1057    
1058     // actually write the sample(s)' data to the gig file
1059     void MainWindow::__import_queued_samples() {
1060     std::cout << "Starting sample import\n" << std::flush;
1061     Glib::ustring error_files;
1062     printf("Samples to import: %d\n", m_SampleImportQueue.size());
1063     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
1064     iter != m_SampleImportQueue.end(); ) {
1065     printf("Importing sample %s\n",(*iter).sample_path.c_str());
1066     SF_INFO info;
1067     info.format = 0;
1068     SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
1069 persson 2398 sf_command(hFile, SFC_SET_SCALE_FLOAT_INT_READ, 0, SF_TRUE);
1070 schoenebeck 1225 try {
1071 persson 1831 if (!hFile) throw std::string(_("could not open file"));
1072 schoenebeck 1225 // determine sample's bit depth
1073     int bitdepth;
1074     switch (info.format & 0xff) {
1075     case SF_FORMAT_PCM_S8:
1076     case SF_FORMAT_PCM_16:
1077 persson 1265 case SF_FORMAT_PCM_U8:
1078 schoenebeck 1225 bitdepth = 16;
1079     break;
1080     case SF_FORMAT_PCM_24:
1081     case SF_FORMAT_PCM_32:
1082     case SF_FORMAT_FLOAT:
1083     case SF_FORMAT_DOUBLE:
1084 persson 1265 bitdepth = 24;
1085 schoenebeck 1225 break;
1086     default:
1087     sf_close(hFile); // close sound file
1088 persson 1831 throw std::string(_("format not supported")); // unsupported subformat (yet?)
1089 schoenebeck 1225 }
1090 persson 1265
1091     const int bufsize = 10000;
1092 schoenebeck 1225 switch (bitdepth) {
1093 persson 1265 case 16: {
1094     short* buffer = new short[bufsize * info.channels];
1095     sf_count_t cnt = info.frames;
1096     while (cnt) {
1097     // libsndfile does the conversion for us (if needed)
1098     int n = sf_readf_short(hFile, buffer, bufsize);
1099     // write from buffer directly (physically) into .gig file
1100     iter->gig_sample->Write(buffer, n);
1101     cnt -= n;
1102     }
1103     delete[] buffer;
1104 schoenebeck 1225 break;
1105 persson 1265 }
1106     case 24: {
1107     int* srcbuf = new int[bufsize * info.channels];
1108     uint8_t* dstbuf = new uint8_t[bufsize * 3 * info.channels];
1109     sf_count_t cnt = info.frames;
1110     while (cnt) {
1111     // libsndfile returns 32 bits, convert to 24
1112     int n = sf_readf_int(hFile, srcbuf, bufsize);
1113     int j = 0;
1114     for (int i = 0 ; i < n * info.channels ; i++) {
1115     dstbuf[j++] = srcbuf[i] >> 8;
1116     dstbuf[j++] = srcbuf[i] >> 16;
1117     dstbuf[j++] = srcbuf[i] >> 24;
1118     }
1119     // write from buffer directly (physically) into .gig file
1120     iter->gig_sample->Write(dstbuf, n);
1121     cnt -= n;
1122     }
1123     delete[] srcbuf;
1124     delete[] dstbuf;
1125 schoenebeck 1225 break;
1126 persson 1265 }
1127 schoenebeck 1225 }
1128     // cleanup
1129     sf_close(hFile);
1130 schoenebeck 1853 // let the sampler re-cache the sample if needed
1131     sample_changed_signal.emit(iter->gig_sample);
1132 schoenebeck 1225 // on success we remove the sample from the import queue,
1133     // otherwise keep it, maybe it works the next time ?
1134     std::list<SampleImportItem>::iterator cur = iter;
1135     ++iter;
1136     m_SampleImportQueue.erase(cur);
1137     } catch (std::string what) {
1138     // remember the files that made trouble (and their cause)
1139 persson 2442 if (!error_files.empty()) error_files += "\n";
1140 schoenebeck 1225 error_files += (*iter).sample_path += " (" + what + ")";
1141     ++iter;
1142     }
1143     }
1144     // show error message box when some sample(s) could not be imported
1145 persson 2442 if (!error_files.empty()) {
1146 schoenebeck 1382 Glib::ustring txt = _("Could not import the following sample(s):\n") + error_files;
1147 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1148     msg.run();
1149     }
1150     }
1151    
1152     void MainWindow::on_action_file_properties()
1153     {
1154     propDialog.show();
1155     propDialog.deiconify();
1156     }
1157    
1158 schoenebeck 2541 void MainWindow::on_action_warn_user_on_extensions() {
1159     Settings::singleton()->warnUserOnExtensions =
1160     !Settings::singleton()->warnUserOnExtensions;
1161     }
1162    
1163 schoenebeck 1225 void MainWindow::on_action_help_about()
1164     {
1165     Gtk::AboutDialog dialog;
1166 persson 1959 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 12) || GTKMM_MAJOR_VERSION > 2
1167     dialog.set_program_name("Gigedit");
1168     #else
1169     dialog.set_name("Gigedit");
1170     #endif
1171 schoenebeck 1225 dialog.set_version(VERSION);
1172 persson 2507 dialog.set_copyright("Copyright (C) 2006-2014 Andreas Persson");
1173 schoenebeck 2476 const std::string sComment =
1174     _("Built " __DATE__ "\nUsing ") +
1175     ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +
1176     _(
1177     "Gigedit is released under the GNU General Public License.\n"
1178     "\n"
1179     "Please notice that this is still a very young instrument editor. "
1180     "So better backup your Gigasampler files before editing them with "
1181     "this application.\n"
1182     "\n"
1183     "Please report bugs to: http://bugs.linuxsampler.org"
1184     );
1185     dialog.set_comments(sComment.c_str());
1186 schoenebeck 1436 dialog.set_website("http://www.linuxsampler.org");
1187     dialog.set_website_label("http://www.linuxsampler.org");
1188 schoenebeck 1225 dialog.run();
1189     }
1190    
1191     PropDialog::PropDialog()
1192 schoenebeck 2560 : eFileFormat(_("File Format")),
1193     eName(_("Name")),
1194 persson 1831 eCreationDate(_("Creation date")),
1195     eComments(_("Comments")),
1196     eProduct(_("Product")),
1197     eCopyright(_("Copyright")),
1198     eArtists(_("Artists")),
1199     eGenre(_("Genre")),
1200     eKeywords(_("Keywords")),
1201     eEngineer(_("Engineer")),
1202     eTechnician(_("Technician")),
1203     eSoftware(_("Software")),
1204     eMedium(_("Medium")),
1205     eSource(_("Source")),
1206     eSourceForm(_("Source form")),
1207     eCommissioned(_("Commissioned")),
1208     eSubject(_("Subject")),
1209 persson 1582 quitButton(Gtk::Stock::CLOSE),
1210 schoenebeck 2560 table(2, 1),
1211     m_file(NULL)
1212 schoenebeck 1225 {
1213 persson 1831 set_title(_("File Properties"));
1214 persson 1582 eName.set_width_chars(50);
1215    
1216     connect(eName, &DLS::Info::Name);
1217     connect(eCreationDate, &DLS::Info::CreationDate);
1218     connect(eComments, &DLS::Info::Comments);
1219     connect(eProduct, &DLS::Info::Product);
1220     connect(eCopyright, &DLS::Info::Copyright);
1221     connect(eArtists, &DLS::Info::Artists);
1222     connect(eGenre, &DLS::Info::Genre);
1223     connect(eKeywords, &DLS::Info::Keywords);
1224     connect(eEngineer, &DLS::Info::Engineer);
1225     connect(eTechnician, &DLS::Info::Technician);
1226     connect(eSoftware, &DLS::Info::Software);
1227     connect(eMedium, &DLS::Info::Medium);
1228     connect(eSource, &DLS::Info::Source);
1229     connect(eSourceForm, &DLS::Info::SourceForm);
1230     connect(eCommissioned, &DLS::Info::Commissioned);
1231     connect(eSubject, &DLS::Info::Subject);
1232    
1233 schoenebeck 2560 table.add(eFileFormat);
1234 persson 1582 table.add(eName);
1235     table.add(eCreationDate);
1236     table.add(eComments);
1237     table.add(eProduct);
1238     table.add(eCopyright);
1239     table.add(eArtists);
1240     table.add(eGenre);
1241     table.add(eKeywords);
1242     table.add(eEngineer);
1243     table.add(eTechnician);
1244     table.add(eSoftware);
1245     table.add(eMedium);
1246     table.add(eSource);
1247     table.add(eSourceForm);
1248     table.add(eCommissioned);
1249     table.add(eSubject);
1250    
1251 schoenebeck 1225 table.set_col_spacings(5);
1252 persson 1582 add(vbox);
1253     table.set_border_width(5);
1254     vbox.add(table);
1255     vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
1256     buttonBox.set_layout(Gtk::BUTTONBOX_END);
1257     buttonBox.set_border_width(5);
1258     buttonBox.show();
1259     buttonBox.pack_start(quitButton);
1260 persson 2151 quitButton.set_can_default();
1261 persson 1582 quitButton.grab_focus();
1262     quitButton.signal_clicked().connect(
1263     sigc::mem_fun(*this, &PropDialog::hide));
1264 schoenebeck 2560 eFileFormat.signal_value_changed().connect(
1265     sigc::mem_fun(*this, &PropDialog::onFileFormatChanged));
1266 schoenebeck 1225
1267 persson 1582 quitButton.show();
1268     vbox.show();
1269 schoenebeck 1225 show_all_children();
1270     }
1271    
1272 schoenebeck 2560 void PropDialog::set_file(gig::File* file)
1273     {
1274     m_file = file;
1275    
1276     // update file format version combo box
1277     const std::string sGiga = "Gigasampler/GigaStudio v";
1278     const int major = file->pVersion->major;
1279     std::vector<std::string> txts;
1280     std::vector<int> values;
1281     txts.push_back(sGiga + "2"); values.push_back(2);
1282 schoenebeck 2566 txts.push_back(sGiga + "3/v4"); values.push_back(3);
1283 schoenebeck 2560 if (major != 2 && major != 3) {
1284     txts.push_back(sGiga + ToString(major)); values.push_back(major);
1285     }
1286     std::vector<const char*> texts;
1287     for (int i = 0; i < txts.size(); ++i) texts.push_back(txts[i].c_str());
1288 persson 2579 texts.push_back(NULL); values.push_back(0);
1289 schoenebeck 2560 eFileFormat.set_choices(&texts[0], &values[0]);
1290     eFileFormat.set_value(major);
1291     }
1292    
1293     void PropDialog::onFileFormatChanged() {
1294     const int major = eFileFormat.get_value();
1295     if (m_file) m_file->pVersion->major = major;
1296     }
1297    
1298 schoenebeck 1225 void PropDialog::set_info(DLS::Info* info)
1299     {
1300 persson 2423 update(info);
1301 schoenebeck 1225 }
1302    
1303 persson 1582
1304 persson 2445 void InstrumentProps::set_Name(const gig::String& name)
1305     {
1306     m->pInfo->Name = name;
1307     }
1308    
1309     void InstrumentProps::update_name()
1310     {
1311     update_model++;
1312     eName.set_value(m->pInfo->Name);
1313     update_model--;
1314     }
1315    
1316 persson 1460 void InstrumentProps::set_IsDrum(bool value)
1317     {
1318 persson 2423 m->IsDrum = value;
1319 persson 1460 }
1320    
1321     void InstrumentProps::set_MIDIBank(uint16_t value)
1322     {
1323 persson 2423 m->MIDIBank = value;
1324 persson 1460 }
1325    
1326     void InstrumentProps::set_MIDIProgram(uint32_t value)
1327     {
1328 persson 2423 m->MIDIProgram = value;
1329 persson 1460 }
1330    
1331 persson 2423 InstrumentProps::InstrumentProps() :
1332     quitButton(Gtk::Stock::CLOSE),
1333     table(2,1),
1334     eName(_("Name")),
1335     eIsDrum(_("Is drum")),
1336     eMIDIBank(_("MIDI bank"), 0, 16383),
1337     eMIDIProgram(_("MIDI program")),
1338     eAttenuation(_("Attenuation"), 0, 96, 0, 1),
1339     eGainPlus6(_("Gain +6dB"), eAttenuation, -6),
1340     eEffectSend(_("Effect send"), 0, 65535),
1341     eFineTune(_("Fine tune"), -8400, 8400),
1342     ePitchbendRange(_("Pitchbend range"), 0, 12),
1343     ePianoReleaseMode(_("Piano release mode")),
1344     eDimensionKeyRangeLow(_("Keyswitching range low")),
1345     eDimensionKeyRangeHigh(_("Keyswitching range high"))
1346 persson 1460 {
1347 persson 1831 set_title(_("Instrument Properties"));
1348 schoenebeck 1225
1349 schoenebeck 1656 eDimensionKeyRangeLow.set_tip(
1350     _("start of the keyboard area which should switch the "
1351     "\"keyswitching\" dimension")
1352     );
1353     eDimensionKeyRangeHigh.set_tip(
1354     _("end of the keyboard area which should switch the "
1355     "\"keyswitching\" dimension")
1356     );
1357    
1358 persson 2445 connect(eName, &InstrumentProps::set_Name);
1359 persson 1460 connect(eIsDrum, &InstrumentProps::set_IsDrum);
1360     connect(eMIDIBank, &InstrumentProps::set_MIDIBank);
1361     connect(eMIDIProgram, &InstrumentProps::set_MIDIProgram);
1362     connect(eAttenuation, &gig::Instrument::Attenuation);
1363     connect(eGainPlus6, &gig::Instrument::Attenuation);
1364     connect(eEffectSend, &gig::Instrument::EffectSend);
1365     connect(eFineTune, &gig::Instrument::FineTune);
1366     connect(ePitchbendRange, &gig::Instrument::PitchbendRange);
1367     connect(ePianoReleaseMode, &gig::Instrument::PianoReleaseMode);
1368 persson 2423 connect(eDimensionKeyRangeLow, eDimensionKeyRangeHigh,
1369     &gig::Instrument::DimensionKeyRange);
1370 persson 1460
1371 persson 2445 eName.signal_value_changed().connect(sig_name_changed.make_slot());
1372    
1373 schoenebeck 1225 table.set_col_spacings(5);
1374    
1375 persson 1582 table.add(eName);
1376     table.add(eIsDrum);
1377     table.add(eMIDIBank);
1378     table.add(eMIDIProgram);
1379     table.add(eAttenuation);
1380     table.add(eGainPlus6);
1381     table.add(eEffectSend);
1382     table.add(eFineTune);
1383     table.add(ePitchbendRange);
1384     table.add(ePianoReleaseMode);
1385     table.add(eDimensionKeyRangeLow);
1386     table.add(eDimensionKeyRangeHigh);
1387 schoenebeck 1225
1388     add(vbox);
1389     table.set_border_width(5);
1390     vbox.pack_start(table);
1391     table.show();
1392     vbox.pack_start(buttonBox, Gtk::PACK_SHRINK);
1393     buttonBox.set_layout(Gtk::BUTTONBOX_END);
1394     buttonBox.set_border_width(5);
1395     buttonBox.show();
1396     buttonBox.pack_start(quitButton);
1397 persson 2151 quitButton.set_can_default();
1398 schoenebeck 1225 quitButton.grab_focus();
1399    
1400     quitButton.signal_clicked().connect(
1401     sigc::mem_fun(*this, &InstrumentProps::hide));
1402    
1403     quitButton.show();
1404     vbox.show();
1405     show_all_children();
1406     }
1407    
1408     void InstrumentProps::set_instrument(gig::Instrument* instrument)
1409     {
1410 persson 2423 update(instrument);
1411 persson 1460
1412     update_model++;
1413 persson 2445 eName.set_value(instrument->pInfo->Name);
1414 persson 1460 eIsDrum.set_value(instrument->IsDrum);
1415     eMIDIBank.set_value(instrument->MIDIBank);
1416     eMIDIProgram.set_value(instrument->MIDIProgram);
1417     update_model--;
1418 schoenebeck 1225 }
1419    
1420    
1421 persson 1261 void MainWindow::file_changed()
1422     {
1423     if (file && !file_is_changed) {
1424     set_title("*" + get_title());
1425     file_is_changed = true;
1426 schoenebeck 1225 }
1427 persson 1261 }
1428 schoenebeck 1225
1429 schoenebeck 2621 void MainWindow::updateSampleRefCountMap(gig::File* gig) {
1430     sample_ref_count.clear();
1431    
1432     if (!gig) return;
1433    
1434     for (gig::Instrument* instrument = gig->GetFirstInstrument(); instrument;
1435     instrument = gig->GetNextInstrument())
1436     {
1437     for (gig::Region* rgn = instrument->GetFirstRegion(); rgn;
1438     rgn = instrument->GetNextRegion())
1439     {
1440     for (int i = 0; i < 256; ++i) {
1441     if (!rgn->pDimensionRegions[i]) continue;
1442     if (rgn->pDimensionRegions[i]->pSample) {
1443     sample_ref_count[rgn->pDimensionRegions[i]->pSample]++;
1444     }
1445     }
1446     }
1447     }
1448     }
1449    
1450 schoenebeck 1382 void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument)
1451 persson 1261 {
1452     file = 0;
1453 schoenebeck 1411 set_file_is_shared(isSharedInstrument);
1454 persson 1261
1455     this->filename = filename ? filename : _("Unsaved Gig File");
1456     set_title(Glib::filename_display_basename(this->filename));
1457     file_has_name = filename;
1458     file_is_changed = false;
1459    
1460 schoenebeck 2560 propDialog.set_file(gig);
1461 schoenebeck 1225 propDialog.set_info(gig->pInfo);
1462    
1463 persson 2442 instrument_name_connection.block();
1464 schoenebeck 1225 for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
1465     instrument = gig->GetNextInstrument()) {
1466 persson 2446 Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
1467 persson 2442
1468 schoenebeck 1225 Gtk::TreeModel::iterator iter = m_refTreeModel->append();
1469     Gtk::TreeModel::Row row = *iter;
1470 persson 2442 row[m_Columns.m_col_name] = name;
1471 schoenebeck 1225 row[m_Columns.m_col_instr] = instrument;
1472 persson 2442
1473     add_instrument_to_menu(name);
1474 schoenebeck 1225 }
1475 persson 2442 instrument_name_connection.unblock();
1476 schoenebeck 2625 uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments")->show();
1477 schoenebeck 1225
1478 schoenebeck 2621 updateSampleRefCountMap(gig);
1479    
1480 schoenebeck 1225 for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
1481     if (group->Name != "") {
1482     Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
1483     Gtk::TreeModel::Row rowGroup = *iterGroup;
1484 persson 2446 rowGroup[m_SamplesModel.m_col_name] = gig_to_utf8(group->Name);
1485 schoenebeck 1225 rowGroup[m_SamplesModel.m_col_group] = group;
1486     rowGroup[m_SamplesModel.m_col_sample] = NULL;
1487     for (gig::Sample* sample = group->GetFirstSample();
1488     sample; sample = group->GetNextSample()) {
1489     Gtk::TreeModel::iterator iterSample =
1490     m_refSamplesTreeModel->append(rowGroup.children());
1491     Gtk::TreeModel::Row rowSample = *iterSample;
1492 persson 2446 rowSample[m_SamplesModel.m_col_name] =
1493     gig_to_utf8(sample->pInfo->Name);
1494 schoenebeck 1225 rowSample[m_SamplesModel.m_col_sample] = sample;
1495     rowSample[m_SamplesModel.m_col_group] = NULL;
1496 schoenebeck 2621 int refcount = sample_ref_count.count(sample) ? sample_ref_count[sample] : 0;
1497     rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
1498 schoenebeck 2625 rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
1499 schoenebeck 1225 }
1500     }
1501     }
1502 schoenebeck 2604
1503     for (int i = 0; gig->GetScriptGroup(i); ++i) {
1504     gig::ScriptGroup* group = gig->GetScriptGroup(i);
1505 schoenebeck 1225
1506 schoenebeck 2604 Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
1507     Gtk::TreeModel::Row rowGroup = *iterGroup;
1508     rowGroup[m_ScriptsModel.m_col_name] = gig_to_utf8(group->Name);
1509     rowGroup[m_ScriptsModel.m_col_group] = group;
1510     rowGroup[m_ScriptsModel.m_col_script] = NULL;
1511     for (int s = 0; group->GetScript(s); ++s) {
1512     gig::Script* script = group->GetScript(s);
1513    
1514     Gtk::TreeModel::iterator iterScript =
1515     m_refScriptsTreeModel->append(rowGroup.children());
1516     Gtk::TreeModel::Row rowScript = *iterScript;
1517     rowScript[m_ScriptsModel.m_col_name] = gig_to_utf8(script->Name);
1518     rowScript[m_ScriptsModel.m_col_script] = script;
1519     rowScript[m_ScriptsModel.m_col_group] = NULL;
1520     }
1521     }
1522 schoenebeck 2624 // unfold all sample groups & script groups by default
1523     m_TreeViewSamples.expand_all();
1524 schoenebeck 2604 m_TreeViewScripts.expand_all();
1525    
1526 persson 1261 file = gig;
1527    
1528 schoenebeck 1225 // select the first instrument
1529 persson 2442 m_TreeView.get_selection()->select(Gtk::TreePath("0"));
1530 persson 2423
1531 persson 2445 instr_props_set_instrument();
1532 persson 2507 gig::Instrument* instrument = get_instrument();
1533     if (instrument) {
1534     midiRules.set_instrument(instrument);
1535     }
1536 persson 2445 }
1537    
1538     bool MainWindow::instr_props_set_instrument()
1539     {
1540     instrumentProps.signal_name_changed().clear();
1541    
1542     Gtk::TreeModel::const_iterator it =
1543     m_TreeView.get_selection()->get_selected();
1544     if (it) {
1545     Gtk::TreeModel::Row row = *it;
1546     gig::Instrument* instrument = row[m_Columns.m_col_instr];
1547    
1548 persson 2423 instrumentProps.set_instrument(instrument);
1549 persson 2445
1550     // make sure instrument tree is updated when user changes the
1551     // instrument name in instrument properties window
1552     instrumentProps.signal_name_changed().connect(
1553     sigc::bind(
1554     sigc::mem_fun(*this,
1555     &MainWindow::instr_name_changed_by_instr_props),
1556     it));
1557     } else {
1558     instrumentProps.hide();
1559 persson 2423 }
1560 persson 2445 return it;
1561 schoenebeck 1225 }
1562    
1563     void MainWindow::show_instr_props()
1564     {
1565 persson 2445 if (instr_props_set_instrument()) {
1566 persson 1533 instrumentProps.show();
1567     instrumentProps.deiconify();
1568 schoenebeck 1225 }
1569     }
1570    
1571 persson 2445 void MainWindow::instr_name_changed_by_instr_props(Gtk::TreeModel::iterator& it)
1572     {
1573     Gtk::TreeModel::Row row = *it;
1574     Glib::ustring name = row[m_Columns.m_col_name];
1575    
1576     gig::Instrument* instrument = row[m_Columns.m_col_instr];
1577 persson 2446 Glib::ustring gigname(gig_to_utf8(instrument->pInfo->Name));
1578     if (gigname != name) {
1579     row[m_Columns.m_col_name] = gigname;
1580 persson 2445 }
1581     }
1582    
1583 persson 2507 void MainWindow::show_midi_rules()
1584     {
1585     if (gig::Instrument* instrument = get_instrument())
1586     {
1587     midiRules.set_instrument(instrument);
1588     midiRules.show();
1589     midiRules.deiconify();
1590     }
1591     }
1592    
1593 schoenebeck 2610 void MainWindow::show_script_slots() {
1594     if (!file) return;
1595     // get selected instrument
1596     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1597     Gtk::TreeModel::iterator it = sel->get_selected();
1598     if (!it) return;
1599     Gtk::TreeModel::Row row = *it;
1600     gig::Instrument* instrument = row[m_Columns.m_col_instr];
1601     if (!instrument) return;
1602    
1603     ScriptSlots* window = new ScriptSlots;
1604     window->setInstrument(instrument);
1605     //window->reparent(*this);
1606     window->show();
1607     }
1608    
1609 schoenebeck 1415 void MainWindow::on_action_view_status_bar() {
1610     Gtk::CheckMenuItem* item =
1611     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuView/Statusbar"));
1612     if (!item) {
1613     std::cerr << "/MenuBar/MenuView/Statusbar == NULL\n";
1614     return;
1615     }
1616     if (item->get_active()) m_StatusBar.show();
1617     else m_StatusBar.hide();
1618     }
1619    
1620 schoenebeck 2464 bool MainWindow::is_copy_samples_unity_note_enabled() const {
1621     Gtk::CheckMenuItem* item =
1622     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity"));
1623     if (!item) {
1624     std::cerr << "/MenuBar/MenuEdit/CopySampleUnity == NULL\n";
1625     return true;
1626     }
1627     return item->get_active();
1628     }
1629    
1630     bool MainWindow::is_copy_samples_fine_tune_enabled() const {
1631     Gtk::CheckMenuItem* item =
1632     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleTune"));
1633     if (!item) {
1634     std::cerr << "/MenuBar/MenuEdit/CopySampleTune == NULL\n";
1635     return true;
1636     }
1637     return item->get_active();
1638     }
1639    
1640     bool MainWindow::is_copy_samples_loop_enabled() const {
1641     Gtk::CheckMenuItem* item =
1642     dynamic_cast<Gtk::CheckMenuItem*>(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleLoop"));
1643     if (!item) {
1644     std::cerr << "/MenuBar/MenuEdit/CopySampleLoop == NULL\n";
1645     return true;
1646     }
1647     return item->get_active();
1648     }
1649    
1650 schoenebeck 1225 void MainWindow::on_button_release(GdkEventButton* button)
1651     {
1652     if (button->type == GDK_2BUTTON_PRESS) {
1653     show_instr_props();
1654     } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1655 persson 2507 // gig v2 files have no midi rules
1656 schoenebeck 2625 const bool bEnabled = !(file->pVersion && file->pVersion->major == 2);
1657 persson 2507 static_cast<Gtk::MenuItem*>(
1658 schoenebeck 2625 uiManager->get_widget("/MenuBar/MenuInstrument/MidiRules"))->set_sensitive(
1659     bEnabled
1660     );
1661     static_cast<Gtk::MenuItem*>(
1662 persson 2507 uiManager->get_widget("/PopupMenu/MidiRules"))->set_sensitive(
1663 schoenebeck 2625 bEnabled
1664     );
1665 schoenebeck 1225 popup_menu->popup(button->button, button->time);
1666     }
1667     }
1668    
1669 persson 2442 void MainWindow::on_instrument_selection_change(Gtk::RadioMenuItem* item) {
1670     if (item->get_active()) {
1671     const std::vector<Gtk::Widget*> children =
1672     instrument_menu->get_children();
1673     std::vector<Gtk::Widget*>::const_iterator it =
1674     find(children.begin(), children.end(), item);
1675     if (it != children.end()) {
1676     int index = it - children.begin();
1677     m_TreeView.get_selection()->select(Gtk::TreePath(ToString(index)));
1678    
1679     m_RegionChooser.set_instrument(file->GetInstrument(index));
1680     }
1681     }
1682 schoenebeck 1225 }
1683    
1684     void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
1685     if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1686     Gtk::Menu* sample_popup =
1687     dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
1688     // update enabled/disabled state of sample popup items
1689     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1690     Gtk::TreeModel::iterator it = sel->get_selected();
1691     bool group_selected = false;
1692     bool sample_selected = false;
1693     if (it) {
1694     Gtk::TreeModel::Row row = *it;
1695     group_selected = row[m_SamplesModel.m_col_group];
1696     sample_selected = row[m_SamplesModel.m_col_sample];
1697     }
1698 schoenebeck 2625
1699    
1700 schoenebeck 1225 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
1701     set_sensitive(group_selected || sample_selected);
1702     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
1703     set_sensitive(group_selected || sample_selected);
1704     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
1705     set_sensitive(file);
1706 schoenebeck 2624 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/ShowSampleRefs"))->
1707     set_sensitive(sample_selected);
1708 schoenebeck 1225 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
1709     set_sensitive(group_selected || sample_selected);
1710     // show sample popup
1711     sample_popup->popup(button->button, button->time);
1712 schoenebeck 2625
1713     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/SampleProperties"))->
1714     set_sensitive(group_selected || sample_selected);
1715     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddSample"))->
1716     set_sensitive(group_selected || sample_selected);
1717     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/AddGroup"))->
1718     set_sensitive(file);
1719     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/ShowSampleRefs"))->
1720     set_sensitive(sample_selected);
1721     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuSample/RemoveSample"))->
1722     set_sensitive(group_selected || sample_selected);
1723 schoenebeck 1225 }
1724     }
1725    
1726 schoenebeck 2604 void MainWindow::on_script_treeview_button_release(GdkEventButton* button) {
1727     if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1728     Gtk::Menu* script_popup =
1729     dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/ScriptPopupMenu"));
1730     // update enabled/disabled state of sample popup items
1731     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
1732     Gtk::TreeModel::iterator it = sel->get_selected();
1733     bool group_selected = false;
1734     bool script_selected = false;
1735     if (it) {
1736     Gtk::TreeModel::Row row = *it;
1737     group_selected = row[m_ScriptsModel.m_col_group];
1738     script_selected = row[m_ScriptsModel.m_col_script];
1739     }
1740     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScript"))->
1741     set_sensitive(group_selected || script_selected);
1742     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/AddScriptGroup"))->
1743     set_sensitive(file);
1744     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/EditScript"))->
1745     set_sensitive(script_selected);
1746     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/ScriptPopupMenu/RemoveScript"))->
1747     set_sensitive(group_selected || script_selected);
1748     // show sample popup
1749     script_popup->popup(button->button, button->time);
1750 schoenebeck 2625
1751     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScript"))->
1752     set_sensitive(group_selected || script_selected);
1753     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/AddScriptGroup"))->
1754     set_sensitive(file);
1755     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/EditScript"))->
1756     set_sensitive(script_selected);
1757     dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuScript/RemoveScript"))->
1758     set_sensitive(group_selected || script_selected);
1759 schoenebeck 2604 }
1760     }
1761 persson 2442
1762     Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu(
1763     const Glib::ustring& name, int position) {
1764    
1765     Gtk::RadioMenuItem::Group instrument_group;
1766     const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
1767     if (!children.empty()) {
1768     instrument_group =
1769     static_cast<Gtk::RadioMenuItem*>(children[0])->get_group();
1770     }
1771     Gtk::RadioMenuItem* item =
1772     new Gtk::RadioMenuItem(instrument_group, name);
1773     if (position < 0) {
1774     instrument_menu->append(*item);
1775     } else {
1776     instrument_menu->insert(*item, position);
1777     }
1778     item->show();
1779     item->signal_activate().connect(
1780     sigc::bind(
1781     sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
1782     item));
1783     return item;
1784     }
1785    
1786     void MainWindow::remove_instrument_from_menu(int index) {
1787     const std::vector<Gtk::Widget*> children =
1788     instrument_menu->get_children();
1789     Gtk::Widget* child = children[index];
1790     instrument_menu->remove(*child);
1791     delete child;
1792     }
1793    
1794     void MainWindow::add_instrument(gig::Instrument* instrument) {
1795 persson 2446 const Glib::ustring name(gig_to_utf8(instrument->pInfo->Name));
1796 persson 2442
1797     // update instrument tree view
1798     instrument_name_connection.block();
1799     Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append();
1800     Gtk::TreeModel::Row rowInstr = *iterInstr;
1801     rowInstr[m_Columns.m_col_name] = name;
1802     rowInstr[m_Columns.m_col_instr] = instrument;
1803     instrument_name_connection.unblock();
1804    
1805     add_instrument_to_menu(name);
1806    
1807     m_TreeView.get_selection()->select(iterInstr);
1808    
1809     file_changed();
1810     }
1811    
1812 schoenebeck 1225 void MainWindow::on_action_add_instrument() {
1813     static int __instrument_indexer = 0;
1814     if (!file) return;
1815     gig::Instrument* instrument = file->AddInstrument();
1816     __instrument_indexer++;
1817 persson 2446 instrument->pInfo->Name = gig_from_utf8(_("Unnamed Instrument ") +
1818     ToString(__instrument_indexer));
1819 persson 2442
1820     add_instrument(instrument);
1821 schoenebeck 1225 }
1822    
1823 schoenebeck 2395 void MainWindow::on_action_duplicate_instrument() {
1824     if (!file) return;
1825 persson 2442
1826 schoenebeck 2395 // retrieve the currently selected instrument
1827     // (being the original instrument to be duplicated)
1828     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1829     Gtk::TreeModel::iterator itSelection = sel->get_selected();
1830     if (!itSelection) return;
1831     Gtk::TreeModel::Row row = *itSelection;
1832     gig::Instrument* instrOrig = row[m_Columns.m_col_instr];
1833     if (!instrOrig) return;
1834 persson 2442
1835 schoenebeck 2395 // duplicate the orginal instrument
1836     gig::Instrument* instrNew = file->AddDuplicateInstrument(instrOrig);
1837     instrNew->pInfo->Name =
1838 persson 2446 instrOrig->pInfo->Name +
1839     gig_from_utf8(Glib::ustring(" (") + _("Copy") + ")");
1840 persson 2442
1841     add_instrument(instrNew);
1842 schoenebeck 2395 }
1843    
1844 schoenebeck 1225 void MainWindow::on_action_remove_instrument() {
1845     if (!file) return;
1846 schoenebeck 1382 if (file_is_shared) {
1847     Gtk::MessageDialog msg(
1848     *this,
1849     _("You cannot delete an instrument from this file, since it's "
1850     "currently used by the sampler."),
1851     false, Gtk::MESSAGE_INFO
1852     );
1853     msg.run();
1854     return;
1855     }
1856    
1857 schoenebeck 1225 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection();
1858     Gtk::TreeModel::iterator it = sel->get_selected();
1859     if (it) {
1860     Gtk::TreeModel::Row row = *it;
1861     gig::Instrument* instr = row[m_Columns.m_col_instr];
1862     try {
1863 persson 2442 Gtk::TreePath path(it);
1864     int index = path[0];
1865    
1866 schoenebeck 1225 // remove instrument from the gig file
1867     if (instr) file->DeleteInstrument(instr);
1868 persson 1261 file_changed();
1869 persson 2423
1870 persson 2442 remove_instrument_from_menu(index);
1871    
1872     // remove row from instruments tree view
1873     m_refTreeModel->erase(it);
1874    
1875     #if GTKMM_MAJOR_VERSION < 3
1876     // select another instrument (in gtk3 this is done
1877     // automatically)
1878     if (!m_refTreeModel->children().empty()) {
1879     if (index == m_refTreeModel->children().size()) {
1880     index--;
1881     }
1882     m_TreeView.get_selection()->select(
1883     Gtk::TreePath(ToString(index)));
1884     }
1885     #endif
1886 persson 2445 instr_props_set_instrument();
1887 persson 2507 instr = get_instrument();
1888     if (instr) {
1889     midiRules.set_instrument(instr);
1890     } else {
1891     midiRules.hide();
1892     }
1893 schoenebeck 1225 } catch (RIFF::Exception e) {
1894     Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
1895     msg.run();
1896     }
1897     }
1898     }
1899    
1900     void MainWindow::on_action_sample_properties() {
1901     //TODO: show a dialog where the selected sample's properties can be edited
1902     Gtk::MessageDialog msg(
1903 persson 1831 *this, _("Sorry, yet to be implemented!"), false, Gtk::MESSAGE_INFO
1904 schoenebeck 1225 );
1905     msg.run();
1906     }
1907    
1908 schoenebeck 2604 void MainWindow::on_action_add_script_group() {
1909     static int __script_indexer = 0;
1910     if (!file) return;
1911     gig::ScriptGroup* group = file->AddScriptGroup();
1912     group->Name = gig_from_utf8(_("Unnamed Group"));
1913     if (__script_indexer) group->Name += " " + ToString(__script_indexer);
1914     __script_indexer++;
1915     // update sample tree view
1916     Gtk::TreeModel::iterator iterGroup = m_refScriptsTreeModel->append();
1917     Gtk::TreeModel::Row rowGroup = *iterGroup;
1918     rowGroup[m_ScriptsModel.m_col_name] = gig_to_utf8(group->Name);
1919     rowGroup[m_ScriptsModel.m_col_script] = NULL;
1920     rowGroup[m_ScriptsModel.m_col_group] = group;
1921     file_changed();
1922     }
1923    
1924     void MainWindow::on_action_add_script() {
1925     if (!file) return;
1926     // get selected group
1927     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
1928     Gtk::TreeModel::iterator it = sel->get_selected();
1929     if (!it) return;
1930     Gtk::TreeModel::Row row = *it;
1931     gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
1932     if (!group) { // not a group, but a script is selected (probably)
1933     gig::Script* script = row[m_ScriptsModel.m_col_script];
1934     if (!script) return;
1935     it = row.parent(); // resolve parent (that is the script's group)
1936     if (!it) return;
1937     row = *it;
1938     group = row[m_ScriptsModel.m_col_group];
1939     if (!group) return;
1940     }
1941    
1942     // add a new script to the .gig file
1943     gig::Script* script = group->AddScript();
1944     Glib::ustring name = _("Unnamed Script");
1945     script->Name = gig_from_utf8(name);
1946    
1947     // add script to the tree view
1948     Gtk::TreeModel::iterator iterScript =
1949     m_refScriptsTreeModel->append(row.children());
1950     Gtk::TreeModel::Row rowScript = *iterScript;
1951     rowScript[m_ScriptsModel.m_col_name] = name;
1952     rowScript[m_ScriptsModel.m_col_script] = script;
1953     rowScript[m_ScriptsModel.m_col_group] = NULL;
1954    
1955     // unfold group of new script item in treeview
1956     Gtk::TreeModel::Path path(iterScript);
1957     m_TreeViewScripts.expand_to_path(path);
1958     }
1959    
1960     void MainWindow::on_action_edit_script() {
1961     if (!file) return;
1962     // get selected script
1963     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
1964     Gtk::TreeModel::iterator it = sel->get_selected();
1965     if (!it) return;
1966     Gtk::TreeModel::Row row = *it;
1967     gig::Script* script = row[m_ScriptsModel.m_col_script];
1968     if (!script) return;
1969    
1970     ScriptEditor* editor = new ScriptEditor;
1971     editor->setScript(script);
1972     //editor->reparent(*this);
1973     editor->show();
1974     }
1975    
1976     void MainWindow::on_action_remove_script() {
1977     if (!file) return;
1978     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
1979     Gtk::TreeModel::iterator it = sel->get_selected();
1980     if (it) {
1981     Gtk::TreeModel::Row row = *it;
1982     gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
1983     gig::Script* script = row[m_ScriptsModel.m_col_script];
1984     Glib::ustring name = row[m_ScriptsModel.m_col_name];
1985     try {
1986     // remove script group or script from the gig file
1987     if (group) {
1988     // notify everybody that we're going to remove these samples
1989     //TODO: scripts_to_be_removed_signal.emit(members);
1990     // delete the group in the .gig file including the
1991     // samples that belong to the group
1992     file->DeleteScriptGroup(group);
1993     // notify that we're done with removal
1994     //TODO: scripts_removed_signal.emit();
1995     file_changed();
1996     } else if (script) {
1997     // notify everybody that we're going to remove this sample
1998     //TODO: std::list<gig::Script*> lscripts;
1999     //TODO: lscripts.push_back(script);
2000     //TODO: scripts_to_be_removed_signal.emit(lscripts);
2001     // remove sample from the .gig file
2002     script->GetGroup()->DeleteScript(script);
2003     // notify that we're done with removal
2004     //TODO: scripts_removed_signal.emit();
2005     dimreg_changed();
2006     file_changed();
2007     }
2008     // remove respective row(s) from samples tree view
2009     m_refScriptsTreeModel->erase(it);
2010     } catch (RIFF::Exception e) {
2011     // pretend we're done with removal (i.e. to avoid dead locks)
2012     //TODO: scripts_removed_signal.emit();
2013     // show error message
2014     Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2015     msg.run();
2016     }
2017     }
2018     }
2019    
2020 schoenebeck 1225 void MainWindow::on_action_add_group() {
2021     static int __sample_indexer = 0;
2022     if (!file) return;
2023     gig::Group* group = file->AddGroup();
2024 persson 2446 group->Name = gig_from_utf8(_("Unnamed Group"));
2025 schoenebeck 1225 if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
2026     __sample_indexer++;
2027     // update sample tree view
2028     Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
2029     Gtk::TreeModel::Row rowGroup = *iterGroup;
2030 persson 2446 rowGroup[m_SamplesModel.m_col_name] = gig_to_utf8(group->Name);
2031 schoenebeck 1225 rowGroup[m_SamplesModel.m_col_sample] = NULL;
2032     rowGroup[m_SamplesModel.m_col_group] = group;
2033 persson 1261 file_changed();
2034 schoenebeck 1225 }
2035    
2036     void MainWindow::on_action_add_sample() {
2037     if (!file) return;
2038     // get selected group
2039     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2040     Gtk::TreeModel::iterator it = sel->get_selected();
2041     if (!it) return;
2042     Gtk::TreeModel::Row row = *it;
2043     gig::Group* group = row[m_SamplesModel.m_col_group];
2044     if (!group) { // not a group, but a sample is selected (probably)
2045     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2046     if (!sample) return;
2047     it = row.parent(); // resolve parent (that is the sample's group)
2048     if (!it) return;
2049     row = *it;
2050     group = row[m_SamplesModel.m_col_group];
2051     if (!group) return;
2052     }
2053     // show 'browse for file' dialog
2054     Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
2055     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2056     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
2057     dialog.set_select_multiple(true);
2058 persson 2169
2059     // matches all file types supported by libsndfile
2060     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2061     Gtk::FileFilter soundfilter;
2062     #else
2063     Glib::RefPtr<Gtk::FileFilter> soundfilter = Gtk::FileFilter::create();
2064     #endif
2065 persson 1262 const char* const supportedFileTypes[] = {
2066 schoenebeck 1225 "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
2067     "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
2068     "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
2069     "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
2070     "*.caf", "*.CAF", NULL
2071     };
2072 persson 2169 const char* soundfiles = _("Sound Files");
2073     const char* allfiles = _("All Files");
2074     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2075 schoenebeck 1225 for (int i = 0; supportedFileTypes[i]; i++)
2076     soundfilter.add_pattern(supportedFileTypes[i]);
2077 persson 2169 soundfilter.set_name(soundfiles);
2078    
2079     // matches every file
2080     Gtk::FileFilter allpassfilter;
2081 schoenebeck 1225 allpassfilter.add_pattern("*.*");
2082 persson 2169 allpassfilter.set_name(allfiles);
2083     #else
2084     for (int i = 0; supportedFileTypes[i]; i++)
2085     soundfilter->add_pattern(supportedFileTypes[i]);
2086     soundfilter->set_name(soundfiles);
2087    
2088     // matches every file
2089     Glib::RefPtr<Gtk::FileFilter> allpassfilter = Gtk::FileFilter::create();
2090     allpassfilter->add_pattern("*.*");
2091     allpassfilter->set_name(allfiles);
2092     #endif
2093 schoenebeck 1225 dialog.add_filter(soundfilter);
2094     dialog.add_filter(allpassfilter);
2095 persson 1725 if (current_sample_dir != "") {
2096     dialog.set_current_folder(current_sample_dir);
2097     }
2098 schoenebeck 1225 if (dialog.run() == Gtk::RESPONSE_OK) {
2099 persson 1725 current_sample_dir = dialog.get_current_folder();
2100 schoenebeck 1225 Glib::ustring error_files;
2101 persson 2169 std::vector<std::string> filenames = dialog.get_filenames();
2102     for (std::vector<std::string>::iterator iter = filenames.begin();
2103 schoenebeck 1225 iter != filenames.end(); ++iter) {
2104     printf("Adding sample %s\n",(*iter).c_str());
2105     // use libsndfile to retrieve file informations
2106     SF_INFO info;
2107     info.format = 0;
2108     SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
2109     try {
2110 persson 1831 if (!hFile) throw std::string(_("could not open file"));
2111 schoenebeck 1225 int bitdepth;
2112     switch (info.format & 0xff) {
2113     case SF_FORMAT_PCM_S8:
2114     case SF_FORMAT_PCM_16:
2115 persson 1265 case SF_FORMAT_PCM_U8:
2116 schoenebeck 1225 bitdepth = 16;
2117     break;
2118     case SF_FORMAT_PCM_24:
2119     case SF_FORMAT_PCM_32:
2120     case SF_FORMAT_FLOAT:
2121     case SF_FORMAT_DOUBLE:
2122 persson 1265 bitdepth = 24;
2123 schoenebeck 1225 break;
2124     default:
2125     sf_close(hFile); // close sound file
2126 persson 1831 throw std::string(_("format not supported")); // unsupported subformat (yet?)
2127 schoenebeck 1225 }
2128     // add a new sample to the .gig file
2129     gig::Sample* sample = file->AddSample();
2130     // file name without path
2131 persson 1262 Glib::ustring filename = Glib::filename_display_basename(*iter);
2132     // remove file extension if there is one
2133     for (int i = 0; supportedFileTypes[i]; i++) {
2134     if (Glib::str_has_suffix(filename, supportedFileTypes[i] + 1)) {
2135     filename.erase(filename.length() - strlen(supportedFileTypes[i] + 1));
2136     break;
2137     }
2138     }
2139 persson 2446 sample->pInfo->Name = gig_from_utf8(filename);
2140 schoenebeck 1225 sample->Channels = info.channels;
2141     sample->BitDepth = bitdepth;
2142     sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
2143     sample->SamplesPerSecond = info.samplerate;
2144 persson 1265 sample->AverageBytesPerSecond = sample->FrameSize * sample->SamplesPerSecond;
2145     sample->BlockAlign = sample->FrameSize;
2146     sample->SamplesTotal = info.frames;
2147    
2148     SF_INSTRUMENT instrument;
2149     if (sf_command(hFile, SFC_GET_INSTRUMENT,
2150     &instrument, sizeof(instrument)) != SF_FALSE)
2151     {
2152     sample->MIDIUnityNote = instrument.basenote;
2153 schoenebeck 2466 sample->FineTune = instrument.detune;
2154 persson 1265
2155     if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) {
2156     sample->Loops = 1;
2157    
2158     switch (instrument.loops[0].mode) {
2159     case SF_LOOP_FORWARD:
2160     sample->LoopType = gig::loop_type_normal;
2161     break;
2162     case SF_LOOP_BACKWARD:
2163     sample->LoopType = gig::loop_type_backward;
2164     break;
2165     case SF_LOOP_ALTERNATING:
2166     sample->LoopType = gig::loop_type_bidirectional;
2167     break;
2168     }
2169     sample->LoopStart = instrument.loops[0].start;
2170     sample->LoopEnd = instrument.loops[0].end;
2171     sample->LoopPlayCount = instrument.loops[0].count;
2172     sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1;
2173     }
2174     }
2175    
2176 schoenebeck 1225 // schedule resizing the sample (which will be done
2177     // physically when File::Save() is called)
2178     sample->Resize(info.frames);
2179     // make sure sample is part of the selected group
2180     group->AddSample(sample);
2181     // schedule that physical resize and sample import
2182     // (data copying), performed when "Save" is requested
2183     SampleImportItem sched_item;
2184     sched_item.gig_sample = sample;
2185     sched_item.sample_path = *iter;
2186     m_SampleImportQueue.push_back(sched_item);
2187     // add sample to the tree view
2188     Gtk::TreeModel::iterator iterSample =
2189     m_refSamplesTreeModel->append(row.children());
2190     Gtk::TreeModel::Row rowSample = *iterSample;
2191 persson 2446 rowSample[m_SamplesModel.m_col_name] =
2192     gig_to_utf8(sample->pInfo->Name);
2193 schoenebeck 1225 rowSample[m_SamplesModel.m_col_sample] = sample;
2194     rowSample[m_SamplesModel.m_col_group] = NULL;
2195     // close sound file
2196     sf_close(hFile);
2197 persson 1261 file_changed();
2198 schoenebeck 1225 } catch (std::string what) { // remember the files that made trouble (and their cause)
2199 persson 2442 if (!error_files.empty()) error_files += "\n";
2200 schoenebeck 1225 error_files += *iter += " (" + what + ")";
2201     }
2202     }
2203     // show error message box when some file(s) could not be opened / added
2204 persson 2442 if (!error_files.empty()) {
2205 schoenebeck 1382 Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files;
2206 schoenebeck 1225 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2207     msg.run();
2208     }
2209     }
2210     }
2211    
2212 schoenebeck 1673 void MainWindow::on_action_replace_all_samples_in_all_groups()
2213     {
2214     if (!file) return;
2215     Gtk::FileChooserDialog dialog(*this, _("Select Folder"),
2216     Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
2217 persson 2442 const char* str =
2218 schoenebeck 1673 _("This is a very specific function. It tries to replace all samples "
2219 persson 1799 "in the current gig file by samples located in the chosen "
2220     "directory.\n\n"
2221     "It works like this: For each sample in the gig file, it tries to "
2222 schoenebeck 1673 "find a sample file in the selected directory with the same name as "
2223 persson 1799 "the sample in the gig file. Optionally, you can add a filename "
2224     "extension below, which will be added to the filename expected to be "
2225 schoenebeck 1673 "found. That is, assume you have a gig file with a sample called "
2226     "'Snare', if you enter '.wav' below (like it's done by default), it "
2227 persson 1799 "expects to find a sample file called 'Snare.wav' and will replace "
2228     "the sample in the gig file accordingly. If you don't need an "
2229     "extension, blank the field below. Any gig sample where no "
2230     "appropriate sample file could be found will be reported and left "
2231 persson 2344 "untouched.\n");
2232     #if GTKMM_MAJOR_VERSION < 3
2233     view::WrapLabel description(str);
2234     #else
2235     Gtk::Label description(str);
2236     description.set_line_wrap();
2237     #endif
2238 schoenebeck 1673 Gtk::HBox entryArea;
2239 persson 2169 Gtk::Label entryLabel( _("Add filename extension: "), Gtk::ALIGN_START);
2240 schoenebeck 1673 Gtk::Entry postfixEntryBox;
2241     postfixEntryBox.set_text(".wav");
2242     entryArea.pack_start(entryLabel);
2243     entryArea.pack_start(postfixEntryBox);
2244     dialog.get_vbox()->pack_start(description, Gtk::PACK_SHRINK);
2245     dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK);
2246     description.show();
2247 persson 1799 entryArea.show_all();
2248 schoenebeck 1673 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2249     dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
2250     dialog.set_select_multiple(false);
2251 persson 1725 if (current_sample_dir != "") {
2252     dialog.set_current_folder(current_sample_dir);
2253     }
2254 schoenebeck 1673 if (dialog.run() == Gtk::RESPONSE_OK)
2255     {
2256 persson 1725 current_sample_dir = dialog.get_current_folder();
2257 schoenebeck 1673 Glib::ustring error_files;
2258 persson 2169 std::string folder = dialog.get_filename();
2259 schoenebeck 1673 for (gig::Sample* sample = file->GetFirstSample();
2260     sample; sample = file->GetNextSample())
2261     {
2262     std::string filename =
2263 persson 2446 folder + G_DIR_SEPARATOR_S +
2264     Glib::filename_from_utf8(gig_to_utf8(sample->pInfo->Name) +
2265     postfixEntryBox.get_text());
2266 schoenebeck 1673 SF_INFO info;
2267     info.format = 0;
2268     SNDFILE* hFile = sf_open(filename.c_str(), SFM_READ, &info);
2269     try
2270     {
2271 persson 1831 if (!hFile) throw std::string(_("could not open file"));
2272 schoenebeck 1673 int bitdepth;
2273     switch (info.format & 0xff) {
2274     case SF_FORMAT_PCM_S8:
2275     case SF_FORMAT_PCM_16:
2276     case SF_FORMAT_PCM_U8:
2277     bitdepth = 16;
2278     break;
2279     case SF_FORMAT_PCM_24:
2280     case SF_FORMAT_PCM_32:
2281     case SF_FORMAT_FLOAT:
2282     case SF_FORMAT_DOUBLE:
2283     bitdepth = 24;
2284     break;
2285     default:
2286     sf_close(hFile);
2287 persson 1831 throw std::string(_("format not supported"));
2288 schoenebeck 1673 }
2289     SampleImportItem sched_item;
2290     sched_item.gig_sample = sample;
2291     sched_item.sample_path = filename;
2292     m_SampleImportQueue.push_back(sched_item);
2293     sf_close(hFile);
2294     file_changed();
2295     }
2296     catch (std::string what)
2297     {
2298 persson 2442 if (!error_files.empty()) error_files += "\n";
2299 persson 2446 error_files += Glib::filename_to_utf8(filename) +
2300     " (" + what + ")";
2301 schoenebeck 1673 }
2302     }
2303     // show error message box when some file(s) could not be opened / added
2304 persson 2442 if (!error_files.empty()) {
2305 schoenebeck 1673 Glib::ustring txt =
2306     _("Could not replace the following sample(s):\n") + error_files;
2307     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2308     msg.run();
2309     }
2310     }
2311     }
2312    
2313 schoenebeck 1225 void MainWindow::on_action_remove_sample() {
2314     if (!file) return;
2315     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2316     Gtk::TreeModel::iterator it = sel->get_selected();
2317     if (it) {
2318     Gtk::TreeModel::Row row = *it;
2319     gig::Group* group = row[m_SamplesModel.m_col_group];
2320     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2321     Glib::ustring name = row[m_SamplesModel.m_col_name];
2322     try {
2323     // remove group or sample from the gig file
2324     if (group) {
2325 persson 2446 // temporarily remember the samples that belong to
2326 schoenebeck 1225 // that group (we need that to clean the queue)
2327     std::list<gig::Sample*> members;
2328     for (gig::Sample* pSample = group->GetFirstSample();
2329     pSample; pSample = group->GetNextSample()) {
2330     members.push_back(pSample);
2331     }
2332 schoenebeck 1322 // notify everybody that we're going to remove these samples
2333     samples_to_be_removed_signal.emit(members);
2334 schoenebeck 1225 // delete the group in the .gig file including the
2335     // samples that belong to the group
2336     file->DeleteGroup(group);
2337 schoenebeck 1322 // notify that we're done with removal
2338     samples_removed_signal.emit();
2339 schoenebeck 1225 // if sample(s) were just previously added, remove
2340     // them from the import queue
2341     for (std::list<gig::Sample*>::iterator member = members.begin();
2342     member != members.end(); ++member) {
2343     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
2344     iter != m_SampleImportQueue.end(); ++iter) {
2345     if ((*iter).gig_sample == *member) {
2346     printf("Removing previously added sample '%s' from group '%s'\n",
2347     (*iter).sample_path.c_str(), name.c_str());
2348     m_SampleImportQueue.erase(iter);
2349     break;
2350     }
2351     }
2352     }
2353 persson 1261 file_changed();
2354 schoenebeck 1225 } else if (sample) {
2355 schoenebeck 1322 // notify everybody that we're going to remove this sample
2356     std::list<gig::Sample*> lsamples;
2357     lsamples.push_back(sample);
2358     samples_to_be_removed_signal.emit(lsamples);
2359 schoenebeck 1225 // remove sample from the .gig file
2360     file->DeleteSample(sample);
2361 schoenebeck 1322 // notify that we're done with removal
2362     samples_removed_signal.emit();
2363 schoenebeck 1225 // if sample was just previously added, remove it from
2364     // the import queue
2365     for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
2366     iter != m_SampleImportQueue.end(); ++iter) {
2367     if ((*iter).gig_sample == sample) {
2368     printf("Removing previously added sample '%s'\n",
2369     (*iter).sample_path.c_str());
2370     m_SampleImportQueue.erase(iter);
2371     break;
2372     }
2373     }
2374 persson 1303 dimreg_changed();
2375 persson 1261 file_changed();
2376 schoenebeck 1225 }
2377     // remove respective row(s) from samples tree view
2378     m_refSamplesTreeModel->erase(it);
2379     } catch (RIFF::Exception e) {
2380 schoenebeck 1322 // pretend we're done with removal (i.e. to avoid dead locks)
2381     samples_removed_signal.emit();
2382     // show error message
2383 schoenebeck 1225 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
2384     msg.run();
2385     }
2386     }
2387     }
2388    
2389 schoenebeck 2610 // see comment on on_sample_treeview_drag_begin()
2390     void MainWindow::on_scripts_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2391     {
2392     first_call_to_drag_data_get = true;
2393     }
2394    
2395     void MainWindow::on_scripts_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2396     Gtk::SelectionData& selection_data, guint, guint)
2397     {
2398     if (!first_call_to_drag_data_get) return;
2399     first_call_to_drag_data_get = false;
2400    
2401     // get selected script
2402     gig::Script* script = NULL;
2403     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewScripts.get_selection();
2404     Gtk::TreeModel::iterator it = sel->get_selected();
2405     if (it) {
2406     Gtk::TreeModel::Row row = *it;
2407     script = row[m_ScriptsModel.m_col_script];
2408     }
2409     // pass the gig::Script as pointer
2410     selection_data.set(selection_data.get_target(), 0/*unused*/,
2411     (const guchar*)&script,
2412     sizeof(script)/*length of data in bytes*/);
2413     }
2414    
2415 persson 1303 // For some reason drag_data_get gets called two times for each
2416     // drag'n'drop (at least when target is an Entry). This work-around
2417     // makes sure the code in drag_data_get and drop_drag_data_received is
2418     // only executed once, as drag_begin only gets called once.
2419     void MainWindow::on_sample_treeview_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
2420     {
2421     first_call_to_drag_data_get = true;
2422     }
2423    
2424 schoenebeck 1225 void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
2425     Gtk::SelectionData& selection_data, guint, guint)
2426     {
2427 persson 1303 if (!first_call_to_drag_data_get) return;
2428     first_call_to_drag_data_get = false;
2429    
2430 schoenebeck 1225 // get selected sample
2431     gig::Sample* sample = NULL;
2432     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2433     Gtk::TreeModel::iterator it = sel->get_selected();
2434     if (it) {
2435     Gtk::TreeModel::Row row = *it;
2436     sample = row[m_SamplesModel.m_col_sample];
2437     }
2438     // pass the gig::Sample as pointer
2439     selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
2440     sizeof(sample)/*length of data in bytes*/);
2441     }
2442    
2443     void MainWindow::on_sample_label_drop_drag_data_received(
2444     const Glib::RefPtr<Gdk::DragContext>& context, int, int,
2445     const Gtk::SelectionData& selection_data, guint, guint time)
2446     {
2447     gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
2448    
2449 persson 1265 if (sample && selection_data.get_length() == sizeof(gig::Sample*)) {
2450 persson 1303 std::cout << "Drop received sample \"" <<
2451     sample->pInfo->Name << "\"" << std::endl;
2452     // drop success
2453     context->drop_reply(true, time);
2454    
2455 schoenebeck 1322 //TODO: we should better move most of the following code to DimRegionEdit::set_sample()
2456    
2457     // notify everybody that we're going to alter the region
2458     gig::Region* region = m_RegionChooser.get_region();
2459     region_to_be_changed_signal.emit(region);
2460    
2461 persson 1303 // find the samplechannel dimension
2462     gig::dimension_def_t* stereo_dimension = 0;
2463     for (int i = 0 ; i < region->Dimensions ; i++) {
2464     if (region->pDimensionDefinitions[i].dimension ==
2465     gig::dimension_samplechannel) {
2466     stereo_dimension = &region->pDimensionDefinitions[i];
2467     break;
2468     }
2469 schoenebeck 1225 }
2470 persson 1303 bool channels_changed = false;
2471     if (sample->Channels == 1 && stereo_dimension) {
2472     // remove the samplechannel dimension
2473 schoenebeck 2550 /* commented out, because it makes it impossible building up an instrument from scratch using two separate L/R samples
2474 persson 1303 region->DeleteDimension(stereo_dimension);
2475     channels_changed = true;
2476     region_changed();
2477 schoenebeck 2550 */
2478 persson 1303 }
2479 schoenebeck 2464 dimreg_edit.set_sample(
2480     sample,
2481     is_copy_samples_unity_note_enabled(),
2482     is_copy_samples_fine_tune_enabled(),
2483     is_copy_samples_loop_enabled()
2484     );
2485 persson 1303
2486     if (sample->Channels == 2 && !stereo_dimension) {
2487     // add samplechannel dimension
2488     gig::dimension_def_t dim;
2489     dim.dimension = gig::dimension_samplechannel;
2490     dim.bits = 1;
2491     dim.zones = 2;
2492     region->AddDimension(&dim);
2493     channels_changed = true;
2494     region_changed();
2495     }
2496     if (channels_changed) {
2497     // unmap all samples with wrong number of channels
2498     // TODO: maybe there should be a warning dialog for this
2499     for (int i = 0 ; i < region->DimensionRegions ; i++) {
2500     gig::DimensionRegion* d = region->pDimensionRegions[i];
2501     if (d->pSample && d->pSample->Channels != sample->Channels) {
2502 schoenebeck 1322 gig::Sample* oldref = d->pSample;
2503     d->pSample = NULL;
2504     sample_ref_changed_signal.emit(oldref, NULL);
2505 persson 1303 }
2506     }
2507     }
2508    
2509 schoenebeck 1322 // notify we're done with altering
2510     region_changed_signal.emit(region);
2511    
2512 persson 1460 file_changed();
2513    
2514 persson 1303 return;
2515 schoenebeck 1225 }
2516     // drop failed
2517     context->drop_reply(false, time);
2518     }
2519    
2520     void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path,
2521     const Gtk::TreeModel::iterator& iter) {
2522     if (!iter) return;
2523     Gtk::TreeModel::Row row = *iter;
2524     Glib::ustring name = row[m_SamplesModel.m_col_name];
2525     gig::Group* group = row[m_SamplesModel.m_col_group];
2526     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2527 persson 2446 gig::String gigname(gig_from_utf8(name));
2528 schoenebeck 1225 if (group) {
2529 persson 2446 if (group->Name != gigname) {
2530     group->Name = gigname;
2531 persson 1261 printf("group name changed\n");
2532     file_changed();
2533     }
2534 schoenebeck 1225 } else if (sample) {
2535 persson 2446 if (sample->pInfo->Name != gigname) {
2536     sample->pInfo->Name = gigname;
2537 persson 1261 printf("sample name changed\n");
2538     file_changed();
2539     }
2540 schoenebeck 1225 }
2541     }
2542    
2543 schoenebeck 2604 void MainWindow::script_name_changed(const Gtk::TreeModel::Path& path,
2544     const Gtk::TreeModel::iterator& iter) {
2545     if (!iter) return;
2546     Gtk::TreeModel::Row row = *iter;
2547     Glib::ustring name = row[m_ScriptsModel.m_col_name];
2548     gig::ScriptGroup* group = row[m_ScriptsModel.m_col_group];
2549     gig::Script* script = row[m_ScriptsModel.m_col_script];
2550     gig::String gigname(gig_from_utf8(name));
2551     if (group) {
2552     if (group->Name != gigname) {
2553     group->Name = gigname;
2554     printf("script group name changed\n");
2555     file_changed();
2556     }
2557     } else if (script) {
2558     if (script->Name != gigname) {
2559     script->Name = gigname;
2560     printf("script name changed\n");
2561     file_changed();
2562     }
2563     }
2564     }
2565    
2566 schoenebeck 1225 void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
2567     const Gtk::TreeModel::iterator& iter) {
2568     if (!iter) return;
2569     Gtk::TreeModel::Row row = *iter;
2570     Glib::ustring name = row[m_Columns.m_col_name];
2571 persson 2442
2572     // change name in instrument menu
2573     int index = path[0];
2574     const std::vector<Gtk::Widget*> children = instrument_menu->get_children();
2575     if (index < children.size()) {
2576     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 16) || GTKMM_MAJOR_VERSION > 2
2577     static_cast<Gtk::RadioMenuItem*>(children[index])->set_label(name);
2578     #else
2579     remove_instrument_from_menu(index);
2580     Gtk::RadioMenuItem* item = add_instrument_to_menu(name, index);
2581     item->set_active();
2582     #endif
2583     }
2584    
2585     // change name in gig
2586 schoenebeck 1225 gig::Instrument* instrument = row[m_Columns.m_col_instr];
2587 persson 2446 gig::String gigname(gig_from_utf8(name));
2588     if (instrument && instrument->pInfo->Name != gigname) {
2589     instrument->pInfo->Name = gigname;
2590 persson 2445
2591     // change name in the instrument properties window
2592     if (instrumentProps.get_instrument() == instrument) {
2593     instrumentProps.update_name();
2594     }
2595    
2596 persson 1261 file_changed();
2597     }
2598 schoenebeck 1225 }
2599 schoenebeck 1322
2600 schoenebeck 2548 void MainWindow::on_action_combine_instruments() {
2601     CombineInstrumentsDialog* d = new CombineInstrumentsDialog(*this, file);
2602     d->show_all();
2603     d->resize(500, 400);
2604     d->run();
2605     if (d->fileWasChanged()) {
2606     // update GUI with new instrument just created
2607     add_instrument(d->newCombinedInstrument());
2608     }
2609     delete d;
2610     }
2611    
2612 schoenebeck 2624 void MainWindow::on_action_view_references() {
2613     Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
2614     Gtk::TreeModel::iterator it = sel->get_selected();
2615     if (!it) return;
2616     Gtk::TreeModel::Row row = *it;
2617     gig::Sample* sample = row[m_SamplesModel.m_col_sample];
2618     if (!sample) return;
2619    
2620     ReferencesView* d = new ReferencesView(*this);
2621     d->setSample(sample);
2622     d->show_all();
2623     d->resize(500, 400);
2624     d->run();
2625     delete d;
2626     }
2627    
2628 schoenebeck 2553 void MainWindow::mergeFiles(const std::vector<std::string>& filenames) {
2629     struct _Source {
2630     std::vector<RIFF::File*> riffs;
2631     std::vector<gig::File*> gigs;
2632    
2633     ~_Source() {
2634     for (int k = 0; k < gigs.size(); ++k) delete gigs[k];
2635     for (int k = 0; k < riffs.size(); ++k) delete riffs[k];
2636     riffs.clear();
2637     gigs.clear();
2638     }
2639     } sources;
2640    
2641     if (filenames.empty())
2642     throw RIFF::Exception(_("No files selected, so nothing done."));
2643    
2644     // first open all input files (to avoid output file corruption)
2645     int i;
2646     try {
2647     for (i = 0; i < filenames.size(); ++i) {
2648     const std::string& filename = filenames[i];
2649     printf("opening file=%s\n", filename.c_str());
2650    
2651     RIFF::File* riff = new RIFF::File(filename);
2652     sources.riffs.push_back(riff);
2653    
2654     gig::File* gig = new gig::File(riff);
2655     sources.gigs.push_back(gig);
2656     }
2657     } catch (RIFF::Exception e) {
2658     throw RIFF::Exception(
2659     _("Error occurred while opening '") +
2660     filenames[i] +
2661     "': " +
2662     e.Message
2663     );
2664     } catch (...) {
2665     throw RIFF::Exception(
2666     _("Unknown exception occurred while opening '") +
2667     filenames[i] + "'"
2668     );
2669     }
2670    
2671     // now merge the opened .gig files to the main .gig file currently being
2672     // open in gigedit
2673     try {
2674     for (i = 0; i < filenames.size(); ++i) {
2675     const std::string& filename = filenames[i];
2676     printf("merging file=%s\n", filename.c_str());
2677     assert(i < sources.gigs.size());
2678    
2679     this->file->AddContentOf(sources.gigs[i]);
2680     }
2681     } catch (RIFF::Exception e) {
2682     throw RIFF::Exception(
2683     _("Error occurred while merging '") +
2684     filenames[i] +
2685     "': " +
2686     e.Message
2687     );
2688     } catch (...) {
2689     throw RIFF::Exception(
2690     _("Unknown exception occurred while merging '") +
2691     filenames[i] + "'"
2692     );
2693     }
2694    
2695     // Note: requires that this file already has a filename !
2696     this->file->Save();
2697     }
2698    
2699     void MainWindow::on_action_merge_files() {
2700     if (this->file->GetFileName().empty()) {
2701     Glib::ustring txt = _(
2702     "You seem to have a new .gig file open that has not been saved "
2703     "yet. You must save it somewhere before starting to merge it with "
2704     "other .gig files though, because during the merge operation the "
2705     "other files' sample data must be written on file level to the "
2706     "target .gig file."
2707     );
2708     Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
2709     msg.run();
2710     return;
2711     }
2712    
2713     Gtk::FileChooserDialog dialog(*this, _("Merge .gig files"));
2714     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2715     dialog.add_button(_("Merge"), Gtk::RESPONSE_OK);
2716     dialog.set_default_response(Gtk::RESPONSE_CANCEL);
2717     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
2718     Gtk::FileFilter filter;
2719     filter.add_pattern("*.gig");
2720     #else
2721     Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
2722     filter->add_pattern("*.gig");
2723     #endif
2724     dialog.set_filter(filter);
2725     if (current_gig_dir != "") {
2726     dialog.set_current_folder(current_gig_dir);
2727     }
2728     dialog.set_select_multiple(true);
2729    
2730     // show warning in the file picker dialog
2731     Gtk::HBox descriptionArea;
2732     descriptionArea.set_spacing(15);
2733     Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
2734     descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
2735     #if GTKMM_MAJOR_VERSION < 3
2736     view::WrapLabel description;
2737     #else
2738     Gtk::Label description;
2739     description.set_line_wrap();
2740     #endif
2741     description.set_markup(_(
2742     "\nSelect at least one .gig file that shall be merged to the .gig file "
2743     "currently being open in gigedit.\n\n"
2744     "<b>Please Note:</b> Merging with other files will modify your "
2745     "currently open .gig file on file level! And be aware that the current "
2746     "merge algorithm does not detect duplicate samples yet. So if you are "
2747     "merging files which are using equivalent sample data, those "
2748     "equivalent samples will currently be treated as separate samples and "
2749     "will accordingly be stored separately in the target .gig file!"
2750     ));
2751     descriptionArea.pack_start(description);
2752     dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
2753     descriptionArea.show_all();
2754    
2755     if (dialog.run() == Gtk::RESPONSE_OK) {
2756     printf("on_action_merge_files self=%x\n", Glib::Threads::Thread::self());
2757     std::vector<std::string> filenames = dialog.get_filenames();
2758    
2759     // merge the selected files to the currently open .gig file
2760     try {
2761     mergeFiles(filenames);
2762     } catch (RIFF::Exception e) {
2763     Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
2764     msg.run();
2765     }
2766    
2767     // update GUI
2768     __refreshEntireGUI();
2769     }
2770     }
2771    
2772 schoenebeck 1411 void MainWindow::set_file_is_shared(bool b) {
2773     this->file_is_shared = b;
2774    
2775     if (file_is_shared) {
2776     m_AttachedStateLabel.set_label(_("live-mode"));
2777     m_AttachedStateImage.set(
2778     Gdk::Pixbuf::create_from_xpm_data(status_attached_xpm)
2779     );
2780     } else {
2781     m_AttachedStateLabel.set_label(_("stand-alone"));
2782     m_AttachedStateImage.set(
2783     Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)
2784     );
2785     }
2786     }
2787    
2788 schoenebeck 2621 void MainWindow::on_sample_ref_count_incremented(gig::Sample* sample, int offset) {
2789     if (!sample) return;
2790     sample_ref_count[sample] += offset;
2791     const int refcount = sample_ref_count[sample];
2792    
2793     Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
2794     for (int g = 0; g < model->children().size(); ++g) {
2795     Gtk::TreeModel::Row rowGroup = model->children()[g];
2796     for (int s = 0; s < rowGroup.children().size(); ++s) {
2797     Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
2798     if (rowSample[m_SamplesModel.m_col_sample] != sample) continue;
2799     rowSample[m_SamplesModel.m_col_refcount] = ToString(refcount) + " " + _("Refs.");
2800 schoenebeck 2625 rowSample[m_SamplesModel.m_color] = refcount ? "black" : "red";
2801 schoenebeck 2621 }
2802     }
2803     }
2804    
2805     void MainWindow::on_sample_ref_changed(gig::Sample* oldSample, gig::Sample* newSample) {
2806     on_sample_ref_count_incremented(oldSample, -1);
2807     on_sample_ref_count_incremented(newSample, +1);
2808     }
2809    
2810     void MainWindow::on_samples_to_be_removed(std::list<gig::Sample*> samples) {
2811     // just in case a new sample is added later with exactly the same memory
2812     // address, which would lead to incorrect refcount if not deleted here
2813     for (std::list<gig::Sample*>::const_iterator it = samples.begin();
2814     it != samples.end(); it != samples.end())
2815     {
2816     sample_ref_count.erase(*it);
2817     }
2818     }
2819    
2820 schoenebeck 2625 void MainWindow::show_samples_tab() {
2821     m_TreeViewNotebook.set_current_page(0);
2822     }
2823    
2824     void MainWindow::show_intruments_tab() {
2825     m_TreeViewNotebook.set_current_page(1);
2826     }
2827    
2828     void MainWindow::show_scripts_tab() {
2829     m_TreeViewNotebook.set_current_page(2);
2830     }
2831    
2832 schoenebeck 1339 sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_to_be_changed() {
2833 schoenebeck 1322 return file_structure_to_be_changed_signal;
2834     }
2835    
2836 schoenebeck 1339 sigc::signal<void, gig::File*>& MainWindow::signal_file_structure_changed() {
2837 schoenebeck 1322 return file_structure_changed_signal;
2838     }
2839    
2840 schoenebeck 1339 sigc::signal<void, std::list<gig::Sample*> >& MainWindow::signal_samples_to_be_removed() {
2841 schoenebeck 1322 return samples_to_be_removed_signal;
2842     }
2843    
2844 schoenebeck 1339 sigc::signal<void>& MainWindow::signal_samples_removed() {
2845 schoenebeck 1322 return samples_removed_signal;
2846     }
2847    
2848 schoenebeck 1339 sigc::signal<void, gig::Region*>& MainWindow::signal_region_to_be_changed() {
2849 schoenebeck 1322 return region_to_be_changed_signal;
2850     }
2851    
2852 schoenebeck 1339 sigc::signal<void, gig::Region*>& MainWindow::signal_region_changed() {
2853 schoenebeck 1322 return region_changed_signal;
2854     }
2855    
2856 schoenebeck 1853 sigc::signal<void, gig::Sample*>& MainWindow::signal_sample_changed() {
2857     return sample_changed_signal;
2858     }
2859    
2860 schoenebeck 1339 sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& MainWindow::signal_sample_ref_changed() {
2861 schoenebeck 1322 return sample_ref_changed_signal;
2862     }
2863    
2864 schoenebeck 1339 sigc::signal<void, gig::DimensionRegion*>& MainWindow::signal_dimreg_to_be_changed() {
2865 schoenebeck 1322 return dimreg_to_be_changed_signal;
2866     }
2867    
2868 schoenebeck 1339 sigc::signal<void, gig::DimensionRegion*>& MainWindow::signal_dimreg_changed() {
2869 schoenebeck 1322 return dimreg_changed_signal;
2870     }
2871 schoenebeck 1654
2872     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_note_on() {
2873     return note_on_signal;
2874     }
2875    
2876     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_note_off() {
2877     return note_off_signal;
2878     }
2879 schoenebeck 1660
2880     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_hit() {
2881     return m_RegionChooser.signal_keyboard_key_hit();
2882     }
2883    
2884     sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {
2885     return m_RegionChooser.signal_keyboard_key_released();
2886     }

  ViewVC Help
Powered by ViewVC