Merging from trunk
[ardour.git] / gtk2_ardour / sfdb_ui.cc
index 891a8dad19da2b15efeee314848fbc0b36ac4f22..79546880de0644c3f04719b48ed4831bf2d69c0f 100644 (file)
 #include <map>
 #include <cerrno>
 
-#include <sndfile.h>
-
-#include <pbd/basename.h>
-
 #include <gtkmm/box.h>
 #include <gtkmm/stock.h>
 
+#include <pbd/convert.h>
+
+#include <gtkmm2ext/utils.h>
+
 #include <ardour/audio_library.h>
 #include <ardour/audioregion.h>
-#include <ardour/sndfile_helpers.h>
-#include <ardour/sndfilesource.h>
+#include <ardour/audiofilesource.h>
 
+#include "ardour_ui.h"
 #include "gui_thread.h"
 #include "prompter.h"
 #include "sfdb_ui.h"
+#include "utils.h"
 
 #include "i18n.h"
 
 using namespace ARDOUR;
-
-std::string length2string (const int32_t frames, const int32_t sample_rate);
+using namespace PBD;
+using namespace std;
 
 SoundFileBox::SoundFileBox ()
        :
@@ -74,6 +75,7 @@ SoundFileBox::SoundFileBox ()
        main_box.pack_start(top_box, false, false);
        main_box.pack_start(bottom_box, false, false);
 
+       field_view.set_model (fields);
        field_view.set_size_request(200, 150);
        field_view.append_column (_("Field"), label_columns.field);
        field_view.append_column_editable (_("Value"), label_columns.data);
