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