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)
89 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
92 ArdourButton::ArdourButton (const std::string& str, Element e)
94 , _tweaks (Tweaks (0))
98 , _corner_radius (2.5)
103 , fill_inactive_color (0)
104 , fill_active_color (0)
105 , text_active_color(0)
106 , text_inactive_color(0)
107 , led_active_color(0)
108 , led_inactive_color(0)
110 , concave_pattern (0)
111 , led_inset_pattern (0)
113 , _act_on_release (true)
115 , _distinct_led_click (false)
118 , _fixed_colors_set (false)
123 ArdourButton::~ArdourButton()
127 if (convex_pattern) {
128 cairo_pattern_destroy (convex_pattern);
131 if (concave_pattern) {
132 cairo_pattern_destroy (concave_pattern);
135 if (led_inset_pattern) {
136 cairo_pattern_destroy (led_inset_pattern);
142 ArdourButton::set_text (const std::string& str)
146 if (!_layout && !_text.empty()) {
147 _layout = Pango::Layout::create (get_pango_context());
151 _layout->set_text (str);
158 ArdourButton::set_markup (const std::string& str)
163 _layout = Pango::Layout::create (get_pango_context());
166 _layout->set_markup (str);
171 ArdourButton::set_angle (const double angle)
177 ArdourButton::set_alignment (const float xa, const float ya)
184 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
188 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
189 text_color = text_active_color;
190 led_color = led_active_color;
192 text_color = text_inactive_color;
193 led_color = led_inactive_color;
196 void (*rounded_function)(cairo_t*, double, double, double, double, double);
198 switch (_corner_mask) {
199 case 0x1: /* upper left only */
200 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
202 case 0x2: /* upper right only */
203 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
205 case 0x3: /* upper only */
206 rounded_function = Gtkmm2ext::rounded_top_rectangle;
208 /* should really have functions for lower right, lower left,
209 lower only, but for now, we don't
212 rounded_function = Gtkmm2ext::rounded_rectangle;
215 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
216 if ((_elements & (Body|Edge)) == (Body|Edge)) {
217 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
218 cairo_set_source_rgba (cr, 0, 0, 0, 1);
223 if ((_elements & Body)==Body) {
224 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
225 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
226 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
228 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
230 ArdourCanvas::set_source_rgba (cr, fill_active_color);
232 } else { //inactive, or it has an indicator
234 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
239 // IMPLICIT ACTIVE: draw a border of the active color
240 if ((_elements & Body)==Body) {
241 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
242 cairo_set_line_width (cr, 2.0);
243 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
244 ArdourCanvas::set_source_rgba (cr, fill_active_color);
249 //show the "convex" or "concave" gradient
250 if (!_flat_buttons) {
251 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
253 cairo_set_source (cr, concave_pattern);
254 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
257 cairo_set_source (cr, convex_pattern);
258 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
265 double x = rint((get_width() - _pixbuf->get_width()) * .5);
266 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
268 if (_elements & Menu) {
269 //if this is a DropDown with an icon, then we need to
270 //move the icon left slightly to accomomodate the arrow
273 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
274 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
277 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
278 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
279 const double x = get_width() * .5;
280 const double y = get_height() * .5;
281 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
282 const double slit = .11 * M_PI;
284 cairo_translate(cr, x, y);
286 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
287 if (active_state() == Gtkmm2ext::ExplicitActive)
288 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
290 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
291 cairo_fill_preserve(cr);
292 cairo_set_source_rgba (cr, .0, .0, .0, .5);
293 cairo_set_line_width(cr, 1);
297 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
299 cairo_rotate (cr, -.5 * M_PI);
300 cairo_move_to(cr, 0, 0);
301 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
302 cairo_line_to(cr, 0, 0);
303 cairo_close_path(cr);
306 cairo_rotate (cr, 2. * M_PI / 3.);
308 cairo_move_to(cr, 0, 0);
309 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
310 cairo_line_to(cr, 0, 0);
311 cairo_close_path(cr);
314 cairo_rotate (cr, 2. * M_PI / 3.);
315 cairo_move_to(cr, 0, 0);
316 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
317 cairo_line_to(cr, 0, 0);
318 cairo_close_path(cr);
323 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
324 if (active_state() == Gtkmm2ext::ExplicitActive)
325 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
327 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
329 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
330 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
335 else if (_elements & RecButton) {
336 const double x = get_width() * .5;
337 const double y = get_height() * .5;
338 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
339 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
340 if (active_state() == Gtkmm2ext::ExplicitActive)
341 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
343 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
344 cairo_fill_preserve(cr);
345 cairo_set_source_rgba (cr, .0, .0, .0, .8);
346 cairo_set_line_width(cr, 1);
349 else if (_elements & CloseCross) {
350 const double x = get_width() * .5;
351 const double y = get_height() * .5;
352 const double o = .5 + std::min(x, y) * .4;
353 ArdourCanvas::set_source_rgba (cr, text_color);
354 cairo_set_line_width(cr, 1);
355 cairo_move_to(cr, x-o, y-o);
356 cairo_line_to(cr, x+o, y+o);
357 cairo_move_to(cr, x+o, y-o);
358 cairo_line_to(cr, x-o, y+o);
362 const int text_margin = char_pixel_width();
365 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
368 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
372 ArdourCanvas::set_source_rgba (cr, text_color);
373 const double text_ypos = (get_height() - _text_height) * .5;
375 if (_elements & Menu) {
376 // always left align (dropdown)
377 cairo_move_to (cr, text_margin, text_ypos);
378 pango_cairo_show_layout (cr, _layout->gobj());
379 } else if ( (_elements & Indicator) == Indicator) {
380 // left/right align depending on LED position
382 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
384 cairo_move_to (cr, text_margin, text_ypos);
386 pango_cairo_show_layout (cr, _layout->gobj());
388 /* centered text otherwise */
395 cairo_rotate(cr, _angle * M_PI / 180.0);
396 cairo_device_to_user(cr, &ww, &wh);
397 xa = (ww - _text_width) * _xalign;
398 ya = (wh - _text_height) * _yalign;
400 /* quick hack for left/bottom alignment at -90deg
401 * TODO this should be generalized incl rotation.
402 * currently only 'user' of this API is meter_strip.cc
404 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
406 cairo_move_to (cr, xa, ya);
407 pango_cairo_update_layout(cr, _layout->gobj());
408 pango_cairo_show_layout (cr, _layout->gobj());
415 if (_elements & Menu) {
416 const float trih = ceil(_diameter * .5);
417 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
419 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
420 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
421 cairo_rel_line_to(cr, -triw2, -trih);
422 cairo_rel_line_to(cr, 2. * triw2, 0);
423 cairo_close_path(cr);
425 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
428 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
429 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
430 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
431 cairo_close_path(cr);
432 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
433 cairo_set_line_width(cr, 1);
438 if (_elements & Indicator) {
441 /* move to the center of the indicator/led */
442 if (_elements & Text) {
443 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
445 cairo_translate (cr, led_xoff, get_height() * .5);
447 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
450 cairo_translate (cr, get_width() * .5, get_height() * .5);
454 if (!_flat_buttons) {
455 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
456 cairo_set_source (cr, led_inset_pattern);
461 cairo_set_source_rgb (cr, 0, 0, 0);
462 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
466 ArdourCanvas::set_source_rgba (cr, led_color);
467 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
473 // a transparent gray layer to indicate insensitivity
474 if ((visual_state() & Gtkmm2ext::Insensitive)) {
475 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
476 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
480 // if requested, show hovering
481 if (ARDOUR::Config->get_widget_prelight()
482 && !((visual_state() & Gtkmm2ext::Insensitive))) {
484 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
485 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
490 //user is currently pressing the button. black outline helps to indicate this
491 if (_grabbed && !(_elements & (Inactive|Menu))) {
492 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
493 cairo_set_line_width(cr, 2);
494 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5); // XXX no longer 'black'
498 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
499 if (visual_state() & Gtkmm2ext::Selected) {
500 #if 0 // outline, edge + 1px outside
501 cairo_set_line_width(cr, 2);
502 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
503 #else // replace edge (if any)
504 cairo_set_line_width(cr, 1);
505 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
507 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
511 //I guess this means we have keyboard focus. I don't think this works currently
513 //A: yes, it's keyboard focus and it does work when there's no editor window
514 // (the editor is always the first receiver for KeyDown).
515 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
517 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
518 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
520 cairo_set_dash (cr, &dashes, 1, 0);
521 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
522 cairo_set_line_width (cr, 1.0);
524 cairo_set_dash (cr, 0, 0, 0);
529 ArdourButton::set_diameter (float d)
531 _diameter = (d*2) + 5.0;
538 ArdourButton::set_corner_radius (float r)
545 ArdourButton::on_size_request (Gtk::Requisition* req)
547 req->width = req->height = 0;
548 CairoWidget::on_size_request (req);
550 if (_diameter == 0) {
551 _diameter = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
554 if ((_elements & Text) && !_text.empty()) {
556 _layout->get_pixel_size (_text_width, ignored);
557 _text_height = char_pixel_height ();
558 req->width += rint(1.6 * char_pixel_width()); // padding
559 req->width += _text_width;
560 req->height = std::max(req->height, (int) ceil(_text_height * BASELINESTRETCH + 1.0));
567 req->width += _pixbuf->get_width() + char_pixel_width();
568 req->height = std::max(req->height, _pixbuf->get_height() + 4);
571 if (_elements & Indicator) {
572 req->width += lrint (_diameter) + char_pixel_width();
573 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
576 if ((_elements & Menu)) {
577 req->width += _diameter + 4;
580 if (_elements & (RecButton | CloseCross)) {
581 assert(!(_elements & Text));
582 const int wh = std::max(char_pixel_width(), char_pixel_height()) * BASELINESTRETCH;
584 req->height = std::max(req->height, (int) wh);
587 if (_tweaks & Square) {
588 // squared buttons are also grouped, we cannot align all texts
589 // -> skip proper center adjustment
590 if (req->width < req->height)
591 req->width = req->height;
592 if (req->height < req->width)
593 req->height = req->width;
594 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
595 // properly centered text for those elements that are centered
596 if ((req->width - _text_width) & 1) { ++req->width; }
597 if ((req->height - _text_height) & 1) { ++req->height; }
602 * This sets the colors used for rendering based on the name of the button, and
603 * thus uses information from the GUI config data.
606 ArdourButton::set_colors ()
608 if (_fixed_colors_set) {
611 std::string name = get_name();
613 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
614 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
616 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
617 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
619 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
620 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
623 * This sets the colors used for rendering based on two fixed values, rather
624 * than basing them on the button name, and thus information in the GUI config
627 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
629 _fixed_colors_set = true;
631 fill_active_color = color_active;
632 fill_inactive_color = color_inactive;
634 unsigned char r, g, b, a;
635 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
637 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
638 (max (double(g), 255.) - min (double(g), 255.)) +
639 (max (double(b), 255.) - min (double(b), 255.));
641 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
642 (max (double(g), 0.) - min (double(g), 0.)) +
643 (max (double(b), 0.) - min (double(b), 0.));
645 text_active_color = (white_contrast > black_contrast) ?
646 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
647 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
650 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
652 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
653 (max (double(g), 255.) - min (double(g), 255.)) +
654 (max (double(b), 255.) - min (double(b), 255.));
656 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
657 (max (double(g), 0.) - min (double(g), 0.)) +
658 (max (double(b), 0.) - min (double(b), 0.));
660 text_inactive_color = (white_contrast > black_contrast) ?
661 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
662 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
664 /* XXX what about led colors ? */
667 set_name (""); /* this will trigger a "style-changed" message and then set_colors() */
671 ArdourButton::build_patterns ()
673 if (convex_pattern) {
674 cairo_pattern_destroy (convex_pattern);
678 if (concave_pattern) {
679 cairo_pattern_destroy (concave_pattern);
683 if (led_inset_pattern) {
684 cairo_pattern_destroy (led_inset_pattern);
688 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
689 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
690 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
693 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
694 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
695 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
697 if (_elements & Indicator) {
698 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
699 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
700 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
707 ArdourButton::set_led_left (bool yn)
713 ArdourButton::on_button_press_event (GdkEventButton *ev)
715 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
716 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
717 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
722 if (binding_proxy.button_press_handler (ev)) {
729 if (!_act_on_release) {
731 _action->activate ();
736 if (_fallthrough_to_parent)
743 ArdourButton::on_button_release_event (GdkEventButton *ev)
745 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
746 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
747 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
748 signal_led_clicked(); /* EMIT SIGNAL */
758 if (_act_on_release) {
760 _action->activate ();
766 if (_fallthrough_to_parent)
773 ArdourButton::set_distinct_led_click (bool yn)
775 _distinct_led_click = yn;
780 ArdourButton::color_handler ()
788 ArdourButton::on_size_allocate (Allocation& alloc)
790 CairoWidget::on_size_allocate (alloc);
796 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
798 watch_connection.disconnect ();
799 binding_proxy.set_controllable (c);
803 ArdourButton::watch ()
805 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
808 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
812 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
816 ArdourButton::controllable_changed ()
818 float val = binding_proxy.get_controllable()->get_value();
820 if (fabs (val) >= 0.5f) {
821 set_active_state (Gtkmm2ext::ExplicitActive);
823 unset_active_state ();
828 ArdourButton::set_related_action (RefPtr<Action> act)
830 Gtkmm2ext::Activatable::set_related_action (act);
834 action_tooltip_changed ();
836 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
839 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
842 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
843 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
844 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
849 ArdourButton::action_toggled ()
851 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
854 if (tact->get_active()) {
855 set_active_state (Gtkmm2ext::ExplicitActive);
857 unset_active_state ();
863 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
869 ArdourButton::on_name_changed ()
871 _char_pixel_width = 0;
872 _char_pixel_height = 0;
880 ArdourButton::setup_led_rect ()
882 if (!(_elements & Indicator)) {
888 _led_rect = new cairo_rectangle_t;
890 if (_elements & Text) {
892 _led_rect->x = char_pixel_width();
894 _led_rect->x = get_width() - char_pixel_width() + _diameter;
898 _led_rect->x = .5 * get_width() - _diameter;
901 _led_rect->y = .5 * (get_height() - _diameter);
902 _led_rect->width = _diameter;
903 _led_rect->height = _diameter;
907 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
914 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
916 bool changed = (_active_state != s);
917 CairoWidget::set_active_state (s);
925 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
927 bool changed = (_visual_state != s);
928 CairoWidget::set_visual_state (s);
937 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
941 return CairoWidget::on_focus_in_event (ev);
945 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
949 return CairoWidget::on_focus_out_event (ev);
953 ArdourButton::on_key_release_event (GdkEventKey *ev) {
955 (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
959 _action->activate ();
963 return CairoWidget::on_key_release_event (ev);
967 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
969 _hovering = (_elements & Inactive) ? false : true;
971 if (ARDOUR::Config->get_widget_prelight()) {
975 return CairoWidget::on_enter_notify_event (ev);
979 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
983 if (ARDOUR::Config->get_widget_prelight()) {
987 return CairoWidget::on_leave_notify_event (ev);
991 ArdourButton::set_tweaks (Tweaks t)
1000 ArdourButton::action_sensitivity_changed ()
1002 if (_action->property_sensitive ()) {
1003 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1005 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1010 ArdourButton::recalc_char_pixel_geometry ()
1012 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1016 _layout = Pango::Layout::create (get_pango_context());
1018 // NB. this is not static, since the geometry is different
1019 // depending on the font used.
1021 std::string t = _layout->get_text();
1022 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1023 _layout->set_text (x);
1024 _layout->get_pixel_size (w, h);
1025 _layout->set_text (t);
1026 _char_pixel_height = std::max(4, h);
1027 // number of actual chars in the string (not bytes)
1028 // Glib to the rescue.
1029 Glib::ustring gx(x);
1030 _char_pixel_width = std::max(4, w / (int)gx.size());
1034 ArdourButton::action_visibility_changed ()
1036 if (_action->property_visible ()) {
1044 ArdourButton::action_tooltip_changed ()
1046 string str = _action->property_tooltip().get_value();
1047 ARDOUR_UI::instance()->set_tip (*this, str);
1051 ArdourButton::set_elements (Element e)
1057 ArdourButton::add_elements (Element e)
1059 _elements = (ArdourButton::Element) (_elements | e);