Episode VI: Return of the ZeroDBline
[ardour.git] / libs / gtkmm2ext / pixfader.cc
index 6099671fd297ec327a0f7800a0db616d3c9ff05b..6e0c6429173c1f523849e88cbe27db053bc14e45 100644 (file)
@@ -20,6 +20,9 @@
 
 
 #include <iostream>
+
+#include "pbd/stacktrace.h"
+
 #include "gtkmm2ext/pixfader.h"
 #include "gtkmm2ext/keyboard.h"
 #include "gtkmm2ext/rgb_macros.h"
@@ -32,121 +35,60 @@ using namespace std;
 #define CORNER_RADIUS 4
 #define FADER_RESERVE (2*CORNER_RADIUS)
 
-PixFader::PixFader (
-       Glib::RefPtr<Gdk::Pixbuf> belt,
-       Glib::RefPtr<Gdk::Pixbuf> belt_desensitised,
-       Gtk::Adjustment& adj,
-       int orientation,
-       int fader_length
-       )
-       : adjustment (adj),
-         _orien(orientation)
+std::list<PixFader::FaderImage*> PixFader::_patterns;
+
+PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
+       : adjustment (adj)
+       , span (fader_length)
+       , girth (fader_girth)
+       , _orien (orientation)
+       , pattern (0)
+       , _hovering (false)
+       , last_drawn (-1)
+       , dragging (false)
 {
-#if 0
-       pixbuf[NORMAL] = belt;
-       pixbuf[DESENSITISED] = belt_desensitised;
-#endif
-       
-       pattern = 0;
-       shine_pattern = 0;
-       
-       _hovering = false;
-       dragging = false;
        default_value = adjustment.get_value();
-       last_drawn = -1;
-
-       view.x = 0;
-       view.y = 0;
-
-       if (orientation == VERT) {
-               view.width = girth = 24;
-       } else {
-               view.height = girth = 24;
-       }
-
-       set_fader_length (fader_length);
+       update_unity_position ();
 
        add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK|Gdk::SCROLL_MASK|Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
 
        adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
        adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
-
-#if 0
-       for (int i = 0; i < STATES; ++i) {
-               Cairo::Format format;
-                       
-               if (pixbuf[i]->get_has_alpha()) {
-                       format = Cairo::FORMAT_ARGB32;
-               } else {
-                       format = Cairo::FORMAT_RGB24;
-               }
-
-               belt_surface[i] = Cairo::ImageSurface::create (format, pixbuf[i]->get_width(), pixbuf[i]->get_height());
-               belt_context[i] = Cairo::Context::create (belt_surface[i]);
-               Gdk::Cairo::set_source_pixbuf (belt_context[i], pixbuf[i], 0.0, 0.0);
-               belt_context[i]->paint();
-       }
-#endif
-       
-        left_r = 0;
-        left_g = 0;
-        left_b = 0;
-
-        right_r = 0;
-        right_g = 0;
-        right_b = 0;
 }
 
 PixFader::~PixFader ()
 {
 }
 
-void
-PixFader::set_border_colors (uint32_t left, uint32_t right)
+cairo_pattern_t*
+PixFader::find_pattern (double afr, double afg, double afb, 
+                       double abr, double abg, double abb, 
+                       int w, int h)
 {
-        int r, g, b;
-        UINT_TO_RGB(left, &r, &g, &b);
-        left_r = r/255.0;
-        left_g = g/255.0;
-        left_b = b/255.0;
-        UINT_TO_RGB(right, &r, &g, &b);
-        right_r = r/255.0;
-        right_g = g/255.0;
-        right_b = b/255.0;
+       for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
+               if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
+                       return (*f)->pattern;
+               }
+       }
+       return 0;
 }
 
 void
 PixFader::create_patterns ()
 {
        Gdk::Color c = get_style()->get_fg (get_state());
-    float r, g, b;
-       r = c.get_red_p ();
-       g = c.get_green_p ();
-       b = c.get_blue_p ();
+       float fr, fg, fb;
+       float br, bg, bb;
 
-       if (_orien == VERT) {
-               pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
-               cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
-               cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
-
-               shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 15, 0);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
-       } else {
-               float rheight = get_height();
-       
-               pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, rheight);
-               cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
-               cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
-
-               shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, rheight);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
-               cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
-       }
+       fr = c.get_red_p ();
+       fg = c.get_green_p ();
+       fb = c.get_blue_p ();
+
+       c = get_style()->get_bg (get_state());
+
+       br = c.get_red_p ();
+       bg = c.get_green_p ();
+       bb = c.get_blue_p ();
 
        if ( !_text.empty()) {
                _layout->get_pixel_size (_text_width, _text_height);
@@ -154,10 +96,92 @@ PixFader::create_patterns ()
                _text_width = 0;
                _text_height = 0;
        }
