--- gigedit/trunk/src/gigedit/mainwindow.cpp 2017/02/12 13:13:06 3108 +++ gigedit/trunk/src/gigedit/mainwindow.cpp 2017/07/30 18:57:35 3339 @@ -35,9 +35,11 @@ #include #include #include +#include #include #include #include +#include #if GTKMM_MAJOR_VERSION < 3 #include "wrapLabel.hh" #endif @@ -58,6 +60,18 @@ #include "../../gfx/status_attached.xpm" #include "../../gfx/status_detached.xpm" #include "gfx/builtinpix.h" +#include "MacroEditor.h" +#include "MacrosSetup.h" +#if defined(__APPLE__) +# include "MacHelper.h" +#endif + +static const Gdk::ModifierType primaryModifierKey = + #if defined(__APPLE__) + Gdk::META_MASK; // Cmd key on Mac + #else + Gdk::CONTROL_MASK; // Ctrl key on all other OSs + #endif MainWindow::MainWindow() : m_DimRegionChooser(*this), @@ -65,6 +79,7 @@ dimreg_all_regions(_("all regions")), dimreg_all_dimregs(_("all dimension splits")), dimreg_stereo(_("both channels")), + labelLegend(_("Legend:")), labelNoSample(_(" No Sample")), labelMissingSample(_(" Missing some Sample(s)")), labelLooped(_(" Looped")), @@ -72,8 +87,14 @@ { loadBuiltInPix(); + this->file = NULL; + // set_border_width(5); -// set_default_size(400, 200); + + if (!Settings::singleton()->autoRestoreWindowDimension) { + set_default_size(800, 600); + set_position(Gtk::WIN_POS_CENTER); + } add(m_VBox); @@ -109,6 +130,8 @@ dimreg_vbox.add(dimreg_edit); dimreg_vbox.pack_start(dimreg_hbox, Gtk::PACK_SHRINK); { + legend_hbox.add(labelLegend); + imageNoSample.set(redDot); imageNoSample.set_alignment(Gtk::ALIGN_END); labelNoSample.set_alignment(Gtk::ALIGN_START); @@ -150,28 +173,30 @@ actionGroup = Gtk::ActionGroup::create(); actionGroup->add(Gtk::Action::create("MenuFile", _("_File"))); - actionGroup->add(Gtk::Action::create("New", _("_New")), - Gtk::AccelKey("n"), + actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW), sigc::mem_fun( *this, &MainWindow::on_action_file_new)); - actionGroup->add(Gtk::Action::create("Open", _("_Open...")), - Gtk::AccelKey("o"), + Glib::RefPtr action = + Gtk::Action::create("Open", Gtk::Stock::OPEN); + action->property_label() = action->property_label() + "..."; + actionGroup->add(action, sigc::mem_fun( *this, &MainWindow::on_action_file_open)); - actionGroup->add(Gtk::Action::create("Save", _("_Save")), - Gtk::AccelKey("s"), + actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE), sigc::mem_fun( *this, &MainWindow::on_action_file_save)); - actionGroup->add(Gtk::Action::create("SaveAs", _("Save _As...")), + action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS); + action->property_label() = action->property_label() + "..."; + actionGroup->add(action, Gtk::AccelKey("s"), sigc::mem_fun( *this, &MainWindow::on_action_file_save_as)); actionGroup->add(Gtk::Action::create("Properties", - _("_Properties")), + Gtk::Stock::PROPERTIES), sigc::mem_fun( *this, &MainWindow::on_action_file_properties)); actionGroup->add(Gtk::Action::create("InstrProperties", - _("_Properties")), + Gtk::Stock::PROPERTIES), sigc::mem_fun( *this, &MainWindow::show_instr_props)); actionGroup->add(Gtk::Action::create("MidiRules", @@ -182,8 +207,7 @@ _("_Script Slots...")), sigc::mem_fun( *this, &MainWindow::show_script_slots)); - actionGroup->add(Gtk::Action::create("Quit", _("_Quit")), - Gtk::AccelKey("q"), + actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT), sigc::mem_fun( *this, &MainWindow::on_action_quit)); actionGroup->add( @@ -195,13 +219,86 @@ sigc::mem_fun(*this, &MainWindow::show_intruments_tab) ); actionGroup->add( - Gtk::Action::create("MenuScript", _("S_cript")), + Gtk::Action::create("MenuScript", _("Scr_ipt")), sigc::mem_fun(*this, &MainWindow::show_scripts_tab) ); actionGroup->add(Gtk::Action::create("AllInstruments", _("_Select"))); + actionGroup->add(Gtk::Action::create("AssignScripts", _("Assign Script"))); actionGroup->add(Gtk::Action::create("MenuEdit", _("_Edit"))); + const Gdk::ModifierType primaryModifierKey = +#if defined(__APPLE__) + Gdk::META_MASK; // Cmd key on Mac +#else + Gdk::CONTROL_MASK; // Ctrl key on all other OSs +#endif + + actionGroup->add(Gtk::Action::create("CopyDimRgn", + _("Copy selected dimension region")), + Gtk::AccelKey(GDK_KEY_c, Gdk::MOD1_MASK), + sigc::mem_fun(*this, &MainWindow::copy_selected_dimrgn)); + + actionGroup->add(Gtk::Action::create("PasteDimRgn", + _("Paste dimension region")), + Gtk::AccelKey(GDK_KEY_v, Gdk::MOD1_MASK), + sigc::mem_fun(*this, &MainWindow::paste_copied_dimrgn)); + + actionGroup->add(Gtk::Action::create("AdjustClipboard", + _("Adjust Clipboard Content")), + Gtk::AccelKey(GDK_KEY_x, Gdk::MOD1_MASK), + sigc::mem_fun(*this, &MainWindow::adjust_clipboard_content)); + + actionGroup->add(Gtk::Action::create("SelectPrevInstr", + _("Select Previous Instrument")), + Gtk::AccelKey(GDK_KEY_Up, primaryModifierKey), + sigc::mem_fun(*this, &MainWindow::select_prev_instrument)); + + actionGroup->add(Gtk::Action::create("SelectNextInstr", + _("Select Next Instrument")), + Gtk::AccelKey(GDK_KEY_Down, primaryModifierKey), + sigc::mem_fun(*this, &MainWindow::select_next_instrument)); + + actionGroup->add(Gtk::Action::create("SelectPrevRegion", + _("Select Previous Region")), + Gtk::AccelKey(GDK_KEY_Left, primaryModifierKey), + sigc::mem_fun(*this, &MainWindow::select_prev_region)); + + actionGroup->add(Gtk::Action::create("SelectNextRegion", + _("Select Next Region")), + Gtk::AccelKey(GDK_KEY_Right, primaryModifierKey), + sigc::mem_fun(*this, &MainWindow::select_next_region)); + + actionGroup->add(Gtk::Action::create("SelectPrevDimRgnZone", + _("Select Previous Dimension Region Zone")), + Gtk::AccelKey(GDK_KEY_Left, Gdk::MOD1_MASK), + sigc::mem_fun(*this, &MainWindow::select_prev_dim_rgn_zone)); + + actionGroup->add(Gtk::Action::create("SelectNextDimRgnZone", + _("Select Next Dimension Region Zone")), + Gtk::AccelKey(GDK_KEY_Right, Gdk::MOD1_MASK), + sigc::mem_fun(*this, &MainWindow::select_next_dim_rgn_zone)); + + actionGroup->add(Gtk::Action::create("SelectPrevDimension", + _("Select Previous Dimension")), + Gtk::AccelKey(GDK_KEY_Up, Gdk::MOD1_MASK), + sigc::mem_fun(*this, &MainWindow::select_prev_dimension)); + + actionGroup->add(Gtk::Action::create("SelectNextDimension", + _("Select Next Dimension")), + Gtk::AccelKey(GDK_KEY_Down, Gdk::MOD1_MASK), + sigc::mem_fun(*this, &MainWindow::select_next_dimension)); + + actionGroup->add(Gtk::Action::create("SelectAddPrevDimRgnZone", + _("Add Previous Dimension Region Zone to Selection")), + Gtk::AccelKey(GDK_KEY_Left, Gdk::MOD1_MASK | Gdk::SHIFT_MASK), + sigc::mem_fun(*this, &MainWindow::select_add_prev_dim_rgn_zone)); + + actionGroup->add(Gtk::Action::create("SelectAddNextDimRgnZone", + _("Add Next Dimension Region Zone to Selection")), + Gtk::AccelKey(GDK_KEY_Right, Gdk::MOD1_MASK | Gdk::SHIFT_MASK), + sigc::mem_fun(*this, &MainWindow::select_add_next_dim_rgn_zone)); + Glib::RefPtr toggle_action = Gtk::ToggleAction::create("CopySampleUnity", _("Copy Sample's _Unity Note")); toggle_action->set_active(true); @@ -218,7 +315,10 @@ actionGroup->add(toggle_action); - actionGroup->add(Gtk::Action::create("MenuView", _("_View"))); + actionGroup->add(Gtk::Action::create("MenuMacro", _("_Macro"))); + + + actionGroup->add(Gtk::Action::create("MenuView", _("Vie_w"))); toggle_action = Gtk::ToggleAction::create("Statusbar", _("_Statusbar")); toggle_action->set_active(true); @@ -245,8 +345,10 @@ sigc::mem_fun(*this, &MainWindow::on_action_refresh_all) ); - actionGroup->add(Gtk::Action::create("MenuHelp", _("_Help"))); - actionGroup->add(Gtk::Action::create("About", _("_About")), + 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), sigc::mem_fun( *this, &MainWindow::on_action_help_about)); actionGroup->add( @@ -258,7 +360,12 @@ sigc::mem_fun(*this, &MainWindow::on_action_duplicate_instrument) ); actionGroup->add( - Gtk::Action::create("RemoveInstrument", _("_Remove")), + Gtk::Action::create("CombInstruments", _("_Combine Instruments ...")), + Gtk::AccelKey(GDK_KEY_j, primaryModifierKey), + sigc::mem_fun(*this, &MainWindow::on_action_combine_instruments) + ); + actionGroup->add( + Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE), sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument) ); @@ -305,7 +412,7 @@ // sample right-click popup actions actionGroup->add( - Gtk::Action::create("SampleProperties", _("_Properties")), + Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES), sigc::mem_fun(*this, &MainWindow::on_action_sample_properties) ); actionGroup->add( @@ -317,7 +424,7 @@ sigc::mem_fun(*this, &MainWindow::on_action_add_sample) ); actionGroup->add( - Gtk::Action::create("RemoveSample", _("_Remove")), + Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE), sigc::mem_fun(*this, &MainWindow::on_action_remove_sample) ); actionGroup->add( @@ -353,7 +460,7 @@ sigc::mem_fun(*this, &MainWindow::on_action_edit_script) ); actionGroup->add( - Gtk::Action::create("RemoveScript", _("_Remove")), + Gtk::Action::create("RemoveScript", Gtk::Stock::REMOVE), sigc::mem_fun(*this, &MainWindow::on_action_remove_script) ); @@ -376,10 +483,29 @@ " " " " " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " " " " " " " " " + " " + " " " " " " " " @@ -398,8 +524,10 @@ " " " " " " + " " " " " " + " " " " " " " " @@ -436,6 +564,7 @@ " " " " " " + " " " " " " " " @@ -530,6 +659,9 @@ instrument_menu = static_cast( uiManager->get_widget("/MenuBar/MenuInstrument/AllInstruments"))->get_submenu(); + assign_scripts_menu = static_cast( + uiManager->get_widget("/MenuBar/MenuInstrument/AssignScripts"))->get_submenu(); + Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar"); m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK); m_VBox.pack_start(m_HPaned); @@ -563,6 +695,7 @@ // Add the TreeView's view columns: m_TreeView.append_column(_("Nr"), m_Columns.m_col_nr); m_TreeView.append_column_editable(_("Instrument"), m_Columns.m_col_name); + m_TreeView.append_column(_("Scripts"), m_Columns.m_col_scripts); m_TreeView.set_headers_visible(true); // establish drag&drop within the instrument tree view, allowing to reorder @@ -738,13 +871,176 @@ // select 'Instruments' tab by default // (gtk allows this only if the tab childs are visible, thats why it's here) - m_TreeViewNotebook.set_current_page(1); + m_TreeViewNotebook.set_current_page(1); + + Gtk::Clipboard::get()->signal_owner_change().connect( + sigc::mem_fun(*this, &MainWindow::on_clipboard_owner_change) + ); + updateClipboardPasteAvailable(); + updateClipboardCopyAvailable(); + + // setup macros and their keyboard accelerators + { + Gtk::Menu* menuMacro = dynamic_cast( + uiManager->get_widget("/MenuBar/MenuMacro") + )->get_submenu(); + + const Gdk::ModifierType noModifier = (Gdk::ModifierType)0; + Gtk::AccelMap::add_entry("/macro_0", GDK_KEY_F1, noModifier); + Gtk::AccelMap::add_entry("/macro_1", GDK_KEY_F2, noModifier); + Gtk::AccelMap::add_entry("/macro_2", GDK_KEY_F3, noModifier); + Gtk::AccelMap::add_entry("/macro_3", GDK_KEY_F4, noModifier); + Gtk::AccelMap::add_entry("/macro_4", GDK_KEY_F5, noModifier); + Gtk::AccelMap::add_entry("/macro_5", GDK_KEY_F6, noModifier); + Gtk::AccelMap::add_entry("/macro_6", GDK_KEY_F7, noModifier); + Gtk::AccelMap::add_entry("/macro_7", GDK_KEY_F8, noModifier); + Gtk::AccelMap::add_entry("/macro_8", GDK_KEY_F9, noModifier); + Gtk::AccelMap::add_entry("/macro_9", GDK_KEY_F10, noModifier); + Gtk::AccelMap::add_entry("/macro_10", GDK_KEY_F11, noModifier); + Gtk::AccelMap::add_entry("/macro_11", GDK_KEY_F12, noModifier); + Gtk::AccelMap::add_entry("/SetupMacros", 'm', primaryModifierKey); + + Glib::RefPtr accelGroup = this->get_accel_group(); + menuMacro->set_accel_group(accelGroup); + + updateMacroMenu(); + } + + // setup "Assign Scripts" keyboard accelerators + { + Gtk::AccelMap::add_entry("/script_0", GDK_KEY_F1, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_1", GDK_KEY_F2, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_2", GDK_KEY_F3, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_3", GDK_KEY_F4, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_4", GDK_KEY_F5, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_5", GDK_KEY_F6, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_6", GDK_KEY_F7, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_7", GDK_KEY_F8, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_8", GDK_KEY_F9, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_9", GDK_KEY_F10, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_10", GDK_KEY_F11, Gdk::SHIFT_MASK); + Gtk::AccelMap::add_entry("/script_11", GDK_KEY_F12, Gdk::SHIFT_MASK); + + Glib::RefPtr accelGroup = this->get_accel_group(); + assign_scripts_menu->set_accel_group(accelGroup); + } + + Glib::signal_idle().connect_once( + sigc::mem_fun(*this, &MainWindow::bringToFront), + 200 + ); } MainWindow::~MainWindow() { } +void MainWindow::bringToFront() { + #if defined(__APPLE__) + macRaiseAppWindow(); + #endif + raise(); + present(); +} + +void MainWindow::updateMacroMenu() { + Gtk::Menu* menuMacro = dynamic_cast( + uiManager->get_widget("/MenuBar/MenuMacro") + )->get_submenu(); + + // remove all entries from "Macro" menu + { + const std::vector children = menuMacro->get_children(); + for (int i = 0; i < children.size(); ++i) { + Gtk::Widget* child = children[i]; + menuMacro->remove(*child); + delete child; + } + } + + // (re)load all macros from config file + try { + Settings::singleton()->loadMacros(m_macros); + } catch (Serialization::Exception e) { + std::cerr << "Exception while loading macros: " << e.Message << std::endl; + } catch (...) { + std::cerr << "Unknown exception while loading macros!" << std::endl; + } + + // add all configured macros as menu items to the "Macro" menu + for (int iMacro = 0; iMacro < m_macros.size(); ++iMacro) { + const Serialization::Archive& macro = m_macros[iMacro]; + std::string name = + macro.name().empty() ? + (std::string(_("Unnamed Macro")) + " " + ToString(iMacro+1)) : macro.name(); + Gtk::MenuItem* item = new Gtk::MenuItem(name); + item->signal_activate().connect( + sigc::bind( + sigc::mem_fun(*this, &MainWindow::onMacroSelected), iMacro + ) + ); + menuMacro->append(*item); + item->set_accel_path("/macro_" + ToString(iMacro)); + Glib::ustring comment = macro.comment(); + if (!comment.empty()) + item->set_tooltip_text(comment); + } + // if there are no macros configured at all, then show a dummy entry instead + if (m_macros.empty()) { + Gtk::MenuItem* item = new Gtk::MenuItem(_("No Macros")); + item->set_sensitive(false); + menuMacro->append(*item); + } + + // add separator line to menu + menuMacro->append(*new Gtk::SeparatorMenuItem); + + { + Gtk::MenuItem* item = new Gtk::MenuItem(_("Setup Macros ...")); + item->signal_activate().connect( + sigc::mem_fun(*this, &MainWindow::setupMacros) + ); + menuMacro->append(*item); + item->set_accel_path("/SetupMacros"); + } + + menuMacro->show_all_children(); +} + +void MainWindow::onMacroSelected(int iMacro) { + printf("onMacroSelected(%d)\n", iMacro); + if (iMacro < 0 || iMacro >= m_macros.size()) return; + Glib::ustring errorText; + try { + applyMacro(m_macros[iMacro]); + } catch (Serialization::Exception e) { + errorText = e.Message; + } catch (...) { + errorText = _("Unknown exception while applying macro"); + } + if (!errorText.empty()) { + Glib::ustring txt = _("Applying macro failed:\n") + errorText; + Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); + msg.run(); + } +} + +void MainWindow::setupMacros() { + MacrosSetup* setup = new MacrosSetup(); + gig::DimensionRegion* pDimRgn = m_DimRegionChooser.get_main_dimregion(); + setup->setMacros(m_macros, &m_serializationArchive, pDimRgn); + setup->signal_macros_changed().connect( + sigc::mem_fun(*this, &MainWindow::onMacrosSetupChanged) + ); + setup->show(); +} + +void MainWindow::onMacrosSetupChanged(const std::vector& macros) { + m_macros = macros; + Settings::singleton()->saveMacros(m_macros); + updateMacroMenu(); +} + bool MainWindow::on_delete_event(GdkEventAny* event) { return !file_is_shared && file_is_changed && !close_confirmation_dialog(); @@ -810,9 +1106,12 @@ } } + m_RegionChooser.setModifyAllRegions(all_regions); m_DimRegionChooser.setModifyAllRegions(all_regions); m_DimRegionChooser.setModifyAllDimensionRegions(all_dimregs); m_DimRegionChooser.setModifyBothChannels(stereo); + + updateClipboardCopyAvailable(); } void MainWindow::dimreg_all_dimregs_toggled() @@ -842,6 +1141,8 @@ } } + updateScriptListOfMenu(); + m_RegionChooser.set_instrument(get_instrument()); if (Settings::singleton()->syncSamplerInstrumentSelection) { @@ -972,7 +1273,7 @@ // 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::String tmpname = filename + ".TMP"; gig->Save(tmpname, &progress); #if defined(WIN32) if (!DeleteFile(filename.c_str())) { @@ -980,14 +1281,14 @@ } #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))); + throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file): " + gig::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))); + throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file): " + gig::String(strerror(errno))); #endif } } @@ -1115,8 +1416,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(_("_Cancel"), Gtk::RESPONSE_CANCEL); - dialog.add_button(file_has_name ? _("_Save") : _("Save _As"), Gtk::RESPONSE_YES); + 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.set_default_response(Gtk::RESPONSE_YES); int response = dialog.run(); dialog.hide(); @@ -1147,7 +1448,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(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.set_default_response(Gtk::RESPONSE_CANCEL); int response = dialog.run(); dialog.hide(); @@ -1161,8 +1462,8 @@ if (file_is_shared && !leaving_shared_mode_dialog()) return; Gtk::FileChooserDialog dialog(*this, _("Open file")); - dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); - dialog.add_button(_("_Open"), Gtk::RESPONSE_OK); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::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; @@ -1357,9 +1658,9 @@ bool MainWindow::file_save_as() { - 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); + 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); dialog.set_default_response(Gtk::RESPONSE_OK); dialog.set_do_overwrite_confirmation(); @@ -1445,12 +1746,12 @@ std::cout << "Starting sample import\n" << std::flush; Glib::ustring error_files; printf("Samples to import: %d\n", int(m_SampleImportQueue.size())); - for (std::list::iterator iter = m_SampleImportQueue.begin(); + for (std::map::iterator iter = m_SampleImportQueue.begin(); iter != m_SampleImportQueue.end(); ) { - printf("Importing sample %s\n",(*iter).sample_path.c_str()); + printf("Importing sample %s\n",iter->second.sample_path.c_str()); SF_INFO info; info.format = 0; - SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info); + SNDFILE* hFile = sf_open(iter->second.sample_path.c_str(), SFM_READ, &info); sf_command(hFile, SFC_SET_SCALE_FLOAT_INT_READ, 0, SF_TRUE); try { if (!hFile) throw std::string(_("could not open file")); @@ -1473,6 +1774,9 @@ throw std::string(_("format not supported")); // unsupported subformat (yet?) } + // reset write position for sample + iter->first->SetPos(0); + const int bufsize = 10000; switch (bitdepth) { case 16: { @@ -1482,7 +1786,7 @@ // libsndfile does the conversion for us (if needed) int n = sf_readf_short(hFile, buffer, bufsize); // write from buffer directly (physically) into .gig file - iter->gig_sample->Write(buffer, n); + iter->first->Write(buffer, n); cnt -= n; } delete[] buffer; @@ -1502,7 +1806,7 @@ dstbuf[j++] = srcbuf[i] >> 24; } // write from buffer directly (physically) into .gig file - iter->gig_sample->Write(dstbuf, n); + iter->first->Write(dstbuf, n); cnt -= n; } delete[] srcbuf; @@ -1513,16 +1817,16 @@ // cleanup sf_close(hFile); // let the sampler re-cache the sample if needed - sample_changed_signal.emit(iter->gig_sample); + sample_changed_signal.emit(iter->first); // on success we remove the sample from the import queue, // otherwise keep it, maybe it works the next time ? - std::list::iterator cur = iter; + std::map::iterator cur = iter; ++iter; m_SampleImportQueue.erase(cur); } catch (std::string what) { // remember the files that made trouble (and their cause) if (!error_files.empty()) error_files += "\n"; - error_files += (*iter).sample_path += " (" + what + ")"; + error_files += iter->second.sample_path += " (" + what + ")"; ++iter; } } @@ -1580,6 +1884,7 @@ dialog.set_comments(sComment.c_str()); dialog.set_website("http://www.linuxsampler.org"); dialog.set_website_label("http://www.linuxsampler.org"); + dialog.set_position(Gtk::WIN_POS_CENTER); dialog.run(); } @@ -1601,10 +1906,15 @@ eSourceForm(_("Source form")), eCommissioned(_("Commissioned")), eSubject(_("Subject")), - quitButton(_("_Close"), true), + quitButton(Gtk::Stock::CLOSE), table(2, 1), m_file(NULL) { + if (!Settings::singleton()->autoRestoreWindowDimension) { + set_default_size(470, 390); + set_position(Gtk::WIN_POS_MOUSE); + } + set_title(_("File Properties")); eName.set_width_chars(50); @@ -1724,7 +2034,7 @@ } InstrumentProps::InstrumentProps() : - quitButton(_("_Close"), true), + quitButton(Gtk::Stock::CLOSE), table(2,1), eName(_("Name")), eIsDrum(_("Is drum")), @@ -1739,6 +2049,11 @@ eDimensionKeyRangeLow(_("Keyswitching range low")), eDimensionKeyRangeHigh(_("Keyswitching range high")) { + if (!Settings::singleton()->autoRestoreWindowDimension) { + //set_default_size(470, 390); + set_position(Gtk::WIN_POS_MOUSE); + } + set_title(_("Instrument Properties")); eDimensionKeyRangeLow.set_tip( @@ -1863,12 +2178,14 @@ for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ; instrument = gig->GetNextInstrument(), ++index) { Glib::ustring name(gig_to_utf8(instrument->pInfo->Name)); + const int iScriptSlots = instrument->ScriptSlotCount(); Gtk::TreeModel::iterator iter = m_refTreeModel->append(); Gtk::TreeModel::Row row = *iter; row[m_Columns.m_col_nr] = index; row[m_Columns.m_col_name] = name; row[m_Columns.m_col_instr] = instrument; + row[m_Columns.m_col_scripts] = iScriptSlots ? ToString(iScriptSlots) : ""; add_instrument_to_menu(name); } @@ -2007,10 +2324,47 @@ ScriptSlots* window = new ScriptSlots; window->setInstrument(instrument); + window->signal_script_slots_changed().connect( + sigc::mem_fun(*this, &MainWindow::onScriptSlotsModified) + ); //window->reparent(*this); window->show(); } +void MainWindow::onScriptSlotsModified(gig::Instrument* pInstrument) { + if (!pInstrument) return; + const int iScriptSlots = pInstrument->ScriptSlotCount(); + + 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] != pInstrument) continue; + row[m_Columns.m_col_scripts] = iScriptSlots ? ToString(iScriptSlots) : ""; + break; + } + + // causes the sampler to reload the instrument with the new script + on_sel_change(); +} + +void MainWindow::assignScript(gig::Script* pScript) { + if (!pScript) { + printf("assignScript() : !script\n"); + return; + } + printf("assignScript('%s')\n", pScript->Name.c_str()); + + gig::Instrument* pInstrument = get_instrument(); + if (!pInstrument) { + printf("!instrument\n"); + return; + } + + pInstrument->AddScriptSlot(pScript); + + onScriptSlotsModified(pInstrument); +} + void MainWindow::on_action_refresh_all() { __refreshEntireGUI(); } @@ -2283,6 +2637,52 @@ } } +void MainWindow::updateScriptListOfMenu() { + // remove all entries from "Assign Script" menu + { + const std::vector children = assign_scripts_menu->get_children(); + for (int i = 0; i < children.size(); ++i) { + Gtk::Widget* child = children[i]; + assign_scripts_menu->remove(*child); + delete child; + } + } + + int iTotalScripts = 0; + + if (!file) goto noScripts; + + // add all configured macros as menu items to the "Macro" menu + for (int iGroup = 0; file->GetScriptGroup(iGroup); ++iGroup) { + gig::ScriptGroup* pGroup = file->GetScriptGroup(iGroup); + for (int iScript = 0; pGroup->GetScript(iScript); ++iScript, ++iTotalScripts) { + gig::Script* pScript = pGroup->GetScript(iScript); + std::string name = pScript->Name; + + Gtk::MenuItem* item = new Gtk::MenuItem(name); + item->signal_activate().connect( + sigc::bind( + sigc::mem_fun(*this, &MainWindow::assignScript), pScript + ) + ); + assign_scripts_menu->append(*item); + item->set_accel_path("/script_" + ToString(iTotalScripts)); + //item->set_tooltip_text(comment); + } + } + + noScripts: + + // if there are no macros configured at all, then show a dummy entry instead + if (!iTotalScripts) { + Gtk::MenuItem* item = new Gtk::MenuItem(_("No Scripts")); + item->set_sensitive(false); + assign_scripts_menu->append(*item); + } + + assign_scripts_menu->show_all_children(); +} + Gtk::RadioMenuItem* MainWindow::add_instrument_to_menu( const Glib::ustring& name, int position) { @@ -2325,12 +2725,11 @@ rowInstr[m_Columns.m_col_nr] = m_refTreeModel->children().size() - 1; rowInstr[m_Columns.m_col_name] = name; rowInstr[m_Columns.m_col_instr] = instrument; + rowInstr[m_Columns.m_col_scripts] = ""; instrument_name_connection.unblock(); add_instrument_to_menu(name); - - m_TreeView.get_selection()->select(iterInstr); - + select_instrument(instrument); file_changed(); } @@ -2613,8 +3012,8 @@ // show 'browse for file' dialog 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.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::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 @@ -2744,7 +3143,7 @@ SampleImportItem sched_item; sched_item.gig_sample = sample; sched_item.sample_path = *iter; - m_SampleImportQueue.push_back(sched_item); + m_SampleImportQueue[sample] = sched_item; // add sample to the tree view if (replace) { row[m_SamplesModel.m_col_name] = gig_to_utf8(sample->pInfo->Name); @@ -2814,7 +3213,7 @@ dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK); description.show(); entryArea.show_all(); - dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(_("Select"), Gtk::RESPONSE_OK); dialog.set_select_multiple(false); if (current_sample_dir != "") { @@ -2854,7 +3253,7 @@ SampleImportItem sched_item; sched_item.gig_sample = sample; sched_item.sample_path = filename; - m_SampleImportQueue.push_back(sched_item); + m_SampleImportQueue[sample] = sched_item; sf_close(hFile); file_changed(); } @@ -2906,15 +3305,12 @@ // if sample(s) were just previously added, remove // them from the import queue for (std::list::iterator member = members.begin(); - member != members.end(); ++member) { - for (std::list::iterator iter = m_SampleImportQueue.begin(); - iter != m_SampleImportQueue.end(); ++iter) { - if ((*iter).gig_sample == *member) { - printf("Removing previously added sample '%s' from group '%s'\n", - (*iter).sample_path.c_str(), name.c_str()); - m_SampleImportQueue.erase(iter); - break; - } + member != members.end(); ++member) + { + if (m_SampleImportQueue.count(*member)) { + printf("Removing previously added sample '%s' from group '%s'\n", + m_SampleImportQueue[sample].sample_path.c_str(), name.c_str()); + m_SampleImportQueue.erase(*member); } } file_changed(); @@ -2929,14 +3325,10 @@ samples_removed_signal.emit(); // if sample was just previously added, remove it from // 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; - } + if (m_SampleImportQueue.count(sample)) { + printf("Removing previously added sample '%s'\n", + m_SampleImportQueue[sample].sample_path.c_str()); + m_SampleImportQueue.erase(sample); } dimreg_changed(); file_changed(); @@ -2992,16 +3384,11 @@ 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; - } + // if sample was just previously added, remove it from the import queue + if (m_SampleImportQueue.count(sample)) { + printf("Removing previously added sample '%s'\n", + m_SampleImportQueue[sample].sample_path.c_str()); + m_SampleImportQueue.erase(sample); } } } catch (RIFF::Exception e) { @@ -3314,8 +3701,25 @@ void MainWindow::on_action_combine_instruments() { CombineInstrumentsDialog* d = new CombineInstrumentsDialog(*this, file); + + // take over selection from instruments list view for the combine dialog's + // list view as pre-selection + std::set indeces; + { + Glib::RefPtr sel = m_TreeView.get_selection(); + std::vector rows = sel->get_selected_rows(); + for (int r = 0; r < rows.size(); ++r) { + Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(rows[r]); + if (it) { + Gtk::TreeModel::Row row = *it; + int index = row[m_Columns.m_col_nr]; + indeces.insert(index); + } + } + } + d->setSelectedInstruments(indeces); + d->show_all(); - d->resize(500, 400); d->run(); if (d->fileWasChanged()) { // update GUI with new instrument just created @@ -3450,7 +3854,7 @@ } Gtk::FileChooserDialog dialog(*this, _("Merge .gig files")); - dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::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 @@ -3577,6 +3981,196 @@ m_TreeViewNotebook.set_current_page(2); } +void MainWindow::select_instrument_by_dir(int dir) { + if (!file) return; + gig::Instrument* pInstrument = get_instrument(); + if (!pInstrument) { + select_instrument( file->GetInstrument(0) ); + return; + } + for (int i = 0; file->GetInstrument(i); ++i) { + if (file->GetInstrument(i) == pInstrument) { + select_instrument( file->GetInstrument(i + dir) ); + return; + } + } +} + +void MainWindow::select_prev_instrument() { + select_instrument_by_dir(-1); +} + +void MainWindow::select_next_instrument() { + select_instrument_by_dir(1); +} + +void MainWindow::select_prev_region() { + m_RegionChooser.select_prev_region(); +} + +void MainWindow::select_next_region() { + m_RegionChooser.select_next_region(); +} + +void MainWindow::select_next_dim_rgn_zone() { + if (m_DimRegionChooser.has_focus()) return; // avoid conflict with key stroke handler of DimenionRegionChooser + m_DimRegionChooser.select_next_dimzone(); +} + +void MainWindow::select_prev_dim_rgn_zone() { + if (m_DimRegionChooser.has_focus()) return; // avoid conflict with key stroke handler of DimenionRegionChooser + m_DimRegionChooser.select_prev_dimzone(); +} + +void MainWindow::select_add_next_dim_rgn_zone() { + m_DimRegionChooser.select_next_dimzone(true); +} + +void MainWindow::select_add_prev_dim_rgn_zone() { + m_DimRegionChooser.select_prev_dimzone(true); +} + +void MainWindow::select_prev_dimension() { + if (m_DimRegionChooser.has_focus()) return; // avoid conflict with key stroke handler of DimenionRegionChooser + m_DimRegionChooser.select_prev_dimension(); +} + +void MainWindow::select_next_dimension() { + if (m_DimRegionChooser.has_focus()) return; // avoid conflict with key stroke handler of DimenionRegionChooser + m_DimRegionChooser.select_next_dimension(); +} + +#define CLIPBOARD_DIMENSIONREGION_TARGET \ + ("libgig.DimensionRegion." + m_serializationArchive.rawDataFormat()) + +void MainWindow::copy_selected_dimrgn() { + gig::DimensionRegion* pDimRgn = m_DimRegionChooser.get_main_dimregion(); + if (!pDimRgn) { + updateClipboardPasteAvailable(); + updateClipboardCopyAvailable(); + return; + } + + std::vector targets; + targets.push_back( Gtk::TargetEntry(CLIPBOARD_DIMENSIONREGION_TARGET) ); + + Glib::RefPtr clipboard = Gtk::Clipboard::get(); + clipboard->set( + targets, + sigc::mem_fun(*this, &MainWindow::on_clipboard_get), + sigc::mem_fun(*this, &MainWindow::on_clipboard_clear) + ); + + m_serializationArchive.serialize(pDimRgn); + + updateClipboardPasteAvailable(); +} + +void MainWindow::paste_copied_dimrgn() { + Glib::RefPtr clipboard = Gtk::Clipboard::get(); + clipboard->request_contents( + CLIPBOARD_DIMENSIONREGION_TARGET, + sigc::mem_fun(*this, &MainWindow::on_clipboard_received) + ); + updateClipboardPasteAvailable(); +} + +void MainWindow::adjust_clipboard_content() { + MacroEditor* editor = new MacroEditor(); + editor->setMacro(&m_serializationArchive, true); + editor->show(); +} + +void MainWindow::updateClipboardPasteAvailable() { + Glib::RefPtr clipboard = Gtk::Clipboard::get(); + clipboard->request_targets( + sigc::mem_fun(*this, &MainWindow::on_clipboard_received_targets) + ); +} + +void MainWindow::updateClipboardCopyAvailable() { + bool bDimensionRegionCopyIsPossible = m_DimRegionChooser.get_main_dimregion(); + static_cast( + uiManager->get_widget("/MenuBar/MenuEdit/CopyDimRgn") + )->set_sensitive(bDimensionRegionCopyIsPossible); +} + +void MainWindow::on_clipboard_owner_change(GdkEventOwnerChange* event) { + updateClipboardPasteAvailable(); +} + +void MainWindow::on_clipboard_get(Gtk::SelectionData& selection_data, guint /*info*/) { + const std::string target = selection_data.get_target(); + if (target == CLIPBOARD_DIMENSIONREGION_TARGET) { + selection_data.set( + CLIPBOARD_DIMENSIONREGION_TARGET, 8 /* "format": probably unused*/, + &m_serializationArchive.rawData()[0], + m_serializationArchive.rawData().size() + ); + } else { + std::cerr << "Clipboard: content for unknown target '" << target << "' requested\n"; + } +} + +void MainWindow::on_clipboard_clear() { + m_serializationArchive.clear(); + updateClipboardPasteAvailable(); + updateClipboardCopyAvailable(); +} + +//NOTE: Might throw exception !!! +void MainWindow::applyMacro(Serialization::Archive& macro) { + gig::DimensionRegion* pDimRgn = m_DimRegionChooser.get_main_dimregion(); + if (!pDimRgn) return; + + for (std::set::iterator itDimReg = dimreg_edit.dimregs.begin(); + itDimReg != dimreg_edit.dimregs.end(); ++itDimReg) + { + gig::DimensionRegion* pDimRgn = *itDimReg; + DimRegionChangeGuard(this, pDimRgn); + macro.deserialize(pDimRgn); + } + //region_changed() + file_changed(); + dimreg_changed(); +} + +void MainWindow::on_clipboard_received(const Gtk::SelectionData& selection_data) { + const std::string target = selection_data.get_target(); + if (target == CLIPBOARD_DIMENSIONREGION_TARGET) { + Glib::ustring errorText; + try { + m_serializationArchive.decode( + selection_data.get_data(), selection_data.get_length() + ); + applyMacro(m_serializationArchive); + } catch (Serialization::Exception e) { + errorText = e.Message; + } catch (...) { + errorText = _("Unknown exception while pasting DimensionRegion"); + } + if (!errorText.empty()) { + Glib::ustring txt = _("Pasting DimensionRegion failed:\n") + errorText; + Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); + msg.run(); + } + } +} + +void MainWindow::on_clipboard_received_targets(const std::vector& targets) { + const bool bDimensionRegionPasteIsPossible = + std::find(targets.begin(), targets.end(), + CLIPBOARD_DIMENSIONREGION_TARGET) != targets.end(); + + static_cast( + uiManager->get_widget("/MenuBar/MenuEdit/PasteDimRgn") + )->set_sensitive(bDimensionRegionPasteIsPossible); + + static_cast( + uiManager->get_widget("/MenuBar/MenuEdit/AdjustClipboard") + )->set_sensitive(bDimensionRegionPasteIsPossible); +} + sigc::signal& MainWindow::signal_file_structure_to_be_changed() { return file_structure_to_be_changed_signal; }