add option to resize meterbridge track-labels
[ardour.git] / gtk2_ardour / meterbridge.cc
index 99b80fb668fe93e4204f126cf64f43fe94a6e4e5..a0110365c18584a2de2dbe0543eb1447ab1ff88d 100644 (file)
@@ -51,6 +51,8 @@
 #include "route_sorter.h"
 #include "actions.h"
 #include "gui_thread.h"
+#include "global_signals.h"
+#include "meter_patterns.h"
 
 #include "i18n.h"
 
@@ -60,6 +62,7 @@ using namespace Gtk;
 using namespace Glib;
 using namespace Gtkmm2ext;
 using namespace std;
+using namespace ArdourMeter;
 
 using PBD::atoi;
 
@@ -93,34 +96,13 @@ struct SignalOrderRouteSorter {
        }
 };
 
-/* modified version of above
- * used in Meterbridge::sync_order_keys()
- */
-struct MeterOrderRouteSorter {
-       bool operator() (MeterStrip *ma, MeterStrip *mb) {
-               boost::shared_ptr<Route> a = ma->route();
-               boost::shared_ptr<Route> b = mb->route();
-               if (a->is_master() || a->is_monitor()) {
-                       /* "a" is a special route (master, monitor, etc), and comes
-                        * last in the mixer ordering
-                        */
-                       return false;
-               } else if (b->is_master() || b->is_monitor()) {
-                       /* everything comes before b */
-                       return true;
-               }
-               return a->order_key (MixerSort) < b->order_key (MixerSort);
-       }
-};
-
-
 Meterbridge::Meterbridge ()
        : Window (Gtk::WINDOW_TOPLEVEL)
        , VisibilityTracker (*((Gtk::Window*) this))
        , _visible (false)
        , _show_busses (false)
-       , metrics_left (1)
-       , metrics_right (2)
+       , metrics_left (1, MeterPeak)
+       , metrics_right (2, MeterPeak)
        , cur_max_width (-1)
 {
        set_name ("Meter Bridge");
@@ -137,7 +119,9 @@ Meterbridge::Meterbridge ()
        Gdk::Geometry geom;
        geom.max_width = 1<<16;
        geom.max_height = max_height;
-       set_geometry_hints(*((Gtk::Window*) this), geom, Gdk::HINT_MAX_SIZE);
+       geom.height_inc = 16;
+       geom.width_inc = 1;
+       set_geometry_hints(*((Gtk::Window*) this), geom, Gdk::HINT_MAX_SIZE | Gdk::HINT_RESIZE_INC);
 
        set_keep_above (true);
        set_border_width (0);
@@ -156,7 +140,8 @@ Meterbridge::Meterbridge ()
        signal_configure_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
        Route::SyncOrderKeys.connect (*this, invalidator (*this), boost::bind (&Meterbridge::sync_order_keys, this, _1), gui_context());
        MeterStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Meterbridge::remove_strip, this, _1), gui_context());
-       MeterStrip::MetricChanged.connect_same_thread (*this, boost::bind(&Meterbridge::update_metrics, this));
+       MeterStrip::MetricChanged.connect (*this, invalidator (*this), boost::bind(&Meterbridge::resync_order, this), gui_context());
+       MeterStrip::ConfigurationChanged.connect (*this, invalidator (*this), boost::bind(&Meterbridge::queue_resize, this), gui_context());
 
        /* work around ScrolledWindowViewport alignment mess Part one */
        Gtk::HBox * yspc = manage (new Gtk::HBox());
@@ -200,10 +185,18 @@ Meterbridge::Meterbridge ()
        Gtk::Viewport* viewport = (Gtk::Viewport*) scroller.get_child();
        viewport->set_shadow_type(Gtk::SHADOW_NONE);
        viewport->set_border_width(0);
+
+       UI::instance()->theme_changed.connect (sigc::mem_fun(*this, &Meterbridge::on_theme_changed));
+       ColorsChanged.connect (sigc::mem_fun (*this, &Meterbridge::on_theme_changed));
+       DPIReset.connect (sigc::mem_fun (*this, &Meterbridge::on_theme_changed));
 }
 
 Meterbridge::~Meterbridge ()
 {
+       while (_metrics.size() > 0) {
+               delete (_metrics.back());
+               _metrics.pop_back();
+       }
 }
 
 void
