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"
28 #include "pbd/stacktrace.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
34 #include "ardour/rc_configuration.h" // for widget prelight preference
36 #include "canvas/utils.h"
37 #include "canvas/colors.h"
39 #include "ardour_button.h"
40 #include "ardour_ui.h"
41 #include "global_signals.h"
45 #define BASELINESTRETCH (1.25)
46 #define TRACKHEADERBTNW (3.10)
56 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
57 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
58 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
60 ArdourButton::ArdourButton (Element e)
62 , _icon (ArdourButton::NoIcon)
63 , _tweaks (Tweaks (0))
64 , _char_pixel_width (0)
65 , _char_pixel_height (0)
66 , _char_avg_pixel_width (0)
70 , _corner_radius (2.5)
75 , fill_inactive_color (0)
76 , fill_active_color (0)
77 , text_active_color(0)
78 , text_inactive_color(0)
80 , led_inactive_color(0)
81 , led_custom_color (0)
82 , use_custom_led_color (false)
85 , led_inset_pattern (0)
87 , _act_on_release (true)
89 , _distinct_led_click (false)
92 , _fixed_colors_set (false)
93 , _fallthrough_to_parent (false)
94 , _layout_ellipsize_width (-1)
95 , _ellipsis (Pango::ELLIPSIZE_NONE)
96 , _update_colors (true)
99 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
102 ArdourButton::ArdourButton (const std::string& str, Element e)
104 , _tweaks (Tweaks (0))
108 , _corner_radius (2.5)
113 , fill_inactive_color (0)
114 , fill_active_color (0)
115 , text_active_color(0)
116 , text_inactive_color(0)
117 , led_active_color(0)
118 , led_inactive_color(0)
119 , led_custom_color (0)
120 , use_custom_led_color (false)
122 , concave_pattern (0)
123 , led_inset_pattern (0)
125 , _act_on_release (true)
127 , _distinct_led_click (false)
130 , _fixed_colors_set (false)
131 , _fallthrough_to_parent (false)
132 , _layout_ellipsize_width (-1)
133 , _ellipsis (Pango::ELLIPSIZE_NONE)
134 , _update_colors (true)
135 , _pattern_height (0)
138 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
139 ARDOUR_UI_UTILS::DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
142 ArdourButton::~ArdourButton()
146 if (convex_pattern) {
147 cairo_pattern_destroy (convex_pattern);
150 if (concave_pattern) {
151 cairo_pattern_destroy (concave_pattern);
154 if (led_inset_pattern) {
155 cairo_pattern_destroy (led_inset_pattern);
160 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
164 _layout->set_font_description (fd);
170 ArdourButton::set_text (const std::string& str)
173 if (!is_realized()) {
177 if (_layout && _layout->get_text() != _text) {
178 _layout->set_text (_text);
184 ArdourButton::set_angle (const double angle)
190 ArdourButton::set_alignment (const float xa, const float ya)
197 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
202 if (_update_colors) {
205 if (get_height() != _pattern_height) {
209 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
210 text_color = text_active_color;
211 led_color = led_active_color;
213 text_color = text_inactive_color;
214 led_color = led_inactive_color;
217 if (use_custom_led_color) {
218 led_color = led_custom_color;
221 void (*rounded_function)(cairo_t*, double, double, double, double, double);
223 switch (_corner_mask) {
224 case 0x1: /* upper left only */
225 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
227 case 0x2: /* upper right only */
228 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
230 case 0x3: /* upper only */
231 rounded_function = Gtkmm2ext::rounded_top_rectangle;
233 /* should really have functions for lower right, lower left,
234 lower only, but for now, we don't
237 rounded_function = Gtkmm2ext::rounded_rectangle;
240 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
241 if ((_elements & (Body|Edge)) == (Body|Edge)) {
242 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
243 cairo_set_source_rgba (cr, 0, 0, 0, 1);
248 if ((_elements & Body)==Body) {
249 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
250 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
251 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
253 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
255 ArdourCanvas::set_source_rgba (cr, fill_active_color);
257 } else { //inactive, or it has an indicator
259 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
264 // IMPLICIT ACTIVE: draw a border of the active color
265 if ((_elements & Body)==Body) {
266 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
267 cairo_set_line_width (cr, 2.0);
268 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
269 ArdourCanvas::set_source_rgba (cr, fill_active_color);
274 //show the "convex" or "concave" gradient
275 if (!_flat_buttons) {
276 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
278 cairo_set_source (cr, concave_pattern);
279 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
282 cairo_set_source (cr, convex_pattern);
283 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
288 #define VECTORICONSTROKEFILL \
289 cairo_set_line_width(cr, 1.5); \
290 cairo_set_source_rgba (cr, 0, 0, 0, 1.0); \
291 cairo_stroke_preserve(cr); \
292 cairo_set_source_rgba (cr, 1, 1, 1, 1.0); \
297 double x = rint((get_width() - _pixbuf->get_width()) * .5);
298 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
299 #if 0 // DEBUG style (print on hover)
300 if (_hovering || (_elements & Inactive)) {
301 printf("%s: p:%dx%d (%dx%d)\n",
303 _pixbuf->get_width(), _pixbuf->get_height(),
304 get_width(), get_height());
307 if (_elements & Menu) {
308 //if this is a DropDown with an icon, then we need to
309 //move the icon left slightly to accomomodate the arrow
312 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
313 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
316 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
317 if ((_elements & VectorIcon) && _icon == RecTapeMode) {
318 const double x = get_width() * .5;
319 const double y = get_height() * .5;
320 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
321 const double slit = .11 * M_PI;
323 cairo_translate(cr, x, y);
325 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
326 if (active_state() == Gtkmm2ext::ExplicitActive)
327 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
329 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
330 cairo_fill_preserve(cr);
331 cairo_set_source_rgba (cr, .0, .0, .0, .5);
332 cairo_set_line_width(cr, 1);
336 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
338 cairo_rotate (cr, -.5 * M_PI);
339 cairo_move_to(cr, 0, 0);
340 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
341 cairo_line_to(cr, 0, 0);
342 cairo_close_path(cr);
345 cairo_rotate (cr, 2. * M_PI / 3.);
347 cairo_move_to(cr, 0, 0);
348 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
349 cairo_line_to(cr, 0, 0);
350 cairo_close_path(cr);
353 cairo_rotate (cr, 2. * M_PI / 3.);
354 cairo_move_to(cr, 0, 0);
355 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
356 cairo_line_to(cr, 0, 0);
357 cairo_close_path(cr);
362 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
363 if (active_state() == Gtkmm2ext::ExplicitActive)
364 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
366 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
368 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
369 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
374 else if ((_elements & VectorIcon) && _icon == RecButton) {
375 const double x = get_width() * .5;
376 const double y = get_height() * .5;
377 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
378 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
379 if (active_state() == Gtkmm2ext::ExplicitActive)
380 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
382 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
383 cairo_fill_preserve(cr);
384 cairo_set_source_rgba (cr, .0, .0, .0, .8);
385 cairo_set_line_width(cr, 1);
388 else if ((_elements & VectorIcon) && _icon == CloseCross) {
389 const double x = get_width() * .5;
390 const double y = get_height() * .5;
391 const double o = .5 + std::min(x, y) * .4;
392 ArdourCanvas::set_source_rgba (cr, text_color);
393 cairo_set_line_width(cr, 1);
394 cairo_move_to(cr, x-o, y-o);
395 cairo_line_to(cr, x+o, y+o);
396 cairo_move_to(cr, x+o, y-o);
397 cairo_line_to(cr, x-o, y+o);
400 else if ((_elements & VectorIcon) && _icon == StripWidth) {
401 const double x0 = get_width() * .2;
402 const double x1 = get_width() * .8;
404 const double y0 = get_height() * .25;
405 const double y1= get_height() * .75;
407 const double ym= get_height() * .5;
410 const double xa0= get_height() * .39;
411 const double xa1= get_height() * .61;
412 const double ya0= get_height() * .35;
413 const double ya1= get_height() * .65;
415 ArdourCanvas::set_source_rgba (cr, text_color);
416 cairo_set_line_width(cr, 1);
419 cairo_move_to(cr, x0, y0);
420 cairo_line_to(cr, x0, y1);
421 cairo_move_to(cr, x1, y0);
422 cairo_line_to(cr, x1, y1);
425 cairo_move_to(cr, x0, ym);
426 cairo_line_to(cr, x1, ym);
429 cairo_move_to(cr, x0, ym);
430 cairo_line_to(cr, xa0, ya0);
431 cairo_move_to(cr, x0, ym);
432 cairo_line_to(cr, xa0, ya1);
435 cairo_move_to(cr, x1, ym);
436 cairo_line_to(cr, xa1, ya0);
437 cairo_move_to(cr, x1, ym);
438 cairo_line_to(cr, xa1, ya1);
441 else if ((_elements & VectorIcon) && _icon == DinMidi) {
442 const double x = get_width() * .5;
443 const double y = get_height() * .5;
444 const double r = std::min(x, y) * .75;
445 ArdourCanvas::set_source_rgba (cr, text_color);
446 cairo_set_line_width(cr, 1);
447 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
450 // pins equally spaced 45deg
451 cairo_arc (cr, x, y * 0.5, r * .15, 0, 2 * M_PI);
453 cairo_arc (cr, x * 0.5, y, r * .15, 0, 2 * M_PI);
455 cairo_arc (cr, x * 1.5, y, r * .15, 0, 2 * M_PI);
457 // .5 + .5 * .5 * sin(45deg), 1.5 - .5 * .5 * cos(45deg)
458 cairo_arc (cr, x * 0.677, y * .677, r * .15, 0, 2 * M_PI);
460 cairo_arc (cr, x * 1.323, y * .677, r * .15, 0, 2 * M_PI);
464 cairo_arc (cr, x, y+r, r * .28, 1.05 * M_PI, 1.95 * M_PI);
467 else if ((_elements & VectorIcon) && _icon == BtnStop) {
468 const int wh = std::min (get_width(), get_height());
470 (get_width() - wh) * .5 + wh * .25,
471 (get_height() - wh) * .5 + wh * .25,
474 VECTORICONSTROKEFILL;
476 else if ((_elements & VectorIcon) && _icon == BtnPlay) {
477 const int wh = std::min (get_width(), get_height()) * .5;
478 const double y = get_height() * .5;
479 const double x = get_width() - wh;
481 const float tri = ceil(.577 * wh); // 1/sqrt(3)
483 cairo_move_to (cr, x + wh * .5, y);
484 cairo_line_to (cr, x - wh * .5, y - tri);
485 cairo_line_to (cr, x - wh * .5, y + tri);
486 cairo_close_path (cr);
488 VECTORICONSTROKEFILL;
490 else if ((_elements & VectorIcon) && _icon == BtnPanic) {
491 const int wh = std::min (get_width(), get_height()) * .1;
492 const double xc = get_width() * .5;
493 const double yh = get_height();
497 VECTORICONSTROKEFILL;
499 cairo_arc (cr, xc, yh *.75, wh, 0, 2 * M_PI);
500 VECTORICONSTROKEFILL;
502 else if ((_elements & VectorIcon) && (_icon == BtnStart || _icon == BtnEnd || _icon == BtnRange)) {
503 // small play triangle
504 int wh = std::min (get_width(), get_height());
505 const double y = get_height() * .5;
506 const double x = get_width() - wh * .5;
508 const float tri = ceil(.577 * wh * 2); // 1/sqrt(3)
510 const float ln = std::min (get_width(), get_height()) * .07;
512 if (_icon == BtnStart || _icon == BtnRange) {
514 x - wh - ln, y - tri * 1.7,
517 VECTORICONSTROKEFILL;
520 if (_icon == BtnEnd || _icon == BtnRange) {
522 x + wh - ln, y - tri * 1.7,
525 VECTORICONSTROKEFILL;
528 cairo_move_to (cr, x + wh, y);
529 cairo_line_to (cr, x - wh, y - tri);
530 cairo_line_to (cr, x - wh, y + tri);
531 cairo_close_path (cr);
533 VECTORICONSTROKEFILL;
535 else if (_elements & VectorIcon) {
540 const int text_margin = char_pixel_width();
542 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
544 #if 0 // DEBUG style (print on hover)
545 if (_hovering || (_elements & Inactive)) {
546 bool layout_font = true;
547 Pango::FontDescription fd = _layout->get_font_description();
548 if (fd.gobj() == NULL) {
550 fd = get_pango_context()->get_font_description();
552 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
554 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
555 ceil(char_pixel_height() * BASELINESTRETCH),
556 _text_width, _text_height,
557 get_width(), get_height(),
558 layout_font ? "L:" : "W:",
559 fd.to_string().c_str());
564 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
568 ArdourCanvas::set_source_rgba (cr, text_color);
569 const double text_ypos = (get_height() - _text_height) * .5;
571 if (_elements & Menu) {
572 // always left align (dropdown)
573 cairo_move_to (cr, text_margin, text_ypos);
574 pango_cairo_show_layout (cr, _layout->gobj());
575 } else if ( (_elements & Indicator) == Indicator) {
576 // left/right align depending on LED position
578 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
580 cairo_move_to (cr, text_margin, text_ypos);
582 pango_cairo_show_layout (cr, _layout->gobj());
584 /* centered text otherwise */
591 cairo_rotate(cr, _angle * M_PI / 180.0);
592 cairo_device_to_user(cr, &ww, &wh);
593 xa = (ww - _text_width) * _xalign;
594 ya = (wh - _text_height) * _yalign;
596 /* quick hack for left/bottom alignment at -90deg
597 * TODO this should be generalized incl rotation.
598 * currently only 'user' of this API is meter_strip.cc
600 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
602 cairo_move_to (cr, xa, ya);
603 pango_cairo_update_layout(cr, _layout->gobj());
604 pango_cairo_show_layout (cr, _layout->gobj());
611 if (_elements & Menu) {
612 const float trih = ceil(_diameter * .5);
613 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
615 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
616 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
617 cairo_rel_line_to(cr, -triw2, -trih);
618 cairo_rel_line_to(cr, 2. * triw2, 0);
619 cairo_close_path(cr);
621 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
624 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
625 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
626 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
627 cairo_close_path(cr);
628 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
629 cairo_set_line_width(cr, 1);
634 if (_elements & Indicator) {
637 /* move to the center of the indicator/led */
638 if (_elements & Text) {
639 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
641 cairo_translate (cr, led_xoff, get_height() * .5);
643 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
646 cairo_translate (cr, get_width() * .5, get_height() * .5);
650 if (!_flat_buttons) {
651 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
652 cairo_set_source (cr, led_inset_pattern);
657 cairo_set_source_rgb (cr, 0, 0, 0);
658 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
662 ArdourCanvas::set_source_rgba (cr, led_color);
663 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
669 // a transparent overlay to indicate insensitivity
670 if ((visual_state() & Gtkmm2ext::Insensitive)) {
671 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
672 uint32_t ins_color = ARDOUR_UI::config()->color ("gtk_background");
673 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
677 // if requested, show hovering
678 if (ARDOUR_UI::config()->get_widget_prelight()
679 && !((visual_state() & Gtkmm2ext::Insensitive))) {
681 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
682 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
687 //user is currently pressing the button. dark outline helps to indicate this
688 if (_grabbed && !(_elements & (Inactive|Menu))) {
689 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
690 cairo_set_line_width(cr, 2);
691 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
695 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
696 if (visual_state() & Gtkmm2ext::Selected) {
697 cairo_set_line_width(cr, 1);
698 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
699 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
703 //I guess this means we have keyboard focus. I don't think this works currently
705 //A: yes, it's keyboard focus and it does work when there's no editor window
706 // (the editor is always the first receiver for KeyDown).
707 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
709 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
710 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
712 cairo_set_dash (cr, &dashes, 1, 0);
713 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
714 cairo_set_line_width (cr, 1.0);
716 cairo_set_dash (cr, 0, 0, 0);
721 ArdourButton::set_corner_radius (float r)
724 CairoWidget::set_dirty ();
728 ArdourButton::on_realize()
730 CairoWidget::on_realize ();
732 if (_layout && _layout->get_text() != _text) {
733 _layout->set_text (_text);
739 ArdourButton::on_size_request (Gtk::Requisition* req)
741 req->width = req->height = 0;
742 CairoWidget::on_size_request (req);
744 if (_diameter == 0) {
745 const float newdia = rint (ARDOUR_UI::config()->get_font_scale () / 9600.0); // 11px with 100% font-scaling
746 if (_diameter != newdia) {
752 if ((_elements & Text) && !_text.empty()) {
753 // if _layout does not exist, char_pixel_height() creates it,
754 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
755 _layout->get_pixel_size (_text_width, _text_height);
756 req->width += rint(1.75 * char_pixel_width()); // padding
757 req->width += _text_width;
764 req->width += _pixbuf->get_width() + char_pixel_width();
765 req->height = std::max(req->height, _pixbuf->get_height() + 4);
768 if (_elements & Indicator) {
769 req->width += lrint (_diameter) + char_pixel_width();
770 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
773 if ((_elements & Menu)) {
774 req->width += _diameter + 4;
777 if (_elements & VectorIcon) {
778 assert(!(_elements & Text));
779 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
781 req->height = std::max(req->height, wh);
784 /* Tweaks to mess the nice stuff above up again. */
785 if (_tweaks & TrackHeader) {
786 // forget everything above and just use a fixed square [em] size
787 // "TrackHeader Buttons" are single letter (usually uppercase)
788 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
789 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
793 else if (_tweaks & Square) {
794 // currerntly unused (again)
795 if (req->width < req->height)
796 req->width = req->height;
797 if (req->height < req->width)
798 req->height = req->width;
799 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
800 // properly centered text for those elements that are centered
801 // (no sub-pixel offset)
802 if ((req->width - _text_width) & 1) { ++req->width; }
803 if ((req->height - _text_height) & 1) { ++req->height; }
806 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
811 * This sets the colors used for rendering based on the name of the button, and
812 * thus uses information from the GUI config data.
815 ArdourButton::set_colors ()
817 _update_colors = false;
818 if (_fixed_colors_set) {
821 std::string name = get_name();
824 fill_active_color = ARDOUR_UI::config()->color (string_compose ("%1: fill active", name), &failed);
826 fill_active_color = ARDOUR_UI::config()->color ("generic button: fill active");
828 fill_inactive_color = ARDOUR_UI::config()->color (string_compose ("%1: fill", name), &failed);
830 fill_inactive_color = ARDOUR_UI::config()->color ("generic button: fill");
833 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
834 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
836 led_active_color = ARDOUR_UI::config()->color (string_compose ("%1: led active", name), &failed);
838 led_active_color = ARDOUR_UI::config()->color ("generic button: led active");
841 /* The inactive color for the LED is just a fairly dark version of the
845 ArdourCanvas::HSV inactive (led_active_color);
848 led_inactive_color = inactive.color ();
852 * This sets the colors used for rendering based on two fixed values, rather
853 * than basing them on the button name, and thus information in the GUI config
856 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
858 _fixed_colors_set = true;
860 fill_active_color = color_active;
861 fill_inactive_color = color_inactive;
863 unsigned char r, g, b, a;
864 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
866 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
867 (max (double(g), 255.) - min (double(g), 255.)) +
868 (max (double(b), 255.) - min (double(b), 255.));
870 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
871 (max (double(g), 0.) - min (double(g), 0.)) +
872 (max (double(b), 0.) - min (double(b), 0.));
874 text_active_color = (white_contrast > black_contrast) ?
875 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
876 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
879 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
881 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
882 (max (double(g), 255.) - min (double(g), 255.)) +
883 (max (double(b), 255.) - min (double(b), 255.));
885 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
886 (max (double(g), 0.) - min (double(g), 0.)) +
887 (max (double(b), 0.) - min (double(b), 0.));
889 text_inactive_color = (white_contrast > black_contrast) ?
890 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
891 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
893 /* XXX what about led colors ? */
894 CairoWidget::set_dirty ();
898 ArdourButton::build_patterns ()
900 if (convex_pattern) {
901 cairo_pattern_destroy (convex_pattern);
905 if (concave_pattern) {
906 cairo_pattern_destroy (concave_pattern);
910 if (led_inset_pattern) {
911 cairo_pattern_destroy (led_inset_pattern);
912 led_inset_pattern = 0;
916 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
917 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
918 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
921 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
922 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
923 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
925 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
926 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
927 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
929 _pattern_height = get_height() ;
933 ArdourButton::set_led_left (bool yn)
939 ArdourButton::on_button_press_event (GdkEventButton *ev)
943 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
944 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
945 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
950 if (binding_proxy.button_press_handler (ev)) {
955 CairoWidget::set_dirty ();
957 if (ev->button == 1 && !_act_on_release) {
959 _action->activate ();
964 if (_fallthrough_to_parent)
971 ArdourButton::on_button_release_event (GdkEventButton *ev)
973 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
974 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
975 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
976 signal_led_clicked(); /* EMIT SIGNAL */
982 CairoWidget::set_dirty ();
984 if (ev->button == 1 && _hovering) {
986 if (_act_on_release) {
988 _action->activate ();
994 if (_fallthrough_to_parent)
1001 ArdourButton::set_distinct_led_click (bool yn)
1003 _distinct_led_click = yn;
1008 ArdourButton::color_handler ()
1010 _update_colors = true;
1011 CairoWidget::set_dirty ();
1015 ArdourButton::on_size_allocate (Allocation& alloc)
1017 CairoWidget::on_size_allocate (alloc);
1022 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
1024 watch_connection.disconnect ();
1025 binding_proxy.set_controllable (c);
1029 ArdourButton::watch ()
1031 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
1034 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
1037 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
1041 ArdourButton::controllable_changed ()
1043 float val = binding_proxy.get_controllable()->get_value();
1045 if (fabs (val) >= 0.5f) {
1046 set_active_state (Gtkmm2ext::ExplicitActive);
1048 unset_active_state ();
1050 CairoWidget::set_dirty ();
1054 ArdourButton::set_related_action (RefPtr<Action> act)
1056 Gtkmm2ext::Activatable::set_related_action (act);
1060 action_tooltip_changed ();
1062 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1065 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
1068 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
1069 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
1070 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
1075 ArdourButton::action_toggled ()
1077 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1080 if (tact->get_active()) {
1081 set_active_state (Gtkmm2ext::ExplicitActive);
1083 unset_active_state ();
1089 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1091 _update_colors = true;
1092 CairoWidget::set_dirty ();
1096 ArdourButton::on_name_changed ()
1098 _char_pixel_width = 0;
1099 _char_pixel_height = 0;
1101 _update_colors = true;
1102 if (is_realized()) {
1108 ArdourButton::setup_led_rect ()
1110 if (!(_elements & Indicator)) {
1117 _led_rect = new cairo_rectangle_t;
1120 if (_elements & Text) {
1122 _led_rect->x = char_pixel_width();
1124 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1128 _led_rect->x = .5 * get_width() - _diameter;
1131 _led_rect->y = .5 * (get_height() - _diameter);
1132 _led_rect->width = _diameter;
1133 _led_rect->height = _diameter;
1137 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1140 if (is_realized()) {
1146 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1148 bool changed = (_active_state != s);
1149 CairoWidget::set_active_state (s);
1151 _update_colors = true;
1152 CairoWidget::set_dirty ();
1157 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1159 bool changed = (_visual_state != s);
1160 CairoWidget::set_visual_state (s);
1162 _update_colors = true;
1163 CairoWidget::set_dirty ();
1168 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1171 CairoWidget::set_dirty ();
1172 return CairoWidget::on_focus_in_event (ev);
1176 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1179 CairoWidget::set_dirty ();
1180 return CairoWidget::on_focus_out_event (ev);
1184 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1186 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1190 _action->activate ();
1194 return CairoWidget::on_key_release_event (ev);
1198 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1200 _hovering = (_elements & Inactive) ? false : true;
1202 if (ARDOUR_UI::config()->get_widget_prelight()) {
1203 CairoWidget::set_dirty ();
1206 return CairoWidget::on_enter_notify_event (ev);
1210 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1214 if (ARDOUR_UI::config()->get_widget_prelight()) {
1215 CairoWidget::set_dirty ();
1218 return CairoWidget::on_leave_notify_event (ev);
1222 ArdourButton::set_tweaks (Tweaks t)
1226 if (is_realized()) {
1233 ArdourButton::action_sensitivity_changed ()
1235 if (_action->property_sensitive ()) {
1236 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1238 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1243 ArdourButton::set_layout_ellipsize_width (int w)
1245 if (_layout_ellipsize_width == w) {
1248 _layout_ellipsize_width = w;
1252 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1253 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1255 if (is_realized ()) {
1261 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1263 if (_ellipsis == e) {
1270 _layout->set_ellipsize(_ellipsis);
1271 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1272 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1274 if (is_realized ()) {
1280 ArdourButton::ensure_layout ()
1284 _layout = Pango::Layout::create (get_pango_context());
1285 _layout->set_ellipsize(_ellipsis);
1286 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1287 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1293 ArdourButton::recalc_char_pixel_geometry ()
1295 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1299 // NB. this is not static, since the geometry is different
1300 // depending on the font used.
1302 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1303 _layout->set_text (x);
1304 _layout->get_pixel_size (w, h);
1305 _char_pixel_height = std::max(4, h);
1306 // number of actual chars in the string (not bytes)
1307 // Glib to the rescue.
1308 Glib::ustring gx(x);
1309 _char_avg_pixel_width = w / (float)gx.size();
1310 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1311 _layout->set_text (_text);
1315 ArdourButton::action_visibility_changed ()
1317 if (_action->property_visible ()) {
1325 ArdourButton::action_tooltip_changed ()
1327 string str = _action->property_tooltip().get_value();
1328 ARDOUR_UI::instance()->set_tip (*this, str);
1332 ArdourButton::set_elements (Element e)
1335 CairoWidget::set_dirty ();
1339 ArdourButton::add_elements (Element e)
1341 _elements = (ArdourButton::Element) (_elements | e);
1342 CairoWidget::set_dirty ();
1346 ArdourButton::set_icon (Icon i)
1349 CairoWidget::set_dirty ();
1353 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1355 if (led_custom_color == c && use_custom_led_color == useit) {
1359 led_custom_color = c;
1360 use_custom_led_color = useit;
1361 CairoWidget::set_dirty ();