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