f4877c5f7aa1f9595f4131e56784a0bc9a704489
[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, 100  /*std::max (20, w + PADDING) */);
47         req->height = std::max (req->height, std::max (12, h + 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::blink (bool onoff)
66 {
67         _blink = onoff;
68         queue_draw ();
69 }
70
71 void
72 ArdourGauge::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
73 {
74         cairo_t* cr = ctx->cobj ();
75         Gtkmm2ext::Color bg = UIConfiguration::instance().color ("gtk_background");
76         Gtkmm2ext::Color base = UIConfiguration::instance ().color ("ruler base");
77         Gtkmm2ext::Color text = UIConfiguration::instance ().color ("ruler text");
78
79         const int width = get_width ();
80         const int height = get_height ();
81
82         cairo_rectangle (cr, 0, 0, width, height);
83         cairo_set_source_rgba (cr, 0,0,0,1 );
84         cairo_fill (cr);
85
86         cairo_rectangle (cr, 1, 1, width-2, height-2);
87         Gtkmm2ext::set_source_rgba (cr, bg);
88         cairo_fill (cr);
89
90         if (alert () && _blink) {
91                 Gtkmm2ext::rounded_rectangle (cr, 1, 1, width - 2, height - 2, 1);
92                 cairo_set_source_rgba (cr, 0.5, 0, 0, 1.0);
93                 cairo_fill (cr);
94         }
95
96         cairo_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING);
97         cairo_clip (cr);
98
99         const float lvl = level ();
100
101         int bw = (width - PADDING - PADDING) * lvl;
102         cairo_rectangle (cr, PADDING, PADDING, bw, height-PADDING);
103
104         switch (indicator ()) {
105                 case Level_OK:
106                         cairo_set_source_rgba (cr, 0, .5, 0, 1.0);
107                         break;
108                 case Level_WARN:
109                         cairo_set_source_rgba (cr, .7, .6, 0, 1.0);
110                         break;
111                 case Level_CRIT:
112                         cairo_set_source_rgba (cr, .9, 0, 0, 1.0);
113                         break;
114         }
115         cairo_fill (cr);
116
117         int w, h;
118         _layout->get_pixel_size (w, h);
119
120         cairo_save (cr);
121         cairo_new_path (cr);
122         cairo_translate (cr, width * .5, height * .5);
123
124         cairo_move_to (cr, w * -.5, h * -.5);
125         pango_cairo_update_layout (cr, _layout->gobj());
126         Gtkmm2ext::set_source_rgb_a (cr, base, 0.5);
127         pango_cairo_layout_path (cr, _layout->gobj());
128         cairo_set_line_width (cr, 1.5);
129         cairo_stroke (cr);
130
131         cairo_move_to (cr, w * -.5, h * -.5);
132         pango_cairo_update_layout (cr, _layout->gobj());
133         Gtkmm2ext::set_source_rgba (cr, text);
134         pango_cairo_show_layout (cr, _layout->gobj());
135
136         cairo_restore (cr);
137 }