Revert TRIANGLE_WIDTH for mac. Must be a pango/cairo issue on mac.
[ardour.git] / gtk2_ardour / ardour_gauge.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
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.
13  *
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include "gtkmm2ext/utils.h"
20 #include "widgets/tooltips.h"
21
22 #include "ardour_gauge.h"
23 #include "ui_config.h"
24
25 #define PADDING 3
26
27 ArdourGauge::ArdourGauge (std::string const& max_text)
28 {
29         _layout = Pango::Layout::create (get_pango_context ());
30         _layout->set_text (max_text);
31 }
32
33 ArdourGauge::~ArdourGauge ()
34 {
35 }
36
37 void
38 ArdourGauge::on_size_request (Gtk::Requisition* req)
39 {
40         req->width = req->height = 0;
41         CairoWidget::on_size_request (req);
42
43         int w, h;
44         _layout->get_pixel_size (w, h);
45
46         req->width = std::max (req->width, std::max (12, h + PADDING));
47         req->height = std::max (req->height, 20 /*std::max (20, w + PADDING) */);
48 }
49
50 void
51 ArdourGauge::update ()
52 {
53         queue_draw ();
54         ArdourWidgets::set_tooltip (*this, tooltip_text ());
55 }
56
57 void
58 ArdourGauge::update (std::string const& txt)
59 {
60         _layout->set_text (txt);
61         update ();
62 }
63
64 void
65 ArdourGauge::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
66 {
67         cairo_t* cr = ctx->cobj ();
68         Gtkmm2ext::Color base = UIConfiguration::instance ().color ("ruler base");
69         Gtkmm2ext::Color text = UIConfiguration::instance ().color ("ruler text");
70
71         const int width = get_width ();
72         const int height = get_height ();
73
74         Gtkmm2ext::rounded_rectangle (cr, 0, 0, width, height, PADDING + 1);
75         Gtkmm2ext::set_source_rgba (cr, base);
76         cairo_fill (cr);
77
78         if (alert ()) {
79                 Gtkmm2ext::rounded_rectangle (cr, 1, 1, width - 2, height - 2, PADDING + 1);
80                 cairo_set_source_rgba (cr, 0.5, 0, 0, 1.0);
81                 cairo_fill (cr);
82         }
83
84         Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING, PADDING + 1);
85         cairo_clip (cr);
86
87         const float lvl = level ();
88
89         int bh = (height - PADDING - PADDING) * lvl;
90         cairo_rectangle (cr, PADDING, height - PADDING - bh, width - PADDING, bh);
91
92         switch (indicator ()) {
93                 case Level_OK:
94                         cairo_set_source_rgba (cr, 0, .5, 0, 1.0);
95                         break;
96                 case Level_WARN:
97                         cairo_set_source_rgba (cr, .7, .6, 0, 1.0);
98                         break;
99                 case Level_CRIT:
100                         cairo_set_source_rgba (cr, .9, 0, 0, 1.0);
101                         break;
102         }
103         cairo_fill (cr);
104
105         int w, h;
106         _layout->get_pixel_size (w, h);
107
108         cairo_save (cr);
109         cairo_new_path (cr);
110         cairo_translate (cr, width * .5, height * .5);
111         cairo_rotate (cr, M_PI * -.5);
112
113         cairo_move_to (cr, w * -.5, h * -.5);
114         pango_cairo_update_layout (cr, _layout->gobj());
115         Gtkmm2ext::set_source_rgb_a (cr, base, 0.5);
116         pango_cairo_layout_path (cr, _layout->gobj());
117         cairo_set_line_width (cr, 1.5);
118         cairo_stroke (cr);
119
120         cairo_move_to (cr, w * -.5, h * -.5);
121         pango_cairo_update_layout (cr, _layout->gobj());
122         Gtkmm2ext::set_source_rgba (cr, text);
123         pango_cairo_show_layout (cr, _layout->gobj());
124
125         cairo_restore (cr);
126 }