@@ -265,6 +258,7 @@ Meterbridge::get_window_pos_and_size ()
 bool
 Meterbridge::hide_window (GdkEventAny *ev)
 {
+       if (!_visible) return 0;
        get_window_pos_and_size();
        _visible = false;
        return just_hide_it(ev, static_cast<Gtk::Window *>(this));
@@ -321,6 +315,7 @@ Meterbridge::on_scroll_event (GdkEventScroll* ev)
 void
 Meterbridge::scroll_left ()
 {
+       if (!scroller.get_hscrollbar()) return;
        Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
        /* stupid GTK: can't rely on clamping across versions */
        scroller.get_hscrollbar()->set_value (max (adj->get_lower(), adj->get_value() - adj->get_step_increment()));
@@ -329,6 +324,7 @@ Meterbridge::scroll_left ()
 void
 Meterbridge::scroll_right ()
 {
+       if (!scroller.get_hscrollbar()) return;
        Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
        /* stupid GTK: can't rely on clamping across versions */
        scroller.get_hscrollbar()->set_value (min (adj->get_upper(), adj->get_value() + adj->get_step_increment()));
@@ -337,15 +333,40 @@ Meterbridge::scroll_right ()
 void
 Meterbridge::on_size_request (Gtk::Requisition* r)
 {
+       meter_clear_pattern_cache(3);
        Gtk::Window::on_size_request(r);
 
        Gdk::Geometry geom;
-       geom.max_width = meterarea.get_width() + metrics_left.get_width() + metrics_right.get_width();
+       Gtk::Requisition mr = meterarea.size_request();
+
+       geom.max_width = mr.width + metrics_left.get_width() + metrics_right.get_width();
        geom.max_height = max_height;
 
+#ifndef GTKOSX
+       /* on OSX this leads to a constant live-loop: show/hide scrollbar
+        * on Linux, the window is resized IFF the scrollbar was not visible
+        */
+       const Gtk::Scrollbar * hsc = scroller.get_hscrollbar();
+       Glib::RefPtr<Gdk::Screen> screen = get_screen ();
+       Gdk::Rectangle monitor_rect;
+       screen->get_monitor_geometry (0, monitor_rect);
+       const int scr_w = monitor_rect.get_width() - 44;
+
+       if (cur_max_width < geom.max_width
+                       && cur_max_width < scr_w
+                       && !(scroller.get_hscrollbar_visible() && hsc)) {
+               int h = r->height;
+               *r = Gtk::Requisition();
+               r->width = geom.max_width;
+               r->height = h;
+       }
+#endif
+
        if (cur_max_width != geom.max_width) {
                cur_max_width = geom.max_width;
-               set_geometry_hints(*((Gtk::Window*) this), geom, Gdk::HINT_MAX_SIZE);
+               geom.height_inc = 16;
+               geom.width_inc = 1;
+               set_geometry_hints(*((Gtk::Window*) this), geom, Gdk::HINT_MAX_SIZE | Gdk::HINT_RESIZE_INC);
        }
 }
 
@@ -355,6 +376,10 @@ Meterbridge::on_size_allocate (Gtk::Allocation& a)
        const Gtk::Scrollbar * hsc = scroller.get_hscrollbar();
 
        if (scroller.get_hscrollbar_visible() && hsc) {
+               if (!scroll_connection.connected()) {
+                       scroll_connection = scroller.get_hscrollbar()->get_adjustment()->signal_value_changed().connect(sigc::mem_fun (*this, &Meterbridge::on_scroll));
+                       scroller.get_hscrollbar()->get_adjustment()->signal_changed().connect(sigc::mem_fun (*this, &Meterbridge::on_scroll));
+               }
                gint scrollbar_spacing;
                gtk_widget_style_get (GTK_WIDGET (scroller.gobj()),
                                "scrollbar-spacing", &scrollbar_spacing, NULL);
@@ -368,6 +393,41 @@ Meterbridge::on_size_allocate (Gtk::Allocation& a)
        Gtk::Window::on_size_allocate(a);
 }
 
+void
+Meterbridge::on_scroll()
+{
+       if (!scroller.get_hscrollbar()) return;
+
+       Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
+       int leftend = adj->get_value();
+       int rightend = scroller.get_width() + leftend;
+
+       int mm_left = _mm_left;
+       int mm_right = _mm_right;
+       ARDOUR::MeterType mt_left = _mt_left;
+       ARDOUR::MeterType mt_right = _mt_right;
+
+       for (unsigned int i = 0; i < _metrics.size(); ++i) {
+               int sx, dx, dy;
+               int mm = _metrics[i]->get_metric_mode();
+               sx = (mm & 2) ? _metrics[i]->get_width() : 0;
+
+               _metrics[i]->translate_coordinates(meterarea, sx, 0, dx, dy);
+
+               if (dx < leftend && !(mm&2)) {
+                       mm_left = mm;
+                       mt_left = _metrics[i]->meter_type();
+               }
+               if (dx > rightend && (mm&2)) {
+                       mm_right = mm;
+                       mt_right = _metrics[i]->meter_type();
+                       break;
+               }
+       }
+       metrics_left.set_metric_mode(mm_left, mt_left);
+       metrics_right.set_metric_mode(mm_right, mt_right);
+}
+
 void
 Meterbridge::set_session (Session* s)
 {
@@ -377,6 +437,9 @@ Meterbridge::set_session (Session* s)
                return;
        }
 
+       metrics_left.set_session(s);
+       metrics_right.set_session(s);
+
        XMLNode* node = _session->instant_xml(X_("Meterbridge"));
        if (node) {
                set_state (*node);
@@ -398,6 +461,7 @@ Meterbridge::set_session (Session* s)
        _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Meterbridge::update_title, this), gui_context());
        _session->StateSaved.connect (_session_connections, invalidator (*this), boost::bind (&Meterbridge::update_title, this), gui_context());
        _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&Meterbridge::parameter_changed, this, _1), gui_context());
