Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / export_timespan_selector.cc
index cea01ff4c8455df59c7e90d56c34ce835c046802..cfed17cf5625d5dc60df7bbe585afd7508d38d67 100644 (file)
@@ -1,56 +1,83 @@
 /*
-    Copyright (C) 2008 Paul Davis
-    Author: Sakari Bergen
+ * Copyright (C) 2008-2013 Sakari Bergen <sakari.bergen@beatwaves.net>
+ * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
+ * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
+ * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
+ * Copyright (C) 2013-2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
+ * Copyright (C) 2015-2016 Tim Mayberry <mojofunk@gmail.com>
+ * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
 
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
-
-#include "export_timespan_selector.h"
+#include <sstream>
+#include <iomanip>
 
-#include "ardour_ui.h"
+#include "pbd/enumwriter.h"
+#include "pbd/string_convert.h"
 
-#include "ardour/tempo.h"
 #include "ardour/location.h"
 #include "ardour/types.h"
 #include "ardour/session.h"
 #include "ardour/export_handler.h"
 #include "ardour/export_timespan.h"
 
-#include "pbd/enumwriter.h"
-#include "pbd/convert.h"
-
-#include <sstream>
-#include <iomanip>
+#include "export_timespan_selector.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace Glib;
 using namespace ARDOUR;
 using namespace PBD;
 using std::string;
 
-ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
-       manager (manager),
-       time_format_label (_("Show Times as:"), Gtk::ALIGN_LEFT)
+ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, ProfileManagerPtr manager, bool multi)
+       : manager (manager)
+       , _realtime_available (true)
+       , time_format_label (_("Show Times as:"), Gtk::ALIGN_LEFT)
+       , realtime_checkbutton (_("Realtime Export"))
 {
        set_session (session);
 
        option_hbox.pack_start (time_format_label, false, false, 0);
        option_hbox.pack_start (time_format_combo, false, false, 6);
 
+       if (multi) {
+               Gtk::Button* b = Gtk::manage (new Gtk::Button (_("Select All")));
+               b->signal_clicked().connect (
+                               sigc::bind (
+                                       sigc::mem_fun (*this, &ExportTimespanSelector::set_selection_state_of_all_timespans), true
+                                       )
+                               );
+               option_hbox.pack_start (*b, false, false, 6);
+
+               b = Gtk::manage (new Gtk::Button (_("Deselect All")));
+               b->signal_clicked().connect (
+                               sigc::bind (
+                                       sigc::mem_fun (*this, &ExportTimespanSelector::set_selection_state_of_all_timespans), false
+                                       )
+                               );
+               option_hbox.pack_start (*b, false, false, 6);
+       }
+       option_hbox.pack_start (realtime_checkbutton, false, false, 6);
+       realtime_checkbutton.set_active (session->config.get_realtime_export ());
+
+       realtime_checkbutton.signal_toggled ().connect (
+                       sigc::mem_fun (*this, &ExportTimespanSelector::toggle_realtime)
+                       );
+
        range_scroller.add (range_view);
 
        pack_start (option_hbox, false, false, 0);
@@ -65,7 +92,6 @@ ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, Profi
 
        time_format_list = Gtk::ListStore::create (time_format_cols);
        time_format_combo.set_model (time_format_list);
-       time_format_combo.set_name ("PaddedButton");
 
        iter = time_format_list->append();
        row = *iter;
@@ -90,6 +116,9 @@ ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, Profi
        /* Range view */
 
        range_list = Gtk::ListStore::create (range_cols);
+       // order by location start times
+       range_list->set_sort_column(range_cols.location, Gtk::SORT_ASCENDING);
+       range_list->set_sort_func(range_cols.location, sigc::mem_fun(*this, &ExportTimespanSelector::location_sorter));
        range_view.set_model (range_list);
        range_view.set_headers_visible (true);
 }
@@ -99,15 +128,29 @@ ExportTimespanSelector::~ExportTimespanSelector ()
 
 }
 
+int
+ExportTimespanSelector::location_sorter(Gtk::TreeModel::iterator a, Gtk::TreeModel::iterator b)
+{
+       Location *l1 = (*a)[range_cols.location];
+       Location *l2 = (*b)[range_cols.location];
+       const Location *ls = _session->locations()->session_range_location();
+
+       // always sort session range first
+       if (l1 == ls)
+               return -1;
+       if (l2 == ls)
+               return +1;
+
+       return l1->start() - l2->start();
+}
+
 void
-ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc)
+ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc, bool rt)
 {
-       TimespanPtr span = _session->get_export_handler()->add_timespan();
+       ExportTimespanPtr span = _session->get_export_handler()->add_timespan();
 
        std::string id;
-       if (loc == state->session_range.get()) {
-               id = "session";
-       } else if (loc == state->selection_range.get()) {
+       if (loc == state->selection_range.get()) {
                id = "selection";
        } else {
                id = loc->id().to_s();
@@ -116,6 +159,7 @@ ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc)
        span->set_range (loc->start(), loc->end());
        span->set_name (loc->name());
        span->set_range_id (id);
+       span->set_realtime (rt);
        state->timespans->push_back (span);
 }
 
@@ -138,6 +182,30 @@ ExportTimespanSelector::sync_with_manager ()
        CriticalSelectionChanged();
 }
 
+void
+ExportTimespanSelector::allow_realtime_export (bool yn)
+{
+       if (_realtime_available == yn) {
+               return;
+       }
+       _realtime_available = yn;
+       realtime_checkbutton.set_sensitive (_realtime_available);
+       update_timespans ();
+}
+
+void
+ExportTimespanSelector::toggle_realtime ()
+{
+       const bool realtime = !_session->config.get_realtime_export ();
+       _session->config.set_realtime_export (realtime);
+       realtime_checkbutton.set_inconsistent (false);
+       realtime_checkbutton.set_active (realtime);
+
+       for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
+               it->set_value (range_cols.realtime, realtime);
+       }
+}
+
 void
 ExportTimespanSelector::change_time_format ()
 {
@@ -157,45 +225,34 @@ ExportTimespanSelector::construct_label (ARDOUR::Location const * location) cons
        std::string start;
        std::string end;
 
-       framepos_t start_frame = location->start();
-       framepos_t end_frame = location->end();
+       samplepos_t start_sample = location->start();
+       samplepos_t end_sample = location->end();
 
        switch (state->time_format) {
-         case AudioClock::BBT:
-               start = bbt_str (start_frame);
-               end = bbt_str (end_frame);
+         case ExportProfileManager::BBT:
+               start = bbt_str (start_sample);
+               end = bbt_str (end_sample);
                break;
 
-         case AudioClock::Timecode:
-               start = timecode_str (start_frame);
-               end = timecode_str (end_frame);
+         case ExportProfileManager::Timecode:
+               start = timecode_str (start_sample);
+               end = timecode_str (end_sample);
                break;
 
-         case AudioClock::MinSec:
-               start = ms_str (start_frame);
-               end = ms_str (end_frame);
+         case ExportProfileManager::MinSec:
+               start = ms_str (start_sample);
+               end = ms_str (end_sample);
                break;
 
-         case AudioClock::Frames:
-               start = to_string (start_frame, std::dec);
-               end = to_string (end_frame, std::dec);
-               break;
-
-         case AudioClock::Off:
+         case ExportProfileManager::Samples:
+               start = to_string (start_sample);
+               end = to_string (end_sample);
                break;
        }
 
-       // label += _("from ");
-
-       // label += "<span color=\"#7fff7f\">";
        label += start;
-//     label += "</span>";
-
        label += _(" to ");
-
-//     label += "<span color=\"#7fff7f\">";
        label += end;
-//     label += "</span>";
 
        return label;
 }
@@ -208,13 +265,13 @@ ExportTimespanSelector::construct_length (ARDOUR::Location const * location) con
        }
 
        std::stringstream s;
-       
+
        switch (state->time_format) {
-       case AudioClock::BBT:
+       case ExportProfileManager::BBT:
                s << bbt_str (location->length ());
                break;
 
-       case AudioClock::Timecode:
+       case ExportProfileManager::Timecode:
        {
                Timecode::Time tc;
                _session->timecode_duration (location->length(), tc);
@@ -222,16 +279,13 @@ ExportTimespanSelector::construct_length (ARDOUR::Location const * location) con
                break;
        }
 
-       case AudioClock::MinSec:
+       case ExportProfileManager::MinSec:
                s << ms_str (location->length ());
                break;
-               
-       case AudioClock::Frames:
+
+       case ExportProfileManager::Samples:
                s << location->length ();
                break;
-               
-       case AudioClock::Off:
-               break;
        }
 
        return s.str ();
@@ -239,7 +293,7 @@ ExportTimespanSelector::construct_length (ARDOUR::Location const * location) con
 
 
 std::string
