--- gigedit/trunk/src/gigedit/mainwindow.cpp 2007/07/05 17:12:20 1261 +++ gigedit/trunk/src/gigedit/mainwindow.cpp 2007/10/15 15:29:58 1436 @@ -17,7 +17,6 @@ * 02110-1301 USA. */ -#include #include #include @@ -25,18 +24,34 @@ #include #include #include +#include + +#include "global.h" #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2 #define ABOUT_DIALOG #include #endif +#if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION < 6) || GLIBMM_MAJOR_VERSION < 2 +namespace Glib { +Glib::ustring filename_display_basename(const std::string& filename) +{ + gchar* gstr = g_path_get_basename(filename.c_str()); + Glib::ustring str(gstr); + g_free(gstr); + return Glib::filename_to_utf8(str); +} +} +#endif + #include #include #include "mainwindow.h" -#define _(String) gettext(String) +#include "../../gfx/status_attached.xpm" +#include "../../gfx/status_detached.xpm" template inline std::string ToString(T o) { std::stringstream ss; @@ -115,6 +130,14 @@ *this, &MainWindow::on_action_quit)); actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument"))); + actionGroup->add(Gtk::Action::create("MenuView", _("_View"))); + Glib::RefPtr toggle_action = + Gtk::ToggleAction::create("Statusbar", _("_Statusbar")); + toggle_action->set_active(true); + actionGroup->add(toggle_action, + 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())); @@ -170,6 +193,9 @@ " " " " " " + " " + " " + " " #ifdef ABOUT_DIALOG " " " " @@ -199,6 +225,12 @@ m_VBox.pack_start(m_HPaned); m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK); m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK); + m_VBox.pack_start(m_StatusBar, Gtk::PACK_SHRINK); + + // Status Bar: + m_StatusBar.pack_start(m_AttachedStateLabel, Gtk::PACK_SHRINK); + m_StatusBar.pack_start(m_AttachedStateImage, Gtk::PACK_SHRINK); + m_StatusBar.show(); m_RegionChooser.signal_region_selected().connect( sigc::mem_fun(*this, &MainWindow::region_changed) ); @@ -234,6 +266,9 @@ std::list drag_target_gig_sample; drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") ); m_TreeViewSamples.drag_source_set(drag_target_gig_sample); + m_TreeViewSamples.signal_drag_begin().connect( + sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_begin) + ); m_TreeViewSamples.signal_drag_data_get().connect( sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get) ); @@ -242,17 +277,50 @@ sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received) ); dimreg_edit.signal_dimreg_changed().connect( - sigc::mem_fun(*this, &MainWindow::file_changed)); + sigc::hide(sigc::mem_fun(*this, &MainWindow::file_changed))); m_RegionChooser.signal_instrument_changed().connect( sigc::mem_fun(*this, &MainWindow::file_changed)); m_DimRegionChooser.signal_region_changed().connect( sigc::mem_fun(*this, &MainWindow::file_changed)); instrumentProps.signal_instrument_changed().connect( sigc::mem_fun(*this, &MainWindow::file_changed)); + + dimreg_edit.signal_dimreg_to_be_changed().connect( + dimreg_to_be_changed_signal.make_slot()); + dimreg_edit.signal_dimreg_changed().connect( + dimreg_changed_signal.make_slot()); + dimreg_edit.signal_sample_ref_changed().connect( + sample_ref_changed_signal.make_slot()); + + m_RegionChooser.signal_instrument_struct_to_be_changed().connect( + sigc::hide( + sigc::bind( + file_structure_to_be_changed_signal.make_slot(), + sigc::ref(this->file) + ) + ) + ); + m_RegionChooser.signal_instrument_struct_changed().connect( + sigc::hide( + sigc::bind( + file_structure_changed_signal.make_slot(), + sigc::ref(this->file) + ) + ) + ); + m_RegionChooser.signal_region_to_be_changed().connect( + region_to_be_changed_signal.make_slot()); + m_RegionChooser.signal_region_changed_signal().connect( + region_changed_signal.make_slot()); + file = 0; file_is_changed = false; + set_file_is_shared(false); show_all_children(); + + // start with a new gig file by default + on_action_file_new(); } MainWindow::~MainWindow() @@ -261,12 +329,12 @@ bool MainWindow::on_delete_event(GdkEventAny* event) { - return file_is_changed && !close_confirmation_dialog(); + return !file_is_shared && file_is_changed && !close_confirmation_dialog(); } void MainWindow::on_action_quit() { - if (file_is_changed && !close_confirmation_dialog()) return; + if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return; hide(); } @@ -380,15 +448,16 @@ m_refTreeModel->clear(); m_refSamplesTreeModel->clear(); // free libgig's gig::File instance - if (file) { - delete file; - file = NULL; - } + if (file && !file_is_shared) delete file; + file = NULL; + set_file_is_shared(false); } void MainWindow::on_action_file_new() { - if (file_is_changed && !close_confirmation_dialog()) return; + if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return; + + if (file_is_shared && !leaving_shared_mode_dialog()) return; // clear all GUI elements __clear(); @@ -407,19 +476,42 @@ Glib::filename_display_basename(filename).c_str()); Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); g_free(msg); +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2 dialog.set_secondary_text(_("If you close without saving, your changes will be lost.")); +#endif 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.set_default_response(Gtk::RESPONSE_YES); int response = dialog.run(); + dialog.hide(); if (response == Gtk::RESPONSE_YES) return file_save(); return response != Gtk::RESPONSE_CANCEL; } +bool MainWindow::leaving_shared_mode_dialog() { + Glib::ustring msg = _("Detach from sampler and proceed working stand-alone?"); + Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2 + dialog.set_secondary_text( + _("If you proceed to work on another instrument file, it won't be " + "used by the sampler until you tell the sampler explicitly to " + "load it.") + ); +#endif + dialog.add_button(_("_Yes, Detach"), Gtk::RESPONSE_YES); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.set_default_response(Gtk::RESPONSE_CANCEL); + int response = dialog.run(); + dialog.hide(); + return response == Gtk::RESPONSE_YES; +} + void MainWindow::on_action_file_open() { - if (file_is_changed && !close_confirmation_dialog()) return; + if (!file_is_shared && file_is_changed && !close_confirmation_dialog()) return; + + if (file_is_shared && !leaving_shared_mode_dialog()) return; Gtk::FileChooserDialog dialog(*this, _("Open file")); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); @@ -434,7 +526,6 @@ if (dialog.run() == Gtk::RESPONSE_OK) { std::string filename = dialog.get_filename(); printf("filename=%s\n", filename.c_str()); - __clear(); printf("on_action_file_open self=%x\n", Glib::Thread::self()); load_file(filename.c_str()); current_dir = Glib::path_get_dirname(filename); @@ -443,6 +534,7 @@ void MainWindow::load_file(const char* name) { + __clear(); load_dialog = new LoadDialog("Loading...", *this); load_dialog->show_all(); loader = new Loader(strdup(name)); @@ -460,8 +552,11 @@ msg.run(); Gtk::Main::quit(); } + // clear all GUI elements + __clear(); + // load the instrument gig::File* pFile = (gig::File*) instr->GetParent(); - load_gig(pFile, 0 /*file name*/); + load_gig(pFile, 0 /*file name*/, true /*shared instrument*/); //TODO: automatically select the given instrument } @@ -483,12 +578,37 @@ file_save(); } -bool MainWindow::file_save() +bool MainWindow::check_if_savable() { if (!file) return false; - if (!file_has_name) return file_save_as(); + + if (!file->GetFirstSample()) { + Gtk::MessageDialog(*this, _("The file could not be saved " + "because it contains no samples"), + false, Gtk::MESSAGE_ERROR).run(); + return false; + } + + for (gig::Instrument* instrument = file->GetFirstInstrument() ; instrument ; + instrument = file->GetNextInstrument()) { + if (!instrument->GetFirstRegion()) { + Gtk::MessageDialog(*this, _("The file could not be saved " + "because there are instruments " + "that have no regions"), + false, Gtk::MESSAGE_ERROR).run(); + return false; + } + } + return true; +} + +bool MainWindow::file_save() +{ + if (!check_if_savable()) return false; + if (!file_is_shared && !file_has_name) return file_save_as(); std::cout << "Saving file\n" << std::flush; + file_structure_to_be_changed_signal.emit(this->file); try { file->Save(); if (file_is_changed) { @@ -496,24 +616,26 @@ file_is_changed = false; } } catch (RIFF::Exception e) { - Glib::ustring txt = "Could not save file: " + e.Message; + file_structure_changed_signal.emit(this->file); + Glib::ustring txt = _("Could not save file: ") + e.Message; Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); msg.run(); return false; } std::cout << "Saving file done\n" << std::flush; __import_queued_samples(); + file_structure_changed_signal.emit(this->file); return true; } void MainWindow::on_action_file_save_as() { + if (!check_if_savable()) return; file_save_as(); } bool MainWindow::file_save_as() { - if (!file) return false; 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); @@ -535,6 +657,7 @@ dialog.set_current_name(Glib::filename_display_basename(filename)); if (dialog.run() == Gtk::RESPONSE_OK) { + file_structure_to_be_changed_signal.emit(this->file); try { std::string filename = dialog.get_filename(); if (!Glib::str_has_suffix(filename, ".gig")) { @@ -548,12 +671,14 @@ file_has_name = true; file_is_changed = false; } catch (RIFF::Exception e) { - Glib::ustring txt = "Could not save file: " + e.Message; + file_structure_changed_signal.emit(this->file); + Glib::ustring txt = _("Could not save file: ") + e.Message; Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); msg.run(); return false; } __import_queued_samples(); + file_structure_changed_signal.emit(this->file); return true; } return false; @@ -576,51 +701,60 @@ int bitdepth; switch (info.format & 0xff) { case SF_FORMAT_PCM_S8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_PCM_16: + case SF_FORMAT_PCM_U8: bitdepth = 16; break; case SF_FORMAT_PCM_24: - bitdepth = 32; // we simply convert to 32 bit for now - break; case SF_FORMAT_PCM_32: - bitdepth = 32; - break; - case SF_FORMAT_PCM_U8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_FLOAT: - bitdepth = 32; - break; case SF_FORMAT_DOUBLE: - bitdepth = 32; // I guess we will always truncate this to 32 bit + bitdepth = 24; break; default: sf_close(hFile); // close sound file throw std::string("format not supported"); // unsupported subformat (yet?) } - // allocate appropriate copy buffer (TODO: for now we copy - // it in one piece, might be tough for very long samples) - // and copy sample data into buffer - int8_t* buffer = NULL; + + const int bufsize = 10000; switch (bitdepth) { - case 16: - buffer = new int8_t[2 * info.channels * info.frames]; - // libsndfile does the conversion for us (if needed) - sf_readf_short(hFile, (short*) buffer, info.frames); + case 16: { + short* buffer = new short[bufsize * info.channels]; + sf_count_t cnt = info.frames; + while (cnt) { + // 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); + cnt -= n; + } + delete[] buffer; break; - case 32: - buffer = new int8_t[4 * info.channels * info.frames]; - // libsndfile does the conversion for us (if needed) - sf_readf_int(hFile, (int*) buffer, info.frames); + } + case 24: { + int* srcbuf = new int[bufsize * info.channels]; + uint8_t* dstbuf = new uint8_t[bufsize * 3 * info.channels]; + sf_count_t cnt = info.frames; + while (cnt) { + // libsndfile returns 32 bits, convert to 24 + int n = sf_readf_int(hFile, srcbuf, bufsize); + int j = 0; + for (int i = 0 ; i < n * info.channels ; i++) { + dstbuf[j++] = srcbuf[i] >> 8; + dstbuf[j++] = srcbuf[i] >> 16; + dstbuf[j++] = srcbuf[i] >> 24; + } + // write from buffer directly (physically) into .gig file + iter->gig_sample->Write(dstbuf, n); + cnt -= n; + } + delete[] srcbuf; + delete[] dstbuf; break; + } } - // write from buffer directly (physically) into .gig file - (*iter).gig_sample->Write(buffer, info.frames); // cleanup sf_close(hFile); - delete[] buffer; // on success we remove the sample from the import queue, // otherwise keep it, maybe it works the next time ? std::list::iterator cur = iter; @@ -635,7 +769,7 @@ } // show error message box when some sample(s) could not be imported if (error_files.size()) { - Glib::ustring txt = "Could not import the following sample(s):\n" + error_files; + Glib::ustring txt = _("Could not import the following sample(s):\n") + error_files; Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); msg.run(); } @@ -652,6 +786,18 @@ #ifdef ABOUT_DIALOG Gtk::AboutDialog dialog; dialog.set_version(VERSION); + dialog.set_copyright("Copyright (C) 2006,2007 Andreas Persson"); + dialog.set_comments( + "Released under the GNU General Public License.\n" + "\n" + "Please notice that this is still a very young instrument editor. " + "So better backup your Gigasampler files before editing them with " + "this application.\n" + "\n" + "Please report bugs to: http://bugs.linuxsampler.org" + ); + dialog.set_website("http://www.linuxsampler.org"); + dialog.set_website_label("http://www.linuxsampler.org"); dialog.run(); #endif } @@ -712,6 +858,22 @@ entry[15].set_text(info->Subject); } +void InstrumentProps::add_prop(BoolEntry& boolentry) +{ + table.attach(boolentry.widget, 0, 2, rowno, rowno + 1, + Gtk::FILL, Gtk::SHRINK); + rowno++; + boolentry.signal_changed_by_user().connect(instrument_changed.make_slot()); +} + +void InstrumentProps::add_prop(BoolEntryPlus6& boolentry) +{ + table.attach(boolentry.widget, 0, 2, rowno, rowno + 1, + Gtk::FILL, Gtk::SHRINK); + rowno++; + boolentry.signal_changed_by_user().connect(instrument_changed.make_slot()); +} + void InstrumentProps::add_prop(LabelWidget& prop) { table.attach(prop.label, 0, 1, rowno, rowno + 1, @@ -726,17 +888,17 @@ : table(2,1), quitButton(Gtk::Stock::CLOSE), eName("Name"), - eIsDrum("IsDrum"), - eMIDIBank("MIDIBank", 0, 16383), - eMIDIProgram("MIDIProgram"), + eIsDrum("Is drum"), + eMIDIBank("MIDI bank", 0, 16383), + eMIDIProgram("MIDI program"), eAttenuation("Attenuation", 0, 96, 0, 1), eGainPlus6("Gain +6dB", eAttenuation, -6), - eEffectSend("EffectSend", 0, 65535), - eFineTune("FineTune", -8400, 8400), - ePitchbendRange("PitchbendRange", 0, 12), - ePianoReleaseMode("PianoReleaseMode"), - eDimensionKeyRangeLow("DimensionKeyRangeLow"), - eDimensionKeyRangeHigh("DimensionKeyRangeHigh") + eEffectSend("Effect send", 0, 65535), + eFineTune("Fine tune", -8400, 8400), + ePitchbendRange("Pitchbend range", 0, 12), + ePianoReleaseMode("Piano release mode"), + eDimensionKeyRangeLow("Dimension key range low"), + eDimensionKeyRangeHigh("Dimension key range high") { set_title("Instrument properties"); @@ -813,7 +975,7 @@ if (h < l) eDimensionKeyRangeLow.set_value(h); } -sigc::signal InstrumentProps::signal_instrument_changed() +sigc::signal& InstrumentProps::signal_instrument_changed() { return instrument_changed; } @@ -826,9 +988,10 @@ } } -void MainWindow::load_gig(gig::File* gig, const char* filename) +void MainWindow::load_gig(gig::File* gig, const char* filename, bool isSharedInstrument) { file = 0; + set_file_is_shared(isSharedInstrument); this->filename = filename ? filename : _("Unsaved Gig File"); set_title(Glib::filename_display_basename(this->filename)); @@ -905,6 +1068,17 @@ } } +void MainWindow::on_action_view_status_bar() { + Gtk::CheckMenuItem* item = + dynamic_cast(uiManager->get_widget("/MenuBar/MenuView/Statusbar")); + if (!item) { + std::cerr << "/MenuBar/MenuView/Statusbar == NULL\n"; + return; + } + if (item->get_active()) m_StatusBar.show(); + else m_StatusBar.hide(); +} + void MainWindow::on_button_release(GdkEventButton* button) { if (button->type == GDK_2BUTTON_PRESS) { @@ -962,6 +1136,17 @@ void MainWindow::on_action_remove_instrument() { if (!file) return; + if (file_is_shared) { + Gtk::MessageDialog msg( + *this, + _("You cannot delete an instrument from this file, since it's " + "currently used by the sampler."), + false, Gtk::MESSAGE_INFO + ); + msg.run(); + return; + } + Glib::RefPtr sel = m_TreeView.get_selection(); Gtk::TreeModel::iterator it = sel->get_selected(); if (it) { @@ -1027,7 +1212,7 @@ dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); dialog.set_select_multiple(true); Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile - const char* supportedFileTypes[] = { + const char* const supportedFileTypes[] = { "*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd", "*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF", "*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64", @@ -1057,25 +1242,15 @@ int bitdepth; switch (info.format & 0xff) { case SF_FORMAT_PCM_S8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_PCM_16: + case SF_FORMAT_PCM_U8: bitdepth = 16; break; case SF_FORMAT_PCM_24: - bitdepth = 32; // we simply convert to 32 bit for now - break; case SF_FORMAT_PCM_32: - bitdepth = 32; - break; - case SF_FORMAT_PCM_U8: - bitdepth = 16; // we simply convert to 16 bit for now - break; case SF_FORMAT_FLOAT: - bitdepth = 32; - break; case SF_FORMAT_DOUBLE: - bitdepth = 32; // I guess we will always truncate this to 32 bit + bitdepth = 24; break; default: sf_close(hFile); // close sound file @@ -1084,11 +1259,52 @@ // add a new sample to the .gig file gig::Sample* sample = file->AddSample(); // file name without path - sample->pInfo->Name = (*iter).substr((*iter).rfind('/') + 1).raw(); + Glib::ustring filename = Glib::filename_display_basename(*iter); + // remove file extension if there is one + for (int i = 0; supportedFileTypes[i]; i++) { + if (Glib::str_has_suffix(filename, supportedFileTypes[i] + 1)) { + filename.erase(filename.length() - strlen(supportedFileTypes[i] + 1)); + break; + } + } + sample->pInfo->Name = filename; sample->Channels = info.channels; sample->BitDepth = bitdepth; sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels; sample->SamplesPerSecond = info.samplerate; + sample->AverageBytesPerSecond = sample->FrameSize * sample->SamplesPerSecond; + sample->BlockAlign = sample->FrameSize; + sample->SamplesTotal = info.frames; + + SF_INSTRUMENT instrument; + if (sf_command(hFile, SFC_GET_INSTRUMENT, + &instrument, sizeof(instrument)) != SF_FALSE) + { + sample->MIDIUnityNote = instrument.basenote; + +#if HAVE_SF_INSTRUMENT_LOOPS + if (instrument.loop_count && instrument.loops[0].mode != SF_LOOP_NONE) { + sample->Loops = 1; + + switch (instrument.loops[0].mode) { + case SF_LOOP_FORWARD: + sample->LoopType = gig::loop_type_normal; + break; + case SF_LOOP_BACKWARD: + sample->LoopType = gig::loop_type_backward; + break; + case SF_LOOP_ALTERNATING: + sample->LoopType = gig::loop_type_bidirectional; + break; + } + sample->LoopStart = instrument.loops[0].start; + sample->LoopEnd = instrument.loops[0].end; + sample->LoopPlayCount = instrument.loops[0].count; + sample->LoopSize = sample->LoopEnd - sample->LoopStart + 1; + } +#endif + } + // schedule resizing the sample (which will be done // physically when File::Save() is called) sample->Resize(info.frames); @@ -1104,7 +1320,7 @@ Gtk::TreeModel::iterator iterSample = m_refSamplesTreeModel->append(row.children()); Gtk::TreeModel::Row rowSample = *iterSample; - rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str(); + rowSample[m_SamplesModel.m_col_name] = filename; rowSample[m_SamplesModel.m_col_sample] = sample; rowSample[m_SamplesModel.m_col_group] = NULL; // close sound file @@ -1117,7 +1333,7 @@ } // show error message box when some file(s) could not be opened / added if (error_files.size()) { - Glib::ustring txt = "Could not add the following sample(s):\n" + error_files; + Glib::ustring txt = _("Could not add the following sample(s):\n") + error_files; Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); msg.run(); } @@ -1143,9 +1359,13 @@ pSample; pSample = group->GetNextSample()) { members.push_back(pSample); } + // notify everybody that we're going to remove these samples + samples_to_be_removed_signal.emit(members); // delete the group in the .gig file including the // samples that belong to the group file->DeleteGroup(group); + // notify that we're done with removal + samples_removed_signal.emit(); // if sample(s) were just previously added, remove // them from the import queue for (std::list::iterator member = members.begin(); @@ -1162,8 +1382,14 @@ } file_changed(); } else if (sample) { + // notify everybody that we're going to remove this sample + std::list lsamples; + lsamples.push_back(sample); + samples_to_be_removed_signal.emit(lsamples); // remove sample from the .gig file file->DeleteSample(sample); + // notify that we're done with removal + samples_removed_signal.emit(); // if sample was just previously added, remove it from // the import queue for (std::list::iterator iter = m_SampleImportQueue.begin(); @@ -1175,20 +1401,36 @@ break; } } + dimreg_changed(); file_changed(); } // remove respective row(s) from samples tree view m_refSamplesTreeModel->erase(it); } catch (RIFF::Exception e) { + // pretend we're done with removal (i.e. to avoid dead locks) + samples_removed_signal.emit(); + // show error message Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR); msg.run(); } } } +// 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 +// only executed once, as drag_begin only gets called once. +void MainWindow::on_sample_treeview_drag_begin(const Glib::RefPtr& context) +{ + first_call_to_drag_data_get = true; +} + void MainWindow::on_sample_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 sample gig::Sample* sample = NULL; Glib::RefPtr sel = m_TreeViewSamples.get_selection(); @@ -1206,20 +1448,65 @@ const Glib::RefPtr& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time) { - gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion(); gig::Sample* sample = *((gig::Sample**) selection_data.get_data()); - if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) { - if (sample != dimregion->pSample) { - dimregion->pSample = sample; - dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str()); - std::cout << "Drop received sample \"" << - dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl; - // drop success - context->drop_reply(true, time); - file_changed(); - return; + if (sample && selection_data.get_length() == sizeof(gig::Sample*)) { + std::cout << "Drop received sample \"" << + sample->pInfo->Name << "\"" << std::endl; + // drop success + context->drop_reply(true, time); + + //TODO: we should better move most of the following code to DimRegionEdit::set_sample() + + // notify everybody that we're going to alter the region + gig::Region* region = m_RegionChooser.get_region(); + region_to_be_changed_signal.emit(region); + + // find the samplechannel dimension + gig::dimension_def_t* stereo_dimension = 0; + for (int i = 0 ; i < region->Dimensions ; i++) { + if (region->pDimensionDefinitions[i].dimension == + gig::dimension_samplechannel) { + stereo_dimension = ®ion->pDimensionDefinitions[i]; + break; + } + } + bool channels_changed = false; + if (sample->Channels == 1 && stereo_dimension) { + // remove the samplechannel dimension + region->DeleteDimension(stereo_dimension); + channels_changed = true; + region_changed(); + } + dimreg_edit.set_sample(sample); + + if (sample->Channels == 2 && !stereo_dimension) { + // add samplechannel dimension + gig::dimension_def_t dim; + dim.dimension = gig::dimension_samplechannel; + dim.bits = 1; + dim.zones = 2; + region->AddDimension(&dim); + channels_changed = true; + region_changed(); } + if (channels_changed) { + // unmap all samples with wrong number of channels + // TODO: maybe there should be a warning dialog for this + for (int i = 0 ; i < region->DimensionRegions ; i++) { + gig::DimensionRegion* d = region->pDimensionRegions[i]; + if (d->pSample && d->pSample->Channels != sample->Channels) { + gig::Sample* oldref = d->pSample; + d->pSample = NULL; + sample_ref_changed_signal.emit(oldref, NULL); + } + } + } + + // notify we're done with altering + region_changed_signal.emit(region); + + return; } // drop failed context->drop_reply(false, time); @@ -1258,3 +1545,55 @@ file_changed(); } } + +void MainWindow::set_file_is_shared(bool b) { + this->file_is_shared = b; + + if (file_is_shared) { + m_AttachedStateLabel.set_label(_("live-mode")); + m_AttachedStateImage.set( + Gdk::Pixbuf::create_from_xpm_data(status_attached_xpm) + ); + } else { + m_AttachedStateLabel.set_label(_("stand-alone")); + m_AttachedStateImage.set( + Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm) + ); + } +} + +sigc::signal& MainWindow::signal_file_structure_to_be_changed() { + return file_structure_to_be_changed_signal; +} + +sigc::signal& MainWindow::signal_file_structure_changed() { + return file_structure_changed_signal; +} + +sigc::signal >& MainWindow::signal_samples_to_be_removed() { + return samples_to_be_removed_signal; +} + +sigc::signal& MainWindow::signal_samples_removed() { + return samples_removed_signal; +} + +sigc::signal& MainWindow::signal_region_to_be_changed() { + return region_to_be_changed_signal; +} + +sigc::signal& MainWindow::signal_region_changed() { + return region_changed_signal; +} + +sigc::signal& MainWindow::signal_sample_ref_changed() { + return sample_ref_changed_signal; +} + +sigc::signal& MainWindow::signal_dimreg_to_be_changed() { + return dimreg_to_be_changed_signal; +} + +sigc::signal& MainWindow::signal_dimreg_changed() { + return dimreg_changed_signal; +}