--- gigedit/trunk/src/gigedit/mainwindow.cpp 2008/02/03 00:19:55 1660 +++ gigedit/trunk/src/gigedit/mainwindow.cpp 2008/02/12 14:13:46 1679 @@ -208,6 +208,10 @@ Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE), sigc::mem_fun(*this, &MainWindow::on_action_remove_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) + ); uiManager = Gtk::UIManager::create(); uiManager->insert_action_group(actionGroup); @@ -248,6 +252,7 @@ " " " " " " + " " " " " " " " @@ -260,6 +265,7 @@ m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK); m_VBox.pack_start(m_HPaned); m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK); + m_VBox.pack_start(m_RegionChooser.m_VirtKeybPropsBox, Gtk::PACK_SHRINK); m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK); m_VBox.pack_start(m_StatusBar, Gtk::PACK_SHRINK); @@ -746,12 +752,41 @@ filter.add_pattern("*.gig"); dialog.set_filter(filter); - if (Glib::path_is_absolute(filename)) { - dialog.set_filename(filename); - } else if (current_dir != "") { - dialog.set_current_folder(current_dir); + // set initial dir and filename of the Save As dialog + // and prepare that initial filename as a copy of the gig + { + std::string basename = Glib::path_get_basename(filename); + std::string dir = Glib::path_get_dirname(filename); + basename = std::string("copy_of_") + basename; + Glib::ustring copyFileName = Glib::build_filename(dir, basename); + if (Glib::path_is_absolute(filename)) { + dialog.set_filename(copyFileName); + } else { + if (current_dir != "") dialog.set_current_folder(current_dir); + } + dialog.set_current_name(Glib::filename_display_basename(copyFileName)); } - dialog.set_current_name(Glib::filename_display_basename(filename)); + + // 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)); + descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK); + warningIcon.show(); + Gtk::Label description; + description.set_markup( + _("\nCAUTION: You MUST use the " + "\"Save\" dialog instead of " + "\"Save As...\" if you want to save " + "to the same .gig file. Using " + "\"Save As...\" for writing to the " + "same .gig file will end up in corrupted sample wave data!\n") + ); + description.set_line_wrap(true); + descriptionArea.pack_start(description, Gtk::PACK_SHRINK); + description.show(); + dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK); + descriptionArea.show(); if (dialog.run() == Gtk::RESPONSE_OK) { file_structure_to_be_changed_signal.emit(this->file); @@ -1489,6 +1524,108 @@ Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); msg.run(); } + } +} + +void MainWindow::on_action_replace_all_samples_in_all_groups() +{ + if (!file) return; + Gtk::FileChooserDialog dialog(*this, _("Select Folder"), + Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); + Gtk::Label description( + _("This is a very specific function. It tries to replace all samples " + "in the current gig file by samples located in the directory chosen " + "by you above.\n\n" + "It works like this: For each sample in the gig file it tries to " + "find a sample file in the selected directory with the same name as " + "the sample in the gig file. Optionally you can add a filename " + "postfix below, which will be added to the filename expected to be " + "found. That is, assume you have a gig file with a sample called " + "'Snare', if you enter '.wav' below (like it's done by default), it " + "assumes to find a sample file called 'Snare.wav' and will replace " + "the sample in the gig file accordingly. If you don't need such a " + "postfix, blank the field below. Any gig sample where no " + "appropriate sample file could be found, will be reported and left " + "untouched.\n\n") + ); + description.set_line_wrap(true); + Gtk::HBox entryArea; + Gtk::Label entryLabel( _("Add Filename Extension: "), Gtk::ALIGN_RIGHT); + Gtk::Entry postfixEntryBox; + postfixEntryBox.set_text(".wav"); + entryArea.pack_start(entryLabel); + entryArea.pack_start(postfixEntryBox); + dialog.get_vbox()->pack_start(description, Gtk::PACK_SHRINK); + dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK); + description.show(); + entryLabel.show(); + postfixEntryBox.show(); + entryArea.show(); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(_("Select"), Gtk::RESPONSE_OK); + dialog.set_select_multiple(false); + // fix label width (because Gtk by design doesn't + // know anything about the parent's size) +#if 0 //FIXME: doesn't work + int dialogW, dialogH, labelW, labelH; + dialog.get_size_request(dialogW, dialogH); + description.get_size_request(labelW, labelH); + std::cout << "dialog(" << dialogW << "," << dialogH << ")\nlabel(" << labelW << "," << labelH << ")\n" << std::flush; + description.set_size_request(dialogW, labelH); +#endif + if (dialog.run() == Gtk::RESPONSE_OK) + { + Glib::ustring error_files; + Glib::ustring folder = dialog.get_filename(); + for (gig::Sample* sample = file->GetFirstSample(); + sample; sample = file->GetNextSample()) + { + std::string filename = + folder + G_DIR_SEPARATOR_S + sample->pInfo->Name + + postfixEntryBox.get_text().raw(); + SF_INFO info; + info.format = 0; + SNDFILE* hFile = sf_open(filename.c_str(), SFM_READ, &info); + 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); + throw std::string("format not supported"); + } + SampleImportItem sched_item; + sched_item.gig_sample = sample; + sched_item.sample_path = filename; + m_SampleImportQueue.push_back(sched_item); + sf_close(hFile); + file_changed(); + } + catch (std::string what) + { + if (error_files.size()) error_files += "\n"; + error_files += filename += " (" + what + ")"; + } + } + // show error message box when some file(s) could not be opened / added + if (error_files.size()) { + Glib::ustring txt = + _("Could not replace the following sample(s):\n") + error_files; + Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); + msg.run(); + } } }