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)
36 Glib::RefPtr<Gdk::Pixbuf> belt,
37 Glib::RefPtr<Gdk::Pixbuf> belt_desensitised,
46 pixbuf[NORMAL] = belt;
47 pixbuf[DESENSITISED] = belt_desensitised;
55 default_value = adjustment.get_value();
61 if (orientation == VERT) {
62 view.width = girth = 24;
64 view.height = girth = 24;
67 set_fader_length (fader_length);
69 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);
71 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
72 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
75 for (int i = 0; i < STATES; ++i) {
78 if (pixbuf[i]->get_has_alpha()) {
79 format = Cairo::FORMAT_ARGB32;
81 format = Cairo::FORMAT_RGB24;
84 belt_surface[i] = Cairo::ImageSurface::create (format, pixbuf[i]->get_width(), pixbuf[i]->get_height());
85 belt_context[i] = Cairo::Context::create (belt_surface[i]);
86 Gdk::Cairo::set_source_pixbuf (belt_context[i], pixbuf[i], 0.0, 0.0);
87 belt_context[i]->paint();
100 PixFader::~PixFader ()
105 PixFader::set_border_colors (uint32_t left, uint32_t right)
108 UINT_TO_RGB(left, &r, &g, &b);
112 UINT_TO_RGB(right, &r, &g, &b);
119 PixFader::create_patterns ()
121 Gdk::Color c = get_style()->get_fg (get_state());
124 g = c.get_green_p ();
127 if (_orien == VERT) {
128 pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
129 cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
130 cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
132 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 15, 0);
133 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
134 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
135 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
136 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
138 float rheight = get_height();
140 pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, rheight);
141 cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
142 cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
144 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, rheight);
145 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
146 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
147 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
148 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
151 if ( !_text.empty()) {
152 _layout->get_pixel_size (_text_width, _text_height);
157 c = get_style()->get_text (get_state());
158 text_r = c.get_red_p ();
159 text_g = c.get_green_p ();
160 text_b = c.get_blue_p ();
164 PixFader::on_expose_event (GdkEventExpose* ev)
166 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
167 cairo_t* cr = context->cobj();
173 // int const pi = get_sensitive() ? NORMAL : DESENSITISED;
175 int ds = display_span ();
177 float w = get_width();
178 float h = get_height();
179 float radius = CORNER_RADIUS;
183 cairo_set_source_rgb (cr, 0,0,0);
184 cairo_rectangle (cr, 0, 0, w, h);
187 /* draw active box */
189 if (_orien == VERT) {
190 if (ds > h - FADER_RESERVE)
191 ds = h - FADER_RESERVE;
193 cairo_set_source (cr, pattern);
194 Gtkmm2ext::rounded_rectangle (cr, 1, 1+ds, w-2, h-(1+ds)-1, radius-1.5);
197 // cairo_set_source (cr, shine_pattern);
198 // Gtkmm2ext::rounded_rectangle (cr, 2, ds, w-4, h-(1+ds)-1, radius-1.5);
201 if (ds < FADER_RESERVE)
204 cairo_set_source (cr, pattern);
205 Gtkmm2ext::rounded_rectangle (cr, 1, 1, ds-1, h-2, radius-1.5);
208 // cairo_set_source (cr, shine_pattern);
209 // Gtkmm2ext::rounded_rectangle (cr, 2, 3, ds-1, 15, radius-1.5);
213 /* draw the unity-position line if it's not at either end*/
215 if ( _orien == VERT && unity_loc < h ) {
216 context->set_line_width (1);
217 context->set_source_rgb (0.0, 1.0, 0.0);
218 context->move_to (1, unity_loc);
219 context->line_to (girth, unity_loc);
221 } else if ( unity_loc < w ){
222 context->set_line_width (1);
223 context->set_source_rgb (0.0, 1.0, 0.0);
224 context->move_to (unity_loc, 1);
225 context->line_to (unity_loc, girth);
230 if ( !_text.empty() ) {
235 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
237 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
238 pango_cairo_show_layout (cr, _layout->gobj());
241 // if (Config->get_widget_prelight()) { //pixfader does not have access to config
243 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
244 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
255 PixFader::on_size_request (GtkRequisition* req)
257 req->width = view.width;
258 req->height = view.height;
262 PixFader::on_size_allocate (Gtk::Allocation& alloc)
264 DrawingArea::on_size_allocate(alloc);
265 if (_orien == VERT) {
266 view.height = span = alloc.get_height();
268 view.width = span = alloc.get_width();
271 update_unity_position ();
278 PixFader::on_button_press_event (GdkEventButton* ev)
280 if (ev->type != GDK_BUTTON_PRESS) {
284 if (ev->button != 1 && ev->button != 2) {
289 grab_loc = (_orien == VERT) ? ev->y : ev->x;
290 grab_start = (_orien == VERT) ? ev->y : ev->x;
291 grab_window = ev->window;
294 if (ev->button == 2) {
295 set_adjustment_from_event (ev);
302 PixFader::on_button_release_event (GdkEventButton* ev)
304 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
306 switch (ev->button) {
313 Keyboard::magic_widget_drop_focus();
317 if (ev_pos == grab_start) {
319 /* no motion - just a click */
321 if (ev->state & Keyboard::TertiaryModifier) {
322 adjustment.set_value (default_value);
323 } else if (ev->state & Keyboard::GainFineScaleModifier) {
324 adjustment.set_value (adjustment.get_lower());
325 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
326 /* above the current display height, remember X Window coords */
327 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
329 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
340 set_adjustment_from_event (ev);
352 PixFader::on_scroll_event (GdkEventScroll* ev)
357 if (ev->state & Keyboard::GainFineScaleModifier) {
358 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
367 if (_orien == VERT) {
369 /* should left/right scroll affect vertical faders ? */
371 switch (ev->direction) {
374 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
377 case GDK_SCROLL_DOWN:
378 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
386 /* up/down scrolls should definitely affect horizontal faders
387 because they are so much easier to use
390 switch (ev->direction) {
392 case GDK_SCROLL_RIGHT:
394 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
397 case GDK_SCROLL_LEFT:
398 case GDK_SCROLL_DOWN:
399 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
410 PixFader::on_motion_notify_event (GdkEventMotion* ev)
414 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
416 if (ev->window != grab_window) {
418 grab_window = ev->window;
422 if (ev->state & Keyboard::GainFineScaleModifier) {
423 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
430 double const delta = ev_pos - grab_loc;
433 double fract = (delta / span);
435 fract = min (1.0, fract);
436 fract = max (-1.0, fract);
438 // X Window is top->bottom for 0..Y
440 if (_orien == VERT) {
444 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
451 PixFader::adjustment_changed ()
453 if (display_span() != last_drawn) {
458 /** @return pixel offset of the current value from the right or bottom of the fader */
460 PixFader::display_span ()
462 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
464 if (_orien == VERT) {
465 ds = (int)floor ( span * (1.0 - fract));
467 ds = (int)floor (span * fract);
474 PixFader::set_fader_length (int l)
476 if (_orien == VERT) {
477 view.height = span = l;
479 view.width = span = l;
482 update_unity_position ();
488 PixFader::update_unity_position ()
490 if (_orien == VERT) {
491 unity_loc = (int) rint (view.height * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
493 unity_loc = (int) rint (default_value * view.width);
500 PixFader::on_enter_notify_event (GdkEventCrossing*)
503 Keyboard::magic_widget_grab_focus ();
509 PixFader::on_leave_notify_event (GdkEventCrossing*)
513 Keyboard::magic_widget_drop_focus();
520 PixFader::set_adjustment_from_event (GdkEventButton* ev)
522 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
524 fract = min (1.0, fract);
525 fract = max (0.0, fract);
527 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
531 PixFader::set_default_value (float d)
534 update_unity_position ();
538 PixFader::set_text (const std::string& str)
542 if (!_layout && !_text.empty()) {
543 _layout = Pango::Layout::create (get_pango_context());
547 _layout->set_text (str);