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"
38 #include "ardour_button.h"
39 #include "ardour_ui.h"
40 #include "global_signals.h"
44 #define BASELINESTRETCH (1.25)
45 #define TRACKHEADERBTNW (3.10)
55 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
56 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
57 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
59 ArdourButton::ArdourButton (Element e)
61 , _tweaks (Tweaks (0))
62 , _char_pixel_width (0)
63 , _char_pixel_height (0)
64 , _char_avg_pixel_width (0)
68 , _corner_radius (2.5)
73 , fill_inactive_color (0)
74 , fill_active_color (0)
75 , text_active_color(0)
76 , text_inactive_color(0)
78 , led_inactive_color(0)
81 , led_inset_pattern (0)
83 , _act_on_release (true)
85 , _distinct_led_click (false)
88 , _fixed_colors_set (false)
89 , _fallthrough_to_parent (false)
90 , _layout_ellipsize_width (-1)
91 , _ellipsis (Pango::ELLIPSIZE_NONE)
92 , _update_colors (true)
95 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
98 ArdourButton::ArdourButton (const std::string& str, Element e)
100 , _tweaks (Tweaks (0))
104 , _corner_radius (2.5)
109 , fill_inactive_color (0)
110 , fill_active_color (0)
111 , text_active_color(0)
112 , text_inactive_color(0)
113 , led_active_color(0)
114 , led_inactive_color(0)
116 , concave_pattern (0)
117 , led_inset_pattern (0)
119 , _act_on_release (true)
121 , _distinct_led_click (false)
124 , _fixed_colors_set (false)
125 , _fallthrough_to_parent (false)
126 , _layout_ellipsize_width (-1)
127 , _ellipsis (Pango::ELLIPSIZE_NONE)
128 , _update_colors (true)
129 , _pattern_height (0)
132 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
133 ARDOUR_UI_UTILS::DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
136 ArdourButton::~ArdourButton()
140 if (convex_pattern) {
141 cairo_pattern_destroy (convex_pattern);
144 if (concave_pattern) {
145 cairo_pattern_destroy (concave_pattern);
148 if (led_inset_pattern) {
149 cairo_pattern_destroy (led_inset_pattern);
154 ArdourButton::set_text (const std::string& str)
157 if (!is_realized()) {
161 if (_layout && _layout->get_text() != _text) {
162 _layout->set_text (_text);
168 ArdourButton::set_angle (const double angle)
174 ArdourButton::set_alignment (const float xa, const float ya)
181 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
186 if (_update_colors) {
189 if (get_height() != _pattern_height) {
193 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
194 text_color = text_active_color;
195 led_color = led_active_color;
197 text_color = text_inactive_color;
198 led_color = led_inactive_color;
201 void (*rounded_function)(cairo_t*, double, double, double, double, double);
203 switch (_corner_mask) {
204 case 0x1: /* upper left only */
205 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
207 case 0x2: /* upper right only */
208 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
210 case 0x3: /* upper only */
211 rounded_function = Gtkmm2ext::rounded_top_rectangle;
213 /* should really have functions for lower right, lower left,
214 lower only, but for now, we don't
217 rounded_function = Gtkmm2ext::rounded_rectangle;
220 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
221 if ((_elements & (Body|Edge)) == (Body|Edge)) {
222 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
223 cairo_set_source_rgba (cr, 0, 0, 0, 1);
228 if ((_elements & Body)==Body) {
229 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
230 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
231 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
233 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
235 ArdourCanvas::set_source_rgba (cr, fill_active_color);
237 } else { //inactive, or it has an indicator
239 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
244 // IMPLICIT ACTIVE: draw a border of the active color
245 if ((_elements & Body)==Body) {
246 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
247 cairo_set_line_width (cr, 2.0);
248 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
249 ArdourCanvas::set_source_rgba (cr, fill_active_color);
254 //show the "convex" or "concave" gradient
255 if (!_flat_buttons) {
256 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
258 cairo_set_source (cr, concave_pattern);
259 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
262 cairo_set_source (cr, convex_pattern);
263 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
270 double x = rint((get_width() - _pixbuf->get_width()) * .5);
271 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
272 #if 0 // DEBUG style (print on hover)
273 if (_hovering || (_elements & Inactive)) {
274 printf("%s: p:%dx%d (%dx%d)\n",
276 _pixbuf->get_width(), _pixbuf->get_height(),
277 get_width(), get_height());
280 if (_elements & Menu) {
281 //if this is a DropDown with an icon, then we need to
282 //move the icon left slightly to accomomodate the arrow
285 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
286 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
289 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
290 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
291 const double x = get_width() * .5;
292 const double y = get_height() * .5;
293 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
294 const double slit = .11 * M_PI;
296 cairo_translate(cr, x, y);
298 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
299 if (active_state() == Gtkmm2ext::ExplicitActive)
300 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
302 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
303 cairo_fill_preserve(cr);
304 cairo_set_source_rgba (cr, .0, .0, .0, .5);
305 cairo_set_line_width(cr, 1);
309 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
311 cairo_rotate (cr, -.5 * M_PI);
312 cairo_move_to(cr, 0, 0);
313 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
314 cairo_line_to(cr, 0, 0);
315 cairo_close_path(cr);
318 cairo_rotate (cr, 2. * M_PI / 3.);
320 cairo_move_to(cr, 0, 0);
321 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
322 cairo_line_to(cr, 0, 0);
323 cairo_close_path(cr);
326 cairo_rotate (cr, 2. * M_PI / 3.);
327 cairo_move_to(cr, 0, 0);
328 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
329 cairo_line_to(cr, 0, 0);
330 cairo_close_path(cr);
335 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
336 if (active_state() == Gtkmm2ext::ExplicitActive)
337 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
339 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
341 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
342 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
347 else if (_elements & RecButton) {
348 const double x = get_width() * .5;
349 const double y = get_height() * .5;
350 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
351 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
352 if (active_state() == Gtkmm2ext::ExplicitActive)
353 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
355 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
356 cairo_fill_preserve(cr);
357 cairo_set_source_rgba (cr, .0, .0, .0, .8);
358 cairo_set_line_width(cr, 1);
361 else if (_elements & CloseCross) {
362 const double x = get_width() * .5;
363 const double y = get_height() * .5;
364 const double o = .5 + std::min(x, y) * .4;
365 ArdourCanvas::set_source_rgba (cr, text_color);
366 cairo_set_line_width(cr, 1);
367 cairo_move_to(cr, x-o, y-o);
368 cairo_line_to(cr, x+o, y+o);
369 cairo_move_to(cr, x+o, y-o);
370 cairo_line_to(cr, x-o, y+o);
374 const int text_margin = char_pixel_width();
376 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
378 #if 0 // DEBUG style (print on hover)
379 if (_hovering || (_elements & Inactive)) {
380 bool layout_font = true;
381 Pango::FontDescription fd = _layout->get_font_description();
382 if (fd.gobj() == NULL) {
384 fd = get_pango_context()->get_font_description();
386 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
388 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
389 ceil(char_pixel_height() * BASELINESTRETCH),
390 _text_width, _text_height,
391 get_width(), get_height(),
392 layout_font ? "L:" : "W:",
393 fd.to_string().c_str());
398 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
402 ArdourCanvas::set_source_rgba (cr, text_color);
403 const double text_ypos = (get_height() - _text_height) * .5;
405 if (_elements & Menu) {
406 // always left align (dropdown)
407 cairo_move_to (cr, text_margin, text_ypos);
408 pango_cairo_show_layout (cr, _layout->gobj());
409 } else if ( (_elements & Indicator) == Indicator) {
410 // left/right align depending on LED position
412 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
414 cairo_move_to (cr, text_margin, text_ypos);
416 pango_cairo_show_layout (cr, _layout->gobj());
418 /* centered text otherwise */
425 cairo_rotate(cr, _angle * M_PI / 180.0);
426 cairo_device_to_user(cr, &ww, &wh);
427 xa = (ww - _text_width) * _xalign;
428 ya = (wh - _text_height) * _yalign;
430 /* quick hack for left/bottom alignment at -90deg
431 * TODO this should be generalized incl rotation.
432 * currently only 'user' of this API is meter_strip.cc
434 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
436 cairo_move_to (cr, xa, floor(ya));
437 pango_cairo_update_layout(cr, _layout->gobj());
438 pango_cairo_show_layout (cr, _layout->gobj());
445 if (_elements & Menu) {
446 const float trih = ceil(_diameter * .5);
447 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
449 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
450 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
451 cairo_rel_line_to(cr, -triw2, -trih);
452 cairo_rel_line_to(cr, 2. * triw2, 0);
453 cairo_close_path(cr);
455 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
458 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
459 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
460 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
461 cairo_close_path(cr);
462 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
463 cairo_set_line_width(cr, 1);
468 if (_elements & Indicator) {
471 /* move to the center of the indicator/led */
472 if (_elements & Text) {
473 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
475 cairo_translate (cr, led_xoff, get_height() * .5);
477 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
480 cairo_translate (cr, get_width() * .5, get_height() * .5);
484 if (!_flat_buttons) {
485 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
486 cairo_set_source (cr, led_inset_pattern);
491 cairo_set_source_rgb (cr, 0, 0, 0);
492 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
496 ArdourCanvas::set_source_rgba (cr, led_color);
497 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
503 // a transparent gray layer to indicate insensitivity
504 if ((visual_state() & Gtkmm2ext::Insensitive)) {
505 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
506 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
510 // if requested, show hovering
511 if (ARDOUR::Config->get_widget_prelight()
512 && !((visual_state() & Gtkmm2ext::Insensitive))) {
514 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
515 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
520 //user is currently pressing the button. dark outline helps to indicate this
521 if (_grabbed && !(_elements & (Inactive|Menu))) {
522 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
523 cairo_set_line_width(cr, 2);
524 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
528 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
529 if (visual_state() & Gtkmm2ext::Selected) {
530 cairo_set_line_width(cr, 1);
531 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
532 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
536 //I guess this means we have keyboard focus. I don't think this works currently
538 //A: yes, it's keyboard focus and it does work when there's no editor window
539 // (the editor is always the first receiver for KeyDown).
540 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
542 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
543 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
545 cairo_set_dash (cr, &dashes, 1, 0);
546 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
547 cairo_set_line_width (cr, 1.0);
549 cairo_set_dash (cr, 0, 0, 0);
554 ArdourButton::set_corner_radius (float r)
557 CairoWidget::set_dirty ();
561 ArdourButton::on_realize()
563 CairoWidget::on_realize ();
565 if (_layout && _layout->get_text() != _text) {
566 _layout->set_text (_text);
572 ArdourButton::on_size_request (Gtk::Requisition* req)
574 req->width = req->height = 0;
575 CairoWidget::on_size_request (req);
577 if (_diameter == 0) {
578 const float newdia = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
579 if (_diameter != newdia) {
585 if ((_elements & Text) && !_text.empty()) {
587 _text_height = char_pixel_height ();
588 // if _layout does not exist, char_pixel_height() creates it,
589 _layout->get_pixel_size (_text_width, ignored);
590 req->width += rint(1.75 * char_pixel_width()); // padding
591 req->width += _text_width;
592 req->height = std::max(req->height, (int) ceil(_text_height * BASELINESTRETCH + 1.0));
599 req->width += _pixbuf->get_width() + char_pixel_width();
600 req->height = std::max(req->height, _pixbuf->get_height() + 4);
603 if (_elements & Indicator) {
604 req->width += lrint (_diameter) + char_pixel_width();
605 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
608 if ((_elements & Menu)) {
609 req->width += _diameter + 4;
612 if (_elements & (RecButton | CloseCross)) {
613 assert(!(_elements & Text));
614 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
616 req->height = std::max(req->height, wh);
619 /* Tweaks to mess the nice stuff above up again. */
620 if (_tweaks & TrackHeader) {
621 // forget everything above and just use a fixed square [em] size
622 // "TrackHeader Buttons" are single letter (usually uppercase)
623 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
624 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
628 else if (_tweaks & Square) {
629 // currerntly unused (again)
630 if (req->width < req->height)
631 req->width = req->height;
632 if (req->height < req->width)
633 req->height = req->width;
634 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
635 // properly centered text for those elements that are centered
636 // (no sub-pixel offset)
637 if ((req->width - _text_width) & 1) { ++req->width; }
638 if ((req->height - _text_height) & 1) { ++req->height; }
641 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
646 * This sets the colors used for rendering based on the name of the button, and
647 * thus uses information from the GUI config data.
650 ArdourButton::set_colors ()
652 _update_colors = false;
653 if (_fixed_colors_set) {
656 std::string name = get_name();
658 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
659 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
661 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
662 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
664 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
665 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
669 * This sets the colors used for rendering based on two fixed values, rather
670 * than basing them on the button name, and thus information in the GUI config
673 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
675 _fixed_colors_set = true;
677 fill_active_color = color_active;
678 fill_inactive_color = color_inactive;
680 unsigned char r, g, b, a;
681 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
683 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
684 (max (double(g), 255.) - min (double(g), 255.)) +
685 (max (double(b), 255.) - min (double(b), 255.));
687 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
688 (max (double(g), 0.) - min (double(g), 0.)) +
689 (max (double(b), 0.) - min (double(b), 0.));
691 text_active_color = (white_contrast > black_contrast) ?
692 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
693 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
696 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
698 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
699 (max (double(g), 255.) - min (double(g), 255.)) +
700 (max (double(b), 255.) - min (double(b), 255.));
702 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
703 (max (double(g), 0.) - min (double(g), 0.)) +
704 (max (double(b), 0.) - min (double(b), 0.));
706 text_inactive_color = (white_contrast > black_contrast) ?
707 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
708 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
710 /* XXX what about led colors ? */
711 CairoWidget::set_dirty ();
715 ArdourButton::build_patterns ()
717 if (convex_pattern) {
718 cairo_pattern_destroy (convex_pattern);
722 if (concave_pattern) {
723 cairo_pattern_destroy (concave_pattern);
727 if (led_inset_pattern) {
728 cairo_pattern_destroy (led_inset_pattern);
729 led_inset_pattern = 0;
733 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
734 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
735 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
738 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
739 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
740 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
742 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
743 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
744 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
746 _pattern_height = get_height() ;
750 ArdourButton::set_led_left (bool yn)
756 ArdourButton::on_button_press_event (GdkEventButton *ev)
758 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
759 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
760 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
765 if (binding_proxy.button_press_handler (ev)) {
770 CairoWidget::set_dirty ();
772 if (!_act_on_release) {
774 _action->activate ();
779 if (_fallthrough_to_parent)
786 ArdourButton::on_button_release_event (GdkEventButton *ev)
788 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
789 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
790 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
791 signal_led_clicked(); /* EMIT SIGNAL */
797 CairoWidget::set_dirty ();
801 if (_act_on_release) {
803 _action->activate ();
809 if (_fallthrough_to_parent)
816 ArdourButton::set_distinct_led_click (bool yn)
818 _distinct_led_click = yn;
823 ArdourButton::color_handler ()
825 _update_colors = true;
826 CairoWidget::set_dirty ();
830 ArdourButton::on_size_allocate (Allocation& alloc)
832 CairoWidget::on_size_allocate (alloc);
837 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
839 watch_connection.disconnect ();
840 binding_proxy.set_controllable (c);
844 ArdourButton::watch ()
846 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
849 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
852 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
856 ArdourButton::controllable_changed ()
858 float val = binding_proxy.get_controllable()->get_value();
860 if (fabs (val) >= 0.5f) {
861 set_active_state (Gtkmm2ext::ExplicitActive);
863 unset_active_state ();
865 CairoWidget::set_dirty ();
869 ArdourButton::set_related_action (RefPtr<Action> act)
871 Gtkmm2ext::Activatable::set_related_action (act);
875 action_tooltip_changed ();
877 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
880 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
883 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
884 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
885 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
890 ArdourButton::action_toggled ()
892 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
895 if (tact->get_active()) {
896 set_active_state (Gtkmm2ext::ExplicitActive);
898 unset_active_state ();
904 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
906 _update_colors = true;
907 CairoWidget::set_dirty ();
911 ArdourButton::on_name_changed ()
913 _char_pixel_width = 0;
914 _char_pixel_height = 0;
916 _update_colors = true;
923 ArdourButton::setup_led_rect ()
925 if (!(_elements & Indicator)) {
931 _led_rect = new cairo_rectangle_t;
933 if (_elements & Text) {
935 _led_rect->x = char_pixel_width();
937 _led_rect->x = get_width() - char_pixel_width() + _diameter;
941 _led_rect->x = .5 * get_width() - _diameter;
944 _led_rect->y = .5 * (get_height() - _diameter);
945 _led_rect->width = _diameter;
946 _led_rect->height = _diameter;
950 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
959 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
961 bool changed = (_active_state != s);
962 CairoWidget::set_active_state (s);
964 _update_colors = true;
965 CairoWidget::set_dirty ();
970 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
972 bool changed = (_visual_state != s);
973 CairoWidget::set_visual_state (s);
975 _update_colors = true;
976 CairoWidget::set_dirty ();
981 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
984 CairoWidget::set_dirty ();
985 return CairoWidget::on_focus_in_event (ev);
989 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
992 CairoWidget::set_dirty ();
993 return CairoWidget::on_focus_out_event (ev);
997 ArdourButton::on_key_release_event (GdkEventKey *ev) {
999 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1003 _action->activate ();
1007 return CairoWidget::on_key_release_event (ev);
1011 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1013 _hovering = (_elements & Inactive) ? false : true;
1015 if (ARDOUR::Config->get_widget_prelight()) {
1016 CairoWidget::set_dirty ();
1019 return CairoWidget::on_enter_notify_event (ev);
1023 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1027 if (ARDOUR::Config->get_widget_prelight()) {
1028 CairoWidget::set_dirty ();
1031 return CairoWidget::on_leave_notify_event (ev);
1035 ArdourButton::set_tweaks (Tweaks t)
1039 if (is_realized()) {
1046 ArdourButton::action_sensitivity_changed ()
1048 if (_action->property_sensitive ()) {
1049 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1051 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1056 ArdourButton::set_layout_ellisize_width (int w)
1058 if (_layout_ellipsize_width == w) {
1061 _layout_ellipsize_width = w;
1065 if (_layout_ellipsize_width > 0) {
1066 _layout->set_width (_layout_ellipsize_width);
1071 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1073 if (_ellipsis == e) {
1080 _layout->set_ellipsize(_ellipsis);
1081 if (_layout_ellipsize_width > 0) {
1082 _layout->set_width (_layout_ellipsize_width);
1084 if (is_realized ()) {
1090 ArdourButton::ensure_layout ()
1094 _layout = Pango::Layout::create (get_pango_context());
1095 _layout->set_ellipsize(_ellipsis);
1096 if (_layout_ellipsize_width > 0) {
1097 _layout->set_width (_layout_ellipsize_width);
1103 ArdourButton::recalc_char_pixel_geometry ()
1105 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1109 // NB. this is not static, since the geometry is different
1110 // depending on the font used.
1112 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1113 _layout->set_text (x);
1114 _layout->get_pixel_size (w, h);
1115 _char_pixel_height = std::max(4, h);
1116 // number of actual chars in the string (not bytes)
1117 // Glib to the rescue.
1118 Glib::ustring gx(x);
1119 _char_avg_pixel_width = w / (float)gx.size();
1120 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1121 _layout->set_text (_text);
1125 ArdourButton::action_visibility_changed ()
1127 if (_action->property_visible ()) {
1135 ArdourButton::action_tooltip_changed ()
1137 string str = _action->property_tooltip().get_value();
1138 ARDOUR_UI::instance()->set_tip (*this, str);
1142 ArdourButton::set_elements (Element e)
1145 CairoWidget::set_dirty ();
1149 ArdourButton::add_elements (Element e)
1151 _elements = (ArdourButton::Element) (_elements | e);
1152 CairoWidget::set_dirty ();