c29f4568ce545421ec62a52951d802a9ebe7876d
[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 #include "pbd/stacktrace.h"
29
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
33
34 #include "ardour/rc_configuration.h" // for widget prelight preference
35
36 #include "canvas/utils.h"
37
38 #include "ardour_button.h"
39 #include "ardour_ui.h"
40 #include "global_signals.h"
41
42 #include "i18n.h"
43
44 #define REFLECTION_HEIGHT 2
45
46 using namespace Gdk;
47 using namespace Gtk;
48 using namespace Glib;
49 using namespace PBD;
50 using std::max;
51 using std::min;
52 using namespace std;
53
54 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
55 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
56 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
57
58 ArdourButton::ArdourButton (Element e)
59         : _elements (e)
60         , _tweaks (Tweaks (0))
61         , _char_pixel_width (0)
62         , _char_pixel_height (0)
63         , _text_width (0)
64         , _text_height (0)
65         , _diameter (11.0)
66         , _corner_radius (2.5)
67         , _corner_mask (0xf)
68         , _angle(0)
69         , _xalign(.5)
70         , _yalign(.5)
71         , fill_inactive_color (0)
72         , fill_active_color (0)
73         , text_active_color(0)
74         , text_inactive_color(0)
75         , led_active_color(0)
76         , led_inactive_color(0)
77         , convex_pattern (0)
78         , concave_pattern (0)
79         , led_inset_pattern (0)
80         , _led_rect (0)
81         , _act_on_release (true)
82         , _led_left (false)
83         , _fixed_diameter (true)
84         , _distinct_led_click (false)
85         , _hovering (false)
86         , _focused (false)
87         , _fixed_colors_set (false)
88         , _fallthrough_to_parent (false)
89 {
90         ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
91 }
92
93 ArdourButton::ArdourButton (const std::string& str, Element e)
94         : _elements (e)
95         , _tweaks (Tweaks (0))
96         , _text_width (0)
97         , _text_height (0)
98         , _diameter (11.0)
99         , _corner_radius (2.5)
100         , _corner_mask (0xf)
101         , _angle(0)
102         , _xalign(.5)
103         , _yalign(.5)
104         , fill_inactive_color (0)
105         , fill_active_color (0)
106         , text_active_color(0)
107         , text_inactive_color(0)
108         , led_active_color(0)
109         , led_inactive_color(0)
110         , convex_pattern (0)
111         , concave_pattern (0)
112         , led_inset_pattern (0)
113         , _led_rect (0)
114         , _act_on_release (true)
115         , _led_left (false)
116         , _fixed_diameter (true)
117         , _distinct_led_click (false)
118         , _hovering (false)
119         , _focused (false)
120         , _fixed_colors_set (false)
121 {
122         set_text (str);
123 }
124
125 ArdourButton::~ArdourButton()
126 {
127         delete _led_rect;
128
129         if (convex_pattern) {
130                 cairo_pattern_destroy (convex_pattern);
131         }
132         
133         if (concave_pattern) {
134                 cairo_pattern_destroy (concave_pattern);
135         }
136         
137         if (led_inset_pattern) {
138                 cairo_pattern_destroy (led_inset_pattern);
139         }
140
141 }
142
143 void
144 ArdourButton::set_text (const std::string& str)
145 {
146         _text = str;
147
148         if (!_layout && !_text.empty()) {
149                 _layout = Pango::Layout::create (get_pango_context());
150         } 
151
152         if (_layout) {
153                 _layout->set_text (str);
154         }
155
156         queue_resize ();
157 }
158
159 void
160 ArdourButton::set_markup (const std::string& str)
161 {
162         _text = str;
163
164         if (!_layout) {
165                 _layout = Pango::Layout::create (get_pango_context());
166         } 
167
168         _layout->set_markup (str);
169         queue_resize ();
170 }
171
172 void
173 ArdourButton::set_angle (const double angle)
174 {
175         _angle = angle;
176 }
177
178 void
179 ArdourButton::set_alignment (const float xa, const float ya)
180 {
181         _xalign = xa;
182         _yalign = ya;
183 }
184
185 void
186 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
187 {
188         uint32_t text_color;
189         uint32_t led_color;
190         if ( active_state() == Gtkmm2ext::ExplicitActive ) {
191                 text_color = text_active_color;
192                 led_color = led_active_color;
193         } else {
194                 text_color = text_inactive_color;
195                 led_color = led_inactive_color;
196         }
197         
198         void (*rounded_function)(cairo_t*, double, double, double, double, double);
199
200         switch (_corner_mask) {
201         case 0x1: /* upper left only */
202                 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
203                 break;
204         case 0x2: /* upper right only */
205                 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
206                 break;
207         case 0x3: /* upper only */
208                 rounded_function = Gtkmm2ext::rounded_top_rectangle;
209                 break;
210                 /* should really have functions for lower right, lower left,
211                    lower only, but for now, we don't
212                 */
213         default:
214                 rounded_function = Gtkmm2ext::rounded_rectangle;
215         }
216
217         if (!_fixed_diameter) {
218                 _diameter = std::min (get_width(), get_height());
219         }
220
221         // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
222         if ((_elements & (Body|Edge)) == (Body|Edge)) {
223                 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
224                 cairo_set_source_rgba (cr, 0, 0, 0, 1);
225                 cairo_fill(cr);
226         }
227
228         // background fill
229         if ((_elements & Body)==Body) {
230                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
231                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
232                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
233                         cairo_fill (cr);
234                 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
235                         //background color
236                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
237                         cairo_fill (cr);
238                 } else {  //inactive, or it has an indicator
239                         //background color
240                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
241                 }
242                 cairo_fill (cr);
243         }
244
245         // IMPLICIT ACTIVE: draw a border of the active color
246         if ((_elements & Body)==Body) {
247                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
248                         cairo_set_line_width (cr, 2.0);
249                         rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius);
250                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
251                         cairo_stroke (cr);
252                 }
253         }
254
255         //show the "convex" or "concave" gradient
256         if (!_flat_buttons) {
257                 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
258                         //concave
259                         cairo_set_source (cr, concave_pattern);
260                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
261                         cairo_fill (cr);
262                 } else {
263                         cairo_set_source (cr, convex_pattern);
264                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
265                         cairo_fill (cr);
266                 }
267         }
268
269         //Pixbuf, if any
270         if (_pixbuf) {
271
272                 double x,y;
273                 x = (get_width() - _pixbuf->get_width())/2.0;
274                 y = (get_height() - _pixbuf->get_height())/2.0;
275
276                 //if this is a DropDown with an icon, then we need to 
277                   //move the icon left slightly to accomomodate the arrow
278                 if (((_elements & Menu)==Menu)) {
279                         cairo_save (cr);
280                         cairo_translate (cr, -8,0 );
281                 }
282                 
283                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
284                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
285                 cairo_fill (cr);
286
287                 //..and then return to our previous drawing position
288                 if (((_elements & Menu)==Menu)) {
289                         cairo_restore (cr);
290                 }
291         }
292         else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
293         if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
294                 const double x = get_width() * .5;
295                 const double y = get_height() * .5;
296                 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
297                 const double slit = .11 * M_PI;
298                 cairo_save(cr);
299                 cairo_translate(cr, x, y);
300
301                 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
302                 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
303                 cairo_fill_preserve(cr);
304                 cairo_set_source_rgba (cr, .0, .0, .0, .5);
305                 cairo_set_line_width(cr, 1);
306                 cairo_stroke(cr);
307
308                 cairo_save(cr);
309                 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
310
311                 cairo_rotate (cr, -.5 * M_PI);
312                 cairo_move_to(cr, 0, 0);
313                 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
314                 cairo_line_to(cr, 0, 0);
315                 cairo_close_path(cr);
316
317                 cairo_fill(cr);
318                 cairo_rotate (cr, 2. * M_PI / 3.);
319
320                 cairo_move_to(cr, 0, 0);
321                 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
322                 cairo_line_to(cr, 0, 0);
323                 cairo_close_path(cr);
324                 cairo_fill(cr);
325
326                 cairo_rotate (cr, 2. * M_PI / 3.);
327                 cairo_move_to(cr, 0, 0);
328                 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
329                 cairo_line_to(cr, 0, 0);
330                 cairo_close_path(cr);
331                 cairo_fill(cr);
332
333                 cairo_restore(cr);
334
335                 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
336                 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
337                 cairo_fill(cr);
338                 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
339                 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
340                 cairo_fill(cr);
341
342                 cairo_restore(cr);
343         }
344         else if (_elements & RecButton) {
345                 const double x = get_width() * .5;
346                 const double y = get_height() * .5;
347                 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
348                 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
349                 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
350                 cairo_fill_preserve(cr);
351                 cairo_set_source_rgba (cr, .0, .0, .0, .5);
352                 cairo_set_line_width(cr, 1);
353                 cairo_stroke(cr);
354         }
355
356         int text_margin;
357         if (get_width() < 75 || (_elements & Menu) ) {
358                 text_margin = 5;
359         } else {
360                 text_margin = 10;
361         }
362
363         // Text, if any
364         if ( !_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
365
366                 cairo_save (cr);
367                 cairo_rectangle (cr, 2, 1, get_width()-4, get_height()-2);
368                 cairo_clip(cr);
369
370                 cairo_new_path (cr);    
371                 ArdourCanvas::set_source_rgba (cr, text_color);
372
373                 if ( (_elements & Menu) == Menu) {
374                         cairo_move_to (cr, rint(text_margin), rint((get_height() - _text_height)/2.0));
375                         pango_cairo_show_layout (cr, _layout->gobj());
376                 } else if ( (_elements & Indicator)  == Indicator) {
377                         if (_led_left) {
378                                 cairo_move_to (cr, rint(text_margin + _diameter + 4), rint((get_height() - _text_height)/2.0));
379                         } else {
380                                 cairo_move_to (cr, rint(text_margin), rint((get_height() - _text_height)/2.0));
381                         }
382                         pango_cairo_show_layout (cr, _layout->gobj());
383                 } else {
384                         /* align text */
385
386                         double ww, wh;
387                         double xa, ya;
388                         ww = get_width();
389                         wh = get_height();
390                         cairo_save (cr); // TODO retain rotataion.. adj. LED,...
391                         cairo_rotate(cr, _angle * M_PI / 180.0);
392                         cairo_device_to_user(cr, &ww, &wh);
393                         xa = (ww - _text_width) * _xalign;
394                         ya = (wh - _text_height) * _yalign;
395
396                         /* quick hack for left/bottom alignment at -90deg
397                          * TODO this should be generalized incl rotation.
398                          * currently only 'user' of this API is meter_strip.cc
399                          */
400                         if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
401
402                         // TODO honor left/right text_margin with min/max()
403
404                         cairo_move_to (cr, rint(.5 + xa), rint(ya));
405                         pango_cairo_update_layout(cr, _layout->gobj());
406                         pango_cairo_show_layout (cr, _layout->gobj());
407                         cairo_restore (cr);
408                 }
409                 cairo_restore (cr);
410         }
411
412         //Menu "triangle"
413         if (((_elements & Menu)==Menu)) {
414                 const float trih = ceil(_diameter/2.0);
415                 const float triw2 = ceil(.577 * _diameter/2.0); // 1/sqrt(3) Equilateral triangle
416                 //menu arrow
417                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
418                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) / 2.0));
419                 cairo_rel_line_to(cr, -triw2, -trih);
420                 cairo_rel_line_to(cr, 2. * triw2, 0);
421                 cairo_close_path(cr);
422
423                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
424                 cairo_fill(cr);
425
426                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) / 2.0));
427                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
428                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
429                 cairo_close_path(cr);
430                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
431                 cairo_set_line_width(cr, 1);
432                 cairo_stroke(cr);
433         }
434         
435         //Indicator LED
436         if (((_elements & Indicator)==Indicator)) {
437
438                 /* move to the center of the indicator/led */
439
440                 cairo_save (cr);
441
442                 if (_elements & Text) {
443                         if (_led_left) {
444                                 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
445                         } else {
446                                 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
447                         }
448                 } else {
449                         cairo_translate (cr, get_width()/2.0, get_height()/2.0);
450                 }
451                 
452                 //inset
453                 if (!_flat_buttons) {
454                         cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
455                         cairo_set_source (cr, led_inset_pattern);
456                         cairo_fill (cr);
457                 }
458
459                 //black ring
460                 cairo_set_source_rgb (cr, 0, 0, 0);
461                 cairo_arc (cr, 0, 0, _diameter/2-1, 0, 2 * M_PI);
462                 cairo_fill(cr);
463
464                 //led color
465                 ArdourCanvas::set_source_rgba (cr, led_color);
466                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
467                 cairo_fill(cr);
468                 
469                 cairo_restore (cr);
470         }
471
472
473         // a transparent gray layer to indicate insensitivity
474         if ((visual_state() & Gtkmm2ext::Insensitive)) {
475                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
476                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
477                 cairo_fill (cr);
478         }
479
480         // if requested, show hovering
481         if (ARDOUR::Config->get_widget_prelight()
482                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
483                 if (_hovering) {
484                         rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
485                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
486                         cairo_fill (cr);
487                 }
488         }
489
490         //user is currently pressing the button.  black outline helps to indicate this
491         if ( _grabbed && !(_elements & (Inactive|Menu))) {
492                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
493                 cairo_set_line_width(cr, 2);
494                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5); // XXX no longer 'black'
495                 cairo_stroke (cr);
496         }
497         
498         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a selection indicator
499         if (visual_state() & Gtkmm2ext::Selected) {
500 #if 0 // outline, edge + 1px outside
501                 cairo_set_line_width(cr, 2);
502                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
503 #else // replace edge (if any)
504                 cairo_set_line_width(cr, 1);
505                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
506 #endif
507                 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
508                 cairo_stroke (cr);
509         }
510         
511         //I guess this means we have keyboard focus.  I don't think this works currently
512         //
513         //A: yes, it's keyboard focus and it does work when there's no editor window
514         //   (the editor is always the first receiver for KeyDown).
515         //   It's needed for eg. the engine-dialog at startup or after closing a sesion.
516         if (_focused) {
517                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
518                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
519                 double dashes = 1;
520                 cairo_set_dash (cr, &dashes, 1, 0);
521                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
522                 cairo_set_line_width (cr, 1.0);
523                 cairo_stroke (cr);
524                 cairo_set_dash (cr, 0, 0, 0);
525         }
526 }
527
528 void
529 ArdourButton::set_diameter (float d)
530 {
531         _diameter = (d*2) + 5.0;
532
533         if (_diameter != 0.0) {
534                 _fixed_diameter = true;
535         }
536
537         build_patterns ();
538         queue_resize ();
539 }
540
541 void
542 ArdourButton::set_corner_radius (float r)
543 {
544         _corner_radius = r;
545         set_dirty ();
546 }
547
548 void
549 ArdourButton::on_size_request (Gtk::Requisition* req)
550 {
551         int xpad = 0;
552         int ypad = 6;
553
554         CairoWidget::on_size_request (req);
555
556         if ((_elements & Text) && !_text.empty()) {
557
558                 //calc our real width for our string (but ignore the height, because that results in inconsistent button heights)
559                 int ignored;
560                 _layout->get_pixel_size (_text_width, ignored);
561                 _text_height = char_pixel_height ();
562
563                 if (_text_width + _diameter < 75) {
564                         xpad = 7;
565                 } else {
566                         xpad = 12;
567                 }
568         } else {
569                 _text_width = 0;
570                 _text_height = 0;
571         }
572
573         if ((_elements & RecButton) || _pixbuf) {
574                 xpad = 6;
575         }
576
577         if ((_elements & Indicator) && _fixed_diameter) {
578                 if (_pixbuf) {
579                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
580                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
581                 } else {
582                         req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
583                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
584                 }
585         } else {
586                 if (_pixbuf) {
587                         req->width = _pixbuf->get_width() + xpad;
588                         req->height = _pixbuf->get_height() + ypad;
589                 }  else {
590                         req->width = _text_width + xpad;
591                         req->height = _text_height + ypad;
592                 }
593         }
594
595         if ((_elements & Menu)) {
596                 req->width += _diameter + 4;
597         }
598
599         if ((_elements & RecButton) && !_pixbuf) {
600                 assert(!(_elements & Text));
601                 req->width += std::max(char_pixel_width(), char_pixel_height());
602                 req->height += std::max(char_pixel_width(), char_pixel_height());
603         }
604
605         req->width += _corner_radius;
606         req->height += _corner_radius;
607
608         if ((_tweaks & Square)==Square) {
609                 if (req->width < req->height)
610                         req->width = req->height;
611                 if (req->height < req->width)
612                         req->height = req->width;
613         }
614 }
615
616 /**
617  * This sets the colors used for rendering based on the name of the button, and
618  * thus uses information from the GUI config data. 
619  */
620 void
621 ArdourButton::set_colors ()
622 {
623         if (_fixed_colors_set) {
624                 return;
625         }
626         std::string name = get_name();
627
628         fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
629         fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
630
631         text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
632         text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
633
634         led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
635         led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
636 }
637 /**
638  * This sets the colors used for rendering based on two fixed values, rather
639  * than basing them on the button name, and thus information in the GUI config
640  * data.
641  */
642 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
643 {
644         _fixed_colors_set = true;
645
646         fill_active_color = color_active;
647         fill_inactive_color = color_inactive;
648
649         unsigned char r, g, b, a;
650         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
651         
652         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
653                 (max (double(g), 255.) - min (double(g), 255.)) +
654                 (max (double(b), 255.) - min (double(b), 255.));
655         
656         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
657                 (max (double(g), 0.) - min (double(g), 0.)) +
658                 (max (double(b), 0.) - min (double(b), 0.));
659         
660         text_active_color = (white_contrast > black_contrast) ?
661                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
662                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
663         
664
665         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
666
667         white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
668                 (max (double(g), 255.) - min (double(g), 255.)) +
669                 (max (double(b), 255.) - min (double(b), 255.));
670         
671         black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
672                 (max (double(g), 0.) - min (double(g), 0.)) +
673                 (max (double(b), 0.) - min (double(b), 0.));
674         
675         text_inactive_color = (white_contrast > black_contrast) ?
676                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
677                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
678         
679         /* XXX what about led colors ? */
680
681         build_patterns ();
682         set_name (""); /* this will trigger a "style-changed" message and then set_colors() */
683 }
684
685 void
686 ArdourButton::build_patterns ()
687 {
688         if (convex_pattern) {
689                 cairo_pattern_destroy (convex_pattern);
690                 convex_pattern = 0;
691         }
692
693         if (concave_pattern) {
694                 cairo_pattern_destroy (concave_pattern);
695                 concave_pattern = 0;
696         }
697
698         if (led_inset_pattern) {
699                 cairo_pattern_destroy (led_inset_pattern);
700         }
701         
702         //convex gradient
703         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
704         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
705         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
706
707         //concave gradient
708         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
709         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
710         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
711
712         if (_elements & Indicator) {
713                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
714                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
715                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
716         }
717
718         set_dirty ();
719 }
720
721 void
722 ArdourButton::set_led_left (bool yn)
723 {
724         _led_left = yn;
725 }
726
727 bool
728 ArdourButton::on_button_press_event (GdkEventButton *ev)
729 {
730         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
731                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
732                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
733                         return true;
734                 }
735         }
736
737         if (binding_proxy.button_press_handler (ev)) {
738                 return true;
739         }
740         
741         _grabbed = true;
742         queue_draw ();
743
744         if (!_act_on_release) {
745                 if (_action) {
746                         _action->activate ();
747                         return true;
748                 }
749         }
750
751         if (_fallthrough_to_parent)
752                 return false;
753
754         return true;
755 }
756
757 bool
758 ArdourButton::on_button_release_event (GdkEventButton *ev)
759 {
760         if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
761                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
762                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
763                         signal_led_clicked(); /* EMIT SIGNAL */
764                         return true;
765                 }
766         }
767
768         _grabbed = false;
769         queue_draw ();
770
771         if (_hovering) {
772                 signal_clicked ();      
773                 if (_act_on_release) {
774                         if (_action) {
775                                 _action->activate ();
776                                 return true;
777                         }
778                 }
779         }
780         
781         if (_fallthrough_to_parent)
782                 return false;
783
784         return true;
785 }
786
787 void
788 ArdourButton::set_distinct_led_click (bool yn)
789 {
790         _distinct_led_click = yn;
791         setup_led_rect ();
792 }
793
794 void
795 ArdourButton::color_handler ()
796 {
797         set_colors ();
798         build_patterns ();
799         set_dirty ();
800 }
801
802 void
803 ArdourButton::on_size_allocate (Allocation& alloc)
804 {
805         CairoWidget::on_size_allocate (alloc);
806         setup_led_rect ();
807         build_patterns ();
808 }
809
810 void
811 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
812 {
813         watch_connection.disconnect ();
814         binding_proxy.set_controllable (c);
815 }
816
817 void
818 ArdourButton::watch ()
819 {
820         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
821
822         if (!c) {
823                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
824                 return;
825         }
826
827         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
828 }
829
830 void
831 ArdourButton::controllable_changed ()
832 {
833         float val = binding_proxy.get_controllable()->get_value();
834
835         if (fabs (val) >= 0.5f) {
836                 set_active_state (Gtkmm2ext::ExplicitActive);
837         } else {
838                 unset_active_state ();
839         }
840 }
841
842 void
843 ArdourButton::set_related_action (RefPtr<Action> act)
844 {
845         Gtkmm2ext::Activatable::set_related_action (act);
846
847         if (_action) {
848
849                 action_tooltip_changed ();
850
851                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
852                 if (tact) {
853                         action_toggled ();
854                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
855                 } 
856
857                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
858                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
859                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
860         }
861 }
862
863 void
864 ArdourButton::action_toggled ()
865 {
866         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
867
868         if (tact) {
869                 if (tact->get_active()) {
870                         set_active_state (Gtkmm2ext::ExplicitActive);
871                 } else {
872                         unset_active_state ();
873                 }
874         }
875 }       
876
877 void
878 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
879 {
880         on_name_changed();
881 }
882
883 void
884 ArdourButton::on_name_changed ()
885 {
886         _char_pixel_width = 0;
887         _char_pixel_height = 0;
888         set_colors ();
889         build_patterns ();
890 }
891
892 void
893 ArdourButton::setup_led_rect ()
894 {
895         int text_margin;
896
897         if (get_width() < 75) {
898                 text_margin = 3;
899         } else {
900                 text_margin = 10;
901         }
902
903         if (_elements & Indicator) {
904                 _led_rect = new cairo_rectangle_t;
905                 
906                 if (_elements & Text) {
907                         if (_led_left) {
908                                 _led_rect->x = text_margin;
909                         } else {
910                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
911                         }
912                 } else {
913                         /* centered */
914                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
915                 }
916
917                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
918                 _led_rect->width = _diameter;
919                 _led_rect->height = _diameter;
920
921         } else {
922                 delete _led_rect;
923                 _led_rect = 0;
924         }
925 }
926
927 void
928 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
929 {
930         _pixbuf = img;
931         queue_draw ();
932 }
933
934 void
935 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
936 {
937         bool changed = (_active_state != s);
938         CairoWidget::set_active_state (s);
939         if (changed) {
940                 set_colors ();
941                 build_patterns ();
942         }
943 }
944         
945 void
946 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
947 {
948         bool changed = (_visual_state != s);
949         CairoWidget::set_visual_state (s);
950         if (changed) {
951                 set_colors ();
952                 build_patterns ();
953         }
954 }
955         
956
957 bool
958 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
959 {
960         _focused = true;
961         queue_draw ();
962         return CairoWidget::on_focus_in_event (ev);
963 }
964
965 bool
966 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
967 {
968         _focused = false;
969         queue_draw ();
970         return CairoWidget::on_focus_out_event (ev);
971 }
972
973 bool
974 ArdourButton::on_key_release_event (GdkEventKey *ev) {
975         if (_focused &&
976                         (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
977         {
978                 signal_clicked();
979                 if (_action) {
980                         _action->activate ();
981                 }
982                 return true;
983         }
984         return CairoWidget::on_key_release_event (ev);
985 }
986
987 bool
988 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
989 {
990         _hovering = (_elements & Inactive) ? false : true;
991
992         if (ARDOUR::Config->get_widget_prelight()) {
993                 queue_draw ();
994         }
995
996         return CairoWidget::on_enter_notify_event (ev);
997 }
998
999 bool
1000 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1001 {
1002         _hovering = false;
1003
1004         if (ARDOUR::Config->get_widget_prelight()) {
1005                 queue_draw ();
1006         }
1007
1008         return CairoWidget::on_leave_notify_event (ev);
1009 }
1010
1011 void
1012 ArdourButton::set_tweaks (Tweaks t)
1013 {
1014         if (_tweaks != t) {
1015                 _tweaks = t;
1016                 queue_draw ();
1017         }
1018 }
1019
1020 void
1021 ArdourButton::action_sensitivity_changed ()
1022 {
1023         if (_action->property_sensitive ()) {
1024                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1025         } else {
1026                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1027         }
1028         
1029 }
1030
1031 void
1032 ArdourButton::recalc_char_pixel_geometry ()
1033 {
1034         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1035                 return;
1036         }
1037         if (!_layout) {
1038                 _layout = Pango::Layout::create (get_pango_context());
1039         }
1040         // NB. this is not static, since the geometry is different
1041         // depending on the font used.
1042         int w, h;
1043         std::string t = _layout->get_text();
1044         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1045         _layout->set_text (x);
1046         _layout->get_pixel_size (w, h);
1047         _layout->set_text (t);
1048         _char_pixel_height = std::max(4, h);
1049         // number of actual chars in the string (not bytes)
1050         // Glib to the rescue.
1051         Glib::ustring gx(x);
1052         _char_pixel_width = std::max(4, w / (int)gx.size());
1053 }
1054
1055 void
1056 ArdourButton::action_visibility_changed ()
1057 {
1058         if (_action->property_visible ()) {
1059                 show ();
1060         } else {
1061                 hide ();
1062         }
1063 }
1064
1065 void
1066 ArdourButton::action_tooltip_changed ()
1067 {
1068         string str = _action->property_tooltip().get_value();
1069         ARDOUR_UI::instance()->set_tip (*this, str);
1070 }
1071
1072 void
1073 ArdourButton::set_elements (Element e)
1074 {
1075         _elements = e;
1076 }
1077
1078 void
1079 ArdourButton::add_elements (Element e)
1080 {
1081         _elements = (ArdourButton::Element) (_elements | e);
1082 }