Debian Codegraph @

source/filtwin.cc

raw
1 // ----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2003-2013 Fons Adriaensen <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // ----------------------------------------------------------------------------
19
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include "styles.h"
25 #include "filtwin.h"
26 #include "global.h"
27
28
29 Filtwin::Filtwin (X_window *parent, X_callback *callb, int xp, int yp, int xs, int ys) :
30 X_window (parent, xp, yp, xs, ys, Colors.plot_bg),
31 _callb (callb),
32 _xs (xs),
33 _ys (ys),
34 _show (false),
35 _dots (0)
36 {
37 int i;
38
39 x_add_events (ExposureMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | StructureNotifyMask);
40
41 _xd = _xs - LSW - RSW;
42 _yd = _ys - TPM - BSH - BWH;
43
44 _plotw = new X_subwin (this, LSW, TPM, _xd, _yd, Colors.plot_bg);
45 _plotw->x_add_events (ExposureMask);
46 _plotw->x_map ();
47
48 _lswin = new X_subwin (this, 0, 0, LSW, _yd + TPM + 8, Colors.plot_bg);
49 _lswin->x_add_events (ExposureMask);
50 _lswin->x_map ();
51
52 _rswin = new X_subwin (this, _xs - RSW, 0, RSW, _yd + TPM + 8, Colors.plot_bg);
53 _rswin->x_set_win_gravity (NorthEastGravity);
54 _rswin->x_add_events (ExposureMask);
55 _rswin->x_map ();
56
57 _bswin = new X_subwin (this, LSW - 8, TPM + _yd, _xd + 16, BSH, Colors.plot_bg);
58 _bswin->x_set_win_gravity (SouthWestGravity);
59 _bswin->x_add_events (ExposureMask);
60 _bswin->x_map ();
61
62 _bbwin = new X_window (this, 0, _ys - BWH, _xs, BWH, XftColors.main_bg->pixel);
63 _bbwin->x_set_win_gravity (SouthWestGravity);
64 _bbwin->x_map ();
65
66 set_xscale ();
67 set_yscale ();
68 _dots1._col = Colors.plot_mk [0] ^ Colors.plot_bg;
69 _dots2._col = Colors.plot_mk [1] ^ Colors.plot_bg;
70 for (i = 0; i < 33; i++)
71 {
72 _dots1._v [i] = 0.5f;
73 _dots2._v [i] = 0.0f;
74 }
75 _dots2._v [ 0] = 1.0f;
76 _dots2._v [32] = 1.0f;
77 }
78
79
80 Filtwin::~Filtwin (void)
81 {
82 }
83
84
85 void Filtwin::handle_event (XEvent *E)
86 {
87 switch (E->type)
88 {
89 case MapNotify:
90 _show = true;
91 break;
92 case UnmapNotify:
93 _show = false;
94 break;
95 case GraphicsExpose:
96 case Expose:
97 expose ((XExposeEvent *) E);
98 break;
99 case ButtonPress:
100 bpress ((XButtonEvent *) E);
101 break;
102 case ButtonRelease:
103 brelse ((XButtonEvent *) E);
104 break;
105 case MotionNotify:
106 motion ((XPointerMovedEvent *) E);
107 break;
108 }
109 }
110
111
112 void Filtwin::handle_callb (int k, X_window *W, XEvent *E )
113 {
114 /*
115 int b, c, e;
116
117 c = X_callback::cb_class (k);
118 e = X_callback::cb_event (k);
119 switch (c)
120 {
121 case X_callback::BUTTON:
122 {
123 X_button *B = (X_button *) W;
124 b = ((XButtonEvent *) E)->button;
125 k = B->cbid ();
126 if (e == X_button::PRESS)
127 {
128 switch (k)
129 {
130 default:
131 ;
132 }
133 }
134 break;
135 }
136
137 default:
138 ;
139 }
140 */
141 }
142
143
144 void Filtwin::expose (XExposeEvent *E)
145 {
146 if (E->window == win ())
147 {
148 }
149 else if (E->window == _plotw->win ())
150 {
151 if (E->count == 0) redraw ();
152 }
153 else if (E->window == _lswin->win ())
154 {
155 if (E->count == 0) plot_lscale ();
156 }
157 else if (E->window == _rswin->win ())
158 {
159 if (E->count == 0) plot_rscale ();
160 }
161 else if (E->window == _bswin->win ())
162 {
163 if (E->count == 0) plot_xscale ();
164 }
165 }
166
167
168 void Filtwin::resize (int xs, int ys)
169 {
170 if ((_xs != xs) || (_ys != ys))
171 {
172 _xs = xs;
173 _ys = ys;
174 _xd = _xs - LSW - RSW;
175 _yd = _ys - TPM - BSH - BWH;
176 x_resize (_xs, _ys);
177 set_xscale ();
178 set_yscale ();
179 _plotw->x_resize (_xd, _yd);
180 _lswin->x_resize (LSW, _yd + TPM + 8);
181 _rswin->x_resize (RSW, _yd + TPM + 8);
182 _bswin->x_resize (_xd + 16, BSH);
183 _bbwin->x_resize (_xs, BWH);
184 }
185 }
186
187
188 void Filtwin::bpress (XButtonEvent *E)
189 {
190 int x, y;
191
192 if (_dots) return;
193 x = E->x - LSW;
194 y = E->y - TPM;
195 if ((y >= _dots1._y1 - 3) && (y < _dots1._y0 + 3)) _dots = &_dots1;
196 else if ((y >= _dots2._y1 - 3) && (y < _dots2._y0 + 3)) _dots = &_dots2;
197 if (_dots)
198 {
199 finddot (x);
200 if (_idot >= 0) movedot (y);
201 }
202 }
203
204
205 void Filtwin::motion (XPointerMovedEvent *E)
206 {
207 int x, y;
208
209 if (_dots)
210 {
211 x = E->x - LSW;
212 y = E->y - TPM;
213 if (E->state & Button3Mask) finddot (x);
214 if (_idot >= 0) movedot (y);
215 }
216 }
217
218
219 void Filtwin::brelse (XButtonEvent *E)
220 {
221 _dots = 0;
222 }
223
224
225 void Filtwin::set_xscale (void)
226 {
227 int i;
228
229 _xunit = (_xd - 1) / 3.3f;
230 for (i = 12; i <= 44; i++)
231 {
232 _xticks [i - 12] = (int)(_xunit * (0.1f * i - 1.15f) + 0.5f);
233 }
234 }
235
236
237 void Filtwin::set_yscale (void)
238 {
239 int i, s;
240
241 s = _yd - 1;
242 for (i = 0; i <= 8; i++) _yticks [i] = s * i / 8;
243 _dots1._y0 = _yticks [6];
244 _dots1._y1 = _yticks [2];
245 _dots2._y0 = _yticks [8];
246 _dots2._y1 = _yticks [7];
247 }
248
249
250 void Filtwin::plot_xscale (void)
251 {
252 int i, d;
253 X_draw D (dpy (), _bswin->win (), dgc (), xft ());
254
255 if (!_show) return;
256 D.setfont (XftFonts.scales);
257 D.clearwin ();
258 D.setfunc (GXcopy);
259 D.setline (0);
260 D.setcolor (XftColors.plot_sc);
261 d = XftFonts.scales->ascent + 1;
262 for (i = 0; i < 33; i++)
263 {
264 D.move (_xticks [i] + 8, 0);
265 if (_xtexts [i])
266 {
267 D.rdraw (0, 5);
268 D.rmove (0, d);
269 D.drawstring (_xtexts [i], 0);
270 }
271 else D.rdraw (0, 3);
272 }
273 }
274
275
276 void Filtwin::plot_lscale (void)
277 {
278 int d, i;
279 X_draw D (dpy (), _lswin->win (), dgc (), xft ());
280
281 if (!_show) return;
282 D.setfont (XftFonts.scales);
283 D.clearwin ();
284 D.setfunc (GXcopy);
285 D.setline (0);
286 D.setcolor (XftColors.plot_sc);
287 d = XftFonts.scales->ascent / 2 - 1;
288 for (i = 0; i <= 8; i++)
289 {
290 D.move (LSW - 1, _yticks [i] + TPM);
291 D.rdraw (-5, 0);
292 }
293 }
294
295
296 void Filtwin::plot_rscale (void)
297 {
298 int d, i;
299 X_draw D (dpy (), _rswin->win (), dgc (), xft ());
300
301 if (!_show) return;
302 D.setfont (XftFonts.scales);
303 D.clearwin ();
304 D.setfunc (GXcopy);
305 D.setline (0);
306 D.setcolor (XftColors.plot_sc);
307 d = XftFonts.scales->ascent / 2 - 1;
308 for (i = 0; i <= 8; i++)
309 {
310 D.move (0, _yticks [i] + TPM);
311 D.rdraw (5, 0);
312 }
313 }
314
315
316 void Filtwin::update (void)
317 {
318 if (_show)
319 {
320 redraw ();
321 }
322 }
323
324
325 void Filtwin::redraw ()
326 {
327 X_draw D (dpy (), _plotw->win (), dgc (), xft ());
328
329 D.setcolor (Colors.plot_bg);
330 D.setfunc (GXcopy);
331 D.clearwin ();
332 plot_grid (&D);
333 /*
334 if (_data)
335 {
336 plot_data (&D);
337 D.setfunc (GXxor);
338 }
339 */
340 plot_dots (&D, &_dots1);
341 plot_dots (&D, &_dots2);
342 }
343
344
345 void Filtwin::plot_grid (X_draw *D)
346 {
347 int i;
348
349 D->setline (0);
350 D->setcolor (Colors.plot_gr);
351 D->move (0, 0);
352 D->rdraw (0, _yd);
353 D->move (_xd - 1, 0);
354 D->rdraw (0, _yd);
355 for (i = 0; i <= 8; i++)
356 {
357 D->move (0, _yticks [i]);
358 D->rdraw (_xd, 0);
359 }
360 for (i = 0; i < 33; i++)
361 {
362 if ((i % 3) == 0)
363 {
364 D->move (_xticks [i], 0);
365 D->rdraw (0, _yd);
366 }
367 }
368 }
369
370
371 void Filtwin::plot_data (X_draw *D)
372 {
373 }
374
375
376 void Filtwin::plot_dots (X_draw *D, Dots *P)
377 {
378 int i, x, y;
379
380 D->setfunc (GXxor);
381 D->setcolor (P->_col);
382 for (i = 0; i < 33; i++)
383 {
384 x = _xticks [i] - 3;
385 y = P->v2y (P->_v [i]) - 3;
386 D->fillrect (x, y, x + 6, y + 6);
387 }
388 }
389
390
391 void Filtwin::finddot (int x)
392 {
393 _idot = (int)(10 * x / _xunit);
394 if (_idot < 0) _idot = 0;
395 if (_idot > 32) _idot = 32;
396 if (abs (_xticks [_idot] - x) > 5) _idot = -1;
397 }
398
399
400 void Filtwin::movedot (int y1)
401 {
402 int x0, y0;
403 X_draw D (dpy (), _plotw->win (), dgc (), 0);
404
405 D.setfunc (GXxor);
406 D.setcolor (_dots->_col);
407 x0 = _xticks [_idot] - 3;
408 y0 = _dots->v2y (_dots->_v [_idot]) - 3;
409 _dots->_v [_idot] = _dots->y2v (y1);
410 y1 = _dots->v2y (_dots->_v [_idot]) - 3;
411 if (y1 != y0)
412 {
413 D.fillrect (x0, y0, x0 + 6, y0 + 6);
414 D.fillrect (x0, y1, x0 + 6, y1 + 6);
415 }
416 }
417
418
419 const char *Filtwin::_xtexts [33] =
420 {
421 0, "20", 0, 0, 0, "50", 0, 0, "100", 0,
422 0, "200", 0, 0, 0, "500", 0, 0, "1k", 0,
423 0, "2k", 0, 0, 0, "5k", 0, 0, "10k", 0,
424 0, "20k", 0
425 };