add ability to specify row colors for region treeview
[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 Gdk;
30 using namespace Glib;
31 using namespace Gtkmm2ext;
32 using namespace std;
33
34 RefPtr<Pixmap> FastMeter::v_pixmap;
35 RefPtr<Bitmap> FastMeter::v_mask;
36 gint       FastMeter::v_pixheight = 0;
37 gint       FastMeter::v_pixwidth = 0;
38
39 RefPtr<Pixmap> FastMeter::h_pixmap;
40 RefPtr<Bitmap> FastMeter::h_mask;
41 gint       FastMeter::h_pixheight = 0;
42 gint       FastMeter::h_pixwidth = 0;
43
44 FastMeter::FastMeter (long hold, unsigned long dimen, Orientation o)
45 {
46         orientation = o;
47         hold_cnt = hold;
48         hold_state = 0;
49         current_peak = 0;
50         current_level = 0;
51         current_user_level = -100.0f;
52         
53         set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
54
55         pixrect.set_x(0);
56         pixrect.set_y(0);
57
58         if (orientation == Vertical) {
59                 pixrect.set_width(min (v_pixwidth, (gint) dimen));
60                 pixrect.set_height(v_pixheight);
61         } else {
62                 pixrect.set_width(h_pixwidth);
63                 pixrect.set_height(min (h_pixheight, (gint) dimen));
64         }
65
66         request_width = pixrect.get_width();
67         request_height= pixrect.get_height();
68 }
69
70 FastMeter::~FastMeter ()
71 {
72 }
73
74 void
75 FastMeter::on_realize ()
76 {
77         DrawingArea::on_realize();
78
79         RefPtr<Style> style = get_style();
80         Color black = style->get_black();
81
82         style->set_bg (STATE_NORMAL, black);
83         style->set_bg (STATE_ACTIVE, black);
84         style->set_bg (STATE_SELECTED, black);
85         style->set_bg (STATE_INSENSITIVE, black);
86 }
87
88 void
89 FastMeter::set_vertical_xpm (const char **xpm)
90 {
91         if (v_pixmap == 0) {
92                 gint w, h;
93
94                 v_pixmap = Pixmap::create_from_xpm(Colormap::get_system(), v_mask, xpm);
95                 v_pixmap->get_size(w, h);
96                 
97                 v_pixheight = h;
98                 v_pixwidth = w;
99         }
100 }
101
102 void
103 FastMeter::set_horizontal_xpm (const char **xpm)
104 {
105         if (h_pixmap == 0) {
106                 gint w, h;
107                 
108                 h_pixmap = Pixmap::create_from_xpm(Colormap::get_system(), h_mask, xpm);
109                 h_pixmap->get_size(w, h);
110                 
111                 h_pixheight = h;
112                 h_pixwidth = w;
113         }
114 }
115
116 void
117 FastMeter::set_hold_count (long val)
118 {
119         if (val < 1) {
120                 val = 1;
121         }
122         
123         hold_cnt = val;
124         hold_state = 0;
125         current_peak = 0;
126         
127         queue_draw ();
128 }
129
130 void
131 FastMeter::on_size_request (GtkRequisition* req)
132 {
133         req->width = request_width;
134         req->height = request_height;
135 }
136
137 bool
138 FastMeter::on_expose_event (GdkEventExpose* ev)
139 {
140         if (orientation == Vertical) {
141                 return vertical_expose (ev);
142         } else {
143                 return horizontal_expose (ev);
144         }
145 }
146
147 bool
148 FastMeter::vertical_expose (GdkEventExpose* ev)
149 {
150         Rectangle intersect;
151         gint top_of_meter;
152         bool blit = false;
153         bool intersecting = false;
154
155         top_of_meter = (gint) floor (v_pixheight * current_level);
156         pixrect.set_height(top_of_meter);
157
158         intersect = pixrect.intersect(wrap(&ev->area), intersecting);
159         if (intersecting) {
160                 /* draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
161                  */
162
163                 RefPtr<Gdk::Window> win(get_window());
164                 win->draw_drawable(get_style()->get_fg_gc(get_state()), v_pixmap, 
165                                    intersect.get_x(), v_pixheight - top_of_meter,
166                                    intersect.get_x(), v_pixheight - top_of_meter,
167                                    intersect.get_width(), intersect.get_height());
168                 
169                 blit = true;
170         }
171
172         /* draw peak bar */
173                 
174         if (hold_state) {
175                 RefPtr<Gdk::Window> win(get_window());
176                 win->draw_drawable(get_style()->get_fg_gc(get_state()), v_pixmap,
177                                    intersect.get_x(), v_pixheight - (gint) floor (v_pixheight * current_peak),
178                                    intersect.get_x(), v_pixheight - (gint) floor (v_pixheight * current_peak),
179                                    intersect.get_width(), 3);
180         }
181
182         return true;
183 }
184
185 bool
186 FastMeter::horizontal_expose (GdkEventExpose* ev)
187 {
188         Rectangle intersect;
189         bool intersecting = false;
190         gint right_of_meter;
191
192         right_of_meter = (gint) floor (h_pixwidth * current_level);
193         pixrect.set_width(right_of_meter);
194
195         intersect = pixrect.intersect(wrap(&ev->area), intersecting);
196         if (intersecting) {
197                 /* draw the part of the meter image that we need. 
198                  */
199
200                 RefPtr<Gdk::Window> win(get_window());
201                 win->draw_drawable(get_style()->get_fg_gc(get_state()), h_pixmap,
202                                    intersect.get_x(), intersect.get_y(),
203                                    intersect.get_x(), intersect.get_y(),
204                                    intersect.get_width(), intersect.get_height());
205         }
206
207         /* draw peak bar */
208                 
209         if (hold_state) {
210                 RefPtr<Gdk::Window> win(get_window());
211                 win->draw_drawable(get_style()->get_fg_gc(get_state()), h_pixmap,
212                               right_of_meter, intersect.get_y(),
213                               right_of_meter, intersect.get_y(),
214                               3, intersect.get_height());
215         }
216
217         return true;
218 }
219
220 void
221 FastMeter::set (float lvl, float usrlvl)
222 {
223         current_level = lvl;
224         current_user_level = usrlvl;
225         
226         if (lvl > current_peak) {
227                 current_peak = lvl;
228                 hold_state = hold_cnt;
229         }
230         
231         if (hold_state > 0) {
232                 if (--hold_state == 0) {
233                         current_peak = lvl;
234                 }
235         }
236
237         queue_draw ();
238 }
239
240 void
241 FastMeter::clear ()
242 {
243         current_level = 0;
244         current_peak = 0;
245         hold_state = 0;
246         queue_draw ();
247 }