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 $
23 #include "gtkmm2ext/pixfader.h"
24 #include "gtkmm2ext/keyboard.h"
25 #include "gtkmm2ext/rgb_macros.h"
26 #include "gtkmm2ext/utils.h"
28 using namespace Gtkmm2ext;
32 #define CORNER_RADIUS 4
33 #define FADER_RESERVE (2*CORNER_RADIUS)
35 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length)
37 , _orien (orientation)
44 default_value = adjustment.get_value();
48 set_fader_length (fader_length);
50 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);
52 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
53 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
64 PixFader::~PixFader ()
69 PixFader::set_border_colors (uint32_t left, uint32_t right)
72 UINT_TO_RGB(left, &r, &g, &b);
76 UINT_TO_RGB(right, &r, &g, &b);
83 PixFader::create_patterns ()
85 Gdk::Color c = get_style()->get_fg (get_state());
89 cairo_pattern_destroy (pattern);
93 cairo_pattern_destroy (shine_pattern);
100 if (_orien == VERT) {
102 pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
103 cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
104 cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
106 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 15, 0);
107 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
108 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
109 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
110 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
114 pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
115 cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
116 cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
118 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
119 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
120 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
121 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
122 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
125 if ( !_text.empty()) {
126 _layout->get_pixel_size (_text_width, _text_height);
132 c = get_style()->get_text (get_state());
134 text_r = c.get_red_p ();
135 text_g = c.get_green_p ();
136 text_b = c.get_blue_p ();
140 PixFader::on_expose_event (GdkEventExpose*)
142 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
143 cairo_t* cr = context->cobj();
149 // int const pi = get_sensitive() ? NORMAL : DESENSITISED;
151 int ds = display_span ();
153 float w = get_width();
154 float h = get_height();
155 float radius = CORNER_RADIUS;
159 cairo_set_source_rgb (cr, 0,0,0);
160 cairo_rectangle (cr, 0, 0, w, h);
163 /* draw active box */
165 if (_orien == VERT) {
167 if (ds > h - FADER_RESERVE)
168 ds = h - FADER_RESERVE;
170 cairo_set_source (cr, pattern);
171 Gtkmm2ext::rounded_top_half_rectangle (cr, 1, 1+ds, w-1, h-(1+ds)-1, radius-1.5);
176 if (ds < FADER_RESERVE)
179 cairo_set_source (cr, pattern);
180 Gtkmm2ext::rounded_right_half_rectangle (cr, 1, 1, ds-1, h-1, radius-1.5);
185 /* draw the unity-position line if it's not at either end*/
187 if ( _orien == VERT) {
188 if (unity_loc < h ) {
189 context->set_line_width (1);
190 context->set_source_rgb (0.0, 1.0, 0.0);
191 context->move_to (1, unity_loc);
192 context->line_to (girth, unity_loc);
196 if ( unity_loc < w ){
197 context->set_line_width (1);
198 context->set_source_rgb (0.0, 1.0, 0.0);
199 context->move_to (unity_loc, 1);
200 context->line_to (unity_loc, girth);
206 if ( !_text.empty() ) {
211 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
213 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
214 pango_cairo_show_layout (cr, _layout->gobj());
217 // if (Config->get_widget_prelight()) { //pixfader does not have access to config
219 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
220 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
231 PixFader::on_size_request (GtkRequisition* req)
233 if (_orien == VERT) {
243 PixFader::on_size_allocate (Gtk::Allocation& alloc)
245 DrawingArea::on_size_allocate(alloc);
247 if (_orien == VERT) {
248 span = alloc.get_height();
250 span = alloc.get_width();
253 update_unity_position ();
260 PixFader::on_button_press_event (GdkEventButton* ev)
262 if (ev->type != GDK_BUTTON_PRESS) {
266 if (ev->button != 1 && ev->button != 2) {
271 grab_loc = (_orien == VERT) ? ev->y : ev->x;
272 grab_start = (_orien == VERT) ? ev->y : ev->x;
273 grab_window = ev->window;
275 gdk_pointer_grab(ev->window,false,
276 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
279 if (ev->button == 2) {
280 set_adjustment_from_event (ev);
287 PixFader::on_button_release_event (GdkEventButton* ev)
289 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
291 switch (ev->button) {
296 gdk_pointer_ungrab (GDK_CURRENT_TIME);
299 Keyboard::magic_widget_drop_focus();
303 if (ev_pos == grab_start) {
305 /* no motion - just a click */
307 if (ev->state & Keyboard::TertiaryModifier) {
308 adjustment.set_value (default_value);
309 } else if (ev->state & Keyboard::GainFineScaleModifier) {
310 adjustment.set_value (adjustment.get_lower());
311 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
312 /* above the current display height, remember X Window coords */
313 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
315 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
326 set_adjustment_from_event (ev);
327 gdk_pointer_ungrab (GDK_CURRENT_TIME);
339 PixFader::on_scroll_event (GdkEventScroll* ev)
344 if (ev->state & Keyboard::GainFineScaleModifier) {
345 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
354 if (_orien == VERT) {
356 /* should left/right scroll affect vertical faders ? */
358 switch (ev->direction) {
361 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
364 case GDK_SCROLL_DOWN:
365 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
373 /* up/down scrolls should definitely affect horizontal faders
374 because they are so much easier to use
377 switch (ev->direction) {
379 case GDK_SCROLL_RIGHT:
381 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
384 case GDK_SCROLL_LEFT:
385 case GDK_SCROLL_DOWN:
386 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
397 PixFader::on_motion_notify_event (GdkEventMotion* ev)
401 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
403 if (ev->window != grab_window) {
405 grab_window = ev->window;
409 if (ev->state & Keyboard::GainFineScaleModifier) {
410 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
417 double const delta = ev_pos - grab_loc;
420 double fract = (delta / span);
422 fract = min (1.0, fract);
423 fract = max (-1.0, fract);
425 // X Window is top->bottom for 0..Y
427 if (_orien == VERT) {
431 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
438 PixFader::adjustment_changed ()
440 if (display_span() != last_drawn) {
445 /** @return pixel offset of the current value from the right or bottom of the fader */
447 PixFader::display_span ()
449 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
451 if (_orien == VERT) {
452 ds = (int)floor ( span * (1.0 - fract));
454 ds = (int)floor (span * fract);
461 PixFader::set_fader_length (int l)
463 if (_orien == VERT) {
469 update_unity_position ();
475 PixFader::update_unity_position ()
477 if (_orien == VERT) {
478 unity_loc = (int) rint (span * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
480 unity_loc = (int) rint (default_value * span);
487 PixFader::on_enter_notify_event (GdkEventCrossing*)
490 Keyboard::magic_widget_grab_focus ();
496 PixFader::on_leave_notify_event (GdkEventCrossing*)
500 Keyboard::magic_widget_drop_focus();
507 PixFader::set_adjustment_from_event (GdkEventButton* ev)
509 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
511 fract = min (1.0, fract);
512 fract = max (0.0, fract);
514 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
518 PixFader::set_default_value (float d)
521 update_unity_position ();
525 PixFader::set_text (const std::string& str)
529 if (!_layout && !_text.empty()) {
530 _layout = Pango::Layout::create (get_pango_context());
534 _layout->set_text (str);