f24b60148d71cd8f0db4a8877bde5d065e15c0a9
[ardour.git] / gtk2_ardour / ardour_button.cc
1 /*
2     Copyright (C) 2010 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 */
19
20 #include <iostream>
21 #include <cmath>
22 #include <algorithm>
23
24 #include <pangomm/layout.h>
25
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31 #include "gtkmm2ext/gui_thread.h"
32
33 #include "ardour/rc_configuration.h" // for widget prelight preference
34
35 #include "ardour_button.h"
36 #include "ardour_ui.h"
37 #include "global_signals.h"
38
39 #include "i18n.h"
40
41 #define REFLECTION_HEIGHT 2
42
43 using namespace Gdk;
44 using namespace Gtk;
45 using namespace Glib;
46 using namespace PBD;
47 using std::max;
48 using std::min;
49 using namespace std;
50
51 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
52 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
53 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
54 bool ArdourButton::_flat_buttons = false;
55
56 ArdourButton::ArdourButton (Element e)
57         : _elements (e)
58         , _tweaks (Tweaks (0))
59         , _text_width (0)
60         , _text_height (0)
61         , _diameter (11.0)
62         , _corner_radius (4.0)
63         , _corner_mask (0xf)
64         , border_color (0)
65         , fill_color_active (0)
66         , fill_color_inactive (0)
67         , fill_pattern (0)
68         , fill_pattern_active (0)
69         , shine_pattern (0)
70         , led_inset_pattern (0)
71         , reflection_pattern (0)
72         , _led_rect (0)
73         , _act_on_release (true)
74         , _led_left (false)
75         , _fixed_diameter (true)
76         , _distinct_led_click (false)
77         , _hovering (false)
78 {
79         ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
80 }
81
82 ArdourButton::ArdourButton (const std::string& str, Element e)
83         : _elements (e)
84         , _tweaks (Tweaks (0))
85         , _text_width (0)
86         , _text_height (0)
87         , _diameter (11.0)
88         , _corner_radius (4.0)
89         , _corner_mask (0xf)
90         , border_color (0)
91         , fill_color_active (0)
92         , fill_color_inactive (0)
93         , fill_pattern (0)
94         , fill_pattern_active (0)
95         , shine_pattern (0)
96         , led_inset_pattern (0)
97         , reflection_pattern (0)
98         , _led_rect (0)
99         , _act_on_release (true)
100         , _led_left (false)
101         , _fixed_diameter (true)
102         , _distinct_led_click (false)
103         , _hovering (false)
104 {
105         set_text (str);
106 }
107
108 ArdourButton::~ArdourButton()
109 {
110         delete _led_rect;
111
112         if (shine_pattern) {
113                 cairo_pattern_destroy (shine_pattern);
114         }
115
116         if (fill_pattern) {
117                 cairo_pattern_destroy (fill_pattern);
118         }
119         
120         if (fill_pattern_active) {
121                 cairo_pattern_destroy (fill_pattern_active);
122         }
123         
124         if (led_inset_pattern) {
125                 cairo_pattern_destroy (led_inset_pattern);
126         }
127         
128         if (reflection_pattern) {
129                 cairo_pattern_destroy (reflection_pattern);
130         }
131
132 }
133
134 void
135 ArdourButton::set_text (const std::string& str)
136 {
137         _text = str;
138
139         if (!_layout && !_text.empty()) {
140                 _layout = Pango::Layout::create (get_pango_context());
141         } 
142
143         if (_layout) {
144                 _layout->set_text (str);
145         }
146
147         queue_resize ();
148 }
149
150 void
151 ArdourButton::set_markup (const std::string& str)
152 {
153         _text = str;
154
155         if (!_layout) {
156                 _layout = Pango::Layout::create (get_pango_context());
157         } 
158
159         _layout->set_text (str);
160         queue_resize ();
161 }
162
163 void
164 ArdourButton::render (cairo_t* cr)
165 {
166         void (*rounded_function)(cairo_t*, double, double, double, double, double);
167
168         switch (_corner_mask) {
169         case 0x1: /* upper left only */
170                 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
171                 break;
172         case 0x2: /* upper right only */
173                 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
174                 break;
175         case 0x3: /* upper only */
176                 rounded_function = Gtkmm2ext::rounded_top_rectangle;
177                 break;
178                 /* should really have functions for lower right, lower left,
179                    lower only, but for now, we don't
180                 */
181         default:
182                 rounded_function = Gtkmm2ext::rounded_rectangle;
183         }
184
185         if (!_fixed_diameter) {
186                 _diameter = std::min (get_width(), get_height());
187         }
188
189         float r,g,b,a;
190
191         if ((_elements & Body)==Body) {
192                 if (_elements & Edge) {
193
194                         cairo_set_source_rgba (cr, 0, 0, 0, 1);
195                         rounded_function(cr, 0, 0, get_width(), get_height(), _corner_radius);
196                         cairo_fill (cr);
197
198                         rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.5);
199                 } else {
200                         rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
201                 }
202
203                 if (active_state() == Gtkmm2ext::ImplicitActive) {
204                         //background color
205                         cairo_set_source (cr, fill_pattern);
206                         cairo_fill (cr);
207
208                         //border
209                         UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
210                         cairo_set_line_width (cr, 1.0);
211                         rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 1.5);
212                         cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
213                         cairo_stroke (cr);
214
215                 } else if (active_state() == Gtkmm2ext::ExplicitActive || ((_elements & Indicator)==Indicator) ) {
216
217                         //background color
218                         cairo_set_source (cr, fill_pattern_active);
219                         cairo_fill (cr);
220
221                 } else {
222
223                         //background color
224                         cairo_set_source (cr, fill_pattern);
225                         cairo_fill (cr);
226
227                 }
228         }
229
230         if ( ((_elements & FlatFace)==FlatFace) && (active_state() != Gtkmm2ext::ExplicitActive) ) {
231
232                 if ( !_flat_buttons ) {
233                         float rheight = get_height()*0.5-REFLECTION_HEIGHT;
234                         Gtkmm2ext::rounded_rectangle (cr, 2, 3, get_width()-4, rheight, _corner_radius-1);
235                         cairo_set_source (cr, shine_pattern);
236                         cairo_fill (cr);
237                 }
238
239                 if (active_state() == Gtkmm2ext::ExplicitActive) {
240
241                         UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
242                         cairo_set_line_width (cr, 2.0);
243                         rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
244                         cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
245                         cairo_fill (cr);
246
247                 } else {
248
249                         UINT_TO_RGBA (fill_color_inactive, &r, &g, &b, &a);
250                         cairo_set_line_width (cr, 2.0);
251                         rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
252                         cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
253                         cairo_fill (cr);
254
255                 }
256         }
257
258         if (_pixbuf) {
259
260                 double x,y;
261                 x = (get_width() - _pixbuf->get_width())/2.0;
262                 y = (get_height() - _pixbuf->get_height())/2.0;
263
264                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
265                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
266                 cairo_fill (cr);
267         }
268
269         /* text, if any */
270
271         int text_margin;
272
273         if (get_width() < 75) {
274                 text_margin = 5;
275         } else {
276                 text_margin = 10;
277         }
278
279         if ( ((_elements & Text)==Text) && !_text.empty()) {
280
281                 cairo_new_path (cr);    
282
283                 if (_elements & Indicator) {
284                         if (_led_left) {
285                                 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
286                         } else {
287                                 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
288                         }
289                 } else {
290                         /* center text */
291                         cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
292                 }
293
294                 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
295                 pango_cairo_show_layout (cr, _layout->gobj());
296         } 
297
298         if (((_elements & Indicator)==Indicator)) {
299
300                 /* move to the center of the indicator/led */
301
302                 cairo_save (cr);
303
304                 if (_elements & Text) {
305                         if (_led_left) {
306                                 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
307                         } else {
308                                 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
309                         }
310                 } else {
311                         cairo_translate (cr, get_width()/2.0, get_height()/2.0);
312                 }
313                 
314                 //inset
315                 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
316                 cairo_set_source (cr, led_inset_pattern);
317                 cairo_fill (cr);
318                 
319                 //black ring
320                 cairo_set_source_rgb (cr, 0, 0, 0);
321                 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
322                 cairo_fill(cr);
323                 
324                 //led color
325                 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
326                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
327                 cairo_fill(cr);
328                 
329                 cairo_restore (cr);
330         }
331
332
333         /* a partially transparent gray layer to indicate insensitivity */
334
335         if ((visual_state() & Gtkmm2ext::Insensitive)) {
336                 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
337                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.5);
338                 cairo_fill (cr);
339         }
340
341         //reflection
342         bool show_reflection = (active_state() == Gtkmm2ext::ExplicitActive);
343         show_reflection &= !_flat_buttons;
344         show_reflection &= !((_elements & Indicator)==Indicator);
345         if ( show_reflection ) {
346                 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
347                 Gtkmm2ext::rounded_rectangle (cr, 2, get_height()*0.5-1, get_width()-4, rheight, _corner_radius-1);
348                 cairo_set_source (cr, shine_pattern);
349                 cairo_fill (cr);
350         }
351
352         /* if requested, show hovering */
353         
354         if (ARDOUR::Config->get_widget_prelight()) {
355                 if (_hovering) {
356                         rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
357                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
358                         cairo_fill (cr);
359                 }
360         }
361 }
362
363 void
364 ArdourButton::set_diameter (float d)
365 {
366         _diameter = (d*2) + 5.0;
367
368         if (_diameter != 0.0) {
369                 _fixed_diameter = true;
370         }
371
372         set_colors ();
373 }
374
375 void
376 ArdourButton::set_corner_radius (float r)
377 {
378         _corner_radius = r;
379         set_dirty ();
380 }
381
382 void
383 ArdourButton::on_size_request (Gtk::Requisition* req)
384 {
385         int xpad = 0;
386         int ypad = 6;
387
388         CairoWidget::on_size_request (req);
389
390         if ((_elements & Text) && !_text.empty()) {
391                 _layout->get_pixel_size (_text_width, _text_height);
392                 if (_text_width + _diameter < 75) {
393                         xpad = 7;
394                 } else {
395                         xpad = 20;
396                 }
397         } else {
398                 _text_width = 0;
399                 _text_height = 0;
400         }
401
402         if (_pixbuf) {
403                 xpad = 6;
404         }
405
406         if ((_elements & Indicator) && _fixed_diameter) {
407                 if (_pixbuf) {
408                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
409                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
410                 } else {
411                         req->width = _text_width + lrint (_diameter) + xpad;
412                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
413                 }
414         } else {
415                 if (_pixbuf) {
416                         req->width = _pixbuf->get_width() + xpad;
417                         req->height = _pixbuf->get_height() + ypad;
418                 }  else {
419                         req->width = _text_width + xpad;
420                         req->height = _text_height + ypad;
421                 }
422         }
423 }
424
425 void
426 ArdourButton::set_colors ()
427 {
428         uint32_t start_color;
429         uint32_t end_color;
430         uint32_t r, g, b, a;
431         uint32_t text_color;
432         uint32_t led_color;
433
434         /* we use the edge of the button to show Selected state, so the
435          * color/pattern used there will vary depending on that
436          */
437         
438         fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
439         fill_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
440         border_color = ARDOUR_UI::config()->color_by_name ( "button border" );
441
442         if (shine_pattern) {
443                 cairo_pattern_destroy (shine_pattern);
444                 shine_pattern = 0;
445         }
446
447         if (fill_pattern) {
448                 cairo_pattern_destroy (fill_pattern);
449                 fill_pattern = 0;
450         }
451
452         if (fill_pattern_active) {
453                 cairo_pattern_destroy (fill_pattern_active);
454                 fill_pattern_active = 0;
455         }
456
457         if (_elements & Body) {
458
459                 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
460
461                 if (_flat_buttons) {
462                         end_color = start_color;
463                 } else {
464                         end_color = fill_color_active;
465                 }
466                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
467
468                 active_r = r/255.0;
469                 active_g = g/255.0;
470                 active_b = b/255.0;
471                 active_a = a/255.0;
472
473                 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
474                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
475                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.1);
476                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.7, 1,1,1,0.2);
477                 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
478
479                 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
480                 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
481                 if (_flat_buttons) {
482                         end_color = start_color;
483                 } else {
484                         end_color = fill_color_inactive;
485                 }
486                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
487                 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
488                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
489                 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
490
491                 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
492                 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
493                 if (_flat_buttons) {
494                         end_color = start_color;
495                 } else {
496                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
497                 }
498                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
499                 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 0, r/255.0,g/255.0,b/255.0, a/255.0);
500                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
501                 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 1, r/255.0,g/255.0,b/255.0, a/255.0);
502         }
503
504         if (led_inset_pattern) {
505                 cairo_pattern_destroy (led_inset_pattern);
506         }
507         
508         if (reflection_pattern) {
509                 cairo_pattern_destroy (reflection_pattern);
510         }
511
512         if (_elements & Indicator) {
513                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
514                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
515                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
516
517                 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
518                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
519                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
520         }
521         
522         /* text and LED colors */
523
524         if (active_state() == Gtkmm2ext::ExplicitActive) {
525                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
526                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
527         } else {
528                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
529                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
530         }
531
532         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
533         text_r = r/255.0;
534         text_g = g/255.0;
535         text_b = b/255.0;
536         text_a = a/255.0;
537         UINT_TO_RGBA (led_color, &r, &g, &b, &a);
538         led_r = r/255.0;
539         led_g = g/255.0;
540         led_b = b/255.0;
541         led_a = a/255.0;
542
543         set_dirty ();
544 }
545
546 void
547 ArdourButton::set_led_left (bool yn)
548 {
549         _led_left = yn;
550 }
551
552 bool
553 ArdourButton::on_button_press_event (GdkEventButton *ev)
554 {
555         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
556                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
557                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
558                         return true;
559                 }
560         }
561
562         if (_tweaks & ShowClick) {
563                 set_active_state (Gtkmm2ext::ExplicitActive);
564         }
565
566         if (binding_proxy.button_press_handler (ev)) {
567                 return true;
568         }
569
570         if (!_act_on_release) {
571                 if (_action) {
572                         _action->activate ();
573                         return true;
574                 }
575         }
576
577         return false;
578 }
579
580 bool
581 ArdourButton::on_button_release_event (GdkEventButton *ev)
582 {
583         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
584                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
585                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
586                         signal_led_clicked(); /* EMIT SIGNAL */
587                         return true;
588                 }
589         }
590
591         if (_tweaks & ShowClick) {
592                 unset_active_state ();
593         }
594
595         signal_clicked ();
596
597         if (_act_on_release) {
598                 if (_action) {
599                         _action->activate ();
600                         return true;
601                 }
602         }
603
604
605         return false;
606 }
607
608 void
609 ArdourButton::set_distinct_led_click (bool yn)
610 {
611         _distinct_led_click = yn;
612         setup_led_rect ();
613 }
614
615 void
616 ArdourButton::color_handler ()
617 {
618         set_colors ();
619         set_dirty ();
620 }
621
622 void
623 ArdourButton::on_size_allocate (Allocation& alloc)
624 {
625         CairoWidget::on_size_allocate (alloc);
626         setup_led_rect ();
627         set_colors ();
628 }
629
630 void
631 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
632 {
633         watch_connection.disconnect ();
634         binding_proxy.set_controllable (c);
635 }
636
637 void
638 ArdourButton::watch ()
639 {
640         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
641
642         if (!c) {
643                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
644                 return;
645         }
646
647         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
648 }
649
650 void
651 ArdourButton::controllable_changed ()
652 {
653         float val = binding_proxy.get_controllable()->get_value();
654
655         if (fabs (val) >= 0.5f) {
656                 set_active_state (Gtkmm2ext::ExplicitActive);
657         } else {
658                 unset_active_state ();
659         }
660 }
661
662 void
663 ArdourButton::set_related_action (RefPtr<Action> act)
664 {
665         Gtkmm2ext::Activatable::set_related_action (act);
666
667         if (_action) {
668
669                 action_tooltip_changed ();
670
671                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
672                 if (tact) {
673                         action_toggled ();
674                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
675                 } 
676
677                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
678                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
679                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
680         }
681 }
682
683 void
684 ArdourButton::action_toggled ()
685 {
686         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
687
688         if (tact) {
689                 if (tact->get_active()) {
690                         set_active_state (Gtkmm2ext::ExplicitActive);
691                 } else {
692                         unset_active_state ();
693                 }
694         }
695 }       
696
697 void
698 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
699 {
700         set_colors ();
701 }
702
703 void
704 ArdourButton::setup_led_rect ()
705 {
706         int text_margin;
707
708         if (get_width() < 75) {
709                 text_margin = 3;
710         } else {
711                 text_margin = 10;
712         }
713
714         if (_elements & Indicator) {
715                 _led_rect = new cairo_rectangle_t;
716                 
717                 if (_elements & Text) {
718                         if (_led_left) {
719                                 _led_rect->x = text_margin;
720                         } else {
721                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
722                         }
723                 } else {
724                         /* centered */
725                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
726                 }
727
728                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
729                 _led_rect->width = _diameter;
730                 _led_rect->height = _diameter;
731
732         } else {
733                 delete _led_rect;
734                 _led_rect = 0;
735         }
736 }
737
738 void
739 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
740 {
741         _pixbuf = img;
742         queue_draw ();
743 }
744
745 void
746 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
747 {
748         bool changed = (_active_state != s);
749         CairoWidget::set_active_state (s);
750         if (changed) {
751                 set_colors ();
752         }
753 }
754         
755 void
756 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
757 {
758         bool changed = (_visual_state != s);
759         CairoWidget::set_visual_state (s);
760         if (changed) {
761                 set_colors ();
762         }
763 }
764         
765 bool
766 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
767 {
768         _hovering = true;
769
770         if (ARDOUR::Config->get_widget_prelight()) {
771                 queue_draw ();
772         }
773
774         return CairoWidget::on_enter_notify_event (ev);
775 }
776
777 bool
778 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
779 {
780         _hovering = false;
781
782         if (ARDOUR::Config->get_widget_prelight()) {
783                 queue_draw ();
784         }
785
786         return CairoWidget::on_leave_notify_event (ev);
787 }
788
789 void
790 ArdourButton::set_tweaks (Tweaks t)
791 {
792         if (_tweaks != t) {
793                 _tweaks = t;
794                 queue_draw ();
795         }
796 }
797
798 void
799 ArdourButton::action_sensitivity_changed ()
800 {
801         if (_action->property_sensitive ()) {
802                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
803         } else {
804                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
805         }
806         
807 }
808
809
810 void
811 ArdourButton::action_visibility_changed ()
812 {
813         if (_action->property_visible ()) {
814                 show ();
815         } else {
816                 hide ();
817         }
818 }
819
820 void
821 ArdourButton::action_tooltip_changed ()
822 {
823         string str = _action->property_tooltip().get_value();
824         ARDOUR_UI::instance()->set_tip (*this, str);
825 }
826
827 void
828 ArdourButton::set_rounded_corner_mask (int mask)
829 {
830         _corner_mask = mask;
831         queue_draw ();
832 }
833
834 void
835 ArdourButton::set_elements (Element e)
836 {
837         _elements = e;
838         set_colors ();
839 }
840
841 void
842 ArdourButton::add_elements (Element e)
843 {
844         _elements = (ArdourButton::Element) (_elements | e);
845         set_colors ();
846 }
847
848 void
849 ArdourButton::set_flat_buttons (bool yn)
850 {
851         _flat_buttons = yn;
852 }