unify BarController with Slider/Pixfader.
[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 5
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         , _orien (orientation)
48         , _pattern (0)
49         , _hovering (false)
50         , _last_drawn (-1)
51         , _dragging (false)
52         , _centered_text (true)
53         , _current_parent (0)
54 {
55         _default_value = adjustment.get_value();
56         update_unity_position ();
57
58         add_events (
59                           Gdk::BUTTON_PRESS_MASK
60                         | Gdk::BUTTON_RELEASE_MASK
61                         | Gdk::POINTER_MOTION_MASK
62                         | Gdk::SCROLL_MASK
63                         | Gdk::ENTER_NOTIFY_MASK
64                         | Gdk::LEAVE_NOTIFY_MASK
65                         );
66
67         adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
68         adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
69
70         if (_orien == VERT) {
71                 DrawingArea::set_size_request(_girth, _span);
72         } else {
73                 DrawingArea::set_size_request(_span, _girth);
74         }
75 }
76
77 PixFader::~PixFader ()
78 {
79         if (_parent_style_change) _parent_style_change.disconnect();
80         if (_layout) _layout.clear (); // drop reference to existing layout
81 }
82
83 cairo_pattern_t*
84 PixFader::find_pattern (double afr, double afg, double afb,
85                         double abr, double abg, double abb,
86                         int w, int h)
87 {
88         for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
89                 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
90                         return (*f)->pattern;
91                 }
92         }
93         return 0;
94 }
95
96 void
97 PixFader::create_patterns ()
98 {
99         Gdk::Color c = get_style()->get_fg (get_state());
100         float fr, fg, fb;
101         float br, bg, bb;
102
103         fr = c.get_red_p ();
104         fg = c.get_green_p ();
105         fb = c.get_blue_p ();
106
107         c = get_style()->get_bg (get_state());
108
109         br = c.get_red_p ();
110         bg = c.get_green_p ();
111         bb = c.get_blue_p ();
112
113         cairo_surface_t* surface;
114         cairo_t* tc = 0;
115
116         if (get_width() <= 1 || get_height() <= 1) {
117                 return;
118         }
119
120         if ((_pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
121                 /* found it - use it */
122                 return;
123         }
124
125         if (_orien == VERT) {
126
127                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
128                 tc = cairo_create (surface);
129
130                 /* paint background + border */
131
132                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
133                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
134                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
135                 cairo_set_source (tc, shade_pattern);
136                 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
137                 cairo_fill (tc);
138
139                 cairo_pattern_destroy (shade_pattern);
140
141                 /* paint lower shade */
142
143                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width() - 2 - CORNER_OFFSET , 0);
144                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
145                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
146                 cairo_set_source (tc, shade_pattern);
147                 Gtkmm2ext::rounded_top_half_rectangle (tc, CORNER_OFFSET, get_height() + CORNER_OFFSET,
148                                 get_width() - CORNER_SIZE, get_height(), CORNER_RADIUS);
149                 cairo_fill (tc);
150
151                 cairo_pattern_destroy (shade_pattern);
152
153                 _pattern = cairo_pattern_create_for_surface (surface);
154
155         } else {
156
157                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
158                 tc = cairo_create (surface);
159
160                 /* paint right shade (background section)*/
161
162                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
163                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
164                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
165                 cairo_set_source (tc, shade_pattern);
166                 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
167                 cairo_fill (tc);
168
169                 /* paint left shade (active section/foreground) */
170
171                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
172                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
173                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
174                 cairo_set_source (tc, shade_pattern);
175                 Gtkmm2ext::rounded_right_half_rectangle (tc, CORNER_OFFSET, CORNER_OFFSET,
176                                 get_width() - CORNER_OFFSET, get_height() - CORNER_SIZE, CORNER_RADIUS);
177                 cairo_fill (tc);
178                 cairo_pattern_destroy (shade_pattern);
179
180                 _pattern = cairo_pattern_create_for_surface (surface);
181         }
182
183         /* cache it for others to use */
184
185         _patterns.push_back (new FaderImage (_pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
186
187         cairo_destroy (tc);
188         cairo_surface_destroy (surface);
189 }
190
191 bool
192 PixFader::on_expose_event (GdkEventExpose* ev)
193 {
194         Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
195         cairo_t* cr = context->cobj();
196
197         // clip to expose area
198         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
199         cairo_clip (cr);
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, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
215                 cairo_fill (cr);
216                 return true;
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, 1);
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 interiour area
234         // results in an outline of 0.5 px the actual outline is exatly 1px at 50% alpha.
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_OFFSET, 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 (_unity_loc > 0) {
289                 context->set_line_width (1);
290                 context->set_line_cap (Cairo::LINE_CAP_ROUND);
291                 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
292                 context->set_source_rgba (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 ) {
295                                 context->move_to (1.5, _unity_loc + CORNER_OFFSET + .5);
296                                 context->line_to (_girth - 1.5, _unity_loc + CORNER_OFFSET + .5);
297                                 context->stroke ();
298                         }
299                 } else {
300                         if ( _unity_loc < w ){
301                                 context->move_to (_unity_loc - CORNER_OFFSET + .5, 1.5);
302                                 context->line_to (_unity_loc - CORNER_OFFSET + .5, _girth - 1.5);
303                                 context->stroke ();
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) {
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         _last_drawn = ds;
335
336         return true;
337 }
338
339 void
340 PixFader::on_size_request (GtkRequisition* req)
341 {
342         if (_orien == VERT) {
343                 req->width = (_girth ? _girth : -1);
344                 req->height = (_span ? _span : -1);
345         } else {
346                 req->height = (_girth ? _girth : -1);
347                 req->width = (_span ? _span : -1);
348         }
349 }
350
351 void
352 PixFader::on_size_allocate (Gtk::Allocation& alloc)
353 {
354         DrawingArea::on_size_allocate(alloc);
355
356         if (_orien == VERT) {
357                 _girth = alloc.get_width ();
358                 _span = alloc.get_height ();
359         } else {
360                 _girth = alloc.get_height ();
361                 _span = alloc.get_width ();
362         }
363
364         if (is_realized()) {
365                 /* recreate patterns in case we've changed size */
366                 create_patterns ();
367         }
368
369         update_unity_position ();
370 }
371
372 bool
373 PixFader::on_button_press_event (GdkEventButton* ev)
374 {
375         if (ev->type != GDK_BUTTON_PRESS) {
376                 if (_dragging) {
377                         remove_modal_grab();
378                         _dragging = false;
379                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
380                         StopGesture ();
381                 }
382                 return false;
383         }
384
385         if (ev->button != 1 && ev->button != 2) {
386                 return false;
387         }
388
389         add_modal_grab ();
390         StartGesture ();
391         _grab_loc = (_orien == VERT) ? ev->y : ev->x;
392         _grab_start = (_orien == VERT) ? ev->y : ev->x;
393         _grab_window = ev->window;
394         _dragging = true;
395         gdk_pointer_grab(ev->window,false,
396                         GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
397                         NULL,NULL,ev->time);
398
399         if (ev->button == 2) {
400                 set_adjustment_from_event (ev);
401         }
402
403         return true;
404 }
405
406 bool
407 PixFader::on_button_release_event (GdkEventButton* ev)
408 {
409         double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
410
411         switch (ev->button) {
412         case 1:
413                 if (_dragging) {
414                         remove_modal_grab();
415                         _dragging = false;
416                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
417                         StopGesture ();
418
419                         if (!_hovering) {
420                                 Keyboard::magic_widget_drop_focus();
421                                 queue_draw ();
422                         }
423
424                         if (ev_pos == _grab_start) {
425
426                                 /* no motion - just a click */
427
428                                 if (ev->state & Keyboard::TertiaryModifier) {
429                                         adjustment.set_value (_default_value);
430                                 } else if (ev->state & Keyboard::GainFineScaleModifier) {
431                                         adjustment.set_value (adjustment.get_lower());
432                                 } else if (ev_pos == display_span()) {
433                                         ; // click on current position, no move.
434                                 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
435                                         /* above the current display height, remember X Window coords */
436                                         adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
437                                 } else {
438                                         adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
439                                 }
440                         }
441                         return true;
442                 }
443                 break;
444
445         case 2:
446                 if (_dragging) {
447                         remove_modal_grab();
448                         _dragging = false;
449                         StopGesture ();
450                         set_adjustment_from_event (ev);
451                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
452                         return true;
453                 }
454                 break;
455
456         default:
457                 break;
458         }
459         return false;
460 }
461
462 bool
463 PixFader::on_scroll_event (GdkEventScroll* ev)
464 {
465         double scale;
466         bool ret = false;
467
468         if (ev->state & Keyboard::GainFineScaleModifier) {
469                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
470                         scale = 0.01;
471                 } else {
472                         scale = 0.05;
473                 }
474         } else {
475                 scale = 0.25;
476         }
477
478         if (_orien == VERT) {
479
480                 /* should left/right scroll affect vertical faders ? */
481
482                 switch (ev->direction) {
483
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
497                 /* up/down scrolls should definitely affect horizontal faders
498                    because they are so much easier to use
499                 */
500
501                 switch (ev->direction) {
502
503                 case GDK_SCROLL_RIGHT:
504                 case GDK_SCROLL_UP:
505                         adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
506                         ret = true;
507                         break;
508                 case GDK_SCROLL_LEFT:
509                 case GDK_SCROLL_DOWN:
510                         adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
511                         ret = true;
512                         break;
513                 default:
514                         break;
515                 }
516         }
517         return ret;
518 }
519
520 bool
521 PixFader::on_motion_notify_event (GdkEventMotion* ev)
522 {
523         if (_dragging) {
524                 double scale = 1.0;
525                 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
526
527                 if (ev->window != _grab_window) {
528                         _grab_loc = ev_pos;
529                         _grab_window = ev->window;
530                         return true;
531                 }
532
533                 if (ev->state & Keyboard::GainFineScaleModifier) {
534                         if (ev->state & Keyboard::GainExtraFineScaleModifier) {
535                                 scale = 0.05;
536                         } else {
537                                 scale = 0.1;
538                         }
539                 }
540
541                 double const delta = ev_pos - _grab_loc;
542                 _grab_loc = ev_pos;
543
544                 double fract = (delta / _span);
545
546                 fract = min (1.0, fract);
547                 fract = max (-1.0, fract);
548
549                 // X Window is top->bottom for 0..Y
550
551                 if (_orien == VERT) {
552                         fract = -fract;
553                 }
554
555                 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
556         }
557
558         return true;
559 }
560
561 void
562 PixFader::adjustment_changed ()
563 {
564         if (display_span() != _last_drawn) {
565                 queue_draw ();
566         }
567 }
568
569 /** @return pixel offset of the current value from the right or bottom of the fader */
570 int
571 PixFader::display_span ()
572 {
573         float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
574         int ds;
575         if (_orien == VERT) {
576                 ds = (int)floor (_span * (1.0 - fract));
577         } else {
578                 ds = (int)floor (_span * fract);
579         }
580
581         return ds;
582 }
583
584 void
585 PixFader::update_unity_position ()
586 {
587         if (_orien == VERT) {
588                 _unity_loc = (int) rint (_span * (1 - ((_default_value - adjustment.get_lower()) / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
589         } else {
590                 _unity_loc = (int) rint ((_default_value - adjustment.get_lower()) * _span / (adjustment.get_upper() - adjustment.get_lower()));
591         }
592
593         queue_draw ();
594 }
595
596 bool
597 PixFader::on_enter_notify_event (GdkEventCrossing*)
598 {
599         _hovering = true;
600         Keyboard::magic_widget_grab_focus ();
601         queue_draw ();
602         return false;
603 }
604
605 bool
606 PixFader::on_leave_notify_event (GdkEventCrossing*)
607 {
608         if (!_dragging) {
609                 _hovering = false;
610                 Keyboard::magic_widget_drop_focus();
611                 queue_draw ();
612         }
613         return false;
614 }
615
616 void
617 PixFader::set_adjustment_from_event (GdkEventButton* ev)
618 {
619         double fract = (_orien == VERT) ? (1.0 - (ev->y / _span)) : (ev->x / _span);
620
621         fract = min (1.0, fract);
622         fract = max (0.0, fract);
623
624         adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
625 }
626
627 void
628 PixFader::set_default_value (float d)
629 {
630         _default_value = d;
631         update_unity_position ();
632 }
633
634 void
635 PixFader::set_text (const std::string& str, bool centered)
636 {
637         if (_layout && _text == str) {
638                 return;
639         }
640         if (!_layout && !str.empty()) {
641                 _layout = Pango::Layout::create (get_pango_context());
642         }
643
644         _text = str;
645         _centered_text = centered;
646         if (_layout) {
647                 _layout->set_text (str);
648                 _layout->get_pixel_size (_text_width, _text_height);
649                 queue_resize ();
650         }
651 }
652
653 void
654 PixFader::on_state_changed (Gtk::StateType old_state)
655 {
656         Widget::on_state_changed (old_state);
657         create_patterns ();
658         queue_draw ();
659 }
660
661 void
662 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
663 {
664         if (_layout) {
665                 std::string txt = _layout->get_text();
666                 _layout.clear (); // drop reference to existing layout
667                 _text = "";
668                 set_text (txt, _centered_text);
669         }
670         /* patterns are cached and re-created as needed 
671          * during 'expose' in the GUI thread */
672         _pattern = 0;
673         queue_draw ();
674 }
675
676 Gdk::Color
677 PixFader::get_parent_bg ()
678 {
679         Widget* parent = get_parent ();
680
681         while (parent) {
682                 if (parent->get_has_window()) {
683                         break;
684                 }
685                 parent = parent->get_parent();
686         }
687
688         if (parent && parent->get_has_window()) {
689                 if (_current_parent != parent) {
690                         if (_parent_style_change) _parent_style_change.disconnect();
691                         _current_parent = parent;
692                         _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &PixFader::on_style_changed));
693                 }
694                 return parent->get_style ()->get_bg (parent->get_state());
695         }
696
697         return get_style ()->get_bg (get_state());
698 }