@@ -108,7 +110,7 @@ SoundFileBox::set_session(Session* s)
 {
        _session = s;
 
-    if (!_session) {
+       if (!_session) {
                play_btn.set_sensitive(false);
        } else {
                _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
@@ -120,30 +122,16 @@ SoundFileBox::setup_labels (string filename)
 {
        path = filename;
 
-    SNDFILE *sf;
-
-       sf_info.format = 0; // libsndfile says to clear this before sf_open().
-
-    if ((sf = sf_open ((char *) filename.c_str(), SFM_READ, &sf_info)) < 0) {
-        return false;
-    }
-
-       sf_close (sf);
-
-    if (sf_info.frames == 0 && sf_info.channels == 0 &&
-               sf_info.samplerate == 0 && sf_info.format == 0 &&
-               sf_info.sections == 0) {
-               /* .. ok, it's not a sound file */
-           return false;
+       string error_msg;
+       if(!AudioFileSource::get_soundfile_info (filename, sf_info, error_msg)) {
+               return false;
        }
 
        length.set_alignment (0.0f, 0.0f);
-       length.set_text (string_compose("Length: %1", length2string(sf_info.frames, sf_info.samplerate)));
+       length.set_text (string_compose("Length: %1", PBD::length2string(sf_info.length, sf_info.samplerate)));
 
        format.set_alignment (0.0f, 0.0f);
-       format.set_text (string_compose("Format: %1, %2", 
-                               sndfile_major_format(sf_info.format),
-                               sndfile_minor_format(sf_info.format)));
+       format.set_text (sf_info.format_name);
 
        channels.set_alignment (0.0f, 0.0f);
        channels.set_text (string_compose("Channels: %1", sf_info.channels));
@@ -161,6 +149,8 @@ SoundFileBox::setup_fields ()
 {
        ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
 
+       fields->clear ();
+
        vector<string> field_list;
        Library->get_fields(field_list);
 
@@ -168,12 +158,15 @@ SoundFileBox::setup_fields ()
        Gtk::TreeModel::iterator iter;
        Gtk::TreeModel::Row row;
        for (i = field_list.begin(); i != field_list.end(); ++i) {
-               string value = Library->get_field(path, *i);
-               iter = fields->append();
-               row = *iter;
-
-               row[label_columns.field] = *i;
-               row[label_columns.data]  = value;
+               if (!(*i == _("channels") || *i == _("samplerate") ||
+                       *i == _("resolution") || *i == _("format"))) {
+                       iter = fields->append();
+                       row = *iter;
+
+                       string value = Library->get_field(path, *i);
+                       row[label_columns.field] = *i;
+                       row[label_columns.data]  = value;
+               }
        }
 }
 
@@ -195,12 +188,12 @@ SoundFileBox::play_btn_clicked ()
 
        if (region_cache.find (path) == region_cache.end()) {
                AudioRegion::SourceList srclist;
-               SndFileSource* sfs;
+               AudioFileSource* afs;
 
                for (int n = 0; n < sf_info.channels; ++n) {
                        try {
-                               sfs = new SndFileSource(path+":"+string_compose("%1", n), false);
-                               srclist.push_back(sfs);
+                               afs = AudioFileSource::create (path+":"+string_compose("%1", n));
+                               srclist.push_back(afs);
 
                        } catch (failed_constructor& err) {
                                error << _("Could not access soundfile: ") << path << endmsg;
@@ -213,7 +206,7 @@ SoundFileBox::play_btn_clicked ()
                }
 
                string result;
-               _session->region_name (result, PBD::basename(srclist[0]->name()), false);
+               _session->region_name (result, Glib::path_get_basename(srclist[0]->name()), false);
                AudioRegion* a_region = new AudioRegion(srclist, 0, srclist[0]->length(), result, 0, Region::DefaultFlags, false);
                region_cache[path] = a_region;
        }
@@ -240,14 +233,17 @@ SoundFileBox::add_field_clicked ()
     ArdourPrompter prompter (true);
     string name;
 
-    prompter.set_prompt (_("Name for field"));
+    prompter.set_prompt (_("Name for Field"));
+    prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
+    prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
 
     switch (prompter.run ()) {
                case Gtk::RESPONSE_ACCEPT:
-                       cout << name << endl;
                prompter.get_result (name);
-                       Library->add_field (name);
-                       Library->save_changes ();
+                       if (name.length()) {
+                               Library->add_field (name);
+                               Library->save_changes ();
+                       }
                break;
 
            default:
@@ -257,13 +253,25 @@ SoundFileBox::add_field_clicked ()
 
 void
 SoundFileBox::remove_field_clicked ()
-{}
+{
+       field_view.get_selection()->selected_foreach_iter(mem_fun(*this, &SoundFileBox::delete_row));
+
+       Library->save_changes ();
+}
 
 void
-SoundFileBox::audition_status_changed (bool active)
+SoundFileBox::delete_row (const Gtk::TreeModel::iterator& iter)
 {
-    ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
+       Gtk::TreeModel::Row row = *iter;
+
+       Library->remove_field(row[label_columns.field]);
+}
 
+void
+SoundFileBox::audition_status_changed (bool active)
+{
+       ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
+       
        if (!active) {
                stop_btn_clicked ();
        }
@@ -271,17 +279,25 @@ SoundFileBox::audition_status_changed (bool active)
 
 void
 SoundFileBox::field_selected ()
-{}
+{
+       if (field_view.get_selection()->count_selected_rows()) {
+               remove_field_btn.set_sensitive(true);
+       } else {
+               remove_field_btn.set_sensitive(false);
+       }
+}
 
-SoundFileBrowser::SoundFileBrowser (std::string title)
-       :
-       ArdourDialog(title),
-       chooser(Gtk::FILE_CHOOSER_ACTION_OPEN)
+SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s)
+       : ArdourDialog (title, false),
+         chooser (Gtk::FILE_CHOOSER_ACTION_OPEN)
 {
        get_vbox()->pack_start(chooser);
        chooser.set_preview_widget(preview);
+       chooser.set_select_multiple (true);
 
        chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
+
+       set_session (s);
 }
 
 void
@@ -296,63 +312,126 @@ SoundFileBrowser::update_preview ()
        chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
 }
 
-SoundFileChooser::SoundFileChooser (std::string title)
+SoundFileChooser::SoundFileChooser (string title, ARDOUR::Session* s)
        :
-       SoundFileBrowser(title)
+       SoundFileBrowser(title, s)
 {
        add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
        add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-
        show_all ();
 }
 
-SoundFileOmega::SoundFileOmega (std::string title)
-       :
-       SoundFileBrowser(title),
-       embed_btn (_("Embed")),
-       import_btn (_("Import")),
-       split_check (_("Split Channels"))
+static const char *import_mode_strings[] = {
+       X_("Add to Region list"),
+       X_("Add as new Track(s)"),
+       X_("Add to selected Track(s)"),
+       0
+};
+
+vector<string> SoundFileOmega::mode_strings;
+
+SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s)
+       : SoundFileBrowser (title, s),
+         split_check (_("Split Channels"))
 {
-       get_action_area()->pack_start(embed_btn);
-       get_action_area()->pack_start(import_btn);
+       if (mode_strings.empty()) {
+               mode_strings = PBD::internationalize (import_mode_strings);
+       }
+
+       ARDOUR_UI::instance()->tooltips().set_tip(split_check, 
+                       _("Create a region for each channel"));
+
+       Gtk::Button* btn = add_button (_("Embed"), ResponseEmbed);
+       ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
+                       _("Link to an external file"));
+
+       btn = add_button (_("Import"), ResponseImport);
+       ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
+                       _("Copy a file to the session folder"));
+
        add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
 
-       chooser.set_extra_widget(split_check);
+       Gtk::HBox *box = manage (new Gtk::HBox());
+
+       Gtkmm2ext::set_popdown_strings (mode_combo, mode_strings);
+
+       set_mode (Editing::ImportAsRegion);
+
+       box->pack_start (split_check);
+       box->pack_start (mode_combo);
 
-       embed_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::embed_clicked));
-       import_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::import_clicked));
+       mode_combo.signal_changed().connect (mem_fun (*this, &SoundFileOmega::mode_changed));
 
