3eff07e9158a20dea99610810e3a93b99c5c526c
[ardour.git] / libs / gtkmm2ext / fastmeter.cc
1 /*
2     Copyright (C) 2003 Paul Davis 
3
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <iostream>
22 #include <cmath>
23 #include <algorithm>
24 #include <gdkmm/rectangle.h>
25 #include <gtkmm2ext/fastmeter.h>
26 #include <gtkmm/style.h>
27
28 using namespace Gtk;
29 using namespace Gtkmm2ext;
30 using namespace std;
31
32 Glib::RefPtr<Gdk::Pixmap> *FastMeter::v_pixmap = 0;
33 Glib::RefPtr<Gdk::Bitmap> *FastMeter::v_mask = 0;
34 gint       FastMeter::v_pixheight = 0;
35 gint       FastMeter::v_pixwidth = 0;
36
37 Glib::RefPtr<Gdk::Pixmap> *FastMeter::h_pixmap = 0;
38 Glib::RefPtr<Gdk::Bitmap> *FastMeter::h_mask = 0;
39 gint       FastMeter::h_pixheight = 0;
40 gint       FastMeter::h_pixwidth = 0;
41
42 FastMeter::FastMeter (long hold, unsigned long dimen, Orientation o)
43 {
44         orientation = o;
45         hold_cnt = hold;
46         hold_state = 0;
47         current_peak = 0;
48         current_level = 0;
49         current_user_level = -100.0f;
50         
51         set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
52         
53         pixrect.set_x(0);
54         pixrect.set_y(0);
55
56         if (orientation == Vertical) {
57                 pixrect.set_width(min (v_pixwidth, (gint) dimen));
58                 pixrect.set_height(v_pixheight);
59         } else {
60                 pixrect.set_width(h_pixwidth);
61                 pixrect.set_height(min (h_pixheight, (gint) dimen));
62         }
63
64         request_width = pixrect.get_width();
65         request_height= pixrect.get_height();
66
67         backing = 0;
68 }
69
70 FastMeter::~FastMeter ()
71 {
72         if (backing) {
73                 (*backing)->unreference();
74         }
75 }
76
77 void
78 FastMeter::set_vertical_xpm (const char **xpm)
79 {
80         if (v_pixmap == 0) {
81                 gint w, h;
82                 
83                 *v_pixmap = Gdk::Pixmap::create_from_xpm(Gdk::Colormap::get_system(), *v_mask, xpm);
84
85                 (*v_pixmap)->get_size(w, h);
86                 
87                 v_pixheight = h;
88                 v_pixwidth = w;
89         }
90 }
91
92 void
93 FastMeter::set_horizontal_xpm (const char **xpm)
94 {
95         if (h_pixmap == 0) {
96                 gint w, h;
97                 
98                 *h_pixmap = Gdk::Pixmap::create_from_xpm(Gdk::Colormap::get_system(), *h_mask, xpm);
99
100                 (*h_pixmap)->get_size(w, h);
101                 
102                 h_pixheight = h;
103                 h_pixwidth = w;
104         }
105 }
106
107 void
108 FastMeter::set_hold_count (long val)
109 {
110         if (val < 1) {
111                 val = 1;
112         }
113         
114         hold_cnt = val;
115         hold_state = 0;
116         current_peak = 0;
117         
118         queue_draw ();
119 }
120
121 void
122 FastMeter::on_size_request (GtkRequisition* req)
123 {
124         req->width = request_width;
125         req->height = request_height;
126 }
127
128 bool
129 FastMeter::on_expose_event (GdkEventExpose* ev)
130 {
131         if (orientation == Vertical) {
132                 return vertical_expose (ev);
133         } else {
134                 return horizontal_expose (ev);
135         }
136 }
137
138 bool
139 FastMeter::vertical_expose (GdkEventExpose* ev)
140 {
141         Gdk::Rectangle intersect;
142         gint top_of_meter;
143         bool blit = false;
144         bool intersecting = false;
145
146         top_of_meter = (gint) floor (v_pixheight * current_level);
147         pixrect.set_height(v_pixheight - top_of_meter); /* bottom of background */
148
149         if (backing == 0) {
150                 *backing = Gdk::Pixmap::create (get_window(), get_width(), get_height());
151         }
152
153         intersect = pixrect.intersect(Glib::wrap(&ev->area), intersecting);
154         if (intersecting) {     
155
156                 /* paint the background (black). */
157                 (*backing)->draw_rectangle(get_style()->get_black_gc(), true, intersect.get_x(), intersect.get_y(), intersect.get_width(), intersect.get_height()); 
158                 blit = true;
159         }
160         
161         pixrect.set_height(top_of_meter);
162
163         intersect = pixrect.intersect(Glib::wrap(&ev->area), intersecting);
164         if (intersecting) {
165                 /* draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
166                  */
167
168                 (*backing)->draw_drawable(get_style()->get_fg_gc(get_state()), *v_pixmap, 
169                                         intersect.get_x(), v_pixheight - top_of_meter,
170                                         intersect.get_x(), v_pixheight - top_of_meter,
171                                         intersect.get_width(), intersect.get_height());
172                 
173                 blit = true;
174         }
175
176         /* draw peak bar */
177                 
178         if (hold_state) {
179                 (*backing)->draw_drawable(get_style()->get_fg_gc(get_state()), *v_pixmap,
180                                 intersect.get_x(), v_pixheight - (gint) floor (v_pixheight * current_peak),
181                                 intersect.get_x(), v_pixheight - (gint) floor (v_pixheight * current_peak),
182                                 intersect.get_width(), 3);
183         }
184
185         /* bilt to on-screen drawable */
186
187         if (blit) {
188                 Glib::RefPtr<Gdk::Window> win (get_window());
189                 win->draw_drawable(get_style()->get_fg_gc(get_state()), *backing,
190                                 ev->area.x, ev->area.y,
191                                 ev->area.x, ev->area.y,
192                                 ev->area.width, ev->area.height);
193         }
194
195         return true;
196 }
197
198 bool
199 FastMeter::horizontal_expose (GdkEventExpose* ev)
200 {
201         Gdk::Rectangle intersect;
202         bool intersecting = false;
203         gint right_of_meter;
204         bool blit = false;
205
206         right_of_meter = (gint) floor (h_pixwidth * current_level);
207
208         pixrect.set_x(right_of_meter);
209         pixrect.set_y(0);
210         pixrect.set_width(h_pixwidth - right_of_meter); 
211         
212         if (backing == 0) {
213                 *backing = Gdk::Pixmap::create(get_window(), get_width(), get_height());
214         }
215
216         intersect = pixrect.intersect(Glib::wrap(&ev->area), intersecting);
217         if (intersecting) {
218                 /* paint the background (black). */
219                 
220                 (*backing)->draw_rectangle(get_style()->get_black_gc(), true,
221                                         intersect.get_x(), intersect.get_y(),
222                                         intersect.get_width(), intersect.get_height());
223                 blit = true;
224         }
225         
226         pixrect.set_x(0);
227         pixrect.set_width(right_of_meter);
228
229         intersect = pixrect.intersect(Glib::wrap(&ev->area), intersecting);
230         if (intersecting) {
231                 /* draw the part of the meter image that we need. 
232                  */
233
234                 (*backing)->draw_drawable(get_style()->get_fg_gc(get_state()), *h_pixmap,
235                                         intersect.get_x(), intersect.get_y(),
236                                         intersect.get_x(), intersect.get_y(),
237                                         intersect.get_width(), intersect.get_height());
238                 
239                 blit = true;
240         }
241
242         /* draw peak bar */
243                 
244         if (hold_state) {
245                 (*backing)->draw_drawable(get_style()->get_fg_gc(get_state()), *h_pixmap,
246                                         right_of_meter, intersect.get_y(),
247                                         right_of_meter, intersect.get_y(),
248                                         3, intersect.get_height());
249         }
250
251         /* bilt to on-screen drawable */
252
253         if (blit) {
254                 Glib::RefPtr<Gdk::Window> win(get_window());
255                 win->draw_drawable(get_style()->get_fg_gc(get_state()), *backing,
256                                 ev->area.x, ev->area.y,
257                                 ev->area.x, ev->area.y,
258                                 ev->area.width, ev->area.height);
259         }
260
261         return true;
262 }
263
264 void
265 FastMeter::set (float lvl, float usrlvl)
266 {
267         current_level = lvl;
268         current_user_level = usrlvl;
269         
270         if (lvl > current_peak) {
271                 current_peak = lvl;
272                 hold_state = hold_cnt;
273         }
274         
275         if (hold_state > 0) {
276                 if (--hold_state == 0) {
277                         current_peak = lvl;
278                 }
279         }
280
281         queue_draw ();
282 }
283
284 void
285 FastMeter::clear ()
286 {
287         current_level = 0;
288         current_peak = 0;
289         hold_state = 0;
290         queue_draw ();
291 }