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, int fader_girth)
39 , _orien (orientation)
46 default_value = adjustment.get_value();
47 update_unity_position ();
49 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);
51 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
52 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
55 PixFader::~PixFader ()
61 PixFader::free_patterns ()
64 cairo_pattern_destroy (pattern);
67 if (texture_pattern) {
68 cairo_pattern_destroy (texture_pattern);
74 PixFader::create_patterns ()
76 Gdk::Color c = get_style()->get_fg (get_state());
85 cairo_surface_t* texture_surface;
87 const double texture_margin = 3.0;
91 pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
92 cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
93 cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
96 texture_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, girth, 6);
97 tc = cairo_create (texture_surface);
99 for (double x = texture_margin; x < girth - texture_margin; x += 3.0) {
100 cairo_set_source_rgba (tc, 0.533, 0.533, 0.580, 1.0);
101 cairo_rectangle (tc, x, 2, 2, 2);
103 cairo_set_source_rgba (tc, 0.337, 0.345, 0.349, 1.0);
104 cairo_rectangle (tc, x, 2, 1, 1);
111 texture_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 6, girth);
112 tc = cairo_create (texture_surface);
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);
119 for (double y = texture_margin; y < girth - texture_margin; y += 3) {
120 cairo_set_source_rgba (tc, 0.533, 0.533, 0.580, 1.0);
121 cairo_rectangle (tc, 0, y, 2, 2);
123 cairo_set_source_rgba (tc, 0.337, 0.345, 0.349, 1.0);
124 cairo_rectangle (tc, 0, y, 1, 1);
130 if (texture_surface) {
131 texture_pattern = cairo_pattern_create_for_surface (texture_surface);
132 cairo_pattern_set_extend (texture_pattern, CAIRO_EXTEND_REPEAT);
135 cairo_surface_destroy (texture_surface);
138 if ( !_text.empty()) {
139 _layout->get_pixel_size (_text_width, _text_height);
145 c = get_style()->get_text (get_state());
147 text_r = c.get_red_p ();
148 text_g = c.get_green_p ();
149 text_b = c.get_blue_p ();
153 PixFader::on_expose_event (GdkEventExpose* ev)
155 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
156 cairo_t* cr = context->cobj();
158 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
165 // int const pi = get_sensitive() ? NORMAL : DESENSITISED;
167 int ds = display_span ();
169 float w = get_width();
170 float h = get_height();
171 float radius = CORNER_RADIUS;
173 /* background/ border */
174 cairo_set_source_rgb (cr, 0.290, 0.286, 0.337);
175 cairo_rectangle (cr, 0, 0, w, h);
178 /* draw active box */
180 cairo_matrix_t matrix;
182 if (_orien == VERT) {
184 if (ds > h - FADER_RESERVE) {
185 ds = h - FADER_RESERVE;
188 cairo_set_source (cr, pattern);
189 Gtkmm2ext::rounded_top_half_rectangle (cr, 1, 1+ds, w-1, h-(1+ds)-1, radius-1.5);
192 if (texture_pattern) {
194 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
195 cairo_set_source (cr, texture_pattern);
196 cairo_matrix_init_translate (&matrix, -1, -(1+ds));
197 cairo_pattern_set_matrix (texture_pattern, &matrix);
198 cairo_rectangle (cr, 1, 1+ds, w-1, h-(1+ds)-1);
205 if (ds < FADER_RESERVE) {
209 cairo_set_source (cr, pattern);
210 Gtkmm2ext::rounded_right_half_rectangle (cr, 1, 1, ds-1, h-1, radius-1.5);
213 if (texture_pattern) {
215 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
216 cairo_set_source (cr, texture_pattern);
217 cairo_matrix_init_translate (&matrix, -1, -1);
218 cairo_pattern_set_matrix (texture_pattern, &matrix);
219 cairo_rectangle (cr, 1, 1, ds-1, h-1);
225 /* draw the unity-position line if it's not at either end*/
227 if ( _orien == VERT) {
228 if (unity_loc < h ) {
229 context->set_line_width (1);
230 context->set_source_rgb (0.0, 1.0, 0.0);
231 context->move_to (1, unity_loc);
232 context->line_to (girth, unity_loc);
236 if ( unity_loc < w ){
237 context->set_line_width (1);
238 context->set_source_rgb (0.0, 1.0, 0.0);
239 context->move_to (unity_loc, 1);
240 context->line_to (unity_loc, girth);
246 if ( !_text.empty() ) {
251 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
253 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
254 pango_cairo_show_layout (cr, _layout->gobj());
257 // if (Config->get_widget_prelight()) { //pixfader does not have access to config
259 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
260 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
271 PixFader::on_size_request (GtkRequisition* req)
273 if (_orien == VERT) {
283 PixFader::on_size_allocate (Gtk::Allocation& alloc)
285 DrawingArea::on_size_allocate(alloc);
287 if (_orien == VERT) {
288 span = alloc.get_height();
289 girth = alloc.get_width ();
291 span = alloc.get_width();
292 girth = alloc.get_height ();
295 update_unity_position ();
304 PixFader::on_button_press_event (GdkEventButton* ev)
306 if (ev->type != GDK_BUTTON_PRESS) {
310 if (ev->button != 1 && ev->button != 2) {
315 grab_loc = (_orien == VERT) ? ev->y : ev->x;
316 grab_start = (_orien == VERT) ? ev->y : ev->x;
317 grab_window = ev->window;
319 gdk_pointer_grab(ev->window,false,
320 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
323 if (ev->button == 2) {
324 set_adjustment_from_event (ev);
331 PixFader::on_button_release_event (GdkEventButton* ev)
333 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
335 switch (ev->button) {
340 gdk_pointer_ungrab (GDK_CURRENT_TIME);
343 Keyboard::magic_widget_drop_focus();
347 if (ev_pos == grab_start) {
349 /* no motion - just a click */
351 if (ev->state & Keyboard::TertiaryModifier) {
352 adjustment.set_value (default_value);
353 } else if (ev->state & Keyboard::GainFineScaleModifier) {
354 adjustment.set_value (adjustment.get_lower());
355 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
356 /* above the current display height, remember X Window coords */
357 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
359 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
370 set_adjustment_from_event (ev);
371 gdk_pointer_ungrab (GDK_CURRENT_TIME);
383 PixFader::on_scroll_event (GdkEventScroll* ev)
388 if (ev->state & Keyboard::GainFineScaleModifier) {
389 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
398 if (_orien == VERT) {
400 /* should left/right scroll affect vertical faders ? */
402 switch (ev->direction) {
405 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
408 case GDK_SCROLL_DOWN:
409 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
417 /* up/down scrolls should definitely affect horizontal faders
418 because they are so much easier to use
421 switch (ev->direction) {
423 case GDK_SCROLL_RIGHT:
425 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
428 case GDK_SCROLL_LEFT:
429 case GDK_SCROLL_DOWN:
430 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
441 PixFader::on_motion_notify_event (GdkEventMotion* ev)
445 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
447 if (ev->window != grab_window) {
449 grab_window = ev->window;
453 if (ev->state & Keyboard::GainFineScaleModifier) {
454 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
461 double const delta = ev_pos - grab_loc;
464 double fract = (delta / span);
466 fract = min (1.0, fract);
467 fract = max (-1.0, fract);
469 // X Window is top->bottom for 0..Y
471 if (_orien == VERT) {
475 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
482 PixFader::adjustment_changed ()
484 if (display_span() != last_drawn) {
489 /** @return pixel offset of the current value from the right or bottom of the fader */
491 PixFader::display_span ()
493 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
495 if (_orien == VERT) {
496 ds = (int)floor ( span * (1.0 - fract));
498 ds = (int)floor (span * fract);
505 PixFader::set_fader_length (int l)
508 update_unity_position ();
513 PixFader::update_unity_position ()
515 if (_orien == VERT) {
516 unity_loc = (int) rint (span * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
518 unity_loc = (int) rint (default_value * span);
525 PixFader::on_enter_notify_event (GdkEventCrossing*)
528 Keyboard::magic_widget_grab_focus ();
534 PixFader::on_leave_notify_event (GdkEventCrossing*)
538 Keyboard::magic_widget_drop_focus();
545 PixFader::set_adjustment_from_event (GdkEventButton* ev)
547 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
549 fract = min (1.0, fract);
550 fract = max (0.0, fract);
552 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
556 PixFader::set_default_value (float d)
559 update_unity_position ();
563 PixFader::set_text (const std::string& str)
567 if (!_layout && !_text.empty()) {
568 _layout = Pango::Layout::create (get_pango_context());
572 _layout->set_text (str);
579 PixFader::on_state_changed (Gtk::StateType old_state)
581 Widget::on_state_changed (old_state);