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>
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);
350 double x = rint((get_width() - _pixbuf->get_width()) * .5);
351 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
352 #if 0 // DEBUG style (print on hover)
353 if (_hovering || (_elements & Inactive)) {
354 printf("%s: p:%dx%d (%dx%d)\n",
356 _pixbuf->get_width(), _pixbuf->get_height(),
357 get_width(), get_height());
360 if (_elements & Menu) {
361 //if this is a DropDown with an icon, then we need to
362 //move the icon left slightly to accomomodate the arrow
365 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
366 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
369 else /* VectorIcon, IconRenderCallback are exclusive to Pixbuf Icons */
370 if (_elements & (VectorIcon | IconRenderCallback)) {
371 int vw = get_width();
372 int vh = get_height();
373 if (_elements & Menu) {
376 if (_elements & VectorIcon) {
377 ArdourIcon::render (cr, _icon, vw, vh, active_state(), text_color);
380 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
382 _icon_render_cb (cr, vw, vh, text_color, _icon_render_cb_data);
387 const int text_margin = char_pixel_width();
389 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
391 #if 0 // DEBUG style (print on hover)
392 if (_hovering || (_elements & Inactive)) {
393 bool layout_font = true;
394 Pango::FontDescription fd = _layout->get_font_description();
395 if (fd.gobj() == NULL) {
397 fd = get_pango_context()->get_font_description();
399 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
401 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
402 ceil(char_pixel_height() * BASELINESTRETCH),
403 _text_width, _text_height,
404 get_width(), get_height(),
405 layout_font ? "L:" : "W:",
406 fd.to_string().c_str());
411 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
415 Gtkmm2ext::set_source_rgba (cr, text_color);
416 const double text_ypos = (get_height() - _text_height) * .5;
418 if (_elements & Menu) {
419 // always left align (dropdown)
420 cairo_move_to (cr, text_margin, text_ypos);
421 pango_cairo_show_layout (cr, _layout->gobj());
422 } else if ( (_elements & Indicator) == Indicator) {
423 // left/right align depending on LED position
425 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
427 cairo_move_to (cr, text_margin, text_ypos);
429 pango_cairo_show_layout (cr, _layout->gobj());
431 /* centered text otherwise */
438 cairo_get_matrix (cr, &m1);
439 cairo_matrix_t m2 = m1;
442 cairo_set_matrix (cr, &m2);
445 cairo_rotate(cr, _angle * M_PI / 180.0);
448 cairo_device_to_user(cr, &ww, &wh);
449 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
450 ya = (wh - _text_height) * _yalign;
452 /* quick hack for left/bottom alignment at -90deg
453 * TODO this should be generalized incl rotation.
454 * currently only 'user' of this API is meter_strip.cc
456 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
458 cairo_move_to (cr, xa + m1.x0, ya + m1.y0);
459 pango_cairo_update_layout(cr, _layout->gobj());
460 pango_cairo_show_layout (cr, _layout->gobj());
466 if (_elements & Menu) {
467 const float trih = ceil(_diameter * .5);
468 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
470 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
471 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
472 cairo_rel_line_to(cr, -triw2, -trih);
473 cairo_rel_line_to(cr, 2. * triw2, 0);
474 cairo_close_path(cr);
476 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
479 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
480 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
481 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
482 cairo_close_path(cr);
483 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
484 cairo_set_line_width(cr, 1);
489 if (_elements & Indicator) {
492 /* move to the center of the indicator/led */
493 if (_elements & Text) {
494 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
496 cairo_translate (cr, led_xoff, get_height() * .5);
498 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
501 cairo_translate (cr, get_width() * .5, get_height() * .5);
505 if (!_flat_buttons) {
506 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
507 cairo_set_source (cr, led_inset_pattern);
512 cairo_set_source_rgb (cr, 0, 0, 0);
513 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
517 Gtkmm2ext::set_source_rgba (cr, led_color);
518 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
524 // a transparent overlay to indicate insensitivity
525 if ((visual_state() & Gtkmm2ext::Insensitive)) {
526 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
527 uint32_t ins_color = UIConfigurationBase::instance().color ("gtk_background");
528 Gtkmm2ext::set_source_rgb_a (cr, ins_color, 0.6);
532 // if requested, show hovering
533 if (UIConfigurationBase::instance().get_widget_prelight()
534 && !((visual_state() & Gtkmm2ext::Insensitive))) {
536 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
537 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
542 //user is currently pressing the button. dark outline helps to indicate this
543 if (_grabbed && !(_elements & (Inactive|Menu))) {
544 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
545 cairo_set_line_width(cr, 2);
546 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
550 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
551 if (visual_state() & Gtkmm2ext::Selected) {
552 cairo_set_line_width(cr, 1);
553 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
554 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
558 //I guess this means we have keyboard focus. I don't think this works currently
560 //A: yes, it's keyboard focus and it does work when there's no editor window
561 // (the editor is always the first receiver for KeyDown).
562 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
564 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
565 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
567 cairo_set_dash (cr, &dashes, 1, 0);
568 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
569 cairo_set_line_width (cr, 1.0);
571 cairo_set_dash (cr, 0, 0, 0);
576 ArdourButton::set_corner_radius (float r)
579 CairoWidget::set_dirty ();
583 ArdourButton::on_realize()
585 CairoWidget::on_realize ();
588 if (_layout->get_text() != _text) {
589 set_text_internal ();
596 ArdourButton::on_size_request (Gtk::Requisition* req)
598 req->width = req->height = 0;
599 CairoWidget::on_size_request (req);
601 if (_diameter == 0) {
602 const float newdia = rintf (11.f * UIConfigurationBase::instance().get_ui_scale());
603 if (_diameter != newdia) {
609 if (_elements & Text) {
612 set_text_internal ();
614 /* render() needs the size of the displayed text */
615 _layout->get_pixel_size (_text_width, _text_height);
617 if (_tweaks & OccasionalText) {
619 /* size should not change based on presence or absence
623 } else if (_layout_ellipsize_width > 0 && _sizing_text.empty()) {
625 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
626 req->width += _layout_ellipsize_width / PANGO_SCALE;
628 } else /*if (!_text.empty() || !_sizing_text.empty()) */ {
630 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
631 req->width += rint(1.75 * char_pixel_width()); // padding
633 if (!_sizing_text.empty()) {
634 _layout->set_text (_sizing_text); /* use sizing text */
637 int sizing_text_width = 0, sizing_text_height = 0;
638 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
640 req->width += sizing_text_width;
642 if (!_sizing_text.empty()) {
643 set_text_internal (); /* restore display text */
647 /* XXX hack (surprise). Deal with two common rotation angles */
649 if (_angle == 90 || _angle == 270) {
650 /* do not swap text width or height because we rely on
651 these being the un-rotated values in ::render()
653 swap (req->width, req->height);
662 req->width += _pixbuf->get_width() + char_pixel_width();
663 req->height = std::max(req->height, _pixbuf->get_height() + 4);
666 if (_elements & Indicator) {
667 req->width += lrint (_diameter) + char_pixel_width();
668 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
671 if ((_elements & Menu)) {
672 req->width += _diameter + 4;
675 if (_elements & (VectorIcon | IconRenderCallback)) {
676 assert(!(_elements & Text));
677 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
679 req->height = std::max(req->height, wh);
682 /* Tweaks to mess the nice stuff above up again. */
683 if (_tweaks & TrackHeader) {
684 // forget everything above and just use a fixed square [em] size
685 // "TrackHeader Buttons" are single letter (usually uppercase)
686 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
687 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
691 else if (_tweaks & Square) {
692 // currerntly unused (again)
693 if (req->width < req->height)
694 req->width = req->height;
695 if (req->height < req->width)
696 req->height = req->width;
697 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
698 // properly centered text for those elements that are centered
699 // (no sub-pixel offset)
700 if ((req->width - _text_width) & 1) { ++req->width; }
701 if ((req->height - _text_height) & 1) { ++req->height; }
704 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
709 * This sets the colors used for rendering based on the name of the button, and
710 * thus uses information from the GUI config data.
713 ArdourButton::set_colors ()
715 _update_colors = false;
717 if (_fixed_colors_set == 0x3) {
721 std::string name = get_name();
724 if (!(_fixed_colors_set & 0x1)) {
725 fill_active_color = UIConfigurationBase::instance().color (string_compose ("%1: fill active", name), &failed);
727 fill_active_color = UIConfigurationBase::instance().color ("generic button: fill active");
731 if (!(_fixed_colors_set & 0x2)) {
732 fill_inactive_color = UIConfigurationBase::instance().color (string_compose ("%1: fill", name), &failed);
734 fill_inactive_color = UIConfigurationBase::instance().color ("generic button: fill");
738 text_active_color = Gtkmm2ext::contrasting_text_color (fill_active_color);
739 text_inactive_color = Gtkmm2ext::contrasting_text_color (fill_inactive_color);
741 led_active_color = UIConfigurationBase::instance().color (string_compose ("%1: led active", name), &failed);
743 led_active_color = UIConfigurationBase::instance().color ("generic button: led active");
746 /* The inactive color for the LED is just a fairly dark version of the
750 Gtkmm2ext::HSV inactive (led_active_color);
753 led_inactive_color = inactive.color ();
757 * This sets the colors used for rendering based on two fixed values, rather
758 * than basing them on the button name, and thus information in the GUI config
761 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
763 set_active_color (color_active);
764 set_inactive_color (color_inactive);
767 void ArdourButton::set_active_color (const uint32_t color)
769 _fixed_colors_set |= 0x1;
771 fill_active_color = color;
773 unsigned char r, g, b, a;
774 UINT_TO_RGBA(color, &r, &g, &b, &a);
776 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
777 (max (double(g), 255.) - min (double(g), 255.)) +
778 (max (double(b), 255.) - min (double(b), 255.));
780 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
781 (max (double(g), 0.) - min (double(g), 0.)) +
782 (max (double(b), 0.) - min (double(b), 0.));
784 text_active_color = (white_contrast > black_contrast) ?
785 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
786 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
788 /* XXX what about led colors ? */
789 CairoWidget::set_dirty ();
792 void ArdourButton::set_inactive_color (const uint32_t color)
794 _fixed_colors_set |= 0x2;
796 fill_inactive_color = color;
798 unsigned char r, g, b, a;
799 UINT_TO_RGBA(color, &r, &g, &b, &a);
801 double 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 double 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)
861 focus_handler (this);
863 if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
864 return _fallthrough_to_parent ? false : true;
867 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
868 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
869 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
874 if (binding_proxy.button_press_handler (ev)) {
879 CairoWidget::set_dirty ();
881 if (ev->button == 1 && !_act_on_release) {
883 _action->activate ();
885 } else if (_auto_toggle) {
886 set_active (!get_active ());
892 return _fallthrough_to_parent ? false : true;
896 ArdourButton::on_button_release_event (GdkEventButton *ev)
898 if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
899 return _fallthrough_to_parent ? false : true;
901 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
902 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
903 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
904 signal_led_clicked(ev); /* EMIT SIGNAL */
910 CairoWidget::set_dirty ();
912 if (ev->button == 1 && _hovering) {
913 if (_act_on_release && _auto_toggle && !_action) {
914 set_active (!get_active ());
917 if (_act_on_release) {
919 _action->activate ();
925 return _fallthrough_to_parent ? false : true;
929 ArdourButton::set_distinct_led_click (bool yn)
931 _distinct_led_click = yn;
936 ArdourButton::color_handler ()
938 _update_colors = true;
939 CairoWidget::set_dirty ();
943 ArdourButton::on_size_allocate (Allocation& alloc)
945 CairoWidget::on_size_allocate (alloc);
950 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
952 watch_connection.disconnect ();
953 binding_proxy.set_controllable (c);
957 ArdourButton::watch ()
959 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
962 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
965 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
969 ArdourButton::controllable_changed ()
971 float val = binding_proxy.get_controllable()->get_value();
973 if (fabs (val) >= 0.5f) {
974 set_active_state (Gtkmm2ext::ExplicitActive);
976 unset_active_state ();
978 CairoWidget::set_dirty ();
982 ArdourButton::set_related_action (RefPtr<Action> act)
984 Gtkmm2ext::Activatable::set_related_action (act);
988 action_tooltip_changed ();
989 action_sensitivity_changed ();
991 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
994 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
997 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
998 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
999 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
1004 ArdourButton::action_toggled ()
1006 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1009 if (tact->get_active()) {
1010 set_active_state (Gtkmm2ext::ExplicitActive);
1012 unset_active_state ();
1018 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1020 _update_colors = true;
1021 CairoWidget::set_dirty ();
1022 _char_pixel_width = 0;
1023 _char_pixel_height = 0;
1024 if (is_realized()) {
1030 ArdourButton::on_name_changed ()
1032 _char_pixel_width = 0;
1033 _char_pixel_height = 0;
1035 _update_colors = true;
1036 if (is_realized()) {
1042 ArdourButton::setup_led_rect ()
1044 if (!(_elements & Indicator)) {
1051 _led_rect = new cairo_rectangle_t;
1054 if (_elements & Text) {
1056 _led_rect->x = char_pixel_width();
1058 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1062 _led_rect->x = .5 * get_width() - _diameter;
1065 _led_rect->y = .5 * (get_height() - _diameter);
1066 _led_rect->width = _diameter;
1067 _led_rect->height = _diameter;
1071 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1073 _elements = (ArdourButton::Element) (_elements & ~ArdourButton::Text);
1075 if (is_realized()) {
1081 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1083 bool changed = (_active_state != s);
1084 CairoWidget::set_active_state (s);
1086 _update_colors = true;
1087 CairoWidget::set_dirty ();
1092 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1094 bool changed = (_visual_state != s);
1095 CairoWidget::set_visual_state (s);
1097 _update_colors = true;
1098 CairoWidget::set_dirty ();
1103 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1106 CairoWidget::set_dirty ();
1107 return CairoWidget::on_focus_in_event (ev);
1111 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1114 CairoWidget::set_dirty ();
1115 return CairoWidget::on_focus_out_event (ev);
1119 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1120 if (_act_on_release && _focused &&
1121 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1123 if (_auto_toggle && !_action) {
1124 set_active (!get_active ());
1128 _action->activate ();
1132 return CairoWidget::on_key_release_event (ev);
1136 ArdourButton::on_key_press_event (GdkEventKey *ev) {
1137 if (!_act_on_release && _focused &&
1138 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1140 if (_auto_toggle && !_action) {
1141 set_active (!get_active ());
1145 _action->activate ();
1149 return CairoWidget::on_key_release_event (ev);
1153 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1155 _hovering = (_elements & Inactive) ? false : true;
1157 if (UIConfigurationBase::instance().get_widget_prelight()) {
1158 CairoWidget::set_dirty ();
1161 boost::shared_ptr<PBD::Controllable> c (binding_proxy.get_controllable ());
1163 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> (c));
1166 return CairoWidget::on_enter_notify_event (ev);
1170 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1174 if (UIConfigurationBase::instance().get_widget_prelight()) {
1175 CairoWidget::set_dirty ();
1178 if (binding_proxy.get_controllable()) {
1179 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> ());
1182 return CairoWidget::on_leave_notify_event (ev);
1186 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1187 /* Our implicit grab due to a button_press was broken by another grab:
1188 * the button will not get any button_release event if the mouse leaves
1189 * while the grab is taken, so unpress ourselves */
1191 CairoWidget::set_dirty ();
1196 ArdourButton::set_tweaks (Tweaks t)
1200 if (is_realized()) {
1207 ArdourButton::action_sensitivity_changed ()
1209 if (_action->property_sensitive ()) {
1210 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1212 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1217 ArdourButton::set_layout_ellipsize_width (int w)
1219 if (_layout_ellipsize_width == w) {
1222 _layout_ellipsize_width = w;
1226 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1227 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1229 if (is_realized ()) {
1235 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1237 if (_ellipsis == e) {
1244 _layout->set_ellipsize(_ellipsis);
1245 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1246 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1248 if (is_realized ()) {
1254 ArdourButton::ensure_layout ()
1258 _layout = Pango::Layout::create (get_pango_context());
1259 _layout->set_ellipsize(_ellipsis);
1260 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1261 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1267 ArdourButton::recalc_char_pixel_geometry ()
1269 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1273 // NB. this is not static, since the geometry is different
1274 // depending on the font used.
1276 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1277 _layout->set_text (x);
1278 _layout->get_pixel_size (w, h);
1279 _char_pixel_height = std::max(4, h);
1280 // number of actual chars in the string (not bytes)
1281 // Glib to the rescue.
1282 Glib::ustring gx(x);
1283 _char_avg_pixel_width = w / (float)gx.size();
1284 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1285 set_text_internal (); /* restore display text */
1289 ArdourButton::action_visibility_changed ()
1291 if (_action->property_visible ()) {
1299 ArdourButton::action_tooltip_changed ()
1301 string str = _action->property_tooltip().get_value();
1302 set_tooltip (*this, str);
1306 ArdourButton::set_elements (Element e)
1309 CairoWidget::set_dirty ();
1313 ArdourButton::add_elements (Element e)
1315 _elements = (ArdourButton::Element) (_elements | e);
1316 CairoWidget::set_dirty ();
1320 ArdourButton::set_icon (ArdourIcon::Icon i)
1323 _icon_render_cb = 0;
1324 _icon_render_cb_data = 0;
1325 _elements = (ArdourButton::Element) ((_elements | VectorIcon) & ~(ArdourButton::Text | IconRenderCallback));
1326 CairoWidget::set_dirty ();
1330 ArdourButton::set_icon (rendercallback_t cb, void* d)
1333 _elements = (ArdourButton::Element) ((_elements | ArdourButton::Text) & ~(IconRenderCallback | VectorIcon));
1334 _icon_render_cb = 0;
1335 _icon_render_cb_data = 0;
1337 _elements = (ArdourButton::Element) ((_elements | IconRenderCallback) & ~(ArdourButton::Text | VectorIcon));
1338 _icon_render_cb = cb;
1339 _icon_render_cb_data = d;
1341 CairoWidget::set_dirty ();
1345 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1347 if (led_custom_color == c && use_custom_led_color == useit) {
1351 led_custom_color = c;
1352 use_custom_led_color = useit;
1353 CairoWidget::set_dirty ();