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"
30 #include "gtkmm2ext/cairo_widget.h"
32 using namespace Gtkmm2ext;
36 #define CORNER_RADIUS 4
38 #define CORNER_OFFSET 1
39 #define FADER_RESERVE 5
41 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
45 , _orien (orientation)
53 default_value = adjustment.get_value();
54 update_unity_position ();
56 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);
58 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
59 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
62 DrawingArea::set_size_request(girth, span);
64 DrawingArea::set_size_request(span, girth);
68 PixFader::~PixFader ()
73 PixFader::on_expose_event (GdkEventExpose* ev)
75 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
76 cairo_t* cr = context->cobj();
78 Gdk::Color fg_col = get_style()->get_fg (get_state());
80 float ds = display_span ();
81 float w = get_width();
82 float h = get_height();
84 //fill in the bg rect ...
85 Gdk::Color c = get_style()->get_bg (Gtk::STATE_PRELIGHT); //why prelight? Shouldn't we be using the parent's color?
86 CairoWidget::set_source_rgb_a (cr, c);
87 cairo_rectangle (cr, 0, 0, w, h);
91 cairo_set_source_rgba (cr, 0.17, 0.17, 0.17, 1.0);
92 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-0.5);
95 //mask off the corners
96 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-0.5);
102 int progress = travel * (1.0-ds);
103 int top = 1 + progress;
106 //background gradient
107 if ( !CairoWidget::flat_buttons() ) {
108 cairo_pattern_t *bg_gradient = cairo_pattern_create_linear (0.0, 0.0, w, 0);
109 cairo_pattern_add_color_stop_rgba (bg_gradient, 0, 0, 0, 0, 0.4);
110 cairo_pattern_add_color_stop_rgba (bg_gradient, 0.2, 0, 0, 0, 0.2);
111 cairo_pattern_add_color_stop_rgba (bg_gradient, 1, 0, 0, 0, 0.0);
112 cairo_set_source (cr, bg_gradient);
113 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-1.5);
115 cairo_pattern_destroy(bg_gradient);
119 CairoWidget::set_source_rgb_a (cr, fg_col, 1.0);
120 Gtkmm2ext::rounded_top_rectangle (cr, 1, top, w-2, bottom, CORNER_RADIUS - 1.5);
124 if (!CairoWidget::flat_buttons() ) {
125 cairo_pattern_t *fg_gradient = cairo_pattern_create_linear (0.0, 0.0, w, 0);
126 cairo_pattern_add_color_stop_rgba (fg_gradient, 0, 0, 0, 0, 0.0);
127 cairo_pattern_add_color_stop_rgba (fg_gradient, 0.1, 0, 0, 0, 0.0);
128 cairo_pattern_add_color_stop_rgba (fg_gradient, 1, 0, 0, 0, 0.3);
129 cairo_set_source (cr, fg_gradient);
130 Gtkmm2ext::rounded_rectangle (cr, 1, top, w-2, bottom, CORNER_RADIUS - 1.5);
132 cairo_pattern_destroy(fg_gradient);
137 int progress = travel * ds;
139 int length = progress;
141 //background gradient
142 if ( !CairoWidget::flat_buttons() ) {
143 cairo_pattern_t *bg_gradient = cairo_pattern_create_linear (0.0, 0.0, 0, h);
144 cairo_pattern_add_color_stop_rgba (bg_gradient, 0, 0, 0, 0, 0.4);
145 cairo_pattern_add_color_stop_rgba (bg_gradient, 0.2, 0, 0, 0, 0.2);
146 cairo_pattern_add_color_stop_rgba (bg_gradient, 1, 0, 0, 0, 0.0);
147 cairo_set_source (cr, bg_gradient);
148 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-1.5);
150 cairo_pattern_destroy(bg_gradient);
154 CairoWidget::set_source_rgb_a (cr, fg_col, 1.0);
155 Gtkmm2ext::rounded_rectangle (cr, left, 1, length, h-2, CORNER_RADIUS - 1.5);
159 if (!CairoWidget::flat_buttons() ) {
160 cairo_pattern_t * fg_gradient = cairo_pattern_create_linear (0.0, 0.0, 0, h);
161 cairo_pattern_add_color_stop_rgba (fg_gradient, 0, 0, 0, 0, 0.0);
162 cairo_pattern_add_color_stop_rgba (fg_gradient, 0.1, 0, 0, 0, 0.0);
163 cairo_pattern_add_color_stop_rgba (fg_gradient, 1, 0, 0, 0, 0.3);
164 cairo_set_source (cr, fg_gradient);
165 Gtkmm2ext::rounded_rectangle (cr, left, 1, length, h-2, CORNER_RADIUS - 1.5);
167 cairo_pattern_destroy(fg_gradient);
171 cairo_reset_clip(cr);
174 cairo_set_line_width (cr, 1.0);
175 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
176 Gtkmm2ext::rounded_rectangle (cr, 0.5, 0.5, w-1, h-1, CORNER_RADIUS);
179 /* draw the unity-position line if it's not at either end*/
181 context->set_line_width (1);
182 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
183 CairoWidget::set_source_rgb_a (cr, c, 1.0);
184 if ( _orien == VERT) {
185 if (unity_loc < h ) {
186 context->move_to (2.5, unity_loc + CORNER_OFFSET + .5);
187 context->line_to (girth-2.5, unity_loc + CORNER_OFFSET + .5);
191 if ( unity_loc < w ){
192 context->move_to (unity_loc - CORNER_OFFSET + .5, 3.5);
193 context->line_to (unity_loc - CORNER_OFFSET + .5, girth-3.5);
200 if ( !_text.empty() ) {
203 if ( !_text.empty()) {
204 _layout->get_pixel_size (_text_width, _text_height);
212 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
213 Gdk::Color c = get_style()->get_text (get_state());
214 CairoWidget::set_source_rgb_a (cr, c, 0.9);
215 pango_cairo_show_layout (cr, _layout->gobj());
218 if (!get_sensitive()) {
219 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
220 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
222 } else if (_hovering) {
223 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
224 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
234 PixFader::on_size_request (GtkRequisition* req)
236 if (_orien == VERT) {
237 req->width = (girth ? girth : -1);
238 req->height = (span ? span : -1);
240 req->height = (girth ? girth : -1);
241 req->width = (span ? span : -1);
246 PixFader::on_size_allocate (Gtk::Allocation& alloc)
248 DrawingArea::on_size_allocate(alloc);
250 if (_orien == VERT) {
251 girth = alloc.get_width ();
252 span = alloc.get_height ();
254 girth = alloc.get_height ();
255 span = alloc.get_width ();
258 update_unity_position ();
262 PixFader::on_button_press_event (GdkEventButton* ev)
264 if (ev->type != GDK_BUTTON_PRESS) {
268 if (ev->button != 1 && ev->button != 2) {
273 grab_loc = (_orien == VERT) ? ev->y : ev->x;
274 grab_start = (_orien == VERT) ? ev->y : ev->x;
275 grab_window = ev->window;
277 gdk_pointer_grab(ev->window,false,
278 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
281 if (ev->button == 2) {
282 set_adjustment_from_event (ev);
289 PixFader::on_button_release_event (GdkEventButton* ev)
291 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
293 switch (ev->button) {
298 gdk_pointer_ungrab (GDK_CURRENT_TIME);
301 Keyboard::magic_widget_drop_focus();
305 if (ev_pos == grab_start) {
307 /* no motion - just a click */
309 if (ev->state & Keyboard::TertiaryModifier) {
310 adjustment.set_value (default_value);
311 } else if (ev->state & Keyboard::GainFineScaleModifier) {
312 adjustment.set_value (adjustment.get_lower());
313 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
314 /* above the current display height, remember X Window coords */
315 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
317 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
328 set_adjustment_from_event (ev);
329 gdk_pointer_ungrab (GDK_CURRENT_TIME);
342 PixFader::on_scroll_event (GdkEventScroll* ev)
347 if (ev->state & Keyboard::GainFineScaleModifier) {
348 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
357 if (_orien == VERT) {
359 /* should left/right scroll affect vertical faders ? */
361 switch (ev->direction) {
364 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
367 case GDK_SCROLL_DOWN:
368 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
376 /* up/down scrolls should definitely affect horizontal faders
377 because they are so much easier to use
380 switch (ev->direction) {
382 case GDK_SCROLL_RIGHT:
384 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
387 case GDK_SCROLL_LEFT:
388 case GDK_SCROLL_DOWN:
389 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
400 PixFader::on_motion_notify_event (GdkEventMotion* ev)
404 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
406 if (ev->window != grab_window) {
408 grab_window = ev->window;
412 if (ev->state & Keyboard::GainFineScaleModifier) {
413 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
420 double const delta = ev_pos - grab_loc;
423 double fract = (delta / span);
425 fract = min (1.0, fract);
426 fract = max (-1.0, fract);
428 // X Window is top->bottom for 0..Y
430 if (_orien == VERT) {
434 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
441 PixFader::adjustment_changed ()
443 if (display_span() != last_drawn) {
448 /** @return pixel offset of the current value from the right or bottom of the fader */
450 PixFader::display_span ()
452 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
458 PixFader::update_unity_position ()
460 if (_orien == VERT) {
461 unity_loc = (int) rint (span * (1 - ((default_value - adjustment.get_lower()) / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
463 unity_loc = (int) rint ((default_value - adjustment.get_lower()) * span / (adjustment.get_upper() - adjustment.get_lower()));
470 PixFader::on_enter_notify_event (GdkEventCrossing*)
473 Keyboard::magic_widget_grab_focus ();
479 PixFader::on_leave_notify_event (GdkEventCrossing*)
483 Keyboard::magic_widget_drop_focus();
490 PixFader::set_adjustment_from_event (GdkEventButton* ev)
492 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
494 fract = min (1.0, fract);
495 fract = max (0.0, fract);
497 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
501 PixFader::set_default_value (float d)
504 update_unity_position ();
508 PixFader::set_text (const std::string& str)
512 if (!_layout && !_text.empty()) {
513 _layout = Pango::Layout::create (get_pango_context());
517 _layout->set_text (str);
518 _layout->get_pixel_size (_text_width, _text_height);
525 PixFader::on_state_changed (Gtk::StateType old_state)
527 Widget::on_state_changed (old_state);
532 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
535 std::string txt = _layout->get_text();
536 _layout.clear (); // drop reference to existing layout