/[svn]/gigedit/trunk/src/gigedit/mainwindow.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2246 - (show annotations) (download)
Fri Aug 19 10:55:41 2011 UTC (12 years, 7 months ago) by persson
File size: 71622 byte(s)
* gtkmm 3 fix: rewrote the custom widgets (regionchooser and
  dimregionchooser) so they only draw pixels in the on_draw
  method. This should make them work again in newer gtkmm 3
  environments.

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

  ViewVC Help
Powered by ViewVC