+       chooser.set_extra_widget (*box);
+       
        show_all ();
 }
 
-void
-SoundFileOmega::embed_clicked ()
+bool
+SoundFileOmega::get_split ()
 {
-       Embedded (chooser.get_filenames(), split_check.get_active());
+       return split_check.get_active();
+}
+
+vector<Glib::ustring>
+SoundFileOmega::get_paths ()
+{
+       return chooser.get_filenames();
 }
 
 void
-SoundFileOmega::import_clicked ()
+SoundFileOmega::set_mode (Editing::ImportMode mode)
 {
-       Imported (chooser.get_filenames(), split_check.get_active());
+       mode_combo.set_active_text (mode_strings[(int)mode]);
+
+       switch (mode) {
+       case Editing::ImportAsRegion:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportAsTrack:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportToTrack:
+               split_check.set_sensitive (false);
+               break;
+       }
 }
 
-std::string
-length2string (const int32_t frames, const int32_t sample_rate)
+Editing::ImportMode
+SoundFileOmega::get_mode ()
 {
-    int secs = (int) (frames / (float) sample_rate);
-    int hrs =  secs / 3600;
-    secs -= (hrs * 3600);
-    int mins = secs / 60;
-    secs -= (mins * 60);
+       vector<string>::iterator i;
+       uint32_t n;
+       string str = mode_combo.get_active_text ();
 
-    int total_secs = (hrs * 3600) + (mins * 60) + secs;
-    int frames_remaining = frames - (total_secs * sample_rate);
-    float fractional_secs = (float) frames_remaining / sample_rate;
+       for (n = 0, i = mode_strings.begin (); i != mode_strings.end(); ++i, ++n) {
+               if (str == (*i)) {
+                       break;
+               }
+       }
 
-    char duration_str[32];
-    sprintf (duration_str, "%02d:%02d:%05.2f", hrs, mins, (float) secs + fractional_secs);
+       if (i == mode_strings.end()) {
+               fatal << string_compose (_("programming error: %1"), X_("unknown import mode string")) << endmsg;
+               /*NOTREACHED*/
+       }
 
-    return duration_str;
+       return (Editing::ImportMode) (n);
 }
 
+void
+SoundFileOmega::mode_changed ()
+{
+       Editing::ImportMode mode = get_mode();
+
+       switch (mode) {
+       case Editing::ImportAsRegion:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportAsTrack:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportToTrack:
+               split_check.set_sensitive (false);
+               break;
+       }
+}