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
37 #define CORNER_OFFSET 1
38 #define FADER_RESERVE 5
40 std::list<PixFader::FaderImage*> PixFader::_patterns;
42 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
46 , _orien (orientation)
52 default_value = adjustment.get_value();
53 update_unity_position ();
55 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);
57 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
58 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
61 DrawingArea::set_size_request(girth, span);
63 DrawingArea::set_size_request(span, girth);
67 PixFader::~PixFader ()
72 PixFader::find_pattern (double afr, double afg, double afb,
73 double abr, double abg, double abb,
76 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
77 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
85 PixFader::create_patterns ()
87 Gdk::Color c = get_style()->get_fg (get_state());
92 fg = c.get_green_p ();
95 c = get_style()->get_bg (get_state());
98 bg = c.get_green_p ();
101 if ( !_text.empty()) {
102 _layout->get_pixel_size (_text_width, _text_height);
108 c = get_style()->get_text (get_state());
110 text_r = c.get_red_p ();
111 text_g = c.get_green_p ();
112 text_b = c.get_blue_p ();
114 cairo_surface_t* surface;
117 if (get_width() <= 1 || get_height() <= 1) {
121 if ((pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
122 /* found it - use it */
126 if (_orien == VERT) {
128 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
129 tc = cairo_create (surface);
131 /* paint background + border */
133 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
134 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
135 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
136 cairo_set_source (tc, shade_pattern);
137 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
140 cairo_pattern_destroy (shade_pattern);
142 /* paint lower shade */
144 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width() - 2 - CORNER_OFFSET , 0);
145 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
146 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
147 cairo_set_source (tc, shade_pattern);
148 Gtkmm2ext::rounded_top_half_rectangle (tc, CORNER_OFFSET, get_height() + CORNER_OFFSET,
149 get_width() - CORNER_SIZE, get_height(), CORNER_RADIUS);
152 cairo_pattern_destroy (shade_pattern);
154 pattern = cairo_pattern_create_for_surface (surface);
158 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
159 tc = cairo_create (surface);
161 /* paint right shade (background section)*/
163 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
164 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
165 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
166 cairo_set_source (tc, shade_pattern);
167 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
170 /* paint left shade (active section/foreground) */
172 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
173 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
174 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
175 cairo_set_source (tc, shade_pattern);
176 Gtkmm2ext::rounded_right_half_rectangle (tc, CORNER_OFFSET, CORNER_OFFSET,
177 get_width() - CORNER_OFFSET, get_height() - CORNER_SIZE, CORNER_RADIUS);
179 cairo_pattern_destroy (shade_pattern);
181 pattern = cairo_pattern_create_for_surface (surface);
184 /* cache it for others to use */
186 _patterns.push_back (new FaderImage (pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
189 cairo_surface_destroy (surface);
193 PixFader::on_expose_event (GdkEventExpose* ev)
195 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
196 cairo_t* cr = context->cobj();
204 /* this isn't supposed to be happen, but some wackiness whereby
205 the pixfader ends up with a 1xN or Nx1 size allocation
206 leads to it. the basic wackiness needs fixing but we
207 shouldn't crash. just fill in the expose area with
211 Gdk::Color c = get_style()->get_bg (get_state());
215 bg = c.get_green_p ();
216 bb = c.get_blue_p ();
217 cairo_set_source_rgb (cr, br, bg, bb);
218 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
223 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
226 int ds = display_span ();
227 float w = get_width();
228 float h = get_height();
230 Gdk::Color c = get_style()->get_bg (Gtk::STATE_PRELIGHT);
231 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
232 cairo_rectangle (cr, 0, 0, w, h);
235 cairo_set_line_width (cr, 1);
236 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
238 cairo_matrix_t matrix;
239 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
240 cairo_stroke_preserve(cr);
242 if (_orien == VERT) {
244 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
245 ds = h - FADER_RESERVE - CORNER_OFFSET;
248 cairo_set_source (cr, pattern);
249 cairo_matrix_init_translate (&matrix, 0, (h - ds));
250 cairo_pattern_set_matrix (pattern, &matrix);
255 if (ds < FADER_RESERVE) {
260 if ds == w, the pattern does not need to be translated
261 if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
262 w to the left, which is -w in pattern space, and w in
264 if ds == 10, then the pattern needs to be moved w - 10
265 to the left, which is -(w-10) in pattern space, which
266 is (w - 10) in user space
268 thus: translation = (w - ds)
271 cairo_set_source (cr, pattern);
272 cairo_matrix_init_translate (&matrix, w - ds, 0);
273 cairo_pattern_set_matrix (pattern, &matrix);
277 /* draw the unity-position line if it's not at either end*/
279 context->set_line_width (1);
280 context->set_line_cap (Cairo::LINE_CAP_ROUND);
281 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
282 context->set_source_rgba (c.get_red_p()*1.5, c.get_green_p()*1.5, c.get_blue_p()*1.5, 0.85);
283 if ( _orien == VERT) {
284 if (unity_loc < h ) {
285 context->move_to (1.5, unity_loc + CORNER_OFFSET + .5);
286 context->line_to (girth - 1.5, unity_loc + CORNER_OFFSET + .5);
290 if ( unity_loc < w ){
291 context->move_to (unity_loc - CORNER_OFFSET + .5, 1.5);
292 context->line_to (unity_loc - CORNER_OFFSET + .5, girth - 1.5);
298 if ( !_text.empty() ) {
302 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
303 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
304 pango_cairo_show_layout (cr, _layout->gobj());
307 if (!get_sensitive()) {
308 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
309 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
311 } else if (_hovering) {
312 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
313 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
323 PixFader::on_size_request (GtkRequisition* req)
325 if (_orien == VERT) {
326 req->width = (girth ? girth : -1);
327 req->height = (span ? span : -1);
329 req->height = (girth ? girth : -1);
330 req->width = (span ? span : -1);
335 PixFader::on_size_allocate (Gtk::Allocation& alloc)
337 DrawingArea::on_size_allocate(alloc);
339 if (_orien == VERT) {
340 girth = alloc.get_width ();
341 span = alloc.get_height ();
343 girth = alloc.get_height ();
344 span = alloc.get_width ();
348 /* recreate patterns in case we've changed size */
352 update_unity_position ();
356 PixFader::on_button_press_event (GdkEventButton* ev)
358 if (ev->type != GDK_BUTTON_PRESS) {
362 if (ev->button != 1 && ev->button != 2) {
367 grab_loc = (_orien == VERT) ? ev->y : ev->x;
368 grab_start = (_orien == VERT) ? ev->y : ev->x;
369 grab_window = ev->window;
371 gdk_pointer_grab(ev->window,false,
372 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
375 if (ev->button == 2) {
376 set_adjustment_from_event (ev);
383 PixFader::on_button_release_event (GdkEventButton* ev)
385 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
387 switch (ev->button) {
392 gdk_pointer_ungrab (GDK_CURRENT_TIME);
395 Keyboard::magic_widget_drop_focus();
399 if (ev_pos == grab_start) {
401 /* no motion - just a click */
403 if (ev->state & Keyboard::TertiaryModifier) {
404 adjustment.set_value (default_value);
405 } else if (ev->state & Keyboard::GainFineScaleModifier) {
406 adjustment.set_value (adjustment.get_lower());
407 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
408 /* above the current display height, remember X Window coords */
409 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
411 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
422 set_adjustment_from_event (ev);
423 gdk_pointer_ungrab (GDK_CURRENT_TIME);
436 PixFader::on_scroll_event (GdkEventScroll* ev)
441 if (ev->state & Keyboard::GainFineScaleModifier) {
442 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
451 if (_orien == VERT) {
453 /* should left/right scroll affect vertical faders ? */
455 switch (ev->direction) {
458 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
461 case GDK_SCROLL_DOWN:
462 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
470 /* up/down scrolls should definitely affect horizontal faders
471 because they are so much easier to use
474 switch (ev->direction) {
476 case GDK_SCROLL_RIGHT:
478 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
481 case GDK_SCROLL_LEFT:
482 case GDK_SCROLL_DOWN:
483 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
494 PixFader::on_motion_notify_event (GdkEventMotion* ev)
498 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
500 if (ev->window != grab_window) {
502 grab_window = ev->window;
506 if (ev->state & Keyboard::GainFineScaleModifier) {
507 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
514 double const delta = ev_pos - grab_loc;
517 double fract = (delta / span);
519 fract = min (1.0, fract);
520 fract = max (-1.0, fract);
522 // X Window is top->bottom for 0..Y
524 if (_orien == VERT) {
528 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
535 PixFader::adjustment_changed ()
537 if (display_span() != last_drawn) {
542 /** @return pixel offset of the current value from the right or bottom of the fader */
544 PixFader::display_span ()
546 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
548 if (_orien == VERT) {
549 ds = (int)floor ( span * (1.0 - fract));
551 ds = (int)floor (span * fract);
558 PixFader::update_unity_position ()
560 if (_orien == VERT) {
561 unity_loc = (int) rint (span * (1 - ((default_value - adjustment.get_lower()) / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
563 unity_loc = (int) rint ((default_value - adjustment.get_lower()) * span / (adjustment.get_upper() - adjustment.get_lower()));
570 PixFader::on_enter_notify_event (GdkEventCrossing*)
573 Keyboard::magic_widget_grab_focus ();
579 PixFader::on_leave_notify_event (GdkEventCrossing*)
583 Keyboard::magic_widget_drop_focus();
590 PixFader::set_adjustment_from_event (GdkEventButton* ev)
592 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
594 fract = min (1.0, fract);
595 fract = max (0.0, fract);
597 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
601 PixFader::set_default_value (float d)
604 update_unity_position ();
608 PixFader::set_text (const std::string& str)
612 if (!_layout && !_text.empty()) {
613 _layout = Pango::Layout::create (get_pango_context());
617 _layout->set_text (str);
618 _layout->get_pixel_size (_text_width, _text_height);
625 PixFader::on_state_changed (Gtk::StateType old_state)
627 Widget::on_state_changed (old_state);
633 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
636 std::string txt = _layout->get_text();
637 _layout.clear (); // drop reference to existing layout
641 /* remember that all patterns are cached and not owned by an individual
642 pixfader. we will lazily create a new pattern when needed.