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