/[svn]/gigedit/trunk/src/gigedit/mainwindow.cpp
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2666 by schoenebeck, Mon Jul 7 15:01:01 2014 UTC revision 2694 by schoenebeck, Tue Jan 6 16:08:48 2015 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (C) 2006-2014 Andreas Persson   * Copyright (C) 2006-2015 Andreas Persson
3   *   *
4   * This program is free software; you can redistribute it and/or   * This program is free software; you can redistribute it and/or
5   * modify it under the terms of the GNU General Public License as   * modify it under the terms of the GNU General Public License as
# Line 217  MainWindow::MainWindow() : Line 217  MainWindow::MainWindow() :
217          sigc::mem_fun(*this, &MainWindow::on_action_warn_user_on_extensions)          sigc::mem_fun(*this, &MainWindow::on_action_warn_user_on_extensions)
218      );      );
219    
220        toggle_action =
221            Gtk::ToggleAction::create("SyncSamplerInstrumentSelection", _("Synchronize sampler's instrument selection"));
222        toggle_action->set_active(Settings::singleton()->syncSamplerInstrumentSelection);
223        actionGroup->add(
224            toggle_action,
225            sigc::mem_fun(*this, &MainWindow::on_action_sync_sampler_instrument_selection)
226        );
227    
228    
229      actionGroup->add(Gtk::Action::create("MenuTools", _("_Tools")));      actionGroup->add(Gtk::Action::create("MenuTools", _("_Tools")));
230    
# Line 336  MainWindow::MainWindow() : Line 344  MainWindow::MainWindow() :
344          "    </menu>"          "    </menu>"
345          "    <menu action='MenuSettings'>"          "    <menu action='MenuSettings'>"
346          "      <menuitem action='WarnUserOnExtensions'/>"          "      <menuitem action='WarnUserOnExtensions'/>"
347            "      <menuitem action='SyncSamplerInstrumentSelection'/>"
348          "    </menu>"          "    </menu>"
349          "    <menu action='MenuHelp'>"          "    <menu action='MenuHelp'>"
350          "      <menuitem action='About'/>"          "      <menuitem action='About'/>"
# Line 397  MainWindow::MainWindow() : Line 406  MainWindow::MainWindow() :
406      }      }
407      {      {
408          Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(          Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
409                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
410            item->set_tooltip_text(_("If checked, the sampler's current instrument will automatically be switched whenever another instrument was selected in gigedit (only available in live-mode)."));
411        }
412        {
413            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
414              uiManager->get_widget("/MenuBar/MenuTools/CombineInstruments"));              uiManager->get_widget("/MenuBar/MenuTools/CombineInstruments"));
415          item->set_tooltip_text(_("Create combi sounds out of individual sounds of this .gig file."));          item->set_tooltip_text(_("Create combi sounds out of individual sounds of this .gig file."));
416      }      }
# Line 548  MainWindow::MainWindow() : Line 562  MainWindow::MainWindow() :
562          sigc::mem_fun(*this, &MainWindow::on_samples_to_be_removed)          sigc::mem_fun(*this, &MainWindow::on_samples_to_be_removed)
563      );      );
564    
565        dimreg_edit.signal_select_sample().connect(
566            sigc::mem_fun(*this, &MainWindow::select_sample)
567        );
568    
569      m_RegionChooser.signal_instrument_struct_to_be_changed().connect(      m_RegionChooser.signal_instrument_struct_to_be_changed().connect(
570          sigc::hide(          sigc::hide(
571              sigc::bind(              sigc::bind(
# Line 688  void MainWindow::on_sel_change() Line 706  void MainWindow::on_sel_change()
706      }      }
707    
708      m_RegionChooser.set_instrument(get_instrument());      m_RegionChooser.set_instrument(get_instrument());
709    
710        if (Settings::singleton()->syncSamplerInstrumentSelection) {
711            switch_sampler_instrument_signal.emit(get_instrument());
712        }
713  }  }
714    
715  void loader_progress_callback(gig::progress_t* progress)  void loader_progress_callback(gig::progress_t* progress)
# Line 708  void Loader::progress_callback(float fra Line 730  void Loader::progress_callback(float fra
730  void Loader::thread_function()  void Loader::thread_function()
731  {  {
732      printf("thread_function self=%x\n", Glib::Threads::Thread::self());      printf("thread_function self=%x\n", Glib::Threads::Thread::self());
733      printf("Start %s\n", filename);      printf("Start %s\n", filename.c_str());
734      RIFF::File* riff = new RIFF::File(filename);      try {
735      gig = new gig::File(riff);          RIFF::File* riff = new RIFF::File(filename);
736      gig::progress_t progress;          gig = new gig::File(riff);
737      progress.callback = loader_progress_callback;          gig::progress_t progress;
738      progress.custom = this;          progress.callback = loader_progress_callback;
739            progress.custom = this;
740      gig->GetInstrument(0, &progress);  
741      printf("End\n");          gig->GetInstrument(0, &progress);
742      finished_dispatcher();          printf("End\n");
743            finished_dispatcher();
744        } catch (RIFF::Exception e) {
745            error_message = e.Message;
746            error_dispatcher.emit();
747        } catch (...) {
748            error_message = _("Unknown exception occurred");
749            error_dispatcher.emit();
750        }
751  }  }
752    
753  Loader::Loader(const char* filename)  Loader::Loader(const char* filename)
754      : filename(filename), thread(0)      : filename(filename), thread(0), progress(0.f)
755  {  {
756  }  }
757    
# Line 755  Glib::Dispatcher& Loader::signal_finishe Line 785  Glib::Dispatcher& Loader::signal_finishe
785      return finished_dispatcher;      return finished_dispatcher;
786  }  }
787    
788  LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)  Glib::Dispatcher& Loader::signal_error()
789    {
790        return error_dispatcher;
791    }
792    
793    void saver_progress_callback(gig::progress_t* progress)
794    {
795        Saver* saver = static_cast<Saver*>(progress->custom);
796        saver->progress_callback(progress->factor);
797    }
798    
799    void Saver::progress_callback(float fraction)
800    {
801        {
802            Glib::Threads::Mutex::Lock lock(progressMutex);
803            progress = fraction;
804        }
805        progress_dispatcher.emit();
806    }
807    
808    void Saver::thread_function()
809    {
810        printf("thread_function self=%x\n", Glib::Threads::Thread::self());
811        printf("Start %s\n", filename.c_str());
812        try {
813            gig::progress_t progress;
814            progress.callback = saver_progress_callback;
815            progress.custom = this;
816    
817            // if no filename was provided, that means "save", if filename was provided means "save as"
818            if (filename.empty()) {
819                gig->Save(&progress);
820            } else {
821                gig->Save(filename, &progress);
822            }
823    
824            printf("End\n");
825            finished_dispatcher.emit();
826        } catch (RIFF::Exception e) {
827            error_message = e.Message;
828            error_dispatcher.emit();
829        } catch (...) {
830            error_message = _("Unknown exception occurred");
831            error_dispatcher.emit();
832        }
833    }
834    
835    Saver::Saver(gig::File* file, Glib::ustring filename)
836        : gig(file), filename(filename), thread(0), progress(0.f)
837    {
838    }
839    
840    void Saver::launch()
841    {
842    #ifdef OLD_THREADS
843        thread = Glib::Thread::create(sigc::mem_fun(*this, &Saver::thread_function), true);
844    #else
845        thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Saver::thread_function));
846    #endif
847        printf("launch thread=%x\n", thread);
848    }
849    
850    float Saver::get_progress()
851    {
852        float res;
853        {
854            Glib::Threads::Mutex::Lock lock(progressMutex);
855            res = progress;
856        }
857        return res;
858    }
859    
860    Glib::Dispatcher& Saver::signal_progress()
861    {
862        return progress_dispatcher;
863    }
864    
865    Glib::Dispatcher& Saver::signal_finished()
866    {
867        return finished_dispatcher;
868    }
869    
870    Glib::Dispatcher& Saver::signal_error()
871    {
872        return error_dispatcher;
873    }
874    
875    ProgressDialog::ProgressDialog(const Glib::ustring& title, Gtk::Window& parent)
876      : Gtk::Dialog(title, parent, true)      : Gtk::Dialog(title, parent, true)
877  {  {
878      get_vbox()->pack_start(progressBar);      get_vbox()->pack_start(progressBar);
879      show_all_children();      show_all_children();
880        resize(600,50);
881  }  }
882    
883  // Clear all GUI elements / controls. This method is typically called  // Clear all GUI elements / controls. This method is typically called
# Line 828  bool MainWindow::close_confirmation_dial Line 946  bool MainWindow::close_confirmation_dial
946      dialog.set_default_response(Gtk::RESPONSE_YES);      dialog.set_default_response(Gtk::RESPONSE_YES);
947      int response = dialog.run();      int response = dialog.run();
948      dialog.hide();      dialog.hide();
949      if (response == Gtk::RESPONSE_YES) return file_save();  
950      return response != Gtk::RESPONSE_CANCEL;      // user decided to exit app without saving
951        if (response == Gtk::RESPONSE_NO) return true;
952    
953        // user cancelled dialog, thus don't close app
954        if (response == Gtk::RESPONSE_CANCEL) return false;
955    
956        // TODO: the following return valid is disabled and hard coded instead for
957        // now, due to the fact that saving with progress bar is now implemented
958        // asynchronously, as a result the app does not close automatically anymore
959        // after saving the file has completed
960        //
961        //   if (response == Gtk::RESPONSE_YES) return file_save();
962        //   return response != Gtk::RESPONSE_CANCEL;
963        //
964        if (response == Gtk::RESPONSE_YES) file_save();
965        return false; // always prevent closing the app for now (see comment above)
966  }  }
967    
968  bool MainWindow::leaving_shared_mode_dialog() {  bool MainWindow::leaving_shared_mode_dialog() {
# Line 880  void MainWindow::on_action_file_open() Line 1013  void MainWindow::on_action_file_open()
1013  void MainWindow::load_file(const char* name)  void MainWindow::load_file(const char* name)
1014  {  {
1015      __clear();      __clear();
1016      load_dialog = new LoadDialog(_("Loading..."), *this);  
1017      load_dialog->show_all();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1018      loader = new Loader(strdup(name));          _("Loading") +  Glib::ustring(" '") +
1019            Glib::filename_display_basename(name) + "' ...",
1020            *this
1021        );
1022        progress_dialog->show_all();
1023        loader = new Loader(name); //FIXME: memory leak!
1024      loader->signal_progress().connect(      loader->signal_progress().connect(
1025          sigc::mem_fun(*this, &MainWindow::on_loader_progress));          sigc::mem_fun(*this, &MainWindow::on_loader_progress));
1026      loader->signal_finished().connect(      loader->signal_finished().connect(
1027          sigc::mem_fun(*this, &MainWindow::on_loader_finished));          sigc::mem_fun(*this, &MainWindow::on_loader_finished));
1028        loader->signal_error().connect(
1029            sigc::mem_fun(*this, &MainWindow::on_loader_error));
1030      loader->launch();      loader->launch();
1031  }  }
1032    
# Line 928  void MainWindow::load_instrument(gig::In Line 1068  void MainWindow::load_instrument(gig::In
1068    
1069  void MainWindow::on_loader_progress()  void MainWindow::on_loader_progress()
1070  {  {
1071      load_dialog->set_fraction(loader->get_progress());      progress_dialog->set_fraction(loader->get_progress());
1072  }  }
1073    
1074  void MainWindow::on_loader_finished()  void MainWindow::on_loader_finished()
1075  {  {
1076      printf("Loader finished!\n");      printf("Loader finished!\n");
1077      printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());      printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());
1078      load_gig(loader->gig, loader->filename);      load_gig(loader->gig, loader->filename.c_str());
1079      load_dialog->hide();      progress_dialog->hide();
1080    }
1081    
1082    void MainWindow::on_loader_error()
1083    {
1084        Glib::ustring txt = _("Could not load file: ") + loader->error_message;
1085        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1086        msg.run();
1087        progress_dialog->hide();
1088  }  }
1089    
1090  void MainWindow::on_action_file_save()  void MainWindow::on_action_file_save()
# Line 975  bool MainWindow::file_save() Line 1123  bool MainWindow::file_save()
1123    
1124      std::cout << "Saving file\n" << std::flush;      std::cout << "Saving file\n" << std::flush;
1125      file_structure_to_be_changed_signal.emit(this->file);      file_structure_to_be_changed_signal.emit(this->file);
1126      try {  
1127          file->Save();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1128          if (file_is_changed) {          _("Saving") +  Glib::ustring(" '") +
1129              set_title(get_title().substr(1));          Glib::filename_display_basename(this->filename) + "' ...",
1130              file_is_changed = false;          *this
1131          }      );
1132      } catch (RIFF::Exception e) {      progress_dialog->show_all();
1133          file_structure_changed_signal.emit(this->file);      saver = new Saver(this->file); //FIXME: memory leak!
1134          Glib::ustring txt = _("Could not save file: ") + e.Message;      saver->signal_progress().connect(
1135          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);          sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1136          msg.run();      saver->signal_finished().connect(
1137          return false;          sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1138      }      saver->signal_error().connect(
1139      std::cout << "Saving file done\n" << std::flush;          sigc::mem_fun(*this, &MainWindow::on_saver_error));
1140        saver->launch();
1141    
1142        return true;
1143    }
1144    
1145    void MainWindow::on_saver_progress()
1146    {
1147        progress_dialog->set_fraction(saver->get_progress());
1148    }
1149    
1150    void MainWindow::on_saver_error()
1151    {
1152        file_structure_changed_signal.emit(this->file);
1153        Glib::ustring txt = _("Could not save file: ") + saver->error_message;
1154        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1155        msg.run();
1156    }
1157    
1158    void MainWindow::on_saver_finished()
1159    {
1160        this->file = saver->gig;
1161        this->filename = saver->filename;
1162        current_gig_dir = Glib::path_get_dirname(filename);
1163        set_title(Glib::filename_display_basename(filename));
1164        file_has_name = true;
1165        file_is_changed = false;
1166        std::cout << "Saving file done. Importing queued samples now ...\n" << std::flush;
1167      __import_queued_samples();      __import_queued_samples();
1168        std::cout << "Importing queued samples done.\n" << std::flush;
1169    
1170      file_structure_changed_signal.emit(this->file);      file_structure_changed_signal.emit(this->file);
1171      return true;  
1172        load_gig(this->file, this->filename.c_str());
1173        progress_dialog->hide();
1174  }  }
1175    
1176  void MainWindow::on_action_file_save_as()  void MainWindow::on_action_file_save_as()
# Line 1056  bool MainWindow::file_save_as() Line 1235  bool MainWindow::file_save_as()
1235      descriptionArea.show_all();      descriptionArea.show_all();
1236    
1237      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1238          file_structure_to_be_changed_signal.emit(this->file);          std::string filename = dialog.get_filename();
1239          try {          if (!Glib::str_has_suffix(filename, ".gig")) {
1240              std::string filename = dialog.get_filename();              filename += ".gig";
             if (!Glib::str_has_suffix(filename, ".gig")) {  
                 filename += ".gig";  
             }  
             printf("filename=%s\n", filename.c_str());  
             file->Save(filename);  
             this->filename = filename;  
             current_gig_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 false;  
1241          }          }
1242          __import_queued_samples();          printf("filename=%s\n", filename.c_str());
1243          file_structure_changed_signal.emit(this->file);  
1244            progress_dialog = new ProgressDialog( //FIXME: memory leak!
1245                _("Saving") +  Glib::ustring(" '") +
1246                Glib::filename_display_basename(filename) + "' ...",
1247                *this
1248            );
1249            progress_dialog->show_all();
1250    
1251            saver = new Saver(file, filename); //FIXME: memory leak!
1252            saver->signal_progress().connect(
1253                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1254            saver->signal_finished().connect(
1255                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1256            saver->signal_error().connect(
1257                sigc::mem_fun(*this, &MainWindow::on_saver_error));
1258            saver->launch();
1259    
1260          return true;          return true;
1261      }      }
1262      return false;      return false;
# Line 1188  void MainWindow::on_action_warn_user_on_ Line 1367  void MainWindow::on_action_warn_user_on_
1367          !Settings::singleton()->warnUserOnExtensions;          !Settings::singleton()->warnUserOnExtensions;
1368  }  }
1369    
1370    void MainWindow::on_action_sync_sampler_instrument_selection() {
1371        Settings::singleton()->syncSamplerInstrumentSelection =
1372            !Settings::singleton()->syncSamplerInstrumentSelection;
1373    }
1374    
1375  void MainWindow::on_action_help_about()  void MainWindow::on_action_help_about()
1376  {  {
1377      Gtk::AboutDialog dialog;      Gtk::AboutDialog dialog;
# Line 1197  void MainWindow::on_action_help_about() Line 1381  void MainWindow::on_action_help_about()
1381      dialog.set_name("Gigedit");      dialog.set_name("Gigedit");
1382  #endif  #endif
1383      dialog.set_version(VERSION);      dialog.set_version(VERSION);
1384      dialog.set_copyright("Copyright (C) 2006-2014 Andreas Persson");      dialog.set_copyright("Copyright (C) 2006-2015 Andreas Persson");
1385      const std::string sComment =      const std::string sComment =
1386          _("Built " __DATE__ "\nUsing ") +          _("Built " __DATE__ "\nUsing ") +
1387          ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +          ::gig::libraryName() + " " + ::gig::libraryVersion() + "\n\n" +
# Line 1709  void MainWindow::on_instrument_selection Line 1893  void MainWindow::on_instrument_selection
1893      }      }
1894  }  }
1895    
1896    void MainWindow::select_sample(gig::Sample* sample) {
1897        Glib::RefPtr<Gtk::TreeModel> model = m_TreeViewSamples.get_model();
1898        for (int g = 0; g < model->children().size(); ++g) {
1899            Gtk::TreeModel::Row rowGroup = model->children()[g];
1900            for (int s = 0; s < rowGroup.children().size(); ++s) {
1901                Gtk::TreeModel::Row rowSample = rowGroup.children()[s];
1902                if (rowSample[m_SamplesModel.m_col_sample] == sample) {
1903                    show_samples_tab();
1904                    m_TreeViewSamples.get_selection()->select(rowGroup.children()[s]);
1905                    Gtk::TreePath path(
1906                        m_TreeViewSamples.get_selection()->get_selected()
1907                    );
1908                    m_TreeViewSamples.scroll_to_row(path);
1909                    return;
1910                }
1911            }
1912        }
1913    }
1914    
1915  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {  void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) {
1916      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {      if (button->type == GDK_BUTTON_PRESS && button->button == 3) {
1917          Gtk::Menu* sample_popup =          Gtk::Menu* sample_popup =
# Line 2735  void MainWindow::mergeFiles(const std::v Line 2938  void MainWindow::mergeFiles(const std::v
2938          );          );
2939      }      }
2940    
2941      // Note: requires that this file already has a filename !      // Finally save gig file persistently to disk ...
2942      this->file->Save();      //NOTE: requires that this gig file already has a filename !
2943        {
2944            std::cout << "Saving file\n" << std::flush;
2945            file_structure_to_be_changed_signal.emit(this->file);
2946    
2947            progress_dialog = new ProgressDialog( //FIXME: memory leak!
2948                _("Saving") +  Glib::ustring(" '") +
2949                Glib::filename_display_basename(this->filename) + "' ...",
2950                *this
2951            );
2952            progress_dialog->show_all();
2953            saver = new Saver(this->file); //FIXME: memory leak!
2954            saver->signal_progress().connect(
2955                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
2956            saver->signal_finished().connect(
2957                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
2958            saver->signal_error().connect(
2959                sigc::mem_fun(*this, &MainWindow::on_saver_error));
2960            saver->launch();
2961        }
2962  }  }
2963    
2964  void MainWindow::on_action_merge_files() {  void MainWindow::on_action_merge_files() {
# Line 2826  void MainWindow::set_file_is_shared(bool Line 3048  void MainWindow::set_file_is_shared(bool
3048              Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)              Gdk::Pixbuf::create_from_xpm_data(status_detached_xpm)
3049          );          );
3050      }      }
3051    
3052        {
3053            Gtk::MenuItem* item = dynamic_cast<Gtk::MenuItem*>(
3054                uiManager->get_widget("/MenuBar/MenuSettings/SyncSamplerInstrumentSelection"));
3055            if (item) item->set_sensitive(b);
3056        }
3057  }  }
3058    
3059  void MainWindow::on_sample_ref_count_incremented(gig::Sample* sample, int offset) {  void MainWindow::on_sample_ref_count_incremented(gig::Sample* sample, int offset) {
# Line 2927  sigc::signal<void, int/*key*/, int/*velo Line 3155  sigc::signal<void, int/*key*/, int/*velo
3155  sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {  sigc::signal<void, int/*key*/, int/*velocity*/>& MainWindow::signal_keyboard_key_released() {
3156      return m_RegionChooser.signal_keyboard_key_released();      return m_RegionChooser.signal_keyboard_key_released();
3157  }  }
3158    
3159    sigc::signal<void, gig::Instrument*>& MainWindow::signal_switch_sampler_instrument() {
3160        return switch_sampler_instrument_signal;
3161    }

Legend:
Removed from v.2666  
changed lines
  Added in v.2694

  ViewVC Help
Powered by ViewVC