un-triple-buffer fastmeter (not finished), fix mixer strip name button, comment edito...
[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;
33 Glib::RefPtr<Gdk::Bitmap> FastMeter::v_mask;
34 gint       FastMeter::v_pixheight = 0;
35 gint       FastMeter::v_pixwidth = 0;
36
37 Glib::RefPtr<Gdk::Pixmap> FastMeter::h_pixmap;
38 Glib::RefPtr<Gdk::Bitmap> FastMeter::h_mask;
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
68 FastMeter::~FastMeter ()
69 {
70 }
71
72 void
73 FastMeter::set_vertical_xpm (const char **xpm)
74 {
75         if (v_pixmap == 0) {
76                 gint w, h;
77
78                 v_pixmap = Gdk::Pixmap::create_from_xpm(Gdk::Colormap::get_system(), v_mask, xpm);
79                 v_pixmap->get_size(w, h);
80                 
81                 v_pixheight = h;
82                 v_pixwidth = w;
83         }
84 }
85
86 void
87 FastMeter::set_horizontal_xpm (const char **xpm)
88 {
89         if (h_pixmap == 0) {
90                 gint w, h;
91                 
92                 h_pixmap = Gdk::Pixmap::create_from_xpm(Gdk::Colormap::get_system(), h_mask, xpm);
93                 h_pixmap->get_size(w, h);
94                 
95                 h_pixheight = h;
96                 h_pixwidth = w;
97         }
98 }
99
100 void
101 FastMeter::set_hold_count (long val)
102 {
103         if (val < 1) {
104                 val = 1;
105         }
106         
107         hold_cnt = val;
108         hold_state = 0;
109         current_peak = 0;
110         
111         queue_draw ();
112 }
113
114 void
115 FastMeter::on_size_request (GtkRequisition* req)
116 {
117         req->width = request_width;
118         req->height = request_height;
119 }
120
121 bool
122 FastMeter::on_expose_event (GdkEventExpose* ev)
123 {
124         if (orientation == Vertical) {
125                 return vertical_expose (ev);
126         } else {
127                 return horizontal_expose (ev);
128         }
129 }
130
131 bool
132 FastMeter::vertical_expose (GdkEventExpose* ev)
133 {
134         Gdk::Rectangle intersect;
135         gint top_of_meter;
136         bool blit = false;
137         bool intersecting = false;
138
139         top_of_meter = (gint) floor (v_pixheight * current_level);
140         pixrect.set_height(top_of_meter);
141
142         intersect = pixrect.intersect(Glib::wrap(&ev->area), intersecting);
143         if (intersecting) {
144                 /* draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
145                  */
146
147                 Glib::RefPtr<Gdk::Window> win(get_window());
148                 win->draw_drawable(get_style()->get_fg_gc(get_state()), v_pixmap, 
149                                    intersect.get_x(), v_pixheight - top_of_meter,
150                                    intersect.get_x(), v_pixheight - top_of_meter,
151                                    intersect.get_width(), intersect.get_height());
152                 
153                 blit = true;
154         }
155
156         /* draw peak bar */
157                 
158         if (hold_state) {
159                 Glib::RefPtr<Gdk::Window> win(get_window());
160                 win->draw_drawable(get_style()->get_fg_gc(get_state()), v_pixmap,
161                                    intersect.get_x(), v_pixheight - (gint) floor (v_pixheight * current_peak),
162                                    intersect.get_x(), v_pixheight - (gint) floor (v_pixheight * current_peak),
163                                    intersect.get_width(), 3);
164         }
165
166         return true;
167 }
168
169 bool
170 FastMeter::horizontal_expose (GdkEventExpose* ev)
171 {
172         Gdk::Rectangle intersect;
173         bool intersecting = false;
174         gint right_of_meter;
175
176         right_of_meter = (gint) floor (h_pixwidth * current_level);
177         pixrect.set_width(right_of_meter);
178
179         intersect = pixrect.intersect(Glib::wrap(&ev->area), intersecting);
180         if (intersecting) {
181                 /* draw the part of the meter image that we need. 
182                  */
183
184                 Glib::RefPtr<Gdk::Window> win(get_window());
185                 win->draw_drawable(get_style()->get_fg_gc(get_state()), h_pixmap,
186                                    intersect.get_x(), intersect.get_y(),
187                                    intersect.get_x(), intersect.get_y(),
188                                    intersect.get_width(), intersect.get_height());
189         }
190
191         /* draw peak bar */
192                 
193         if (hold_state) {
194                 Glib::RefPtr<Gdk::Window> win(get_window());
195                 win->draw_drawable(get_style()->get_fg_gc(get_state()), h_pixmap,
196                               right_of_meter, intersect.get_y(),
197                               right_of_meter, intersect.get_y(),
198                               3, intersect.get_height());
199         }
200
201         return true;
202 }
203
204 void
205 FastMeter::set (float lvl, float usrlvl)
206 {
207         current_level = lvl;
208         current_user_level = usrlvl;
209         
210         if (lvl > current_peak) {
211                 current_peak = lvl;
212                 hold_state = hold_cnt;
213         }
214         
215         if (hold_state > 0) {
216                 if (--hold_state == 0) {
217                         current_peak = lvl;
218                 }
219         }
220
221         queue_draw ();
222 }
223
224 void
225 FastMeter::clear ()
226 {
227         current_level = 0;
228         current_peak = 0;
229         hold_state = 0;
230         queue_draw ();
231 }