f62e703d73e55e73cd2f4dce70cc85d24c6d24dd
[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                                 } else if (ev_pos == slider_pos) {
435                                         ; // click on current position, no move.
436                                 } else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
437                                         /* above the current display height, remember X Window coords */
438                                         _adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
439                                 } else {
440                                         _adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
441                                 }
442                         }
443                         return true;
444                 }
445                 break;
446
447         case 2:
448                 if (_dragging) {
449                         remove_modal_grab();
450                         _dragging = false;
451                         StopGesture ();
452                         set_adjustment_from_event (ev);
453                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
454                         return true;
455                 }
456                 break;
457
458         default:
459                 break;
460         }
461         return false;
462 }
463
464 bool
465 PixFader::on_scroll_event (GdkEventScroll* ev)
466 {
467         double scale;
468         bool ret = false;
469
470         if (ev->state & Keyboard::GainFineScaleModifier) {
471                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
472                         scale = 0.005;
473                 } else {
474                         scale = 0.05;
475                 }
476         } else {
477                 scale = 0.25;
478         }
479
480         if (_orien == VERT) {
481                 switch (ev->direction) {
482                         case GDK_SCROLL_UP:
483                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
484                                 ret = true;
485                                 break;
486                         case GDK_SCROLL_DOWN:
487                                 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
488                                 ret = true;
489                                 break;
490                         default:
491                                 break;
492                 }
493         } else {
494                 int dir = ev->direction;
495
496                 if (ev->state & Keyboard::ScrollHorizontalModifier || !(_tweaks & NoVerticalScroll)) {
497                         if (ev->direction == GDK_SCROLL_UP) dir = GDK_SCROLL_RIGHT;
498                         if (ev->direction == GDK_SCROLL_DOWN) dir = GDK_SCROLL_LEFT;
499                 }
500
501                 switch (dir) {
502                         case GDK_SCROLL_RIGHT:
503                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
504                                 ret = true;
505                                 break;
506                         case GDK_SCROLL_LEFT:
507                                 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
508                                 ret = true;
509                                 break;
510                         default:
511                                 break;
512                 }
513         }
514         return ret;
515 }
516
517 bool
518 PixFader::on_motion_notify_event (GdkEventMotion* ev)
519 {
520         if (_dragging) {
521                 double scale = 1.0;
522                 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
523
524                 if (ev->window != _grab_window) {
525                         _grab_loc = ev_pos;
526                         _grab_window = ev->window;
527                         return true;
528                 }
529
530                 if (ev->state & Keyboard::GainFineScaleModifier) {
531                         if (ev->state & Keyboard::GainExtraFineScaleModifier) {
532                                 scale = 0.05;
533                         } else {
534                                 scale = 0.1;
535                         }
536                 }
537
538                 double const delta = ev_pos - _grab_loc;
539                 _grab_loc = ev_pos;
540
541                 double fract = (delta / _span);
542
543                 fract = min (1.0, fract);
544                 fract = max (-1.0, fract);
545
546                 // X Window is top->bottom for 0..Y
547
548                 if (_orien == VERT) {
549                         fract = -fract;
550                 }
551
552                 _adjustment.set_value (_adjustment.get_value() + scale * fract * (_adjustment.get_upper() - _adjustment.get_lower()));
553         }
554
555         return true;
556 }
557
558 void
559 PixFader::adjustment_changed ()
560 {
561         if (display_span() != _last_drawn) {
562                 queue_draw ();
563         }
564 }
565
566 /** @return pixel offset of the current value from the right or bottom of the fader */
567 int
568 PixFader::display_span ()
569 {
570         float fract = (_adjustment.get_value () - _adjustment.get_lower()) / ((_adjustment.get_upper() - _adjustment.get_lower()));
571         int ds;
572         if (_orien == VERT) {
573                 ds = (int)rint (_span * (1.0 - fract));
574         } else {
575                 ds = (int)rint (_span * fract);
576         }
577
578         return ds;
579 }
580
581 void
582 PixFader::update_unity_position ()
583 {
584         if (_orien == VERT) {
585                 _unity_loc = (int) rint (_span * (1 - ((_default_value - _adjustment.get_lower()) / (_adjustment.get_upper() - _adjustment.get_lower())))) - 1;
586         } else {
587                 _unity_loc = (int) rint ((_default_value - _adjustment.get_lower()) * _span / (_adjustment.get_upper() - _adjustment.get_lower()));
588         }
589
590         queue_draw ();
591 }
592
593 bool
594 PixFader::on_enter_notify_event (GdkEventCrossing*)
595 {
596         _hovering = true;
597         if (!(_tweaks & NoVerticalScroll)) {
598                 Keyboard::magic_widget_grab_focus ();
599         }
600         queue_draw ();
601         return false;
602 }
603
604 bool
605 PixFader::on_leave_notify_event (GdkEventCrossing*)
606 {
607         if (!_dragging) {
608                 _hovering = false;
609                 if (!(_tweaks & NoVerticalScroll)) {
610                         Keyboard::magic_widget_drop_focus();
611                 }
612                 queue_draw ();
613         }
614         return false;
615 }
616
617 void
618 PixFader::set_adjustment_from_event (GdkEventButton* ev)
619 {
620         double fract = (_orien == VERT) ? (1.0 - (ev->y / _span)) : (ev->x / _span);
621
622         fract = min (1.0, fract);
623         fract = max (0.0, fract);
624
625         _adjustment.set_value (fract * (_adjustment.get_upper () - _adjustment.get_lower ()));
626 }
627
628 void
629 PixFader::set_default_value (float d)
630 {
631         _default_value = d;
632         update_unity_position ();
633 }
634
635 void
636 PixFader::set_tweaks (Tweaks t)
637 {
638         bool need_redraw = false;
639         if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
640                 need_redraw = true;
641         }
642         _tweaks = t;
643         if (need_redraw) {
644                 queue_draw();
645         }
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 }