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 $
24 #include "pbd/stacktrace.h"
26 #include "gtkmm2ext/pixfader.h"
27 #include "gtkmm2ext/keyboard.h"
28 #include "gtkmm2ext/rgb_macros.h"
29 #include "gtkmm2ext/utils.h"
31 using namespace Gtkmm2ext;
35 #define CORNER_RADIUS 4
36 #define FADER_RESERVE (2*CORNER_RADIUS)
38 std::list<PixFader::FaderImage*> PixFader::_patterns;
40 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
44 , _orien (orientation)
50 default_value = adjustment.get_value();
51 update_unity_position ();
53 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);
55 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
56 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
59 DrawingArea::set_size_request(girth, span);
61 DrawingArea::set_size_request(span, girth);
65 PixFader::~PixFader ()
70 PixFader::find_pattern (double afr, double afg, double afb,
71 double abr, double abg, double abb,
74 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
75 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
83 PixFader::create_patterns ()
85 Gdk::Color c = get_style()->get_fg (get_state());
90 fg = c.get_green_p ();
93 c = get_style()->get_bg (get_state());
96 bg = c.get_green_p ();
99 if ( !_text.empty()) {
100 _layout->get_pixel_size (_text_width, _text_height);
106 c = get_style()->get_text (get_state());
108 text_r = c.get_red_p ();
109 text_g = c.get_green_p ();
110 text_b = c.get_blue_p ();
112 cairo_surface_t* surface;
114 float radius = CORNER_RADIUS;
116 double w = get_width();
118 if (w <= 1 || get_height() <= 1) {
122 if ((pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
123 /* found it - use it */
127 if (_orien == VERT) {
129 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
130 tc = cairo_create (surface);
132 /* paint background + border */
134 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
135 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
136 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
137 cairo_set_source (tc, shade_pattern);
138 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
141 cairo_pattern_destroy (shade_pattern);
143 /* paint lower shade */
147 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, w, 0);
148 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
149 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
150 cairo_set_source (tc, shade_pattern);
151 Gtkmm2ext::rounded_top_half_rectangle (tc, 1.0, get_height(), w, get_height(), radius-1.5);
154 cairo_pattern_destroy (shade_pattern);
156 pattern = cairo_pattern_create_for_surface (surface);
160 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
161 tc = cairo_create (surface);
163 /* paint right shade (background section)*/
165 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
166 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
167 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
168 cairo_set_source (tc, shade_pattern);
169 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
172 /* paint left shade (active section/foreground) */
174 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
175 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
176 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
177 cairo_set_source (tc, shade_pattern);
178 Gtkmm2ext::rounded_right_half_rectangle (tc, 0, 1, get_width(), get_height() - 2.0, radius-1.5);
180 cairo_pattern_destroy (shade_pattern);
182 pattern = cairo_pattern_create_for_surface (surface);
185 /* cache it for others to use */
187 _patterns.push_back (new FaderImage (pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
190 cairo_surface_destroy (surface);
194 PixFader::on_expose_event (GdkEventExpose* ev)
196 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
197 cairo_t* cr = context->cobj();
205 /* this isn't supposed to be happen, but some wackiness whereby
206 the pixfader ends up with a 1xN or Nx1 size allocation
207 leads to it. the basic wackiness needs fixing but we
208 shouldn't crash. just fill in the expose area with
212 Gdk::Color c = get_style()->get_bg (get_state());
216 bg = c.get_green_p ();
217 bb = c.get_blue_p ();
218 cairo_set_source_rgb (cr, br, bg, bb);
219 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
225 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
228 int ds = display_span ();
229 float w = get_width();
230 float h = get_height();
232 cairo_matrix_t matrix;
234 if (_orien == VERT) {
236 if (ds > h - FADER_RESERVE) {
237 ds = h - FADER_RESERVE;
240 cairo_set_source (cr, pattern);
241 cairo_matrix_init_translate (&matrix, 0, (h - ds));
242 cairo_pattern_set_matrix (pattern, &matrix);
243 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
248 if (ds < FADER_RESERVE) {
253 if ds == w, the pattern does not need to be translated
254 if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
255 w to the left, which is -w in pattern space, and w in
257 if ds == 10, then the pattern needs to be moved w - 10
258 to the left, which is -(w-10) in pattern space, which
259 is (w - 10) in user space
261 thus: translation = (w - ds)
264 cairo_set_source (cr, pattern);
265 cairo_matrix_init_translate (&matrix, w - ds, 0);
266 cairo_pattern_set_matrix (pattern, &matrix);
267 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
271 /* draw the unity-position line if it's not at either end*/
273 context->set_line_width (1);
274 context->set_line_cap (Cairo::LINE_CAP_ROUND);
275 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
276 context->set_source_rgba (c.get_red_p()*1.5, c.get_green_p()*1.5, c.get_blue_p()*1.5, 0.85);
277 if ( _orien == VERT) {
278 if (unity_loc < h ) {
279 context->move_to (1.5, unity_loc + .5);
280 context->line_to (girth - 1.5, unity_loc + .5);
284 if ( unity_loc < w ){
285 context->move_to (unity_loc + .5, 1.5);
286 context->line_to (unity_loc + .5, girth - 1.5);
292 if ( !_text.empty() ) {
296 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
297 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
298 pango_cairo_show_layout (cr, _layout->gobj());
301 if (!get_sensitive()) {
302 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
303 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
305 } else if (_hovering) {
306 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
307 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
317 PixFader::on_size_request (GtkRequisition* req)
319 if (_orien == VERT) {
320 req->width = (girth ? girth : -1);
321 req->height = (span ? span : -1);
323 req->height = (girth ? girth : -1);
324 req->width = (span ? span : -1);
329 PixFader::on_size_allocate (Gtk::Allocation& alloc)
331 DrawingArea::on_size_allocate(alloc);
333 if (_orien == VERT) {
334 girth = alloc.get_width ();
335 span = alloc.get_height ();
337 girth = alloc.get_height ();
338 span = alloc.get_width ();
342 /* recreate patterns in case we've changed size */
346 update_unity_position ();
350 PixFader::on_button_press_event (GdkEventButton* ev)
352 if (ev->type != GDK_BUTTON_PRESS) {
356 if (ev->button != 1 && ev->button != 2) {
361 grab_loc = (_orien == VERT) ? ev->y : ev->x;
362 grab_start = (_orien == VERT) ? ev->y : ev->x;
363 grab_window = ev->window;
365 gdk_pointer_grab(ev->window,false,
366 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
369 if (ev->button == 2) {
370 set_adjustment_from_event (ev);
377 PixFader::on_button_release_event (GdkEventButton* ev)
379 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
381 switch (ev->button) {
386 gdk_pointer_ungrab (GDK_CURRENT_TIME);
389 Keyboard::magic_widget_drop_focus();
393 if (ev_pos == grab_start) {
395 /* no motion - just a click */
397 if (ev->state & Keyboard::TertiaryModifier) {
398 adjustment.set_value (default_value);
399 } else if (ev->state & Keyboard::GainFineScaleModifier) {
400 adjustment.set_value (adjustment.get_lower());
401 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
402 /* above the current display height, remember X Window coords */
403 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
405 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
416 set_adjustment_from_event (ev);
417 gdk_pointer_ungrab (GDK_CURRENT_TIME);
430 PixFader::on_scroll_event (GdkEventScroll* ev)
435 if (ev->state & Keyboard::GainFineScaleModifier) {
436 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
445 if (_orien == VERT) {
447 /* should left/right scroll affect vertical faders ? */
449 switch (ev->direction) {
452 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
455 case GDK_SCROLL_DOWN:
456 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
464 /* up/down scrolls should definitely affect horizontal faders
465 because they are so much easier to use
468 switch (ev->direction) {
470 case GDK_SCROLL_RIGHT:
472 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
475 case GDK_SCROLL_LEFT:
476 case GDK_SCROLL_DOWN:
477 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
488 PixFader::on_motion_notify_event (GdkEventMotion* ev)
492 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
494 if (ev->window != grab_window) {
496 grab_window = ev->window;
500 if (ev->state & Keyboard::GainFineScaleModifier) {
501 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
508 double const delta = ev_pos - grab_loc;
511 double fract = (delta / span);
513 fract = min (1.0, fract);
514 fract = max (-1.0, fract);
516 // X Window is top->bottom for 0..Y
518 if (_orien == VERT) {
522 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
529 PixFader::adjustment_changed ()
531 if (display_span() != last_drawn) {
536 /** @return pixel offset of the current value from the right or bottom of the fader */
538 PixFader::display_span ()
540 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
542 if (_orien == VERT) {
543 ds = (int)floor ( span * (1.0 - fract));
545 ds = (int)floor (span * fract);
552 PixFader::update_unity_position ()
554 if (_orien == VERT) {
555 unity_loc = (int) rint (span * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
557 unity_loc = (int) rint (default_value * span / (adjustment.get_upper() - adjustment.get_lower()));
564 PixFader::on_enter_notify_event (GdkEventCrossing*)
567 Keyboard::magic_widget_grab_focus ();
573 PixFader::on_leave_notify_event (GdkEventCrossing*)
577 Keyboard::magic_widget_drop_focus();
584 PixFader::set_adjustment_from_event (GdkEventButton* ev)
586 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
588 fract = min (1.0, fract);
589 fract = max (0.0, fract);
591 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
595 PixFader::set_default_value (float d)
598 update_unity_position ();
602 PixFader::set_text (const std::string& str)
606 if (!_layout && !_text.empty()) {
607 _layout = Pango::Layout::create (get_pango_context());
611 _layout->set_text (str);
612 _layout->get_pixel_size (_text_width, _text_height);
619 PixFader::on_state_changed (Gtk::StateType old_state)
621 Widget::on_state_changed (old_state);
626 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
629 std::string txt = _layout->get_text();
630 _layout.clear (); // drop reference to existing layout
634 /* remember that all patterns are cached and not owned by an individual
635 pixfader. we will lazily create a new pattern when needed.