2 * Copyright (C) 2010 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2017-2018 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <pangomm/layout.h>
25 #include <gtkmm/toggleaction.h>
27 #include "pbd/compose.h"
28 #include "pbd/controllable.h"
29 #include "pbd/error.h"
30 #include "pbd/stacktrace.h"
32 #include "gtkmm2ext/colors.h"
33 #include "gtkmm2ext/gui_thread.h"
34 #include "gtkmm2ext/rgb_macros.h"
35 #include "gtkmm2ext/utils.h"
37 #include "widgets/ardour_button.h"
38 #include "widgets/tooltips.h"
39 #include "widgets/ui_config.h"
43 #define BASELINESTRETCH (1.25)
44 #define TRACKHEADERBTNW (3.10)
49 using namespace ArdourWidgets;
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);
58 ArdourButton::ArdourButton (Element e, bool toggle)
62 , _icon (ArdourIcon::NoIcon)
64 , _icon_render_cb_data (0)
65 , _tweaks (Tweaks (0))
66 , _char_pixel_width (0)
67 , _char_pixel_height (0)
68 , _char_avg_pixel_width (0)
72 , _corner_radius (3.5)
77 , fill_inactive_color (0)
78 , fill_active_color (0)
79 , text_active_color(0)
80 , text_inactive_color(0)
82 , led_inactive_color(0)
83 , led_custom_color (0)
84 , use_custom_led_color (false)
87 , led_inset_pattern (0)
89 , _act_on_release (true)
90 , _auto_toggle (toggle)
92 , _distinct_led_click (false)
95 , _fixed_colors_set (false)
96 , _fallthrough_to_parent (false)
97 , _layout_ellipsize_width (-1)
98 , _ellipsis (Pango::ELLIPSIZE_NONE)
99 , _update_colors (true)
100 , _pattern_height (0)
102 UIConfigurationBase::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
103 /* This is not provided by gtkmm */
104 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
107 ArdourButton::ArdourButton (const std::string& str, Element e, bool toggle)
111 , _icon (ArdourIcon::NoIcon)
112 , _tweaks (Tweaks (0))
113 , _char_pixel_width (0)
114 , _char_pixel_height (0)
115 , _char_avg_pixel_width (0)
119 , _corner_radius (3.5)
124 , fill_inactive_color (0)
125 , fill_active_color (0)
126 , text_active_color(0)
127 , text_inactive_color(0)
128 , led_active_color(0)
129 , led_inactive_color(0)
130 , led_custom_color (0)
131 , use_custom_led_color (false)
133 , concave_pattern (0)
134 , led_inset_pattern (0)
136 , _act_on_release (true)
137 , _auto_toggle (toggle)
139 , _distinct_led_click (false)
142 , _fixed_colors_set (false)
143 , _fallthrough_to_parent (false)
144 , _layout_ellipsize_width (-1)
145 , _ellipsis (Pango::ELLIPSIZE_NONE)
146 , _update_colors (true)
147 , _pattern_height (0)
150 UIConfigurationBase::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
151 UIConfigurationBase::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
152 /* This is not provided by gtkmm */
153 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
156 ArdourButton::~ArdourButton()
160 if (convex_pattern) {
161 cairo_pattern_destroy (convex_pattern);
164 if (concave_pattern) {
165 cairo_pattern_destroy (concave_pattern);
168 if (led_inset_pattern) {
169 cairo_pattern_destroy (led_inset_pattern);
174 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
178 _layout->set_font_description (fd);
180 _char_pixel_width = 0;
181 _char_pixel_height = 0;
186 ArdourButton::set_text_internal () {
189 _layout->set_markup (_text);
191 _layout->set_text (_text);
196 ArdourButton::set_text (const std::string& str, bool markup)
198 if (!(_elements & Text)) {
201 if (_text == str && _markup == markup) {
207 if (!is_realized()) {
211 if (_layout && _layout->get_text() != _text) {
212 set_text_internal ();
213 /* on_size_request() will fill in _text_width/height
214 * so queue it even if _sizing_text != "" */
220 ArdourButton::set_sizing_text (const std::string& str)
222 if (_sizing_text == str) {
230 ArdourButton::set_angle (const double angle)
236 ArdourButton::set_alignment (const float xa, const float ya)
243 /* TODO make this a dedicated function elsewhere.
246 * virtual ArdourButton::render_vector_icon()
247 * ArdourIconButton::render_vector_icon
250 * ARDOUR_UI_UTILS::render_vector_icon()
253 ArdourButton::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
255 cairo_t* cr = ctx->cobj();
260 const float corner_radius = _boxy_buttons ? 0 : std::max(2.f, _corner_radius * UIConfigurationBase::instance().get_ui_scale());
262 if (_update_colors) {
265 if (get_height() != _pattern_height) {
269 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
270 text_color = text_active_color;
271 led_color = led_active_color;
273 text_color = text_inactive_color;
274 led_color = led_inactive_color;
277 if (use_custom_led_color) {
278 led_color = led_custom_color;
281 void (*rounded_function)(cairo_t*, double, double, double, double, double);
283 switch (_corner_mask) {
284 case 0x1: /* upper left only */
285 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
287 case 0x2: /* upper right only */
288 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
290 case 0x3: /* upper only */
291 rounded_function = Gtkmm2ext::rounded_top_rectangle;
293 /* should really have functions for lower right, lower left,
294 lower only, but for now, we don't
297 rounded_function = Gtkmm2ext::rounded_rectangle;
300 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
301 if ((_elements & (Body|Edge)) == (Body|Edge)) {
302 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
303 cairo_set_source_rgba (cr, 0, 0, 0, 1);
308 if ((_elements & Body)==Body) {
309 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
310 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
311 Gtkmm2ext::set_source_rgba (cr, fill_inactive_color);
313 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
315 Gtkmm2ext::set_source_rgba (cr, fill_active_color);
317 } else { //inactive, or it has an indicator
319 Gtkmm2ext::set_source_rgba (cr, fill_inactive_color);
324 // IMPLICIT ACTIVE: draw a border of the active color
325 if ((_elements & Body)==Body) {
326 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
327 cairo_set_line_width (cr, 2.0);
328 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
329 Gtkmm2ext::set_source_rgba (cr, fill_active_color);
334 //show the "convex" or "concave" gradient
335 if (!_flat_buttons && (_elements & Body)==Body) {
336 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
338 cairo_set_source (cr, concave_pattern);
339 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
342 cairo_set_source (cr, convex_pattern);
343 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
348 const int text_margin = char_pixel_width();
352 double x = rint((get_width() - _pixbuf->get_width()) * .5);
353 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
354 #if 0 // DEBUG style (print on hover)
355 if (_hovering || (_elements & Inactive)) {
356 printf("%s: p:%dx%d (%dx%d)\n",
358 _pixbuf->get_width(), _pixbuf->get_height(),
359 get_width(), get_height());
362 if (_elements & Menu) {
363 //if this is a DropDown with an icon, then we need to
364 //move the icon left slightly to accomomodate the arrow
367 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
368 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
371 else /* VectorIcon, IconRenderCallback are exclusive to Pixbuf Icons */
372 if (_elements & (VectorIcon | IconRenderCallback)) {
373 int vw = get_width();
374 int vh = get_height();
377 if (_elements & Menu) {
380 if (_elements & Indicator) {
381 vw -= _diameter + .5 * text_margin;
383 cairo_translate (cr, _diameter + text_margin, 0);
386 if (_elements & Text) {
387 vw -= _text_width + text_margin;
389 if (_elements & VectorIcon) {
390 ArdourIcon::render (cr, _icon, vw, vh, active_state(), text_color);
392 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
394 _icon_render_cb (cr, vw, vh, text_color, _icon_render_cb_data);
400 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
402 #if 0 // DEBUG style (print on hover)
403 if (_hovering || (_elements & Inactive)) {
404 bool layout_font = true;
405 Pango::FontDescription fd = _layout->get_font_description();
406 if (fd.gobj() == NULL) {
408 fd = get_pango_context()->get_font_description();
410 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
412 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
413 ceil(char_pixel_height() * BASELINESTRETCH),
414 _text_width, _text_height,
415 get_width(), get_height(),
416 layout_font ? "L:" : "W:",
417 fd.to_string().c_str());
422 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
426 Gtkmm2ext::set_source_rgba (cr, text_color);
427 const double text_ypos = (get_height() - _text_height) * .5;
429 if (_elements & Menu) {
430 // always left align (dropdown)
431 cairo_move_to (cr, text_margin, text_ypos);
432 pango_cairo_show_layout (cr, _layout->gobj());
433 } else if ( (_elements & Indicator) == Indicator) {
434 // left/right align depending on LED position
436 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
438 cairo_move_to (cr, text_margin, text_ypos);
440 pango_cairo_show_layout (cr, _layout->gobj());
441 } else if (VectorIcon == (_elements & VectorIcon)) {
442 cairo_move_to (cr, get_width () - text_margin - _text_width, text_ypos);
443 pango_cairo_show_layout (cr, _layout->gobj());
445 /* centered text otherwise */
452 cairo_get_matrix (cr, &m1);
453 cairo_matrix_t m2 = m1;
456 cairo_set_matrix (cr, &m2);
459 cairo_rotate(cr, _angle * M_PI / 180.0);
462 cairo_device_to_user(cr, &ww, &wh);
463 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
464 ya = (wh - _text_height) * _yalign;
466 /* quick hack for left/bottom alignment at -90deg
467 * TODO this should be generalized incl rotation.
468 * currently only 'user' of this API is meter_strip.cc
470 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
472 cairo_move_to (cr, xa + m1.x0, ya + m1.y0);
473 pango_cairo_update_layout(cr, _layout->gobj());
474 pango_cairo_show_layout (cr, _layout->gobj());
480 if (_elements & Menu) {
481 const float trih = ceil(_diameter * .5);
482 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
484 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
485 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
486 cairo_rel_line_to(cr, -triw2, -trih);
487 cairo_rel_line_to(cr, 2. * triw2, 0);
488 cairo_close_path(cr);
490 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
493 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
494 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
495 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
496 cairo_close_path(cr);
497 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
498 cairo_set_line_width(cr, 1);
503 if (_elements & Indicator) {
506 /* move to the center of the indicator/led */
507 if (_elements & (Text | VectorIcon | IconRenderCallback)) {
508 int led_xoff = ceil((char_pixel_width() + _diameter) * .5);
510 cairo_translate (cr, led_xoff, get_height() * .5);
512 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
515 cairo_translate (cr, get_width() * .5, get_height() * .5);
519 if (!_flat_buttons) {
520 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
521 cairo_set_source (cr, led_inset_pattern);
526 cairo_set_source_rgb (cr, 0, 0, 0);
527 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
531 Gtkmm2ext::set_source_rgba (cr, led_color);
532 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
538 // a transparent overlay to indicate insensitivity
539 if ((visual_state() & Gtkmm2ext::Insensitive)) {
540 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
541 uint32_t ins_color = UIConfigurationBase::instance().color ("gtk_background");
542 Gtkmm2ext::set_source_rgb_a (cr, ins_color, 0.6);
546 // if requested, show hovering
547 if (UIConfigurationBase::instance().get_widget_prelight()
548 && !((visual_state() & Gtkmm2ext::Insensitive))) {
550 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
551 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
556 //user is currently pressing the button. dark outline helps to indicate this
557 if (_grabbed && !(_elements & (Inactive|Menu))) {
558 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
559 cairo_set_line_width(cr, 2);
560 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
564 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
565 if (visual_state() & Gtkmm2ext::Selected) {
566 cairo_set_line_width(cr, 1);
567 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
568 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
572 //I guess this means we have keyboard focus. I don't think this works currently
574 //A: yes, it's keyboard focus and it does work when there's no editor window
575 // (the editor is always the first receiver for KeyDown).
576 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
578 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
579 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
581 cairo_set_dash (cr, &dashes, 1, 0);
582 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
583 cairo_set_line_width (cr, 1.0);
585 cairo_set_dash (cr, 0, 0, 0);
590 ArdourButton::set_corner_radius (float r)
593 CairoWidget::set_dirty ();
597 ArdourButton::on_realize()
599 CairoWidget::on_realize ();
602 if (_layout->get_text() != _text) {
603 set_text_internal ();
610 ArdourButton::on_size_request (Gtk::Requisition* req)
612 req->width = req->height = 0;
613 CairoWidget::on_size_request (req);
615 if (_diameter == 0) {
616 const float newdia = rintf (11.f * UIConfigurationBase::instance().get_ui_scale());
617 if (_diameter != newdia) {
623 if (_elements & Text) {
626 set_text_internal ();
628 /* render() needs the size of the displayed text */
629 _layout->get_pixel_size (_text_width, _text_height);
631 if (_tweaks & OccasionalText) {
633 /* size should not change based on presence or absence
637 } else if (_layout_ellipsize_width > 0 && _sizing_text.empty()) {
639 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
640 req->width += _layout_ellipsize_width / PANGO_SCALE;
642 } else /*if (!_text.empty() || !_sizing_text.empty()) */ {
644 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
645 req->width += rint(1.75 * char_pixel_width()); // padding
647 if (!_sizing_text.empty()) {
648 _layout->set_text (_sizing_text); /* use sizing text */
651 int sizing_text_width = 0, sizing_text_height = 0;
652 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
654 req->width += sizing_text_width;
656 if (!_sizing_text.empty()) {
657 set_text_internal (); /* restore display text */
661 /* XXX hack (surprise). Deal with two common rotation angles */
663 if (_angle == 90 || _angle == 270) {
664 /* do not swap text width or height because we rely on
665 these being the un-rotated values in ::render()
667 swap (req->width, req->height);
676 req->width += _pixbuf->get_width() + char_pixel_width();
677 req->height = std::max(req->height, _pixbuf->get_height() + 4);
680 if (_elements & Indicator) {
681 req->width += ceil (_diameter + char_pixel_width());
682 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
685 if ((_elements & Menu)) {
686 req->width += _diameter + 4;
689 if (_elements & (VectorIcon | IconRenderCallback)) {
690 const int wh = std::max (8., std::max (ceil (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
692 req->height = std::max(req->height, wh);
695 /* Tweaks to mess the nice stuff above up again. */
696 if (_tweaks & TrackHeader) {
697 // forget everything above and just use a fixed square [em] size
698 // "TrackHeader Buttons" are single letter (usually uppercase)
699 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
700 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
704 else if (_tweaks & Square) {
705 // currerntly unused (again)
706 if (req->width < req->height)
707 req->width = req->height;
708 if (req->height < req->width)
709 req->height = req->width;
710 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
711 // properly centered text for those elements that are centered
712 // (no sub-pixel offset)
713 if ((req->width - _text_width) & 1) { ++req->width; }
714 if ((req->height - _text_height) & 1) { ++req->height; }
717 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
722 * This sets the colors used for rendering based on the name of the button, and
723 * thus uses information from the GUI config data.
726 ArdourButton::set_colors ()
728 _update_colors = false;
730 if (_fixed_colors_set == 0x3) {
734 std::string name = get_name();
737 if (!(_fixed_colors_set & 0x1)) {
738 fill_active_color = UIConfigurationBase::instance().color (string_compose ("%1: fill active", name), &failed);
740 fill_active_color = UIConfigurationBase::instance().color ("generic button: fill active");
744 if (!(_fixed_colors_set & 0x2)) {
745 fill_inactive_color = UIConfigurationBase::instance().color (string_compose ("%1: fill", name), &failed);
747 fill_inactive_color = UIConfigurationBase::instance().color ("generic button: fill");
751 text_active_color = Gtkmm2ext::contrasting_text_color (fill_active_color);
752 text_inactive_color = Gtkmm2ext::contrasting_text_color (fill_inactive_color);
754 led_active_color = UIConfigurationBase::instance().color (string_compose ("%1: led active", name), &failed);
756 led_active_color = UIConfigurationBase::instance().color ("generic button: led active");
759 /* The inactive color for the LED is just a fairly dark version of the
763 Gtkmm2ext::HSV inactive (led_active_color);
766 led_inactive_color = inactive.color ();
770 * This sets the colors used for rendering based on two fixed values, rather
771 * than basing them on the button name, and thus information in the GUI config
774 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
776 set_active_color (color_active);
777 set_inactive_color (color_inactive);
780 void ArdourButton::set_active_color (const uint32_t color)
782 _fixed_colors_set |= 0x1;
784 fill_active_color = color;
786 unsigned char r, g, b, a;
787 UINT_TO_RGBA(color, &r, &g, &b, &a);
789 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
790 (max (double(g), 255.) - min (double(g), 255.)) +
791 (max (double(b), 255.) - min (double(b), 255.));
793 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
794 (max (double(g), 0.) - min (double(g), 0.)) +
795 (max (double(b), 0.) - min (double(b), 0.));
797 text_active_color = (white_contrast > black_contrast) ?
798 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
799 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
801 /* XXX what about led colors ? */
802 CairoWidget::set_dirty ();
805 void ArdourButton::set_inactive_color (const uint32_t color)
807 _fixed_colors_set |= 0x2;
809 fill_inactive_color = color;
811 unsigned char r, g, b, a;
812 UINT_TO_RGBA(color, &r, &g, &b, &a);
814 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
815 (max (double(g), 255.) - min (double(g), 255.)) +
816 (max (double(b), 255.) - min (double(b), 255.));
818 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
819 (max (double(g), 0.) - min (double(g), 0.)) +
820 (max (double(b), 0.) - min (double(b), 0.));
822 text_inactive_color = (white_contrast > black_contrast) ?
823 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
824 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
826 /* XXX what about led colors ? */
827 CairoWidget::set_dirty ();
830 void ArdourButton::reset_fixed_colors ()
832 if (_fixed_colors_set == 0) {
835 _fixed_colors_set = 0;
836 _update_colors = true;
837 CairoWidget::set_dirty ();
841 ArdourButton::build_patterns ()
843 if (convex_pattern) {
844 cairo_pattern_destroy (convex_pattern);
848 if (concave_pattern) {
849 cairo_pattern_destroy (concave_pattern);
853 if (led_inset_pattern) {
854 cairo_pattern_destroy (led_inset_pattern);
855 led_inset_pattern = 0;
859 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
860 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
861 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
864 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
865 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
866 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
868 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
869 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
870 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
872 _pattern_height = get_height() ;
876 ArdourButton::set_led_left (bool yn)
882 ArdourButton::on_button_press_event (GdkEventButton *ev)
884 focus_handler (this);
886 if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
887 return _fallthrough_to_parent ? false : true;
890 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
891 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
892 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
897 if (binding_proxy.button_press_handler (ev)) {
902 CairoWidget::set_dirty ();
904 if (ev->button == 1 && !_act_on_release) {
906 _action->activate ();
908 } else if (_auto_toggle) {
909 set_active (!get_active ());
915 return _fallthrough_to_parent ? false : true;
919 ArdourButton::on_button_release_event (GdkEventButton *ev)
921 if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
922 return _fallthrough_to_parent ? false : true;
924 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
925 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
926 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
927 signal_led_clicked(ev); /* EMIT SIGNAL */
933 CairoWidget::set_dirty ();
935 if (ev->button == 1 && _hovering) {
936 if (_act_on_release && _auto_toggle && !_action) {
937 set_active (!get_active ());
940 if (_act_on_release) {
942 _action->activate ();
948 return _fallthrough_to_parent ? false : true;
952 ArdourButton::set_distinct_led_click (bool yn)
954 _distinct_led_click = yn;
959 ArdourButton::color_handler ()
961 _update_colors = true;
962 CairoWidget::set_dirty ();
966 ArdourButton::on_size_allocate (Allocation& alloc)
968 CairoWidget::on_size_allocate (alloc);
973 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
975 watch_connection.disconnect ();
976 binding_proxy.set_controllable (c);
980 ArdourButton::watch ()
982 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
985 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
988 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
992 ArdourButton::controllable_changed ()
994 float val = binding_proxy.get_controllable()->get_value();
996 if (fabs (val) >= 0.5f) {
997 set_active_state (Gtkmm2ext::ExplicitActive);
999 unset_active_state ();
1001 CairoWidget::set_dirty ();
1005 ArdourButton::set_related_action (RefPtr<Action> act)
1007 Gtkmm2ext::Activatable::set_related_action (act);
1011 action_tooltip_changed ();
1012 action_sensitivity_changed ();
1014 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1017 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
1020 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
1021 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
1022 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
1027 ArdourButton::action_toggled ()
1029 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1032 if (tact->get_active()) {
1033 set_active_state (Gtkmm2ext::ExplicitActive);
1035 unset_active_state ();
1041 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1043 _update_colors = true;
1044 CairoWidget::set_dirty ();
1045 _char_pixel_width = 0;
1046 _char_pixel_height = 0;
1047 if (is_realized()) {
1053 ArdourButton::on_name_changed ()
1055 _char_pixel_width = 0;
1056 _char_pixel_height = 0;
1058 _update_colors = true;
1059 if (is_realized()) {
1065 ArdourButton::setup_led_rect ()
1067 if (!(_elements & Indicator)) {
1074 _led_rect = new cairo_rectangle_t;
1077 if (_elements & Text) {
1079 _led_rect->x = char_pixel_width();
1081 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1085 _led_rect->x = .5 * get_width() - _diameter;
1088 _led_rect->y = .5 * (get_height() - _diameter);
1089 _led_rect->width = _diameter;
1090 _led_rect->height = _diameter;
1094 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1096 _elements = (ArdourButton::Element) (_elements & ~ArdourButton::Text);
1098 if (is_realized()) {
1104 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1106 bool changed = (_active_state != s);
1107 CairoWidget::set_active_state (s);
1109 _update_colors = true;
1110 CairoWidget::set_dirty ();
1115 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1117 bool changed = (_visual_state != s);
1118 CairoWidget::set_visual_state (s);
1120 _update_colors = true;
1121 CairoWidget::set_dirty ();
1126 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1129 CairoWidget::set_dirty ();
1130 return CairoWidget::on_focus_in_event (ev);
1134 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1137 CairoWidget::set_dirty ();
1138 return CairoWidget::on_focus_out_event (ev);
1142 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1143 if (_act_on_release && _focused &&
1144 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1146 if (_auto_toggle && !_action) {
1147 set_active (!get_active ());
1151 _action->activate ();
1155 return CairoWidget::on_key_release_event (ev);
1159 ArdourButton::on_key_press_event (GdkEventKey *ev) {
1160 if (!_act_on_release && _focused &&
1161 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1163 if (_auto_toggle && !_action) {
1164 set_active (!get_active ());
1168 _action->activate ();
1172 return CairoWidget::on_key_release_event (ev);
1176 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1178 _hovering = (_elements & Inactive) ? false : true;
1180 if (UIConfigurationBase::instance().get_widget_prelight()) {
1181 CairoWidget::set_dirty ();
1184 boost::shared_ptr<PBD::Controllable> c (binding_proxy.get_controllable ());
1186 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> (c));
1189 return CairoWidget::on_enter_notify_event (ev);
1193 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1197 if (UIConfigurationBase::instance().get_widget_prelight()) {
1198 CairoWidget::set_dirty ();
1201 if (binding_proxy.get_controllable()) {
1202 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> ());
1205 return CairoWidget::on_leave_notify_event (ev);
1209 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1210 /* Our implicit grab due to a button_press was broken by another grab:
1211 * the button will not get any button_release event if the mouse leaves
1212 * while the grab is taken, so unpress ourselves */
1214 CairoWidget::set_dirty ();
1219 ArdourButton::set_tweaks (Tweaks t)
1223 if (is_realized()) {
1230 ArdourButton::action_sensitivity_changed ()
1232 if (_action->property_sensitive ()) {
1233 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1235 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1240 ArdourButton::set_layout_ellipsize_width (int w)
1242 if (_layout_ellipsize_width == w) {
1245 _layout_ellipsize_width = w;
1249 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1250 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1252 if (is_realized ()) {
1258 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1260 if (_ellipsis == e) {
1267 _layout->set_ellipsize(_ellipsis);
1268 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1269 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1271 if (is_realized ()) {
1277 ArdourButton::ensure_layout ()
1281 _layout = Pango::Layout::create (get_pango_context());
1282 _layout->set_ellipsize(_ellipsis);
1283 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1284 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1290 ArdourButton::recalc_char_pixel_geometry ()
1292 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1296 // NB. this is not static, since the geometry is different
1297 // depending on the font used.
1299 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1300 _layout->set_text (x);
1301 _layout->get_pixel_size (w, h);
1302 _char_pixel_height = std::max(4, h);
1303 // number of actual chars in the string (not bytes)
1304 // Glib to the rescue.
1305 Glib::ustring gx(x);
1306 _char_avg_pixel_width = w / (float)gx.size();
1307 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1308 set_text_internal (); /* restore display text */
1312 ArdourButton::action_visibility_changed ()
1314 if (_action->property_visible ()) {
1322 ArdourButton::action_tooltip_changed ()
1324 string str = _action->property_tooltip().get_value();
1325 set_tooltip (*this, str);
1329 ArdourButton::set_elements (Element e)
1332 CairoWidget::set_dirty ();
1336 ArdourButton::add_elements (Element e)
1338 _elements = (ArdourButton::Element) (_elements | e);
1339 CairoWidget::set_dirty ();
1343 ArdourButton::set_icon (ArdourIcon::Icon i)
1346 _icon_render_cb = 0;
1347 _icon_render_cb_data = 0;
1348 _elements = (ArdourButton::Element) ((_elements | VectorIcon) & ~(ArdourButton::Text | IconRenderCallback));
1349 CairoWidget::set_dirty ();
1353 ArdourButton::set_icon (rendercallback_t cb, void* d)
1356 _elements = (ArdourButton::Element) ((_elements | ArdourButton::Text) & ~(IconRenderCallback | VectorIcon));
1357 _icon_render_cb = 0;
1358 _icon_render_cb_data = 0;
1360 _elements = (ArdourButton::Element) ((_elements | IconRenderCallback) & ~(ArdourButton::Text | VectorIcon));
1361 _icon_render_cb = cb;
1362 _icon_render_cb_data = d;
1364 CairoWidget::set_dirty ();
1368 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1370 if (led_custom_color == c && use_custom_led_color == useit) {
1374 led_custom_color = c;
1375 use_custom_led_color = useit;
1376 CairoWidget::set_dirty ();