--- gigedit/trunk/src/gigedit/mainwindow.cpp 2007/06/10 10:56:11 1225 +++ gigedit/trunk/src/gigedit/mainwindow.cpp 2007/09/07 21:18:31 1328 @@ -26,11 +26,23 @@ #include #include -#if GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6 +#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 @@ -99,10 +111,9 @@ action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS); action->property_label() = action->property_label() + "..."; actionGroup->add(action, - *(new Gtk::AccelKey("s")), + Gtk::AccelKey("s"), sigc::mem_fun( - *this, &MainWindow::on_action_file_save_as) - ); + *this, &MainWindow::on_action_file_save_as)); actionGroup->add(Gtk::Action::create("Properties", Gtk::Stock::PROPERTIES), sigc::mem_fun( @@ -113,7 +124,7 @@ *this, &MainWindow::show_instr_props)); actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT), sigc::mem_fun( - *this, &MainWindow::hide)); + *this, &MainWindow::on_action_quit)); actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument"))); action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP); @@ -153,7 +164,7 @@ uiManager = Gtk::UIManager::create(); uiManager->insert_action_group(actionGroup); - // add_accel_group(uiManager->get_accel_group()); + add_accel_group(uiManager->get_accel_group()); Glib::ustring ui_info = "" @@ -201,9 +212,9 @@ m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK); m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK); - m_RegionChooser.signal_sel_changed().connect( + m_RegionChooser.signal_region_selected().connect( sigc::mem_fun(*this, &MainWindow::region_changed) ); - m_DimRegionChooser.signal_sel_changed().connect( + m_DimRegionChooser.signal_dimregion_selected().connect( sigc::mem_fun(*this, &MainWindow::dimreg_changed) ); @@ -235,6 +246,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,16 +256,67 @@ dimreg_edit.wSample->signal_drag_data_received().connect( sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received) ); + dimreg_edit.signal_dimreg_changed().connect( + 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; show_all_children(); + + // start with a new gig file by default + on_action_file_new(); } MainWindow::~MainWindow() { } +bool MainWindow::on_delete_event(GdkEventAny* event) +{ + return file_is_changed && !close_confirmation_dialog(); +} + +void MainWindow::on_action_quit() +{ + if (file_is_changed && !close_confirmation_dialog()) return; + hide(); +} + void MainWindow::region_changed() { m_DimRegionChooser.set_region(m_RegionChooser.get_region()); @@ -370,6 +435,8 @@ void MainWindow::on_action_file_new() { + if (file_is_changed && !close_confirmation_dialog()) return; + // clear all GUI elements __clear(); // create a new .gig file (virtually yet) @@ -378,27 +445,54 @@ gig::Instrument* pInstrument = pFile->AddInstrument(); pInstrument->pInfo->Name = "Unnamed Instrument"; // update GUI with that new gig::File - load_gig(pFile, NULL /*no file name yet*/); + load_gig(pFile, 0 /*no file name yet*/); +} + +bool MainWindow::close_confirmation_dialog() +{ + gchar* msg = g_strdup_printf(_("Save changes to \"%s\" before closing?"), + 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; } void MainWindow::on_action_file_open() { + if (file_is_changed && !close_confirmation_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.set_default_response(Gtk::RESPONSE_OK); Gtk::FileFilter filter; filter.add_pattern("*.gig"); dialog.set_filter(filter); + if (current_dir != "") { + dialog.set_current_folder(current_dir); + } if (dialog.run() == Gtk::RESPONSE_OK) { - printf("filename=%s\n", dialog.get_filename().c_str()); - __clear(); + std::string filename = dialog.get_filename(); + printf("filename=%s\n", filename.c_str()); printf("on_action_file_open self=%x\n", Glib::Thread::self()); - load_file(dialog.get_filename().c_str()); + load_file(filename.c_str()); + current_dir = Glib::path_get_dirname(filename); } } void MainWindow::load_file(const char* name) { + __clear(); load_dialog = new LoadDialog("Loading...", *this); load_dialog->show_all(); loader = new Loader(strdup(name)); @@ -416,8 +510,11 @@ msg.run(); Gtk::Main::quit(); } + // clear all GUI elements + __clear(); + // load the instrument gig::File* pFile = (gig::File*) instr->GetParent(); - load_gig(pFile, NULL /*file name*/); + load_gig(pFile, 0 /*file name*/); //TODO: automatically select the given instrument } @@ -436,41 +533,113 @@ void MainWindow::on_action_file_save() { - if (!file) return; + file_save(); +} + +bool MainWindow::check_if_savable() +{ + if (!file) return false; + + 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_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) { + set_title(get_title().substr(1)); + file_is_changed = false; + } } catch (RIFF::Exception e) { + 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; + 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 (!file) return; - Gtk::FileChooserDialog dialog(*this, "Open", Gtk::FILE_CHOOSER_ACTION_SAVE); + if (!check_if_savable()) return; + file_save_as(); +} + +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); + dialog.set_default_response(Gtk::RESPONSE_OK); + +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 8) || GTKMM_MAJOR_VERSION > 2 + dialog.set_do_overwrite_confirmation(); + // TODO: an overwrite dialog for gtkmm < 2.8 +#endif Gtk::FileFilter filter; 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); + } + dialog.set_current_name(Glib::filename_display_basename(filename)); + if (dialog.run() == Gtk::RESPONSE_OK) { - printf("filename=%s\n", dialog.get_filename().c_str()); + file_structure_to_be_changed_signal.emit(this->file); try { - file->Save(dialog.get_filename()); + std::string filename = dialog.get_filename(); + if (!Glib::str_has_suffix(filename, ".gig")) { + filename += ".gig"; + } + printf("filename=%s\n", filename.c_str()); + file->Save(filename); + this->filename = filename; + current_dir = Glib::path_get_dirname(filename); + set_title(Glib::filename_display_basename(filename)); + file_has_name = true; + file_is_changed = false; } catch (RIFF::Exception e) { + 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; + return false; } __import_queued_samples(); + file_structure_changed_signal.emit(this->file); + return true; } + return false; } // actually write the sample(s)' data to the gig file @@ -490,51 +659,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; @@ -626,6 +804,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, @@ -633,23 +827,24 @@ table.attach(prop.widget, 1, 2, rowno, rowno + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK); rowno++; + prop.signal_changed_by_user().connect(instrument_changed.make_slot()); } InstrumentProps::InstrumentProps() : 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"); @@ -669,9 +864,9 @@ add_prop(eDimensionKeyRangeLow); add_prop(eDimensionKeyRangeHigh); - eDimensionKeyRangeLow.signal_value_changed().connect( + eDimensionKeyRangeLow.signal_changed_by_user().connect( sigc::mem_fun(*this, &InstrumentProps::key_range_low_changed)); - eDimensionKeyRangeHigh.signal_value_changed().connect( + eDimensionKeyRangeHigh.signal_changed_by_user().connect( sigc::mem_fun(*this, &InstrumentProps::key_range_high_changed)); add(vbox); @@ -696,7 +891,6 @@ void InstrumentProps::set_instrument(gig::Instrument* instrument) { - update_gui = false; eName.set_ptr(&instrument->pInfo->Name); eIsDrum.set_ptr(&instrument->IsDrum); eMIDIBank.set_ptr(&instrument->MIDIBank); @@ -707,9 +901,10 @@ eFineTune.set_ptr(&instrument->FineTune); ePitchbendRange.set_ptr(&instrument->PitchbendRange); ePianoReleaseMode.set_ptr(&instrument->PianoReleaseMode); + eDimensionKeyRangeLow.set_ptr(0); + eDimensionKeyRangeHigh.set_ptr(0); eDimensionKeyRangeLow.set_ptr(&instrument->DimensionKeyRange.low); eDimensionKeyRangeHigh.set_ptr(&instrument->DimensionKeyRange.high); - update_gui = true; } void InstrumentProps::key_range_low_changed() @@ -726,17 +921,27 @@ if (h < l) eDimensionKeyRangeLow.set_value(h); } -void MainWindow::load_gig(gig::File* gig, const char* filename) +sigc::signal InstrumentProps::signal_instrument_changed() { - file = gig; + return instrument_changed; +} - if (filename) { - const char *basename = strrchr(filename, '/'); - basename = basename ? basename + 1 : filename; - set_title(basename); - } else { - set_title("unnamed"); +void MainWindow::file_changed() +{ + if (file && !file_is_changed) { + set_title("*" + get_title()); + file_is_changed = true; } +} + +void MainWindow::load_gig(gig::File* gig, const char* filename) +{ + file = 0; + + this->filename = filename ? filename : _("Unsaved Gig File"); + set_title(Glib::filename_display_basename(this->filename)); + file_has_name = filename; + file_is_changed = false; propDialog.set_info(gig->pInfo); @@ -785,6 +990,8 @@ } } + file = gig; + // select the first instrument Glib::RefPtr tree_sel_ref = m_TreeView.get_selection(); tree_sel_ref->select(Gtk::TreePath("0")); @@ -858,6 +1065,7 @@ Gtk::TreeModel::Row rowInstr = *iterInstr; rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str(); rowInstr[m_Columns.m_col_instr] = instrument; + file_changed(); } void MainWindow::on_action_remove_instrument() { @@ -872,6 +1080,7 @@ if (instr) file->DeleteInstrument(instr); // remove respective row from instruments tree view m_refTreeModel->erase(it); + file_changed(); } catch (RIFF::Exception e) { Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR); msg.run(); @@ -900,6 +1109,7 @@ rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str(); rowGroup[m_SamplesModel.m_col_sample] = NULL; rowGroup[m_SamplesModel.m_col_group] = group; + file_changed(); } void MainWindow::on_action_add_sample() { @@ -925,7 +1135,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", @@ -955,25 +1165,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 @@ -982,11 +1182,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); @@ -1002,11 +1243,12 @@ 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 sf_close(hFile); + file_changed(); } catch (std::string what) { // remember the files that made trouble (and their cause) if (error_files.size()) error_files += "\n"; error_files += *iter += " (" + what + ")"; @@ -1040,9 +1282,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(); @@ -1057,9 +1303,16 @@ } } } + 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(); @@ -1071,19 +1324,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(); @@ -1101,19 +1371,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); - 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); @@ -1127,9 +1443,17 @@ gig::Group* group = row[m_SamplesModel.m_col_group]; gig::Sample* sample = row[m_SamplesModel.m_col_sample]; if (group) { - group->Name = name; + if (group->Name != name) { + group->Name = name; + printf("group name changed\n"); + file_changed(); + } } else if (sample) { - sample->pInfo->Name = name.raw(); + if (sample->pInfo->Name != name.raw()) { + sample->pInfo->Name = name.raw(); + printf("sample name changed\n"); + file_changed(); + } } } @@ -1139,5 +1463,44 @@ Gtk::TreeModel::Row row = *iter; Glib::ustring name = row[m_Columns.m_col_name]; gig::Instrument* instrument = row[m_Columns.m_col_instr]; - if (instrument) instrument->pInfo->Name = name.raw(); + if (instrument && instrument->pInfo->Name != name.raw()) { + instrument->pInfo->Name = name.raw(); + file_changed(); + } +} + +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; }