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 4
38 #define CORNER_OFFSET 1
39 #define FADER_RESERVE 5
41 std::list<PixFader::FaderImage*> PixFader::_patterns;
43 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
47 , _orien (orientation)
54 default_value = adjustment.get_value();
55 update_unity_position ();
58 Gdk::BUTTON_PRESS_MASK
59 | Gdk::BUTTON_RELEASE_MASK
60 | Gdk::POINTER_MOTION_MASK
62 | Gdk::ENTER_NOTIFY_MASK
63 | Gdk::LEAVE_NOTIFY_MASK
66 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
67 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
70 DrawingArea::set_size_request(girth, span);
72 DrawingArea::set_size_request(span, girth);
76 PixFader::~PixFader ()
78 if (_parent_style_change) _parent_style_change.disconnect();
82 PixFader::find_pattern (double afr, double afg, double afb,
83 double abr, double abg, double abb,
86 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
87 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
95 PixFader::create_patterns ()
97 Gdk::Color c = get_style()->get_fg (get_state());
102 fg = c.get_green_p ();
103 fb = c.get_blue_p ();
105 c = get_style()->get_bg (get_state());
108 bg = c.get_green_p ();
109 bb = c.get_blue_p ();
111 if ( _layout && !_text.empty()) {
112 _layout->get_pixel_size (_text_width, _text_height);
118 cairo_surface_t* surface;
121 if (get_width() <= 1 || get_height() <= 1) {
125 if ((pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
126 /* found it - use it */
130 if (_orien == VERT) {
132 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
133 tc = cairo_create (surface);
135 /* paint background + border */
137 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
138 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
139 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
140 cairo_set_source (tc, shade_pattern);
141 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
144 cairo_pattern_destroy (shade_pattern);
146 /* paint lower shade */
148 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width() - 2 - CORNER_OFFSET , 0);
149 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
150 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
151 cairo_set_source (tc, shade_pattern);
152 Gtkmm2ext::rounded_top_half_rectangle (tc, CORNER_OFFSET, get_height() + CORNER_OFFSET,
153 get_width() - CORNER_SIZE, get_height(), CORNER_RADIUS);
156 cairo_pattern_destroy (shade_pattern);
158 pattern = cairo_pattern_create_for_surface (surface);
162 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
163 tc = cairo_create (surface);
165 /* paint right shade (background section)*/
167 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
168 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
169 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
170 cairo_set_source (tc, shade_pattern);
171 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
174 /* paint left shade (active section/foreground) */
176 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
177 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
178 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
179 cairo_set_source (tc, shade_pattern);
180 Gtkmm2ext::rounded_right_half_rectangle (tc, CORNER_OFFSET, CORNER_OFFSET,
181 get_width() - CORNER_OFFSET, get_height() - CORNER_SIZE, CORNER_RADIUS);
183 cairo_pattern_destroy (shade_pattern);
185 pattern = cairo_pattern_create_for_surface (surface);
188 /* cache it for others to use */
190 _patterns.push_back (new FaderImage (pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
193 cairo_surface_destroy (surface);
197 PixFader::on_expose_event (GdkEventExpose* ev)
199 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
200 cairo_t* cr = context->cobj();
202 // clip to expose area
203 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
211 /* this isn't supposed to be happen, but some wackiness whereby
212 * the pixfader ends up with a 1xN or Nx1 size allocation
213 * leads to it. the basic wackiness needs fixing but we
214 * shouldn't crash. just fill in the expose area with
218 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
219 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
224 int ds = display_span ();
225 const float w = get_width();
226 const float h = get_height();
228 CairoWidget::set_source_rgb_a (cr, get_parent_bg(), 1);
229 cairo_rectangle (cr, 0, 0, w, h);
232 cairo_set_line_width (cr, 1);
233 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
235 cairo_matrix_t matrix;
236 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
237 // we use a 'trick' here: The stoke is off by .5px but filling the interiour area
238 // results in an outline of 0.5 px the actual outline is exatly 1px at 50% alpha.
239 cairo_stroke_preserve(cr);
241 if (_orien == VERT) {
243 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
244 ds = h - FADER_RESERVE - CORNER_OFFSET;
247 if (!CairoWidget::flat_buttons() ) {
248 cairo_set_source (cr, pattern);
249 cairo_matrix_init_translate (&matrix, 0, (h - ds));
250 cairo_pattern_set_matrix (pattern, &matrix);
252 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
254 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
255 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, ds + CORNER_OFFSET,
256 w - CORNER_SIZE, h - ds - CORNER_SIZE, CORNER_RADIUS);
262 if (ds < FADER_RESERVE) {
268 * if ds == w, the pattern does not need to be translated
269 * if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
270 * w to the left, which is -w in pattern space, and w in user space
271 * if ds == 10, then the pattern needs to be moved w - 10
272 * to the left, which is -(w-10) in pattern space, which
273 * is (w - 10) in user space
274 * thus: translation = (w - ds)
277 if (!CairoWidget::flat_buttons() ) {
278 cairo_set_source (cr, pattern);
279 cairo_matrix_init_translate (&matrix, w - ds, 0);
280 cairo_pattern_set_matrix (pattern, &matrix);
282 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
284 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
285 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET,
286 ds - CORNER_OFFSET, h - CORNER_SIZE, CORNER_RADIUS);
291 /* draw the unity-position line if it's not at either end*/
293 context->set_line_width (1);
294 context->set_line_cap (Cairo::LINE_CAP_ROUND);
295 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
296 context->set_source_rgba (c.get_red_p()*1.5, c.get_green_p()*1.5, c.get_blue_p()*1.5, 0.85);
297 if ( _orien == VERT) {
298 if (unity_loc < h ) {
299 context->move_to (1.5, unity_loc + CORNER_OFFSET + .5);
300 context->line_to (girth - 1.5, unity_loc + CORNER_OFFSET + .5);
304 if ( unity_loc < w ){
305 context->move_to (unity_loc - CORNER_OFFSET + .5, 1.5);
306 context->line_to (unity_loc - CORNER_OFFSET + .5, girth - 1.5);
312 if ( !_text.empty() ) {
315 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
316 CairoWidget::set_source_rgb_a (cr, get_style()->get_text (get_state()), 1);
317 pango_cairo_show_layout (cr, _layout->gobj());
320 if (!get_sensitive()) {
321 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
322 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
324 } else if (_hovering) {
325 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
326 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
336 PixFader::on_size_request (GtkRequisition* req)
338 if (_orien == VERT) {
339 req->width = (girth ? girth : -1);
340 req->height = (span ? span : -1);
342 req->height = (girth ? girth : -1);
343 req->width = (span ? span : -1);
348 PixFader::on_size_allocate (Gtk::Allocation& alloc)
350 DrawingArea::on_size_allocate(alloc);
352 if (_orien == VERT) {
353 girth = alloc.get_width ();
354 span = alloc.get_height ();
356 girth = alloc.get_height ();
357 span = alloc.get_width ();
361 /* recreate patterns in case we've changed size */
365 update_unity_position ();
369 PixFader::on_button_press_event (GdkEventButton* ev)
371 if (ev->type != GDK_BUTTON_PRESS) {
375 if (ev->button != 1 && ev->button != 2) {
380 grab_loc = (_orien == VERT) ? ev->y : ev->x;
381 grab_start = (_orien == VERT) ? ev->y : ev->x;
382 grab_window = ev->window;
384 gdk_pointer_grab(ev->window,false,
385 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
388 if (ev->button == 2) {
389 set_adjustment_from_event (ev);
396 PixFader::on_button_release_event (GdkEventButton* ev)
398 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
400 switch (ev->button) {
405 gdk_pointer_ungrab (GDK_CURRENT_TIME);
408 Keyboard::magic_widget_drop_focus();
412 if (ev_pos == grab_start) {
414 /* no motion - just a click */
416 if (ev->state & Keyboard::TertiaryModifier) {
417 adjustment.set_value (default_value);
418 } else if (ev->state & Keyboard::GainFineScaleModifier) {
419 adjustment.set_value (adjustment.get_lower());
420 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
421 /* above the current display height, remember X Window coords */
422 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
424 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
435 set_adjustment_from_event (ev);
436 gdk_pointer_ungrab (GDK_CURRENT_TIME);
449 PixFader::on_scroll_event (GdkEventScroll* ev)
454 if (ev->state & Keyboard::GainFineScaleModifier) {
455 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
464 if (_orien == VERT) {
466 /* should left/right scroll affect vertical faders ? */
468 switch (ev->direction) {
471 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
474 case GDK_SCROLL_DOWN:
475 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
483 /* up/down scrolls should definitely affect horizontal faders
484 because they are so much easier to use
487 switch (ev->direction) {
489 case GDK_SCROLL_RIGHT:
491 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
494 case GDK_SCROLL_LEFT:
495 case GDK_SCROLL_DOWN:
496 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
507 PixFader::on_motion_notify_event (GdkEventMotion* ev)
511 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
513 if (ev->window != grab_window) {
515 grab_window = ev->window;
519 if (ev->state & Keyboard::GainFineScaleModifier) {
520 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
527 double const delta = ev_pos - grab_loc;
530 double fract = (delta / span);
532 fract = min (1.0, fract);
533 fract = max (-1.0, fract);
535 // X Window is top->bottom for 0..Y
537 if (_orien == VERT) {
541 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
548 PixFader::adjustment_changed ()
550 if (display_span() != last_drawn) {
555 /** @return pixel offset of the current value from the right or bottom of the fader */
557 PixFader::display_span ()
559 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
561 if (_orien == VERT) {
562 ds = (int)floor (span * (1.0 - fract));
564 ds = (int)floor (span * fract);
571 PixFader::update_unity_position ()
573 if (_orien == VERT) {
574 unity_loc = (int) rint (span * (1 - ((default_value - adjustment.get_lower()) / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
576 unity_loc = (int) rint ((default_value - adjustment.get_lower()) * span / (adjustment.get_upper() - adjustment.get_lower()));
583 PixFader::on_enter_notify_event (GdkEventCrossing*)
586 Keyboard::magic_widget_grab_focus ();
592 PixFader::on_leave_notify_event (GdkEventCrossing*)
596 Keyboard::magic_widget_drop_focus();
603 PixFader::set_adjustment_from_event (GdkEventButton* ev)
605 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
607 fract = min (1.0, fract);
608 fract = max (0.0, fract);
610 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
614 PixFader::set_default_value (float d)
617 update_unity_position ();
621 PixFader::set_text (const std::string& str)
625 if (!_layout && !_text.empty()) {
626 _layout = Pango::Layout::create (get_pango_context());
627 // XXX explicityly unref in dtor?
631 _layout->set_text (str);
632 _layout->get_pixel_size (_text_width, _text_height);
639 PixFader::on_state_changed (Gtk::StateType old_state)
641 Widget::on_state_changed (old_state);
647 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
650 std::string txt = _layout->get_text();
651 _layout.clear (); // drop reference to existing layout
654 /* patterns are cached and re-created as needed
655 * during 'expose' in the GUI thread */
661 PixFader::get_parent_bg ()
663 Widget* parent = get_parent ();
666 if (parent->get_has_window()) {
669 parent = parent->get_parent();
672 if (parent && parent->get_has_window()) {
673 if (_current_parent != parent) {
674 if (_parent_style_change) _parent_style_change.disconnect();
675 _current_parent = parent;
676 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &PixFader::on_style_changed));
678 return parent->get_style ()->get_bg (parent->get_state());
681 return get_style ()->get_bg (get_state());