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