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)
94 ArdourButton::~ArdourButton()
100 ArdourButton::set_text (const std::string& str)
104 if (!_layout && !_text.empty()) {
105 _layout = Pango::Layout::create (get_pango_context());
109 _layout->set_text (str);
116 ArdourButton::set_markup (const std::string& str)
121 _layout = Pango::Layout::create (get_pango_context());
124 _layout->set_text (str);
129 ArdourButton::render (cairo_t* cr)
131 if (!_fixed_diameter) {
132 _diameter = std::min (_width, _height);
135 /* background fill. use parent window style, so that we fit in nicely.
138 Color c = get_parent_bg ();
140 cairo_rectangle (cr, 0, 0, _width, _height);
141 cairo_stroke_preserve (cr);
142 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
145 if (_elements & Edge) {
146 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
147 cairo_set_source (cr, edge_pattern);
151 if (_elements & Body) {
152 if (_elements & Edge) {
153 Gtkmm2ext::rounded_rectangle (cr, 1, 1, _width-2, _height-2, _corner_radius - 1.0);
155 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius - 1.0);
157 cairo_set_source (cr, fill_pattern);
164 x = (_width - _pixbuf->get_width())/2.0;
165 y = (_height - _pixbuf->get_height())/2.0;
167 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
168 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
182 if ((_elements & Text) && !_text.empty()) {
184 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
186 if (_elements & Indicator) {
188 cairo_move_to (cr, text_margin + _diameter + 4, _height/2.0 - _text_height/2.0);
190 cairo_move_to (cr, text_margin, _height/2.0 - _text_height/2.0);
194 cairo_move_to (cr, (_width - _text_width)/2.0, _height/2.0 - _text_height/2.0);
197 pango_cairo_show_layout (cr, _layout->gobj());
200 if (_elements & Indicator) {
202 /* move to the center of the indicator/led */
206 if (_elements & Text) {
208 cairo_translate (cr, text_margin + (_diameter/2.0), _height/2.0);
210 cairo_translate (cr, _width - ((_diameter/2.0) + 4.0), _height/2.0);
213 cairo_translate (cr, _width/2.0, _height/2.0);
217 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
218 cairo_set_source (cr, led_inset_pattern);
222 cairo_set_source_rgb (cr, 0, 0, 0);
223 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
227 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
228 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
232 cairo_scale(cr, 0.7, 0.7);
233 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
234 cairo_set_source (cr, reflection_pattern);
242 /* a partially transparent gray layer to indicate insensitivity */
244 if ((visual_state() & Gtkmm2ext::Insensitive)) {
245 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
246 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
250 /* if requested, show hovering */
252 if (ARDOUR::Config->get_widget_prelight()) {
254 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
255 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
262 ArdourButton::set_diameter (float d)
264 _diameter = (d*2) + 5.0;
266 if (_diameter != 0.0) {
267 _fixed_diameter = true;
274 ArdourButton::set_corner_radius (float r)
281 ArdourButton::on_size_request (Gtk::Requisition* req)
286 CairoWidget::on_size_request (req);
288 if ((_elements & Text) && !_text.empty()) {
289 _layout->get_pixel_size (_text_width, _text_height);
290 if (_text_width + _diameter < 75) {
304 if ((_elements & Indicator) && _fixed_diameter) {
306 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
307 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
309 req->width = _text_width + lrint (_diameter) + xpad;
310 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
314 req->width = _pixbuf->get_width() + xpad;
315 req->height = _pixbuf->get_height() + ypad;
317 req->width = _text_width + xpad;
318 req->height = _text_height + ypad;
324 ArdourButton::set_colors ()
326 uint32_t start_color;
332 /* we use the edge of the button to show Selected state, so the
333 * color/pattern used there will vary depending on that
337 cairo_pattern_destroy (edge_pattern);
340 if (_elements & Edge) {
342 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
343 if (visual_state() & Gtkmm2ext::Selected) {
344 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
345 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
347 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
348 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
350 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
351 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
352 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
353 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
357 /* the fill pattern is used to indicate Normal/Active/Mid state
361 cairo_pattern_destroy (fill_pattern);
364 if (_elements & Body) {
365 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
367 if (active_state() == Gtkmm2ext::Mid) {
368 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start mid", get_name()));
369 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end mid", get_name()));
370 } else if (active_state() == Gtkmm2ext::Active) {
371 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
372 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
374 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
375 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
377 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
378 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
379 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
380 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
382 cerr << "Button color for " << get_name() << ": " << hex << start_color << " .. " << end_color << dec << endl;
386 if (led_inset_pattern) {
387 cairo_pattern_destroy (led_inset_pattern);
390 if (reflection_pattern) {
391 cairo_pattern_destroy (reflection_pattern);
394 if (_elements & Indicator) {
395 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
396 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
397 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
399 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
400 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
401 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
404 /* text and LED colors depend on Active/Normal/Mid */
406 if (active_state() == Gtkmm2ext::Active) {
407 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
408 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
409 } else if (active_state() == Gtkmm2ext::Mid) {
410 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text mid", get_name()));
411 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led mid", get_name()));
413 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
414 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
417 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
422 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
432 ArdourButton::set_led_left (bool yn)
438 ArdourButton::on_button_press_event (GdkEventButton *ev)
440 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
441 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
442 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
447 if (_tweaks & ShowClick) {
448 set_active_state (Gtkmm2ext::Active);
451 if (binding_proxy.button_press_handler (ev)) {
455 if (!_act_on_release) {
457 _action->activate ();
466 ArdourButton::on_button_release_event (GdkEventButton *ev)
468 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
469 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
470 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
471 signal_led_clicked(); /* EMIT SIGNAL */
476 if (_tweaks & ShowClick) {
477 unset_active_state ();
482 if (_act_on_release) {
484 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
485 _action->activate ();
495 ArdourButton::set_distinct_led_click (bool yn)
497 _distinct_led_click = yn;
502 ArdourButton::color_handler ()
509 ArdourButton::on_size_allocate (Allocation& alloc)
511 CairoWidget::on_size_allocate (alloc);
517 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
519 watch_connection.disconnect ();
520 binding_proxy.set_controllable (c);
524 ArdourButton::watch ()
526 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
529 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
533 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
537 ArdourButton::controllable_changed ()
539 float val = binding_proxy.get_controllable()->get_value();
541 if (fabs (val) >= 0.5f) {
542 set_active_state (Gtkmm2ext::Active);
544 unset_active_state ();
549 ArdourButton::set_related_action (RefPtr<Action> act)
555 string str = _action->property_tooltip().get_value();
557 ARDOUR_UI::instance()->set_tip (*this, str);
560 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
562 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
565 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
566 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
567 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
572 ArdourButton::action_toggled ()
574 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
577 if (tact->get_active()) {
578 set_active_state (Gtkmm2ext::Active);
580 unset_active_state ();
586 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
592 ArdourButton::setup_led_rect ()
602 if (_elements & Indicator) {
603 _led_rect = new cairo_rectangle_t;
605 if (_elements & Text) {
607 _led_rect->x = text_margin;
609 _led_rect->x = _width - text_margin - _diameter/2.0;
613 _led_rect->x = _width/2.0 - _diameter/2.0;
616 _led_rect->y = _height/2.0 - _diameter/2.0;
617 _led_rect->width = _diameter;
618 _led_rect->height = _diameter;
627 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
634 ArdourButton::set_active (bool yn)
636 /* this is an API simplification for buttons
637 that only use the Active and Normal states.
641 set_active_state (Gtkmm2ext::Active);
643 unset_active_state ();
648 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
650 bool changed = (_active_state != s);
651 CairoWidget::set_active_state (s);
658 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
660 bool changed = (_visual_state != s);
661 CairoWidget::set_visual_state (s);
668 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
672 if (ARDOUR::Config->get_widget_prelight()) {
676 return CairoWidget::on_enter_notify_event (ev);
680 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
684 if (ARDOUR::Config->get_widget_prelight()) {
688 return CairoWidget::on_leave_notify_event (ev);
692 ArdourButton::set_tweaks (Tweaks t)
701 ArdourButton::action_sensitivity_changed ()
703 if (_action->property_sensitive ()) {
704 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
706 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
713 ArdourButton::action_visibility_changed ()
715 if (_action->property_visible ()) {
723 ArdourButton::action_tooltip_changed ()
725 string str = _action->property_tooltip().get_value();
726 ARDOUR_UI::instance()->set_tip (*this, str);