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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1100 - (show annotations) (download)
Sat Mar 17 09:20:19 2007 UTC (17 years, 1 month ago) by persson
File size: 39797 byte(s)
* code refactoring: dimregion editor and parameter editors extracted
  to separate files.
* loading progress dialog now also shown when filename is given as
  argument

1 /*
2 * Copyright (C) 2006, 2007 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 <libintl.h>
21 #include <iostream>
22
23 #include <gtkmm/filechooserdialog.h>
24 #include <gtkmm/messagedialog.h>
25 #include <gtkmm/stock.h>
26 #include <gtkmm/targetentry.h>
27
28 #if GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6
29 #define ABOUT_DIALOG
30 #include <gtkmm/aboutdialog.h>
31 #endif
32
33 #include <stdio.h>
34 #include <sndfile.h>
35
36 #include "mainwindow.h"
37
38 #define _(String) gettext(String)
39
40 template<class T> inline std::string ToString(T o) {
41 std::stringstream ss;
42 ss << o;
43 return ss.str();
44 }
45
46 MainWindow::MainWindow()
47 {
48 // set_border_width(5);
49 // set_default_size(400, 200);
50
51
52 add(m_VBox);
53
54 // Handle selection
55 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
56 tree_sel_ref->signal_changed().connect(
57 sigc::mem_fun(*this, &MainWindow::on_sel_change));
58
59 // m_TreeView.set_reorderable();
60
61 m_TreeView.signal_button_press_event().connect_notify(
62 sigc::mem_fun(*this, &MainWindow::on_button_release));
63
64 // Add the TreeView tab, inside a ScrolledWindow, with the button underneath:
65 m_ScrolledWindow.add(m_TreeView);
66 // m_ScrolledWindow.set_size_request(200, 600);
67 m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
68
69 m_ScrolledWindowSamples.add(m_TreeViewSamples);
70 m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
71
72
73 m_TreeViewNotebook.set_size_request(300);
74
75 m_HPaned.add1(m_TreeViewNotebook);
76 m_HPaned.add2(dimreg_edit);
77
78
79 m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples");
80 m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments");
81
82
83 actionGroup = Gtk::ActionGroup::create();
84
85 actionGroup->add(Gtk::Action::create("MenuFile", _("_File")));
86 actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW),
87 sigc::mem_fun(
88 *this, &MainWindow::on_action_file_new));
89 Glib::RefPtr<Gtk::Action> action =
90 Gtk::Action::create("Open", Gtk::Stock::OPEN);
91 action->property_label() = action->property_label() + "...";
92 actionGroup->add(action,
93 sigc::mem_fun(
94 *this, &MainWindow::on_action_file_open));
95 actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE),
96 sigc::mem_fun(
97 *this, &MainWindow::on_action_file_save));
98 action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS);
99 action->property_label() = action->property_label() + "...";
100 actionGroup->add(action,
101 *(new Gtk::AccelKey("<shift><control>s")),
102 sigc::mem_fun(
103 *this, &MainWindow::on_action_file_save_as)
104 );
105 actionGroup->add(Gtk::Action::create("Properties",
106 Gtk::Stock::PROPERTIES),
107 sigc::mem_fun(
108 *this, &MainWindow::on_action_file_properties));
109 actionGroup->add(Gtk::Action::create("InstrProperties",
110 Gtk::Stock::PROPERTIES),
111 sigc::mem_fun(
112 *this, &MainWindow::show_instr_props));
113 actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT),
114 sigc::mem_fun(
115 *this, &MainWindow::hide));
116 actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument")));
117
118 action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP);
119 actionGroup->add(Gtk::Action::create("MenuHelp",
120 action->property_label()));
121 #ifdef ABOUT_DIALOG
122 actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT),
123 sigc::mem_fun(
124 *this, &MainWindow::on_action_help_about));
125 #endif
126 action = Gtk::Action::create("Remove", Gtk::Stock::REMOVE);
127 actionGroup->add(action,
128 sigc::mem_fun(
129 *this, &MainWindow::hide));
130
131 // sample right-click popup actions
132 actionGroup->add(
133 Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES),
134 sigc::mem_fun(*this, &MainWindow::on_action_sample_properties)
135 );
136 actionGroup->add(
137 Gtk::Action::create("AddGroup", _("Add _Group")),
138 sigc::mem_fun(*this, &MainWindow::on_action_add_group)
139 );
140 actionGroup->add(
141 Gtk::Action::create("AddSample", _("Add _Sample(s)")),
142 sigc::mem_fun(*this, &MainWindow::on_action_add_sample)
143 );
144 actionGroup->add(
145 Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),
146 sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
147 );
148
149 uiManager = Gtk::UIManager::create();
150 uiManager->insert_action_group(actionGroup);
151 // add_accel_group(uiManager->get_accel_group());
152
153 Glib::ustring ui_info =
154 "<ui>"
155 " <menubar name='MenuBar'>"
156 " <menu action='MenuFile'>"
157 " <menuitem action='New'/>"
158 " <menuitem action='Open'/>"
159 " <separator/>"
160 " <menuitem action='Save'/>"
161 " <menuitem action='SaveAs'/>"
162 " <separator/>"
163 " <menuitem action='Properties'/>"
164 " <separator/>"
165 " <menuitem action='Quit'/>"
166 " </menu>"
167 " <menu action='MenuInstrument'>"
168 " </menu>"
169 #ifdef ABOUT_DIALOG
170 " <menu action='MenuHelp'>"
171 " <menuitem action='About'/>"
172 " </menu>"
173 #endif
174 " </menubar>"
175 " <popup name='PopupMenu'>"
176 " <menuitem action='InstrProperties'/>"
177 " <menuitem action='Remove'/>"
178 " </popup>"
179 " <popup name='SamplePopupMenu'>"
180 " <menuitem action='SampleProperties'/>"
181 " <menuitem action='AddGroup'/>"
182 " <menuitem action='AddSample'/>"
183 " <separator/>"
184 " <menuitem action='RemoveSample'/>"
185 " </popup>"
186 "</ui>";
187 uiManager->add_ui_from_string(ui_info);
188
189 popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu"));
190
191 Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar");
192 m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK);
193 m_VBox.pack_start(m_HPaned);
194 m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK);
195 m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK);
196
197 m_RegionChooser.signal_sel_changed().connect(
198 sigc::mem_fun(*this, &MainWindow::region_changed) );
199 m_DimRegionChooser.signal_sel_changed().connect(
200 sigc::mem_fun(*this, &MainWindow::dimreg_changed) );
201
202
203 // Create the Tree model:
204 m_refTreeModel = Gtk::ListStore::create(m_Columns);
205 m_TreeView.set_model(m_refTreeModel);
206 m_refTreeModel->signal_row_changed().connect(
207 sigc::mem_fun(*this, &MainWindow::instrument_name_changed)
208 );
209
210 // Add the TreeView's view columns:
211 m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name);
212 m_TreeView.set_headers_visible(false);
213
214 // create samples treeview (including its data model)
215 m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel);
216 m_TreeViewSamples.set_model(m_refSamplesTreeModel);
217 // m_TreeViewSamples.set_reorderable();
218 m_TreeViewSamples.append_column_editable("Samples", m_SamplesModel.m_col_name);
219 m_TreeViewSamples.set_headers_visible(false);
220 m_TreeViewSamples.signal_button_press_event().connect_notify(
221 sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release)
222 );
223 m_refSamplesTreeModel->signal_row_changed().connect(
224 sigc::mem_fun(*this, &MainWindow::sample_name_changed)
225 );
226
227 // establish drag&drop between samples tree view and dimension region 'Sample' text entry
228 std::list<Gtk::TargetEntry> drag_target_gig_sample;
229 drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") );
230 //drag_target_gig_sample.push_back( Gtk::TargetEntry("STRING") );
231 //drag_target_gig_sample.push_back( Gtk::TargetEntry("text/plain") );
232 m_TreeViewSamples.drag_source_set(drag_target_gig_sample);
233 m_TreeViewSamples.signal_drag_data_get().connect(
234 sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get)
235 );
236 dimreg_edit.wSample->drag_dest_set(drag_target_gig_sample);
237 dimreg_edit.wSample->signal_drag_data_received().connect(
238 sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received)
239 );
240
241 file = 0;
242
243 show_all_children();
244 }
245
246 MainWindow::~MainWindow()
247 {
248 }
249
250 void MainWindow::region_changed()
251 {
252 m_DimRegionChooser.set_region(m_RegionChooser.get_region());
253 }
254
255 void MainWindow::dimreg_changed()
256 {
257 dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion());
258 }
259
260 void MainWindow::on_sel_change()
261 {
262 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
263
264 Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
265 if (it)
266 {
267 Gtk::TreeModel::Row row = *it;
268 std::cout << row[m_Columns.m_col_name] << std::endl;
269
270 if (row[m_Columns.m_col_instr])
271 m_RegionChooser.set_instrument(row[m_Columns.m_col_instr]);
272 }
273 }
274
275 void loader_progress_callback(gig::progress_t* progress)
276 {
277 Loader* loader = static_cast<Loader*>(progress->custom);
278 loader->progress_callback(progress->factor);
279 }
280
281 void Loader::progress_callback(float fraction)
282 {
283 {
284 Glib::Mutex::Lock lock(progressMutex);
285 progress = fraction;
286 }
287 progress_dispatcher();
288 }
289
290 void Loader::thread_function()
291 {
292 printf("thread_function self=%x\n", Glib::Thread::self());
293 printf("Start %s\n", filename);
294 RIFF::File* riff = new RIFF::File(filename);
295 gig = new gig::File(riff);
296 gig::progress_t progress;
297 progress.callback = loader_progress_callback;
298 progress.custom = this;
299
300 gig->GetInstrument(0, &progress);
301 printf("End\n");
302 finished_dispatcher();
303 }
304
305 Loader::Loader(const char* filename)
306 : thread(0), filename(filename)
307 {
308 }
309
310 void Loader::launch()
311 {
312 thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true);
313 printf("launch thread=%x\n", thread);
314 }
315
316 float Loader::get_progress()
317 {
318 float res;
319 {
320 Glib::Mutex::Lock lock(progressMutex);
321 res = progress;
322 }
323 return res;
324 }
325
326 Glib::Dispatcher& Loader::signal_progress()
327 {
328 return progress_dispatcher;
329 }
330
331 Glib::Dispatcher& Loader::signal_finished()
332 {
333 return finished_dispatcher;
334 }
335
336 LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)
337 : Gtk::Dialog(title, parent, true)
338 {
339 get_vbox()->pack_start(progressBar);
340 show_all_children();
341 }
342
343 void MainWindow::on_action_file_new()
344 {
345 m_SampleImportQueue.clear();
346 }
347
348 void MainWindow::on_action_file_open()
349 {
350 Gtk::FileChooserDialog dialog(*this, _("Open file"));
351 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
352 dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
353 Gtk::FileFilter filter;
354 filter.add_pattern("*.gig");
355 dialog.set_filter(filter);
356 if (dialog.run() == Gtk::RESPONSE_OK) {
357 printf("filename=%s\n", dialog.get_filename().c_str());
358
359 // remove all entries from "Instrument" menu
360 Gtk::MenuItem* instrument_menu =
361 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
362 instrument_menu->hide();
363 for (int i = 0; i < instrument_menu->get_submenu()->items().size(); i++) {
364 delete &instrument_menu->get_submenu()->items()[i];
365 }
366 instrument_menu->get_submenu()->items().clear();
367
368 m_SampleImportQueue.clear();
369 m_refTreeModel->clear();
370 m_refSamplesTreeModel->clear();
371 if (file) delete file;
372
373 printf("on_action_file_open self=%x\n", Glib::Thread::self());
374 load_file(dialog.get_filename().c_str());
375 }
376 }
377
378 void MainWindow::load_file(const char* name)
379 {
380 load_dialog = new LoadDialog("Loading...", *this);
381 load_dialog->show_all();
382 loader = new Loader(strdup(name));
383 loader->signal_progress().connect(
384 sigc::mem_fun(*this, &MainWindow::on_loader_progress));
385 loader->signal_finished().connect(
386 sigc::mem_fun(*this, &MainWindow::on_loader_finished));
387 loader->launch();
388 }
389
390 void MainWindow::on_loader_progress()
391 {
392 load_dialog->set_fraction(loader->get_progress());
393 }
394
395 void MainWindow::on_loader_finished()
396 {
397 printf("Loader finished!\n");
398 printf("on_loader_finished self=%x\n", Glib::Thread::self());
399 load_gig(loader->gig, loader->filename);
400
401
402 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
403 tree_sel_ref->select(Gtk::TreePath("0"));
404
405 load_dialog->hide();
406 }
407
408 void MainWindow::on_action_file_save()
409 {
410 if (!file) return;
411 std::cout << "Saving file\n" << std::flush;
412 try {
413 file->Save();
414 } catch (RIFF::Exception e) {
415 Glib::ustring txt = "Could not save file: " + e.Message;
416 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
417 msg.run();
418 return;
419 }
420 std::cout << "Saving file done\n" << std::flush;
421 __import_queued_samples();
422 }
423
424 void MainWindow::on_action_file_save_as()
425 {
426 if (!file) return;
427 Gtk::FileChooserDialog dialog(*this, "Open", Gtk::FILE_CHOOSER_ACTION_SAVE);
428 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
429 dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
430 Gtk::FileFilter filter;
431 filter.add_pattern("*.gig");
432 dialog.set_filter(filter);
433 if (dialog.run() == Gtk::RESPONSE_OK) {
434 printf("filename=%s\n", dialog.get_filename().c_str());
435 try {
436 file->Save(dialog.get_filename());
437 } catch (RIFF::Exception e) {
438 Glib::ustring txt = "Could not save file: " + e.Message;
439 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
440 msg.run();
441 return;
442 }
443 __import_queued_samples();
444 }
445 }
446
447 // actually write the sample(s)' data to the gig file
448 void MainWindow::__import_queued_samples() {
449 std::cout << "Starting sample import\n" << std::flush;
450 Glib::ustring error_files;
451 printf("Samples to import: %d\n", m_SampleImportQueue.size());
452 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
453 iter != m_SampleImportQueue.end(); ) {
454 printf("Importing sample %s\n",(*iter).sample_path.c_str());
455 SF_INFO info;
456 info.format = 0;
457 SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
458 try {
459 if (!hFile) throw std::string("could not open file");
460 // determine sample's bit depth
461 int bitdepth;
462 switch (info.format & 0xff) {
463 case SF_FORMAT_PCM_S8:
464 bitdepth = 16; // we simply convert to 16 bit for now
465 break;
466 case SF_FORMAT_PCM_16:
467 bitdepth = 16;
468 break;
469 case SF_FORMAT_PCM_24:
470 bitdepth = 32; // we simply convert to 32 bit for now
471 break;
472 case SF_FORMAT_PCM_32:
473 bitdepth = 32;
474 break;
475 case SF_FORMAT_PCM_U8:
476 bitdepth = 16; // we simply convert to 16 bit for now
477 break;
478 case SF_FORMAT_FLOAT:
479 bitdepth = 32;
480 break;
481 case SF_FORMAT_DOUBLE:
482 bitdepth = 32; // I guess we will always truncate this to 32 bit
483 break;
484 default:
485 sf_close(hFile); // close sound file
486 throw std::string("format not supported"); // unsupported subformat (yet?)
487 }
488 // allocate appropriate copy buffer (TODO: for now we copy
489 // it in one piece, might be tough for very long samples)
490 // and copy sample data into buffer
491 int8_t* buffer = NULL;
492 switch (bitdepth) {
493 case 16:
494 buffer = new int8_t[2 * info.channels * info.frames];
495 // libsndfile does the conversion for us (if needed)
496 sf_readf_short(hFile, (short*) buffer, info.frames);
497 break;
498 case 32:
499 buffer = new int8_t[4 * info.channels * info.frames];
500 // libsndfile does the conversion for us (if needed)
501 sf_readf_int(hFile, (int*) buffer, info.frames);
502 break;
503 }
504 // write from buffer directly (physically) into .gig file
505 (*iter).gig_sample->Write(buffer, info.frames);
506 // cleanup
507 sf_close(hFile);
508 delete buffer;
509 // on success we remove the sample from the import queue,
510 // otherwise keep it, maybe it works the next time ?
511 std::list<SampleImportItem>::iterator cur = iter;
512 ++iter;
513 m_SampleImportQueue.erase(cur);
514 } catch (std::string what) {
515 // remember the files that made trouble (and their cause)
516 if (error_files.size()) error_files += "\n";
517 error_files += (*iter).sample_path += " (" + what + ")";
518 ++iter;
519 }
520 }
521 // show error message box when some sample(s) could not be imported
522 if (error_files.size()) {
523 Glib::ustring txt = "Could not import the following sample(s):\n" + error_files;
524 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
525 msg.run();
526 }
527 }
528
529 void MainWindow::on_action_file_properties()
530 {
531 propDialog.show();
532 propDialog.deiconify();
533 }
534
535 void MainWindow::on_action_help_about()
536 {
537 #ifdef ABOUT_DIALOG
538 Gtk::AboutDialog dialog;
539 dialog.set_version(VERSION);
540 dialog.run();
541 #endif
542 }
543
544 PropDialog::PropDialog()
545 : table(2,1)
546 {
547 table.set_col_spacings(5);
548 char* propLabels[] = {
549 "Name:",
550 "CreationDate:",
551 "Comments:", // TODO: multiline
552 "Product:",
553 "Copyright:",
554 "Artists:",
555 "Genre:",
556 "Keywords:",
557 "Engineer:",
558 "Technician:",
559 "Software:", // TODO: readonly
560 "Medium:",
561 "Source:",
562 "SourceForm:",
563 "Commissioned:",
564 "Subject:"
565 };
566 for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
567 label[i].set_text(propLabels[i]);
568 label[i].set_alignment(Gtk::ALIGN_LEFT);
569 table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
570 table.attach(entry[i], 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND,
571 Gtk::SHRINK);
572 }
573
574 add(table);
575 // add_button(Gtk::Stock::CANCEL, 0);
576 // add_button(Gtk::Stock::OK, 1);
577 show_all_children();
578 }
579
580 void PropDialog::set_info(DLS::Info* info)
581 {
582 entry[0].set_text(info->Name);
583 entry[1].set_text(info->CreationDate);
584 entry[2].set_text(Glib::convert(info->Comments, "UTF-8", "ISO-8859-1"));
585 entry[3].set_text(info->Product);
586 entry[4].set_text(info->Copyright);
587 entry[5].set_text(info->Artists);
588 entry[6].set_text(info->Genre);
589 entry[7].set_text(info->Keywords);
590 entry[8].set_text(info->Engineer);
591 entry[9].set_text(info->Technician);
592 entry[10].set_text(info->Software);
593 entry[11].set_text(info->Medium);
594 entry[12].set_text(info->Source);
595 entry[13].set_text(info->SourceForm);
596 entry[14].set_text(info->Commissioned);
597 entry[15].set_text(info->Subject);
598 }
599
600
601 InstrumentProps::InstrumentProps()
602 : table(2,1),
603 quitButton(Gtk::Stock::CLOSE)
604 {
605 table.set_col_spacings(5);
606 char* propLabels[] = {
607 "Name:",
608 "IsDrum:",
609 "MIDIBank:",
610 "MIDIProgram:",
611 "Attenuation:",
612 "EffectSend:",
613 "FineTune:",
614 "PitchbendRange:",
615 "PianoReleaseMode:",
616 "DimensionKeyRange:",
617 };
618 int entryIdx = 0, checkIdx = 0;
619 for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
620 label[i].set_text(propLabels[i]);
621 label[i].set_alignment(Gtk::ALIGN_LEFT);
622 table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
623 if (i == 1 || i == 8)
624 table.attach(check[checkIdx++], 1, 2, i, i + 1,
625 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
626 else
627 table.attach(entry[entryIdx++], 1, 2, i, i + 1,
628 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
629 }
630
631 // vbox { table buttonBox { quitButton } }
632
633 //get_vbox()->pack_start(table);
634 // set_border_width(6);
635 add(vbox);
636 table.set_border_width(2);
637 vbox.pack_start(table);
638 table.show();
639 vbox.pack_start(buttonBox);
640 buttonBox.set_layout(Gtk::BUTTONBOX_END);
641 buttonBox.set_border_width(5);
642 buttonBox.show();
643 buttonBox.pack_start(quitButton);
644 quitButton.set_flags(Gtk::CAN_DEFAULT);
645 quitButton.grab_focus();
646
647 quitButton.signal_clicked().connect(
648 sigc::mem_fun(*this, &InstrumentProps::hide));
649
650 // quitButton.grab_default();
651 quitButton.show();
652 // add(table);
653 vbox.show();
654 show_all_children();
655 }
656
657 extern char* notes[];
658
659 void InstrumentProps::set_instrument(gig::Instrument* instrument)
660 {
661 char buf[100];
662
663 int entryIdx = 0, checkIdx = 0;
664 entry[entryIdx++].set_text(instrument->pInfo->Name);
665 check[checkIdx++].set_active(instrument->IsDrum);
666 sprintf(buf, "%d", instrument->MIDIBank);
667 entry[entryIdx++].set_text(buf);
668 sprintf(buf, "%d", instrument->MIDIProgram);
669 entry[entryIdx++].set_text(buf);
670 sprintf(buf, "%d", instrument->Attenuation);
671 entry[entryIdx++].set_text(buf);
672 sprintf(buf, "%d", instrument->EffectSend);
673 entry[entryIdx++].set_text(buf);
674 sprintf(buf, "%d", instrument->FineTune);
675 entry[entryIdx++].set_text(buf);
676 sprintf(buf, "%d", instrument->PitchbendRange);
677 entry[entryIdx++].set_text(buf);
678 check[checkIdx++].set_active(instrument->PianoReleaseMode);
679 sprintf(buf, "%s%d (%d)..%s%d (%d)",
680 notes[instrument->DimensionKeyRange.low % 12],
681 instrument->DimensionKeyRange.low / 12 - 1,
682 instrument->DimensionKeyRange.low,
683 notes[instrument->DimensionKeyRange.high % 12],
684 instrument->DimensionKeyRange.high / 12 - 1,
685 instrument->DimensionKeyRange.high);
686 entry[entryIdx].set_text(buf);
687 }
688
689 void MainWindow::load_gig(gig::File* gig, const char* filename)
690 {
691 file = gig;
692
693 const char *basename = strrchr(filename, '/');
694 basename = basename ? basename + 1 : filename;
695
696 set_title(basename);
697
698 propDialog.set_info(gig->pInfo);
699
700 Gtk::MenuItem* instrument_menu =
701 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
702
703 int instrument_index = 0;
704 Gtk::RadioMenuItem::Group instrument_group;
705 for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
706 instrument = gig->GetNextInstrument()) {
707 Gtk::TreeModel::iterator iter = m_refTreeModel->append();
708 Gtk::TreeModel::Row row = *iter;
709 row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
710 row[m_Columns.m_col_instr] = instrument;
711 // create a menu item for this instrument
712 Gtk::RadioMenuItem* item =
713 new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());
714 instrument_menu->get_submenu()->append(*item);
715 item->signal_activate().connect(
716 sigc::bind(
717 sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
718 instrument_index
719 )
720 );
721 instrument_index++;
722 }
723 instrument_menu->show();
724 instrument_menu->get_submenu()->show_all_children();
725
726 for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
727 if (group->Name != "") {
728 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
729 Gtk::TreeModel::Row rowGroup = *iterGroup;
730 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
731 rowGroup[m_SamplesModel.m_col_group] = group;
732 rowGroup[m_SamplesModel.m_col_sample] = NULL;
733 for (gig::Sample* sample = group->GetFirstSample();
734 sample; sample = group->GetNextSample()) {
735 Gtk::TreeModel::iterator iterSample =
736 m_refSamplesTreeModel->append(rowGroup.children());
737 Gtk::TreeModel::Row rowSample = *iterSample;
738 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
739 rowSample[m_SamplesModel.m_col_sample] = sample;
740 rowSample[m_SamplesModel.m_col_group] = NULL;
741 }
742 }
743 }
744 }
745
746 void MainWindow::show_instr_props()
747 {
748 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
749 Gtk::TreeModel::iterator it = tree_sel_ref->get_selected();
750 if (it)
751 {
752 Gtk::TreeModel::Row row = *it;
753 if (row[m_Columns.m_col_instr])
754 {
755 instrumentProps.set_instrument(row[m_Columns.m_col_instr]);
756 instrumentProps.show();
757 instrumentProps.deiconify();
758 }
759 }
760 }
761
762 void MainWindow::on_button_release(GdkEventButton* button)
763 {
764 if (button->type == GDK_2BUTTON_PRESS) {
765 show_instr_props();
766 } else if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
767 popup_menu->popup(button->button, button->time);
768 }
769 }
770
771 void MainWindow::on_instrument_selection_change(int index) {
772 m_RegionChooser.set_instrument(file->GetInstrument(index));
773 }
774
775 void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
776 if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
777 Gtk::Menu* sample_popup =
778 dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu"));
779 // update enabled/disabled state of sample popup items
780 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
781 Gtk::TreeModel::iterator it = sel->get_selected();
782 bool group_selected = false;
783 bool sample_selected = false;
784 if (it) {
785 Gtk::TreeModel::Row row = *it;
786 group_selected = row[m_SamplesModel.m_col_group];
787 sample_selected = row[m_SamplesModel.m_col_sample];
788 }
789 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))->
790 set_sensitive(group_selected || sample_selected);
791 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))->
792 set_sensitive(group_selected || sample_selected);
793 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))->
794 set_sensitive(file);
795 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))->
796 set_sensitive(group_selected || sample_selected);
797 // show sample popup
798 sample_popup->popup(button->button, button->time);
799 }
800 }
801
802 void MainWindow::on_action_sample_properties() {
803 //TODO: show a dialog where the selected sample's properties can be edited
804 }
805
806 void MainWindow::on_action_add_group() {
807 static int __sample_indexer = 0;
808 if (!file) return;
809 gig::Group* group = file->AddGroup();
810 group->Name = "Unnamed Group";
811 if (__sample_indexer) group->Name += " " + ToString(__sample_indexer);
812 __sample_indexer++;
813 // update sample tree view
814 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
815 Gtk::TreeModel::Row rowGroup = *iterGroup;
816 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
817 rowGroup[m_SamplesModel.m_col_sample] = NULL;
818 rowGroup[m_SamplesModel.m_col_group] = group;
819 }
820
821 void MainWindow::on_action_add_sample() {
822 if (!file) return;
823 // get selected group
824 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
825 Gtk::TreeModel::iterator it = sel->get_selected();
826 if (!it) return;
827 Gtk::TreeModel::Row row = *it;
828 gig::Group* group = row[m_SamplesModel.m_col_group];
829 if (!group) { // not a group, but a sample is selected (probably)
830 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
831 if (!sample) return;
832 it = row.parent(); // resolve parent (that is the sample's group)
833 if (!it) return;
834 row = *it;
835 group = row[m_SamplesModel.m_col_group];
836 if (!group) return;
837 }
838 // show 'browse for file' dialog
839 Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)"));
840 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
841 dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
842 dialog.set_select_multiple(true);
843 Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile
844 const char* supportedFileTypes[] = {
845 "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd",
846 "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF",
847 "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64",
848 "*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK",
849 "*.caf", "*.CAF", NULL
850 };
851 for (int i = 0; supportedFileTypes[i]; i++)
852 soundfilter.add_pattern(supportedFileTypes[i]);
853 soundfilter.set_name("Sound Files");
854 Gtk::FileFilter allpassfilter; // matches every file
855 allpassfilter.add_pattern("*.*");
856 allpassfilter.set_name("All Files");
857 dialog.add_filter(soundfilter);
858 dialog.add_filter(allpassfilter);
859 if (dialog.run() == Gtk::RESPONSE_OK) {
860 Glib::ustring error_files;
861 Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();
862 for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();
863 iter != filenames.end(); ++iter) {
864 printf("Adding sample %s\n",(*iter).c_str());
865 // use libsndfile to retrieve file informations
866 SF_INFO info;
867 info.format = 0;
868 SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info);
869 try {
870 if (!hFile) throw std::string("could not open file");
871 int bitdepth;
872 switch (info.format & 0xff) {
873 case SF_FORMAT_PCM_S8:
874 bitdepth = 16; // we simply convert to 16 bit for now
875 break;
876 case SF_FORMAT_PCM_16:
877 bitdepth = 16;
878 break;
879 case SF_FORMAT_PCM_24:
880 bitdepth = 32; // we simply convert to 32 bit for now
881 break;
882 case SF_FORMAT_PCM_32:
883 bitdepth = 32;
884 break;
885 case SF_FORMAT_PCM_U8:
886 bitdepth = 16; // we simply convert to 16 bit for now
887 break;
888 case SF_FORMAT_FLOAT:
889 bitdepth = 32;
890 break;
891 case SF_FORMAT_DOUBLE:
892 bitdepth = 32; // I guess we will always truncate this to 32 bit
893 break;
894 default:
895 sf_close(hFile); // close sound file
896 throw std::string("format not supported"); // unsupported subformat (yet?)
897 }
898 // add a new sample to the .gig file
899 gig::Sample* sample = file->AddSample();
900 // file name without path
901 sample->pInfo->Name = (*iter).substr((*iter).rfind('/') + 1).raw();
902 sample->Channels = info.channels;
903 sample->BitDepth = bitdepth;
904 sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels;
905 sample->SamplesPerSecond = info.samplerate;
906 // schedule resizing the sample (which will be done
907 // physically when File::Save() is called)
908 sample->Resize(info.frames);
909 // make sure sample is part of the selected group
910 group->AddSample(sample);
911 // schedule that physical resize and sample import
912 // (data copying), performed when "Save" is requested
913 SampleImportItem sched_item;
914 sched_item.gig_sample = sample;
915 sched_item.sample_path = *iter;
916 m_SampleImportQueue.push_back(sched_item);
917 // add sample to the tree view
918 Gtk::TreeModel::iterator iterSample =
919 m_refSamplesTreeModel->append(row.children());
920 Gtk::TreeModel::Row rowSample = *iterSample;
921 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
922 rowSample[m_SamplesModel.m_col_sample] = sample;
923 rowSample[m_SamplesModel.m_col_group] = NULL;
924 // close sound file
925 sf_close(hFile);
926 } catch (std::string what) { // remember the files that made trouble (and their cause)
927 if (error_files.size()) error_files += "\n";
928 error_files += *iter += " (" + what + ")";
929 }
930 }
931 // show error message box when some file(s) could not be opened / added
932 if (error_files.size()) {
933 Glib::ustring txt = "Could not add the following sample(s):\n" + error_files;
934 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
935 msg.run();
936 }
937 }
938 }
939
940 void MainWindow::on_action_remove_sample() {
941 if (!file) return;
942 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
943 Gtk::TreeModel::iterator it = sel->get_selected();
944 if (it) {
945 Gtk::TreeModel::Row row = *it;
946 gig::Group* group = row[m_SamplesModel.m_col_group];
947 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
948 Glib::ustring name = row[m_SamplesModel.m_col_name];
949 try {
950 // remove group or sample from the gig file
951 if (group) {
952 // temporarily remember the samples that bolong to
953 // that group (we need that to clean the queue)
954 std::list<gig::Sample*> members;
955 for (gig::Sample* pSample = group->GetFirstSample();
956 pSample; pSample = group->GetNextSample()) {
957 members.push_back(pSample);
958 }
959 // delete the group in the .gig file including the
960 // samples that belong to the group
961 file->DeleteGroup(group);
962 // if sample(s) were just previously added, remove
963 // them from the import queue
964 for (std::list<gig::Sample*>::iterator member = members.begin();
965 member != members.end(); ++member) {
966 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
967 iter != m_SampleImportQueue.end(); ++iter) {
968 if ((*iter).gig_sample == *member) {
969 printf("Removing previously added sample '%s' from group '%s'\n",
970 (*iter).sample_path.c_str(), name.c_str());
971 m_SampleImportQueue.erase(iter);
972 break;
973 }
974 }
975 }
976 } else if (sample) {
977 // remove sample from the .gig file
978 file->DeleteSample(sample);
979 // if sample was just previously added, remove it from
980 // the import queue
981 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
982 iter != m_SampleImportQueue.end(); ++iter) {
983 if ((*iter).gig_sample == sample) {
984 printf("Removing previously added sample '%s'\n",
985 (*iter).sample_path.c_str());
986 m_SampleImportQueue.erase(iter);
987 break;
988 }
989 }
990 }
991 // remove respective row(s) from samples tree view
992 m_refSamplesTreeModel->erase(it);
993 } catch (RIFF::Exception e) {
994 Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR);
995 msg.run();
996 }
997 }
998 }
999
1000 void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&,
1001 Gtk::SelectionData& selection_data, guint, guint)
1002 {
1003 // get selected sample
1004 gig::Sample* sample = NULL;
1005 Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection();
1006 Gtk::TreeModel::iterator it = sel->get_selected();
1007 if (it) {
1008 Gtk::TreeModel::Row row = *it;
1009 sample = row[m_SamplesModel.m_col_sample];
1010 }
1011 // pass the gig::Sample as pointer
1012 selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample,
1013 sizeof(sample)/*length of data in bytes*/);
1014 }
1015
1016 void MainWindow::on_sample_label_drop_drag_data_received(
1017 const Glib::RefPtr<Gdk::DragContext>& context, int, int,
1018 const Gtk::SelectionData& selection_data, guint, guint time)
1019 {
1020 gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion();
1021 gig::Sample* sample = *((gig::Sample**) selection_data.get_data());
1022
1023 if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) {
1024 if (sample != dimregion->pSample) {
1025 dimregion->pSample = sample;
1026 dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str());
1027 std::cout << "Drop received sample \"" <<
1028 dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl;
1029 // drop success
1030 context->drop_reply(true, time);
1031 return;
1032 }
1033 }
1034 // drop failed
1035 context->drop_reply(false, time);
1036 }
1037
1038 void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path,
1039 const Gtk::TreeModel::iterator& iter) {
1040 if (!iter) return;
1041 Gtk::TreeModel::Row row = *iter;
1042 Glib::ustring name = row[m_SamplesModel.m_col_name];
1043 gig::Group* group = row[m_SamplesModel.m_col_group];
1044 gig::Sample* sample = row[m_SamplesModel.m_col_sample];
1045 if (group) {
1046 group->Name = name;
1047 std::cout << "Group name changed\n" << std::flush;
1048 } else if (sample) {
1049 sample->pInfo->Name = name.raw();
1050 std::cout << "Sample name changed\n" << std::flush;
1051 }
1052 }
1053
1054 void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path,
1055 const Gtk::TreeModel::iterator& iter) {
1056 std::cout << "Instrument name changed\n" << std::flush;
1057 if (!iter) return;
1058 Gtk::TreeModel::Row row = *iter;
1059 Glib::ustring name = row[m_Columns.m_col_name];
1060 gig::Instrument* instrument = row[m_Columns.m_col_instr];
1061 if (instrument) instrument->pInfo->Name = name.raw();
1062 }

  ViewVC Help
Powered by ViewVC