no more button text position rounding
[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                 const double text_ypos = (get_height() - _text_height) * .5;
374
375                 if (_elements & Menu) {
376                         // always left align (dropdown)
377                         cairo_move_to (cr, text_margin, text_ypos);
378                         pango_cairo_show_layout (cr, _layout->gobj());
379                 } else if ( (_elements & Indicator)  == Indicator) {
380                         // left/right align depending on LED position
381                         if (_led_left) {
382                                 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
383                         } else {
384                                 cairo_move_to (cr, text_margin, text_ypos);
385                         }
386                         pango_cairo_show_layout (cr, _layout->gobj());
387                 } else {
388                         /* centered text otherwise */
389                         double ww, wh;
390                         double xa, ya;
391                         ww = get_width();
392                         wh = get_height();
393
394                         cairo_save (cr);
395                         cairo_rotate(cr, _angle * M_PI / 180.0);
396                         cairo_device_to_user(cr, &ww, &wh);
397                         xa = (ww - _text_width) * _xalign;
398                         ya = (wh - _text_height) * _yalign;
399
400                         /* quick hack for left/bottom alignment at -90deg
401                          * TODO this should be generalized incl rotation.
402                          * currently only 'user' of this API is meter_strip.cc
403                          */
404                         if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
405
406                         cairo_move_to (cr, xa, ya);
407                         pango_cairo_update_layout(cr, _layout->gobj());
408                         pango_cairo_show_layout (cr, _layout->gobj());
409                         cairo_restore (cr);
410                 }
411                 cairo_restore (cr);
412         }
413
414         //Menu "triangle"
415         if (_elements & Menu) {
416                 const float trih = ceil(_diameter * .5);
417                 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
418                 //menu arrow
419                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
420                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
421                 cairo_rel_line_to(cr, -triw2, -trih);
422                 cairo_rel_line_to(cr, 2. * triw2, 0);
423                 cairo_close_path(cr);
424
425                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
426                 cairo_fill(cr);
427
428                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
429                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
430                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
431                 cairo_close_path(cr);
432                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
433                 cairo_set_line_width(cr, 1);
434                 cairo_stroke(cr);
435         }
436
437         //Indicator LED
438         if (_elements & Indicator) {
439                 cairo_save (cr);
440
441                 /* move to the center of the indicator/led */
442                 if (_elements & Text) {
443                         int led_xoff = ceil(char_pixel_width() + _diameter * .5);
444                         if (_led_left) {
445                                 cairo_translate (cr, led_xoff, get_height() * .5);
446                         } else {
447                                 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
448                         }
449                 } else {
450                         cairo_translate (cr, get_width() * .5, get_height() * .5);
451                 }
452
453                 //inset
454                 if (!_flat_buttons) {
455                         cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
456                         cairo_set_source (cr, led_inset_pattern);
457                         cairo_fill (cr);
458                 }
459
460                 //black ring
461                 cairo_set_source_rgb (cr, 0, 0, 0);
462                 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
463                 cairo_fill(cr);
464
465                 //led color
466                 ArdourCanvas::set_source_rgba (cr, led_color);
467                 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
468                 cairo_fill(cr);
469
470                 cairo_restore (cr);
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         build_patterns ();
534         queue_resize ();
535 }
536
537 void
538 ArdourButton::set_corner_radius (float r)
539 {
540         _corner_radius = r;
541         set_dirty ();
542 }
543
544 void
545 ArdourButton::on_size_request (Gtk::Requisition* req)
546 {
547         req->width = req->height = 0;
548         CairoWidget::on_size_request (req);
549
550         if (_diameter == 0) {
551                 _diameter = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
552         }
553
554         if ((_elements & Text) && !_text.empty()) {
555                 int ignored;
556                 _layout->get_pixel_size (_text_width, ignored);
557                 _text_height = char_pixel_height ();
558                 req->width += rint(1.6 * char_pixel_width()); // padding
559                 req->width += _text_width;
560                 req->height = std::max(req->height, (int) ceil(_text_height * BASELINESTRETCH + 1.0));
561         } else {
562                 _text_width = 0;
563                 _text_height = 0;
564         }
565
566         if (_pixbuf) {
567                 req->width += _pixbuf->get_width() + char_pixel_width();
568                 req->height = std::max(req->height, _pixbuf->get_height() + 4);
569         }
570
571         if (_elements & Indicator) {
572                 req->width += lrint (_diameter) + char_pixel_width();
573                 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
574         }
575
576         if ((_elements & Menu)) {
577                 req->width += _diameter + 4;
578         }
579
580         if (_elements & (RecButton | CloseCross)) {
581                 assert(!(_elements & Text));
582                 const int wh = std::max(char_pixel_width(), char_pixel_height()) * BASELINESTRETCH;
583                 req->width += wh;
584                 req->height = std::max(req->height, (int) wh);
585         }
586
587         if (_tweaks & Square) {
588                 // squared buttons are also grouped, we cannot align all texts
589                 // -> skip proper center adjustment
590                 if (req->width < req->height)
591                         req->width = req->height;
592                 if (req->height < req->width)
593                         req->height = req->width;
594         } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
595                 // properly centered text for those elements that are centered
596                 if ((req->width - _text_width) & 1) { ++req->width; }
597                 if ((req->height - _text_height) & 1) { ++req->height; }
598         }
599 }
600
601 /**
602  * This sets the colors used for rendering based on the name of the button, and
603  * thus uses information from the GUI config data.
604  */
605 void
606 ArdourButton::set_colors ()
607 {
608         if (_fixed_colors_set) {
609                 return;
610         }
611         std::string name = get_name();
612
613         fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
614         fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
615
616         text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
617         text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
618
619         led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
620         led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
621 }
622 /**
623  * This sets the colors used for rendering based on two fixed values, rather
624  * than basing them on the button name, and thus information in the GUI config
625  * data.
626  */
627 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
628 {
629         _fixed_colors_set = true;
630
631         fill_active_color = color_active;
632         fill_inactive_color = color_inactive;
633
634         unsigned char r, g, b, a;
635         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
636
637         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
638                 (max (double(g), 255.) - min (double(g), 255.)) +
639                 (max (double(b), 255.) - min (double(b), 255.));
640
641         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
642                 (max (double(g), 0.) - min (double(g), 0.)) +
643                 (max (double(b), 0.) - min (double(b), 0.));
644
645         text_active_color = (white_contrast > black_contrast) ?
646                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
647                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
648
649
650         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
651
652         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         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_inactive_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         /* XXX what about led colors ? */
665
666         build_patterns ();
667         set_name (""); /* this will trigger a "style-changed" message and then set_colors() */
668 }
669
670 void
671 ArdourButton::build_patterns ()
672 {
673         if (convex_pattern) {
674                 cairo_pattern_destroy (convex_pattern);
675                 convex_pattern = 0;
676         }
677
678         if (concave_pattern) {
679                 cairo_pattern_destroy (concave_pattern);
680                 concave_pattern = 0;
681         }
682
683         if (led_inset_pattern) {
684                 cairo_pattern_destroy (led_inset_pattern);
685         }
686
687         //convex gradient
688         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
689         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
690         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
691
692         //concave gradient
693         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
694         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
695         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
696
697         if (_elements & Indicator) {
698                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
699                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
700                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
701         }
702
703         set_dirty ();
704 }
705
706 void
707 ArdourButton::set_led_left (bool yn)
708 {
709         _led_left = yn;
710 }
711
712 bool
713 ArdourButton::on_button_press_event (GdkEventButton *ev)
714 {
715         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
716                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
717                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
718                         return true;
719                 }
720         }
721
722         if (binding_proxy.button_press_handler (ev)) {
723                 return true;
724         }
725
726         _grabbed = true;
727         queue_draw ();
728
729         if (!_act_on_release) {
730                 if (_action) {
731                         _action->activate ();
732                         return true;
733                 }
734         }
735
736         if (_fallthrough_to_parent)
737                 return false;
738
739         return true;
740 }
741
742 bool
743 ArdourButton::on_button_release_event (GdkEventButton *ev)
744 {
745         if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
746                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
747                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
748                         signal_led_clicked(); /* EMIT SIGNAL */
749                         return true;
750                 }
751         }
752
753         _grabbed = false;
754         queue_draw ();
755
756         if (_hovering) {
757                 signal_clicked ();
758                 if (_act_on_release) {
759                         if (_action) {
760                                 _action->activate ();
761                                 return true;
762                         }
763                 }
764         }
765
766         if (_fallthrough_to_parent)
767                 return false;
768
769         return true;
770 }
771
772 void
773 ArdourButton::set_distinct_led_click (bool yn)
774 {
775         _distinct_led_click = yn;
776         setup_led_rect ();
777 }
778
779 void
780 ArdourButton::color_handler ()
781 {
782         set_colors ();
783         build_patterns ();
784         set_dirty ();
785 }
786
787 void
788 ArdourButton::on_size_allocate (Allocation& alloc)
789 {
790         CairoWidget::on_size_allocate (alloc);
791         setup_led_rect ();
792         build_patterns ();
793 }
794
795 void
796 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
797 {
798         watch_connection.disconnect ();
799         binding_proxy.set_controllable (c);
800 }
801
802 void
803 ArdourButton::watch ()
804 {
805         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
806
807         if (!c) {
808                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
809                 return;
810         }
811
812         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
813 }
814
815 void
816 ArdourButton::controllable_changed ()
817 {
818         float val = binding_proxy.get_controllable()->get_value();
819
820         if (fabs (val) >= 0.5f) {
821                 set_active_state (Gtkmm2ext::ExplicitActive);
822         } else {
823                 unset_active_state ();
824         }
825 }
826
827 void
828 ArdourButton::set_related_action (RefPtr<Action> act)
829 {
830         Gtkmm2ext::Activatable::set_related_action (act);
831
832         if (_action) {
833
834                 action_tooltip_changed ();
835
836                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
837                 if (tact) {
838                         action_toggled ();
839                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
840                 }
841
842                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
843                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
844                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
845         }
846 }
847
848 void
849 ArdourButton::action_toggled ()
850 {
851         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
852
853         if (tact) {
854                 if (tact->get_active()) {
855                         set_active_state (Gtkmm2ext::ExplicitActive);
856                 } else {
857                         unset_active_state ();
858                 }
859         }
860 }
861
862 void
863 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
864 {
865         on_name_changed();
866 }
867
868 void
869 ArdourButton::on_name_changed ()
870 {
871         _char_pixel_width = 0;
872         _char_pixel_height = 0;
873         _diameter = 0;
874         set_colors ();
875         build_patterns ();
876         queue_resize ();
877 }
878
879 void
880 ArdourButton::setup_led_rect ()
881 {
882         if (!(_elements & Indicator)) {
883                 delete _led_rect;
884                 _led_rect = 0;
885                 return;
886         }
887
888         _led_rect = new cairo_rectangle_t;
889
890         if (_elements & Text) {
891                 if (_led_left) {
892                         _led_rect->x = char_pixel_width();
893                 } else {
894                         _led_rect->x = get_width() - char_pixel_width() + _diameter;
895                 }
896         } else {
897                 /* centered */
898                 _led_rect->x = .5 * get_width() - _diameter;
899         }
900
901         _led_rect->y = .5 * (get_height() - _diameter);
902         _led_rect->width = _diameter;
903         _led_rect->height = _diameter;
904 }
905
906 void
907 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
908 {
909         _pixbuf = img;
910         queue_draw ();
911 }
912
913 void
914 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
915 {
916         bool changed = (_active_state != s);
917         CairoWidget::set_active_state (s);
918         if (changed) {
919                 set_colors ();
920                 build_patterns ();
921         }
922 }
923
924 void
925 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
926 {
927         bool changed = (_visual_state != s);
928         CairoWidget::set_visual_state (s);
929         if (changed) {
930                 set_colors ();
931                 build_patterns ();
932         }
933 }
934
935
936 bool
937 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
938 {
939         _focused = true;
940         queue_draw ();
941         return CairoWidget::on_focus_in_event (ev);
942 }
943
944 bool
945 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
946 {
947         _focused = false;
948         queue_draw ();
949         return CairoWidget::on_focus_out_event (ev);
950 }
951
952 bool
953 ArdourButton::on_key_release_event (GdkEventKey *ev) {
954         if (_focused &&
955                         (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
956         {
957                 signal_clicked();
958                 if (_action) {
959                         _action->activate ();
960                 }
961                 return true;
962         }
963         return CairoWidget::on_key_release_event (ev);
964 }
965
966 bool
967 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
968 {
969         _hovering = (_elements & Inactive) ? false : true;
970
971         if (ARDOUR::Config->get_widget_prelight()) {
972                 queue_draw ();
973         }
974
975         return CairoWidget::on_enter_notify_event (ev);
976 }
977
978 bool
979 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
980 {
981         _hovering = false;
982
983         if (ARDOUR::Config->get_widget_prelight()) {
984                 queue_draw ();
985         }
986
987         return CairoWidget::on_leave_notify_event (ev);
988 }
989
990 void
991 ArdourButton::set_tweaks (Tweaks t)
992 {
993         if (_tweaks != t) {
994                 _tweaks = t;
995                 queue_draw ();
996         }
997 }
998
999 void
1000 ArdourButton::action_sensitivity_changed ()
1001 {
1002         if (_action->property_sensitive ()) {
1003                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1004         } else {
1005                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1006         }
1007 }
1008
1009 void
1010 ArdourButton::recalc_char_pixel_geometry ()
1011 {
1012         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1013                 return;
1014         }
1015         if (!_layout) {
1016                 _layout = Pango::Layout::create (get_pango_context());
1017         }
1018         // NB. this is not static, since the geometry is different
1019         // depending on the font used.
1020         int w, h;
1021         std::string t = _layout->get_text();
1022         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1023         _layout->set_text (x);
1024         _layout->get_pixel_size (w, h);
1025         _layout->set_text (t);
1026         _char_pixel_height = std::max(4, h);
1027         // number of actual chars in the string (not bytes)
1028         // Glib to the rescue.
1029         Glib::ustring gx(x);
1030         _char_pixel_width = std::max(4, w / (int)gx.size());
1031 }
1032
1033 void
1034 ArdourButton::action_visibility_changed ()
1035 {
1036         if (_action->property_visible ()) {
1037                 show ();
1038         } else {
1039                 hide ();
1040         }
1041 }
1042
1043 void
1044 ArdourButton::action_tooltip_changed ()
1045 {
1046         string str = _action->property_tooltip().get_value();
1047         ARDOUR_UI::instance()->set_tip (*this, str);
1048 }
1049
1050 void
1051 ArdourButton::set_elements (Element e)
1052 {
1053         _elements = e;
1054 }
1055
1056 void
1057 ArdourButton::add_elements (Element e)
1058 {
1059         _elements = (ArdourButton::Element) (_elements | e);
1060 }