--- gigedit/trunk/src/gigedit/mainwindow.cpp 2015/01/06 18:11:27 2695 +++ gigedit/trunk/src/gigedit/mainwindow.cpp 2016/07/18 11:25:13 2968 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2015 Andreas Persson + * Copyright (C) 2006-2016 Andreas Persson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,6 +20,14 @@ #include #include +#include +// threads.h must be included first to be able to build with +// G_DISABLE_DEPRECATED +#if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION == 31 && GLIBMM_MICRO_VERSION >= 2) || \ + (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION > 31) || GLIBMM_MAJOR_VERSION > 2 +#include +#endif + #include #include #include @@ -27,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -110,30 +117,28 @@ actionGroup = Gtk::ActionGroup::create(); actionGroup->add(Gtk::Action::create("MenuFile", _("_File"))); - actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW), + actionGroup->add(Gtk::Action::create("New", _("_New")), + Gtk::AccelKey("n"), sigc::mem_fun( *this, &MainWindow::on_action_file_new)); - Glib::RefPtr action = - Gtk::Action::create("Open", Gtk::Stock::OPEN); - action->property_label() = action->property_label() + "..."; - actionGroup->add(action, + actionGroup->add(Gtk::Action::create("Open", _("_Open...")), + Gtk::AccelKey("o"), sigc::mem_fun( *this, &MainWindow::on_action_file_open)); - actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE), + actionGroup->add(Gtk::Action::create("Save", _("_Save")), + Gtk::AccelKey("s"), sigc::mem_fun( *this, &MainWindow::on_action_file_save)); - action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS); - action->property_label() = action->property_label() + "..."; - actionGroup->add(action, + actionGroup->add(Gtk::Action::create("SaveAs", _("Save _As...")), Gtk::AccelKey("s"), sigc::mem_fun( *this, &MainWindow::on_action_file_save_as)); actionGroup->add(Gtk::Action::create("Properties", - Gtk::Stock::PROPERTIES), + _("_Properties")), sigc::mem_fun( *this, &MainWindow::on_action_file_properties)); actionGroup->add(Gtk::Action::create("InstrProperties", - Gtk::Stock::PROPERTIES), + _("_Properties")), sigc::mem_fun( *this, &MainWindow::show_instr_props)); actionGroup->add(Gtk::Action::create("MidiRules", @@ -144,7 +149,8 @@ _("_Script Slots...")), sigc::mem_fun( *this, &MainWindow::show_script_slots)); - actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT), + actionGroup->add(Gtk::Action::create("Quit", _("_Quit")), + Gtk::AccelKey("q"), sigc::mem_fun( *this, &MainWindow::on_action_quit)); actionGroup->add( @@ -187,10 +193,27 @@ sigc::mem_fun( *this, &MainWindow::on_action_view_status_bar)); - action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP); - actionGroup->add(Gtk::Action::create("MenuHelp", - action->property_label())); - actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT), + toggle_action = + Gtk::ToggleAction::create("AutoRestoreWinDim", _("_Auto Restore Window Dimension")); + toggle_action->set_active(Settings::singleton()->autoRestoreWindowDimension); + actionGroup->add(toggle_action, + sigc::mem_fun( + *this, &MainWindow::on_auto_restore_win_dim)); + + toggle_action = + Gtk::ToggleAction::create("SaveWithTemporaryFile", _("Save with _temporary file")); + toggle_action->set_active(Settings::singleton()->saveWithTemporaryFile); + actionGroup->add(toggle_action, + sigc::mem_fun( + *this, &MainWindow::on_save_with_temporary_file)); + + actionGroup->add( + Gtk::Action::create("RefreshAll", _("_Refresh All")), + sigc::mem_fun(*this, &MainWindow::on_action_refresh_all) + ); + + actionGroup->add(Gtk::Action::create("MenuHelp", _("_Help"))); + actionGroup->add(Gtk::Action::create("About", _("_About")), sigc::mem_fun( *this, &MainWindow::on_action_help_about)); actionGroup->add( @@ -202,7 +225,7 @@ sigc::mem_fun(*this, &MainWindow::on_action_duplicate_instrument) ); actionGroup->add( - Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE), + Gtk::Action::create("RemoveInstrument", _("_Remove")), sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument) ); @@ -225,6 +248,14 @@ sigc::mem_fun(*this, &MainWindow::on_action_sync_sampler_instrument_selection) ); + toggle_action = + Gtk::ToggleAction::create("MoveRootNoteWithRegionMoved", _("Move root note with region moved")); + toggle_action->set_active(Settings::singleton()->moveRootNoteWithRegionMoved); + actionGroup->add( + toggle_action, + sigc::mem_fun(*this, &MainWindow::on_action_move_root_note_with_region_moved) + ); + actionGroup->add(Gtk::Action::create("MenuTools", _("_Tools"))); @@ -241,7 +272,7 @@ // sample right-click popup actions actionGroup->add( - Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES), + Gtk::Action::create("SampleProperties", _("_Properties")), sigc::mem_fun(*this, &MainWindow::on_action_sample_properties) ); actionGroup->add( @@ -253,14 +284,23 @@ sigc::mem_fun(*this, &MainWindow::on_action_add_sample) ); actionGroup->add( - Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE), + Gtk::Action::create("RemoveSample", _("_Remove")), sigc::mem_fun(*this, &MainWindow::on_action_remove_sample) ); actionGroup->add( + Gtk::Action::create("RemoveUnusedSamples", _("Remove _Unused Samples")), + sigc::mem_fun(*this, &MainWindow::on_action_remove_unused_samples) + ); + actionGroup->add( Gtk::Action::create("ShowSampleRefs", _("Show References...")), sigc::mem_fun(*this, &MainWindow::on_action_view_references) ); actionGroup->add( + Gtk::Action::create("ReplaceSample", + _("Replace Sample...")), + sigc::mem_fun(*this, &MainWindow::on_action_replace_sample) + ); + actionGroup->add( Gtk::Action::create("ReplaceAllSamplesInAllGroups", _("Replace All Samples in All Groups...")), sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups) @@ -280,7 +320,7 @@ sigc::mem_fun(*this, &MainWindow::on_action_edit_script) ); actionGroup->add( - Gtk::Action::create("RemoveScript", Gtk::Stock::REMOVE), + Gtk::Action::create("RemoveScript", _("_Remove")), sigc::mem_fun(*this, &MainWindow::on_action_remove_script) ); @@ -312,9 +352,11 @@ " " " " " " + " " " " " " " " + " " " " " " " " @@ -337,6 +379,9 @@ " " " " " " + " " + " " + " " " " " " " " @@ -345,6 +390,8 @@ " " " " " " + " " + " " " " " " " " @@ -364,9 +411,11 @@ " " " " " " + " " " " " " " " + " " " " " " " " @@ -411,6 +460,30 @@ } { Gtk::MenuItem* item = dynamic_cast( + uiManager->get_widget("/MenuBar/MenuSettings/MoveRootNoteWithRegionMoved")); + item->set_tooltip_text(_("If checked, and when a region is moved by dragging it around on the virtual keyboard, the keybord position dependent pitch will move exactly with the amount of semi tones the region was moved around.")); + } + { + Gtk::MenuItem* item = dynamic_cast( + uiManager->get_widget("/MenuBar/MenuSample/RemoveUnusedSamples")); + item->set_tooltip_text(_("Removes all samples that are not referenced by any instrument (i.e. red ones).")); + // copy tooltip to popup menu + Gtk::MenuItem* item2 = dynamic_cast( + uiManager->get_widget("/SamplePopupMenu/RemoveUnusedSamples")); + item2->set_tooltip_text(item->get_tooltip_text()); + } + { + Gtk::MenuItem* item = dynamic_cast( + uiManager->get_widget("/MenuBar/MenuView/RefreshAll")); + item->set_tooltip_text(_("Reloads the currently open gig file and updates the entire graphical user interface.")); + } + { + Gtk::MenuItem* item = dynamic_cast( + uiManager->get_widget("/MenuBar/MenuView/AutoRestoreWinDim")); + item->set_tooltip_text(_("If checked, size and position of all windows will be saved and automatically restored next time.")); + } + { + Gtk::MenuItem* item = dynamic_cast( uiManager->get_widget("/MenuBar/MenuTools/CombineInstruments")); item->set_tooltip_text(_("Create combi sounds out of individual sounds of this .gig file.")); } @@ -448,7 +521,7 @@ // Create the Tree model: m_refTreeModel = Gtk::ListStore::create(m_Columns); m_TreeView.set_model(m_refTreeModel); - m_TreeView.set_tooltip_text(_("Right click here for actions on instruments & MIDI Rules.")); + m_TreeView.set_tooltip_text(_("Right click here for actions on instruments & MIDI Rules. Drag & drop to change the order of instruments.")); instrument_name_connection = m_refTreeModel->signal_row_changed().connect( sigc::mem_fun(*this, &MainWindow::instrument_name_changed) ); @@ -456,6 +529,24 @@ // Add the TreeView's view columns: m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name); m_TreeView.set_headers_visible(false); + + // establish drag&drop within the instrument tree view, allowing to reorder + // the sequence of instruments within the gig file + { + std::vector drag_target_instrument; + drag_target_instrument.push_back(Gtk::TargetEntry("gig::Instrument")); + m_TreeView.drag_source_set(drag_target_instrument); + m_TreeView.drag_dest_set(drag_target_instrument); + m_TreeView.signal_drag_begin().connect( + sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_begin) + ); + m_TreeView.signal_drag_data_get().connect( + sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drag_data_get) + ); + m_TreeView.signal_drag_data_received().connect( + sigc::mem_fun(*this, &MainWindow::on_instruments_treeview_drop_drag_data_received) + ); + } // create samples treeview (including its data model) m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel); @@ -540,6 +631,8 @@ sigc::hide(sigc::mem_fun(*this, &MainWindow::file_changed))); m_RegionChooser.signal_instrument_changed().connect( sigc::mem_fun(*this, &MainWindow::file_changed)); + m_RegionChooser.signal_instrument_changed().connect( + sigc::mem_fun(*this, &MainWindow::region_changed)); m_DimRegionChooser.signal_region_changed().connect( sigc::mem_fun(*this, &MainWindow::file_changed)); instrumentProps.signal_changed().connect( @@ -729,7 +822,8 @@ void Loader::thread_function() { - printf("thread_function self=%x\n", Glib::Threads::Thread::self()); + printf("thread_function self=%p\n", + static_cast(Glib::Threads::Thread::self())); printf("Start %s\n", filename.c_str()); try { RIFF::File* riff = new RIFF::File(filename); @@ -751,7 +845,7 @@ } Loader::Loader(const char* filename) - : filename(filename), thread(0), progress(0.f) + : filename(filename), gig(0), thread(0), progress(0.f) { } @@ -762,7 +856,7 @@ #else thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Loader::thread_function)); #endif - printf("launch thread=%x\n", thread); + printf("launch thread=%p\n", static_cast(thread)); } float Loader::get_progress() @@ -807,7 +901,8 @@ void Saver::thread_function() { - printf("thread_function self=%x\n", Glib::Threads::Thread::self()); + printf("thread_function self=%p\n", + static_cast(Glib::Threads::Thread::self())); printf("Start %s\n", filename.c_str()); try { gig::progress_t progress; @@ -816,7 +911,34 @@ // if no filename was provided, that means "save", if filename was provided means "save as" if (filename.empty()) { - gig->Save(&progress); + if (!Settings::singleton()->saveWithTemporaryFile) { + // save directly over the existing .gig file + // (requires less disk space than solution below + // but may be slower) + gig->Save(&progress); + } else { + // save the file as separate temporary file first, + // then move the saved file over the old file + // (may result in performance speedup during save) + String tmpname = filename + ".TMP"; + gig->Save(tmpname, &progress); + #if defined(WIN32) + if (!DeleteFile(filename.c_str())) { + throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file)."); + } + #else // POSIX ... + if (unlink(filename.c_str())) { + throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file): " + String(strerror(errno))); + } + #endif + if (rename(tmpname.c_str(), filename.c_str())) { + #if defined(WIN32) + throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file)."); + #else + throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file): " + String(strerror(errno))); + #endif + } + } } else { gig->Save(filename, &progress); } @@ -844,7 +966,7 @@ #else thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Saver::thread_function)); #endif - printf("launch thread=%x\n", thread); + printf("launch thread=%p\n", static_cast(thread)); } float Saver::get_progress() @@ -941,8 +1063,8 @@ g_free(msg); dialog.set_secondary_text(_("If you close without saving, your changes will be lost.")); dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO); - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); - dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(file_has_name ? _("_Save") : _("Save _As"), Gtk::RESPONSE_YES); dialog.set_default_response(Gtk::RESPONSE_YES); int response = dialog.run(); dialog.hide(); @@ -973,7 +1095,7 @@ "used by the sampler until you tell the sampler explicitly to " "load it.")); dialog.add_button(_("_Yes, Detach"), Gtk::RESPONSE_YES); - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); dialog.set_default_response(Gtk::RESPONSE_CANCEL); int response = dialog.run(); dialog.hide(); @@ -987,8 +1109,8 @@ if (file_is_shared && !leaving_shared_mode_dialog()) return; Gtk::FileChooserDialog dialog(*this, _("Open file")); - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); - dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(_("_Open"), Gtk::RESPONSE_OK); dialog.set_default_response(Gtk::RESPONSE_OK); #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 Gtk::FileFilter filter; @@ -1004,7 +1126,8 @@ if (dialog.run() == Gtk::RESPONSE_OK) { std::string filename = dialog.get_filename(); printf("filename=%s\n", filename.c_str()); - printf("on_action_file_open self=%x\n", Glib::Threads::Thread::self()); + printf("on_action_file_open self=%p\n", + static_cast(Glib::Threads::Thread::self())); load_file(filename.c_str()); current_gig_dir = Glib::path_get_dirname(filename); } @@ -1074,7 +1197,8 @@ void MainWindow::on_loader_finished() { printf("Loader finished!\n"); - printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self()); + printf("on_loader_finished self=%p\n", + static_cast(Glib::Threads::Thread::self())); load_gig(loader->gig, loader->filename.c_str()); progress_dialog->hide(); } @@ -1169,7 +1293,7 @@ file_structure_changed_signal.emit(this->file); - load_gig(this->file, this->filename.c_str()); + __refreshEntireGUI(); progress_dialog->hide(); } @@ -1181,9 +1305,9 @@ bool MainWindow::file_save_as() { - Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE); - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); - dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); + Gtk::FileChooserDialog dialog(*this, _("Save As"), Gtk::FILE_CHOOSER_ACTION_SAVE); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(_("_Save"), Gtk::RESPONSE_OK); dialog.set_default_response(Gtk::RESPONSE_OK); dialog.set_do_overwrite_confirmation(); @@ -1214,7 +1338,9 @@ // show warning in the dialog Gtk::HBox descriptionArea; descriptionArea.set_spacing(15); - Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG)); + Gtk::Image warningIcon; + warningIcon.set_from_icon_name("dialog-warning", + Gtk::IconSize(Gtk::ICON_SIZE_DIALOG)); descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK); #if GTKMM_MAJOR_VERSION < 3 view::WrapLabel description; @@ -1266,7 +1392,7 @@ void MainWindow::__import_queued_samples() { std::cout << "Starting sample import\n" << std::flush; Glib::ustring error_files; - printf("Samples to import: %d\n", m_SampleImportQueue.size()); + printf("Samples to import: %d\n", int(m_SampleImportQueue.size())); for (std::list::iterator iter = m_SampleImportQueue.begin(); iter != m_SampleImportQueue.end(); ) { printf("Importing sample %s\n",(*iter).sample_path.c_str()); @@ -1372,6 +1498,11 @@ !Settings::singleton()->syncSamplerInstrumentSelection; } +void MainWindow::on_action_move_root_note_with_region_moved() { + Settings::singleton()->moveRootNoteWithRegionMoved = + !Settings::singleton()->moveRootNoteWithRegionMoved; +} + void MainWindow::on_action_help_about() { Gtk::AboutDialog dialog; @@ -1381,7 +1512,7 @@ dialog.set_name("Gigedit"); #endif dialog.set_version(VERSION); - dialog.set_copyright("Copyright (C) 2006-2015 Andreas Persson"); + dialog.set_copyright("Copyright (C) 2006-2016 Andreas Persson"); const std::string sComment = _("Built " __DATE__ "\nUsing ") + ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" + @@ -1418,7 +1549,7 @@ eSourceForm(_("Source form")), eCommissioned(_("Commissioned")), eSubject(_("Subject")), - quitButton(Gtk::Stock::CLOSE), + quitButton(_("_Close"), true), table(2, 1), m_file(NULL) { @@ -1541,7 +1672,7 @@ } InstrumentProps::InstrumentProps() : - quitButton(Gtk::Stock::CLOSE), + quitButton(_("_Close"), true), table(2,1), eName(_("Name")), eIsDrum(_("Is drum")), @@ -1664,7 +1795,10 @@ file = 0; set_file_is_shared(isSharedInstrument); - this->filename = filename ? filename : _("Unsaved Gig File"); + this->filename = + (filename && strlen(filename) > 0) ? + filename : (!gig->GetFileName().empty()) ? + gig->GetFileName() : _("Unsaved Gig File"); set_title(Glib::filename_display_basename(this->filename)); file_has_name = filename; file_is_changed = false; @@ -1818,6 +1952,10 @@ window->show(); } +void MainWindow::on_action_refresh_all() { + __refreshEntireGUI(); +} + void MainWindow::on_action_view_status_bar() { Gtk::CheckMenuItem* item = dynamic_cast(uiManager->get_widget("/MenuBar/MenuView/Statusbar")); @@ -1829,6 +1967,26 @@ else m_StatusBar.hide(); } +void MainWindow::on_auto_restore_win_dim() { + Gtk::CheckMenuItem* item = + dynamic_cast(uiManager->get_widget("/MenuBar/MenuView/AutoRestoreWinDim")); + if (!item) { + std::cerr << "/MenuBar/MenuView/AutoRestoreWinDim == NULL\n"; + return; + } + Settings::singleton()->autoRestoreWindowDimension = item->get_active(); +} + +void MainWindow::on_save_with_temporary_file() { + Gtk::CheckMenuItem* item = + dynamic_cast(uiManager->get_widget("/MenuBar/MenuSettings/SaveWithTemporaryFile")); + if (!item) { + std::cerr << "/MenuBar/MenuSettings/SaveWithTemporaryFile == NULL\n"; + return; + } + Settings::singleton()->saveWithTemporaryFile = item->get_active(); +} + bool MainWindow::is_copy_samples_unity_note_enabled() const { Gtk::CheckMenuItem* item = dynamic_cast(uiManager->get_widget("/MenuBar/MenuEdit/CopySampleUnity")); @@ -1893,6 +2051,25 @@ } } +void MainWindow::select_instrument(gig::Instrument* instrument) { + if (!instrument) return; + + Glib::RefPtr model = m_TreeView.get_model(); + for (int i = 0; i < model->children().size(); ++i) { + Gtk::TreeModel::Row row = model->children()[i]; + if (row[m_Columns.m_col_instr] == instrument) { + // select and show the respective instrument in the list view + show_intruments_tab(); + m_TreeView.get_selection()->select(model->children()[i]); + Gtk::TreePath path( + m_TreeView.get_selection()->get_selected() + ); + m_TreeView.scroll_to_row(path); + on_sel_change(); // the regular instrument selection change callback + } + } +} + /// Returns true if requested dimension region was successfully selected and scrolled to in the list view, false on error. bool MainWindow::select_dimension_region(gig::DimensionRegion* dimRgn) { gig::Region* pRegion = (gig::Region*) dimRgn->GetParent(); @@ -2232,6 +2409,12 @@ if (!script) return; ScriptEditor* editor = new ScriptEditor; + editor->signal_script_to_be_changed.connect( + signal_script_to_be_changed.make_slot() + ); + editor->signal_script_changed.connect( + signal_script_changed.make_slot() + ); editor->setScript(script); //editor->reparent(*this); editor->show(); @@ -2297,28 +2480,40 @@ file_changed(); } +void MainWindow::on_action_replace_sample() { + add_or_replace_sample(true); +} + void MainWindow::on_action_add_sample() { + add_or_replace_sample(false); +} + +void MainWindow::add_or_replace_sample(bool replace) { if (!file) return; - // get selected group + + // get selected group (and probably selected sample) Glib::RefPtr sel = m_TreeViewSamples.get_selection(); Gtk::TreeModel::iterator it = sel->get_selected(); if (!it) return; Gtk::TreeModel::Row row = *it; + gig::Sample* sample = NULL; gig::Group* group = row[m_SamplesModel.m_col_group]; if (!group) { // not a group, but a sample is selected (probably) - gig::Sample* sample = row[m_SamplesModel.m_col_sample]; - if (!sample) return; + if (replace) sample = row[m_SamplesModel.m_col_sample]; + if (!row[m_SamplesModel.m_col_sample]) return; it = row.parent(); // resolve parent (that is the sample's group) if (!it) return; - row = *it; - group = row[m_SamplesModel.m_col_group]; + if (!replace) row = *it; + group = (*it)[m_SamplesModel.m_col_group]; if (!group) return; } + if (replace && !sample) return; + // show 'browse for file' dialog - Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)")); - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); - dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); - dialog.set_select_multiple(true); + Gtk::FileChooserDialog dialog(*this, replace ? _("Replace Sample with") : _("Add Sample(s)")); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(_("_Open"), Gtk::RESPONSE_OK); + dialog.set_select_multiple(!replace); // allow multi audio file selection only when adding new samples, does not make sense when replacing a specific sample // matches all file types supported by libsndfile #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 @@ -2389,8 +2584,8 @@ sf_close(hFile); // close sound file throw std::string(_("format not supported")); // unsupported subformat (yet?) } - // add a new sample to the .gig file - gig::Sample* sample = file->AddSample(); + // add a new sample to the .gig file (if adding is requested actually) + if (!replace) sample = file->AddSample(); // file name without path Glib::ustring filename = Glib::filename_display_basename(*iter); // remove file extension if there is one @@ -2441,7 +2636,7 @@ // physically when File::Save() is called) sample->Resize(info.frames); // make sure sample is part of the selected group - group->AddSample(sample); + if (!replace) group->AddSample(sample); // schedule that physical resize and sample import // (data copying), performed when "Save" is requested SampleImportItem sched_item; @@ -2449,13 +2644,17 @@ sched_item.sample_path = *iter; m_SampleImportQueue.push_back(sched_item); // add sample to the tree view - Gtk::TreeModel::iterator iterSample = - m_refSamplesTreeModel->append(row.children()); - Gtk::TreeModel::Row rowSample = *iterSample; - rowSample[m_SamplesModel.m_col_name] = - gig_to_utf8(sample->pInfo->Name); - rowSample[m_SamplesModel.m_col_sample] = sample; - rowSample[m_SamplesModel.m_col_group] = NULL; + if (replace) { + row[m_SamplesModel.m_col_name] = gig_to_utf8(sample->pInfo->Name); + } else { + Gtk::TreeModel::iterator iterSample = + m_refSamplesTreeModel->append(row.children()); + Gtk::TreeModel::Row rowSample = *iterSample; + rowSample[m_SamplesModel.m_col_name] = + gig_to_utf8(sample->pInfo->Name); + rowSample[m_SamplesModel.m_col_sample] = sample; + rowSample[m_SamplesModel.m_col_group] = NULL; + } // close sound file sf_close(hFile); file_changed(); @@ -2466,7 +2665,11 @@ } // show error message box when some file(s) could not be opened / added if (!error_files.empty()) { - Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files; + Glib::ustring txt = + (replace + ? _("Failed to replace sample with:\n") + : _("Could not add the following sample(s):\n")) + + error_files; Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); msg.run(); } @@ -2509,7 +2712,7 @@ dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK); description.show(); entryArea.show_all(); - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); dialog.add_button(_("Select"), Gtk::RESPONSE_OK); dialog.set_select_multiple(false); if (current_sample_dir != "") { @@ -2533,18 +2736,14 @@ try { if (!hFile) throw std::string(_("could not open file")); - int bitdepth; switch (info.format & 0xff) { case SF_FORMAT_PCM_S8: case SF_FORMAT_PCM_16: case SF_FORMAT_PCM_U8: - bitdepth = 16; - break; case SF_FORMAT_PCM_24: case SF_FORMAT_PCM_32: case SF_FORMAT_FLOAT: case SF_FORMAT_DOUBLE: - bitdepth = 24; break; default: sf_close(hFile); @@ -2650,6 +2849,71 @@ } } +void MainWindow::on_action_remove_unused_samples() { + if (!file) return; + + // collect all samples that are not referenced by any instrument + std::list lsamples; + for (int iSample = 0; file->GetSample(iSample); ++iSample) { + gig::Sample* sample = file->GetSample(iSample); + bool isUsed = false; + for (gig::Instrument* instrument = file->GetFirstInstrument(); instrument; + instrument = file->GetNextInstrument()) + { + for (gig::Region* rgn = instrument->GetFirstRegion(); rgn; + rgn = instrument->GetNextRegion()) + { + for (int i = 0; i < 256; ++i) { + if (!rgn->pDimensionRegions[i]) continue; + if (rgn->pDimensionRegions[i]->pSample != sample) continue; + isUsed = true; + goto endOfRefSearch; + } + } + } + endOfRefSearch: + if (!isUsed) lsamples.push_back(sample); + } + + if (lsamples.empty()) return; + + // notify everybody that we're going to remove these samples + samples_to_be_removed_signal.emit(lsamples); + + // remove collected samples + try { + for (std::list::iterator itSample = lsamples.begin(); + itSample != lsamples.end(); ++itSample) + { + gig::Sample* sample = *itSample; + // remove sample from the .gig file + file->DeleteSample(sample); + // if sample was just previously added, remove it fro the import queue + for (std::list::iterator iter = m_SampleImportQueue.begin(); + iter != m_SampleImportQueue.end(); ++iter) + { + if ((*iter).gig_sample == sample) { + printf("Removing previously added sample '%s'\n", + (*iter).sample_path.c_str()); + m_SampleImportQueue.erase(iter); + break; + } + } + } + } catch (RIFF::Exception e) { + // show error message + Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR); + msg.run(); + } + + // notify everybody that we're done with removal + samples_removed_signal.emit(); + + dimreg_changed(); + file_changed(); + __refreshEntireGUI(); +} + // see comment on on_sample_treeview_drag_begin() void MainWindow::on_scripts_treeview_drag_begin(const Glib::RefPtr& context) { @@ -2676,6 +2940,62 @@ sizeof(script)/*length of data in bytes*/); } +// see comment on on_sample_treeview_drag_begin() +void MainWindow::on_instruments_treeview_drag_begin(const Glib::RefPtr& context) +{ + first_call_to_drag_data_get = true; +} + +void MainWindow::on_instruments_treeview_drag_data_get(const Glib::RefPtr&, + Gtk::SelectionData& selection_data, guint, guint) +{ + if (!first_call_to_drag_data_get) return; + first_call_to_drag_data_get = false; + + // get selected source instrument + gig::Instrument* src = NULL; + { + Glib::RefPtr sel = m_TreeView.get_selection(); + Gtk::TreeModel::iterator it = sel->get_selected(); + if (it) { + Gtk::TreeModel::Row row = *it; + src = row[m_Columns.m_col_instr]; + } + } + if (!src) return; + + // pass the source gig::Instrument as pointer + selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&src, + sizeof(src)/*length of data in bytes*/); +} + +void MainWindow::on_instruments_treeview_drop_drag_data_received( + const Glib::RefPtr& context, int x, int y, + const Gtk::SelectionData& selection_data, guint, guint time) +{ + gig::Instrument* src = *((gig::Instrument**) selection_data.get_data()); + if (!src || selection_data.get_length() != sizeof(gig::Instrument*)) + return; + + gig::Instrument* dst = NULL; + { + Gtk::TreeModel::Path path; + const bool found = m_TreeView.get_path_at_pos(x, y, path); + if (!found) return; + + Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path); + if (!iter) return; + Gtk::TreeModel::Row row = *iter; + dst = row[m_Columns.m_col_instr]; + } + if (!dst) return; + + //printf("dragdrop received src=%s dst=%s\n", src->pInfo->Name.c_str(), dst->pInfo->Name.c_str()); + src->MoveTo(dst); + __refreshEntireGUI(); + select_instrument(src); +} + // For some reason drag_data_get gets called two times for each // drag'n'drop (at least when target is an Entry). This work-around // makes sure the code in drag_data_get and drop_drag_data_received is @@ -2837,6 +3157,12 @@ if (!script) return; ScriptEditor* editor = new ScriptEditor; + editor->signal_script_to_be_changed.connect( + signal_script_to_be_changed.make_slot() + ); + editor->signal_script_changed.connect( + signal_script_changed.make_slot() + ); editor->setScript(script); //editor->reparent(*this); editor->show(); @@ -3012,7 +3338,7 @@ } Gtk::FileChooserDialog dialog(*this, _("Merge .gig files")); - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); dialog.add_button(_("Merge"), Gtk::RESPONSE_OK); dialog.set_default_response(Gtk::RESPONSE_CANCEL); #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 @@ -3031,7 +3357,9 @@ // show warning in the file picker dialog Gtk::HBox descriptionArea; descriptionArea.set_spacing(15); - Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG)); + Gtk::Image warningIcon; + warningIcon.set_from_icon_name("dialog-warning", + Gtk::IconSize(Gtk::ICON_SIZE_DIALOG)); descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK); #if GTKMM_MAJOR_VERSION < 3 view::WrapLabel description; @@ -3054,7 +3382,8 @@ descriptionArea.show_all(); if (dialog.run() == Gtk::RESPONSE_OK) { - printf("on_action_merge_files self=%x\n", Glib::Threads::Thread::self()); + printf("on_action_merge_files self=%p\n", + static_cast(Glib::Threads::Thread::self())); std::vector filenames = dialog.get_filenames(); // merge the selected files to the currently open .gig file @@ -3066,7 +3395,7 @@ } // update GUI - __refreshEntireGUI(); + __refreshEntireGUI(); } }