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)
65 , fill_color_active (0)
66 , fill_color_inactive (0)
68 , fill_pattern_active (0)
70 , led_inset_pattern (0)
71 , reflection_pattern (0)
73 , _act_on_release (true)
75 , _fixed_diameter (true)
76 , _distinct_led_click (false)
79 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
82 ArdourButton::ArdourButton (const std::string& str, Element e)
84 , _tweaks (Tweaks (0))
88 , _corner_radius (4.0)
91 , fill_color_active (0)
92 , fill_color_inactive (0)
94 , fill_pattern_active (0)
96 , led_inset_pattern (0)
97 , reflection_pattern (0)
99 , _act_on_release (true)
101 , _fixed_diameter (true)
102 , _distinct_led_click (false)
108 ArdourButton::~ArdourButton()
113 cairo_pattern_destroy (shine_pattern);
117 cairo_pattern_destroy (fill_pattern);
120 if (fill_pattern_active) {
121 cairo_pattern_destroy (fill_pattern_active);
124 if (led_inset_pattern) {
125 cairo_pattern_destroy (led_inset_pattern);
128 if (reflection_pattern) {
129 cairo_pattern_destroy (reflection_pattern);
135 ArdourButton::set_text (const std::string& str)
139 if (!_layout && !_text.empty()) {
140 _layout = Pango::Layout::create (get_pango_context());
144 _layout->set_text (str);
151 ArdourButton::set_markup (const std::string& str)
156 _layout = Pango::Layout::create (get_pango_context());
159 _layout->set_text (str);
164 ArdourButton::render (cairo_t* cr)
166 void (*rounded_function)(cairo_t*, double, double, double, double, double);
168 switch (_corner_mask) {
169 case 0x1: /* upper left only */
170 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
172 case 0x2: /* upper right only */
173 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
175 case 0x3: /* upper only */
176 rounded_function = Gtkmm2ext::rounded_top_rectangle;
178 /* should really have functions for lower right, lower left,
179 lower only, but for now, we don't
182 rounded_function = Gtkmm2ext::rounded_rectangle;
185 if (!_fixed_diameter) {
186 _diameter = std::min (get_width(), get_height());
191 if ((_elements & Body)==Body) {
192 if (_elements & Edge) {
194 cairo_set_source_rgba (cr, 0, 0, 0, 1);
195 rounded_function(cr, 0, 0, get_width(), get_height(), _corner_radius);
198 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.5);
200 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
203 if (active_state() == Gtkmm2ext::ImplicitActive) {
205 if (!(_tweaks & ImplicitUsesSolidColor)) {
206 cairo_set_source (cr, fill_pattern);
208 cairo_set_source (cr, fill_pattern_active);
212 if (!(_tweaks & ImplicitUsesSolidColor)) {
214 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
215 cairo_set_line_width (cr, 1.0);
216 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 1.5);
217 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
221 } else if (active_state() == Gtkmm2ext::ExplicitActive || ((_elements & Indicator)==Indicator) ) {
224 cairo_set_source (cr, fill_pattern_active);
230 cairo_set_source (cr, fill_pattern);
236 if ( ((_elements & FlatFace)==FlatFace) && (active_state() != Gtkmm2ext::ExplicitActive) ) {
238 if ( !_flat_buttons ) {
239 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
240 Gtkmm2ext::rounded_rectangle (cr, 2, 3, get_width()-4, rheight, _corner_radius-1);
241 cairo_set_source (cr, shine_pattern);
245 if (active_state() == Gtkmm2ext::ExplicitActive) {
247 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
248 cairo_set_line_width (cr, 2.0);
249 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
250 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
255 UINT_TO_RGBA (fill_color_inactive, &r, &g, &b, &a);
256 cairo_set_line_width (cr, 2.0);
257 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
258 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
267 x = (get_width() - _pixbuf->get_width())/2.0;
268 y = (get_height() - _pixbuf->get_height())/2.0;
270 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
271 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
279 if (get_width() < 75) {
285 if ( ((_elements & Text)==Text) && !_text.empty()) {
289 if (_elements & Indicator) {
291 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
293 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
297 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
300 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
301 pango_cairo_show_layout (cr, _layout->gobj());
304 if (((_elements & Indicator)==Indicator)) {
306 /* move to the center of the indicator/led */
310 if (_elements & Text) {
312 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
314 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
317 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
321 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
322 cairo_set_source (cr, led_inset_pattern);
326 cairo_set_source_rgb (cr, 0, 0, 0);
327 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
331 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
332 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
339 /* a partially transparent gray layer to indicate insensitivity */
341 if ((visual_state() & Gtkmm2ext::Insensitive)) {
342 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
343 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.5);
348 bool show_reflection = (active_state() == Gtkmm2ext::ExplicitActive);
349 show_reflection &= !_flat_buttons;
350 show_reflection &= !((_elements & Indicator)==Indicator);
351 if ( show_reflection ) {
352 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
353 Gtkmm2ext::rounded_rectangle (cr, 2, get_height()*0.5-1, get_width()-4, rheight, _corner_radius-1);
354 cairo_set_source (cr, shine_pattern);
358 /* if requested, show hovering */
360 if (ARDOUR::Config->get_widget_prelight()) {
362 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
363 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
370 ArdourButton::set_diameter (float d)
372 _diameter = (d*2) + 5.0;
374 if (_diameter != 0.0) {
375 _fixed_diameter = true;
382 ArdourButton::set_corner_radius (float r)
389 ArdourButton::on_size_request (Gtk::Requisition* req)
394 CairoWidget::on_size_request (req);
396 if ((_elements & Text) && !_text.empty()) {
397 _layout->get_pixel_size (_text_width, _text_height);
398 if (_text_width + _diameter < 75) {
412 if ((_elements & Indicator) && _fixed_diameter) {
414 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
415 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
417 req->width = _text_width + lrint (_diameter) + xpad;
418 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
422 req->width = _pixbuf->get_width() + xpad;
423 req->height = _pixbuf->get_height() + ypad;
425 req->width = _text_width + xpad;
426 req->height = _text_height + ypad;
432 ArdourButton::set_colors ()
434 uint32_t start_color;
440 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
441 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
443 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
445 fill_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
446 border_color = ARDOUR_UI::config()->color_by_name ( "button border" );
449 cairo_pattern_destroy (shine_pattern);
454 cairo_pattern_destroy (fill_pattern);
458 if (fill_pattern_active) {
459 cairo_pattern_destroy (fill_pattern_active);
460 fill_pattern_active = 0;
463 if (_elements & Body) {
465 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
468 end_color = start_color;
470 end_color = fill_color_active;
472 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
479 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
480 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
481 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.1);
482 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.7, 1,1,1,0.2);
483 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
485 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
487 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
489 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
490 end_color = fill_color_inactive;
492 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
493 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
494 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
495 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
497 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
499 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
500 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
502 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
505 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
506 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
507 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
509 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
510 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
513 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
514 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 0, r/255.0,g/255.0,b/255.0, a/255.0);
515 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
516 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 1, r/255.0,g/255.0,b/255.0, a/255.0);
519 if (led_inset_pattern) {
520 cairo_pattern_destroy (led_inset_pattern);
523 if (reflection_pattern) {
524 cairo_pattern_destroy (reflection_pattern);
527 if (_elements & Indicator) {
528 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
529 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
530 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
532 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
533 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
534 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
537 /* text and LED colors */
539 if (active_state() == Gtkmm2ext::ExplicitActive || ((_tweaks & ImplicitUsesSolidColor) && active_state() == Gtkmm2ext::ImplicitActive)) {
540 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
541 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
543 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
544 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
547 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
552 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
562 ArdourButton::set_led_left (bool yn)
568 ArdourButton::on_button_press_event (GdkEventButton *ev)
570 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
571 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
572 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
577 if (_tweaks & ShowClick) {
578 set_active_state (Gtkmm2ext::ExplicitActive);
581 if (binding_proxy.button_press_handler (ev)) {
585 if (!_act_on_release) {
587 _action->activate ();
596 ArdourButton::on_button_release_event (GdkEventButton *ev)
598 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
599 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
600 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
601 signal_led_clicked(); /* EMIT SIGNAL */
606 if (_tweaks & ShowClick) {
607 unset_active_state ();
612 if (_act_on_release) {
614 _action->activate ();
624 ArdourButton::set_distinct_led_click (bool yn)
626 _distinct_led_click = yn;
631 ArdourButton::color_handler ()
638 ArdourButton::on_size_allocate (Allocation& alloc)
640 CairoWidget::on_size_allocate (alloc);
646 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
648 watch_connection.disconnect ();
649 binding_proxy.set_controllable (c);
653 ArdourButton::watch ()
655 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
658 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
662 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
666 ArdourButton::controllable_changed ()
668 float val = binding_proxy.get_controllable()->get_value();
670 if (fabs (val) >= 0.5f) {
671 set_active_state (Gtkmm2ext::ExplicitActive);
673 unset_active_state ();
678 ArdourButton::set_related_action (RefPtr<Action> act)
680 Gtkmm2ext::Activatable::set_related_action (act);
684 action_tooltip_changed ();
686 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
689 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
692 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
693 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
694 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
699 ArdourButton::action_toggled ()
701 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
704 if (tact->get_active()) {
705 set_active_state (Gtkmm2ext::ExplicitActive);
707 unset_active_state ();
713 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
719 ArdourButton::setup_led_rect ()
723 if (get_width() < 75) {
729 if (_elements & Indicator) {
730 _led_rect = new cairo_rectangle_t;
732 if (_elements & Text) {
734 _led_rect->x = text_margin;
736 _led_rect->x = get_width() - text_margin - _diameter/2.0;
740 _led_rect->x = get_width()/2.0 - _diameter/2.0;
743 _led_rect->y = get_height()/2.0 - _diameter/2.0;
744 _led_rect->width = _diameter;
745 _led_rect->height = _diameter;
754 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
761 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
763 bool changed = (_active_state != s);
764 CairoWidget::set_active_state (s);
771 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
773 bool changed = (_visual_state != s);
774 CairoWidget::set_visual_state (s);
781 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
785 if (ARDOUR::Config->get_widget_prelight()) {
789 return CairoWidget::on_enter_notify_event (ev);
793 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
797 if (ARDOUR::Config->get_widget_prelight()) {
801 return CairoWidget::on_leave_notify_event (ev);
805 ArdourButton::set_tweaks (Tweaks t)
814 ArdourButton::action_sensitivity_changed ()
816 if (_action->property_sensitive ()) {
817 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
819 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
826 ArdourButton::action_visibility_changed ()
828 if (_action->property_visible ()) {
836 ArdourButton::action_tooltip_changed ()
838 string str = _action->property_tooltip().get_value();
839 ARDOUR_UI::instance()->set_tip (*this, str);
843 ArdourButton::set_rounded_corner_mask (int mask)
850 ArdourButton::set_elements (Element e)
857 ArdourButton::add_elements (Element e)
859 _elements = (ArdourButton::Element) (_elements | e);
864 ArdourButton::set_flat_buttons (bool yn)