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