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 cairo_set_source (cr, fill_pattern);
209 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
210 cairo_set_line_width (cr, 1.0);
211 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 1.5);
212 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
215 } else if (active_state() == Gtkmm2ext::ExplicitActive || ((_elements & Indicator)==Indicator) ) {
218 cairo_set_source (cr, fill_pattern_active);
224 cairo_set_source (cr, fill_pattern);
230 if ( ((_elements & FlatFace)==FlatFace) && (active_state() != Gtkmm2ext::ExplicitActive) ) {
232 if ( !_flat_buttons ) {
233 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
234 Gtkmm2ext::rounded_rectangle (cr, 2, 3, get_width()-4, rheight, _corner_radius-1);
235 cairo_set_source (cr, shine_pattern);
239 if (active_state() == Gtkmm2ext::ExplicitActive) {
241 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
242 cairo_set_line_width (cr, 2.0);
243 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
244 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
249 UINT_TO_RGBA (fill_color_inactive, &r, &g, &b, &a);
250 cairo_set_line_width (cr, 2.0);
251 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
252 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
261 x = (get_width() - _pixbuf->get_width())/2.0;
262 y = (get_height() - _pixbuf->get_height())/2.0;
264 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
265 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
273 if (get_width() < 75) {
279 if ( ((_elements & Text)==Text) && !_text.empty()) {
283 if (_elements & Indicator) {
285 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
287 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
291 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
294 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
295 pango_cairo_show_layout (cr, _layout->gobj());
298 if (((_elements & Indicator)==Indicator)) {
300 /* move to the center of the indicator/led */
304 if (_elements & Text) {
306 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
308 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
311 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
315 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
316 cairo_set_source (cr, led_inset_pattern);
320 cairo_set_source_rgb (cr, 0, 0, 0);
321 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
325 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
326 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
333 /* a partially transparent gray layer to indicate insensitivity */
335 if ((visual_state() & Gtkmm2ext::Insensitive)) {
336 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
337 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.5);
342 bool show_reflection = (active_state() == Gtkmm2ext::ExplicitActive);
343 show_reflection &= !_flat_buttons;
344 show_reflection &= !((_elements & Indicator)==Indicator);
345 if ( show_reflection ) {
346 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
347 Gtkmm2ext::rounded_rectangle (cr, 2, get_height()*0.5-1, get_width()-4, rheight, _corner_radius-1);
348 cairo_set_source (cr, shine_pattern);
352 /* if requested, show hovering */
354 if (ARDOUR::Config->get_widget_prelight()) {
356 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
357 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
364 ArdourButton::set_diameter (float d)
366 _diameter = (d*2) + 5.0;
368 if (_diameter != 0.0) {
369 _fixed_diameter = true;
376 ArdourButton::set_corner_radius (float r)
383 ArdourButton::on_size_request (Gtk::Requisition* req)
388 CairoWidget::on_size_request (req);
390 if ((_elements & Text) && !_text.empty()) {
391 _layout->get_pixel_size (_text_width, _text_height);
392 if (_text_width + _diameter < 75) {
406 if ((_elements & Indicator) && _fixed_diameter) {
408 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
409 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
411 req->width = _text_width + lrint (_diameter) + xpad;
412 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
416 req->width = _pixbuf->get_width() + xpad;
417 req->height = _pixbuf->get_height() + ypad;
419 req->width = _text_width + xpad;
420 req->height = _text_height + ypad;
426 ArdourButton::set_colors ()
428 uint32_t start_color;
434 /* we use the edge of the button to show Selected state, so the
435 * color/pattern used there will vary depending on that
438 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
439 fill_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
440 border_color = ARDOUR_UI::config()->color_by_name ( "button border" );
443 cairo_pattern_destroy (shine_pattern);
448 cairo_pattern_destroy (fill_pattern);
452 if (fill_pattern_active) {
453 cairo_pattern_destroy (fill_pattern_active);
454 fill_pattern_active = 0;
457 if (_elements & Body) {
459 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
462 end_color = start_color;
464 end_color = fill_color_active;
466 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
473 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
474 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
475 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.1);
476 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.7, 1,1,1,0.2);
477 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
479 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
480 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
482 end_color = start_color;
484 end_color = fill_color_inactive;
486 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
487 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
488 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
489 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
491 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
492 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
494 end_color = start_color;
496 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
498 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
499 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 0, r/255.0,g/255.0,b/255.0, a/255.0);
500 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
501 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 1, r/255.0,g/255.0,b/255.0, a/255.0);
504 if (led_inset_pattern) {
505 cairo_pattern_destroy (led_inset_pattern);
508 if (reflection_pattern) {
509 cairo_pattern_destroy (reflection_pattern);
512 if (_elements & Indicator) {
513 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
514 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
515 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
517 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
518 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
519 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
522 /* text and LED colors */
524 if (active_state() == Gtkmm2ext::ExplicitActive) {
525 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
526 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
528 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
529 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
532 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
537 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
547 ArdourButton::set_led_left (bool yn)
553 ArdourButton::on_button_press_event (GdkEventButton *ev)
555 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
556 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
557 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
562 if (_tweaks & ShowClick) {
563 set_active_state (Gtkmm2ext::ExplicitActive);
566 if (binding_proxy.button_press_handler (ev)) {
570 if (!_act_on_release) {
572 _action->activate ();
581 ArdourButton::on_button_release_event (GdkEventButton *ev)
583 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
584 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
585 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
586 signal_led_clicked(); /* EMIT SIGNAL */
591 if (_tweaks & ShowClick) {
592 unset_active_state ();
597 if (_act_on_release) {
599 _action->activate ();
609 ArdourButton::set_distinct_led_click (bool yn)
611 _distinct_led_click = yn;
616 ArdourButton::color_handler ()
623 ArdourButton::on_size_allocate (Allocation& alloc)
625 CairoWidget::on_size_allocate (alloc);
631 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
633 watch_connection.disconnect ();
634 binding_proxy.set_controllable (c);
638 ArdourButton::watch ()
640 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
643 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
647 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
651 ArdourButton::controllable_changed ()
653 float val = binding_proxy.get_controllable()->get_value();
655 if (fabs (val) >= 0.5f) {
656 set_active_state (Gtkmm2ext::ExplicitActive);
658 unset_active_state ();
663 ArdourButton::set_related_action (RefPtr<Action> act)
665 Gtkmm2ext::Activatable::set_related_action (act);
669 action_tooltip_changed ();
671 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
674 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
677 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
678 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
679 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
684 ArdourButton::action_toggled ()
686 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
689 if (tact->get_active()) {
690 set_active_state (Gtkmm2ext::ExplicitActive);
692 unset_active_state ();
698 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
704 ArdourButton::setup_led_rect ()
708 if (get_width() < 75) {
714 if (_elements & Indicator) {
715 _led_rect = new cairo_rectangle_t;
717 if (_elements & Text) {
719 _led_rect->x = text_margin;
721 _led_rect->x = get_width() - text_margin - _diameter/2.0;
725 _led_rect->x = get_width()/2.0 - _diameter/2.0;
728 _led_rect->y = get_height()/2.0 - _diameter/2.0;
729 _led_rect->width = _diameter;
730 _led_rect->height = _diameter;
739 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
746 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
748 bool changed = (_active_state != s);
749 CairoWidget::set_active_state (s);
756 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
758 bool changed = (_visual_state != s);
759 CairoWidget::set_visual_state (s);
766 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
770 if (ARDOUR::Config->get_widget_prelight()) {
774 return CairoWidget::on_enter_notify_event (ev);
778 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
782 if (ARDOUR::Config->get_widget_prelight()) {
786 return CairoWidget::on_leave_notify_event (ev);
790 ArdourButton::set_tweaks (Tweaks t)
799 ArdourButton::action_sensitivity_changed ()
801 if (_action->property_sensitive ()) {
802 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
804 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
811 ArdourButton::action_visibility_changed ()
813 if (_action->property_visible ()) {
821 ArdourButton::action_tooltip_changed ()
823 string str = _action->property_tooltip().get_value();
824 ARDOUR_UI::instance()->set_tip (*this, str);
828 ArdourButton::set_rounded_corner_mask (int mask)
835 ArdourButton::set_elements (Element e)
842 ArdourButton::add_elements (Element e)
844 _elements = (ArdourButton::Element) (_elements | e);
849 ArdourButton::set_flat_buttons (bool yn)