2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 $Id: fastmeter.h 570 2006-06-07 21:21:21Z sampo $
25 #include "pbd/stacktrace.h"
27 #include "gtkmm2ext/cairo_widget.h"
28 #include "gtkmm2ext/keyboard.h"
29 #include "gtkmm2ext/utils.h"
31 #include "widgets/ardour_fader.h"
35 using namespace Gtkmm2ext;
36 using namespace ArdourWidgets;
38 #define CORNER_RADIUS 2.5
40 #define CORNER_OFFSET 1
41 #define FADER_RESERVE 6
43 std::list<ArdourFader::FaderImage*> ArdourFader::_patterns;
45 ArdourFader::ArdourFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
51 , _span (fader_length)
52 , _girth (fader_girth)
53 , _min_span (fader_length)
54 , _min_girth (fader_girth)
55 , _orien (orientation)
59 , _centered_text (true)
62 _default_value = _adjustment.get_value();
63 update_unity_position ();
66 Gdk::BUTTON_PRESS_MASK
67 | Gdk::BUTTON_RELEASE_MASK
68 | Gdk::POINTER_MOTION_MASK
70 | Gdk::ENTER_NOTIFY_MASK
71 | Gdk::LEAVE_NOTIFY_MASK
74 _adjustment.signal_value_changed().connect (mem_fun (*this, &ArdourFader::adjustment_changed));
75 _adjustment.signal_changed().connect (mem_fun (*this, &ArdourFader::adjustment_changed));
76 signal_grab_broken_event ().connect (mem_fun (*this, &ArdourFader::on_grab_broken_event));
78 CairoWidget::set_size_request(_girth, _span);
80 CairoWidget::set_size_request(_span, _girth);
84 ArdourFader::~ArdourFader ()
86 if (_parent_style_change) _parent_style_change.disconnect();
87 if (_layout) _layout.clear (); // drop reference to existing layout
91 ArdourFader::flush_pattern_cache () {
92 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
93 cairo_pattern_destroy ((*f)->pattern);
100 ArdourFader::find_pattern (double afr, double afg, double afb,
101 double abr, double abg, double abb,
104 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
105 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
106 return (*f)->pattern;
113 ArdourFader::create_patterns ()
115 Gdk::Color c = get_style()->get_fg (get_state());
120 fg = c.get_green_p ();
121 fb = c.get_blue_p ();
123 c = get_style()->get_bg (get_state());
126 bg = c.get_green_p ();
127 bb = c.get_blue_p ();
129 cairo_surface_t* surface;
132 if (get_width() <= 1 || get_height() <= 1) {
136 if ((_pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
137 /* found it - use it */
141 if (_orien == VERT) {
143 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
144 tc = cairo_create (surface);
146 /* paint background + border */
148 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
149 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
150 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
151 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
152 cairo_set_source (tc, shade_pattern);
153 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
156 cairo_pattern_destroy (shade_pattern);
158 /* paint lower shade */
160 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width() - 2 - CORNER_OFFSET , 0);
161 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
162 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
163 cairo_set_source (tc, shade_pattern);
164 Gtkmm2ext::rounded_top_half_rectangle (tc, CORNER_OFFSET, get_height() + CORNER_OFFSET,
165 get_width() - CORNER_SIZE, get_height(), CORNER_RADIUS);
168 cairo_pattern_destroy (shade_pattern);
170 _pattern = cairo_pattern_create_for_surface (surface);
174 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
175 tc = cairo_create (surface);
177 /* paint right shade (background section)*/
179 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
180 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
181 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
182 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
183 cairo_set_source (tc, shade_pattern);
184 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
187 /* paint left shade (active section/foreground) */
189 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
190 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
191 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
192 cairo_set_source (tc, shade_pattern);
193 Gtkmm2ext::rounded_right_half_rectangle (tc, CORNER_OFFSET, CORNER_OFFSET,
194 get_width() - CORNER_OFFSET, get_height() - CORNER_SIZE, CORNER_RADIUS);
196 cairo_pattern_destroy (shade_pattern);
198 _pattern = cairo_pattern_create_for_surface (surface);
201 /* cache it for others to use */
203 _patterns.push_back (new FaderImage (_pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
206 cairo_surface_destroy (surface);
210 ArdourFader::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t* area)
212 cairo_t* cr = ctx->cobj();
219 /* this isn't supposed to be happen, but some wackiness whereby
220 * the pixfader ends up with a 1xN or Nx1 size allocation
221 * leads to it. the basic wackiness needs fixing but we
222 * shouldn't crash. just fill in the expose area with
226 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
227 cairo_rectangle (cr, area->x, area->y, area->width, area->height);
233 int ds = display_span ();
234 const float w = get_width();
235 const float h = get_height();
237 CairoWidget::set_source_rgb_a (cr, get_parent_bg(), 1);
238 cairo_rectangle (cr, 0, 0, w, h);
241 cairo_set_line_width (cr, 2);
242 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
244 cairo_matrix_t matrix;
245 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
246 // we use a 'trick' here: The stoke is off by .5px but filling the interior area
247 // after a stroke of 2px width results in an outline of 1px
248 cairo_stroke_preserve(cr);
250 if (_orien == VERT) {
252 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
253 ds = h - FADER_RESERVE - CORNER_OFFSET;
256 if (!CairoWidget::flat_buttons() ) {
257 cairo_set_source (cr, _pattern);
258 cairo_matrix_init_translate (&matrix, 0, (h - ds));
259 cairo_pattern_set_matrix (_pattern, &matrix);
261 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
263 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
264 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, ds + CORNER_OFFSET,
265 w - CORNER_SIZE, h - ds - CORNER_SIZE, CORNER_RADIUS);
271 if (ds < FADER_RESERVE) {
277 * if ds == w, the pattern does not need to be translated
278 * if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
279 * w to the left, which is -w in pattern space, and w in user space
280 * if ds == 10, then the pattern needs to be moved w - 10
281 * to the left, which is -(w-10) in pattern space, which
282 * is (w - 10) in user space
283 * thus: translation = (w - ds)
286 if (!CairoWidget::flat_buttons() ) {
287 cairo_set_source (cr, _pattern);
288 cairo_matrix_init_translate (&matrix, w - ds, 0);
289 cairo_pattern_set_matrix (_pattern, &matrix);
291 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
293 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
294 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET,
295 ds - CORNER_SIZE, h - CORNER_SIZE, CORNER_RADIUS);
300 /* draw the unity-position line if it's not at either end*/
301 if (!(_tweaks & NoShowUnityLine) && _unity_loc > CORNER_RADIUS) {
302 cairo_set_line_width(cr, 1);
303 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
304 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
305 cairo_set_source_rgba (cr, c.get_red_p() * 1.5, c.get_green_p() * 1.5, c.get_blue_p() * 1.5, 0.85);
306 if (_orien == VERT) {
307 if (_unity_loc < h - CORNER_RADIUS) {
308 cairo_move_to (cr, 1.5, _unity_loc + CORNER_OFFSET + .5);
309 cairo_line_to (cr, _girth - 1.5, _unity_loc + CORNER_OFFSET + .5);
313 if (_unity_loc < w - CORNER_RADIUS) {
314 cairo_move_to (cr, _unity_loc - CORNER_OFFSET + .5, 1.5);
315 cairo_line_to (cr, _unity_loc - CORNER_OFFSET + .5, _girth - 1.5);
321 if (_layout && !_text.empty() && _orien == HORIZ) {
323 if (_centered_text) {
325 cairo_move_to (cr, (w - _text_width)/2.0, h/2.0 - _text_height/2.0);
326 } else if (ds > .5 * w) {
327 cairo_move_to (cr, CORNER_OFFSET + 3, h/2.0 - _text_height/2.0);
328 cairo_set_operator(cr, CAIRO_OPERATOR_XOR);
330 cairo_move_to (cr, w - _text_width - CORNER_OFFSET - 3, h/2.0 - _text_height/2.0);
332 CairoWidget::set_source_rgb_a (cr, get_style()->get_text (get_state()), 1);
333 pango_cairo_show_layout (cr, _layout->gobj());
337 if (!get_sensitive()) {
338 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
339 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
341 } else if (_hovering && CairoWidget::widget_prelight()) {
342 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
343 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
349 ArdourFader::on_size_request (GtkRequisition* req)
351 if (_orien == VERT) {
352 req->width = (_min_girth ? _min_girth : -1);
353 req->height = (_min_span ? _min_span : -1);
355 req->height = (_min_girth ? _min_girth : -1);
356 req->width = (_min_span ? _min_span : -1);
361 ArdourFader::on_size_allocate (Gtk::Allocation& alloc)
363 int old_girth = _girth;
364 int old_span = _span;
366 CairoWidget::on_size_allocate(alloc);
368 if (_orien == VERT) {
369 _girth = alloc.get_width ();
370 _span = alloc.get_height ();
372 _girth = alloc.get_height ();
373 _span = alloc.get_width ();
376 if (is_realized() && ((old_girth != _girth) || (old_span != _span))) {
377 /* recreate patterns in case we've changed size */
381 update_unity_position ();
385 ArdourFader::on_grab_broken_event (GdkEventGrabBroken* ev)
390 gdk_pointer_ungrab (GDK_CURRENT_TIME);
393 return (_tweaks & NoButtonForward) ? true : false;
397 ArdourFader::on_button_press_event (GdkEventButton* ev)
399 if (ev->type != GDK_BUTTON_PRESS) {
403 gdk_pointer_ungrab (GDK_CURRENT_TIME);
406 return (_tweaks & NoButtonForward) ? true : false;
409 if (ev->button != 1 && ev->button != 2) {
415 _grab_loc = (_orien == VERT) ? ev->y : ev->x;
416 _grab_start = (_orien == VERT) ? ev->y : ev->x;
417 _grab_window = ev->window;
419 gdk_pointer_grab(ev->window,false,
420 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
423 if (ev->button == 2) {
424 set_adjustment_from_event (ev);
427 return (_tweaks & NoButtonForward) ? true : false;
431 ArdourFader::on_button_release_event (GdkEventButton* ev)
433 double ev_pos = (_orien == VERT) ? ev->y : ev->x;
435 switch (ev->button) {
440 gdk_pointer_ungrab (GDK_CURRENT_TIME);
444 if (!(_tweaks & NoVerticalScroll)) {
445 Keyboard::magic_widget_drop_focus();
450 if (ev_pos == _grab_start) {
451 /* no motion - just a click */
452 ev_pos = rint(ev_pos);
454 if (ev->state & Keyboard::TertiaryModifier) {
455 _adjustment.set_value (_default_value);
456 } else if (ev->state & Keyboard::GainFineScaleModifier) {
457 _adjustment.set_value (_adjustment.get_lower());
458 #if 0 // ignore clicks
459 } else if (ev_pos == slider_pos) {
460 ; // click on current position, no move.
461 } else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
462 /* above the current display height, remember X Window coords */
463 _adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
465 _adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
478 set_adjustment_from_event (ev);
479 gdk_pointer_ungrab (GDK_CURRENT_TIME);
491 ArdourFader::on_scroll_event (GdkEventScroll* ev)
496 if (ev->state & Keyboard::GainFineScaleModifier) {
497 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
506 if (_orien == VERT) {
507 switch (ev->direction) {
509 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
512 case GDK_SCROLL_DOWN:
513 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
520 int dir = ev->direction;
522 if (ev->state & Keyboard::ScrollHorizontalModifier || !(_tweaks & NoVerticalScroll)) {
523 if (ev->direction == GDK_SCROLL_UP) dir = GDK_SCROLL_RIGHT;
524 if (ev->direction == GDK_SCROLL_DOWN) dir = GDK_SCROLL_LEFT;
528 case GDK_SCROLL_RIGHT:
529 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
532 case GDK_SCROLL_LEFT:
533 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
544 ArdourFader::on_motion_notify_event (GdkEventMotion* ev)
548 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
550 if (ev->window != _grab_window) {
552 _grab_window = ev->window;
556 if (ev->state & Keyboard::GainFineScaleModifier) {
557 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
564 double const delta = ev_pos - _grab_loc;
567 const double off = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
568 const double span = _span - off;
569 double fract = (delta / span);
571 fract = min (1.0, fract);
572 fract = max (-1.0, fract);
574 // X Window is top->bottom for 0..Y
576 if (_orien == VERT) {
580 _adjustment.set_value (_adjustment.get_value() + scale * fract * (_adjustment.get_upper() - _adjustment.get_lower()));
587 ArdourFader::adjustment_changed ()
592 /** @return pixel offset of the current value from the right or bottom of the fader */
594 ArdourFader::display_span ()
596 float fract = (_adjustment.get_value () - _adjustment.get_lower()) / ((_adjustment.get_upper() - _adjustment.get_lower()));
598 if (_orien == VERT) {
599 const double off = FADER_RESERVE + CORNER_OFFSET;
600 const double span = _span - off;
601 ds = (int)rint (span * (1.0 - fract));
603 const double off = FADER_RESERVE;
604 const double span = _span - off;
605 ds = (int)rint (span * fract + off);
612 ArdourFader::update_unity_position ()
614 if (_orien == VERT) {
615 const double span = _span - FADER_RESERVE - CORNER_OFFSET;
616 _unity_loc = (int) rint (span * (1 - ((_default_value - _adjustment.get_lower()) / (_adjustment.get_upper() - _adjustment.get_lower())))) - 1;
618 const double span = _span - FADER_RESERVE;
619 _unity_loc = (int) rint (FADER_RESERVE + (_default_value - _adjustment.get_lower()) * span / (_adjustment.get_upper() - _adjustment.get_lower()));
626 ArdourFader::on_enter_notify_event (GdkEventCrossing*)
629 if (!(_tweaks & NoVerticalScroll)) {
630 Keyboard::magic_widget_grab_focus ();
637 ArdourFader::on_leave_notify_event (GdkEventCrossing*)
641 if (!(_tweaks & NoVerticalScroll)) {
642 Keyboard::magic_widget_drop_focus();
650 ArdourFader::set_adjustment_from_event (GdkEventButton* ev)
652 const double off = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
653 const double span = _span - off;
654 double fract = (_orien == VERT) ? (1.0 - ((ev->y - off) / span)) : ((ev->x - off) / span);
656 fract = min (1.0, fract);
657 fract = max (0.0, fract);
659 _adjustment.set_value (fract * (_adjustment.get_upper () - _adjustment.get_lower ()));
663 ArdourFader::set_default_value (float d)
666 update_unity_position ();
670 ArdourFader::set_tweaks (Tweaks t)
672 bool need_redraw = false;
673 if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
683 ArdourFader::set_text (const std::string& str, bool centered, bool expose)
685 if (_layout && _text == str) {
688 if (!_layout && !str.empty()) {
689 _layout = Pango::Layout::create (get_pango_context());
693 _centered_text = centered;
695 _layout->set_text (str);
696 _layout->get_pixel_size (_text_width, _text_height);
698 if (expose) queue_draw ();
703 ArdourFader::on_state_changed (Gtk::StateType old_state)
705 Widget::on_state_changed (old_state);
711 ArdourFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
714 std::string txt = _layout->get_text();
715 _layout.clear (); // drop reference to existing layout
717 set_text (txt, _centered_text, false);
719 /* patterns are cached and re-created as needed
720 * during 'expose' in the GUI thread */
726 ArdourFader::get_parent_bg ()
728 Widget* parent = get_parent ();
731 if (parent->get_has_window()) {
734 parent = parent->get_parent();
737 if (parent && parent->get_has_window()) {
738 if (_current_parent != parent) {
739 if (_parent_style_change) _parent_style_change.disconnect();
740 _current_parent = parent;
741 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &ArdourFader::on_style_changed));
743 return parent->get_style ()->get_bg (parent->get_state());
746 return get_style ()->get_bg (get_state());