/[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 1661 by schoenebeck, Sun Feb 3 14:10:47 2008 UTC revision 1725 by persson, Sat Apr 26 08:52:15 2008 UTC
# Line 18  Line 18 
18   */   */
19    
20  #include <iostream>  #include <iostream>
21    #include <cstring>
22    
23  #include <gtkmm/filechooserdialog.h>  #include <gtkmm/filechooserdialog.h>
24  #include <gtkmm/messagedialog.h>  #include <gtkmm/messagedialog.h>
# Line 208  MainWindow::MainWindow() : Line 209  MainWindow::MainWindow() :
209          Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),          Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE),
210          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)          sigc::mem_fun(*this, &MainWindow::on_action_remove_sample)
211      );      );
212        actionGroup->add(
213            Gtk::Action::create("ReplaceAllSamplesInAllGroups", _("Replace All Samples In All Groups")),
214            sigc::mem_fun(*this, &MainWindow::on_action_replace_all_samples_in_all_groups)
215        );
216    
217      uiManager = Gtk::UIManager::create();      uiManager = Gtk::UIManager::create();
218      uiManager->insert_action_group(actionGroup);      uiManager->insert_action_group(actionGroup);
# Line 248  MainWindow::MainWindow() : Line 253  MainWindow::MainWindow() :
253          "    <menuitem action='SampleProperties'/>"          "    <menuitem action='SampleProperties'/>"
254          "    <menuitem action='AddGroup'/>"          "    <menuitem action='AddGroup'/>"
255          "    <menuitem action='AddSample'/>"          "    <menuitem action='AddSample'/>"
256            "    <menuitem action='ReplaceAllSamplesInAllGroups' />"
257          "    <separator/>"          "    <separator/>"
258          "    <menuitem action='RemoveSample'/>"          "    <menuitem action='RemoveSample'/>"
259          "  </popup>"          "  </popup>"
# Line 618  void MainWindow::on_action_file_open() Line 624  void MainWindow::on_action_file_open()
624      Gtk::FileFilter filter;      Gtk::FileFilter filter;
625      filter.add_pattern("*.gig");      filter.add_pattern("*.gig");
626      dialog.set_filter(filter);      dialog.set_filter(filter);
627      if (current_dir != "") {      if (current_gig_dir != "") {
628          dialog.set_current_folder(current_dir);          dialog.set_current_folder(current_gig_dir);
629      }      }
630      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
631          std::string filename = dialog.get_filename();          std::string filename = dialog.get_filename();
632          printf("filename=%s\n", filename.c_str());          printf("filename=%s\n", filename.c_str());
633          printf("on_action_file_open self=%x\n", Glib::Thread::self());          printf("on_action_file_open self=%x\n", Glib::Thread::self());
634          load_file(filename.c_str());          load_file(filename.c_str());
635          current_dir = Glib::path_get_dirname(filename);          current_gig_dir = Glib::path_get_dirname(filename);
636      }      }
637  }  }
638    
# Line 747  bool MainWindow::file_save_as() Line 753  bool MainWindow::file_save_as()
753      filter.add_pattern("*.gig");      filter.add_pattern("*.gig");
754      dialog.set_filter(filter);      dialog.set_filter(filter);
755    
756      if (Glib::path_is_absolute(filename)) {      // set initial dir and filename of the Save As dialog
757          dialog.set_filename(filename);      // and prepare that initial filename as a copy of the gig
758      } else if (current_dir != "") {      {
759          dialog.set_current_folder(current_dir);          std::string basename = Glib::path_get_basename(filename);
760            std::string dir = Glib::path_get_dirname(filename);
761            basename = std::string("copy_of_") + basename;
762            Glib::ustring copyFileName = Glib::build_filename(dir, basename);
763            if (Glib::path_is_absolute(filename)) {
764                dialog.set_filename(copyFileName);
765            } else {
766                if (current_gig_dir != "") dialog.set_current_folder(current_gig_dir);
767            }
768            dialog.set_current_name(Glib::filename_display_basename(copyFileName));
769      }      }
770      dialog.set_current_name(Glib::filename_display_basename(filename));  
771        // show warning in the dialog
772        Gtk::HBox descriptionArea;
773        descriptionArea.set_spacing(15);
774        Gtk::Image warningIcon(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize(Gtk::ICON_SIZE_DIALOG));
775        descriptionArea.pack_start(warningIcon, Gtk::PACK_SHRINK);
776        warningIcon.show();
777        Gtk::Label description;
778        description.set_markup(
779            _("\n<b>CAUTION:</b> You <b>MUST</b> use the "
780              "<span style=\"italic\">\"Save\"</span> dialog instead of "
781              "<span style=\"italic\">\"Save As...\"</span> if you want to save "
782              "to the same .gig file. Using "
783              "<span style=\"italic\">\"Save As...\"</span> for writing to the "
784              "same .gig file will end up in corrupted sample wave data!\n")
785        );
786        description.set_line_wrap(true);
787        descriptionArea.pack_start(description, Gtk::PACK_SHRINK);
788        description.show();
789        dialog.get_vbox()->pack_start(descriptionArea, Gtk::PACK_SHRINK);
790        descriptionArea.show();
791    
792      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
793          file_structure_to_be_changed_signal.emit(this->file);          file_structure_to_be_changed_signal.emit(this->file);
# Line 764  bool MainWindow::file_save_as() Line 799  bool MainWindow::file_save_as()
799              printf("filename=%s\n", filename.c_str());              printf("filename=%s\n", filename.c_str());
800              file->Save(filename);              file->Save(filename);
801              this->filename = filename;              this->filename = filename;
802              current_dir = Glib::path_get_dirname(filename);              current_gig_dir = Glib::path_get_dirname(filename);
803              set_title(Glib::filename_display_basename(filename));              set_title(Glib::filename_display_basename(filename));
804              file_has_name = true;              file_has_name = true;
805              file_is_changed = false;              file_is_changed = false;
# Line 1380  void MainWindow::on_action_add_sample() Line 1415  void MainWindow::on_action_add_sample()
1415      allpassfilter.set_name("All Files");      allpassfilter.set_name("All Files");
1416      dialog.add_filter(soundfilter);      dialog.add_filter(soundfilter);
1417      dialog.add_filter(allpassfilter);      dialog.add_filter(allpassfilter);
1418        if (current_sample_dir != "") {
1419            dialog.set_current_folder(current_sample_dir);
1420        }
1421      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1422            current_sample_dir = dialog.get_current_folder();
1423          Glib::ustring error_files;          Glib::ustring error_files;
1424          Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();          Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames();
1425          for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();          for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin();
# Line 1490  void MainWindow::on_action_add_sample() Line 1529  void MainWindow::on_action_add_sample()
1529              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1530              msg.run();              msg.run();
1531          }          }
1532        }
1533    }
1534    
1535    void MainWindow::on_action_replace_all_samples_in_all_groups()
1536    {
1537        if (!file) return;
1538        Gtk::FileChooserDialog dialog(*this, _("Select Folder"),
1539                                      Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
1540        Gtk::Label description(
1541            _("This is a very specific function. It tries to replace all samples "
1542              "in the current gig file by samples located in the directory chosen "
1543              "by you above.\n\n"
1544              "It works like this: For each sample in the gig file it tries to "
1545              "find a sample file in the selected directory with the same name as "
1546              "the sample in the gig file. Optionally you can add a filename "
1547              "postfix below, which will be added to the filename expected to be "
1548              "found. That is, assume you have a gig file with a sample called "
1549              "'Snare', if you enter '.wav' below (like it's done by default), it "
1550              "assumes to find a sample file called 'Snare.wav' and will replace "
1551              "the sample in the gig file accordingly. If you don't need such a "
1552              "postfix, blank the field below. Any gig sample where no "
1553              "appropriate sample file could be found, will be reported and left "
1554              "untouched.\n\n")
1555        );
1556        description.set_line_wrap(true);
1557        Gtk::HBox entryArea;
1558        Gtk::Label entryLabel( _("Add Filename Extension: "), Gtk::ALIGN_RIGHT);
1559        Gtk::Entry postfixEntryBox;
1560        postfixEntryBox.set_text(".wav");
1561        entryArea.pack_start(entryLabel);
1562        entryArea.pack_start(postfixEntryBox);
1563        dialog.get_vbox()->pack_start(description, Gtk::PACK_SHRINK);
1564        dialog.get_vbox()->pack_start(entryArea, Gtk::PACK_SHRINK);
1565        description.show();
1566        entryLabel.show();
1567        postfixEntryBox.show();
1568        entryArea.show();
1569        dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1570        dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
1571        dialog.set_select_multiple(false);
1572        if (current_sample_dir != "") {
1573            dialog.set_current_folder(current_sample_dir);
1574        }
1575        // fix label width (because Gtk by design doesn't
1576        // know anything about the parent's size)
1577    #if 0 //FIXME: doesn't work
1578        int dialogW, dialogH, labelW, labelH;
1579        dialog.get_size_request(dialogW, dialogH);
1580        description.get_size_request(labelW, labelH);
1581        std::cout << "dialog(" << dialogW << "," << dialogH << ")\nlabel(" << labelW << "," << labelH << ")\n" << std::flush;
1582        description.set_size_request(dialogW, labelH);
1583    #endif
1584        if (dialog.run() == Gtk::RESPONSE_OK)
1585        {
1586            current_sample_dir = dialog.get_current_folder();
1587            Glib::ustring error_files;
1588            Glib::ustring folder = dialog.get_filename();
1589            for (gig::Sample* sample = file->GetFirstSample();
1590                 sample; sample = file->GetNextSample())
1591            {
1592                std::string filename =
1593                    folder + G_DIR_SEPARATOR_S + sample->pInfo->Name +
1594                    postfixEntryBox.get_text().raw();
1595                SF_INFO info;
1596                info.format = 0;
1597                SNDFILE* hFile = sf_open(filename.c_str(), SFM_READ, &info);
1598                try
1599                {
1600                    if (!hFile) throw std::string("could not open file");
1601                    int bitdepth;
1602                    switch (info.format & 0xff) {
1603                        case SF_FORMAT_PCM_S8:
1604                        case SF_FORMAT_PCM_16:
1605                        case SF_FORMAT_PCM_U8:
1606                            bitdepth = 16;
1607                            break;
1608                        case SF_FORMAT_PCM_24:
1609                        case SF_FORMAT_PCM_32:
1610                        case SF_FORMAT_FLOAT:
1611                        case SF_FORMAT_DOUBLE:
1612                            bitdepth = 24;
1613                            break;
1614                        default:
1615                            sf_close(hFile);
1616                            throw std::string("format not supported");
1617                    }
1618                    SampleImportItem sched_item;
1619                    sched_item.gig_sample  = sample;
1620                    sched_item.sample_path = filename;
1621                    m_SampleImportQueue.push_back(sched_item);
1622                    sf_close(hFile);
1623                    file_changed();
1624                }
1625                catch (std::string what)
1626                {
1627                    if (error_files.size()) error_files += "\n";
1628                        error_files += filename += " (" + what + ")";
1629                }
1630            }
1631            // show error message box when some file(s) could not be opened / added
1632            if (error_files.size()) {
1633                Glib::ustring txt =
1634                    _("Could not replace the following sample(s):\n") + error_files;
1635                Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1636                msg.run();
1637            }
1638      }      }
1639  }  }
1640    

Legend:
Removed from v.1661  
changed lines
  Added in v.1725

  ViewVC Help
Powered by ViewVC