Use MTC instead of MIDI Timecode as the string for the external sync button. Fixes...
[ardour.git] / gtk2_ardour / audio_clock.cc
index c37c6cab9c5391c988c9f08712240988f44827f5..f0ae7c76f4779d4be922b980c974a9bad14cfa13 100644 (file)
@@ -23,6 +23,7 @@
 #include "pbd/convert.h"
 #include "pbd/enumwriter.h"
 
+#include <gtkmm/style.h>
 #include <gtkmm2ext/utils.h>
 
 #include "ardour/ardour.h"
@@ -35,6 +36,7 @@
 #include "audio_clock.h"
 #include "utils.h"
 #include "keyboard.h"
+#include "gui_thread.h"
 #include "i18n.h"
 
 using namespace ARDOUR;
@@ -64,7 +66,7 @@ const uint32_t AudioClock::field_length[(int) AudioClock::AudioFrames+1] = {
        10   /* Audio Frame */
 };
 
-AudioClock::AudioClock (std::string clock_name, bool transient, std::string widget_name, 
+AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name, 
                        bool allow_edit, bool follows_playhead, bool duration, bool with_info)
        : _name (clock_name),
          is_transient (transient),
@@ -214,8 +216,6 @@ AudioClock::AudioClock (std::string clock_name, bool transient, std::string widg
        clock_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
        clock_base.signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &AudioClock::field_button_release_event), Timecode_Hours));
 
-       Session::TimecodeOffsetChanged.connect (sigc::mem_fun (*this, &AudioClock::timecode_offset_changed));
-
        if (editable) {
                setup_events ();
        }
@@ -414,7 +414,7 @@ AudioClock::on_realize ()
 }
 
 void