+       Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&Meterbridge::parameter_changed, this, _1), gui_context());
 
        if (_visible) {
                show_window();
@@ -411,8 +475,8 @@ Meterbridge::session_going_away ()
 {
        ENSURE_GUI_THREAD (*this, &Meterbridge::session_going_away);
 
-       for (list<MeterStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-               delete (*i);
+       for (list<MeterBridgeStrip>::iterator i = strips.begin(); i != strips.end(); ++i) {
+               delete ((*i).s);
        }
 
        strips.clear ();
@@ -481,26 +545,24 @@ Meterbridge::set_state (const XMLNode& node)
 XMLNode&
 Meterbridge::get_state (void)
 {
+       char buf[32];
        XMLNode* node = new XMLNode ("Meterbridge");
 
-       if (is_realized()) {
-               Glib::RefPtr<Gdk::Window> win = get_window();
-
+       if (is_realized() && _visible) {
                get_window_pos_and_size ();
-
-               XMLNode* geometry = new XMLNode ("geometry");
-               char buf[32];
-               snprintf(buf, sizeof(buf), "%d", m_width);
-               geometry->add_property(X_("x_size"), string(buf));
-               snprintf(buf, sizeof(buf), "%d", m_height);
-               geometry->add_property(X_("y_size"), string(buf));
-               snprintf(buf, sizeof(buf), "%d", m_root_x);
-               geometry->add_property(X_("x_pos"), string(buf));
-               snprintf(buf, sizeof(buf), "%d", m_root_y);
-               geometry->add_property(X_("y_pos"), string(buf));
-               node->add_child_nocopy (*geometry);
        }
 
+       XMLNode* geometry = new XMLNode ("geometry");
+       snprintf(buf, sizeof(buf), "%d", m_width);
+       geometry->add_property(X_("x_size"), string(buf));
+       snprintf(buf, sizeof(buf), "%d", m_height);
+       geometry->add_property(X_("y_size"), string(buf));
+       snprintf(buf, sizeof(buf), "%d", m_root_x);
+       geometry->add_property(X_("x_pos"), string(buf));
+       snprintf(buf, sizeof(buf), "%d", m_root_y);
+       geometry->add_property(X_("y_pos"), string(buf));
+       node->add_child_nocopy (*geometry);
+
        node->add_property ("show-meterbridge", _visible ? "yes" : "no");
        return *node;
 }
@@ -526,9 +588,9 @@ Meterbridge::fast_update_strips ()
        if (!is_mapped () || !_session) {
                return;
        }
-       for (list<MeterStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-               // TODO skip inactive/hidden routes
-               (*i)->fast_update ();
+       for (list<MeterBridgeStrip>::iterator i = strips.begin(); i != strips.end(); ++i) {
+               if (!(*i).visible) continue;
+               (*i).s->fast_update ();
        }
 }
 
@@ -546,15 +608,14 @@ Meterbridge::add_strips (RouteList& routes)
                }
 
                strip = new MeterStrip (_session, route);
-               strips.push_back (strip);
+               strips.push_back (MeterBridgeStrip(strip));
                route->active_changed.connect (*this, invalidator (*this), boost::bind (&Meterbridge::resync_order, this), gui_context ());
 
                meterarea.pack_start (*strip, false, false);
                strip->show();
        }
 
-       sync_order_keys(MixerSort);
-       update_metrics();
+       resync_order();
 }
 
 void
@@ -564,77 +625,154 @@ Meterbridge::remove_strip (MeterStrip* strip)
                return;
        }
 
