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/utils.h"
33 #include "gtkmm2ext/rgb_macros.h"
34 #include "gtkmm2ext/gui_thread.h"
36 #include "canvas/utils.h"
37 #include "canvas/colors.h"
39 #include "widgets/ardour_button.h"
40 #include "widgets/tooltips.h"
41 #include "widgets/ui_config.h"
45 #define BASELINESTRETCH (1.25)
46 #define TRACKHEADERBTNW (3.10)
51 using namespace ArdourWidgets;
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)
64 , _icon (ArdourIcon::NoIcon)
66 , _icon_render_cb_data (0)
67 , _tweaks (Tweaks (0))
68 , _char_pixel_width (0)
69 , _char_pixel_height (0)
70 , _char_avg_pixel_width (0)
74 , _corner_radius (3.5)
79 , fill_inactive_color (0)
80 , fill_active_color (0)
81 , text_active_color(0)
82 , text_inactive_color(0)
84 , led_inactive_color(0)
85 , led_custom_color (0)
86 , use_custom_led_color (false)
89 , led_inset_pattern (0)
91 , _act_on_release (true)
93 , _distinct_led_click (false)
96 , _fixed_colors_set (false)
97 , _fallthrough_to_parent (false)
98 , _layout_ellipsize_width (-1)
99 , _ellipsis (Pango::ELLIPSIZE_NONE)
100 , _update_colors (true)
101 , _pattern_height (0)
103 UIConfigurationBase::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
104 /* This is not provided by gtkmm */
105 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
108 ArdourButton::ArdourButton (const std::string& str, Element e)
112 , _icon (ArdourIcon::NoIcon)
113 , _tweaks (Tweaks (0))
114 , _char_pixel_width (0)
115 , _char_pixel_height (0)
116 , _char_avg_pixel_width (0)
120 , _corner_radius (3.5)
125 , fill_inactive_color (0)
126 , fill_active_color (0)
127 , text_active_color(0)
128 , text_inactive_color(0)
129 , led_active_color(0)
130 , led_inactive_color(0)
131 , led_custom_color (0)
132 , use_custom_led_color (false)
134 , concave_pattern (0)
135 , led_inset_pattern (0)
137 , _act_on_release (true)
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) {
226 if (!is_realized()) {
236 ArdourButton::set_angle (const double angle)
242 ArdourButton::set_alignment (const float xa, const float ya)
249 /* TODO make this a dedicated function elsewhere.
252 * virtual ArdourButton::render_vector_icon()
253 * ArdourIconButton::render_vector_icon
256 * ARDOUR_UI_UTILS::render_vector_icon()
259 ArdourButton::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
261 cairo_t* cr = ctx->cobj();
266 const float corner_radius = std::max(2.f, _corner_radius * UIConfigurationBase::instance().get_ui_scale());
268 if (_update_colors) {
271 if (get_height() != _pattern_height) {
275 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
276 text_color = text_active_color;
277 led_color = led_active_color;
279 text_color = text_inactive_color;
280 led_color = led_inactive_color;
283 if (use_custom_led_color) {
284 led_color = led_custom_color;
287 void (*rounded_function)(cairo_t*, double, double, double, double, double);
289 switch (_corner_mask) {
290 case 0x1: /* upper left only */
291 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
293 case 0x2: /* upper right only */
294 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
296 case 0x3: /* upper only */
297 rounded_function = Gtkmm2ext::rounded_top_rectangle;
299 /* should really have functions for lower right, lower left,
300 lower only, but for now, we don't
303 rounded_function = Gtkmm2ext::rounded_rectangle;
306 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
307 if ((_elements & (Body|Edge)) == (Body|Edge)) {
308 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
309 cairo_set_source_rgba (cr, 0, 0, 0, 1);
314 if ((_elements & Body)==Body) {
315 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
316 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
317 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
319 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
321 ArdourCanvas::set_source_rgba (cr, fill_active_color);
323 } else { //inactive, or it has an indicator
325 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
330 // IMPLICIT ACTIVE: draw a border of the active color
331 if ((_elements & Body)==Body) {
332 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
333 cairo_set_line_width (cr, 2.0);
334 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
335 ArdourCanvas::set_source_rgba (cr, fill_active_color);
340 //show the "convex" or "concave" gradient
341 if (!_flat_buttons && (_elements & Body)==Body) {
342 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
344 cairo_set_source (cr, concave_pattern);
345 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
348 cairo_set_source (cr, convex_pattern);
349 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
356 double x = rint((get_width() - _pixbuf->get_width()) * .5);
357 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
358 #if 0 // DEBUG style (print on hover)
359 if (_hovering || (_elements & Inactive)) {
360 printf("%s: p:%dx%d (%dx%d)\n",
362 _pixbuf->get_width(), _pixbuf->get_height(),
363 get_width(), get_height());
366 if (_elements & Menu) {
367 //if this is a DropDown with an icon, then we need to
368 //move the icon left slightly to accomomodate the arrow
371 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
372 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
375 else /* VectorIcon, IconRenderCallback are exclusive to Pixbuf Icons */
376 if (_elements & (VectorIcon | IconRenderCallback)) {
377 int vw = get_width();
378 int vh = get_height();
379 if (_elements & Menu) {
382 if (_elements & VectorIcon) {
383 ArdourIcon::render (cr, _icon, vw, vh, active_state(), text_color);
386 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
388 _icon_render_cb (cr, vw, vh, text_color, _icon_render_cb_data);
393 const int text_margin = char_pixel_width();
395 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
397 #if 0 // DEBUG style (print on hover)
398 if (_hovering || (_elements & Inactive)) {
399 bool layout_font = true;
400 Pango::FontDescription fd = _layout->get_font_description();
401 if (fd.gobj() == NULL) {
403 fd = get_pango_context()->get_font_description();
405 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
407 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
408 ceil(char_pixel_height() * BASELINESTRETCH),
409 _text_width, _text_height,
410 get_width(), get_height(),
411 layout_font ? "L:" : "W:",
412 fd.to_string().c_str());
417 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
421 ArdourCanvas::set_source_rgba (cr, text_color);
422 const double text_ypos = (get_height() - _text_height) * .5;
424 if (_elements & Menu) {
425 // always left align (dropdown)
426 cairo_move_to (cr, text_margin, text_ypos);
427 pango_cairo_show_layout (cr, _layout->gobj());
428 } else if ( (_elements & Indicator) == Indicator) {
429 // left/right align depending on LED position
431 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
433 cairo_move_to (cr, text_margin, text_ypos);
435 pango_cairo_show_layout (cr, _layout->gobj());
437 /* centered text otherwise */
444 cairo_get_matrix (cr, &m1);
445 cairo_matrix_t m2 = m1;
448 cairo_set_matrix (cr, &m2);
451 cairo_rotate(cr, _angle * M_PI / 180.0);
454 cairo_device_to_user(cr, &ww, &wh);
455 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
456 ya = (wh - _text_height) * _yalign;
458 /* quick hack for left/bottom alignment at -90deg
459 * TODO this should be generalized incl rotation.
460 * currently only 'user' of this API is meter_strip.cc
462 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
464 cairo_move_to (cr, xa + m1.x0, ya + m1.y0);
465 pango_cairo_update_layout(cr, _layout->gobj());
466 pango_cairo_show_layout (cr, _layout->gobj());
472 if (_elements & Menu) {
473 const float trih = ceil(_diameter * .5);
474 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
476 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
477 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
478 cairo_rel_line_to(cr, -triw2, -trih);
479 cairo_rel_line_to(cr, 2. * triw2, 0);
480 cairo_close_path(cr);
482 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, .5 - triw2, .5 - trih);
487 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
488 cairo_close_path(cr);
489 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
490 cairo_set_line_width(cr, 1);
495 if (_elements & Indicator) {
498 /* move to the center of the indicator/led */
499 if (_elements & Text) {
500 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
502 cairo_translate (cr, led_xoff, get_height() * .5);
504 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
507 cairo_translate (cr, get_width() * .5, get_height() * .5);
511 if (!_flat_buttons) {
512 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
513 cairo_set_source (cr, led_inset_pattern);
518 cairo_set_source_rgb (cr, 0, 0, 0);
519 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
523 ArdourCanvas::set_source_rgba (cr, led_color);
524 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
530 // a transparent overlay to indicate insensitivity
531 if ((visual_state() & Gtkmm2ext::Insensitive)) {
532 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
533 uint32_t ins_color = UIConfigurationBase::instance().color ("gtk_background");
534 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
538 // if requested, show hovering
539 if (UIConfigurationBase::instance().get_widget_prelight()
540 && !((visual_state() & Gtkmm2ext::Insensitive))) {
542 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
543 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
548 //user is currently pressing the button. dark outline helps to indicate this
549 if (_grabbed && !(_elements & (Inactive|Menu))) {
550 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
551 cairo_set_line_width(cr, 2);
552 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
556 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
557 if (visual_state() & Gtkmm2ext::Selected) {
558 cairo_set_line_width(cr, 1);
559 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
560 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
564 //I guess this means we have keyboard focus. I don't think this works currently
566 //A: yes, it's keyboard focus and it does work when there's no editor window
567 // (the editor is always the first receiver for KeyDown).
568 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
570 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
571 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
573 cairo_set_dash (cr, &dashes, 1, 0);
574 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
575 cairo_set_line_width (cr, 1.0);
577 cairo_set_dash (cr, 0, 0, 0);
582 ArdourButton::set_corner_radius (float r)
585 CairoWidget::set_dirty ();
589 ArdourButton::on_realize()
591 CairoWidget::on_realize ();
594 if (_layout->get_text() != _text) {
595 set_text_internal ();
602 ArdourButton::on_size_request (Gtk::Requisition* req)
604 req->width = req->height = 0;
605 CairoWidget::on_size_request (req);
607 if (_diameter == 0) {
608 const float newdia = rintf (11.f * UIConfigurationBase::instance().get_ui_scale());
609 if (_diameter != newdia) {
615 if (_elements & Text) {
618 set_text_internal ();
620 /* render() needs the size of the displayed text */
621 _layout->get_pixel_size (_text_width, _text_height);
623 if (_tweaks & OccasionalText) {
625 /* size should not change based on presence or absence
629 } else /*if (!_text.empty() || !_sizing_text.empty()) */ {
631 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
632 req->width += rint(1.75 * char_pixel_width()); // padding
634 if (!_sizing_text.empty()) {
635 _layout->set_text (_sizing_text); /* use sizing text */
638 int sizing_text_width = 0, sizing_text_height = 0;
639 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
641 req->width += sizing_text_width;
643 if (!_sizing_text.empty()) {
644 set_text_internal (); /* restore display text */
648 /* XXX hack (surprise). Deal with two common rotation angles */
650 if (_angle == 90 || _angle == 270) {
651 /* do not swap text width or height because we rely on
652 these being the un-rotated values in ::render()
654 swap (req->width, req->height);
663 req->width += _pixbuf->get_width() + char_pixel_width();
664 req->height = std::max(req->height, _pixbuf->get_height() + 4);
667 if (_elements & Indicator) {
668 req->width += lrint (_diameter) + char_pixel_width();
669 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
672 if ((_elements & Menu)) {
673 req->width += _diameter + 4;
676 if (_elements & (VectorIcon | IconRenderCallback)) {
677 assert(!(_elements & Text));
678 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
680 req->height = std::max(req->height, wh);
683 /* Tweaks to mess the nice stuff above up again. */
684 if (_tweaks & TrackHeader) {
685 // forget everything above and just use a fixed square [em] size
686 // "TrackHeader Buttons" are single letter (usually uppercase)
687 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
688 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
692 else if (_tweaks & Square) {
693 // currerntly unused (again)
694 if (req->width < req->height)
695 req->width = req->height;
696 if (req->height < req->width)
697 req->height = req->width;
698 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
699 // properly centered text for those elements that are centered
700 // (no sub-pixel offset)
701 if ((req->width - _text_width) & 1) { ++req->width; }
702 if ((req->height - _text_height) & 1) { ++req->height; }
705 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
710 * This sets the colors used for rendering based on the name of the button, and
711 * thus uses information from the GUI config data.
714 ArdourButton::set_colors ()
716 _update_colors = false;
718 if (_fixed_colors_set == 0x3) {
722 std::string name = get_name();
725 if (!(_fixed_colors_set & 0x1)) {
726 fill_active_color = UIConfigurationBase::instance().color (string_compose ("%1: fill active", name), &failed);
728 fill_active_color = UIConfigurationBase::instance().color ("generic button: fill active");
732 if (!(_fixed_colors_set & 0x2)) {
733 fill_inactive_color = UIConfigurationBase::instance().color (string_compose ("%1: fill", name), &failed);
735 fill_inactive_color = UIConfigurationBase::instance().color ("generic button: fill");
739 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
740 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
742 led_active_color = UIConfigurationBase::instance().color (string_compose ("%1: led active", name), &failed);
744 led_active_color = UIConfigurationBase::instance().color ("generic button: led active");
747 /* The inactive color for the LED is just a fairly dark version of the
751 ArdourCanvas::HSV inactive (led_active_color);
754 led_inactive_color = inactive.color ();
758 * This sets the colors used for rendering based on two fixed values, rather
759 * than basing them on the button name, and thus information in the GUI config
762 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
764 set_active_color (color_active);
765 set_inactive_color (color_inactive);
768 void ArdourButton::set_active_color (const uint32_t color)
770 _fixed_colors_set |= 0x1;
772 fill_active_color = color;
774 unsigned char r, g, b, a;
775 UINT_TO_RGBA(color, &r, &g, &b, &a);
777 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
778 (max (double(g), 255.) - min (double(g), 255.)) +
779 (max (double(b), 255.) - min (double(b), 255.));
781 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
782 (max (double(g), 0.) - min (double(g), 0.)) +
783 (max (double(b), 0.) - min (double(b), 0.));
785 text_active_color = (white_contrast > black_contrast) ?
786 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
787 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
789 /* XXX what about led colors ? */
790 CairoWidget::set_dirty ();
793 void ArdourButton::set_inactive_color (const uint32_t color)
795 _fixed_colors_set |= 0x2;
797 fill_inactive_color = color;
799 unsigned char r, g, b, a;
800 UINT_TO_RGBA(color, &r, &g, &b, &a);
802 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
803 (max (double(g), 255.) - min (double(g), 255.)) +
804 (max (double(b), 255.) - min (double(b), 255.));
806 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
807 (max (double(g), 0.) - min (double(g), 0.)) +
808 (max (double(b), 0.) - min (double(b), 0.));
810 text_inactive_color = (white_contrast > black_contrast) ?
811 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
812 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
814 /* XXX what about led colors ? */
815 CairoWidget::set_dirty ();
819 ArdourButton::build_patterns ()
821 if (convex_pattern) {
822 cairo_pattern_destroy (convex_pattern);
826 if (concave_pattern) {
827 cairo_pattern_destroy (concave_pattern);
831 if (led_inset_pattern) {
832 cairo_pattern_destroy (led_inset_pattern);
833 led_inset_pattern = 0;
837 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
838 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
839 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
842 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
843 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
844 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
846 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
847 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
848 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
850 _pattern_height = get_height() ;
854 ArdourButton::set_led_left (bool yn)
860 ArdourButton::on_button_press_event (GdkEventButton *ev)
862 focus_handler (this);
864 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
865 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
866 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
871 if (binding_proxy.button_press_handler (ev)) {
876 CairoWidget::set_dirty ();
878 if (ev->button == 1 && !_act_on_release) {
880 _action->activate ();
885 if (_fallthrough_to_parent)
892 ArdourButton::on_button_release_event (GdkEventButton *ev)
894 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
895 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
896 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
897 signal_led_clicked(ev); /* EMIT SIGNAL */
903 CairoWidget::set_dirty ();
905 if (ev->button == 1 && _hovering) {
907 if (_act_on_release) {
909 _action->activate ();
915 if (_fallthrough_to_parent)
922 ArdourButton::set_distinct_led_click (bool yn)
924 _distinct_led_click = yn;
929 ArdourButton::color_handler ()
931 _update_colors = true;
932 CairoWidget::set_dirty ();
936 ArdourButton::on_size_allocate (Allocation& alloc)
938 CairoWidget::on_size_allocate (alloc);
943 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
945 watch_connection.disconnect ();
946 binding_proxy.set_controllable (c);
950 ArdourButton::watch ()
952 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
955 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
958 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
962 ArdourButton::controllable_changed ()
964 float val = binding_proxy.get_controllable()->get_value();
966 if (fabs (val) >= 0.5f) {
967 set_active_state (Gtkmm2ext::ExplicitActive);
969 unset_active_state ();
971 CairoWidget::set_dirty ();
975 ArdourButton::set_related_action (RefPtr<Action> act)
977 Gtkmm2ext::Activatable::set_related_action (act);
981 action_tooltip_changed ();
982 action_sensitivity_changed ();
984 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
987 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
990 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
991 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
992 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
997 ArdourButton::action_toggled ()
999 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1002 if (tact->get_active()) {
1003 set_active_state (Gtkmm2ext::ExplicitActive);
1005 unset_active_state ();
1011 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1013 _update_colors = true;
1014 CairoWidget::set_dirty ();
1015 _char_pixel_width = 0;
1016 _char_pixel_height = 0;
1017 if (is_realized()) {
1023 ArdourButton::on_name_changed ()
1025 _char_pixel_width = 0;
1026 _char_pixel_height = 0;
1028 _update_colors = true;
1029 if (is_realized()) {
1035 ArdourButton::setup_led_rect ()
1037 if (!(_elements & Indicator)) {
1044 _led_rect = new cairo_rectangle_t;
1047 if (_elements & Text) {
1049 _led_rect->x = char_pixel_width();
1051 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1055 _led_rect->x = .5 * get_width() - _diameter;
1058 _led_rect->y = .5 * (get_height() - _diameter);
1059 _led_rect->width = _diameter;
1060 _led_rect->height = _diameter;
1064 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1066 _elements = (ArdourButton::Element) (_elements & ~ArdourButton::Text);
1068 if (is_realized()) {
1074 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1076 bool changed = (_active_state != s);
1077 CairoWidget::set_active_state (s);
1079 _update_colors = true;
1080 CairoWidget::set_dirty ();
1085 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1087 bool changed = (_visual_state != s);
1088 CairoWidget::set_visual_state (s);
1090 _update_colors = true;
1091 CairoWidget::set_dirty ();
1096 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1099 CairoWidget::set_dirty ();
1100 return CairoWidget::on_focus_in_event (ev);
1104 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1107 CairoWidget::set_dirty ();
1108 return CairoWidget::on_focus_out_event (ev);
1112 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1113 if (_act_on_release && _focused &&
1114 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1118 _action->activate ();
1122 return CairoWidget::on_key_release_event (ev);
1126 ArdourButton::on_key_press_event (GdkEventKey *ev) {
1127 if (!_act_on_release && _focused &&
1128 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1132 _action->activate ();
1136 return CairoWidget::on_key_release_event (ev);
1140 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1142 _hovering = (_elements & Inactive) ? false : true;
1144 if (UIConfigurationBase::instance().get_widget_prelight()) {
1145 CairoWidget::set_dirty ();
1148 boost::shared_ptr<PBD::Controllable> c (binding_proxy.get_controllable ());
1150 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> (c));
1153 return CairoWidget::on_enter_notify_event (ev);
1157 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1161 if (UIConfigurationBase::instance().get_widget_prelight()) {
1162 CairoWidget::set_dirty ();
1165 if (binding_proxy.get_controllable()) {
1166 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> ());
1169 return CairoWidget::on_leave_notify_event (ev);
1173 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1174 /* Our implicit grab due to a button_press was broken by another grab:
1175 * the button will not get any button_release event if the mouse leaves
1176 * while the grab is taken, so unpress ourselves */
1178 CairoWidget::set_dirty ();
1183 ArdourButton::set_tweaks (Tweaks t)
1187 if (is_realized()) {
1194 ArdourButton::action_sensitivity_changed ()
1196 if (_action->property_sensitive ()) {
1197 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1199 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1204 ArdourButton::set_layout_ellipsize_width (int w)
1206 if (_layout_ellipsize_width == w) {
1209 _layout_ellipsize_width = w;
1213 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1214 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1216 if (is_realized ()) {
1222 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1224 if (_ellipsis == e) {
1231 _layout->set_ellipsize(_ellipsis);
1232 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1233 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1235 if (is_realized ()) {
1241 ArdourButton::ensure_layout ()
1245 _layout = Pango::Layout::create (get_pango_context());
1246 _layout->set_ellipsize(_ellipsis);
1247 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1248 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1254 ArdourButton::recalc_char_pixel_geometry ()
1256 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1260 // NB. this is not static, since the geometry is different
1261 // depending on the font used.
1263 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1264 _layout->set_text (x);
1265 _layout->get_pixel_size (w, h);
1266 _char_pixel_height = std::max(4, h);
1267 // number of actual chars in the string (not bytes)
1268 // Glib to the rescue.
1269 Glib::ustring gx(x);
1270 _char_avg_pixel_width = w / (float)gx.size();
1271 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1272 set_text_internal (); /* restore display text */
1276 ArdourButton::action_visibility_changed ()
1278 if (_action->property_visible ()) {
1286 ArdourButton::action_tooltip_changed ()
1288 string str = _action->property_tooltip().get_value();
1289 set_tooltip (*this, str);
1293 ArdourButton::set_elements (Element e)
1296 CairoWidget::set_dirty ();
1300 ArdourButton::add_elements (Element e)
1302 _elements = (ArdourButton::Element) (_elements | e);
1303 CairoWidget::set_dirty ();
1307 ArdourButton::set_icon (ArdourIcon::Icon i)
1310 _icon_render_cb = 0;
1311 _icon_render_cb_data = 0;
1312 _elements = (ArdourButton::Element) ((_elements | VectorIcon) & ~(ArdourButton::Text | IconRenderCallback));
1313 CairoWidget::set_dirty ();
1317 ArdourButton::set_icon (rendercallback_t cb, void* d)
1320 _elements = (ArdourButton::Element) ((_elements | ArdourButton::Text) & ~(IconRenderCallback | VectorIcon));
1321 _icon_render_cb = 0;
1322 _icon_render_cb_data = 0;
1324 _elements = (ArdourButton::Element) ((_elements | IconRenderCallback) & ~(ArdourButton::Text | VectorIcon));
1325 _icon_render_cb = cb;
1326 _icon_render_cb_data = d;
1328 CairoWidget::set_dirty ();
1332 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1334 if (led_custom_color == c && use_custom_led_color == useit) {
1338 led_custom_color = c;
1339 use_custom_led_color = useit;
1340 CairoWidget::set_dirty ();