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