-ExportTimespanSelector::bbt_str (framepos_t frames) const
+ExportTimespanSelector::bbt_str (samplepos_t samples) const
 {
        if (!_session) {
                return "Error!";
@@ -247,14 +301,14 @@ ExportTimespanSelector::bbt_str (framepos_t frames) const
 
        std::ostringstream oss;
        Timecode::BBT_Time time;
-       _session->bbt_time (frames, time);
+       _session->bbt_time (samples, time);
 
        print_padded (oss, time);
        return oss.str ();
 }
 
 std::string
-ExportTimespanSelector::timecode_str (framecnt_t frames) const
+ExportTimespanSelector::timecode_str (samplecnt_t samples) const
 {
        if (!_session) {
                return "Error!";
@@ -263,7 +317,7 @@ ExportTimespanSelector::timecode_str (framecnt_t frames) const
        std::ostringstream oss;
        Timecode::Time time;
 
-       _session->timecode_time (frames, time);
+       _session->timecode_time (samples, time);
 
        oss << std::setfill('0') << std::right <<
          std::setw(2) <<
@@ -279,27 +333,27 @@ ExportTimespanSelector::timecode_str (framecnt_t frames) const
 }
 
 std::string
-ExportTimespanSelector::ms_str (framecnt_t frames) const
+ExportTimespanSelector::ms_str (samplecnt_t samples) const
 {
        if (!_session) {
                return "Error!";
        }
 
        std::ostringstream oss;
-       framecnt_t left;
+       samplecnt_t left;
        int hrs;
        int mins;
        int secs;
        int sec_promilles;
 
-       left = frames;
-       hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
-       left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
-       mins = (int) floor (left / (_session->frame_rate() * 60.0f));
-       left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
-       secs = (int) floor (left / (float) _session->frame_rate());
-       left -= (framecnt_t) floor (secs * _session->frame_rate());
-       sec_promilles = (int) (left * 1000 / (float) _session->frame_rate() + 0.5);
+       left = samples;
+       hrs = (int) floor (left / (_session->sample_rate() * 60.0f * 60.0f));
+       left -= (samplecnt_t) floor (hrs * _session->sample_rate() * 60.0f * 60.0f);
+       mins = (int) floor (left / (_session->sample_rate() * 60.0f));
+       left -= (samplecnt_t) floor (mins * _session->sample_rate() * 60.0f);
+       secs = (int) floor (left / (float) _session->sample_rate());
+       left -= (samplecnt_t) floor ((double)(secs * _session->sample_rate()));
+       sec_promilles = (int) (left * 1000 / (float) _session->sample_rate() + 0.5);
 
        oss << std::setfill('0') << std::right <<
          std::setw(2) <<
@@ -323,22 +377,29 @@ ExportTimespanSelector::update_range_name (std::string const & path, std::string
        CriticalSelectionChanged();
 }
 
+void
+ExportTimespanSelector::set_selection_state_of_all_timespans (bool s)
+{
+       for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
+               it->set_value (range_cols.selected, s);
+       }
+}
+
 /*** ExportTimespanSelectorSingle ***/
 
 ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * session, ProfileManagerPtr manager, std::string range_id) :
-       ExportTimespanSelector (session, manager),
+       ExportTimespanSelector (session, manager, false),
        range_id (range_id)
 {
        range_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
+       range_view.append_column_editable (_("RT"), range_cols.realtime);
        range_view.append_column_editable (_("Range"), range_cols.name);
 
-       // Adjust selector height
-       int x_offset, y_offset, width, height;
-       Gtk::CellRenderer * renderer = *range_view.get_column(0)->get_cell_renderers().begin();
-       renderer->get_size (range_view, x_offset, y_offset, width, height);
-       range_scroller.set_size_request (-1, height);
+       if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
+               renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_timespans)));
+       }
 
-       if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (0))) {
+       if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (1))) {
                renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_range_name));
        }
 
@@ -348,17 +409,24 @@ ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * se
        range_view.append_column (*label_col);
 
        range_view.append_column (_("Length"), range_cols.length);
+       range_view.append_column (_("Creation Date"), range_cols.date);
+}
+
+void
+ExportTimespanSelectorSingle::allow_realtime_export (bool yn)
+{
+       ExportTimespanSelector::allow_realtime_export (yn);
+       range_view.get_column (0)->set_visible (_realtime_available);
 }
 
 void
 ExportTimespanSelectorSingle::fill_range_list ()
 {
        if (!state) { return; }
+       const bool realtime = _session->config.get_realtime_export ();
 
        std::string id;
-       if (!range_id.compare (X_("session"))) {
-               id = state->session_range->id().to_s();
-       } else if (!range_id.compare (X_("selection"))) {
+       if (!range_id.compare (X_("selection"))) {
                id = state->selection_range->id().to_s();
        } else {
                id = range_id;
@@ -377,11 +445,17 @@ ExportTimespanSelectorSingle::fill_range_list ()
 
                        row[range_cols.location] = *it;
                        row[range_cols.selected] = true;
+                       row[range_cols.realtime] = realtime;
                        row[range_cols.name] = (*it)->name();
                        row[range_cols.label] = construct_label (*it);
                        row[range_cols.length] = construct_length (*it);
 
-                       add_range_to_selection (*it);
+                       Glib::DateTime gdt(Glib::DateTime::create_now_local ((*it)->timestamp()));
+                       row[range_cols.timestamp] = (*it)->timestamp();
+                       row[range_cols.date] = gdt.format ("%F %H:%M");;
+
+
+                       add_range_to_selection (*it, false);
 
                        break;
                }
@@ -390,19 +464,39 @@ ExportTimespanSelectorSingle::fill_range_list ()
        set_time_format_from_state();
 }
 
+void
+ExportTimespanSelectorSingle::update_timespans ()
+{
+       state->timespans->clear();
+       const bool realtime = _session->config.get_realtime_export ();
+       bool inconsistent = false;
+
+       for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
+               add_range_to_selection (it->get_value (range_cols.location), it->get_value (range_cols.realtime) && _realtime_available);
+               if (it->get_value (range_cols.realtime) != realtime) {
+                       inconsistent = true;
+               }
+       }
+       realtime_checkbutton.set_inconsistent (inconsistent);
+}
+
 /*** ExportTimespanSelectorMultiple ***/
 
 ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session * session, ProfileManagerPtr manager) :
