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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1101 - (show annotations) (download)
Sat Mar 17 17:46:09 2007 UTC (17 years ago) by schoenebeck
File size: 42025 byte(s)
* implemented creating a new .gig file
  (Menu->File->New)
* implemented adding a new instrument
  (right-click popup in instruments treeview)
* implemented removing an instrument
  (right-click popup in instruments treeview)

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

  ViewVC Help
Powered by ViewVC