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"
49 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
50 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
51 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
52 bool ArdourButton::_flat_buttons = false;
54 ArdourButton::ArdourButton (Element e)
56 , _tweaks (Tweaks (0))
60 , _corner_radius (5.0)
63 , fill_color_active (0)
64 , fill_color_inactive (0)
66 , fill_pattern_active (0)
68 , led_inset_pattern (0)
69 , reflection_pattern (0)
71 , _act_on_release (true)
73 , _fixed_diameter (true)
74 , _distinct_led_click (false)
77 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
80 ArdourButton::ArdourButton (const std::string& str, Element e)
82 , _tweaks (Tweaks (0))
86 , _corner_radius (5.0)
89 , fill_color_active (0)
90 , fill_color_inactive (0)
92 , fill_pattern_active (0)
94 , led_inset_pattern (0)
95 , reflection_pattern (0)
97 , _act_on_release (true)
99 , _fixed_diameter (true)
100 , _distinct_led_click (false)
106 ArdourButton::~ArdourButton()
111 cairo_pattern_destroy (shine_pattern);
115 cairo_pattern_destroy (fill_pattern);
118 if (fill_pattern_active) {
119 cairo_pattern_destroy (fill_pattern_active);
122 if (led_inset_pattern) {
123 cairo_pattern_destroy (led_inset_pattern);
126 if (reflection_pattern) {
127 cairo_pattern_destroy (reflection_pattern);
133 ArdourButton::set_text (const std::string& str)
137 if (!_layout && !_text.empty()) {
138 _layout = Pango::Layout::create (get_pango_context());
142 _layout->set_text (str);
149 ArdourButton::set_markup (const std::string& str)
154 _layout = Pango::Layout::create (get_pango_context());
157 _layout->set_text (str);
162 ArdourButton::render (cairo_t* cr)
164 void (*rounded_function)(cairo_t*, double, double, double, double, double);
166 switch (_corner_mask) {
167 case 0x1: /* upper left only */
168 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
170 case 0x2: /* upper right only */
171 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
173 case 0x3: /* upper only */
174 rounded_function = Gtkmm2ext::rounded_top_rectangle;
176 /* should really have functions for lower right, lower left,
177 lower only, but for now, we don't
180 rounded_function = Gtkmm2ext::rounded_rectangle;
183 if (!_fixed_diameter) {
184 _diameter = std::min (get_width(), get_height());
189 if (_elements & Edge) {
190 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
191 UINT_TO_RGBA (border_color, &r, &g, &b, &a);
192 //cairo_set_source_rgba (cr, r/255.0,g/255.0,b/255.0,a/255.0); //TODO: why doesn't this work?
193 cairo_set_source_rgba (cr, 0,0,0,0.9);
197 if (_elements & Body) {
198 if (_elements & Edge) {
199 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.0);
201 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
204 if (active_state() == Gtkmm2ext::ImplicitActive) {
206 cairo_set_source (cr, fill_pattern);
207 cairo_fill_preserve (cr);
210 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
211 cairo_set_line_width (cr, 2.0);
212 // rounded_function (cr, 3, 3, get_width()-6, get_height()-6, _corner_radius - 1.0);
213 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
217 if (!_flat_buttons) {
218 rounded_function (cr, 2, 2, get_width()-4, get_height()/2-2, _corner_radius - 1.0);
219 cairo_set_source (cr, shine_pattern);
222 } else if (active_state() == Gtkmm2ext::ExplicitActive) {
225 cairo_set_source (cr, fill_pattern_active);
229 if (!_flat_buttons) {
230 rounded_function (cr, 2, 2, get_width()-4, get_height()/2-2, _corner_radius - 1.0);
231 cairo_set_source (cr, shine_pattern);
237 cairo_set_source (cr, fill_pattern);
241 if (!_flat_buttons) {
242 rounded_function (cr, 2, 2, get_width()-4, get_height()/2-2, _corner_radius - 1.0);
243 cairo_set_source (cr, shine_pattern);
252 x = (get_width() - _pixbuf->get_width())/2.0;
253 y = (get_height() - _pixbuf->get_height())/2.0;
255 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
256 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
264 if (get_width() < 75) {
270 if ((_elements & Text) && !_text.empty()) {
274 if (_elements & Indicator) {
276 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
278 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
282 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
285 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
286 pango_cairo_show_layout (cr, _layout->gobj());
289 if (_elements & Indicator) {
291 /* move to the center of the indicator/led */
295 if (_elements & Text) {
297 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
299 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
302 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
306 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
307 cairo_set_source (cr, led_inset_pattern);
311 cairo_set_source_rgb (cr, 0, 0, 0);
312 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
316 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
317 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
321 cairo_scale(cr, 0.7, 0.7);
322 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
323 cairo_set_source (cr, reflection_pattern);
331 /* a partially transparent gray layer to indicate insensitivity */
333 if ((visual_state() & Gtkmm2ext::Insensitive)) {
334 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
335 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
339 /* if requested, show hovering */
341 if (ARDOUR::Config->get_widget_prelight()) {
343 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
344 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
351 ArdourButton::set_diameter (float d)
353 _diameter = (d*2) + 5.0;
355 if (_diameter != 0.0) {
356 _fixed_diameter = true;
363 ArdourButton::set_corner_radius (float r)
370 ArdourButton::on_size_request (Gtk::Requisition* req)
375 CairoWidget::on_size_request (req);
377 if ((_elements & Text) && !_text.empty()) {
378 _layout->get_pixel_size (_text_width, _text_height);
379 if (_text_width + _diameter < 75) {
393 if ((_elements & Indicator) && _fixed_diameter) {
395 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
396 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
398 req->width = _text_width + lrint (_diameter) + xpad;
399 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
403 req->width = _pixbuf->get_width() + xpad;
404 req->height = _pixbuf->get_height() + ypad;
406 req->width = _text_width + xpad;
407 req->height = _text_height + ypad;
413 ArdourButton::set_colors ()
415 uint32_t start_color;
421 /* we use the edge of the button to show Selected state, so the
422 * color/pattern used there will vary depending on that
425 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
426 fill_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
427 border_color = ARDOUR_UI::config()->color_by_name ( "button border" );
430 cairo_pattern_destroy (shine_pattern);
435 cairo_pattern_destroy (fill_pattern);
439 if (fill_pattern_active) {
440 cairo_pattern_destroy (fill_pattern_active);
441 fill_pattern_active = 0;
444 if (_elements & Body) {
446 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()/2-2);
447 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
450 end_color = start_color;
452 end_color = fill_color_active;
454 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
461 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.1);
462 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.4);
463 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
465 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
466 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
468 end_color = start_color;
470 end_color = fill_color_inactive;
472 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
473 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
474 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
475 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
477 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
478 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
480 end_color = start_color;
482 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
484 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
485 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 0, r/255.0,g/255.0,b/255.0, a/255.0);
486 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
487 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 1, r/255.0,g/255.0,b/255.0, a/255.0);
490 if (led_inset_pattern) {
491 cairo_pattern_destroy (led_inset_pattern);
494 if (reflection_pattern) {
495 cairo_pattern_destroy (reflection_pattern);
498 if (_elements & Indicator) {
499 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
500 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
501 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
503 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
504 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
505 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
508 /* text and LED colors */
510 if (active_state() == Gtkmm2ext::ExplicitActive) {
511 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
512 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
514 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
515 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
518 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
523 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
533 ArdourButton::set_led_left (bool yn)
539 ArdourButton::on_button_press_event (GdkEventButton *ev)
541 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
542 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
543 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
548 if (_tweaks & ShowClick) {
549 set_active_state (Gtkmm2ext::ExplicitActive);
552 if (binding_proxy.button_press_handler (ev)) {
556 if (!_act_on_release) {
558 _action->activate ();
567 ArdourButton::on_button_release_event (GdkEventButton *ev)
569 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
570 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
571 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
572 signal_led_clicked(); /* EMIT SIGNAL */
577 if (_tweaks & ShowClick) {
578 unset_active_state ();
583 if (_act_on_release) {
585 _action->activate ();
595 ArdourButton::set_distinct_led_click (bool yn)
597 _distinct_led_click = yn;
602 ArdourButton::color_handler ()
609 ArdourButton::on_size_allocate (Allocation& alloc)
611 CairoWidget::on_size_allocate (alloc);
617 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
619 watch_connection.disconnect ();
620 binding_proxy.set_controllable (c);
624 ArdourButton::watch ()
626 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
629 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
633 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
637 ArdourButton::controllable_changed ()
639 float val = binding_proxy.get_controllable()->get_value();
641 if (fabs (val) >= 0.5f) {
642 set_active_state (Gtkmm2ext::ExplicitActive);
644 unset_active_state ();
649 ArdourButton::set_related_action (RefPtr<Action> act)
651 Gtkmm2ext::Activatable::set_related_action (act);
655 action_tooltip_changed ();
657 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
660 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
663 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
664 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
665 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
670 ArdourButton::action_toggled ()
672 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
675 if (tact->get_active()) {
676 set_active_state (Gtkmm2ext::ExplicitActive);
678 unset_active_state ();
684 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
690 ArdourButton::setup_led_rect ()
694 if (get_width() < 75) {
700 if (_elements & Indicator) {
701 _led_rect = new cairo_rectangle_t;
703 if (_elements & Text) {
705 _led_rect->x = text_margin;
707 _led_rect->x = get_width() - text_margin - _diameter/2.0;
711 _led_rect->x = get_width()/2.0 - _diameter/2.0;
714 _led_rect->y = get_height()/2.0 - _diameter/2.0;
715 _led_rect->width = _diameter;
716 _led_rect->height = _diameter;
725 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
732 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
734 bool changed = (_active_state != s);
735 CairoWidget::set_active_state (s);
742 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
744 bool changed = (_visual_state != s);
745 CairoWidget::set_visual_state (s);
752 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
756 if (ARDOUR::Config->get_widget_prelight()) {
760 return CairoWidget::on_enter_notify_event (ev);
764 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
768 if (ARDOUR::Config->get_widget_prelight()) {
772 return CairoWidget::on_leave_notify_event (ev);
776 ArdourButton::set_tweaks (Tweaks t)
785 ArdourButton::action_sensitivity_changed ()
787 if (_action->property_sensitive ()) {
788 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
790 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
797 ArdourButton::action_visibility_changed ()
799 if (_action->property_visible ()) {
807 ArdourButton::action_tooltip_changed ()
809 string str = _action->property_tooltip().get_value();
810 ARDOUR_UI::instance()->set_tip (*this, str);
814 ArdourButton::set_rounded_corner_mask (int mask)
821 ArdourButton::set_elements (Element e)
828 ArdourButton::set_flat_buttons (bool yn)