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,
45 pixbuf[NORMAL] = belt;
46 pixbuf[DESENSITISED] = belt_desensitised;
53 default_value = adjustment.get_value();
59 if (orientation == VERT) {
60 view.width = girth = 24;
62 view.height = girth = 24;
65 set_fader_length (fader_length);
67 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);
69 adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
70 adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
72 for (int i = 0; i < STATES; ++i) {
75 if (pixbuf[i]->get_has_alpha()) {
76 format = Cairo::FORMAT_ARGB32;
78 format = Cairo::FORMAT_RGB24;
81 belt_surface[i] = Cairo::ImageSurface::create (format, pixbuf[i]->get_width(), pixbuf[i]->get_height());
82 belt_context[i] = Cairo::Context::create (belt_surface[i]);
83 Gdk::Cairo::set_source_pixbuf (belt_context[i], pixbuf[i], 0.0, 0.0);
84 belt_context[i]->paint();
96 PixFader::~PixFader ()
101 PixFader::set_border_colors (uint32_t left, uint32_t right)
104 UINT_TO_RGB(left, &r, &g, &b);
108 UINT_TO_RGB(right, &r, &g, &b);
115 PixFader::create_patterns ()
117 Gdk::Color c = get_style()->get_fg (get_state());
120 g = c.get_green_p ();
123 if (_orien == VERT) {
124 pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
125 cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
126 cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
128 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 15, 0);
129 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
130 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
131 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
132 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
134 float rheight = get_height();
136 pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, rheight);
137 cairo_pattern_add_color_stop_rgba (pattern, 0, r*0.8,g*0.8,b*0.8, 1.0);
138 cairo_pattern_add_color_stop_rgba (pattern, 1, r*0.6,g*0.6,b*0.6, 1.0);
140 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, rheight);
141 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
142 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.3);
143 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.0);
144 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.0);
147 if ( !_text.empty()) {
148 _layout->get_pixel_size (_text_width, _text_height);
153 c = get_style()->get_text (get_state());
154 text_r = c.get_red_p ();
155 text_g = c.get_green_p ();
156 text_b = c.get_blue_p ();
160 PixFader::on_expose_event (GdkEventExpose* ev)
162 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
163 cairo_t* cr = context->cobj();
169 // int const pi = get_sensitive() ? NORMAL : DESENSITISED;
171 int ds = display_span ();
173 float w = get_width();
174 float h = get_height();
175 float radius = CORNER_RADIUS;
179 cairo_set_source_rgb (cr, 0,0,0);
180 cairo_rectangle (cr, 0, 0, w, h);
183 /* draw active box */
185 if (_orien == VERT) {
186 if (ds > h - FADER_RESERVE)
187 ds = h - FADER_RESERVE;
189 cairo_set_source (cr, pattern);
190 Gtkmm2ext::rounded_rectangle (cr, 1, 1+ds, w-2, h-(1+ds)-1, radius-1.5);
193 // cairo_set_source (cr, shine_pattern);
194 // Gtkmm2ext::rounded_rectangle (cr, 2, ds, w-4, h-(1+ds)-1, radius-1.5);
197 if (ds < FADER_RESERVE)
200 cairo_set_source (cr, pattern);
201 Gtkmm2ext::rounded_rectangle (cr, 1, 1, ds-1, h-2, radius-1.5);
204 // cairo_set_source (cr, shine_pattern);
205 // Gtkmm2ext::rounded_rectangle (cr, 2, 3, ds-1, 15, radius-1.5);
209 /* draw the unity-position line if it's not at either end*/
211 if ( _orien == VERT && unity_loc < h ) {
212 context->set_line_width (1);
213 context->set_source_rgb (0.0, 1.0, 0.0);
214 context->move_to (1, unity_loc);
215 context->line_to (girth, unity_loc);
217 } else if ( unity_loc < w ){
218 context->set_line_width (1);
219 context->set_source_rgb (0.0, 1.0, 0.0);
220 context->move_to (unity_loc, 1);
221 context->line_to (unity_loc, girth);
226 if ( !_text.empty() ) {
231 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
233 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
234 pango_cairo_show_layout (cr, _layout->gobj());
237 // if (Config->get_widget_prelight()) { //pixfader does not have access to config
239 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
240 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
251 PixFader::on_size_request (GtkRequisition* req)
253 req->width = view.width;
254 req->height = view.height;
258 PixFader::on_size_allocate (Gtk::Allocation& alloc)
260 DrawingArea::on_size_allocate(alloc);
261 if (_orien == VERT) {
262 view.height = span = alloc.get_height();
264 view.width = span = alloc.get_width();
267 update_unity_position ();
274 PixFader::on_button_press_event (GdkEventButton* ev)
276 if (ev->type != GDK_BUTTON_PRESS) {
280 if (ev->button != 1 && ev->button != 2) {
285 grab_loc = (_orien == VERT) ? ev->y : ev->x;
286 grab_start = (_orien == VERT) ? ev->y : ev->x;
287 grab_window = ev->window;
290 if (ev->button == 2) {
291 set_adjustment_from_event (ev);
298 PixFader::on_button_release_event (GdkEventButton* ev)
300 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
302 switch (ev->button) {
309 Keyboard::magic_widget_drop_focus();
313 if (ev_pos == grab_start) {
315 /* no motion - just a click */
317 if (ev->state & Keyboard::TertiaryModifier) {
318 adjustment.set_value (default_value);
319 } else if (ev->state & Keyboard::GainFineScaleModifier) {
320 adjustment.set_value (adjustment.get_lower());
321 } else if ((_orien == VERT && ev_pos < span - display_span()) || (_orien == HORIZ && ev_pos > span - display_span())) {
322 /* above the current display height, remember X Window coords */
323 adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
325 adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
336 set_adjustment_from_event (ev);
348 PixFader::on_scroll_event (GdkEventScroll* ev)
353 if (ev->state & Keyboard::GainFineScaleModifier) {
354 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
363 if (_orien == VERT) {
365 /* should left/right scroll affect vertical faders ? */
367 switch (ev->direction) {
370 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
373 case GDK_SCROLL_DOWN:
374 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
382 /* up/down scrolls should definitely affect horizontal faders
383 because they are so much easier to use
386 switch (ev->direction) {
388 case GDK_SCROLL_RIGHT:
390 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
393 case GDK_SCROLL_LEFT:
394 case GDK_SCROLL_DOWN:
395 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
406 PixFader::on_motion_notify_event (GdkEventMotion* ev)
410 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
412 if (ev->window != grab_window) {
414 grab_window = ev->window;
418 if (ev->state & Keyboard::GainFineScaleModifier) {
419 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
426 double const delta = ev_pos - grab_loc;
429 double fract = (delta / span);
431 fract = min (1.0, fract);
432 fract = max (-1.0, fract);
434 // X Window is top->bottom for 0..Y
436 if (_orien == VERT) {
440 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
447 PixFader::adjustment_changed ()
449 if (display_span() != last_drawn) {
454 /** @return pixel offset of the current value from the right or bottom of the fader */
456 PixFader::display_span ()
458 float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
460 if (_orien == VERT) {
461 ds = (int)floor ( span * (1.0 - fract));
463 ds = (int)floor (span * fract);
470 PixFader::set_fader_length (int l)
472 if (_orien == VERT) {
473 view.height = span = l;
475 view.width = span = l;
478 update_unity_position ();
484 PixFader::update_unity_position ()
486 if (_orien == VERT) {
487 unity_loc = (int) rint (view.height * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
489 unity_loc = (int) rint (default_value * view.width);
496 PixFader::on_enter_notify_event (GdkEventCrossing*)
499 Keyboard::magic_widget_grab_focus ();
505 PixFader::on_leave_notify_event (GdkEventCrossing*)
509 Keyboard::magic_widget_drop_focus();
516 PixFader::set_adjustment_from_event (GdkEventButton* ev)
518 double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
520 fract = min (1.0, fract);
521 fract = max (0.0, fract);
523 adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
527 PixFader::set_default_value (float d)
530 update_unity_position ();
534 PixFader::set_text (const std::string& str)
538 if (!_layout && !_text.empty()) {
539 _layout = Pango::Layout::create (get_pango_context());
543 _layout->set_text (str);