-       list<MeterStrip *>::iterator i;
-       if ((i = find (strips.begin(), strips.end(), strip)) != strips.end()) {
-               strips.erase (i);
-       }
-       update_metrics();
-}
-
-void
-Meterbridge::update_metrics ()
-{
-       bool have_midi = false;
-       for (list<MeterStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-               if ( (*i)->has_midi ()) {
-                       have_midi = true;
+       list<MeterBridgeStrip>::iterator i;
+       for (list<MeterBridgeStrip>::iterator i = strips.begin(); i != strips.end(); ++i) {
+               if ( (*i).s == strip) {
+                       strips.erase (i);
                        break;
                }
        }
-       if (have_midi) {
-               metrics_right.set_metric_mode(2);
-       } else {
-               metrics_right.set_metric_mode(3);
-       }
 }
 
 void
-Meterbridge::sync_order_keys (RouteSortOrderKey src)
+Meterbridge::sync_order_keys (RouteSortOrderKey)
 {
+       Glib::Threads::Mutex::Lock lm (_resync_mutex);
+
        MeterOrderRouteSorter sorter;
-       std::list<MeterStrip *> copy (strips);
-       copy.sort(sorter);
+       strips.sort(sorter);
 
        int pos = 0;
+       int vis = 0;
+       MeterStrip * last = 0;
+
+       unsigned int metrics = 0;
+       MeterType lmt = MeterPeak;
+       bool have_midi = false;
+       metrics_left.set_metric_mode(1, lmt);
 
-       for (list<MeterStrip *>::iterator i = copy.begin(); i != copy.end(); ++i) {
+       for (list<MeterBridgeStrip>::iterator i = strips.begin(); i != strips.end(); ++i) {
 
-               if (! (*i)->route()->active()) {
-                       (*i)->hide();
+               if (! (*i).s->route()->active()) {
+                       (*i).s->hide();
+                       (*i).visible = false;
                }
-               else if ((*i)->route()->is_master()) {
+               else if ((*i).s->route()->is_master()) {
                        if (_show_master) {
-                               (*i)->show();
+                               (*i).s->show();
+                               (*i).visible = true;
+                               vis++;
                        } else {
-                               (*i)->hide();
+                               (*i).s->hide();
+                               (*i).visible = false;
                        }
                }
-               else if (boost::dynamic_pointer_cast<AudioTrack>((*i)->route()) == 0
-                               && boost::dynamic_pointer_cast<MidiTrack>((*i)->route()) == 0
+               else if (boost::dynamic_pointer_cast<AudioTrack>((*i).s->route()) == 0
+                               && boost::dynamic_pointer_cast<MidiTrack>((*i).s->route()) == 0
                                ) {
                        /* non-master bus */
                        if (_show_busses) {
-                               (*i)->show();
+                               (*i).s->show();
+                               (*i).visible = true;
+                               vis++;
                        } else {
-                               (*i)->hide();
+                               (*i).s->hide();
+                               (*i).visible = false;
                        }
                }
-               else if (boost::dynamic_pointer_cast<MidiTrack>((*i)->route())) {
+               else if (boost::dynamic_pointer_cast<MidiTrack>((*i).s->route())) {
                        if (_show_midi) {
-                               (*i)->show();
+                               (*i).s->show();
+                               (*i).visible = true;
+                               vis++;
                        } else {
-                               (*i)->hide();
+                               (*i).s->hide();
+                               (*i).visible = false;
                        }
                }
                else {
-                       (*i)->show();
+                       (*i).s->show();
+                       (*i).visible = true;
+                               vis++;
+               }
+
+               (*i).s->set_tick_bar(0);
+
+               MeterType nmt = (*i).s->meter_type();
+               if (nmt == MeterKrms) nmt = MeterPeak; // identical metrics
+               if (pos == 0) {
+                       (*i).s->set_tick_bar(1);
+               }
+
+               if ((*i).visible && nmt != lmt && pos == 0) {
+                       lmt = nmt;
+                       metrics_left.set_metric_mode(1, lmt);
+               } else if ((*i).visible && nmt != lmt) {
+
+                       if (last) {
+                               last->set_tick_bar(last->get_tick_bar() | 2);
+                       }
+                       (*i).s->set_tick_bar((*i).s->get_tick_bar() | 1);
+
+                       if (_metrics.size() <= metrics) {
+                               _metrics.push_back(new MeterStrip(have_midi ? 2 : 3, lmt));
+                               meterarea.pack_start (*_metrics[metrics], false, false);
+                               _metrics[metrics]->set_session(_session);
+                               _metrics[metrics]->show();
+                       } else {
+                               _metrics[metrics]->set_metric_mode(have_midi ? 2 : 3, lmt);
+                       }
+                       meterarea.reorder_child(*_metrics[metrics], pos++);
+                       metrics++;
+
+                       lmt = nmt;
+
+                       if (_metrics.size() <= metrics) {
+                               _metrics.push_back(new MeterStrip(1, lmt));
+                               meterarea.pack_start (*_metrics[metrics], false, false);
+                               _metrics[metrics]->set_session(_session);
+                               _metrics[metrics]->show();
+                       } else {
+                               _metrics[metrics]->set_metric_mode(1, lmt);
+                       }
+                       meterarea.reorder_child(*_metrics[metrics], pos++);
+                       metrics++;
+                       have_midi = false;
+               }
+
+               if ((*i).visible && (*i).s->has_midi()) {
+                       have_midi = true;
+               }
+
+               meterarea.reorder_child(*((*i).s), pos++);
+               if ((*i).visible) {
+                       last = (*i).s;
                }
-               meterarea.reorder_child(*(*i), pos++);
        }
+
+       if (last) {
+               last->set_tick_bar(last->get_tick_bar() | 2);
+       }
+
+       metrics_right.set_metric_mode(have_midi ? 2 : 3, lmt);
+
+       while (_metrics.size() > metrics) {
+               meterarea.remove(*_metrics.back());
+               delete (_metrics.back());
+               _metrics.pop_back();
+       }
+
+       _mm_left = metrics_left.get_metric_mode();
+       _mt_left = metrics_left.meter_type();
+       _mm_right = metrics_right.get_metric_mode();
+       _mt_right = metrics_right.meter_type();
+
+       on_scroll();
+       queue_resize();
 }
 
 void
-Meterbridge::resync_order ()
+Meterbridge::resync_order()
 {
        sync_order_keys(MixerSort);
 }
@@ -644,14 +782,38 @@ Meterbridge::parameter_changed (std::string const & p)
 {
        if (p == "show-busses-on-meterbridge") {
                _show_busses = _session->config.get_show_busses_on_meterbridge();
-               sync_order_keys(MixerSort);
+               resync_order();
        }
        else if (p == "show-master-on-meterbridge") {
                _show_master = _session->config.get_show_master_on_meterbridge();
-               sync_order_keys(MixerSort);
+               resync_order();
        }
        else if (p == "show-midi-on-meterbridge") {
                _show_midi = _session->config.get_show_midi_on_meterbridge();
-               sync_order_keys(MixerSort);
+               resync_order();
+       }
+       else if (p == "meter-line-up-level") {
+               meter_clear_pattern_cache();
+       }
+       else if (p == "show-rec-on-meterbridge") {
+               scroller.queue_resize();
+       }
+       else if (p == "show-mute-on-meterbridge") {
+               scroller.queue_resize();
+       }
+       else if (p == "show-solo-on-meterbridge") {
+               scroller.queue_resize();
+       }
+       else if (p == "show-name-on-meterbridge") {
+               scroller.queue_resize();
        }
+       else if (p == "meterbridge-label-height") {
+               scroller.queue_resize();
+       }
+}
+
+void
+Meterbridge::on_theme_changed ()
+{
+       meter_clear_pattern_cache();
 }