-AudioClock::set (nframes_t when, bool force, nframes_t offset, char which)
+AudioClock::set (framepos_t when, bool force, framecnt_t offset, char which)
 {
 
        if ((!force && !is_visible()) || _session == 0) {
@@ -473,14 +473,18 @@ AudioClock::set (nframes_t when, bool force, nframes_t offset, char which)
 }
 
 void
-AudioClock::timecode_offset_changed ()
+AudioClock::session_configuration_changed (std::string p)
 {
-       nframes_t current;
+       if (p != "timecode-offset" && p != "timecode-offset-negative") {
+               return;
+       }
+       
+       framecnt_t current;
 
        switch (_mode) {
        case Timecode:
                if (is_duration) {
-                       current = current_duration();
+                       current = current_duration ();
                } else {
                        current = current_time ();
                }
@@ -492,17 +496,17 @@ AudioClock::timecode_offset_changed ()
 }
 
 void
-AudioClock::set_frames (nframes_t when, bool /*force*/)
+AudioClock::set_frames (framepos_t when, bool /*force*/)
 {
        char buf[32];
-       snprintf (buf, sizeof (buf), "%u", when);
+       snprintf (buf, sizeof (buf), "%" PRId64, when);
        audio_frames_label.set_text (buf);
 
        if (frames_upper_info_label) {
-               nframes_t rate = _session->frame_rate();
+               framecnt_t rate = _session->frame_rate();
 
                if (fmod (rate, 1000.0) == 0.000) {
-                       sprintf (buf, "%uK", rate/1000);
+                       sprintf (buf, "%" PRId64 "K", rate/1000);
                } else {
                        sprintf (buf, "%.3fK", rate/1000.0f);
                }
@@ -527,19 +531,19 @@ AudioClock::set_frames (nframes_t when, bool /*force*/)
 }
 
 void
-AudioClock::set_minsec (nframes_t when, bool force)
+AudioClock::set_minsec (framepos_t when, bool force)
 {
        char buf[32];
-       nframes_t left;
+       framecnt_t left;
        int hrs;
        int mins;
        float secs;
 
        left = when;
        hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
-       left -= (nframes_t) floor (hrs * _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 -= (nframes_t) floor (mins * _session->frame_rate() * 60.0f);
+       left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
        secs = left / (float) _session->frame_rate();
 
        if (force || hrs != ms_last_hrs) {
@@ -562,7 +566,7 @@ AudioClock::set_minsec (nframes_t when, bool force)
 }
 
 void
-AudioClock::set_timecode (nframes_t when, bool force)
+AudioClock::set_timecode (framepos_t when, bool force)
 {
        char buf[32];
        Timecode::Time timecode;
@@ -633,10 +637,10 @@ AudioClock::set_timecode (nframes_t when, bool force)
 }
 
 void
-AudioClock::set_bbt (nframes_t when, bool force)
+AudioClock::set_bbt (framepos_t when, bool force)
 {
        char buf[16];
-       BBT_Time bbt;
+       Timecode::BBT_Time bbt;
 
        /* handle a common case */
        if (is_duration) {
@@ -667,7 +671,7 @@ AudioClock::set_bbt (nframes_t when, bool force)
        }
 
        if (bbt_upper_info_label) {
-               nframes64_t pos;
+               framepos_t pos;
 
                if (bbt_reference_time < 0) {
                        pos = when;
@@ -695,6 +699,8 @@ AudioClock::set_session (Session *s)
 
        if (_session) {
 
+               _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
+
                XMLProperty* prop;
                XMLNode* node = _session->extra_xml (X_("ClockModes"));
                AudioClock::Mode amode;
@@ -1192,7 +1198,7 @@ AudioClock::field_button_press_event (GdkEventButton *ev, Field /*field*/)
                return false;
        }
 
-       nframes_t frames = 0;
+       framepos_t frames = 0;
 
        switch (ev->button) {
        case 1:
@@ -1201,7 +1207,7 @@ AudioClock::field_button_press_event (GdkEventButton *ev, Field /*field*/)
                        ValueChanged (); /* EMIT_SIGNAL */
                                        }
 
-                /* make absolutely sure that the pointer is grabbed */
+               /* make absolutely sure that the pointer is grabbed */
                gdk_pointer_grab(ev->window,false ,
                                 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
                                 NULL,NULL,ev->time);
@@ -1238,7 +1244,7 @@ AudioClock::field_button_scroll_event (GdkEventScroll *ev, Field field)
                return false;
        }
 
-       nframes_t frames = 0;
+       framepos_t frames = 0;
 
        switch (ev->direction) {
 
@@ -1307,8 +1313,8 @@ AudioClock::field_motion_notify_event (GdkEventMotion *ev, Field field)
 
        if (trunc(drag_accum) != 0) {
 
-               nframes_t frames;
-               nframes_t pos ;
+               framepos_t frames;
+               framepos_t pos;
                int dir;
                dir = (drag_accum < 0 ? 1:-1);
                pos = current_time();
@@ -1316,7 +1322,7 @@ AudioClock::field_motion_notify_event (GdkEventMotion *ev, Field field)
 
                if (frames  != 0 &&  frames * drag_accum < current_time()) {
 
-                       set ((nframes_t) floor (pos - drag_accum * frames), false); // minus because up is negative in computer-land
+                       set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in computer-land
 
                } else {
                        set (0 , false);
@@ -1332,24 +1338,23 @@ AudioClock::field_motion_notify_event (GdkEventMotion *ev, Field field)
        return true;
 }
 
-nframes_t
-AudioClock::get_frames (Field field,nframes_t pos,int dir)
+framepos_t
+AudioClock::get_frames (Field field, framepos_t pos, int dir)
 {
-
-       nframes_t frames = 0;
-       BBT_Time bbt;
+       framecnt_t frames = 0;
+       Timecode::BBT_Time bbt;
        switch (field) {
        case Timecode_Hours:
-               frames = (nframes_t) floor (3600.0 * _session->frame_rate());
+               frames = (framecnt_t) floor (3600.0 * _session->frame_rate());
                break;
        case Timecode_Minutes:
-               frames = (nframes_t) floor (60.0 * _session->frame_rate());
+               frames = (framecnt_t) floor (60.0 * _session->frame_rate());
                break;
        case Timecode_Seconds:
                frames = _session->frame_rate();
                break;
        case Timecode_Frames:
-               frames = (nframes_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
+               frames = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
                break;
 
        case AudioFrames:
@@ -1357,10 +1362,10 @@ AudioClock::get_frames (Field field,nframes_t pos,int dir)
                break;
 
        case MS_Hours:
-               frames = (nframes_t) floor (3600.0 * _session->frame_rate());
+               frames = (framecnt_t) floor (3600.0 * _session->frame_rate());
                break;
        case MS_Minutes:
-               frames = (nframes_t) floor (60.0 * _session->frame_rate());
+               frames = (framecnt_t) floor (60.0 * _session->frame_rate());
                break;
        case MS_Seconds:
                frames = _session->frame_rate();
@@ -1389,10 +1394,10 @@ AudioClock::get_frames (Field field,nframes_t pos,int dir)
        return frames;
 }
 
-nframes_t
-AudioClock::current_time (nframes_t pos) const
+framepos_t
+AudioClock::current_time (framepos_t pos) const
 {
-       nframes_t ret = 0;
+       framepos_t ret = 0;
 
        switch (_mode) {
        case Timecode:
@@ -1417,10 +1422,10 @@ AudioClock::current_time (nframes_t pos) const
        return ret;
 }
 
-nframes_t
-AudioClock::current_duration (nframes_t pos) const
+framepos_t
+AudioClock::current_duration (framepos_t pos) const
 {
-       nframes_t ret = 0;
+       framepos_t ret = 0;
 
        switch (_mode) {
        case Timecode:
@@ -1484,7 +1489,7 @@ AudioClock::timecode_sanitize_display()
        }
 }
 
-nframes_t
+framepos_t
 AudioClock::timecode_frame_from_display () const
 {
        if (_session == 0) {
@@ -1492,7 +1497,7 @@ AudioClock::timecode_frame_from_display () const
        }
 
        Timecode::Time timecode;
-       nframes_t sample;
+       framepos_t sample;
 
        timecode.hours = atoi (hours_label.get_text());
        timecode.minutes = atoi (minutes_label.get_text());
@@ -1515,12 +1520,12 @@ AudioClock::timecode_frame_from_display () const
 
        // Testcode for timecode<->sample conversions (P.S.)
        Timecode::Time timecode1;
-       nframes_t sample1;
-       nframes_t oldsample = 0;
+       framepos_t sample1;
+       framepos_t oldsample = 0;
        Timecode::Time timecode2;
-       nframes_t sample_increment;
+       framecnt_t sample_increment;
 
-       sample_increment = (long)rint(_session->frame_rate() / _session->timecode_frames_per_second);
+       sample_increment = (framecnt_t)rint(_session->frame_rate() / _session->timecode_frames_per_second);
 
 #ifdef Timecode_SAMPLE_TEST_1
        // Test 1: use_offset = false, use_subframes = false
@@ -1877,7 +1882,7 @@ AudioClock::timecode_frame_from_display () const
        return sample;
 }
 
-nframes_t
+framepos_t
 AudioClock::minsec_frame_from_display () const
 {
        if (_session == 0) {
@@ -1888,13 +1893,13 @@ AudioClock::minsec_frame_from_display () const
        int mins = atoi (ms_minutes_label.get_text());
        float secs = atof (ms_seconds_label.get_text());
 
-       nframes_t sr = _session->frame_rate();
+       framecnt_t sr = _session->frame_rate();
 
-       return (nframes_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr));
+       return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr));
 }
 
-nframes_t
-AudioClock::bbt_frame_from_display (nframes_t pos) const
+framepos_t
+AudioClock::bbt_frame_from_display (framepos_t pos) const
 {
        if (_session == 0) {
                error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
@@ -1908,26 +1913,24 @@ AudioClock::bbt_frame_from_display (nframes_t pos) const
        any.bbt.beats = atoi (beats_label.get_text());
        any.bbt.ticks = atoi (ticks_label.get_text());
 
-       if (is_duration) {
-               any.bbt.bars++;
-               any.bbt.beats++;
-       }
-
-       nframes_t ret = _session->convert_to_frames_at (pos, any);
+       if (is_duration) {
+               any.bbt.bars++;
+               any.bbt.beats++;
+       }
 
-       return ret;
+       return _session->convert_to_frames_at (pos, any);
 }
 
 
-nframes_t
-AudioClock::bbt_frame_duration_from_display (nframes_t pos) const
+framepos_t
+AudioClock::bbt_frame_duration_from_display (framepos_t pos) const
 {
        if (_session == 0) {
                error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
                return 0;
        }
 
-       BBT_Time bbt;
+       Timecode::BBT_Time bbt;
 
 
        bbt.bars = atoi (bars_label.get_text());
@@ -1937,10 +1940,10 @@ AudioClock::bbt_frame_duration_from_display (nframes_t pos) const
        return _session->tempo_map().bbt_duration_at(pos,bbt,1);
 }
 
-nframes_t
+framepos_t
 AudioClock::audio_frame_from_display () const
 {
-       return (nframes_t) atoi (audio_frames_label.get_text());
+       return (framepos_t) atoi (audio_frames_label.get_text());
 }
 
 void
@@ -1962,7 +1965,7 @@ AudioClock::build_ops_menu ()
        if (editable && !is_duration && !_follows_playhead) {
                ops_items.push_back (SeparatorElem());
                ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
-               ops_items.push_back (MenuElem (_("Locate to this time"), sigc::mem_fun(*this, &AudioClock::locate)));
+               ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
        }
 }
 
@@ -1983,7 +1986,7 @@ AudioClock::locate ()
                return;
        }
        
-       _session->request_locate (current_time(), false);
+       _session->request_locate (current_time(), _session->transport_rolling ());
 }
 
 void
@@ -2043,30 +2046,30 @@ AudioClock::set_mode (Mode m)
 void
 AudioClock::set_size_requests ()
 {
-       /* note that in some fonts, "88" is narrower than "00", hence the 2 pixel padding */
+       /* note that in some fonts, "88" is narrower than "00" */
 
        switch (_mode) {
        case Timecode:
-               Gtkmm2ext::set_size_request_to_display_given_text (hours_label, "-00", 5, 5);
-               Gtkmm2ext::set_size_request_to_display_given_text (minutes_label, "00", 5, 5);
-               Gtkmm2ext::set_size_request_to_display_given_text (seconds_label, "00", 5, 5);
-               Gtkmm2ext::set_size_request_to_display_given_text (frames_label, "00", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (hours_label, "-88", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (minutes_label, "88", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (seconds_label, "88", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (frames_label, "88", 5, 5);
                break;
 
        case BBT:
-               Gtkmm2ext::set_size_request_to_display_given_text (bars_label, "-000", 5, 5);
-               Gtkmm2ext::set_size_request_to_display_given_text (beats_label, "00", 5, 5);
-               Gtkmm2ext::set_size_request_to_display_given_text (ticks_label, "0000", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (bars_label, "-888", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (beats_label, "88", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (ticks_label, "8888", 5, 5);
                break;
 
        case MinSec:
-               Gtkmm2ext::set_size_request_to_display_given_text (ms_hours_label, "00", 5, 5);
-               Gtkmm2ext::set_size_request_to_display_given_text (ms_minutes_label, "00", 5, 5);
-               Gtkmm2ext::set_size_request_to_display_given_text (ms_seconds_label, "00.000", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (ms_hours_label, "88", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (ms_minutes_label, "88", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (ms_seconds_label, "88.888", 5, 5);
                break;
 
        case Frames:
-               Gtkmm2ext::set_size_request_to_display_given_text (audio_frames_label, "0000000000", 5, 5);
+               Gtkmm2ext::set_size_request_to_display_given_text (audio_frames_label, "8888888888", 5, 5);
                break;
 
        case Off:
@@ -2077,7 +2080,62 @@ AudioClock::set_size_requests ()
 }
 
 void
-AudioClock::set_bbt_reference (nframes64_t pos)
+AudioClock::set_bbt_reference (framepos_t pos)
 {
        bbt_reference_time = pos;
 }
+
+void
+AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
+{
+       HBox::on_style_changed (old_style);
+
+       /* propagate style changes to all component widgets that should inherit the main one */
+
+       Glib::RefPtr<RcStyle> rcstyle = get_modifier_style();
+
+       clock_base.modify_style (rcstyle);
+       audio_frames_label.modify_style (rcstyle);
+       hours_label.modify_style (rcstyle);
+       minutes_label.modify_style (rcstyle);
+       seconds_label.modify_style (rcstyle);
+       frames_label.modify_style (rcstyle);
+       bars_label.modify_style (rcstyle);
+       beats_label.modify_style (rcstyle);
+       ticks_label.modify_style (rcstyle);
+       ms_hours_label.modify_style (rcstyle);
+       ms_minutes_label.modify_style (rcstyle);
+       ms_seconds_label.modify_style (rcstyle);
+       hours_ebox.modify_style (rcstyle);
+       minutes_ebox.modify_style (rcstyle);
+       seconds_ebox.modify_style (rcstyle);
+       frames_ebox.modify_style (rcstyle);
+       audio_frames_ebox.modify_style (rcstyle);
+       bars_ebox.modify_style (rcstyle);
+       beats_ebox.modify_style (rcstyle);
+       ticks_ebox.modify_style (rcstyle);
+       ms_hours_ebox.modify_style (rcstyle);
+       ms_minutes_ebox.modify_style (rcstyle);
+       ms_seconds_ebox.modify_style (rcstyle);
+
+       colon1.modify_style (rcstyle);
+       colon2.modify_style (rcstyle);
+       colon3.modify_style (rcstyle);
+       colon4.modify_style (rcstyle);
+       colon5.modify_style (rcstyle);
+       b1.modify_style (rcstyle);
+       b2.modify_style (rcstyle);
+
+       set_size_requests ();
+}
+
+void
+AudioClock::set_is_duration (bool yn)
+{
+       if (yn == is_duration) {
+               return;
+       }
+        
+       is_duration = yn;
+       set (last_when, true, 0, 's');
+}