summaryrefslogtreecommitdiff
path: root/gtk2_ardour/ardour_gauge.cc
blob: d93d2d15d66037be79162a40377940788e99b080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "gtkmm2ext/utils.h"
#include "widgets/tooltips.h"

#include "ardour_gauge.h"
#include "ui_config.h"

#define PADDING 3

ArdourGauge::ArdourGauge (std::string const& max_text)
{
	_layout = Pango::Layout::create (get_pango_context ());
	_layout->set_text (max_text);
}

ArdourGauge::~ArdourGauge ()
{
}

void
ArdourGauge::on_size_request (Gtk::Requisition* req)
{
	req->width = req->height = 0;
	CairoWidget::on_size_request (req);

	int w, h;
	_layout->get_pixel_size (w, h);

	req->width = std::max (req->width, 100  /*std::max (20, w + PADDING) */);
	req->height = std::max (req->height, std::max (12, h + PADDING));
}

void
ArdourGauge::update ()
{
	queue_draw ();
	ArdourWidgets::set_tooltip (*this, tooltip_text ());
}

void
ArdourGauge::update (std::string const& txt)
{
	_layout->set_text (txt);
	update ();
}

void
ArdourGauge::blink (bool onoff)
{
	_blink = onoff;
	queue_draw ();
}

void
ArdourGauge::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
{
	cairo_t* cr = ctx->cobj ();
	Gtkmm2ext::Color bg = UIConfiguration::instance().color ("gtk_background");
	Gtkmm2ext::Color base = UIConfiguration::instance ().color ("ruler base");
	Gtkmm2ext::Color text = UIConfiguration::instance ().color ("ruler text");

	const int width = get_width ();
	const int height = get_height ();

	cairo_rectangle (cr, 0, 0, width, height);
	cairo_set_source_rgba (cr, 0,0,0,1 );
	cairo_fill (cr);

	cairo_rectangle (cr, 1, 1, width-2, height-2);
	Gtkmm2ext::set_source_rgba (cr, bg);
	cairo_fill (cr);

	if (alert () && _blink) {
		Gtkmm2ext::rounded_rectangle (cr, 1, 1, width - 2, height - 2, 1);
		cairo_set_source_rgba (cr, 0.5, 0, 0, 1.0);
		cairo_fill (cr);
	}

	cairo_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING);
	cairo_clip (cr);

	const float lvl = level ();

	int bw = (width - PADDING - PADDING) * lvl;
	cairo_rectangle (cr, PADDING, PADDING, bw, height-PADDING);

	switch (indicator ()) {
		case Level_OK:
			break;
		case Level_WARN:
			cairo_set_source_rgba (cr, .7, .6, 0, 1.0);
			cairo_fill (cr);
			break;
		case Level_CRIT:
			cairo_set_source_rgba (cr, .9, 0, 0, 1.0);
			cairo_fill (cr);
			break;
	}

	int w, h;
	_layout->get_pixel_size (w, h);

	cairo_save (cr);

	cairo_translate (cr, 2+PADDING, height * .5);
	cairo_move_to (cr, 0, h * -.5);  //vertically center the text
	pango_cairo_update_layout (cr, _layout->gobj());
	Gtkmm2ext::set_source_rgba (cr, text);
	pango_cairo_show_layout (cr, _layout->gobj());

	cairo_restore (cr);
}