remove cruft (unused UI::ui_scale)
[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::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t* area)
209 {
210         cairo_t* cr = ctx->cobj();
211
212         if (!_pattern) {
213                 create_patterns();
214         }
215
216         if (!_pattern) {
217                 /* this isn't supposed to be happen, but some wackiness whereby
218                  * the pixfader ends up with a 1xN or Nx1 size allocation
219                  * leads to it. the basic wackiness needs fixing but we
220                  * shouldn't crash. just fill in the expose area with
221                  * our bg color.
222                  */
223
224                 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
225                 cairo_rectangle (cr, area->x, area->y, area->width, area->height);
226                 cairo_fill (cr);
227                 return;
228         }
229
230         OnExpose();
231         int ds = display_span ();
232         const float w = get_width();
233         const float h = get_height();
234
235         CairoWidget::set_source_rgb_a (cr, get_parent_bg(), 1);
236         cairo_rectangle (cr, 0, 0, w, h);
237         cairo_fill(cr);
238
239         cairo_set_line_width (cr, 2);
240         cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
241
242         cairo_matrix_t matrix;
243         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
244         // we use a 'trick' here: The stoke is off by .5px but filling the interior area
245         // after a stroke of 2px width results in an outline of 1px
246         cairo_stroke_preserve(cr);
247
248         if (_orien == VERT) {
249
250                 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
251                         ds = h - FADER_RESERVE - CORNER_OFFSET;
252                 }
253
254                 if (!CairoWidget::flat_buttons() ) {
255                         cairo_set_source (cr, _pattern);
256                         cairo_matrix_init_translate (&matrix, 0, (h - ds));
257                         cairo_pattern_set_matrix (_pattern, &matrix);
258                 } else {
259                         CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
260                         cairo_fill (cr);
261                         CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
262                         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, ds + CORNER_OFFSET,
263                                         w - CORNER_SIZE, h - ds - CORNER_SIZE, CORNER_RADIUS);
264                 }
265                 cairo_fill (cr);
266
267         } else {
268
269                 if (ds < FADER_RESERVE) {
270                         ds = FADER_RESERVE;
271                 }
272                 assert(ds <= w);
273
274                 /*
275                  * if ds == w, the pattern does not need to be translated
276                  * if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
277                  * w to the left, which is -w in pattern space, and w in user space
278                  * if ds == 10, then the pattern needs to be moved w - 10
279                  * to the left, which is -(w-10) in pattern space, which
280                  * is (w - 10) in user space
281                  * thus: translation = (w - ds)
282                  */
283
284                 if (!CairoWidget::flat_buttons() ) {
285                         cairo_set_source (cr, _pattern);
286                         cairo_matrix_init_translate (&matrix, w - ds, 0);
287                         cairo_pattern_set_matrix (_pattern, &matrix);
288                 } else {
289                         CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
290                         cairo_fill (cr);
291                         CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
292                         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET,
293                                         ds - CORNER_SIZE, h - CORNER_SIZE, CORNER_RADIUS);
294                 }
295                 cairo_fill (cr);
296         }
297
298         /* draw the unity-position line if it's not at either end*/
299         if (!(_tweaks & NoShowUnityLine) && _unity_loc > CORNER_RADIUS) {
300                 cairo_set_line_width(cr, 1);
301                 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
302                 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
303                 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);
304                 if (_orien == VERT) {
305                         if (_unity_loc < h - CORNER_RADIUS) {
306                                 cairo_move_to (cr, 1.5, _unity_loc + CORNER_OFFSET + .5);
307                                 cairo_line_to (cr, _girth - 1.5, _unity_loc + CORNER_OFFSET + .5);
308                                 cairo_stroke (cr);
309                         }
310                 } else {
311                         if (_unity_loc < w - CORNER_RADIUS) {
312                                 cairo_move_to (cr, _unity_loc - CORNER_OFFSET + .5, 1.5);
313                                 cairo_line_to (cr, _unity_loc - CORNER_OFFSET + .5, _girth - 1.5);
314                                 cairo_stroke (cr);
315                         }
316                 }
317         }
318
319         if (_layout && !_text.empty() && _orien == HORIZ) {
320                 cairo_save (cr);
321                 if (_centered_text) {
322                         /* center text */
323                         cairo_move_to (cr, (w - _text_width)/2.0, h/2.0 - _text_height/2.0);
324                 } else if (ds > .5 * w) {
325                         cairo_move_to (cr, CORNER_OFFSET + 3, h/2.0 - _text_height/2.0);
326                         cairo_set_operator(cr, CAIRO_OPERATOR_XOR);
327                 } else {
328                         cairo_move_to (cr, w - _text_width - CORNER_OFFSET - 3, h/2.0 - _text_height/2.0);
329                 }
330                 CairoWidget::set_source_rgb_a (cr, get_style()->get_text (get_state()), 1);
331                 pango_cairo_show_layout (cr, _layout->gobj());
332                 cairo_restore (cr);
333         }
334
335         if (!get_sensitive()) {
336                 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
337                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
338                 cairo_fill (cr);
339         } else if (_hovering && CairoWidget::widget_prelight()) {
340                 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
341                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
342                 cairo_fill (cr);
343         }
344 }
345
346 void
347 PixFader::on_size_request (GtkRequisition* req)
348 {
349         if (_orien == VERT) {
350                 req->width = (_min_girth ? _min_girth : -1);
351                 req->height = (_min_span ? _min_span : -1);
352         } else {
353                 req->height = (_min_girth ? _min_girth : -1);
354                 req->width = (_min_span ? _min_span : -1);
355         }
356 }
357
358 void
359 PixFader::on_size_allocate (Gtk::Allocation& alloc)
360 {
361         int old_girth = _girth;
362         int old_span = _span;
363
364         CairoWidget::on_size_allocate(alloc);
365
366         if (_orien == VERT) {
367                 _girth = alloc.get_width ();
368                 _span = alloc.get_height ();
369         } else {
370                 _girth = alloc.get_height ();
371                 _span = alloc.get_width ();
372         }
373
374         if (is_realized() && ((old_girth != _girth) || (old_span != _span))) {
375                 /* recreate patterns in case we've changed size */
376                 create_patterns ();
377         }
378
379         update_unity_position ();
380 }
381
382 bool
383 PixFader::on_grab_broken_event (GdkEventGrabBroken* ev)
384 {
385         if (_dragging) {
386                 remove_modal_grab();
387                 _dragging = false;
388                 gdk_pointer_ungrab (GDK_CURRENT_TIME);
389                 StopGesture ();
390         }
391         return (_tweaks & NoButtonForward) ? true : false;
392 }
393
394 bool
395 PixFader::on_button_press_event (GdkEventButton* ev)
396 {
397         if (ev->type != GDK_BUTTON_PRESS) {
398                 if (_dragging) {
399                         remove_modal_grab();
400                         _dragging = false;
401                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
402                         StopGesture ();
403                 }
404                 return (_tweaks & NoButtonForward) ? true : false;
405         }
406
407         if (ev->button != 1 && ev->button != 2) {
408                 return false;
409         }
410
411         add_modal_grab ();
412         StartGesture ();
413         _grab_loc = (_orien == VERT) ? ev->y : ev->x;
414         _grab_start = (_orien == VERT) ? ev->y : ev->x;
415         _grab_window = ev->window;
416         _dragging = true;
417         gdk_pointer_grab(ev->window,false,
418                         GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
419                         NULL,NULL,ev->time);
420
421         if (ev->button == 2) {
422                 set_adjustment_from_event (ev);
423         }
424
425         return (_tweaks & NoButtonForward) ? true : false;
426 }
427
428 bool
429 PixFader::on_button_release_event (GdkEventButton* ev)
430 {
431         double ev_pos = (_orien == VERT) ? ev->y : ev->x;
432
433         switch (ev->button) {
434         case 1:
435                 if (_dragging) {
436                         remove_modal_grab();
437                         _dragging = false;
438                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
439                         StopGesture ();
440
441                         if (!_hovering) {
442                                 if (!(_tweaks & NoVerticalScroll)) {
443                                         Keyboard::magic_widget_drop_focus();
444                                 }
445                                 queue_draw ();
446                         }
447
448                         if (ev_pos == _grab_start) {
449                                 /* no motion - just a click */
450                                 ev_pos = rint(ev_pos);
451
452                                 if (ev->state & Keyboard::TertiaryModifier) {
453                                         _adjustment.set_value (_default_value);
454                                 } else if (ev->state & Keyboard::GainFineScaleModifier) {
455                                         _adjustment.set_value (_adjustment.get_lower());
456 #if 0 // ignore clicks
457                                 } else if (ev_pos == slider_pos) {
458                                         ; // click on current position, no move.
459                                 } else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
460                                         /* above the current display height, remember X Window coords */
461                                         _adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
462                                 } else {
463                                         _adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
464 #endif
465                                 }
466                         }
467                         return true;
468                 }
469                 break;
470
471         case 2:
472                 if (_dragging) {
473                         remove_modal_grab();
474                         _dragging = false;
475                         StopGesture ();
476                         set_adjustment_from_event (ev);
477                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
478                         return true;
479                 }
480                 break;
481
482         default:
483                 break;
484         }
485         return false;
486 }
487
488 bool
489 PixFader::on_scroll_event (GdkEventScroll* ev)
490 {
491         double scale;
492         bool ret = false;
493
494         if (ev->state & Keyboard::GainFineScaleModifier) {
495                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
496                         scale = 0.005;
497                 } else {
498                         scale = 0.1;
499                 }
500         } else {
501                 scale = 1.0;
502         }
503
504         if (_orien == VERT) {
505                 switch (ev->direction) {
506                         case GDK_SCROLL_UP:
507                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
508                                 ret = true;
509                                 break;
510                         case GDK_SCROLL_DOWN:
511                                 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
512                                 ret = true;
513                                 break;
514                         default:
515                                 break;
516                 }
517         } else {
518                 int dir = ev->direction;
519
520                 if (ev->state & Keyboard::ScrollHorizontalModifier || !(_tweaks & NoVerticalScroll)) {
521                         if (ev->direction == GDK_SCROLL_UP) dir = GDK_SCROLL_RIGHT;
522                         if (ev->direction == GDK_SCROLL_DOWN) dir = GDK_SCROLL_LEFT;
523                 }
524
525                 switch (dir) {
526                         case GDK_SCROLL_RIGHT:
527                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
528                                 ret = true;
529                                 break;
530                         case GDK_SCROLL_LEFT:
531                                 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
532                                 ret = true;
533                                 break;
534                         default:
535                                 break;
536                 }
537         }
538         return ret;
539 }
540
541 bool
542 PixFader::on_motion_notify_event (GdkEventMotion* ev)
543 {
544         if (_dragging) {
545                 double scale = 1.0;
546                 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
547
548                 if (ev->window != _grab_window) {
549                         _grab_loc = ev_pos;
550                         _grab_window = ev->window;
551                         return true;
552                 }
553
554                 if (ev->state & Keyboard::GainFineScaleModifier) {
555                         if (ev->state & Keyboard::GainExtraFineScaleModifier) {
556                                 scale = 0.005;
557                         } else {
558                                 scale = 0.1;
559                         }
560                 }
561
562                 double const delta = ev_pos - _grab_loc;
563                 _grab_loc = ev_pos;
564
565                 const double off  = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
566                 const double span = _span - off;
567                 double fract = (delta / span);
568
569                 fract = min (1.0, fract);
570                 fract = max (-1.0, fract);
571
572                 // X Window is top->bottom for 0..Y
573
574                 if (_orien == VERT) {
575                         fract = -fract;
576                 }
577
578                 _adjustment.set_value (_adjustment.get_value() + scale * fract * (_adjustment.get_upper() - _adjustment.get_lower()));
579         }
580
581         return true;
582 }
583
584 void
585 PixFader::adjustment_changed ()
586 {
587         queue_draw ();
588 }
589
590 /** @return pixel offset of the current value from the right or bottom of the fader */
591 int
592 PixFader::display_span ()
593 {
594         float fract = (_adjustment.get_value () - _adjustment.get_lower()) / ((_adjustment.get_upper() - _adjustment.get_lower()));
595         int ds;
596         if (_orien == VERT) {
597                 const double off  = FADER_RESERVE + CORNER_OFFSET;
598                 const double span = _span - off;
599                 ds = (int)rint (span * (1.0 - fract));
600         } else {
601                 const double off  = FADER_RESERVE;
602                 const double span = _span - off;
603                 ds = (int)rint (span * fract + off);
604         }
605
606         return ds;
607 }
608
609 void
610 PixFader::update_unity_position ()
611 {
612         if (_orien == VERT) {
613                 const double span = _span - FADER_RESERVE - CORNER_OFFSET;
614                 _unity_loc = (int) rint (span * (1 - ((_default_value - _adjustment.get_lower()) / (_adjustment.get_upper() - _adjustment.get_lower())))) - 1;
615         } else {
616                 const double span = _span - FADER_RESERVE;
617                 _unity_loc = (int) rint (FADER_RESERVE + (_default_value - _adjustment.get_lower()) * span / (_adjustment.get_upper() - _adjustment.get_lower()));
618         }
619
620         queue_draw ();
621 }
622
623 bool
624 PixFader::on_enter_notify_event (GdkEventCrossing*)
625 {
626         _hovering = true;
627         if (!(_tweaks & NoVerticalScroll)) {
628                 Keyboard::magic_widget_grab_focus ();
629         }
630         queue_draw ();
631         return false;
632 }
633
634 bool
635 PixFader::on_leave_notify_event (GdkEventCrossing*)
636 {
637         if (!_dragging) {
638                 _hovering = false;
639                 if (!(_tweaks & NoVerticalScroll)) {
640                         Keyboard::magic_widget_drop_focus();
641                 }
642                 queue_draw ();
643         }
644         return false;
645 }
646
647 void
648 PixFader::set_adjustment_from_event (GdkEventButton* ev)
649 {
650         const double off  = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
651         const double span = _span - off;
652         double fract = (_orien == VERT) ? (1.0 - ((ev->y - off) / span)) : ((ev->x - off) / span);
653
654         fract = min (1.0, fract);
655         fract = max (0.0, fract);
656
657         _adjustment.set_value (fract * (_adjustment.get_upper () - _adjustment.get_lower ()));
658 }
659
660 void
661 PixFader::set_default_value (float d)
662 {
663         _default_value = d;
664         update_unity_position ();
665 }
666
667 void
668 PixFader::set_tweaks (Tweaks t)
669 {
670         bool need_redraw = false;
671         if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
672                 need_redraw = true;
673         }
674         _tweaks = t;
675         if (need_redraw) {
676                 queue_draw();
677         }
678 }
679
680 void
681 PixFader::set_text (const std::string& str, bool centered, bool expose)
682 {
683         if (_layout && _text == str) {
684                 return;
685         }
686         if (!_layout && !str.empty()) {
687                 _layout = Pango::Layout::create (get_pango_context());
688         }
689
690         _text = str;
691         _centered_text = centered;
692         if (_layout) {
693                 _layout->set_text (str);
694                 _layout->get_pixel_size (_text_width, _text_height);
695                 // queue_resize ();
696                 if (expose) queue_draw ();
697         }
698 }
699
700 void
701 PixFader::on_state_changed (Gtk::StateType old_state)
702 {
703         Widget::on_state_changed (old_state);
704         create_patterns ();
705         queue_draw ();
706 }
707
708 void
709 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
710 {
711         if (_layout) {
712                 std::string txt = _layout->get_text();
713                 _layout.clear (); // drop reference to existing layout
714                 _text = "";
715                 set_text (txt, _centered_text, false);
716         }
717         /* patterns are cached and re-created as needed
718          * during 'expose' in the GUI thread */
719         _pattern = 0;
720         queue_draw ();
721 }
722
723 Gdk::Color
724 PixFader::get_parent_bg ()
725 {
726         Widget* parent = get_parent ();
727
728         while (parent) {
729                 if (parent->get_has_window()) {
730                         break;
731                 }
732                 parent = parent->get_parent();
733         }
734
735         if (parent && parent->get_has_window()) {
736                 if (_current_parent != parent) {
737                         if (_parent_style_change) _parent_style_change.disconnect();
738                         _current_parent = parent;
739                         _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &PixFader::on_style_changed));
740                 }
741                 return parent->get_style ()->get_bg (parent->get_state());
742         }
743
744         return get_style ()->get_bg (get_state());
745 }