-  ExportTimespanSelector (session, manager)
+  ExportTimespanSelector (session, manager, true)
 {
        range_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        range_view.append_column_editable ("", range_cols.selected);
+       range_view.append_column_editable (_("RT"), range_cols.realtime);
        range_view.append_column_editable (_("Range"), range_cols.name);
 
        if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
                renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
        }
-       if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (1))) {
+       if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (1))) {
+               renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
+       }
+       if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (2))) {
                renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_range_name));
        }
 
@@ -412,12 +506,26 @@ ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session
        range_view.append_column (*label_col);
 
        range_view.append_column (_("Length"), range_cols.length);
+       range_view.append_column (_("Creation Date"), range_cols.date);
+
+       range_list->set_sort_column(5, Gtk::SORT_DESCENDING);
+       Gtk::TreeViewColumn* date_col = range_view.get_column(5); // date column
+       date_col->set_sort_column(7); // set sort as the timestamp
+
+}
+
+void
+ExportTimespanSelectorMultiple::allow_realtime_export (bool yn)
+{
+       ExportTimespanSelector::allow_realtime_export (yn);
+       range_view.get_column (1)->set_visible (_realtime_available);
 }
 
 void
 ExportTimespanSelectorMultiple::fill_range_list ()
 {
        if (!state) { return; }
+       const bool realtime = _session->config.get_realtime_export ();
 
        range_list->clear();
 
@@ -430,9 +538,15 @@ ExportTimespanSelectorMultiple::fill_range_list ()
 
                row[range_cols.location] = *it;
                row[range_cols.selected] = false;
+               row[range_cols.realtime] = realtime;
                row[range_cols.name] = (*it)->name();
                row[range_cols.label] = construct_label (*it);
                row[range_cols.length] = construct_length (*it);
+
+               Glib::DateTime gdt(Glib::DateTime::create_now_local ((*it)->timestamp()));
+               row[range_cols.timestamp] = (*it)->timestamp();
+               row[range_cols.date] = gdt.format ("%F %H:%M");;
+
        }
 
        set_selection_from_state ();
@@ -448,10 +562,10 @@ ExportTimespanSelectorMultiple::set_selection_from_state ()
                for (tree_it = range_list->children().begin(); tree_it != range_list->children().end(); ++tree_it) {
                        Location * loc = tree_it->get_value (range_cols.location);
 
-                       if ((!id.compare ("session") && loc == state->session_range.get()) ||
-                           (!id.compare ("selection") && loc == state->selection_range.get()) ||
-                           (!id.compare (loc->id().to_s()))) {
+                       if ((id == "selection" && loc == state->selection_range.get()) ||
+                           (id == loc->id().to_s())) {
                                tree_it->set_value (range_cols.selected, true);
+                               tree_it->set_value (range_cols.realtime, (*it)->realtime ());
                        }
                }
        }
@@ -470,11 +584,17 @@ void
 ExportTimespanSelectorMultiple::update_timespans ()
 {
        state->timespans->clear();
+       const bool realtime = _session->config.get_realtime_export ();
+       bool inconsistent = false;
 
        for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
                if (it->get_value (range_cols.selected)) {
-                       add_range_to_selection (it->get_value (range_cols.location));
+                       add_range_to_selection (it->get_value (range_cols.location), it->get_value (range_cols.realtime) && _realtime_available);
+               }
+               if (it->get_value (range_cols.realtime) != realtime) {
+                       inconsistent = true;
                }
        }
+       realtime_checkbutton.set_inconsistent (inconsistent);
 }