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"
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31 #include "gtkmm2ext/gui_thread.h"
33 #include "ardour/rc_configuration.h" // for widget prelight preference
35 #include "ardour_button.h"
36 #include "ardour_ui.h"
37 #include "global_signals.h"
41 #define REFLECTION_HEIGHT 2
51 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
52 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
53 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
54 bool ArdourButton::_flat_buttons = false;
56 ArdourButton::ArdourButton (Element e)
58 , _tweaks (Tweaks (0))
62 , _corner_radius (4.0)
68 , fill_color_active (0)
69 , fill_color_active_start(0)
70 , fill_color_active_end(0)
71 , fill_color_inactive_start(0)
72 , fill_color_inactive_end(0)
73 , text_color_inactive(0)
74 , text_color_active(0)
75 , led_color_inactive(0)
78 , fill_pattern_active (0)
80 , led_inset_pattern (0)
81 , reflection_pattern (0)
83 , _act_on_release (true)
85 , _fixed_diameter (true)
86 , _distinct_led_click (false)
89 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 (4.0)
104 , fill_color_active (0)
105 , fill_color_active_start(0)
106 , fill_color_active_end(0)
107 , fill_color_inactive_start(0)
108 , fill_color_inactive_end(0)
109 , text_color_inactive(0)
110 , text_color_active(0)
111 , led_color_inactive(0)
112 , led_color_active(0)
114 , fill_pattern_active (0)
116 , led_inset_pattern (0)
117 , reflection_pattern (0)
119 , _act_on_release (true)
121 , _fixed_diameter (true)
122 , _distinct_led_click (false)
128 ArdourButton::~ArdourButton()
133 cairo_pattern_destroy (shine_pattern);
137 cairo_pattern_destroy (fill_pattern);
140 if (fill_pattern_active) {
141 cairo_pattern_destroy (fill_pattern_active);
144 if (led_inset_pattern) {
145 cairo_pattern_destroy (led_inset_pattern);
148 if (reflection_pattern) {
149 cairo_pattern_destroy (reflection_pattern);
155 ArdourButton::set_text (const std::string& str)
159 if (!_layout && !_text.empty()) {
160 _layout = Pango::Layout::create (get_pango_context());
164 _layout->set_text (str);
171 ArdourButton::set_markup (const std::string& str)
176 _layout = Pango::Layout::create (get_pango_context());
179 _layout->set_markup (str);
184 ArdourButton::set_angle (const double angle)
190 ArdourButton::set_alignment (const float xa, const float ya)
197 ArdourButton::render (cairo_t* cr)
199 void (*rounded_function)(cairo_t*, double, double, double, double, double);
201 switch (_corner_mask) {
202 case 0x1: /* upper left only */
203 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
205 case 0x2: /* upper right only */
206 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
208 case 0x3: /* upper only */
209 rounded_function = Gtkmm2ext::rounded_top_rectangle;
211 /* should really have functions for lower right, lower left,
212 lower only, but for now, we don't
215 rounded_function = Gtkmm2ext::rounded_rectangle;
218 if (!_fixed_diameter) {
219 _diameter = std::min (get_width(), get_height());
224 if ((_elements & Body)==Body) {
225 if (_elements & Edge) {
227 cairo_set_source_rgba (cr, 0, 0, 0, 1);
228 rounded_function(cr, 0, 0, get_width(), get_height(), _corner_radius);
231 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.5);
233 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
236 if (active_state() == Gtkmm2ext::ImplicitActive) {
238 if (!(_tweaks & ImplicitUsesSolidColor)) {
239 cairo_set_source (cr, fill_pattern);
241 cairo_set_source (cr, fill_pattern_active);
245 if (!(_tweaks & ImplicitUsesSolidColor)) {
247 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
248 cairo_set_line_width (cr, 1.0);
249 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 1.5);
250 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
254 } else if (active_state() == Gtkmm2ext::ExplicitActive || ((_elements & Indicator)==Indicator) ) {
257 cairo_set_source (cr, fill_pattern_active);
263 cairo_set_source (cr, fill_pattern);
269 if ( ((_elements & FlatFace)==FlatFace) && (active_state() != Gtkmm2ext::ExplicitActive) ) {
271 if ( !_flat_buttons ) {
272 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
273 Gtkmm2ext::rounded_rectangle (cr, 2, 3, get_width()-4, rheight, _corner_radius-1);
274 cairo_set_source (cr, shine_pattern);
278 if (active_state() == Gtkmm2ext::ExplicitActive) {
280 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
281 cairo_set_line_width (cr, 2.0);
282 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
283 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
288 UINT_TO_RGBA (fill_color_inactive_end, &r, &g, &b, &a);
289 cairo_set_line_width (cr, 2.0);
290 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
291 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
300 x = (get_width() - _pixbuf->get_width())/2.0;
301 y = (get_height() - _pixbuf->get_height())/2.0;
303 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
304 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
312 if (get_width() < 75) {
318 if ( ((_elements & Text)==Text) && !_text.empty()) {
320 cairo_rectangle (cr, 2, 1, get_width()-4, get_height()-2);
324 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
326 if (_elements & Indicator) {
328 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
330 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
332 pango_cairo_show_layout (cr, _layout->gobj());
340 cairo_save (cr); // TODO retain rotataion.. adj. LED,...
341 cairo_rotate(cr, _angle * M_PI / 180.0);
342 cairo_device_to_user(cr, &ww, &wh);
343 xa = (ww - _text_width) * _xalign;
344 ya = (wh - _text_height) * _yalign;
346 /* quick hack for left/bottom alignment at -90deg
347 * TODO this should be generalized incl rotation.
348 * currently only 'user' of this API is meter_strip.cc
350 if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
352 // TODO honor left/right text_margin with min/max()
354 cairo_move_to (cr, xa, ya);
355 pango_cairo_update_layout(cr, _layout->gobj());
356 pango_cairo_show_layout (cr, _layout->gobj());
359 /* use old center'ed layout for follow up items - until rotation/aligment code is completed */
360 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
365 if (((_elements & Indicator)==Indicator)) {
367 /* move to the center of the indicator/led */
371 if (_elements & Text) {
373 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
375 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
378 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
382 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
383 cairo_set_source (cr, led_inset_pattern);
387 cairo_set_source_rgb (cr, 0, 0, 0);
388 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
392 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
393 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
400 /* a partially transparent gray layer to indicate insensitivity */
402 if ((visual_state() & Gtkmm2ext::Insensitive)) {
403 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
404 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
409 bool show_reflection = (active_state() == Gtkmm2ext::ExplicitActive);
410 show_reflection &= !_flat_buttons;
411 show_reflection &= !((_elements & Indicator)==Indicator);
412 if ( show_reflection ) {
413 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
414 Gtkmm2ext::rounded_rectangle (cr, 2, get_height()*0.5-1, get_width()-4, rheight, _corner_radius-1);
415 cairo_set_source (cr, shine_pattern);
419 /* if requested, show hovering */
421 if (ARDOUR::Config->get_widget_prelight()
422 && !((visual_state() & Gtkmm2ext::Insensitive))) {
424 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
425 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
432 ArdourButton::set_diameter (float d)
434 _diameter = (d*2) + 5.0;
436 if (_diameter != 0.0) {
437 _fixed_diameter = true;
445 ArdourButton::set_corner_radius (float r)
452 ArdourButton::on_size_request (Gtk::Requisition* req)
457 CairoWidget::on_size_request (req);
459 if ((_elements & Text) && !_text.empty()) {
460 _layout->get_pixel_size (_text_width, _text_height);
461 if (_text_width + _diameter < 75) {
475 if ((_elements & Indicator) && _fixed_diameter) {
477 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
478 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
480 req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
481 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
485 req->width = _pixbuf->get_width() + xpad;
486 req->height = _pixbuf->get_height() + ypad;
488 req->width = _text_width + xpad;
489 req->height = _text_height + ypad;
492 req->width += _corner_radius;
496 ArdourButton::set_colors ()
498 Glib::ustring button_name = get_name();
500 if (button_name == "") {
504 border_color = ARDOUR_UI::config()->color_by_name ("button border");
506 fill_color_active_start = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", button_name));
507 fill_color_active_end = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", button_name));
509 fill_color_inactive_start = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", button_name));
510 fill_color_inactive_end = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", button_name));
512 text_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", button_name));
513 text_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", button_name));
515 led_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", button_name));
516 led_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", button_name));
519 void ArdourButton::set_bg_colors (const uint32_t color_active, const uint32_t color_inactive)
523 fill_color_active_start = fill_color_active_end = color_active;
525 unsigned char r, g, b, a;
526 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
528 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
529 (max (double(g), 255.) - min (double(g), 255.)) +
530 (max (double(b), 255.) - min (double(b), 255.));
532 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
533 (max (double(g), 0.) - min (double(g), 0.)) +
534 (max (double(b), 0.) - min (double(b), 0.));
537 text_color_inactive = (white_contrast > black_contrast) ?
538 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
539 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
541 fill_color_inactive_start = fill_color_inactive_end = color_inactive;
547 ArdourButton::build_patterns()
549 uint32_t start_color;
555 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
556 fill_color_active = led_color_active;
558 fill_color_active = fill_color_active_end;
562 cairo_pattern_destroy (shine_pattern);
567 cairo_pattern_destroy (fill_pattern);
571 if (fill_pattern_active) {
572 cairo_pattern_destroy (fill_pattern_active);
573 fill_pattern_active = 0;
576 if (_elements & Body) {
578 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
579 start_color = fill_color_active_start;
582 end_color = start_color;
584 end_color = fill_color_active;
586 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
593 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
594 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
595 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.1);
596 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.7, 1,1,1,0.2);
597 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
599 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
601 end_color = start_color = fill_color_inactive_end;
603 start_color = fill_color_inactive_start;
604 end_color = fill_color_inactive_end;
606 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
607 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
608 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
609 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
611 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
613 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
614 end_color = start_color = led_color_active;
616 end_color = start_color = fill_color_active_end;
619 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
620 start_color = led_color_inactive;
621 end_color = led_color_active;
623 start_color = fill_color_active_start;
624 end_color = fill_color_active_end;
627 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
628 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 0, r/255.0,g/255.0,b/255.0, a/255.0);
629 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
630 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 1, r/255.0,g/255.0,b/255.0, a/255.0);
633 if (led_inset_pattern) {
634 cairo_pattern_destroy (led_inset_pattern);
637 if (reflection_pattern) {
638 cairo_pattern_destroy (reflection_pattern);
641 if (_elements & Indicator) {
642 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
643 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
644 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
646 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
647 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
648 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
651 /* text and LED colors */
653 if (active_state() == Gtkmm2ext::ExplicitActive || ((_tweaks & ImplicitUsesSolidColor) && active_state() == Gtkmm2ext::ImplicitActive)) {
654 text_color = text_color_active;
655 led_color = led_color_active;
657 text_color = text_color_inactive;
658 led_color = led_color_inactive;
661 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
666 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
676 ArdourButton::set_led_left (bool yn)
682 ArdourButton::on_button_press_event (GdkEventButton *ev)
684 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
685 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
686 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
691 if (_tweaks & ShowClick) {
692 set_active_state (Gtkmm2ext::ExplicitActive);
695 if (binding_proxy.button_press_handler (ev)) {
699 if (!_act_on_release) {
701 _action->activate ();
710 ArdourButton::on_button_release_event (GdkEventButton *ev)
712 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
713 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
714 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
715 signal_led_clicked(); /* EMIT SIGNAL */
720 if (_tweaks & ShowClick) {
721 unset_active_state ();
726 if (_act_on_release) {
728 _action->activate ();
738 ArdourButton::set_distinct_led_click (bool yn)
740 _distinct_led_click = yn;
745 ArdourButton::color_handler ()
753 ArdourButton::on_size_allocate (Allocation& alloc)
755 CairoWidget::on_size_allocate (alloc);
761 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
763 watch_connection.disconnect ();
764 binding_proxy.set_controllable (c);
768 ArdourButton::watch ()
770 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
773 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
777 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
781 ArdourButton::controllable_changed ()
783 float val = binding_proxy.get_controllable()->get_value();
785 if (fabs (val) >= 0.5f) {
786 set_active_state (Gtkmm2ext::ExplicitActive);
788 unset_active_state ();
793 ArdourButton::set_related_action (RefPtr<Action> act)
795 Gtkmm2ext::Activatable::set_related_action (act);
799 action_tooltip_changed ();
801 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
804 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
807 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
808 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
809 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
814 ArdourButton::action_toggled ()
816 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
819 if (tact->get_active()) {
820 set_active_state (Gtkmm2ext::ExplicitActive);
822 unset_active_state ();
828 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
835 ArdourButton::setup_led_rect ()
839 if (get_width() < 75) {
845 if (_elements & Indicator) {
846 _led_rect = new cairo_rectangle_t;
848 if (_elements & Text) {
850 _led_rect->x = text_margin;
852 _led_rect->x = get_width() - text_margin - _diameter/2.0;
856 _led_rect->x = get_width()/2.0 - _diameter/2.0;
859 _led_rect->y = get_height()/2.0 - _diameter/2.0;
860 _led_rect->width = _diameter;
861 _led_rect->height = _diameter;
870 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
877 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
879 bool changed = (_active_state != s);
880 CairoWidget::set_active_state (s);
887 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
889 bool changed = (_visual_state != s);
890 CairoWidget::set_visual_state (s);
897 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
901 if (ARDOUR::Config->get_widget_prelight()) {
905 return CairoWidget::on_enter_notify_event (ev);
909 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
913 if (ARDOUR::Config->get_widget_prelight()) {
917 return CairoWidget::on_leave_notify_event (ev);
921 ArdourButton::set_tweaks (Tweaks t)
930 ArdourButton::action_sensitivity_changed ()
932 if (_action->property_sensitive ()) {
933 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
935 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
941 ArdourButton::action_visibility_changed ()
943 if (_action->property_visible ()) {
951 ArdourButton::action_tooltip_changed ()
953 string str = _action->property_tooltip().get_value();
954 ARDOUR_UI::instance()->set_tip (*this, str);
958 ArdourButton::set_rounded_corner_mask (int mask)
965 ArdourButton::set_elements (Element e)
972 ArdourButton::add_elements (Element e)
974 _elements = (ArdourButton::Element) (_elements | e);
979 ArdourButton::set_flat_buttons (bool yn)