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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1104 - (show annotations) (download)
Sun Mar 18 17:15:00 2007 UTC (17 years, 1 month ago) by persson
File size: 42143 byte(s)
* DimRegionEdit is now disbaled when no region is selected (fixes
  crash for "new file")
* instrument is selected after it is created
* minor code cleanup

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 Gtk::TreeModel::Row row = *it;
271 std::cout << row[m_Columns.m_col_name] << std::endl;
272
273 m_RegionChooser.set_instrument(row[m_Columns.m_col_instr]);
274 } else {
275 m_RegionChooser.set_instrument(0);
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 load_dialog->hide();
422 }
423
424 void MainWindow::on_action_file_save()
425 {
426 if (!file) return;
427 std::cout << "Saving file\n" << std::flush;
428 try {
429 file->Save();
430 } catch (RIFF::Exception e) {
431 Glib::ustring txt = "Could not save file: " + e.Message;
432 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
433 msg.run();
434 return;
435 }
436 std::cout << "Saving file done\n" << std::flush;
437 __import_queued_samples();
438 }
439
440 void MainWindow::on_action_file_save_as()
441 {
442 if (!file) return;
443 Gtk::FileChooserDialog dialog(*this, "Open", Gtk::FILE_CHOOSER_ACTION_SAVE);
444 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
445 dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
446 Gtk::FileFilter filter;
447 filter.add_pattern("*.gig");
448 dialog.set_filter(filter);
449 if (dialog.run() == Gtk::RESPONSE_OK) {
450 printf("filename=%s\n", dialog.get_filename().c_str());
451 try {
452 file->Save(dialog.get_filename());
453 } catch (RIFF::Exception e) {
454 Glib::ustring txt = "Could not save file: " + e.Message;
455 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
456 msg.run();
457 return;
458 }
459 __import_queued_samples();
460 }
461 }
462
463 // actually write the sample(s)' data to the gig file
464 void MainWindow::__import_queued_samples() {
465 std::cout << "Starting sample import\n" << std::flush;
466 Glib::ustring error_files;
467 printf("Samples to import: %d\n", m_SampleImportQueue.size());
468 for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin();
469 iter != m_SampleImportQueue.end(); ) {
470 printf("Importing sample %s\n",(*iter).sample_path.c_str());
471 SF_INFO info;
472 info.format = 0;
473 SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info);
474 try {
475 if (!hFile) throw std::string("could not open file");
476 // determine sample's bit depth
477 int bitdepth;
478 switch (info.format & 0xff) {
479 case SF_FORMAT_PCM_S8:
480 bitdepth = 16; // we simply convert to 16 bit for now
481 break;
482 case SF_FORMAT_PCM_16:
483 bitdepth = 16;
484 break;
485 case SF_FORMAT_PCM_24:
486 bitdepth = 32; // we simply convert to 32 bit for now
487 break;
488 case SF_FORMAT_PCM_32:
489 bitdepth = 32;
490 break;
491 case SF_FORMAT_PCM_U8:
492 bitdepth = 16; // we simply convert to 16 bit for now
493 break;
494 case SF_FORMAT_FLOAT:
495 bitdepth = 32;
496 break;
497 case SF_FORMAT_DOUBLE:
498 bitdepth = 32; // I guess we will always truncate this to 32 bit
499 break;
500 default:
501 sf_close(hFile); // close sound file
502 throw std::string("format not supported"); // unsupported subformat (yet?)
503 }
504 // allocate appropriate copy buffer (TODO: for now we copy
505 // it in one piece, might be tough for very long samples)
506 // and copy sample data into buffer
507 int8_t* buffer = NULL;
508 switch (bitdepth) {
509 case 16:
510 buffer = new int8_t[2 * info.channels * info.frames];
511 // libsndfile does the conversion for us (if needed)
512 sf_readf_short(hFile, (short*) buffer, info.frames);
513 break;
514 case 32:
515 buffer = new int8_t[4 * info.channels * info.frames];
516 // libsndfile does the conversion for us (if needed)
517 sf_readf_int(hFile, (int*) buffer, info.frames);
518 break;
519 }
520 // write from buffer directly (physically) into .gig file
521 (*iter).gig_sample->Write(buffer, info.frames);
522 // cleanup
523 sf_close(hFile);
524 delete[] buffer;
525 // on success we remove the sample from the import queue,
526 // otherwise keep it, maybe it works the next time ?
527 std::list<SampleImportItem>::iterator cur = iter;
528 ++iter;
529 m_SampleImportQueue.erase(cur);
530 } catch (std::string what) {
531 // remember the files that made trouble (and their cause)
532 if (error_files.size()) error_files += "\n";
533 error_files += (*iter).sample_path += " (" + what + ")";
534 ++iter;
535 }
536 }
537 // show error message box when some sample(s) could not be imported
538 if (error_files.size()) {
539 Glib::ustring txt = "Could not import the following sample(s):\n" + error_files;
540 Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
541 msg.run();
542 }
543 }
544
545 void MainWindow::on_action_file_properties()
546 {
547 propDialog.show();
548 propDialog.deiconify();
549 }
550
551 void MainWindow::on_action_help_about()
552 {
553 #ifdef ABOUT_DIALOG
554 Gtk::AboutDialog dialog;
555 dialog.set_version(VERSION);
556 dialog.run();
557 #endif
558 }
559
560 PropDialog::PropDialog()
561 : table(2,1)
562 {
563 table.set_col_spacings(5);
564 char* propLabels[] = {
565 "Name:",
566 "CreationDate:",
567 "Comments:", // TODO: multiline
568 "Product:",
569 "Copyright:",
570 "Artists:",
571 "Genre:",
572 "Keywords:",
573 "Engineer:",
574 "Technician:",
575 "Software:", // TODO: readonly
576 "Medium:",
577 "Source:",
578 "SourceForm:",
579 "Commissioned:",
580 "Subject:"
581 };
582 for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
583 label[i].set_text(propLabels[i]);
584 label[i].set_alignment(Gtk::ALIGN_LEFT);
585 table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
586 table.attach(entry[i], 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND,
587 Gtk::SHRINK);
588 }
589
590 add(table);
591 // add_button(Gtk::Stock::CANCEL, 0);
592 // add_button(Gtk::Stock::OK, 1);
593 show_all_children();
594 }
595
596 void PropDialog::set_info(DLS::Info* info)
597 {
598 entry[0].set_text(info->Name);
599 entry[1].set_text(info->CreationDate);
600 entry[2].set_text(Glib::convert(info->Comments, "UTF-8", "ISO-8859-1"));
601 entry[3].set_text(info->Product);
602 entry[4].set_text(info->Copyright);
603 entry[5].set_text(info->Artists);
604 entry[6].set_text(info->Genre);
605 entry[7].set_text(info->Keywords);
606 entry[8].set_text(info->Engineer);
607 entry[9].set_text(info->Technician);
608 entry[10].set_text(info->Software);
609 entry[11].set_text(info->Medium);
610 entry[12].set_text(info->Source);
611 entry[13].set_text(info->SourceForm);
612 entry[14].set_text(info->Commissioned);
613 entry[15].set_text(info->Subject);
614 }
615
616
617 InstrumentProps::InstrumentProps()
618 : table(2,1),
619 quitButton(Gtk::Stock::CLOSE)
620 {
621 table.set_col_spacings(5);
622 char* propLabels[] = {
623 "Name:",
624 "IsDrum:",
625 "MIDIBank:",
626 "MIDIProgram:",
627 "Attenuation:",
628 "EffectSend:",
629 "FineTune:",
630 "PitchbendRange:",
631 "PianoReleaseMode:",
632 "DimensionKeyRange:",
633 };
634 int entryIdx = 0, checkIdx = 0;
635 for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) {
636 label[i].set_text(propLabels[i]);
637 label[i].set_alignment(Gtk::ALIGN_LEFT);
638 table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK);
639 if (i == 1 || i == 8)
640 table.attach(check[checkIdx++], 1, 2, i, i + 1,
641 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
642 else
643 table.attach(entry[entryIdx++], 1, 2, i, i + 1,
644 Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
645 }
646
647 // vbox { table buttonBox { quitButton } }
648
649 //get_vbox()->pack_start(table);
650 // set_border_width(6);
651 add(vbox);
652 table.set_border_width(2);
653 vbox.pack_start(table);
654 table.show();
655 vbox.pack_start(buttonBox);
656 buttonBox.set_layout(Gtk::BUTTONBOX_END);
657 buttonBox.set_border_width(5);
658 buttonBox.show();
659 buttonBox.pack_start(quitButton);
660 quitButton.set_flags(Gtk::CAN_DEFAULT);
661 quitButton.grab_focus();
662
663 quitButton.signal_clicked().connect(
664 sigc::mem_fun(*this, &InstrumentProps::hide));
665
666 // quitButton.grab_default();
667 quitButton.show();
668 // add(table);
669 vbox.show();
670 show_all_children();
671 }
672
673 extern char* notes[];
674
675 void InstrumentProps::set_instrument(gig::Instrument* instrument)
676 {
677 char buf[100];
678
679 int entryIdx = 0, checkIdx = 0;
680 entry[entryIdx++].set_text(instrument->pInfo->Name);
681 check[checkIdx++].set_active(instrument->IsDrum);
682 sprintf(buf, "%d", instrument->MIDIBank);
683 entry[entryIdx++].set_text(buf);
684 sprintf(buf, "%d", instrument->MIDIProgram);
685 entry[entryIdx++].set_text(buf);
686 sprintf(buf, "%d", instrument->Attenuation);
687 entry[entryIdx++].set_text(buf);
688 sprintf(buf, "%d", instrument->EffectSend);
689 entry[entryIdx++].set_text(buf);
690 sprintf(buf, "%d", instrument->FineTune);
691 entry[entryIdx++].set_text(buf);
692 sprintf(buf, "%d", instrument->PitchbendRange);
693 entry[entryIdx++].set_text(buf);
694 check[checkIdx++].set_active(instrument->PianoReleaseMode);
695 sprintf(buf, "%s%d (%d)..%s%d (%d)",
696 notes[instrument->DimensionKeyRange.low % 12],
697 instrument->DimensionKeyRange.low / 12 - 1,
698 instrument->DimensionKeyRange.low,
699 notes[instrument->DimensionKeyRange.high % 12],
700 instrument->DimensionKeyRange.high / 12 - 1,
701 instrument->DimensionKeyRange.high);
702 entry[entryIdx].set_text(buf);
703 }
704
705 void MainWindow::load_gig(gig::File* gig, const char* filename)
706 {
707 file = gig;
708
709 if (filename) {
710 const char *basename = strrchr(filename, '/');
711 basename = basename ? basename + 1 : filename;
712 set_title(basename);
713 } else {
714 set_title("unnamed");
715 }
716
717 propDialog.set_info(gig->pInfo);
718
719 Gtk::MenuItem* instrument_menu =
720 dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument"));
721
722 int instrument_index = 0;
723 Gtk::RadioMenuItem::Group instrument_group;
724 for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ;
725 instrument = gig->GetNextInstrument()) {
726 Gtk::TreeModel::iterator iter = m_refTreeModel->append();
727 Gtk::TreeModel::Row row = *iter;
728 row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str();
729 row[m_Columns.m_col_instr] = instrument;
730 // create a menu item for this instrument
731 Gtk::RadioMenuItem* item =
732 new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str());
733 instrument_menu->get_submenu()->append(*item);
734 item->signal_activate().connect(
735 sigc::bind(
736 sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change),
737 instrument_index
738 )
739 );
740 instrument_index++;
741 }
742 instrument_menu->show();
743 instrument_menu->get_submenu()->show_all_children();
744
745 for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) {
746 if (group->Name != "") {
747 Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append();
748 Gtk::TreeModel::Row rowGroup = *iterGroup;
749 rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str();
750 rowGroup[m_SamplesModel.m_col_group] = group;
751 rowGroup[m_SamplesModel.m_col_sample] = NULL;
752 for (gig::Sample* sample = group->GetFirstSample();
753 sample; sample = group->GetNextSample()) {
754 Gtk::TreeModel::iterator iterSample =
755 m_refSamplesTreeModel->append(rowGroup.children());
756 Gtk::TreeModel::Row rowSample = *iterSample;
757 rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str();
758 rowSample[m_SamplesModel.m_col_sample] = sample;
759 rowSample[m_SamplesModel.m_col_group] = NULL;
760 }
761 }
762 }
763
764 // select the first instrument
765 Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection();
766 tree_sel_ref->select(Gtk::TreePath("0"));
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