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);
53 ArdourButton::ArdourButton (Element e)
55 , _tweaks (Tweaks (0))
56 , _act_on_release (true)
60 , _corner_radius (9.0)
63 , led_inset_pattern (0)
64 , reflection_pattern (0)
66 , _fixed_diameter (true)
67 , _distinct_led_click (false)
71 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
74 ArdourButton::ArdourButton (const std::string& str, Element e)
76 , _act_on_release (true)
80 , _corner_radius (9.0)
83 , led_inset_pattern (0)
84 , reflection_pattern (0)
86 , _fixed_diameter (true)
87 , _distinct_led_click (false)
93 ArdourButton::~ArdourButton()
99 ArdourButton::set_text (const std::string& str)
103 if (!_layout && !_text.empty()) {
104 _layout = Pango::Layout::create (get_pango_context());
108 _layout->set_text (str);
115 ArdourButton::set_markup (const std::string& str)
120 _layout = Pango::Layout::create (get_pango_context());
123 _layout->set_text (str);
128 ArdourButton::render (cairo_t* cr)
130 if (!_fixed_diameter) {
131 _diameter = std::min (_width, _height);
134 /* background fill. use parent window style, so that we fit in nicely.
137 Color c = get_parent_bg ();
139 cairo_rectangle (cr, 0, 0, _width, _height);
140 cairo_stroke_preserve (cr);
141 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
144 if (_elements & Edge) {
145 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
146 cairo_set_source (cr, edge_pattern);
150 if (_elements & Body) {
151 if (_elements & Edge) {
152 Gtkmm2ext::rounded_rectangle (cr, 1, 1, _width-2, _height-2, _corner_radius - 1.0);
154 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius - 1.0);
156 cairo_set_source (cr, fill_pattern);
163 x = (_width - _pixbuf->get_width())/2.0;
164 y = (_height - _pixbuf->get_height())/2.0;
166 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
167 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
181 if ((_elements & Text) && !_text.empty()) {
183 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
185 if (_elements & Indicator) {
187 cairo_move_to (cr, text_margin + _diameter + 4, _height/2.0 - _text_height/2.0);
189 cairo_move_to (cr, text_margin, _height/2.0 - _text_height/2.0);
193 cairo_move_to (cr, (_width - _text_width)/2.0, _height/2.0 - _text_height/2.0);
196 pango_cairo_show_layout (cr, _layout->gobj());
199 if (_elements & Indicator) {
201 /* move to the center of the indicator/led */
205 if (_elements & Text) {
207 cairo_translate (cr, text_margin + (_diameter/2.0), _height/2.0);
209 cairo_translate (cr, _width - ((_diameter/2.0) + 4.0), _height/2.0);
212 cairo_translate (cr, _width/2.0, _height/2.0);
216 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
217 cairo_set_source (cr, led_inset_pattern);
221 cairo_set_source_rgb (cr, 0, 0, 0);
222 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
226 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
227 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
231 cairo_scale(cr, 0.7, 0.7);
232 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
233 cairo_set_source (cr, reflection_pattern);
241 /* a partially transparent gray layer to indicate insensitivity */
243 if ((visual_state() & Gtkmm2ext::Insensitive)) {
244 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
245 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
249 /* if requested, show hovering */
251 if (ARDOUR::Config->get_widget_prelight()) {
253 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
254 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
261 ArdourButton::set_diameter (float d)
263 _diameter = (d*2) + 5.0;
265 if (_diameter != 0.0) {
266 _fixed_diameter = true;
273 ArdourButton::set_corner_radius (float r)
280 ArdourButton::on_size_request (Gtk::Requisition* req)
285 CairoWidget::on_size_request (req);
287 if ((_elements & Text) && !_text.empty()) {
288 _layout->get_pixel_size (_text_width, _text_height);
289 if (_text_width + _diameter < 75) {
303 if ((_elements & Indicator) && _fixed_diameter) {
305 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
306 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
308 req->width = _text_width + lrint (_diameter) + xpad;
309 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
313 req->width = _pixbuf->get_width() + xpad;
314 req->height = _pixbuf->get_height() + ypad;
316 req->width = _text_width + xpad;
317 req->height = _text_height + ypad;
323 ArdourButton::set_colors ()
325 uint32_t start_color;
331 /* we use the edge of the button to show Selected state, so the
332 * color/pattern used there will vary depending on that
336 cairo_pattern_destroy (edge_pattern);
339 if (_elements & Edge) {
341 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
342 if (visual_state() & Gtkmm2ext::Selected) {
343 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
344 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
346 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
347 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
349 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
350 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
351 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
352 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
356 /* the fill pattern is used to indicate Normal/Active/Mid state
360 cairo_pattern_destroy (fill_pattern);
363 if (_elements & Body) {
364 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
366 if (active_state() == Gtkmm2ext::Mid) {
367 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start mid", get_name()));
368 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end mid", get_name()));
369 } else if (active_state() == Gtkmm2ext::Active) {
370 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
371 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
373 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
374 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
376 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
377 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
378 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
379 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
382 if (led_inset_pattern) {
383 cairo_pattern_destroy (led_inset_pattern);
386 if (reflection_pattern) {
387 cairo_pattern_destroy (reflection_pattern);
390 if (_elements & Indicator) {
391 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
392 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
393 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
395 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
396 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
397 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
400 /* text and LED colors depend on Active/Normal/Mid */
402 if (active_state() == Gtkmm2ext::Active) {
403 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
404 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
405 } else if (active_state() == Gtkmm2ext::Mid) {
406 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text mid", get_name()));
407 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led mid", get_name()));
409 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
410 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
413 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
418 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
428 ArdourButton::set_led_left (bool yn)
434 ArdourButton::on_button_press_event (GdkEventButton *ev)
436 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
437 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
438 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
443 if (_tweaks & ShowClick) {
444 set_active_state (Gtkmm2ext::Active);
447 if (binding_proxy.button_press_handler (ev)) {
451 if (!_act_on_release) {
453 _action->activate ();
462 ArdourButton::on_button_release_event (GdkEventButton *ev)
464 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
465 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
466 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
467 signal_led_clicked(); /* EMIT SIGNAL */
472 if (_tweaks & ShowClick) {
473 unset_active_state ();
478 if (_act_on_release) {
480 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
481 _action->activate ();
491 ArdourButton::set_distinct_led_click (bool yn)
493 _distinct_led_click = yn;
498 ArdourButton::color_handler ()
505 ArdourButton::on_size_allocate (Allocation& alloc)
507 CairoWidget::on_size_allocate (alloc);
513 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
515 watch_connection.disconnect ();
516 binding_proxy.set_controllable (c);
520 ArdourButton::watch ()
522 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
525 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
529 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
533 ArdourButton::controllable_changed ()
535 float val = binding_proxy.get_controllable()->get_value();
537 if (fabs (val) >= 0.5f) {
538 set_active_state (Gtkmm2ext::Active);
540 unset_active_state ();
545 ArdourButton::set_related_action (RefPtr<Action> act)
551 string str = _action->property_tooltip().get_value();
553 ARDOUR_UI::instance()->set_tip (*this, str);
556 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
558 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
561 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
562 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
563 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
568 ArdourButton::action_toggled ()
570 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
573 if (tact->get_active()) {
574 set_active_state (Gtkmm2ext::Active);
576 unset_active_state ();
582 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
588 ArdourButton::setup_led_rect ()
598 if (_elements & Indicator) {
599 _led_rect = new cairo_rectangle_t;
601 if (_elements & Text) {
603 _led_rect->x = text_margin;
605 _led_rect->x = _width - text_margin - _diameter/2.0;
609 _led_rect->x = _width/2.0 - _diameter/2.0;
612 _led_rect->y = _height/2.0 - _diameter/2.0;
613 _led_rect->width = _diameter;
614 _led_rect->height = _diameter;
623 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
630 ArdourButton::set_active (bool yn)
632 /* this is an API simplification for buttons
633 that only use the Active and Normal states.
637 set_active_state (Gtkmm2ext::Active);
639 unset_active_state ();
644 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
646 bool changed = (_active_state != s);
647 CairoWidget::set_active_state (s);
654 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
656 bool changed = (_visual_state != s);
657 CairoWidget::set_visual_state (s);
664 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
668 if (ARDOUR::Config->get_widget_prelight()) {
672 return CairoWidget::on_enter_notify_event (ev);
676 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
680 if (ARDOUR::Config->get_widget_prelight()) {
684 return CairoWidget::on_leave_notify_event (ev);
688 ArdourButton::set_tweaks (Tweaks t)
697 ArdourButton::action_sensitivity_changed ()
699 if (_action->property_sensitive ()) {
700 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
702 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
709 ArdourButton::action_visibility_changed ()
711 if (_action->property_visible ()) {
719 ArdourButton::action_tooltip_changed ()
721 string str = _action->property_tooltip().get_value();
722 ARDOUR_UI::instance()->set_tip (*this, str);