2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <pangomm/layout.h>
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/stacktrace.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
34 #include "ardour/rc_configuration.h" // for widget prelight preference
36 #include "canvas/utils.h"
37 #include "canvas/colors.h"
39 #include "ardour_button.h"
40 #include "ardour_ui.h"
41 #include "global_signals.h"
45 #define BASELINESTRETCH (1.25)
46 #define TRACKHEADERBTNW (3.10)
56 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
57 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
58 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
60 ArdourButton::ArdourButton (Element e)
62 , _tweaks (Tweaks (0))
63 , _char_pixel_width (0)
64 , _char_pixel_height (0)
65 , _char_avg_pixel_width (0)
69 , _corner_radius (2.5)
74 , fill_inactive_color (0)
75 , fill_active_color (0)
76 , text_active_color(0)
77 , text_inactive_color(0)
79 , led_inactive_color(0)
80 , led_custom_color (0)
81 , use_custom_led_color (false)
84 , led_inset_pattern (0)
86 , _act_on_release (true)
88 , _distinct_led_click (false)
91 , _fixed_colors_set (false)
92 , _fallthrough_to_parent (false)
93 , _layout_ellipsize_width (-1)
94 , _ellipsis (Pango::ELLIPSIZE_NONE)
95 , _update_colors (true)
98 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
101 ArdourButton::ArdourButton (const std::string& str, Element e)
103 , _tweaks (Tweaks (0))
107 , _corner_radius (2.5)
112 , fill_inactive_color (0)
113 , fill_active_color (0)
114 , text_active_color(0)
115 , text_inactive_color(0)
116 , led_active_color(0)
117 , led_inactive_color(0)
118 , led_custom_color (0)
119 , use_custom_led_color (false)
121 , concave_pattern (0)
122 , led_inset_pattern (0)
124 , _act_on_release (true)
126 , _distinct_led_click (false)
129 , _fixed_colors_set (false)
130 , _fallthrough_to_parent (false)
131 , _layout_ellipsize_width (-1)
132 , _ellipsis (Pango::ELLIPSIZE_NONE)
133 , _update_colors (true)
134 , _pattern_height (0)
137 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
138 ARDOUR_UI_UTILS::DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
141 ArdourButton::~ArdourButton()
145 if (convex_pattern) {
146 cairo_pattern_destroy (convex_pattern);
149 if (concave_pattern) {
150 cairo_pattern_destroy (concave_pattern);
153 if (led_inset_pattern) {
154 cairo_pattern_destroy (led_inset_pattern);
159 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
163 _layout->set_font_description (fd);
169 ArdourButton::set_text (const std::string& str)
172 if (!is_realized()) {
176 if (_layout && _layout->get_text() != _text) {
177 _layout->set_text (_text);
183 ArdourButton::set_angle (const double angle)
189 ArdourButton::set_alignment (const float xa, const float ya)
196 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
201 if (_update_colors) {
204 if (get_height() != _pattern_height) {
208 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
209 text_color = text_active_color;
210 led_color = led_active_color;
212 text_color = text_inactive_color;
213 led_color = led_inactive_color;
216 if (use_custom_led_color) {
217 led_color = led_custom_color;
220 void (*rounded_function)(cairo_t*, double, double, double, double, double);
222 switch (_corner_mask) {
223 case 0x1: /* upper left only */
224 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
226 case 0x2: /* upper right only */
227 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
229 case 0x3: /* upper only */
230 rounded_function = Gtkmm2ext::rounded_top_rectangle;
232 /* should really have functions for lower right, lower left,
233 lower only, but for now, we don't
236 rounded_function = Gtkmm2ext::rounded_rectangle;
239 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
240 if ((_elements & (Body|Edge)) == (Body|Edge)) {
241 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
242 cairo_set_source_rgba (cr, 0, 0, 0, 1);
247 if ((_elements & Body)==Body) {
248 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
249 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
250 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
252 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
254 ArdourCanvas::set_source_rgba (cr, fill_active_color);
256 } else { //inactive, or it has an indicator
258 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
263 // IMPLICIT ACTIVE: draw a border of the active color
264 if ((_elements & Body)==Body) {
265 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
266 cairo_set_line_width (cr, 2.0);
267 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
268 ArdourCanvas::set_source_rgba (cr, fill_active_color);
273 //show the "convex" or "concave" gradient
274 if (!_flat_buttons) {
275 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
277 cairo_set_source (cr, concave_pattern);
278 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
281 cairo_set_source (cr, convex_pattern);
282 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
289 double x = rint((get_width() - _pixbuf->get_width()) * .5);
290 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
291 #if 0 // DEBUG style (print on hover)
292 if (_hovering || (_elements & Inactive)) {
293 printf("%s: p:%dx%d (%dx%d)\n",
295 _pixbuf->get_width(), _pixbuf->get_height(),
296 get_width(), get_height());
299 if (_elements & Menu) {
300 //if this is a DropDown with an icon, then we need to
301 //move the icon left slightly to accomomodate the arrow
304 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
305 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
308 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
309 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
310 const double x = get_width() * .5;
311 const double y = get_height() * .5;
312 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
313 const double slit = .11 * M_PI;
315 cairo_translate(cr, x, y);
317 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
318 if (active_state() == Gtkmm2ext::ExplicitActive)
319 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
321 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
322 cairo_fill_preserve(cr);
323 cairo_set_source_rgba (cr, .0, .0, .0, .5);
324 cairo_set_line_width(cr, 1);
328 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
330 cairo_rotate (cr, -.5 * M_PI);
331 cairo_move_to(cr, 0, 0);
332 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
333 cairo_line_to(cr, 0, 0);
334 cairo_close_path(cr);
337 cairo_rotate (cr, 2. * M_PI / 3.);
339 cairo_move_to(cr, 0, 0);
340 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
341 cairo_line_to(cr, 0, 0);
342 cairo_close_path(cr);
345 cairo_rotate (cr, 2. * M_PI / 3.);
346 cairo_move_to(cr, 0, 0);
347 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
348 cairo_line_to(cr, 0, 0);
349 cairo_close_path(cr);
354 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
355 if (active_state() == Gtkmm2ext::ExplicitActive)
356 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
358 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
360 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
361 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
366 else if (_elements & RecButton) {
367 const double x = get_width() * .5;
368 const double y = get_height() * .5;
369 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
370 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
371 if (active_state() == Gtkmm2ext::ExplicitActive)
372 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
374 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
375 cairo_fill_preserve(cr);
376 cairo_set_source_rgba (cr, .0, .0, .0, .8);
377 cairo_set_line_width(cr, 1);
380 else if (_elements & CloseCross) {
381 const double x = get_width() * .5;
382 const double y = get_height() * .5;
383 const double o = .5 + std::min(x, y) * .4;
384 ArdourCanvas::set_source_rgba (cr, text_color);
385 cairo_set_line_width(cr, 1);
386 cairo_move_to(cr, x-o, y-o);
387 cairo_line_to(cr, x+o, y+o);
388 cairo_move_to(cr, x+o, y-o);
389 cairo_line_to(cr, x-o, y+o);
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_rotate(cr, _angle * M_PI / 180.0);
445 cairo_device_to_user(cr, &ww, &wh);
446 xa = (ww - _text_width) * _xalign;
447 ya = (wh - _text_height) * _yalign;
449 /* quick hack for left/bottom alignment at -90deg
450 * TODO this should be generalized incl rotation.
451 * currently only 'user' of this API is meter_strip.cc
453 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
455 cairo_move_to (cr, xa, ya);
456 pango_cairo_update_layout(cr, _layout->gobj());
457 pango_cairo_show_layout (cr, _layout->gobj());
464 if (_elements & Menu) {
465 const float trih = ceil(_diameter * .5);
466 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
468 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
469 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
470 cairo_rel_line_to(cr, -triw2, -trih);
471 cairo_rel_line_to(cr, 2. * triw2, 0);
472 cairo_close_path(cr);
474 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, .5 - triw2, .5 - trih);
479 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
480 cairo_close_path(cr);
481 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
482 cairo_set_line_width(cr, 1);
487 if (_elements & Indicator) {
490 /* move to the center of the indicator/led */
491 if (_elements & Text) {
492 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
494 cairo_translate (cr, led_xoff, get_height() * .5);
496 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
499 cairo_translate (cr, get_width() * .5, get_height() * .5);
503 if (!_flat_buttons) {
504 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
505 cairo_set_source (cr, led_inset_pattern);
510 cairo_set_source_rgb (cr, 0, 0, 0);
511 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
515 ArdourCanvas::set_source_rgba (cr, led_color);
516 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
522 // a transparent overlay to indicate insensitivity
523 if ((visual_state() & Gtkmm2ext::Insensitive)) {
524 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
525 uint32_t ins_color = ARDOUR_UI::config()->color ("gtk_background");
526 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
530 // if requested, show hovering
531 if (ARDOUR_UI::config()->get_widget_prelight()
532 && !((visual_state() & Gtkmm2ext::Insensitive))) {
534 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
535 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
540 //user is currently pressing the button. dark outline helps to indicate this
541 if (_grabbed && !(_elements & (Inactive|Menu))) {
542 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
543 cairo_set_line_width(cr, 2);
544 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
548 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
549 if (visual_state() & Gtkmm2ext::Selected) {
550 cairo_set_line_width(cr, 1);
551 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
552 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
556 //I guess this means we have keyboard focus. I don't think this works currently
558 //A: yes, it's keyboard focus and it does work when there's no editor window
559 // (the editor is always the first receiver for KeyDown).
560 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
562 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
563 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
565 cairo_set_dash (cr, &dashes, 1, 0);
566 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
567 cairo_set_line_width (cr, 1.0);
569 cairo_set_dash (cr, 0, 0, 0);
574 ArdourButton::set_corner_radius (float r)
577 CairoWidget::set_dirty ();
581 ArdourButton::on_realize()
583 CairoWidget::on_realize ();
585 if (_layout && _layout->get_text() != _text) {
586 _layout->set_text (_text);
592 ArdourButton::on_size_request (Gtk::Requisition* req)
594 req->width = req->height = 0;
595 CairoWidget::on_size_request (req);
597 if (_diameter == 0) {
598 const float newdia = rint (ARDOUR_UI::config()->get_font_scale () / 9600.0); // 11px with 100% font-scaling
599 if (_diameter != newdia) {
605 if ((_elements & Text) && !_text.empty()) {
606 // if _layout does not exist, char_pixel_height() creates it,
607 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
608 _layout->get_pixel_size (_text_width, _text_height);
609 req->width += rint(1.75 * char_pixel_width()); // padding
610 req->width += _text_width;
617 req->width += _pixbuf->get_width() + char_pixel_width();
618 req->height = std::max(req->height, _pixbuf->get_height() + 4);
621 if (_elements & Indicator) {
622 req->width += lrint (_diameter) + char_pixel_width();
623 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
626 if ((_elements & Menu)) {
627 req->width += _diameter + 4;
630 if (_elements & (RecButton | CloseCross)) {
631 assert(!(_elements & Text));
632 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
634 req->height = std::max(req->height, wh);
637 /* Tweaks to mess the nice stuff above up again. */
638 if (_tweaks & TrackHeader) {
639 // forget everything above and just use a fixed square [em] size
640 // "TrackHeader Buttons" are single letter (usually uppercase)
641 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
642 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
646 else if (_tweaks & Square) {
647 // currerntly unused (again)
648 if (req->width < req->height)
649 req->width = req->height;
650 if (req->height < req->width)
651 req->height = req->width;
652 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
653 // properly centered text for those elements that are centered
654 // (no sub-pixel offset)
655 if ((req->width - _text_width) & 1) { ++req->width; }
656 if ((req->height - _text_height) & 1) { ++req->height; }
659 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
664 * This sets the colors used for rendering based on the name of the button, and
665 * thus uses information from the GUI config data.
668 ArdourButton::set_colors ()
670 _update_colors = false;
671 if (_fixed_colors_set) {
674 std::string name = get_name();
677 fill_active_color = ARDOUR_UI::config()->color (string_compose ("%1: fill active", name), &failed);
679 fill_active_color = ARDOUR_UI::config()->color ("generic button: fill active");
681 fill_inactive_color = ARDOUR_UI::config()->color (string_compose ("%1: fill", name), &failed);
683 fill_inactive_color = ARDOUR_UI::config()->color ("generic button: fill");
686 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
687 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
689 led_active_color = ARDOUR_UI::config()->color (string_compose ("%1: led active", name), &failed);
691 led_active_color = ARDOUR_UI::config()->color ("generic button: led active");
694 /* The inactive color for the LED is just a fairly dark version of the
698 ArdourCanvas::HSV inactive (led_active_color);
701 led_inactive_color = inactive.color ();
705 * This sets the colors used for rendering based on two fixed values, rather
706 * than basing them on the button name, and thus information in the GUI config
709 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
711 _fixed_colors_set = true;
713 fill_active_color = color_active;
714 fill_inactive_color = color_inactive;
716 unsigned char r, g, b, a;
717 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
719 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
720 (max (double(g), 255.) - min (double(g), 255.)) +
721 (max (double(b), 255.) - min (double(b), 255.));
723 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
724 (max (double(g), 0.) - min (double(g), 0.)) +
725 (max (double(b), 0.) - min (double(b), 0.));
727 text_active_color = (white_contrast > black_contrast) ?
728 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
729 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
732 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
734 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
735 (max (double(g), 255.) - min (double(g), 255.)) +
736 (max (double(b), 255.) - min (double(b), 255.));
738 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
739 (max (double(g), 0.) - min (double(g), 0.)) +
740 (max (double(b), 0.) - min (double(b), 0.));
742 text_inactive_color = (white_contrast > black_contrast) ?
743 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
744 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
746 /* XXX what about led colors ? */
747 CairoWidget::set_dirty ();
751 ArdourButton::build_patterns ()
753 if (convex_pattern) {
754 cairo_pattern_destroy (convex_pattern);
758 if (concave_pattern) {
759 cairo_pattern_destroy (concave_pattern);
763 if (led_inset_pattern) {
764 cairo_pattern_destroy (led_inset_pattern);
765 led_inset_pattern = 0;
769 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
770 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
771 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
774 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
775 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
776 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
778 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
779 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
780 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
782 _pattern_height = get_height() ;
786 ArdourButton::set_led_left (bool yn)
792 ArdourButton::on_button_press_event (GdkEventButton *ev)
796 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
797 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
798 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
803 if (binding_proxy.button_press_handler (ev)) {
808 CairoWidget::set_dirty ();
810 if (ev->button == 1 && !_act_on_release) {
812 _action->activate ();
817 if (_fallthrough_to_parent)
824 ArdourButton::on_button_release_event (GdkEventButton *ev)
826 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
827 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
828 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
829 signal_led_clicked(); /* EMIT SIGNAL */
835 CairoWidget::set_dirty ();
837 if (ev->button == 1 && _hovering) {
839 if (_act_on_release) {
841 _action->activate ();
847 if (_fallthrough_to_parent)
854 ArdourButton::set_distinct_led_click (bool yn)
856 _distinct_led_click = yn;
861 ArdourButton::color_handler ()
863 _update_colors = true;
864 CairoWidget::set_dirty ();
868 ArdourButton::on_size_allocate (Allocation& alloc)
870 CairoWidget::on_size_allocate (alloc);
875 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
877 watch_connection.disconnect ();
878 binding_proxy.set_controllable (c);
882 ArdourButton::watch ()
884 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
887 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
890 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
894 ArdourButton::controllable_changed ()
896 float val = binding_proxy.get_controllable()->get_value();
898 if (fabs (val) >= 0.5f) {
899 set_active_state (Gtkmm2ext::ExplicitActive);
901 unset_active_state ();
903 CairoWidget::set_dirty ();
907 ArdourButton::set_related_action (RefPtr<Action> act)
909 Gtkmm2ext::Activatable::set_related_action (act);
913 action_tooltip_changed ();
915 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
918 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
921 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
922 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
923 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
928 ArdourButton::action_toggled ()
930 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
933 if (tact->get_active()) {
934 set_active_state (Gtkmm2ext::ExplicitActive);
936 unset_active_state ();
942 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
944 _update_colors = true;
945 CairoWidget::set_dirty ();
949 ArdourButton::on_name_changed ()
951 _char_pixel_width = 0;
952 _char_pixel_height = 0;
954 _update_colors = true;
961 ArdourButton::setup_led_rect ()
963 if (!(_elements & Indicator)) {
970 _led_rect = new cairo_rectangle_t;
973 if (_elements & Text) {
975 _led_rect->x = char_pixel_width();
977 _led_rect->x = get_width() - char_pixel_width() + _diameter;
981 _led_rect->x = .5 * get_width() - _diameter;
984 _led_rect->y = .5 * (get_height() - _diameter);
985 _led_rect->width = _diameter;
986 _led_rect->height = _diameter;
990 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
999 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1001 bool changed = (_active_state != s);
1002 CairoWidget::set_active_state (s);
1004 _update_colors = true;
1005 CairoWidget::set_dirty ();
1010 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1012 bool changed = (_visual_state != s);
1013 CairoWidget::set_visual_state (s);
1015 _update_colors = true;
1016 CairoWidget::set_dirty ();
1021 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1024 CairoWidget::set_dirty ();
1025 return CairoWidget::on_focus_in_event (ev);
1029 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1032 CairoWidget::set_dirty ();
1033 return CairoWidget::on_focus_out_event (ev);
1037 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1039 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1043 _action->activate ();
1047 return CairoWidget::on_key_release_event (ev);
1051 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1053 _hovering = (_elements & Inactive) ? false : true;
1055 if (ARDOUR_UI::config()->get_widget_prelight()) {
1056 CairoWidget::set_dirty ();
1059 return CairoWidget::on_enter_notify_event (ev);
1063 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1067 if (ARDOUR_UI::config()->get_widget_prelight()) {
1068 CairoWidget::set_dirty ();
1071 return CairoWidget::on_leave_notify_event (ev);
1075 ArdourButton::set_tweaks (Tweaks t)
1079 if (is_realized()) {
1086 ArdourButton::action_sensitivity_changed ()
1088 if (_action->property_sensitive ()) {
1089 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1091 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1096 ArdourButton::set_layout_ellipsize_width (int w)
1098 if (_layout_ellipsize_width == w) {
1101 _layout_ellipsize_width = w;
1105 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1106 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1108 if (is_realized ()) {
1114 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1116 if (_ellipsis == e) {
1123 _layout->set_ellipsize(_ellipsis);
1124 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1125 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1127 if (is_realized ()) {
1133 ArdourButton::ensure_layout ()
1137 _layout = Pango::Layout::create (get_pango_context());
1138 _layout->set_ellipsize(_ellipsis);
1139 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1140 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1146 ArdourButton::recalc_char_pixel_geometry ()
1148 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1152 // NB. this is not static, since the geometry is different
1153 // depending on the font used.
1155 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1156 _layout->set_text (x);
1157 _layout->get_pixel_size (w, h);
1158 _char_pixel_height = std::max(4, h);
1159 // number of actual chars in the string (not bytes)
1160 // Glib to the rescue.
1161 Glib::ustring gx(x);
1162 _char_avg_pixel_width = w / (float)gx.size();
1163 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1164 _layout->set_text (_text);
1168 ArdourButton::action_visibility_changed ()
1170 if (_action->property_visible ()) {
1178 ArdourButton::action_tooltip_changed ()
1180 string str = _action->property_tooltip().get_value();
1181 ARDOUR_UI::instance()->set_tip (*this, str);
1185 ArdourButton::set_elements (Element e)
1188 CairoWidget::set_dirty ();
1192 ArdourButton::add_elements (Element e)
1194 _elements = (ArdourButton::Element) (_elements | e);
1195 CairoWidget::set_dirty ();
1199 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1201 if (led_custom_color == c && use_custom_led_color == useit) {
1205 led_custom_color = c;
1206 use_custom_led_color = useit;
1207 CairoWidget::set_dirty ();