2 Copyright (C) 2010 Paul Davis
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.
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.
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.
24 #include <pangomm/layout.h>
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/stacktrace.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
34 #include "ardour/rc_configuration.h" // for widget prelight preference
36 #include "canvas/utils.h"
37 #include "canvas/colors.h"
39 #include "ardour_button.h"
40 #include "ardour_ui.h"
41 #include "global_signals.h"
45 #define BASELINESTRETCH (1.25)
46 #define TRACKHEADERBTNW (3.10)
56 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
57 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
58 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
60 ArdourButton::ArdourButton (Element e)
62 , _tweaks (Tweaks (0))
63 , _char_pixel_width (0)
64 , _char_pixel_height (0)
65 , _char_avg_pixel_width (0)
69 , _corner_radius (2.5)
74 , fill_inactive_color (0)
75 , fill_active_color (0)
76 , text_active_color(0)
77 , text_inactive_color(0)
79 , led_inactive_color(0)
80 , led_custom_color (0)
81 , use_custom_led_color (false)
84 , led_inset_pattern (0)
86 , _act_on_release (true)
88 , _distinct_led_click (false)
91 , _fixed_colors_set (false)
92 , _fallthrough_to_parent (false)
93 , _layout_ellipsize_width (-1)
94 , _ellipsis (Pango::ELLIPSIZE_NONE)
95 , _update_colors (true)
98 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
101 ArdourButton::ArdourButton (const std::string& str, Element e)
103 , _tweaks (Tweaks (0))
107 , _corner_radius (2.5)
112 , fill_inactive_color (0)
113 , fill_active_color (0)
114 , text_active_color(0)
115 , text_inactive_color(0)
116 , led_active_color(0)
117 , led_inactive_color(0)
118 , led_custom_color (0)
119 , use_custom_led_color (false)
121 , concave_pattern (0)
122 , led_inset_pattern (0)
124 , _act_on_release (true)
126 , _distinct_led_click (false)
129 , _fixed_colors_set (false)
130 , _fallthrough_to_parent (false)
131 , _layout_ellipsize_width (-1)
132 , _ellipsis (Pango::ELLIPSIZE_NONE)
133 , _update_colors (true)
134 , _pattern_height (0)
137 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
138 ARDOUR_UI_UTILS::DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
141 ArdourButton::~ArdourButton()
145 if (convex_pattern) {
146 cairo_pattern_destroy (convex_pattern);
149 if (concave_pattern) {
150 cairo_pattern_destroy (concave_pattern);
153 if (led_inset_pattern) {
154 cairo_pattern_destroy (led_inset_pattern);
159 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
163 _layout->set_font_description (fd);
169 ArdourButton::set_text (const std::string& str)
172 if (!is_realized()) {
176 if (_layout && _layout->get_text() != _text) {
177 _layout->set_text (_text);
183 ArdourButton::set_angle (const double angle)
189 ArdourButton::set_alignment (const float xa, const float ya)
196 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
201 if (_update_colors) {
204 if (get_height() != _pattern_height) {
208 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
209 text_color = text_active_color;
210 led_color = led_active_color;
212 text_color = text_inactive_color;
213 led_color = led_inactive_color;
216 if (use_custom_led_color) {
217 led_color = led_custom_color;
220 void (*rounded_function)(cairo_t*, double, double, double, double, double);
222 switch (_corner_mask) {
223 case 0x1: /* upper left only */
224 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
226 case 0x2: /* upper right only */
227 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
229 case 0x3: /* upper only */
230 rounded_function = Gtkmm2ext::rounded_top_rectangle;
232 /* should really have functions for lower right, lower left,
233 lower only, but for now, we don't
236 rounded_function = Gtkmm2ext::rounded_rectangle;
239 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
240 if ((_elements & (Body|Edge)) == (Body|Edge)) {
241 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
242 cairo_set_source_rgba (cr, 0, 0, 0, 1);
247 if ((_elements & Body)==Body) {
248 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
249 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
250 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
252 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
254 ArdourCanvas::set_source_rgba (cr, fill_active_color);
256 } else { //inactive, or it has an indicator
258 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
263 // IMPLICIT ACTIVE: draw a border of the active color
264 if ((_elements & Body)==Body) {
265 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
266 cairo_set_line_width (cr, 2.0);
267 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
268 ArdourCanvas::set_source_rgba (cr, fill_active_color);
273 //show the "convex" or "concave" gradient
274 if (!_flat_buttons) {
275 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
277 cairo_set_source (cr, concave_pattern);
278 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
281 cairo_set_source (cr, convex_pattern);
282 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
289 double x = rint((get_width() - _pixbuf->get_width()) * .5);
290 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
291 #if 0 // DEBUG style (print on hover)
292 if (_hovering || (_elements & Inactive)) {
293 printf("%s: p:%dx%d (%dx%d)\n",
295 _pixbuf->get_width(), _pixbuf->get_height(),
296 get_width(), get_height());
299 if (_elements & Menu) {
300 //if this is a DropDown with an icon, then we need to
301 //move the icon left slightly to accomomodate the arrow
304 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
305 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
308 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
309 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
310 const double x = get_width() * .5;
311 const double y = get_height() * .5;
312 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
313 const double slit = .11 * M_PI;
315 cairo_translate(cr, x, y);
317 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
318 if (active_state() == Gtkmm2ext::ExplicitActive)
319 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
321 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
322 cairo_fill_preserve(cr);
323 cairo_set_source_rgba (cr, .0, .0, .0, .5);
324 cairo_set_line_width(cr, 1);
328 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
330 cairo_rotate (cr, -.5 * M_PI);
331 cairo_move_to(cr, 0, 0);
332 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
333 cairo_line_to(cr, 0, 0);
334 cairo_close_path(cr);
337 cairo_rotate (cr, 2. * M_PI / 3.);
339 cairo_move_to(cr, 0, 0);
340 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
341 cairo_line_to(cr, 0, 0);
342 cairo_close_path(cr);
345 cairo_rotate (cr, 2. * M_PI / 3.);
346 cairo_move_to(cr, 0, 0);
347 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
348 cairo_line_to(cr, 0, 0);
349 cairo_close_path(cr);
354 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
355 if (active_state() == Gtkmm2ext::ExplicitActive)
356 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
358 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
360 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
361 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
366 else if (_elements & RecButton) {
367 const double x = get_width() * .5;
368 const double y = get_height() * .5;
369 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
370 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
371 if (active_state() == Gtkmm2ext::ExplicitActive)
372 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
374 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
375 cairo_fill_preserve(cr);
376 cairo_set_source_rgba (cr, .0, .0, .0, .8);
377 cairo_set_line_width(cr, 1);
380 else if (_elements & CloseCross) {
381 const double x = get_width() * .5;
382 const double y = get_height() * .5;
383 const double o = .5 + std::min(x, y) * .4;
384 ArdourCanvas::set_source_rgba (cr, text_color);
385 cairo_set_line_width(cr, 1);
386 cairo_move_to(cr, x-o, y-o);
387 cairo_line_to(cr, x+o, y+o);
388 cairo_move_to(cr, x+o, y-o);
389 cairo_line_to(cr, x-o, y+o);
392 else if (_elements & StripWidth) {
393 const double x0 = get_width() * .2;
394 const double x1 = get_width() * .8;
396 const double y0 = get_height() * .25;
397 const double y1= get_height() * .75;
399 const double ym= get_height() * .5;
402 const double xa0= get_height() * .39;
403 const double xa1= get_height() * .61;
404 const double ya0= get_height() * .35;
405 const double ya1= get_height() * .65;
407 ArdourCanvas::set_source_rgba (cr, text_color);
408 cairo_set_line_width(cr, 1);
411 cairo_move_to(cr, x0, y0);
412 cairo_line_to(cr, x0, y1);
413 cairo_move_to(cr, x1, y0);
414 cairo_line_to(cr, x1, y1);
417 cairo_move_to(cr, x0, ym);
418 cairo_line_to(cr, x1, ym);
421 cairo_move_to(cr, x0, ym);
422 cairo_line_to(cr, xa0, ya0);
423 cairo_move_to(cr, x0, ym);
424 cairo_line_to(cr, xa0, ya1);
427 cairo_move_to(cr, x1, ym);
428 cairo_line_to(cr, xa1, ya0);
429 cairo_move_to(cr, x1, ym);
430 cairo_line_to(cr, xa1, ya1);
433 else if (_elements & DinMidi) {
434 const double x = get_width() * .5;
435 const double y = get_height() * .5;
436 const double r = std::min(x, y) * .75;
437 ArdourCanvas::set_source_rgba (cr, text_color);
438 cairo_set_line_width(cr, 1);
439 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
442 // pins equally spaced 45deg
443 cairo_arc (cr, x, y * 0.5, r * .15, 0, 2 * M_PI);
445 cairo_arc (cr, x * 0.5, y, r * .15, 0, 2 * M_PI);
447 cairo_arc (cr, x * 1.5, y, r * .15, 0, 2 * M_PI);
449 // .5 + .5 * .5 * sin(45deg), 1.5 - .5 * .5 * cos(45deg)
450 cairo_arc (cr, x * 0.677, y * .677, r * .15, 0, 2 * M_PI);
452 cairo_arc (cr, x * 1.323, y * .677, r * .15, 0, 2 * M_PI);
456 cairo_arc (cr, x, y+r, r * .28, 1.05 * M_PI, 1.95 * M_PI);
460 const int text_margin = char_pixel_width();
462 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
464 #if 0 // DEBUG style (print on hover)
465 if (_hovering || (_elements & Inactive)) {
466 bool layout_font = true;
467 Pango::FontDescription fd = _layout->get_font_description();
468 if (fd.gobj() == NULL) {
470 fd = get_pango_context()->get_font_description();
472 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
474 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
475 ceil(char_pixel_height() * BASELINESTRETCH),
476 _text_width, _text_height,
477 get_width(), get_height(),
478 layout_font ? "L:" : "W:",
479 fd.to_string().c_str());
484 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
488 ArdourCanvas::set_source_rgba (cr, text_color);
489 const double text_ypos = (get_height() - _text_height) * .5;
491 if (_elements & Menu) {
492 // always left align (dropdown)
493 cairo_move_to (cr, text_margin, text_ypos);
494 pango_cairo_show_layout (cr, _layout->gobj());
495 } else if ( (_elements & Indicator) == Indicator) {
496 // left/right align depending on LED position
498 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
500 cairo_move_to (cr, text_margin, text_ypos);
502 pango_cairo_show_layout (cr, _layout->gobj());
504 /* centered text otherwise */
511 cairo_rotate(cr, _angle * M_PI / 180.0);
512 cairo_device_to_user(cr, &ww, &wh);
513 xa = (ww - _text_width) * _xalign;
514 ya = (wh - _text_height) * _yalign;
516 /* quick hack for left/bottom alignment at -90deg
517 * TODO this should be generalized incl rotation.
518 * currently only 'user' of this API is meter_strip.cc
520 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
522 cairo_move_to (cr, xa, ya);
523 pango_cairo_update_layout(cr, _layout->gobj());
524 pango_cairo_show_layout (cr, _layout->gobj());
531 if (_elements & Menu) {
532 const float trih = ceil(_diameter * .5);
533 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
535 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
536 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
537 cairo_rel_line_to(cr, -triw2, -trih);
538 cairo_rel_line_to(cr, 2. * triw2, 0);
539 cairo_close_path(cr);
541 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
544 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
545 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
546 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
547 cairo_close_path(cr);
548 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
549 cairo_set_line_width(cr, 1);
554 if (_elements & Indicator) {
557 /* move to the center of the indicator/led */
558 if (_elements & Text) {
559 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
561 cairo_translate (cr, led_xoff, get_height() * .5);
563 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
566 cairo_translate (cr, get_width() * .5, get_height() * .5);
570 if (!_flat_buttons) {
571 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
572 cairo_set_source (cr, led_inset_pattern);
577 cairo_set_source_rgb (cr, 0, 0, 0);
578 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
582 ArdourCanvas::set_source_rgba (cr, led_color);
583 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
589 // a transparent overlay to indicate insensitivity
590 if ((visual_state() & Gtkmm2ext::Insensitive)) {
591 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
592 uint32_t ins_color = ARDOUR_UI::config()->color ("gtk_background");
593 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
597 // if requested, show hovering
598 if (ARDOUR_UI::config()->get_widget_prelight()
599 && !((visual_state() & Gtkmm2ext::Insensitive))) {
601 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
602 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
607 //user is currently pressing the button. dark outline helps to indicate this
608 if (_grabbed && !(_elements & (Inactive|Menu))) {
609 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
610 cairo_set_line_width(cr, 2);
611 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
615 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
616 if (visual_state() & Gtkmm2ext::Selected) {
617 cairo_set_line_width(cr, 1);
618 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
619 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
623 //I guess this means we have keyboard focus. I don't think this works currently
625 //A: yes, it's keyboard focus and it does work when there's no editor window
626 // (the editor is always the first receiver for KeyDown).
627 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
629 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
630 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
632 cairo_set_dash (cr, &dashes, 1, 0);
633 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
634 cairo_set_line_width (cr, 1.0);
636 cairo_set_dash (cr, 0, 0, 0);
641 ArdourButton::set_corner_radius (float r)
644 CairoWidget::set_dirty ();
648 ArdourButton::on_realize()
650 CairoWidget::on_realize ();
652 if (_layout && _layout->get_text() != _text) {
653 _layout->set_text (_text);
659 ArdourButton::on_size_request (Gtk::Requisition* req)
661 req->width = req->height = 0;
662 CairoWidget::on_size_request (req);
664 if (_diameter == 0) {
665 const float newdia = rint (ARDOUR_UI::config()->get_font_scale () / 9600.0); // 11px with 100% font-scaling
666 if (_diameter != newdia) {
672 if ((_elements & Text) && !_text.empty()) {
673 // if _layout does not exist, char_pixel_height() creates it,
674 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
675 _layout->get_pixel_size (_text_width, _text_height);
676 req->width += rint(1.75 * char_pixel_width()); // padding
677 req->width += _text_width;
684 req->width += _pixbuf->get_width() + char_pixel_width();
685 req->height = std::max(req->height, _pixbuf->get_height() + 4);
688 if (_elements & Indicator) {
689 req->width += lrint (_diameter) + char_pixel_width();
690 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
693 if ((_elements & Menu)) {
694 req->width += _diameter + 4;
697 if (_elements & (RecButton | CloseCross | StripWidth | DinMidi)) {
698 assert(!(_elements & Text));
699 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
701 req->height = std::max(req->height, wh);
704 /* Tweaks to mess the nice stuff above up again. */
705 if (_tweaks & TrackHeader) {
706 // forget everything above and just use a fixed square [em] size
707 // "TrackHeader Buttons" are single letter (usually uppercase)
708 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
709 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
713 else if (_tweaks & Square) {
714 // currerntly unused (again)
715 if (req->width < req->height)
716 req->width = req->height;
717 if (req->height < req->width)
718 req->height = req->width;
719 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
720 // properly centered text for those elements that are centered
721 // (no sub-pixel offset)
722 if ((req->width - _text_width) & 1) { ++req->width; }
723 if ((req->height - _text_height) & 1) { ++req->height; }
726 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
731 * This sets the colors used for rendering based on the name of the button, and
732 * thus uses information from the GUI config data.
735 ArdourButton::set_colors ()
737 _update_colors = false;
738 if (_fixed_colors_set) {
741 std::string name = get_name();
744 fill_active_color = ARDOUR_UI::config()->color (string_compose ("%1: fill active", name), &failed);
746 fill_active_color = ARDOUR_UI::config()->color ("generic button: fill active");
748 fill_inactive_color = ARDOUR_UI::config()->color (string_compose ("%1: fill", name), &failed);
750 fill_inactive_color = ARDOUR_UI::config()->color ("generic button: fill");
753 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
754 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
756 led_active_color = ARDOUR_UI::config()->color (string_compose ("%1: led active", name), &failed);
758 led_active_color = ARDOUR_UI::config()->color ("generic button: led active");
761 /* The inactive color for the LED is just a fairly dark version of the
765 ArdourCanvas::HSV inactive (led_active_color);
768 led_inactive_color = inactive.color ();
772 * This sets the colors used for rendering based on two fixed values, rather
773 * than basing them on the button name, and thus information in the GUI config
776 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
778 _fixed_colors_set = true;
780 fill_active_color = color_active;
781 fill_inactive_color = color_inactive;
783 unsigned char r, g, b, a;
784 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
786 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
787 (max (double(g), 255.) - min (double(g), 255.)) +
788 (max (double(b), 255.) - min (double(b), 255.));
790 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
791 (max (double(g), 0.) - min (double(g), 0.)) +
792 (max (double(b), 0.) - min (double(b), 0.));
794 text_active_color = (white_contrast > black_contrast) ?
795 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
796 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
799 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
801 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
802 (max (double(g), 255.) - min (double(g), 255.)) +
803 (max (double(b), 255.) - min (double(b), 255.));
805 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
806 (max (double(g), 0.) - min (double(g), 0.)) +
807 (max (double(b), 0.) - min (double(b), 0.));
809 text_inactive_color = (white_contrast > black_contrast) ?
810 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
811 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
813 /* XXX what about led colors ? */
814 CairoWidget::set_dirty ();
818 ArdourButton::build_patterns ()
820 if (convex_pattern) {
821 cairo_pattern_destroy (convex_pattern);
825 if (concave_pattern) {
826 cairo_pattern_destroy (concave_pattern);
830 if (led_inset_pattern) {
831 cairo_pattern_destroy (led_inset_pattern);
832 led_inset_pattern = 0;
836 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
837 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
838 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
841 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
842 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
843 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
845 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
846 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
847 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
849 _pattern_height = get_height() ;
853 ArdourButton::set_led_left (bool yn)
859 ArdourButton::on_button_press_event (GdkEventButton *ev)
863 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
864 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
865 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
870 if (binding_proxy.button_press_handler (ev)) {
875 CairoWidget::set_dirty ();
877 if (ev->button == 1 && !_act_on_release) {
879 _action->activate ();
884 if (_fallthrough_to_parent)
891 ArdourButton::on_button_release_event (GdkEventButton *ev)
893 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
894 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
895 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
896 signal_led_clicked(); /* EMIT SIGNAL */
902 CairoWidget::set_dirty ();
904 if (ev->button == 1 && _hovering) {
906 if (_act_on_release) {
908 _action->activate ();
914 if (_fallthrough_to_parent)
921 ArdourButton::set_distinct_led_click (bool yn)
923 _distinct_led_click = yn;
928 ArdourButton::color_handler ()
930 _update_colors = true;
931 CairoWidget::set_dirty ();
935 ArdourButton::on_size_allocate (Allocation& alloc)
937 CairoWidget::on_size_allocate (alloc);
942 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
944 watch_connection.disconnect ();
945 binding_proxy.set_controllable (c);
949 ArdourButton::watch ()
951 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
954 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
957 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
961 ArdourButton::controllable_changed ()
963 float val = binding_proxy.get_controllable()->get_value();
965 if (fabs (val) >= 0.5f) {
966 set_active_state (Gtkmm2ext::ExplicitActive);
968 unset_active_state ();
970 CairoWidget::set_dirty ();
974 ArdourButton::set_related_action (RefPtr<Action> act)
976 Gtkmm2ext::Activatable::set_related_action (act);
980 action_tooltip_changed ();
982 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
985 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
988 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
989 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
990 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
995 ArdourButton::action_toggled ()
997 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1000 if (tact->get_active()) {
1001 set_active_state (Gtkmm2ext::ExplicitActive);
1003 unset_active_state ();
1009 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1011 _update_colors = true;
1012 CairoWidget::set_dirty ();
1016 ArdourButton::on_name_changed ()
1018 _char_pixel_width = 0;
1019 _char_pixel_height = 0;
1021 _update_colors = true;
1022 if (is_realized()) {
1028 ArdourButton::setup_led_rect ()
1030 if (!(_elements & Indicator)) {
1037 _led_rect = new cairo_rectangle_t;
1040 if (_elements & Text) {
1042 _led_rect->x = char_pixel_width();
1044 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1048 _led_rect->x = .5 * get_width() - _diameter;
1051 _led_rect->y = .5 * (get_height() - _diameter);
1052 _led_rect->width = _diameter;
1053 _led_rect->height = _diameter;
1057 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1060 if (is_realized()) {
1066 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1068 bool changed = (_active_state != s);
1069 CairoWidget::set_active_state (s);
1071 _update_colors = true;
1072 CairoWidget::set_dirty ();
1077 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1079 bool changed = (_visual_state != s);
1080 CairoWidget::set_visual_state (s);
1082 _update_colors = true;
1083 CairoWidget::set_dirty ();
1088 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1091 CairoWidget::set_dirty ();
1092 return CairoWidget::on_focus_in_event (ev);
1096 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1099 CairoWidget::set_dirty ();
1100 return CairoWidget::on_focus_out_event (ev);
1104 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1106 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1110 _action->activate ();
1114 return CairoWidget::on_key_release_event (ev);
1118 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1120 _hovering = (_elements & Inactive) ? false : true;
1122 if (ARDOUR_UI::config()->get_widget_prelight()) {
1123 CairoWidget::set_dirty ();
1126 return CairoWidget::on_enter_notify_event (ev);
1130 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1134 if (ARDOUR_UI::config()->get_widget_prelight()) {
1135 CairoWidget::set_dirty ();
1138 return CairoWidget::on_leave_notify_event (ev);
1142 ArdourButton::set_tweaks (Tweaks t)
1146 if (is_realized()) {
1153 ArdourButton::action_sensitivity_changed ()
1155 if (_action->property_sensitive ()) {
1156 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1158 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1163 ArdourButton::set_layout_ellipsize_width (int w)
1165 if (_layout_ellipsize_width == w) {
1168 _layout_ellipsize_width = w;
1172 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1173 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1175 if (is_realized ()) {
1181 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1183 if (_ellipsis == e) {
1190 _layout->set_ellipsize(_ellipsis);
1191 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1192 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1194 if (is_realized ()) {
1200 ArdourButton::ensure_layout ()
1204 _layout = Pango::Layout::create (get_pango_context());
1205 _layout->set_ellipsize(_ellipsis);
1206 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1207 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1213 ArdourButton::recalc_char_pixel_geometry ()
1215 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1219 // NB. this is not static, since the geometry is different
1220 // depending on the font used.
1222 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1223 _layout->set_text (x);
1224 _layout->get_pixel_size (w, h);
1225 _char_pixel_height = std::max(4, h);
1226 // number of actual chars in the string (not bytes)
1227 // Glib to the rescue.
1228 Glib::ustring gx(x);
1229 _char_avg_pixel_width = w / (float)gx.size();
1230 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1231 _layout->set_text (_text);
1235 ArdourButton::action_visibility_changed ()
1237 if (_action->property_visible ()) {
1245 ArdourButton::action_tooltip_changed ()
1247 string str = _action->property_tooltip().get_value();
1248 ARDOUR_UI::instance()->set_tip (*this, str);
1252 ArdourButton::set_elements (Element e)
1255 CairoWidget::set_dirty ();
1259 ArdourButton::add_elements (Element e)
1261 _elements = (ArdourButton::Element) (_elements | e);
1262 CairoWidget::set_dirty ();
1266 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1268 if (led_custom_color == c && use_custom_led_color == useit) {
1272 led_custom_color = c;
1273 use_custom_led_color = useit;
1274 CairoWidget::set_dirty ();