move show_gdk_event_state() utility function from gtk2_ardour to libs/gtkmm2ext
[ardour.git] / libs / gtkmm2ext / pixfader.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
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.
8
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.
13
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.
17
18     $Id: fastmeter.h 570 2006-06-07 21:21:21Z sampo $
19 */
20
21
22 #include <iostream>
23 #include <assert.h>
24
25 #include "pbd/stacktrace.h"
26
27 #include "gtkmm2ext/cairo_widget.h"
28 #include "gtkmm2ext/keyboard.h"
29 #include "gtkmm2ext/pixfader.h"
30 #include "gtkmm2ext/utils.h"
31
32 using namespace Gtkmm2ext;
33 using namespace Gtk;
34 using namespace std;
35
36 #define CORNER_RADIUS 2.5
37 #define CORNER_SIZE   2
38 #define CORNER_OFFSET 1
39 #define FADER_RESERVE 6
40
41 std::list<PixFader::FaderImage*> PixFader::_patterns;
42
43 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
44         : _layout (0)
45         , _tweaks (Tweaks(0))
46         , _adjustment (adj)
47         , _text_width (0)
48         , _text_height (0)
49         , _span (fader_length)
50         , _girth (fader_girth)
51         , _min_span (fader_length)
52         , _min_girth (fader_girth)
53         , _orien (orientation)
54         , _pattern (0)
55         , _hovering (false)
56         , _dragging (false)
57         , _centered_text (true)
58         , _current_parent (0)
59 {
60         _default_value = _adjustment.get_value();
61         update_unity_position ();
62
63         add_events (
64                           Gdk::BUTTON_PRESS_MASK
65                         | Gdk::BUTTON_RELEASE_MASK
66                         | Gdk::POINTER_MOTION_MASK
67                         | Gdk::SCROLL_MASK
68                         | Gdk::ENTER_NOTIFY_MASK
69                         | Gdk::LEAVE_NOTIFY_MASK
70                         );
71
72         _adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
73         _adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
74         signal_grab_broken_event ().connect (mem_fun (*this, &PixFader::on_grab_broken_event));
75         if (_orien == VERT) {
76                 CairoWidget::set_size_request(_girth, _span);
77         } else {
78                 CairoWidget::set_size_request(_span, _girth);
79         }
80 }
81
82 PixFader::~PixFader ()
83 {
84         if (_parent_style_change) _parent_style_change.disconnect();
85         if (_layout) _layout.clear (); // drop reference to existing layout
86 }
87
88 void
89 PixFader::flush_pattern_cache () {
90         for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
91                 cairo_pattern_destroy ((*f)->pattern);
92         }
93         _patterns.clear();
94 }
95
96
97 cairo_pattern_t*
98 PixFader::find_pattern (double afr, double afg, double afb,
99                         double abr, double abg, double abb,
100                         int w, int h)
101 {
102         for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
103                 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
104                         return (*f)->pattern;
105                 }
106         }
107         return 0;
108 }
109
110 void
111 PixFader::create_patterns ()
112 {
113         Gdk::Color c = get_style()->get_fg (get_state());
114         float fr, fg, fb;
115         float br, bg, bb;
116
117         fr = c.get_red_p ();
118         fg = c.get_green_p ();
119         fb = c.get_blue_p ();
120
121         c = get_style()->get_bg (get_state());
122
123         br = c.get_red_p ();
124         bg = c.get_green_p ();
125         bb = c.get_blue_p ();
126
127         cairo_surface_t* surface;
128         cairo_t* tc = 0;
129
130         if (get_width() <= 1 || get_height() <= 1) {
131                 return;
132         }
133
134         if ((_pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
135                 /* found it - use it */
136                 return;
137         }
138
139         if (_orien == VERT) {
140
141                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
142                 tc = cairo_create (surface);
143
144                 /* paint background + border */
145
146                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
147                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
148                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
149                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
150                 cairo_set_source (tc, shade_pattern);
151                 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
152                 cairo_fill (tc);
153
154                 cairo_pattern_destroy (shade_pattern);
155
156                 /* paint lower shade */
157
158                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width() - 2 - CORNER_OFFSET , 0);
159                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
160                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
161                 cairo_set_source (tc, shade_pattern);
162                 Gtkmm2ext::rounded_top_half_rectangle (tc, CORNER_OFFSET, get_height() + CORNER_OFFSET,
163                                 get_width() - CORNER_SIZE, get_height(), CORNER_RADIUS);
164                 cairo_fill (tc);
165
166                 cairo_pattern_destroy (shade_pattern);
167
168                 _pattern = cairo_pattern_create_for_surface (surface);
169
170         } else {
171
172                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
173                 tc = cairo_create (surface);
174
175                 /* paint right shade (background section)*/
176
177                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
178                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
179                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
180                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
181                 cairo_set_source (tc, shade_pattern);
182                 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
183                 cairo_fill (tc);
184
185                 /* paint left shade (active section/foreground) */
186
187                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
188                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
189                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
190                 cairo_set_source (tc, shade_pattern);
191                 Gtkmm2ext::rounded_right_half_rectangle (tc, CORNER_OFFSET, CORNER_OFFSET,
192                                 get_width() - CORNER_OFFSET, get_height() - CORNER_SIZE, CORNER_RADIUS);
193                 cairo_fill (tc);
194                 cairo_pattern_destroy (shade_pattern);
195
196                 _pattern = cairo_pattern_create_for_surface (surface);
197         }
198
199         /* cache it for others to use */
200
201         _patterns.push_back (new FaderImage (_pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
202
203         cairo_destroy (tc);
204         cairo_surface_destroy (surface);
205 }
206
207 void
208 PixFader::render (cairo_t *cr, cairo_rectangle_t* area)
209 {
210         if (!_pattern) {
211                 create_patterns();
212         }
213
214         if (!_pattern) {
215                 /* this isn't supposed to be happen, but some wackiness whereby
216                  * the pixfader ends up with a 1xN or Nx1 size allocation
217                  * leads to it. the basic wackiness needs fixing but we
218                  * shouldn't crash. just fill in the expose area with
219                  * our bg color.
220                  */
221
222                 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
223                 cairo_rectangle (cr, area->x, area->y, area->width, area->height);
224                 cairo_fill (cr);
225                 return;
226         }
227
228         OnExpose();
229         int ds = display_span ();
230         const float w = get_width();
231         const float h = get_height();
232
233         CairoWidget::set_source_rgb_a (cr, get_parent_bg(), 1);
234         cairo_rectangle (cr, 0, 0, w, h);
235         cairo_fill(cr);
236
237         cairo_set_line_width (cr, 2);
238         cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
239
240         cairo_matrix_t matrix;
241         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
242         // we use a 'trick' here: The stoke is off by .5px but filling the interior area
243         // after a stroke of 2px width results in an outline of 1px
244         cairo_stroke_preserve(cr);
245
246         if (_orien == VERT) {
247
248                 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
249                         ds = h - FADER_RESERVE - CORNER_OFFSET;
250                 }
251
252                 if (!CairoWidget::flat_buttons() ) {
253                         cairo_set_source (cr, _pattern);
254                         cairo_matrix_init_translate (&matrix, 0, (h - ds));
255                         cairo_pattern_set_matrix (_pattern, &matrix);
256                 } else {
257                         CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
258                         cairo_fill (cr);
259                         CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
260                         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, ds + CORNER_OFFSET,
261                                         w - CORNER_SIZE, h - ds - CORNER_SIZE, CORNER_RADIUS);
262                 }
263                 cairo_fill (cr);
264
265         } else {
266
267                 if (ds < FADER_RESERVE) {
268                         ds = FADER_RESERVE;
269                 }
270                 assert(ds <= w);
271
272                 /*
273                  * if ds == w, the pattern does not need to be translated
274                  * if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
275                  * w to the left, which is -w in pattern space, and w in user space
276                  * if ds == 10, then the pattern needs to be moved w - 10
277                  * to the left, which is -(w-10) in pattern space, which
278                  * is (w - 10) in user space
279                  * thus: translation = (w - ds)
280                  */
281
282                 if (!CairoWidget::flat_buttons() ) {
283                         cairo_set_source (cr, _pattern);
284                         cairo_matrix_init_translate (&matrix, w - ds, 0);
285                         cairo_pattern_set_matrix (_pattern, &matrix);
286                 } else {
287                         CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
288                         cairo_fill (cr);
289                         CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
290                         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET,
291                                         ds - CORNER_SIZE, h - CORNER_SIZE, CORNER_RADIUS);
292                 }
293                 cairo_fill (cr);
294         }
295
296         /* draw the unity-position line if it's not at either end*/
297         if (!(_tweaks & NoShowUnityLine) && _unity_loc > CORNER_RADIUS) {
298                 cairo_set_line_width(cr, 1);
299                 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
300                 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
301                 cairo_set_source_rgba (cr, c.get_red_p() * 1.5, c.get_green_p() * 1.5, c.get_blue_p() * 1.5, 0.85);
302                 if (_orien == VERT) {
303                         if (_unity_loc < h - CORNER_RADIUS) {
304                                 cairo_move_to (cr, 1.5, _unity_loc + CORNER_OFFSET + .5);
305                                 cairo_line_to (cr, _girth - 1.5, _unity_loc + CORNER_OFFSET + .5);
306                                 cairo_stroke (cr);
307                         }
308                 } else {
309                         if (_unity_loc < w - CORNER_RADIUS) {
310                                 cairo_move_to (cr, _unity_loc - CORNER_OFFSET + .5, 1.5);
311                                 cairo_line_to (cr, _unity_loc - CORNER_OFFSET + .5, _girth - 1.5);
312                                 cairo_stroke (cr);
313                         }
314                 }
315         }
316
317         if (_layout && !_text.empty() && _orien == HORIZ) {
318                 cairo_save (cr);
319                 if (_centered_text) {
320                         /* center text */
321                         cairo_move_to (cr, (w - _text_width)/2.0, h/2.0 - _text_height/2.0);
322                 } else if (ds > .5 * w) {
323                         cairo_move_to (cr, CORNER_OFFSET + 3, h/2.0 - _text_height/2.0);
324                         cairo_set_operator(cr, CAIRO_OPERATOR_XOR);
325                 } else {
326                         cairo_move_to (cr, w - _text_width - CORNER_OFFSET - 3, h/2.0 - _text_height/2.0);
327                 }
328                 CairoWidget::set_source_rgb_a (cr, get_style()->get_text (get_state()), 1);
329                 pango_cairo_show_layout (cr, _layout->gobj());
330                 cairo_restore (cr);
331         }
332
333         if (!get_sensitive()) {
334                 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
335                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
336                 cairo_fill (cr);
337         } else if (_hovering && CairoWidget::widget_prelight()) {
338                 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
339                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
340                 cairo_fill (cr);
341         }
342 }
343
344 void
345 PixFader::on_size_request (GtkRequisition* req)
346 {
347         if (_orien == VERT) {
348                 req->width = (_min_girth ? _min_girth : -1);
349                 req->height = (_min_span ? _min_span : -1);
350         } else {
351                 req->height = (_min_girth ? _min_girth : -1);
352                 req->width = (_min_span ? _min_span : -1);
353         }
354 }
355
356 void
357 PixFader::on_size_allocate (Gtk::Allocation& alloc)
358 {
359         int old_girth = _girth;
360         int old_span = _span;
361
362         CairoWidget::on_size_allocate(alloc);
363
364         if (_orien == VERT) {
365                 _girth = alloc.get_width ();
366                 _span = alloc.get_height ();
367         } else {
368                 _girth = alloc.get_height ();
369                 _span = alloc.get_width ();
370         }
371
372         if (is_realized() && ((old_girth != _girth) || (old_span != _span))) {
373                 /* recreate patterns in case we've changed size */
374                 create_patterns ();
375         }
376
377         update_unity_position ();
378 }
379
380 bool
381 PixFader::on_grab_broken_event (GdkEventGrabBroken* ev)
382 {
383         if (_dragging) {
384                 remove_modal_grab();
385                 _dragging = false;
386                 gdk_pointer_ungrab (GDK_CURRENT_TIME);
387                 StopGesture ();
388         }
389         return (_tweaks & NoButtonForward) ? true : false;
390 }
391
392 bool
393 PixFader::on_button_press_event (GdkEventButton* ev)
394 {
395         if (ev->type != GDK_BUTTON_PRESS) {
396                 if (_dragging) {
397                         remove_modal_grab();
398                         _dragging = false;
399                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
400                         StopGesture ();
401                 }
402                 return (_tweaks & NoButtonForward) ? true : false;
403         }
404
405         if (ev->button != 1 && ev->button != 2) {
406                 return false;
407         }
408
409         add_modal_grab ();
410         StartGesture ();
411         _grab_loc = (_orien == VERT) ? ev->y : ev->x;
412         _grab_start = (_orien == VERT) ? ev->y : ev->x;
413         _grab_window = ev->window;
414         _dragging = true;
415         gdk_pointer_grab(ev->window,false,
416                         GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
417                         NULL,NULL,ev->time);
418
419         if (ev->button == 2) {
420                 set_adjustment_from_event (ev);
421         }
422
423         return (_tweaks & NoButtonForward) ? true : false;
424 }
425
426 bool
427 PixFader::on_button_release_event (GdkEventButton* ev)
428 {
429         double ev_pos = (_orien == VERT) ? ev->y : ev->x;
430
431         switch (ev->button) {
432         case 1:
433                 if (_dragging) {
434                         remove_modal_grab();
435                         _dragging = false;
436                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
437                         StopGesture ();
438
439                         if (!_hovering) {
440                                 if (!(_tweaks & NoVerticalScroll)) {
441                                         Keyboard::magic_widget_drop_focus();
442                                 }
443                                 queue_draw ();
444                         }
445
446                         if (ev_pos == _grab_start) {
447                                 /* no motion - just a click */
448                                 ev_pos = rint(ev_pos);
449
450                                 if (ev->state & Keyboard::TertiaryModifier) {
451                                         _adjustment.set_value (_default_value);
452                                 } else if (ev->state & Keyboard::GainFineScaleModifier) {
453                                         _adjustment.set_value (_adjustment.get_lower());
454 #if 0 // ignore clicks
455                                 } else if (ev_pos == slider_pos) {
456                                         ; // click on current position, no move.
457                                 } else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
458                                         /* above the current display height, remember X Window coords */
459                                         _adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
460                                 } else {
461                                         _adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
462 #endif
463                                 }
464                         }
465                         return true;
466                 }
467                 break;
468
469         case 2:
470                 if (_dragging) {
471                         remove_modal_grab();
472                         _dragging = false;
473                         StopGesture ();
474                         set_adjustment_from_event (ev);
475                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
476                         return true;
477                 }
478                 break;
479
480         default:
481                 break;
482         }
483         return false;
484 }
485
486 bool
487 PixFader::on_scroll_event (GdkEventScroll* ev)
488 {
489         double scale;
490         bool ret = false;
491
492         if (ev->state & Keyboard::GainFineScaleModifier) {
493                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
494                         scale = 0.005;
495                 } else {
496                         scale = 0.1;
497                 }
498         } else {
499                 scale = 1.0;
500         }
501
502         if (_orien == VERT) {
503                 switch (ev->direction) {
504                         case GDK_SCROLL_UP:
505                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
506                                 ret = true;
507                                 break;
508                         case GDK_SCROLL_DOWN:
509                                 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
510                                 ret = true;
511                                 break;
512                         default:
513                                 break;
514                 }
515         } else {
516                 int dir = ev->direction;
517
518                 if (ev->state & Keyboard::ScrollHorizontalModifier || !(_tweaks & NoVerticalScroll)) {
519                         if (ev->direction == GDK_SCROLL_UP) dir = GDK_SCROLL_RIGHT;
520                         if (ev->direction == GDK_SCROLL_DOWN) dir = GDK_SCROLL_LEFT;
521                 }
522
523                 switch (dir) {
524                         case GDK_SCROLL_RIGHT:
525                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
526                                 ret = true;
527                                 break;
528                         case GDK_SCROLL_LEFT:
529                                 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
530                                 ret = true;
531                                 break;
532                         default:
533                                 break;
534                 }
535         }
536         return ret;
537 }
538
539 bool
540 PixFader::on_motion_notify_event (GdkEventMotion* ev)
541 {
542         if (_dragging) {
543                 double scale = 1.0;
544                 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
545
546                 if (ev->window != _grab_window) {
547                         _grab_loc = ev_pos;
548                         _grab_window = ev->window;
549                         return true;
550                 }
551
552                 if (ev->state & Keyboard::GainFineScaleModifier) {
553                         if (ev->state & Keyboard::GainExtraFineScaleModifier) {
554                                 scale = 0.005;
555                         } else {
556                                 scale = 0.1;
557                         }
558                 }
559
560                 double const delta = ev_pos - _grab_loc;
561                 _grab_loc = ev_pos;
562
563                 const double off  = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
564                 const double span = _span - off;
565                 double fract = (delta / span);
566
567                 fract = min (1.0, fract);
568                 fract = max (-1.0, fract);
569
570                 // X Window is top->bottom for 0..Y
571
572                 if (_orien == VERT) {
573                         fract = -fract;
574                 }
575
576                 _adjustment.set_value (_adjustment.get_value() + scale * fract * (_adjustment.get_upper() - _adjustment.get_lower()));
577         }
578
579         return true;
580 }
581
582 void
583 PixFader::adjustment_changed ()
584 {
585         queue_draw ();
586 }
587
588 /** @return pixel offset of the current value from the right or bottom of the fader */
589 int
590 PixFader::display_span ()
591 {
592         float fract = (_adjustment.get_value () - _adjustment.get_lower()) / ((_adjustment.get_upper() - _adjustment.get_lower()));
593         int ds;
594         if (_orien == VERT) {
595                 const double off  = FADER_RESERVE + CORNER_OFFSET;
596                 const double span = _span - off;
597                 ds = (int)rint (span * (1.0 - fract));
598         } else {
599                 const double off  = FADER_RESERVE;
600                 const double span = _span - off;
601                 ds = (int)rint (span * fract + off);
602         }
603
604         return ds;
605 }
606
607 void
608 PixFader::update_unity_position ()
609 {
610         if (_orien == VERT) {
611                 const double span = _span - FADER_RESERVE - CORNER_OFFSET;
612                 _unity_loc = (int) rint (span * (1 - ((_default_value - _adjustment.get_lower()) / (_adjustment.get_upper() - _adjustment.get_lower())))) - 1;
613         } else {
614                 const double span = _span - FADER_RESERVE;
615                 _unity_loc = (int) rint (FADER_RESERVE + (_default_value - _adjustment.get_lower()) * span / (_adjustment.get_upper() - _adjustment.get_lower()));
616         }
617
618         queue_draw ();
619 }
620
621 bool
622 PixFader::on_enter_notify_event (GdkEventCrossing*)
623 {
624         _hovering = true;
625         if (!(_tweaks & NoVerticalScroll)) {
626                 Keyboard::magic_widget_grab_focus ();
627         }
628         queue_draw ();
629         return false;
630 }
631
632 bool
633 PixFader::on_leave_notify_event (GdkEventCrossing*)
634 {
635         if (!_dragging) {
636                 _hovering = false;
637                 if (!(_tweaks & NoVerticalScroll)) {
638                         Keyboard::magic_widget_drop_focus();
639                 }
640                 queue_draw ();
641         }
642         return false;
643 }
644
645 void
646 PixFader::set_adjustment_from_event (GdkEventButton* ev)
647 {
648         const double off  = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
649         const double span = _span - off;
650         double fract = (_orien == VERT) ? (1.0 - ((ev->y - off) / span)) : ((ev->x - off) / span);
651
652         fract = min (1.0, fract);
653         fract = max (0.0, fract);
654
655         _adjustment.set_value (fract * (_adjustment.get_upper () - _adjustment.get_lower ()));
656 }
657
658 void
659 PixFader::set_default_value (float d)
660 {
661         _default_value = d;
662         update_unity_position ();
663 }
664
665 void
666 PixFader::set_tweaks (Tweaks t)
667 {
668         bool need_redraw = false;
669         if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
670                 need_redraw = true;
671         }
672         _tweaks = t;
673         if (need_redraw) {
674                 queue_draw();
675         }
676 }
677
678 void
679 PixFader::set_text (const std::string& str, bool centered, bool expose)
680 {
681         if (_layout && _text == str) {
682                 return;
683         }
684         if (!_layout && !str.empty()) {
685                 _layout = Pango::Layout::create (get_pango_context());
686         }
687
688         _text = str;
689         _centered_text = centered;
690         if (_layout) {
691                 _layout->set_text (str);
692                 _layout->get_pixel_size (_text_width, _text_height);
693                 // queue_resize ();
694                 if (expose) queue_draw ();
695         }
696 }
697
698 void
699 PixFader::on_state_changed (Gtk::StateType old_state)
700 {
701         Widget::on_state_changed (old_state);
702         create_patterns ();
703         queue_draw ();
704 }
705
706 void
707 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
708 {
709         if (_layout) {
710                 std::string txt = _layout->get_text();
711                 _layout.clear (); // drop reference to existing layout
712                 _text = "";
713                 set_text (txt, _centered_text, false);
714         }
715         /* patterns are cached and re-created as needed
716          * during 'expose' in the GUI thread */
717         _pattern = 0;
718         queue_draw ();
719 }
720
721 Gdk::Color
722 PixFader::get_parent_bg ()
723 {
724         Widget* parent = get_parent ();
725
726         while (parent) {
727                 if (parent->get_has_window()) {
728                         break;
729                 }
730                 parent = parent->get_parent();
731         }
732
733         if (parent && parent->get_has_window()) {
734                 if (_current_parent != parent) {
735                         if (_parent_style_change) _parent_style_change.disconnect();
736                         _current_parent = parent;
737                         _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &PixFader::on_style_changed));
738                 }
739                 return parent->get_style ()->get_bg (parent->get_state());
740         }
741
742         return get_style ()->get_bg (get_state());
743 }