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)
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)
60 , _tweaks (Tweaks (0))
61 , _char_pixel_width (0)
62 , _char_pixel_height (0)
66 , _corner_radius (2.5)
71 , fill_inactive_color (0)
72 , fill_active_color (0)
73 , text_active_color(0)
74 , text_inactive_color(0)
76 , led_inactive_color(0)
79 , led_inset_pattern (0)
81 , _act_on_release (true)
83 , _distinct_led_click (false)
86 , _fixed_colors_set (false)
87 , _fallthrough_to_parent (false)
88 , _layout_ellipsize_width (-1)
89 , _ellipsis (Pango::ELLIPSIZE_NONE)
90 , _update_colors (true)
93 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
96 ArdourButton::ArdourButton (const std::string& str, Element e)
98 , _tweaks (Tweaks (0))
102 , _corner_radius (2.5)
107 , fill_inactive_color (0)
108 , fill_active_color (0)
109 , text_active_color(0)
110 , text_inactive_color(0)
111 , led_active_color(0)
112 , led_inactive_color(0)
114 , concave_pattern (0)
115 , led_inset_pattern (0)
117 , _act_on_release (true)
119 , _distinct_led_click (false)
122 , _fixed_colors_set (false)
123 , _fallthrough_to_parent (false)
124 , _layout_ellipsize_width (-1)
125 , _ellipsis (Pango::ELLIPSIZE_NONE)
126 , _update_colors (true)
127 , _pattern_height (0)
130 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
133 ArdourButton::~ArdourButton()
137 if (convex_pattern) {
138 cairo_pattern_destroy (convex_pattern);
141 if (concave_pattern) {
142 cairo_pattern_destroy (concave_pattern);
145 if (led_inset_pattern) {
146 cairo_pattern_destroy (led_inset_pattern);
151 ArdourButton::set_text (const std::string& str)
154 if (!is_realized()) {
158 if (_layout && _layout->get_text() != _text) {
159 _layout->set_text (_text);
165 ArdourButton::set_angle (const double angle)
171 ArdourButton::set_alignment (const float xa, const float ya)
178 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
183 if (_update_colors) {
186 if (get_height() != _pattern_height) {
190 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
191 text_color = text_active_color;
192 led_color = led_active_color;
194 text_color = text_inactive_color;
195 led_color = led_inactive_color;
198 void (*rounded_function)(cairo_t*, double, double, double, double, double);
200 switch (_corner_mask) {
201 case 0x1: /* upper left only */
202 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
204 case 0x2: /* upper right only */
205 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
207 case 0x3: /* upper only */
208 rounded_function = Gtkmm2ext::rounded_top_rectangle;
210 /* should really have functions for lower right, lower left,
211 lower only, but for now, we don't
214 rounded_function = Gtkmm2ext::rounded_rectangle;
217 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
218 if ((_elements & (Body|Edge)) == (Body|Edge)) {
219 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
220 cairo_set_source_rgba (cr, 0, 0, 0, 1);
225 if ((_elements & Body)==Body) {
226 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
227 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
228 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
230 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
232 ArdourCanvas::set_source_rgba (cr, fill_active_color);
234 } else { //inactive, or it has an indicator
236 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
241 // IMPLICIT ACTIVE: draw a border of the active color
242 if ((_elements & Body)==Body) {
243 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
244 cairo_set_line_width (cr, 2.0);
245 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
246 ArdourCanvas::set_source_rgba (cr, fill_active_color);
251 //show the "convex" or "concave" gradient
252 if (!_flat_buttons) {
253 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
255 cairo_set_source (cr, concave_pattern);
256 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
259 cairo_set_source (cr, convex_pattern);
260 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
267 double x = rint((get_width() - _pixbuf->get_width()) * .5);
268 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
270 if (_elements & Menu) {
271 //if this is a DropDown with an icon, then we need to
272 //move the icon left slightly to accomomodate the arrow
275 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
276 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
279 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
280 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
281 const double x = get_width() * .5;
282 const double y = get_height() * .5;
283 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
284 const double slit = .11 * M_PI;
286 cairo_translate(cr, x, y);
288 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
289 if (active_state() == Gtkmm2ext::ExplicitActive)
290 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
292 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
293 cairo_fill_preserve(cr);
294 cairo_set_source_rgba (cr, .0, .0, .0, .5);
295 cairo_set_line_width(cr, 1);
299 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
301 cairo_rotate (cr, -.5 * M_PI);
302 cairo_move_to(cr, 0, 0);
303 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
304 cairo_line_to(cr, 0, 0);
305 cairo_close_path(cr);
308 cairo_rotate (cr, 2. * M_PI / 3.);
310 cairo_move_to(cr, 0, 0);
311 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
312 cairo_line_to(cr, 0, 0);
313 cairo_close_path(cr);
316 cairo_rotate (cr, 2. * M_PI / 3.);
317 cairo_move_to(cr, 0, 0);
318 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
319 cairo_line_to(cr, 0, 0);
320 cairo_close_path(cr);
325 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
326 if (active_state() == Gtkmm2ext::ExplicitActive)
327 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
329 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
331 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
332 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
337 else if (_elements & RecButton) {
338 const double x = get_width() * .5;
339 const double y = get_height() * .5;
340 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
341 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
342 if (active_state() == Gtkmm2ext::ExplicitActive)
343 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
345 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
346 cairo_fill_preserve(cr);
347 cairo_set_source_rgba (cr, .0, .0, .0, .8);
348 cairo_set_line_width(cr, 1);
351 else if (_elements & CloseCross) {
352 const double x = get_width() * .5;
353 const double y = get_height() * .5;
354 const double o = .5 + std::min(x, y) * .4;
355 ArdourCanvas::set_source_rgba (cr, text_color);
356 cairo_set_line_width(cr, 1);
357 cairo_move_to(cr, x-o, y-o);
358 cairo_line_to(cr, x+o, y+o);
359 cairo_move_to(cr, x+o, y-o);
360 cairo_line_to(cr, x-o, y+o);
364 const int text_margin = char_pixel_width();
367 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
369 #if 0 // DEBUG style (print on hover)
371 bool layout_font = true;
372 Pango::FontDescription fd = _layout->get_font_description();
373 if (fd.gobj() == NULL) {
375 fd = get_pango_context()->get_font_description();
377 printf("%s: f:%dx%d bh:%.0f tw:%d (%dx%d) %s\"%s\"\n",
379 char_pixel_width(), char_pixel_height(),
380 ceil(_text_height * BASELINESTRETCH),
382 get_width(), get_height(),
383 layout_font ? "L:" : "W:",
384 fd.to_string().c_str());
389 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
393 ArdourCanvas::set_source_rgba (cr, text_color);
394 const double text_ypos = (get_height() - _text_height) * .5;
396 if (_elements & Menu) {
397 // always left align (dropdown)
398 cairo_move_to (cr, text_margin, text_ypos);
399 pango_cairo_show_layout (cr, _layout->gobj());
400 } else if ( (_elements & Indicator) == Indicator) {
401 // left/right align depending on LED position
403 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
405 cairo_move_to (cr, text_margin, text_ypos);
407 pango_cairo_show_layout (cr, _layout->gobj());
409 /* centered text otherwise */
416 cairo_rotate(cr, _angle * M_PI / 180.0);
417 cairo_device_to_user(cr, &ww, &wh);
418 xa = (ww - _text_width) * _xalign;
419 ya = (wh - _text_height) * _yalign;
421 /* quick hack for left/bottom alignment at -90deg
422 * TODO this should be generalized incl rotation.
423 * currently only 'user' of this API is meter_strip.cc
425 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
427 cairo_move_to (cr, xa, ya);
428 pango_cairo_update_layout(cr, _layout->gobj());
429 pango_cairo_show_layout (cr, _layout->gobj());
436 if (_elements & Menu) {
437 const float trih = ceil(_diameter * .5);
438 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
440 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
441 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
442 cairo_rel_line_to(cr, -triw2, -trih);
443 cairo_rel_line_to(cr, 2. * triw2, 0);
444 cairo_close_path(cr);
446 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
449 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
450 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
451 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
452 cairo_close_path(cr);
453 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
454 cairo_set_line_width(cr, 1);
459 if (_elements & Indicator) {
462 /* move to the center of the indicator/led */
463 if (_elements & Text) {
464 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
466 cairo_translate (cr, led_xoff, get_height() * .5);
468 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
471 cairo_translate (cr, get_width() * .5, get_height() * .5);
475 if (!_flat_buttons) {
476 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
477 cairo_set_source (cr, led_inset_pattern);
482 cairo_set_source_rgb (cr, 0, 0, 0);
483 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
487 ArdourCanvas::set_source_rgba (cr, led_color);
488 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
494 // a transparent gray layer to indicate insensitivity
495 if ((visual_state() & Gtkmm2ext::Insensitive)) {
496 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
497 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
501 // if requested, show hovering
502 if (ARDOUR::Config->get_widget_prelight()
503 && !((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.905, 0.917, 0.925, 0.2);
511 //user is currently pressing the button. dark outline helps to indicate this
512 if (_grabbed && !(_elements & (Inactive|Menu))) {
513 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
514 cairo_set_line_width(cr, 2);
515 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
519 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
520 if (visual_state() & Gtkmm2ext::Selected) {
521 cairo_set_line_width(cr, 1);
522 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
523 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
527 //I guess this means we have keyboard focus. I don't think this works currently
529 //A: yes, it's keyboard focus and it does work when there's no editor window
530 // (the editor is always the first receiver for KeyDown).
531 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
533 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
534 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
536 cairo_set_dash (cr, &dashes, 1, 0);
537 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
538 cairo_set_line_width (cr, 1.0);
540 cairo_set_dash (cr, 0, 0, 0);
545 ArdourButton::set_corner_radius (float r)
548 CairoWidget::set_dirty ();
552 ArdourButton::on_realize()
554 CairoWidget::on_realize ();
556 if (_layout && _layout->get_text() != _text) {
557 _layout->set_text (_text);
562 ArdourButton::on_size_request (Gtk::Requisition* req)
564 req->width = req->height = 0;
565 CairoWidget::on_size_request (req);
567 if (_diameter == 0) {
568 const float newdia = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
569 if (_diameter != newdia) {
575 if ((_elements & Text) && !_text.empty()) {
577 _text_height = char_pixel_height ();
578 // if _layout does not exist, char_pixel_height() creates it,
579 _layout->get_pixel_size (_text_width, ignored);
580 req->width += rint(1.6 * char_pixel_width()); // padding
581 req->width += _text_width;
582 req->height = std::max(req->height, (int) ceil(_text_height * BASELINESTRETCH + 1.0));
589 req->width += _pixbuf->get_width() + char_pixel_width();
590 req->height = std::max(req->height, _pixbuf->get_height() + 4);
593 if (_elements & Indicator) {
594 req->width += lrint (_diameter) + char_pixel_width();
595 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
598 if ((_elements & Menu)) {
599 req->width += _diameter + 4;
602 if (_elements & (RecButton | CloseCross)) {
603 assert(!(_elements & Text));
604 const int wh = std::max(char_pixel_width(), char_pixel_height()) * BASELINESTRETCH;
606 req->height = std::max(req->height, (int) wh);
609 if (_tweaks & Square) {
610 // squared buttons are also grouped, we cannot align all texts
611 // -> skip proper center adjustment
612 if (req->width < req->height)
613 req->width = req->height;
614 if (req->height < req->width)
615 req->height = req->width;
616 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
617 // properly centered text for those elements that are centered
618 if ((req->width - _text_width) & 1) { ++req->width; }
619 if ((req->height - _text_height) & 1) { ++req->height; }
624 * This sets the colors used for rendering based on the name of the button, and
625 * thus uses information from the GUI config data.
628 ArdourButton::set_colors ()
630 _update_colors = false;
631 if (_fixed_colors_set) {
634 std::string name = get_name();
636 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
637 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
639 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
640 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
642 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
643 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
647 * This sets the colors used for rendering based on two fixed values, rather
648 * than basing them on the button name, and thus information in the GUI config
651 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
653 _fixed_colors_set = true;
655 fill_active_color = color_active;
656 fill_inactive_color = color_inactive;
658 unsigned char r, g, b, a;
659 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
661 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
662 (max (double(g), 255.) - min (double(g), 255.)) +
663 (max (double(b), 255.) - min (double(b), 255.));
665 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
666 (max (double(g), 0.) - min (double(g), 0.)) +
667 (max (double(b), 0.) - min (double(b), 0.));
669 text_active_color = (white_contrast > black_contrast) ?
670 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
671 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
674 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
676 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
677 (max (double(g), 255.) - min (double(g), 255.)) +
678 (max (double(b), 255.) - min (double(b), 255.));
680 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
681 (max (double(g), 0.) - min (double(g), 0.)) +
682 (max (double(b), 0.) - min (double(b), 0.));
684 text_inactive_color = (white_contrast > black_contrast) ?
685 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
686 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
688 /* XXX what about led colors ? */
690 /* trigger a "style-changed" message */
695 ArdourButton::build_patterns ()
697 if (convex_pattern) {
698 cairo_pattern_destroy (convex_pattern);
702 if (concave_pattern) {
703 cairo_pattern_destroy (concave_pattern);
707 if (led_inset_pattern) {
708 cairo_pattern_destroy (led_inset_pattern);
709 led_inset_pattern = 0;
713 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
714 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
715 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
718 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
719 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
720 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
722 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
723 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
724 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
726 _pattern_height = get_height() ;
730 ArdourButton::set_led_left (bool yn)
736 ArdourButton::on_button_press_event (GdkEventButton *ev)
738 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
739 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
740 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
745 if (binding_proxy.button_press_handler (ev)) {
750 CairoWidget::set_dirty ();
752 if (!_act_on_release) {
754 _action->activate ();
759 if (_fallthrough_to_parent)
766 ArdourButton::on_button_release_event (GdkEventButton *ev)
768 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
769 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
770 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
771 signal_led_clicked(); /* EMIT SIGNAL */
777 CairoWidget::set_dirty ();
781 if (_act_on_release) {
783 _action->activate ();
789 if (_fallthrough_to_parent)
796 ArdourButton::set_distinct_led_click (bool yn)
798 _distinct_led_click = yn;
803 ArdourButton::color_handler ()
805 _update_colors = true;
806 CairoWidget::set_dirty ();
810 ArdourButton::on_size_allocate (Allocation& alloc)
812 CairoWidget::on_size_allocate (alloc);
814 _update_colors = true;
818 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
820 watch_connection.disconnect ();
821 binding_proxy.set_controllable (c);
825 ArdourButton::watch ()
827 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
830 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
833 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
837 ArdourButton::controllable_changed ()
839 float val = binding_proxy.get_controllable()->get_value();
841 if (fabs (val) >= 0.5f) {
842 set_active_state (Gtkmm2ext::ExplicitActive);
844 unset_active_state ();
846 CairoWidget::set_dirty ();
850 ArdourButton::set_related_action (RefPtr<Action> act)
852 Gtkmm2ext::Activatable::set_related_action (act);
856 action_tooltip_changed ();
858 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
861 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
864 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
865 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
866 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
871 ArdourButton::action_toggled ()
873 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
876 if (tact->get_active()) {
877 set_active_state (Gtkmm2ext::ExplicitActive);
879 unset_active_state ();
885 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
891 ArdourButton::on_name_changed ()
893 _char_pixel_width = 0;
894 _char_pixel_height = 0;
896 _update_colors = true;
903 ArdourButton::setup_led_rect ()
905 if (!(_elements & Indicator)) {
911 _led_rect = new cairo_rectangle_t;
913 if (_elements & Text) {
915 _led_rect->x = char_pixel_width();
917 _led_rect->x = get_width() - char_pixel_width() + _diameter;
921 _led_rect->x = .5 * get_width() - _diameter;
924 _led_rect->y = .5 * (get_height() - _diameter);
925 _led_rect->width = _diameter;
926 _led_rect->height = _diameter;
930 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
939 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
941 bool changed = (_active_state != s);
942 CairoWidget::set_active_state (s);
944 _update_colors = true;
945 CairoWidget::set_dirty ();
950 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
952 bool changed = (_visual_state != s);
953 CairoWidget::set_visual_state (s);
955 _update_colors = true;
956 CairoWidget::set_dirty ();
961 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
964 CairoWidget::set_dirty ();
965 return CairoWidget::on_focus_in_event (ev);
969 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
972 CairoWidget::set_dirty ();
973 return CairoWidget::on_focus_out_event (ev);
977 ArdourButton::on_key_release_event (GdkEventKey *ev) {
979 (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
983 _action->activate ();
987 return CairoWidget::on_key_release_event (ev);
991 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
993 _hovering = (_elements & Inactive) ? false : true;
995 if (ARDOUR::Config->get_widget_prelight()) {
996 CairoWidget::set_dirty ();
999 return CairoWidget::on_enter_notify_event (ev);
1003 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1007 if (ARDOUR::Config->get_widget_prelight()) {
1008 CairoWidget::set_dirty ();
1011 return CairoWidget::on_leave_notify_event (ev);
1015 ArdourButton::set_tweaks (Tweaks t)
1019 CairoWidget::set_dirty ();
1024 ArdourButton::action_sensitivity_changed ()
1026 if (_action->property_sensitive ()) {
1027 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1029 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1034 ArdourButton::set_layout_ellisize_width (int w)
1036 if (_layout_ellipsize_width == w) {
1039 _layout_ellipsize_width = w;
1043 if (_layout_ellipsize_width > 0) {
1044 _layout->set_width (_layout_ellipsize_width);
1049 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1051 if (_ellipsis == e) {
1058 _layout->set_ellipsize(_ellipsis);
1059 if (is_realized () && _layout_ellipsize_width > 0) {
1060 _layout->set_width (_layout_ellipsize_width);
1066 ArdourButton::ensure_layout ()
1070 _layout = Pango::Layout::create (get_pango_context());
1071 _layout->set_ellipsize(_ellipsis);
1072 if (_layout_ellipsize_width > 0) {
1073 _layout->set_width (_layout_ellipsize_width);
1079 ArdourButton::recalc_char_pixel_geometry ()
1081 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1085 // NB. this is not static, since the geometry is different
1086 // depending on the font used.
1088 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1089 _layout->set_text (x);
1090 _layout->get_pixel_size (w, h);
1091 _char_pixel_height = std::max(4, h);
1092 // number of actual chars in the string (not bytes)
1093 // Glib to the rescue.
1094 Glib::ustring gx(x);
1095 _char_pixel_width = std::max(4, w / (int)gx.size());
1096 _layout->set_text (_text);
1100 ArdourButton::action_visibility_changed ()
1102 if (_action->property_visible ()) {
1110 ArdourButton::action_tooltip_changed ()
1112 string str = _action->property_tooltip().get_value();
1113 ARDOUR_UI::instance()->set_tip (*this, str);
1117 ArdourButton::set_elements (Element e)
1120 CairoWidget::set_dirty ();
1124 ArdourButton::add_elements (Element e)
1126 _elements = (ArdourButton::Element) (_elements | e);
1127 CairoWidget::set_dirty ();