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)
54 default_value = adjustment.get_value();
55 update_unity_position ();
57 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);
59 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
60 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
63 DrawingArea::set_size_request(girth, span);
65 DrawingArea::set_size_request(span, girth);
69 PixFader::~PixFader ()
74 PixFader::on_expose_event (GdkEventExpose* ev)
76 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
77 cairo_t* cr = context->cobj();
79 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
80 cairo_clip_preserve (cr);
82 Gdk::Color fg_col = get_style()->get_fg (get_state());
84 float ds = display_span ();
85 float w = get_width();
86 float h = get_height();
88 Gdk::Color bg (get_parent_bg());
89 CairoWidget::set_source_rgb_a (cr, bg);
90 cairo_rectangle (cr, 0, 0, w, h);
94 cairo_set_source_rgba (cr, 0.17, 0.17, 0.17, 1.0);
95 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-0.5);
98 //mask off the corners
99 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-0.5);
102 if (_orien == VERT) {
105 int progress = travel * (1.0-ds);
106 int top = 1 + progress;
109 //background gradient
110 if ( !CairoWidget::flat_buttons() ) {
111 cairo_pattern_t *bg_gradient = cairo_pattern_create_linear (0.0, 0.0, w, 0);
112 cairo_pattern_add_color_stop_rgba (bg_gradient, 0, 0, 0, 0, 0.4);
113 cairo_pattern_add_color_stop_rgba (bg_gradient, 0.2, 0, 0, 0, 0.2);
114 cairo_pattern_add_color_stop_rgba (bg_gradient, 1, 0, 0, 0, 0.0);
115 cairo_set_source (cr, bg_gradient);
116 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-1.5);
118 cairo_pattern_destroy(bg_gradient);
122 CairoWidget::set_source_rgb_a (cr, fg_col, 1.0);
123 Gtkmm2ext::rounded_top_rectangle (cr, 1, top, w-2, bottom, CORNER_RADIUS - 1.5);
127 if (!CairoWidget::flat_buttons() ) {
128 cairo_pattern_t *fg_gradient = cairo_pattern_create_linear (0.0, 0.0, w, 0);
129 cairo_pattern_add_color_stop_rgba (fg_gradient, 0, 0, 0, 0, 0.0);
130 cairo_pattern_add_color_stop_rgba (fg_gradient, 0.1, 0, 0, 0, 0.0);
131 cairo_pattern_add_color_stop_rgba (fg_gradient, 1, 0, 0, 0, 0.3);
132 cairo_set_source (cr, fg_gradient);
133 Gtkmm2ext::rounded_rectangle (cr, 1, top, w-2, bottom, CORNER_RADIUS - 1.5);
135 cairo_pattern_destroy(fg_gradient);
140 int progress = travel * ds;
142 int length = progress;
144 //background gradient
145 if ( !CairoWidget::flat_buttons() ) {
146 cairo_pattern_t *bg_gradient = cairo_pattern_create_linear (0.0, 0.0, 0, h);
147 cairo_pattern_add_color_stop_rgba (bg_gradient, 0, 0, 0, 0, 0.4);
148 cairo_pattern_add_color_stop_rgba (bg_gradient, 0.2, 0, 0, 0, 0.2);
149 cairo_pattern_add_color_stop_rgba (bg_gradient, 1, 0, 0, 0, 0.0);
150 cairo_set_source (cr, bg_gradient);
151 Gtkmm2ext::rounded_rectangle (cr, 1, 1, w-2, h-2, CORNER_RADIUS-1.5);
153 cairo_pattern_destroy(bg_gradient);
157 CairoWidget::set_source_rgb_a (cr, fg_col, 1.0);
158 Gtkmm2ext::rounded_rectangle (cr, left, 1, length, h-2, CORNER_RADIUS - 1.5);
162 if (!CairoWidget::flat_buttons() ) {
163 cairo_pattern_t * fg_gradient = cairo_pattern_create_linear (0.0, 0.0, 0, h);
164 cairo_pattern_add_color_stop_rgba (fg_gradient, 0, 0, 0, 0, 0.0);
165 cairo_pattern_add_color_stop_rgba (fg_gradient, 0.1, 0, 0, 0, 0.0);
166 cairo_pattern_add_color_stop_rgba (fg_gradient, 1, 0, 0, 0, 0.3);
167 cairo_set_source (cr, fg_gradient);
168 Gtkmm2ext::rounded_rectangle (cr, left, 1, length, h-2, CORNER_RADIUS - 1.5);
170 cairo_pattern_destroy(fg_gradient);
174 cairo_reset_clip(cr);
177 cairo_set_line_width (cr, 1.0);
178 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
179 Gtkmm2ext::rounded_rectangle (cr, 0.5, 0.5, w-1, h-1, CORNER_RADIUS);
182 /* draw the unity-position line if it's not at either end*/
184 context->set_line_width (1);
185 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
186 CairoWidget::set_source_rgb_a (cr, c, 1.0);
187 if ( _orien == VERT) {
188 if (unity_loc < h ) {
189 context->move_to (2.5, unity_loc + CORNER_OFFSET + .5);
190 context->line_to (girth-2.5, unity_loc + CORNER_OFFSET + .5);
194 if ( unity_loc < w ){
195 context->move_to (unity_loc - CORNER_OFFSET + .5, 3.5);
196 context->line_to (unity_loc - CORNER_OFFSET + .5, girth-3.5);
203 if ( !_text.empty() ) {
206 if ( !_text.empty()) {
207 _layout->get_pixel_size (_text_width, _text_height);
215 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
216 Gdk::Color c = get_style()->get_text (get_state());
217 CairoWidget::set_source_rgb_a (cr, c, 0.9);
218 pango_cairo_show_layout (cr, _layout->gobj());
221 if (!get_sensitive()) {
222 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
223 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
225 } else if (_hovering) {
226 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
227 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
237 PixFader::on_size_request (GtkRequisition* req)
239 if (_orien == VERT) {
240 req->width = (girth ? girth : -1);
241 req->height = (span ? span : -1);
243 req->height = (girth ? girth : -1);
244 req->width = (span ? span : -1);
249 PixFader::on_size_allocate (Gtk::Allocation& alloc)
251 DrawingArea::on_size_allocate(alloc);
253 if (_orien == VERT) {
254 girth = alloc.get_width ();
255 span = alloc.get_height ();
257 girth = alloc.get_height ();
258 span = alloc.get_width ();
261 update_unity_position ();
265 PixFader::on_button_press_event (GdkEventButton* ev)
267 if (ev->type != GDK_BUTTON_PRESS) {
271 if (ev->button != 1 && ev->button != 2) {
276 grab_loc = (_orien == VERT) ? ev->y : ev->x;
277 grab_start = (_orien == VERT) ? ev->y : ev->x;
278 grab_window = ev->window;
280 gdk_pointer_grab(ev->window,false,
281 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
284 if (ev->button == 2) {
285 set_adjustment_from_event (ev);
292 PixFader::on_button_release_event (GdkEventButton* ev)
294 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
296 switch (ev->button) {
301 gdk_pointer_ungrab (GDK_CURRENT_TIME);
304 Keyboard::magic_widget_drop_focus();
308 if (ev_pos == grab_start) {
310 /* no motion - just a click */
312 if (ev->state & Keyboard::TertiaryModifier) {
313 adjustment.set_value (default_value);
314 } else if (ev->state & Keyboard::GainFineScaleModifier) {
315 adjustment.set_value (adjustment.get_lower());
316 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
317 /* above the current display height, remember X Window coords */
318 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
320 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
331 set_adjustment_from_event (ev);
332 gdk_pointer_ungrab (GDK_CURRENT_TIME);
345 PixFader::on_scroll_event (GdkEventScroll* ev)
350 if (ev->state & Keyboard::GainFineScaleModifier) {
351 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
360 if (_orien == VERT) {
362 /* should left/right scroll affect vertical faders ? */
364 switch (ev->direction) {
367 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
370 case GDK_SCROLL_DOWN:
371 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
379 /* up/down scrolls should definitely affect horizontal faders
380 because they are so much easier to use
383 switch (ev->direction) {
385 case GDK_SCROLL_RIGHT:
387 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
390 case GDK_SCROLL_LEFT:
391 case GDK_SCROLL_DOWN:
392 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
403 PixFader::on_motion_notify_event (GdkEventMotion* ev)
407 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
409 if (ev->window != grab_window) {
411 grab_window = ev->window;
415 if (ev->state & Keyboard::GainFineScaleModifier) {
416 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
423 double const delta = ev_pos - grab_loc;
426 double fract = (delta / span);
428 fract = min (1.0, fract);
429 fract = max (-1.0, fract);
431 // X Window is top->bottom for 0..Y
433 if (_orien == VERT) {
437 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
444 PixFader::adjustment_changed ()
446 if (display_span() != last_drawn) {
451 /** @return pixel offset of the current value from the right or bottom of the fader */
453 PixFader::display_span ()
455 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
461 PixFader::update_unity_position ()
463 if (_orien == VERT) {
464 unity_loc = (int) rint (span * (1 - ((default_value - adjustment.get_lower()) / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
466 unity_loc = (int) rint ((default_value - adjustment.get_lower()) * span / (adjustment.get_upper() - adjustment.get_lower()));
473 PixFader::on_enter_notify_event (GdkEventCrossing*)
476 Keyboard::magic_widget_grab_focus ();
482 PixFader::on_leave_notify_event (GdkEventCrossing*)
486 Keyboard::magic_widget_drop_focus();
493 PixFader::set_adjustment_from_event (GdkEventButton* ev)
495 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
497 fract = min (1.0, fract);
498 fract = max (0.0, fract);
500 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
504 PixFader::set_default_value (float d)
507 update_unity_position ();
511 PixFader::set_text (const std::string& str)
515 if (!_layout && !_text.empty()) {
516 _layout = Pango::Layout::create (get_pango_context());
520 _layout->set_text (str);
521 _layout->get_pixel_size (_text_width, _text_height);
528 PixFader::on_state_changed (Gtk::StateType old_state)
530 Widget::on_state_changed (old_state);
535 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
538 std::string txt = _layout->get_text();
539 _layout.clear (); // drop reference to existing layout
547 PixFader::get_parent_bg ()
551 parent = get_parent ();
554 if (!parent->get_has_window()) {
555 parent = parent->get_parent();
561 if (parent && parent->get_has_window()) {
562 if (_current_parent != parent) {
563 if (_parent_style_change) _parent_style_change.disconnect();
564 _current_parent = parent;
565 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &PixFader::on_style_changed));
567 return parent->get_style ()->get_bg (parent->get_state());
570 return get_style ()->get_bg (get_state());