+
        c = get_style()->get_text (get_state());
+
        text_r = c.get_red_p ();
        text_g = c.get_green_p ();
        text_b = c.get_blue_p ();
+
+       cairo_surface_t* surface;
+       cairo_t* tc = 0;
+       float radius = CORNER_RADIUS;
+
+       double w = get_width();
+       
+       if (w <= 1 || get_height() <= 1) {
+               return;
+       }
+
+       if ((pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
+               /* found it - use it */
+               return;
+       }
+
+       if (_orien == VERT) {
+               
+               surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
+               tc = cairo_create (surface);
+
+               /* paint background + border */
+
+               cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
+               cairo_set_source (tc, shade_pattern);
+               cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
+               cairo_fill (tc);
+
+               cairo_pattern_destroy (shade_pattern);
+               
+               /* paint lower shade */
+               
+               w -= 2.0;
+
+               shade_pattern = cairo_pattern_create_linear (0.0, 0.0, w, 0);
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
+               cairo_set_source (tc, shade_pattern);
+               Gtkmm2ext::rounded_top_half_rectangle (tc, 1.0, get_height(), w, get_height(), radius-1.5);
+               cairo_fill (tc);
+
+               cairo_pattern_destroy (shade_pattern);
+
+               pattern = cairo_pattern_create_for_surface (surface);
+
+       } else {
+
+               surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
+               tc = cairo_create (surface);
+
+               /* paint right shade (background section)*/
+
+               cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
+               cairo_set_source (tc, shade_pattern);
+               cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
+               cairo_fill (tc);
+
+               /* paint left shade (active section/foreground) */
+               
+               shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
+               cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
+               cairo_set_source (tc, shade_pattern);
+               Gtkmm2ext::rounded_right_half_rectangle (tc, 0, 1, get_width(), get_height() - 2.0, radius-1.5);
+               cairo_fill (tc);
+               cairo_pattern_destroy (shade_pattern);
+               
+               pattern = cairo_pattern_create_for_surface (surface);
+       }
+
+       /* cache it for others to use */
+
+       _patterns.push_back (new FaderImage (pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
+
+       cairo_destroy (tc);
+       cairo_surface_destroy (surface);
 }
 
 bool
@@ -169,71 +193,101 @@ PixFader::on_expose_event (GdkEventExpose* ev)
        if (!pattern) {
                create_patterns();
        }
-       
-//     int const pi = get_sensitive() ? NORMAL : DESENSITISED;
-       
-       int ds = display_span ();
 
-       float w = get_width();
-       float h = get_height();
-       float radius = CORNER_RADIUS;
+       if (!pattern) {
+
+               /* this isn't supposed to be happen, but some wackiness whereby
+                  the pixfader ends up with a 1xN or Nx1 size allocation
+                  leads to it. the basic wackiness needs fixing but we
+                  shouldn't crash. just fill in the expose area with 
+                  our bg color.
+               */
 
-       /* black border */
+               Gdk::Color c = get_style()->get_bg (get_state());
+               float br, bg, bb;
+
+               br = c.get_red_p ();
+               bg = c.get_green_p ();
+               bb = c.get_blue_p ();
+               cairo_set_source_rgb (cr, br, bg, bb);
+               cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
+               cairo_fill (cr);
+
+               return true;
+       }
+                  
+       cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
+       cairo_clip (cr);
 
-       cairo_set_source_rgb (cr, 0,0,0);
-       cairo_rectangle (cr, 0, 0, w, h);
-       cairo_fill (cr);
+       int ds = display_span ();
+       float w = get_width();
+       float h = get_height();
 
-       /* draw active box */
+       cairo_matrix_t matrix;
 
        if (_orien == VERT) {
-               if (ds > h - FADER_RESERVE)
+
+               if (ds > h - FADER_RESERVE) {
                        ds = h - FADER_RESERVE;
+               }
 
                cairo_set_source (cr, pattern);
-               Gtkmm2ext::rounded_rectangle (cr, 1, 1+ds, w-2, h-(1+ds)-1, radius-1.5);
+               cairo_matrix_init_translate (&matrix, 0, (h - ds));
+               cairo_pattern_set_matrix (pattern, &matrix);
+               Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
                cairo_fill (cr);
 
-//             cairo_set_source (cr, shine_pattern);
-//             Gtkmm2ext::rounded_rectangle (cr, 2, ds, w-4, h-(1+ds)-1, radius-1.5);
-//             cairo_fill (cr);
        } else {
-               if (ds < FADER_RESERVE)
+
+               if (ds < FADER_RESERVE) {
                        ds = FADER_RESERVE;
+               }
+
+               /*
+                 if ds == w, the pattern does not need to be translated
+                 if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
+                     w to the left, which is -w in pattern space, and w in
+                     user space
+                 if ds == 10, then the pattern needs to be moved w - 10
+                     to the left, which is -(w-10) in pattern space, which 
+                     is (w - 10) in user space
+
+                 thus: translation = (w - ds)
+                */
 
                cairo_set_source (cr, pattern);
-               Gtkmm2ext::rounded_rectangle (cr, 1, 1, ds-1, h-2, radius-1.5);
+               cairo_matrix_init_translate (&matrix, w - ds, 0);
+               cairo_pattern_set_matrix (pattern, &matrix);
+               Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
                cairo_fill (cr);
-
-//             cairo_set_source (cr, shine_pattern);
-//             Gtkmm2ext::rounded_rectangle (cr, 2, 3, ds-1, 15, radius-1.5);
-//             cairo_fill (cr);
        }
-       
+               
        /* draw the unity-position line if it's not at either end*/
        if (unity_loc > 0) {
-               if ( _orien == VERT && unity_loc < h ) {
-                                       context->set_line_width (1); 
-                                       context->set_source_rgb (0.0, 1.0, 0.0);
-                                       context->move_to (1, unity_loc);
-                                       context->line_to (girth, unity_loc);
-                                       context->stroke ();
-               } else if ( unity_loc < w ){
-                                       context->set_line_width (1); 
-                                       context->set_source_rgb (0.0, 1.0, 0.0);
-                                       context->move_to (unity_loc, 1);
-                                       context->line_to (unity_loc, girth);
-                                       context->stroke ();
+               context->set_line_width (1);
+               context->set_line_cap (Cairo::LINE_CAP_ROUND);
+               Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
+               context->set_source_rgba (c.get_red_p()*1.5, c.get_green_p()*1.5, c.get_blue_p()*1.5, 0.85);
+               if ( _orien == VERT) {
+                       if (unity_loc < h ) {
+                               context->move_to (1.5, unity_loc + .5);
+                               context->line_to (girth - 1.5, unity_loc + .5);
+                               context->stroke ();
+                       }
+               } else {
+                       if ( unity_loc < w ){
+                               context->move_to (unity_loc + .5, 1.5);
+                               context->line_to (unity_loc + .5, girth - 1.5);
+                               context->stroke ();
+                       }
                }
        }
-       
-       if ( !_text.empty() ) {
 
-               cairo_new_path (cr);    
+       if ( !_text.empty() ) {
 
                /* center text */
+               cairo_new_path (cr);
                cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
-
                cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
                pango_cairo_show_layout (cr, _layout->gobj());
        } 
@@ -241,7 +295,7 @@ PixFader::on_expose_event (GdkEventExpose* ev)
 //     if (Config->get_widget_prelight()) {  //pixfader does not have access to config
                if (_hovering) {
                        Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
-                       cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
+                       cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
                        cairo_fill (cr);
                }
 //     }
@@ -254,24 +308,34 @@ PixFader::on_expose_event (GdkEventExpose* ev)
 void
 PixFader::on_size_request (GtkRequisition* req)
 {
-       req->width = view.width;
-       req->height = view.height;
+       if (_orien == VERT) {
+               req->width = (girth ? girth : -1);
+               req->height = (span ? span : -1);
+       } else {
+               req->height = (girth ? girth : -1);
+               req->width = (span ? span : -1);
+       }
 }
 
 void
 PixFader::on_size_allocate (Gtk::Allocation& alloc)
 {
        DrawingArea::on_size_allocate(alloc);
+
        if (_orien == VERT) {
-               view.height = span = alloc.get_height();
+               girth = alloc.get_width ();
+               span = alloc.get_height ();
        } else {
-               view.width = span = alloc.get_width();
+               girth = alloc.get_height ();
+               span = alloc.get_width ();
+       }
+
+       if (is_realized()) {
+               /* recreate patterns in case we've changed size */
+               create_patterns ();
        }
 
        update_unity_position ();
-       create_patterns();
-       
-       queue_draw ();
 }
 
 bool
@@ -290,6 +354,9 @@ PixFader::on_button_press_event (GdkEventButton* ev)
        grab_start = (_orien == VERT) ? ev->y : ev->x;
        grab_window = ev->window;
        dragging = true;
+       gdk_pointer_grab(ev->window,false,
+                       GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
+                       NULL,NULL,ev->time);
 
        if (ev->button == 2) {
                set_adjustment_from_event (ev);
@@ -308,6 +375,7 @@ PixFader::on_button_release_event (GdkEventButton* ev)
                if (dragging) {
                        remove_modal_grab();
                        dragging = false;
+                       gdk_pointer_ungrab (GDK_CURRENT_TIME);
 
                        if (!_hovering) {
                                Keyboard::magic_widget_drop_focus();
@@ -322,14 +390,14 @@ PixFader::on_button_release_event (GdkEventButton* ev)
                                        adjustment.set_value (default_value);
                                } else if (ev->state & Keyboard::GainFineScaleModifier) {
                                        adjustment.set_value (adjustment.get_lower());
-                               } else if ((_orien == VERT && ev_pos < span - display_span()) || (_orien == HORIZ && ev_pos > span - display_span())) {
+                               } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
                                        /* above the current display height, remember X Window coords */
                                        adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
                                } else {
                                        adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
                                }
                        }
-
+                       return true;
                } 
                break;
                
@@ -338,6 +406,8 @@ PixFader::on_button_release_event (GdkEventButton* ev)
                        remove_modal_grab();
                        dragging = false;
                        set_adjustment_from_event (ev);
+                       gdk_pointer_ungrab (GDK_CURRENT_TIME);
+                       return true;
                }
                break;
 
@@ -470,27 +540,13 @@ PixFader::display_span ()
        return ds;
 }
 
-void
-PixFader::set_fader_length (int l)
-{
-       if (_orien == VERT) {
-               view.height = span = l;
-       } else {
-               view.width = span = l;
-       }
-
-       update_unity_position ();
-
-       queue_draw ();
-}
-
 void
 PixFader::update_unity_position ()
 {
        if (_orien == VERT) {
-               unity_loc = (int) rint (view.height * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
+               unity_loc = (int) rint (span * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
        } else {
-               unity_loc = (int) rint (default_value * view.width);
+               unity_loc = (int) rint (default_value * span / (adjustment.get_upper() - adjustment.get_lower()));
        }
 
        queue_draw ();
@@ -545,8 +601,31 @@ PixFader::set_text (const std::string& str)
 
        if (_layout) {
                _layout->set_text (str);
+               _layout->get_pixel_size (_text_width, _text_height);
        }
 
        queue_resize ();
 }
 
+void
+PixFader::on_state_changed (Gtk::StateType old_state)
+{
+       Widget::on_state_changed (old_state);
+       create_patterns ();
+}
+
+void
+PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
+{
+       if (_layout) {
+               std::string txt = _layout->get_text();
+               _layout.clear (); // drop reference to existing layout
+               set_text (txt);
+       }
+
+       /* remember that all patterns are cached and not owned by an individual
+          pixfader. we will lazily create a new pattern when needed.
+       */
+
+       pattern = 0;
+}