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_button.h"
34 #include "ardour_ui.h"
35 #include "global_signals.h"
47 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
48 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
49 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
51 ArdourButton::ArdourButton (Element e)
53 , _act_on_release (true)
57 , _corner_radius (9.0)
60 , led_inset_pattern (0)
61 , reflection_pattern (0)
63 , _fixed_diameter (true)
64 , _distinct_led_click (false)
67 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
68 StateChanged.connect (sigc::mem_fun (*this, &ArdourButton::state_handler));
71 ArdourButton::~ArdourButton()
77 ArdourButton::set_text (const std::string& str)
81 if (!_layout && !_text.empty()) {
82 _layout = Pango::Layout::create (get_pango_context());
85 _layout->set_text (str);
91 ArdourButton::set_markup (const std::string& str)
96 _layout = Pango::Layout::create (get_pango_context());
99 _layout->set_text (str);
104 ArdourButton::render (cairo_t* cr)
106 if (!_fixed_diameter) {
107 _diameter = std::min (_width, _height);
110 /* background fill. use parent window style, so that we fit in nicely.
113 Color c = get_parent_bg ();
115 cairo_rectangle (cr, 0, 0, _width, _height);
116 cairo_stroke_preserve (cr);
117 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
120 if (_elements & Edge) {
121 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
122 cairo_set_source (cr, edge_pattern);
126 if (_elements & Body) {
127 if (_elements & Edge) {
128 Gtkmm2ext::rounded_rectangle (cr, 1, 1, _width-2, _height-2, _corner_radius - 1.0);
130 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius - 1.0);
132 cairo_set_source (cr, fill_pattern);
146 if ((_elements & Text) && !_text.empty()) {
148 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
150 if (_elements & Indicator) {
152 cairo_move_to (cr, text_margin + _diameter + 4, _height/2.0 - _text_height/2.0);
154 cairo_move_to (cr, text_margin, _height/2.0 - _text_height/2.0);
158 cairo_move_to (cr, (_width - _text_width)/2.0, _height/2.0 - _text_height/2.0);
161 pango_cairo_show_layout (cr, _layout->gobj());
164 if (_elements & Indicator) {
166 /* move to the center of the indicator/led */
170 if (_elements & Text) {
172 cairo_translate (cr, text_margin + (_diameter/2.0), _height/2.0);
174 cairo_translate (cr, _width - ((_diameter/2.0) + 4.0), _height/2.0);
177 cairo_translate (cr, _width/2.0, _height/2.0);
181 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
182 cairo_set_source (cr, led_inset_pattern);
186 cairo_set_source_rgb (cr, 0, 0, 0);
187 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
191 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
192 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
196 cairo_scale(cr, 0.7, 0.7);
197 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
198 cairo_set_source (cr, reflection_pattern);
205 /* a partially transparent gray layer to indicate insensitivity */
207 if ((visual_state() & Insensitive)) {
208 cairo_rectangle (cr, 0, 0, _width, _height);
209 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
215 ArdourButton::state_handler ()
221 ArdourButton::set_diameter (float d)
223 _diameter = (d*2) + 5.0;
225 if (_diameter != 0.0) {
226 _fixed_diameter = true;
233 ArdourButton::set_corner_radius (float r)
240 ArdourButton::on_size_request (Gtk::Requisition* req)
245 CairoWidget::on_size_request (req);
247 if ((_elements & Text) && !_text.empty()) {
248 _layout->get_pixel_size (_text_width, _text_height);
249 if (_text_width + _diameter < 75) {
259 if ((_elements & Indicator) && _fixed_diameter) {
260 req->width = _text_width + lrint (_diameter) + xpad;
261 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
263 req->width = _text_width + xpad;
264 req->height = _text_height + ypad;
269 ArdourButton::set_colors ()
271 uint32_t start_color;
277 /* we use the edge of the button to show Selected state, so the
278 * color/pattern used there will vary depending on that
282 cairo_pattern_destroy (edge_pattern);
285 if (_elements & Edge) {
287 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
288 if (visual_state() & CairoWidget::Selected) {
289 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border start selected", get_name()));
290 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border end selected", get_name()));
292 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border start", get_name()));
293 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border end", get_name()));
295 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
296 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
297 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
298 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
302 /* the fill pattern is used to indicate Normal/Active/Mid state
306 cairo_pattern_destroy (fill_pattern);
309 if (_elements & Body) {
310 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
312 if (active_state() == Mid) {
313 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start mid", get_name()));
314 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end mid", get_name()));
315 } else if (active_state() == Active) {
316 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start active", get_name()));
317 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end active", get_name()));
319 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start", get_name()));
320 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end", get_name()));
322 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
323 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
324 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
325 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
328 if (led_inset_pattern) {
329 cairo_pattern_destroy (led_inset_pattern);
332 if (reflection_pattern) {
333 cairo_pattern_destroy (reflection_pattern);
336 if (_elements & Indicator) {
337 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
338 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
339 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
341 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
342 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
343 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
346 /* text and LED colors depend on Active/Normal/Mid */
348 if (active_state() == Active) {
349 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text active", get_name()));
350 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led active", get_name()));
351 } else if (active_state() == Mid) {
352 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text mid", get_name()));
353 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led active", get_name()));
355 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text", get_name()));
356 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led", get_name()));
359 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
364 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
374 ArdourButton::set_led_left (bool yn)
380 ArdourButton::on_button_press_event (GdkEventButton *ev)
382 if ((_elements & Indicator) && _led_rect) {
383 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
384 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
389 if (binding_proxy.button_press_handler (ev)) {
393 if (!_act_on_release) {
395 _action->activate ();
404 ArdourButton::on_button_release_event (GdkEventButton *ev)
406 if ((_elements & Indicator) && _distinct_led_click) {
408 /* if within LED, emit signal */
411 int top = lrint (_height/2.0 - _diameter/2.0);
412 int bottom = lrint (_height/2.0 + _diameter/2.0);
424 if (_elements & Text) {
428 left = _width - ((_diameter/2.0) + 4.0);
431 left = _width/2.0 - _diameter/2.0;
434 right = left + _diameter;
436 if (ev->x >= left && ev->x <= right && ev->y <= bottom && ev->y >= top) {
437 signal_led_clicked(); /* EMIT SIGNAL */
442 if (_act_on_release) {
444 _action->activate ();
453 ArdourButton::set_distinct_led_click (bool yn)
455 _distinct_led_click = yn;
460 ArdourButton::color_handler ()
467 ArdourButton::on_size_allocate (Allocation& alloc)
469 CairoWidget::on_size_allocate (alloc);
475 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
477 watch_connection.disconnect ();
478 binding_proxy.set_controllable (c);
482 ArdourButton::watch ()
484 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
487 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
491 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
495 ArdourButton::controllable_changed ()
497 float val = binding_proxy.get_controllable()->get_value();
499 if (fabs (val) >= 0.5f) {
500 set_active_state (CairoWidget::Active);
502 unset_active_state ();
507 ArdourButton::set_related_action (RefPtr<Action> act)
511 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
513 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
518 ArdourButton::action_toggled ()
520 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
523 if (tact->get_active()) {
524 set_active_state (CairoWidget::Active);
526 unset_active_state ();
532 ArdourButton::on_style_changed (const RefPtr<Style>&)
538 ArdourButton::setup_led_rect ()
540 if (_elements & Indicator) {
541 _led_rect = new cairo_rectangle_t;
543 if (_elements & Text) {
547 _led_rect->x = lrint (_width - 4 - _diameter/2.0);
550 _led_rect->x = _width/2.0 - (_diameter/2.0);
553 _led_rect->y = _height/2.0 - _diameter/2.0;
554 _led_rect->width = _diameter;
555 _led_rect->height = _diameter;