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 "gtkmm2ext/pixfader.h"
25 #include "gtkmm2ext/keyboard.h"
26 #include "gtkmm2ext/rgb_macros.h"
27 #include "gtkmm2ext/utils.h"
29 using namespace Gtkmm2ext;
33 #define CORNER_RADIUS 4
34 #define FADER_RESERVE (2*CORNER_RADIUS)
36 std::list<PixFader::FaderImage*> PixFader::_patterns;
38 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
42 , _orien (orientation)
48 default_value = adjustment.get_value();
49 update_unity_position ();
51 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);
53 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
54 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
57 PixFader::~PixFader ()
62 PixFader::find_pattern (double afr, double afg, double afb,
63 double abr, double abg, double abb,
66 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
67 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
75 PixFader::create_patterns ()
77 Gdk::Color c = get_style()->get_fg (get_state());
82 fg = c.get_green_p ();
85 c = get_style()->get_bg (get_state());
88 bg = c.get_green_p ();
91 cairo_surface_t* surface;
93 float radius = CORNER_RADIUS;
95 double w = get_width();
97 if ((pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
98 /* found it - use it */
102 if (_orien == VERT) {
104 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
105 tc = cairo_create (surface);
107 /* paint background + border */
109 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
110 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
111 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
112 cairo_set_source (tc, shade_pattern);
113 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
116 cairo_pattern_destroy (shade_pattern);
118 /* paint lower shade */
122 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, w, 0);
123 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
124 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
125 cairo_set_source (tc, shade_pattern);
126 Gtkmm2ext::rounded_top_half_rectangle (tc, 1.0, get_height(), w, get_height(), radius-1.5);
129 cairo_pattern_destroy (shade_pattern);
131 pattern = cairo_pattern_create_for_surface (surface);
135 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
136 tc = cairo_create (surface);
138 /* paint right shade (background section)*/
140 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
141 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
142 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
143 cairo_set_source (tc, shade_pattern);
144 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
147 /* paint left shade (active section/foreground) */
149 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
150 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
151 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
152 cairo_set_source (tc, shade_pattern);
153 Gtkmm2ext::rounded_right_half_rectangle (tc, 0, 1, get_width(), get_height() - 2.0, radius-1.5);
155 cairo_pattern_destroy (shade_pattern);
157 pattern = cairo_pattern_create_for_surface (surface);
160 /* cache it for others to use */
162 _patterns.push_back (new FaderImage (pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
165 cairo_surface_destroy (surface);
167 if ( !_text.empty()) {
168 _layout->get_pixel_size (_text_width, _text_height);
174 c = get_style()->get_text (get_state());
176 text_r = c.get_red_p ();
177 text_g = c.get_green_p ();
178 text_b = c.get_blue_p ();
182 PixFader::on_expose_event (GdkEventExpose* ev)
184 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
185 cairo_t* cr = context->cobj();
187 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
194 int ds = display_span ();
195 float w = get_width();
196 float h = get_height();
198 cairo_matrix_t matrix;
200 if (_orien == VERT) {
202 if (ds > h - FADER_RESERVE) {
203 ds = h - FADER_RESERVE;
206 cairo_set_source (cr, pattern);
207 cairo_matrix_init_translate (&matrix, 0, (h - ds));
208 cairo_pattern_set_matrix (pattern, &matrix);
209 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
214 if (ds < FADER_RESERVE) {
219 if ds == w, the pattern does not need to be translated
220 if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
221 w to the left, which is -w in pattern space, and w in
223 if ds == 10, then the pattern needs to be moved w - 10
224 to the left, which is -(w-10) in pattern space, which
225 is (w - 10) in user space
227 thus: translation = (w - ds)
230 cairo_set_source (cr, pattern);
231 cairo_matrix_init_translate (&matrix, w - ds, 0);
232 cairo_pattern_set_matrix (pattern, &matrix);
233 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
237 /* draw the unity-position line if it's not at either end*/
239 if ( _orien == VERT) {
240 if (unity_loc < h ) {
241 context->set_line_width (1);
242 context->set_source_rgb (0.0, 1.0, 0.0);
243 context->move_to (1, unity_loc);
244 context->line_to (girth - 2.0, unity_loc);
248 if ( unity_loc < w ){
249 context->set_line_width (1);
250 context->set_source_rgb (0.0, 1.0, 0.0);
251 context->move_to (unity_loc, 1);
252 context->line_to (unity_loc, girth - 2.0);
258 if ( !_text.empty() ) {
263 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
265 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
266 pango_cairo_show_layout (cr, _layout->gobj());
269 // if (Config->get_widget_prelight()) { //pixfader does not have access to config
271 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
272 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
283 PixFader::on_size_request (GtkRequisition* req)
285 if (_orien == VERT) {
295 PixFader::on_size_allocate (Gtk::Allocation& alloc)
297 DrawingArea::on_size_allocate(alloc);
299 if (_orien == VERT) {
300 girth = alloc.get_width ();
301 span = alloc.get_height ();
303 girth = alloc.get_height ();
304 span = alloc.get_width ();
307 update_unity_position ();
316 PixFader::on_button_press_event (GdkEventButton* ev)
318 if (ev->type != GDK_BUTTON_PRESS) {
322 if (ev->button != 1 && ev->button != 2) {
327 grab_loc = (_orien == VERT) ? ev->y : ev->x;
328 grab_start = (_orien == VERT) ? ev->y : ev->x;
329 grab_window = ev->window;
331 gdk_pointer_grab(ev->window,false,
332 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
335 if (ev->button == 2) {
336 set_adjustment_from_event (ev);
343 PixFader::on_button_release_event (GdkEventButton* ev)
345 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
347 switch (ev->button) {
352 gdk_pointer_ungrab (GDK_CURRENT_TIME);
355 Keyboard::magic_widget_drop_focus();
359 if (ev_pos == grab_start) {
361 /* no motion - just a click */
363 if (ev->state & Keyboard::TertiaryModifier) {
364 adjustment.set_value (default_value);
365 } else if (ev->state & Keyboard::GainFineScaleModifier) {
366 adjustment.set_value (adjustment.get_lower());
367 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
368 /* above the current display height, remember X Window coords */
369 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
371 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
382 set_adjustment_from_event (ev);
383 gdk_pointer_ungrab (GDK_CURRENT_TIME);
396 PixFader::on_scroll_event (GdkEventScroll* ev)
401 if (ev->state & Keyboard::GainFineScaleModifier) {
402 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
411 if (_orien == VERT) {
413 /* should left/right scroll affect vertical faders ? */
415 switch (ev->direction) {
418 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
421 case GDK_SCROLL_DOWN:
422 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
430 /* up/down scrolls should definitely affect horizontal faders
431 because they are so much easier to use
434 switch (ev->direction) {
436 case GDK_SCROLL_RIGHT:
438 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
441 case GDK_SCROLL_LEFT:
442 case GDK_SCROLL_DOWN:
443 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
454 PixFader::on_motion_notify_event (GdkEventMotion* ev)
458 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
460 if (ev->window != grab_window) {
462 grab_window = ev->window;
466 if (ev->state & Keyboard::GainFineScaleModifier) {
467 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
474 double const delta = ev_pos - grab_loc;
477 double fract = (delta / span);
479 fract = min (1.0, fract);
480 fract = max (-1.0, fract);
482 // X Window is top->bottom for 0..Y
484 if (_orien == VERT) {
488 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
495 PixFader::adjustment_changed ()
497 if (display_span() != last_drawn) {
502 /** @return pixel offset of the current value from the right or bottom of the fader */
504 PixFader::display_span ()
506 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
508 if (_orien == VERT) {
509 ds = (int)floor ( span * (1.0 - fract));
511 ds = (int)floor (span * fract);
518 PixFader::set_fader_length (int l)
521 update_unity_position ();
526 PixFader::update_unity_position ()
528 if (_orien == VERT) {
529 unity_loc = (int) rint (span * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
531 unity_loc = (int) rint (default_value * span / (adjustment.get_upper() - adjustment.get_lower()));
538 PixFader::on_enter_notify_event (GdkEventCrossing*)
541 Keyboard::magic_widget_grab_focus ();
547 PixFader::on_leave_notify_event (GdkEventCrossing*)
551 Keyboard::magic_widget_drop_focus();
558 PixFader::set_adjustment_from_event (GdkEventButton* ev)
560 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
562 fract = min (1.0, fract);
563 fract = max (0.0, fract);
565 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
569 PixFader::set_default_value (float d)
572 update_unity_position ();
576 PixFader::set_text (const std::string& str)
580 if (!_layout && !_text.empty()) {
581 _layout = Pango::Layout::create (get_pango_context());
585 _layout->set_text (str);
592 PixFader::on_state_changed (Gtk::StateType old_state)
594 Widget::on_state_changed (old_state);