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/pixfader.h"
30 #include "gtkmm2ext/utils.h"
32 using namespace Gtkmm2ext;
36 #define CORNER_RADIUS 2.5
38 #define CORNER_OFFSET 1
39 #define FADER_RESERVE 6
41 std::list<PixFader::FaderImage*> PixFader::_patterns;
43 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
49 , _span (fader_length)
50 , _girth (fader_girth)
51 , _min_span (fader_length)
52 , _min_girth (fader_girth)
53 , _orien (orientation)
57 , _centered_text (true)
60 _default_value = _adjustment.get_value();
61 update_unity_position ();
64 Gdk::BUTTON_PRESS_MASK
65 | Gdk::BUTTON_RELEASE_MASK
66 | Gdk::POINTER_MOTION_MASK
68 | Gdk::ENTER_NOTIFY_MASK
69 | Gdk::LEAVE_NOTIFY_MASK
72 _adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
73 _adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
74 signal_grab_broken_event ().connect (mem_fun (*this, &PixFader::on_grab_broken_event));
76 CairoWidget::set_size_request(_girth, _span);
78 CairoWidget::set_size_request(_span, _girth);
82 PixFader::~PixFader ()
84 if (_parent_style_change) _parent_style_change.disconnect();
85 if (_layout) _layout.clear (); // drop reference to existing layout
89 PixFader::flush_pattern_cache () {
90 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
91 cairo_pattern_destroy ((*f)->pattern);
98 PixFader::find_pattern (double afr, double afg, double afb,
99 double abr, double abg, double abb,
102 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
103 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
104 return (*f)->pattern;
111 PixFader::create_patterns ()
113 Gdk::Color c = get_style()->get_fg (get_state());
118 fg = c.get_green_p ();
119 fb = c.get_blue_p ();
121 c = get_style()->get_bg (get_state());
124 bg = c.get_green_p ();
125 bb = c.get_blue_p ();
127 cairo_surface_t* surface;
130 if (get_width() <= 1 || get_height() <= 1) {
134 if ((_pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
135 /* found it - use it */
139 if (_orien == VERT) {
141 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
142 tc = cairo_create (surface);
144 /* paint background + border */
146 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
147 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
148 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
149 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
150 cairo_set_source (tc, shade_pattern);
151 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
154 cairo_pattern_destroy (shade_pattern);
156 /* paint lower shade */
158 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width() - 2 - CORNER_OFFSET , 0);
159 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
160 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
161 cairo_set_source (tc, shade_pattern);
162 Gtkmm2ext::rounded_top_half_rectangle (tc, CORNER_OFFSET, get_height() + CORNER_OFFSET,
163 get_width() - CORNER_SIZE, get_height(), CORNER_RADIUS);
166 cairo_pattern_destroy (shade_pattern);
168 _pattern = cairo_pattern_create_for_surface (surface);
172 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
173 tc = cairo_create (surface);
175 /* paint right shade (background section)*/
177 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
178 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
179 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
180 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
181 cairo_set_source (tc, shade_pattern);
182 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
185 /* paint left shade (active section/foreground) */
187 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
188 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
189 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
190 cairo_set_source (tc, shade_pattern);
191 Gtkmm2ext::rounded_right_half_rectangle (tc, CORNER_OFFSET, CORNER_OFFSET,
192 get_width() - CORNER_OFFSET, get_height() - CORNER_SIZE, CORNER_RADIUS);
194 cairo_pattern_destroy (shade_pattern);
196 _pattern = cairo_pattern_create_for_surface (surface);
199 /* cache it for others to use */
201 _patterns.push_back (new FaderImage (_pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
204 cairo_surface_destroy (surface);
208 PixFader::render (cairo_t *cr, cairo_rectangle_t* area)
215 /* this isn't supposed to be happen, but some wackiness whereby
216 * the pixfader ends up with a 1xN or Nx1 size allocation
217 * leads to it. the basic wackiness needs fixing but we
218 * shouldn't crash. just fill in the expose area with
222 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
223 cairo_rectangle (cr, area->x, area->y, area->width, area->height);
229 int ds = display_span ();
230 const float w = get_width();
231 const float h = get_height();
233 CairoWidget::set_source_rgb_a (cr, get_parent_bg(), 1);
234 cairo_rectangle (cr, 0, 0, w, h);
237 cairo_set_line_width (cr, 2);
238 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
240 cairo_matrix_t matrix;
241 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
242 // we use a 'trick' here: The stoke is off by .5px but filling the interior area
243 // after a stroke of 2px width results in an outline of 1px
244 cairo_stroke_preserve(cr);
246 if (_orien == VERT) {
248 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
249 ds = h - FADER_RESERVE - CORNER_OFFSET;
252 if (!CairoWidget::flat_buttons() ) {
253 cairo_set_source (cr, _pattern);
254 cairo_matrix_init_translate (&matrix, 0, (h - ds));
255 cairo_pattern_set_matrix (_pattern, &matrix);
257 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
259 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
260 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, ds + CORNER_OFFSET,
261 w - CORNER_SIZE, h - ds - CORNER_SIZE, CORNER_RADIUS);
267 if (ds < FADER_RESERVE) {
273 * if ds == w, the pattern does not need to be translated
274 * if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
275 * w to the left, which is -w in pattern space, and w in user space
276 * if ds == 10, then the pattern needs to be moved w - 10
277 * to the left, which is -(w-10) in pattern space, which
278 * is (w - 10) in user space
279 * thus: translation = (w - ds)
282 if (!CairoWidget::flat_buttons() ) {
283 cairo_set_source (cr, _pattern);
284 cairo_matrix_init_translate (&matrix, w - ds, 0);
285 cairo_pattern_set_matrix (_pattern, &matrix);
287 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
289 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
290 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET,
291 ds - CORNER_SIZE, h - CORNER_SIZE, CORNER_RADIUS);
296 /* draw the unity-position line if it's not at either end*/
297 if (!(_tweaks & NoShowUnityLine) && _unity_loc > CORNER_RADIUS) {
298 cairo_set_line_width(cr, 1);
299 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
300 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
301 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);
302 if (_orien == VERT) {
303 if (_unity_loc < h - CORNER_RADIUS) {
304 cairo_move_to (cr, 1.5, _unity_loc + CORNER_OFFSET + .5);
305 cairo_line_to (cr, _girth - 1.5, _unity_loc + CORNER_OFFSET + .5);
309 if (_unity_loc < w - CORNER_RADIUS) {
310 cairo_move_to (cr, _unity_loc - CORNER_OFFSET + .5, 1.5);
311 cairo_line_to (cr, _unity_loc - CORNER_OFFSET + .5, _girth - 1.5);
317 if (_layout && !_text.empty() && _orien == HORIZ) {
319 if (_centered_text) {
321 cairo_move_to (cr, (w - _text_width)/2.0, h/2.0 - _text_height/2.0);
322 } else if (ds > .5 * w) {
323 cairo_move_to (cr, CORNER_OFFSET + 3, h/2.0 - _text_height/2.0);
324 cairo_set_operator(cr, CAIRO_OPERATOR_XOR);
326 cairo_move_to (cr, w - _text_width - CORNER_OFFSET - 3, h/2.0 - _text_height/2.0);
328 CairoWidget::set_source_rgb_a (cr, get_style()->get_text (get_state()), 1);
329 pango_cairo_show_layout (cr, _layout->gobj());
333 if (!get_sensitive()) {
334 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
335 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
337 } else if (_hovering && CairoWidget::widget_prelight()) {
338 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
339 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
345 PixFader::on_size_request (GtkRequisition* req)
347 if (_orien == VERT) {
348 req->width = (_min_girth ? _min_girth : -1);
349 req->height = (_min_span ? _min_span : -1);
351 req->height = (_min_girth ? _min_girth : -1);
352 req->width = (_min_span ? _min_span : -1);
357 PixFader::on_size_allocate (Gtk::Allocation& alloc)
359 int old_girth = _girth;
360 int old_span = _span;
362 CairoWidget::on_size_allocate(alloc);
364 if (_orien == VERT) {
365 _girth = alloc.get_width ();
366 _span = alloc.get_height ();
368 _girth = alloc.get_height ();
369 _span = alloc.get_width ();
372 if (is_realized() && ((old_girth != _girth) || (old_span != _span))) {
373 /* recreate patterns in case we've changed size */
377 update_unity_position ();
381 PixFader::on_grab_broken_event (GdkEventGrabBroken* ev)
386 gdk_pointer_ungrab (GDK_CURRENT_TIME);
389 return (_tweaks & NoButtonForward) ? true : false;
393 PixFader::on_button_press_event (GdkEventButton* ev)
395 if (ev->type != GDK_BUTTON_PRESS) {
399 gdk_pointer_ungrab (GDK_CURRENT_TIME);
402 return (_tweaks & NoButtonForward) ? true : false;
405 if (ev->button != 1 && ev->button != 2) {
411 _grab_loc = (_orien == VERT) ? ev->y : ev->x;
412 _grab_start = (_orien == VERT) ? ev->y : ev->x;
413 _grab_window = ev->window;
415 gdk_pointer_grab(ev->window,false,
416 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
419 if (ev->button == 2) {
420 set_adjustment_from_event (ev);
423 return (_tweaks & NoButtonForward) ? true : false;
427 PixFader::on_button_release_event (GdkEventButton* ev)
429 double ev_pos = (_orien == VERT) ? ev->y : ev->x;
431 switch (ev->button) {
436 gdk_pointer_ungrab (GDK_CURRENT_TIME);
440 if (!(_tweaks & NoVerticalScroll)) {
441 Keyboard::magic_widget_drop_focus();
446 if (ev_pos == _grab_start) {
447 /* no motion - just a click */
448 ev_pos = rint(ev_pos);
450 if (ev->state & Keyboard::TertiaryModifier) {
451 _adjustment.set_value (_default_value);
452 } else if (ev->state & Keyboard::GainFineScaleModifier) {
453 _adjustment.set_value (_adjustment.get_lower());
454 #if 0 // ignore clicks
455 } else if (ev_pos == slider_pos) {
456 ; // click on current position, no move.
457 } else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
458 /* above the current display height, remember X Window coords */
459 _adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
461 _adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
474 set_adjustment_from_event (ev);
475 gdk_pointer_ungrab (GDK_CURRENT_TIME);
487 PixFader::on_scroll_event (GdkEventScroll* ev)
492 if (ev->state & Keyboard::GainFineScaleModifier) {
493 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
502 if (_orien == VERT) {
503 switch (ev->direction) {
505 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
508 case GDK_SCROLL_DOWN:
509 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
516 int dir = ev->direction;
518 if (ev->state & Keyboard::ScrollHorizontalModifier || !(_tweaks & NoVerticalScroll)) {
519 if (ev->direction == GDK_SCROLL_UP) dir = GDK_SCROLL_RIGHT;
520 if (ev->direction == GDK_SCROLL_DOWN) dir = GDK_SCROLL_LEFT;
524 case GDK_SCROLL_RIGHT:
525 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
528 case GDK_SCROLL_LEFT:
529 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
540 PixFader::on_motion_notify_event (GdkEventMotion* ev)
544 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
546 if (ev->window != _grab_window) {
548 _grab_window = ev->window;
552 if (ev->state & Keyboard::GainFineScaleModifier) {
553 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
560 double const delta = ev_pos - _grab_loc;
563 const double off = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
564 const double span = _span - off;
565 double fract = (delta / span);
567 fract = min (1.0, fract);
568 fract = max (-1.0, fract);
570 // X Window is top->bottom for 0..Y
572 if (_orien == VERT) {
576 _adjustment.set_value (_adjustment.get_value() + scale * fract * (_adjustment.get_upper() - _adjustment.get_lower()));
583 PixFader::adjustment_changed ()
588 /** @return pixel offset of the current value from the right or bottom of the fader */
590 PixFader::display_span ()
592 float fract = (_adjustment.get_value () - _adjustment.get_lower()) / ((_adjustment.get_upper() - _adjustment.get_lower()));
594 if (_orien == VERT) {
595 const double off = FADER_RESERVE + CORNER_OFFSET;
596 const double span = _span - off;
597 ds = (int)rint (span * (1.0 - fract));
599 const double off = FADER_RESERVE;
600 const double span = _span - off;
601 ds = (int)rint (span * fract + off);
608 PixFader::update_unity_position ()
610 if (_orien == VERT) {
611 const double span = _span - FADER_RESERVE - CORNER_OFFSET;
612 _unity_loc = (int) rint (span * (1 - ((_default_value - _adjustment.get_lower()) / (_adjustment.get_upper() - _adjustment.get_lower())))) - 1;
614 const double span = _span - FADER_RESERVE;
615 _unity_loc = (int) rint (FADER_RESERVE + (_default_value - _adjustment.get_lower()) * span / (_adjustment.get_upper() - _adjustment.get_lower()));
622 PixFader::on_enter_notify_event (GdkEventCrossing*)
625 if (!(_tweaks & NoVerticalScroll)) {
626 Keyboard::magic_widget_grab_focus ();
633 PixFader::on_leave_notify_event (GdkEventCrossing*)
637 if (!(_tweaks & NoVerticalScroll)) {
638 Keyboard::magic_widget_drop_focus();
646 PixFader::set_adjustment_from_event (GdkEventButton* ev)
648 const double off = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
649 const double span = _span - off;
650 double fract = (_orien == VERT) ? (1.0 - ((ev->y - off) / span)) : ((ev->x - off) / span);
652 fract = min (1.0, fract);
653 fract = max (0.0, fract);
655 _adjustment.set_value (fract * (_adjustment.get_upper () - _adjustment.get_lower ()));
659 PixFader::set_default_value (float d)
662 update_unity_position ();
666 PixFader::set_tweaks (Tweaks t)
668 bool need_redraw = false;
669 if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
679 PixFader::set_text (const std::string& str, bool centered, bool expose)
681 if (_layout && _text == str) {
684 if (!_layout && !str.empty()) {
685 _layout = Pango::Layout::create (get_pango_context());
689 _centered_text = centered;
691 _layout->set_text (str);
692 _layout->get_pixel_size (_text_width, _text_height);
694 if (expose) queue_draw ();
699 PixFader::on_state_changed (Gtk::StateType old_state)
701 Widget::on_state_changed (old_state);
707 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
710 std::string txt = _layout->get_text();
711 _layout.clear (); // drop reference to existing layout
713 set_text (txt, _centered_text, false);
715 /* patterns are cached and re-created as needed
716 * during 'expose' in the GUI thread */
722 PixFader::get_parent_bg ()
724 Widget* parent = get_parent ();
727 if (parent->get_has_window()) {
730 parent = parent->get_parent();
733 if (parent && parent->get_has_window()) {
734 if (_current_parent != parent) {
735 if (_parent_style_change) _parent_style_change.disconnect();
736 _current_parent = parent;
737 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &PixFader::on_style_changed));
739 return parent->get_style ()->get_bg (parent->get_state());
742 return get_style ()->get_bg (get_state());