fix jack transport return value
[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                 DrawingArea::set_size_request(_girth, _span);
78         } else {
79                 DrawingArea::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 bool
200 PixFader::on_expose_event (GdkEventExpose* ev)
201 {
202         Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
203         cairo_t* cr = context->cobj();
204
205         // clip to expose area
206         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
207         cairo_clip (cr);
208
209         if (!_pattern) {
210                 create_patterns();
211         }
212
213         if (!_pattern) {
214                 /* this isn't supposed to be happen, but some wackiness whereby
215                  * the pixfader ends up with a 1xN or Nx1 size allocation
216                  * leads to it. the basic wackiness needs fixing but we
217                  * shouldn't crash. just fill in the expose area with
218                  * our bg color.
219                  */
220
221                 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
222                 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
223                 cairo_fill (cr);
224                 return true;
225         }
226
227         OnExpose();
228         int ds = display_span ();
229         const float w = get_width();
230         const float h = get_height();
231
232         CairoWidget::set_source_rgb_a (cr, get_parent_bg(), 1);
233         cairo_rectangle (cr, 0, 0, w, h);
234         cairo_fill(cr);
235
236         cairo_set_line_width (cr, 2);
237         cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
238
239         cairo_matrix_t matrix;
240         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
241         // we use a 'trick' here: The stoke is off by .5px but filling the interior area
242         // after a stroke of 2px width results in an outline of 1px
243         cairo_stroke_preserve(cr);
244
245         if (_orien == VERT) {
246
247                 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
248                         ds = h - FADER_RESERVE - CORNER_OFFSET;
249                 }
250
251                 if (!CairoWidget::flat_buttons() ) {
252                         cairo_set_source (cr, _pattern);
253                         cairo_matrix_init_translate (&matrix, 0, (h - ds));
254                         cairo_pattern_set_matrix (_pattern, &matrix);
255                 } else {
256                         CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
257                         cairo_fill (cr);
258                         CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
259                         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, ds + CORNER_OFFSET,
260                                         w - CORNER_SIZE, h - ds - CORNER_SIZE, CORNER_RADIUS);
261                 }
262                 cairo_fill (cr);
263
264         } else {
265
266                 if (ds < FADER_RESERVE) {
267                         ds = FADER_RESERVE;
268                 }
269                 assert(ds <= w);
270
271                 /*
272                  * if ds == w, the pattern does not need to be translated
273                  * if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
274                  * w to the left, which is -w in pattern space, and w in user space
275                  * if ds == 10, then the pattern needs to be moved w - 10
276                  * to the left, which is -(w-10) in pattern space, which
277                  * is (w - 10) in user space
278                  * thus: translation = (w - ds)
279                  */
280
281                 if (!CairoWidget::flat_buttons() ) {
282                         cairo_set_source (cr, _pattern);
283                         cairo_matrix_init_translate (&matrix, w - ds, 0);
284                         cairo_pattern_set_matrix (_pattern, &matrix);
285                 } else {
286                         CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
287                         cairo_fill (cr);
288                         CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
289                         Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET,
290                                         ds - CORNER_SIZE, h - CORNER_SIZE, CORNER_RADIUS);
291                 }
292                 cairo_fill (cr);
293         }
294
295         /* draw the unity-position line if it's not at either end*/
296         if (!(_tweaks & NoShowUnityLine) && _unity_loc > CORNER_RADIUS) {
297                 context->set_line_width (1);
298                 context->set_line_cap (Cairo::LINE_CAP_ROUND);
299                 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
300                 context->set_source_rgba (c.get_red_p() * 1.5, c.get_green_p() * 1.5, c.get_blue_p() * 1.5, 0.85);
301                 if (_orien == VERT) {
302                         if (_unity_loc < h - CORNER_RADIUS) {
303                                 context->move_to (1.5, _unity_loc + CORNER_OFFSET + .5);
304                                 context->line_to (_girth - 1.5, _unity_loc + CORNER_OFFSET + .5);
305                                 context->stroke ();
306                         }
307                 } else {
308                         if (_unity_loc < w - CORNER_RADIUS) {
309                                 context->move_to (_unity_loc - CORNER_OFFSET + .5, 1.5);
310                                 context->line_to (_unity_loc - CORNER_OFFSET + .5, _girth - 1.5);
311                                 context->stroke ();
312                         }
313                 }
314         }
315
316         if (_layout && !_text.empty() && _orien == HORIZ) {
317                 cairo_save (cr);
318                 if (_centered_text) {
319                         /* center text */
320                         cairo_move_to (cr, (w - _text_width)/2.0, h/2.0 - _text_height/2.0);
321                 } else if (ds > .5 * w) {
322                         cairo_move_to (cr, CORNER_OFFSET + 3, h/2.0 - _text_height/2.0);
323                         cairo_set_operator(cr, CAIRO_OPERATOR_XOR);
324                 } else {
325                         cairo_move_to (cr, w - _text_width - CORNER_OFFSET - 3, h/2.0 - _text_height/2.0);
326                 }
327                 CairoWidget::set_source_rgb_a (cr, get_style()->get_text (get_state()), 1);
328                 pango_cairo_show_layout (cr, _layout->gobj());
329                 cairo_restore (cr);
330         }
331
332         if (!get_sensitive()) {
333                 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
334                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
335                 cairo_fill (cr);
336         } else if (_hovering) {
337                 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
338                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
339                 cairo_fill (cr);
340         }
341
342         _last_drawn = ds;
343
344         return true;
345 }
346
347 void
348 PixFader::on_size_request (GtkRequisition* req)
349 {
350         if (_orien == VERT) {
351                 req->width = (_min_girth ? _min_girth : -1);
352                 req->height = (_min_span ? _min_span : -1);
353         } else {
354                 req->height = (_min_girth ? _min_girth : -1);
355                 req->width = (_min_span ? _min_span : -1);
356         }
357 }
358
359 void
360 PixFader::on_size_allocate (Gtk::Allocation& alloc)
361 {
362         DrawingArea::on_size_allocate(alloc);
363
364         if (_orien == VERT) {
365                 _girth = alloc.get_width ();
366                 _span = alloc.get_height ();
367         } else {
368                 _girth = alloc.get_height ();
369                 _span = alloc.get_width ();
370         }
371
372         if (is_realized()) {
373                 /* recreate patterns in case we've changed size */
374                 create_patterns ();
375         }
376
377         update_unity_position ();
378 }
379
380 bool
381 PixFader::on_button_press_event (GdkEventButton* ev)
382 {
383         if (ev->type != GDK_BUTTON_PRESS) {
384                 if (_dragging) {
385                         remove_modal_grab();
386                         _dragging = false;
387                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
388                         StopGesture ();
389                 }
390                 return (_tweaks & NoButtonForward) ? true : false;
391         }
392
393         if (ev->button != 1 && ev->button != 2) {
394                 return false;
395         }
396
397         add_modal_grab ();
398         StartGesture ();
399         _grab_loc = (_orien == VERT) ? ev->y : ev->x;
400         _grab_start = (_orien == VERT) ? ev->y : ev->x;
401         _grab_window = ev->window;
402         _dragging = true;
403         gdk_pointer_grab(ev->window,false,
404                         GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
405                         NULL,NULL,ev->time);
406
407         if (ev->button == 2) {
408                 set_adjustment_from_event (ev);
409         }
410
411         return (_tweaks & NoButtonForward) ? true : false;
412 }
413
414 bool
415 PixFader::on_button_release_event (GdkEventButton* ev)
416 {
417         double ev_pos = (_orien == VERT) ? ev->y : ev->x;
418
419         switch (ev->button) {
420         case 1:
421                 if (_dragging) {
422                         remove_modal_grab();
423                         _dragging = false;
424                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
425                         StopGesture ();
426
427                         if (!_hovering) {
428                                 if (!(_tweaks & NoVerticalScroll)) {
429                                         Keyboard::magic_widget_drop_focus();
430                                 }
431                                 queue_draw ();
432                         }
433
434                         if (ev_pos == _grab_start) {
435                                 /* no motion - just a click */
436                                 const double slider_pos =  display_span();
437                                 ev_pos = rint(ev_pos);
438
439                                 if (ev->state & Keyboard::TertiaryModifier) {
440                                         _adjustment.set_value (_default_value);
441                                 } else if (ev->state & Keyboard::GainFineScaleModifier) {
442                                         _adjustment.set_value (_adjustment.get_lower());
443                                 } else if (ev_pos == slider_pos) {
444                                         ; // click on current position, no move.
445                                 } else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
446                                         /* above the current display height, remember X Window coords */
447                                         _adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
448                                 } else {
449                                         _adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
450                                 }
451                         }
452                         return true;
453                 }
454                 break;
455
456         case 2:
457                 if (_dragging) {
458                         remove_modal_grab();
459                         _dragging = false;
460                         StopGesture ();
461                         set_adjustment_from_event (ev);
462                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
463                         return true;
464                 }
465                 break;
466
467         default:
468                 break;
469         }
470         return false;
471 }
472
473 bool
474 PixFader::on_scroll_event (GdkEventScroll* ev)
475 {
476         double scale;
477         bool ret = false;
478
479         if (ev->state & Keyboard::GainFineScaleModifier) {
480                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
481                         scale = 0.01;
482                 } else {
483                         scale = 0.05;
484                 }
485         } else {
486                 scale = 0.25;
487         }
488
489         if (_orien == VERT) {
490                 switch (ev->direction) {
491                         case GDK_SCROLL_UP:
492                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
493                                 ret = true;
494                                 break;
495                         case GDK_SCROLL_DOWN:
496                                 _adjustment.set_value (_adjustment.get_value() - (_adjustment.get_page_increment() * scale));
497                                 ret = true;
498                                 break;
499                         default:
500                                 break;
501                 }
502         } else {
503                 int dir = ev->direction;
504
505                 if (ev->state & Keyboard::ScrollHorizontalModifier || !(_tweaks & NoVerticalScroll)) {
506                         if (ev->direction == GDK_SCROLL_UP) dir = GDK_SCROLL_RIGHT;
507                         if (ev->direction == GDK_SCROLL_DOWN) dir = GDK_SCROLL_LEFT;
508                 }
509
510                 switch (dir) {
511                         case GDK_SCROLL_RIGHT:
512                                 _adjustment.set_value (_adjustment.get_value() + (_adjustment.get_page_increment() * scale));
513                                 ret = true;
514                                 break;
515                         case GDK_SCROLL_LEFT:
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         if (!(_tweaks & NoVerticalScroll)) {
607                 Keyboard::magic_widget_grab_focus ();
608         }
609         queue_draw ();
610         return false;
611 }
612
613 bool
614 PixFader::on_leave_notify_event (GdkEventCrossing*)
615 {
616         if (!_dragging) {
617                 _hovering = false;
618                 if (!(_tweaks & NoVerticalScroll)) {
619                         Keyboard::magic_widget_drop_focus();
620                 }
621                 queue_draw ();
622         }
623         return false;
624 }
625
626 void
627 PixFader::set_adjustment_from_event (GdkEventButton* ev)
628 {
629         double fract = (_orien == VERT) ? (1.0 - (ev->y / _span)) : (ev->x / _span);
630
631         fract = min (1.0, fract);
632         fract = max (0.0, fract);
633
634         _adjustment.set_value (fract * (_adjustment.get_upper () - _adjustment.get_lower ()));
635 }
636
637 void
638 PixFader::set_default_value (float d)
639 {
640         _default_value = d;
641         update_unity_position ();
642 }
643
644 void
645 PixFader::set_tweaks (Tweaks t)
646 {
647         bool need_redraw = false;
648         if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
649                 need_redraw = true;
650         }
651         _tweaks = t;
652         if (need_redraw) {
653                 queue_draw();
654         }
655 }
656
657 void
658 PixFader::set_text (const std::string& str, bool centered, bool expose)
659 {
660         if (_layout && _text == str) {
661                 return;
662         }
663         if (!_layout && !str.empty()) {
664                 _layout = Pango::Layout::create (get_pango_context());
665         }
666
667         _text = str;
668         _centered_text = centered;
669         if (_layout) {
670                 _layout->set_text (str);
671                 _layout->get_pixel_size (_text_width, _text_height);
672                 // queue_resize ();
673                 if (expose) queue_draw ();
674         }
675 }
676
677 void
678 PixFader::on_state_changed (Gtk::StateType old_state)
679 {
680         Widget::on_state_changed (old_state);
681         create_patterns ();
682         queue_draw ();
683 }
684
685 void
686 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
687 {
688         if (_layout) {
689                 std::string txt = _layout->get_text();
690                 _layout.clear (); // drop reference to existing layout
691                 _text = "";
692                 set_text (txt, _centered_text, false);
693         }
694         /* patterns are cached and re-created as needed 
695          * during 'expose' in the GUI thread */
696         _pattern = 0;
697         queue_draw ();
698 }
699
700 Gdk::Color
701 PixFader::get_parent_bg ()
702 {
703         Widget* parent = get_parent ();
704
705         while (parent) {
706                 if (parent->get_has_window()) {
707                         break;
708                 }
709                 parent = parent->get_parent();
710         }
711
712         if (parent && parent->get_has_window()) {
713                 if (_current_parent != parent) {
714                         if (_parent_style_change) _parent_style_change.disconnect();
715                         _current_parent = parent;
716                         _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &PixFader::on_style_changed));
717                 }
718                 return parent->get_style ()->get_bg (parent->get_state());
719         }
720
721         return get_style ()->get_bg (get_state());
722 }