~ubuntu-branches/ubuntu/vivid/aeolus/vivid

« back to all changes in this revision

Viewing changes to source/functionwin.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-04-19 19:12:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100419191251-hgarjfcdfl7c0ryl
Tags: 0.8.4-3
debian/patches/01-makefile.patch: Drop -march=native flag, it isn't valid
for Debian packages as the results are unpredictable, thanks to
Bastian Blank for reporting this (Closes: #578278).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
 
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
 
 
19
 
 
20
#include <stdlib.h>
 
21
#include <stdio.h>
 
22
#include "functionwin.h"
 
23
#include "callbacks.h"
 
24
#include "styles.h"
 
25
 
 
26
 
 
27
 
 
28
Functionwin::Functionwin (X_window *parent, X_callback *callb, int xp, int yp,
 
29
                          unsigned long bgnd, unsigned long grid, unsigned long mark) :
 
30
    X_window (parent, xp, yp, 100, 100, bgnd),
 
31
    _callb (callb), _bgnd (bgnd), _grid (grid), _mark (mark)
 
32
{
 
33
    x_add_events (ExposureMask | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask);
 
34
    x_set_bit_gravity (NorthWestGravity);
 
35
    for (int i = 0; i < NFUNC; i++)
 
36
    {
 
37
        _sc [i] = 0;
 
38
        _yc [i] = 0;
 
39
        _st [i] = 0;
 
40
    }
 
41
    _fc = 0;
 
42
    _ic = -1;
 
43
    _im = -1;
 
44
}
 
45
 
 
46
 
 
47
Functionwin::~Functionwin (void)
 
48
{
 
49
    for (int i = 0; i < NFUNC; i++)
 
50
    {
 
51
        delete[] _yc [i];
 
52
        delete[] _st [i];
 
53
    }
 
54
}
 
55
 
 
56
 
 
57
void Functionwin::handle_event (XEvent *E)
 
58
{
 
59
    switch (E->type)
 
60
    {
 
61
    case Expose:
 
62
        expose ((XExposeEvent *) E);
 
63
        break;  
 
64
 
 
65
    case ButtonPress:
 
66
        bpress ((XButtonEvent *) E);
 
67
        break;  
 
68
 
 
69
    case MotionNotify:
 
70
        motion ((XPointerMovedEvent *) E);
 
71
        break;  
 
72
 
 
73
    case ButtonRelease:
 
74
        brelse ((XButtonEvent *) E);
 
75
        break;  
 
76
 
 
77
   default:
 
78
        printf ("Multilsider::event %d\n", E->type);
 
79
    }
 
80
}
 
81
 
 
82
 
 
83
void Functionwin::set_xparam (int n, int x0, int dx)
 
84
{
 
85
    _n = n;
 
86
    _x0 = x0;
 
87
    _dx = dx;
 
88
    _xs = 2 * _x0 + (_n - 1) * _dx;
 
89
}
 
90
 
 
91
 
 
92
void Functionwin::set_yparam (int k, X_scale_style *scale, unsigned long color)
 
93
{
 
94
    if (k == 0)
 
95
    {
 
96
        _ys = scale->pixlen ();
 
97
        _ymax = _ys - 1 - scale->pix [0];
 
98
        _ymin = _ys - 1 - scale->pix [scale->nseg];
 
99
    }
 
100
    _sc [k] = scale;
 
101
    _co [k] = color;
 
102
    delete[] _yc [k];
 
103
    delete[] _st [k];
 
104
    _yc [k] = new int [_n];
 
105
    _st [k] = new char [_n];
 
106
    reset (k);
 
107
}
 
108
 
 
109
 
 
110
void Functionwin::show (void)
 
111
{
 
112
    x_resize (_xs, _ys);
 
113
    x_map ();   
 
114
}
 
115
 
 
116
 
 
117
void Functionwin::reset (int k)
 
118
{
 
119
    for (int i = 0; i < _n; i++)
 
120
    {
 
121
        _yc [k][i] = _ymax;
 
122
        _st [k][i] = 0;  
 
123
    }
 
124
}
 
125
 
 
126
 
 
127
void Functionwin::set_point (int k, int i, float v)
 
128
{
 
129
    if (_sc [k])
 
130
    {
 
131
        _st [k][i] = 1;
 
132
        _yc [k][i] = _ys - 1 - _sc [k]->calcpix (v);
 
133
    }
 
134
}
 
135
 
 
136
 
 
137
void Functionwin::upd_point (int k, int i, float v)
 
138
{
 
139
    if (_sc [k])
 
140
    {
 
141
        plot_line (k);
 
142
        _st [k][i] = 1;
 
143
        _yc [k][i] = _ys - 1 - _sc [k]->calcpix (v);
 
144
        plot_line (k);
 
145
    }
 
146
}
 
147
 
 
148
 
 
149
void Functionwin::clr_point (int k, int i)
 
150
{
 
151
    if (_sc [k])
 
152
    {
 
153
        plot_line (k);
 
154
        _st [k][i] = 0;
 
155
        plot_line (k);
 
156
    }
 
157
}
 
158
 
 
159
 
 
160
void Functionwin::set_mark (int i)
 
161
{
 
162
    if (_im != i)
 
163
    {
 
164
       plot_mark ();
 
165
       _im = i;
 
166
       plot_mark ();
 
167
    }        
 
168
}
 
169
 
 
170
 
 
171
void Functionwin::expose (XExposeEvent *E)
 
172
{
 
173
    if (E->count) return;
 
174
    redraw ();
 
175
}
 
176
 
 
177
 
 
178
void Functionwin::redraw (void)
 
179
{
 
180
    plot_grid ();
 
181
    plot_mark ();
 
182
    for (int i = 0; i < NFUNC; i++) if (_sc [i]) plot_line (i);
 
183
}
 
184
 
 
185
 
 
186
void Functionwin::bpress (XButtonEvent *E)
 
187
{
 
188
    int   i, j, n, x, y;
 
189
    int  *yc;
 
190
    char *st;
 
191
 
 
192
    x = E->x - _x0;
 
193
    y = E->y;
 
194
    i = (x + _dx / 2) / _dx;
 
195
    if ((i < 0) || (i >= _n)) return;
 
196
    if (abs (x - i * _dx) > 8) return;   
 
197
 
 
198
    yc = _yc [_fc];
 
199
    st = _st [_fc];
 
200
 
 
201
    if (E->state & ControlMask)
 
202
    {
 
203
        if (st [i])
 
204
        {
 
205
            for (j = n = 0; j < _n; j++) if (st [j]) n++;
 
206
            if ((n > 1) && (abs (y - yc [i]) <= 8))
 
207
            {
 
208
                plot_line (_fc);
 
209
                st [i] = 0;
 
210
                plot_line (_fc);
 
211
                if (_callb)
 
212
                {
 
213
                    _ic = i;
 
214
                    _vc = _sc [_fc]->calcval (_ys - 1 - y);
 
215
                    _callb->handle_callb (CB_FW_SEL, this, 0);
 
216
                    _callb->handle_callb (CB_FW_UND, this, 0);
 
217
                    _ic = -1;
 
218
                }                
 
219
            }
 
220
        }
 
221
        else
 
222
        {
 
223
           plot_line (_fc);
 
224
           if (y > _ymax) y = _ymax;
 
225
           if (y < _ymin) y = _ymin;
 
226
           yc [i] = y;
 
227
           st [i] = 1;
 
228
           plot_line (_fc);
 
229
           if (_callb)
 
230
           {
 
231
               _ic = i;
 
232
               _vc = _sc [_fc]->calcval (_ys - 1 - y);
 
233
               _callb->handle_callb (CB_FW_SEL, this, 0);
 
234
               _callb->handle_callb (CB_FW_DEF, this, 0);
 
235
           }
 
236
        }
 
237
    }
 
238
    else
 
239
    {
 
240
        for (j = 0; j < NFUNC; j++)
 
241
        {
 
242
            if (_sc [j] && _st [j][i] && (abs (_yc [j][i] - y) <= 8))
 
243
            {
 
244
                _fc = j;
 
245
                _ic = i;     
 
246
                if (_callb) _callb->handle_callb (CB_FW_SEL, this, 0);
 
247
                break;                
 
248
            }
 
249
        }
 
250
    }
 
251
}
 
252
 
 
253
 
 
254
void Functionwin::motion (XPointerMovedEvent *E)
 
255
{
 
256
    if (_ic >= 0)
 
257
    {
 
258
       if (E->state & Button3Mask) move_curve (E->y);
 
259
       else  move_point (E->y);
 
260
    }
 
261
}
 
262
 
 
263
 
 
264
void Functionwin::brelse (XButtonEvent *E)
 
265
{
 
266
    _ic = -1;
 
267
}
 
268
 
 
269
 
 
270
void Functionwin::plot_grid (void)
 
271
{
 
272
    int    i, x;
 
273
    X_draw D (dpy (), win (), dgc (), 0);
 
274
 
 
275
    D.clearwin ();
 
276
    D.setfunc (GXcopy);
 
277
    D.setcolor (_grid);
 
278
 
 
279
    for (i = 0; i <= _sc [0]->nseg; i++)
 
280
    {
 
281
        D.move (0, _ys - _sc [0]->pix [i] - 1);
 
282
        D.rdraw (_xs, 0);
 
283
    }
 
284
    x = _x0;
 
285
    for (i = 0; i <= 10; i++)
 
286
    {
 
287
        D.move (x, 0);
 
288
        D.rdraw (0, _ys); 
 
289
        x += _dx;
 
290
    }
 
291
 
 
292
    D.setcolor (Colors.main_ds);
 
293
    D.move (0, _ys);
 
294
    D.draw (0,0);
 
295
    D.draw (_xs, 0);
 
296
}
 
297
 
 
298
 
 
299
void Functionwin::plot_mark (void)
 
300
{
 
301
    X_draw D (dpy (), win (), dgc (), 0);
 
302
 
 
303
    if (_im >= 0)
 
304
    {
 
305
        D.setfunc (GXxor);
 
306
        D.setcolor (_mark ^ _grid);
 
307
        D.move (_x0 + _im * _dx, 0);
 
308
        D.rdraw (0, _ys);
 
309
    }
 
310
}
 
311
 
 
312
 
 
313
void Functionwin::plot_line (int k)
 
314
{
 
315
    int    i0, i1, x0, x1; 
 
316
    int   *yc;
 
317
    char  *st;
 
318
    X_draw D (dpy (), win (), dgc (), 0);
 
319
 
 
320
    yc = _yc [k];
 
321
    st = _st [k];
 
322
    D.setcolor (_co [k] ^ _bgnd);
 
323
    D.setfunc (GXxor);
 
324
 
 
325
    x0 = x1 = _x0;
 
326
    i0 = 0;
 
327
    if (st [0]) D.drawrect (x0 - 4, yc [i0] - 4, x0 + 4, yc [i0] + 4);
 
328
    for (i1 = 1; i1 < _n; i1++)
 
329
    {
 
330
        x1 += _dx;
 
331
        if (st [i1])
 
332
        {
 
333
            if (st [i0]) D.move (x0, yc [i0]);
 
334
            else         D.move (x0, yc [i1]);
 
335
            D.draw (x1, yc [i1]);
 
336
            D.drawrect (x1 - 4, yc [i1] - 4, x1 + 4, yc [i1] + 4);
 
337
            x0 = x1;
 
338
            i0 = i1;
 
339
        }    
 
340
    }
 
341
    if (x1 != x0)
 
342
    {
 
343
         D.move (x0, yc [i0]);
 
344
         D.draw (x1, yc [i0]);
 
345
    }
 
346
}
 
347
 
 
348
 
 
349
void Functionwin::move_point (int y)
 
350
{
 
351
    plot_line (_fc);
 
352
    if (y > _ymax) y = _ymax;
 
353
    if (y < _ymin) y = _ymin;
 
354
    _yc [_fc][_ic] = y;
 
355
    plot_line (_fc);
 
356
    if (_callb)
 
357
    {
 
358
        _vc = _sc [_fc]->calcval (_ys - 1 - y);
 
359
        _callb->handle_callb (CB_FW_UPD, this, 0);
 
360
    }
 
361
}
 
362
 
 
363
 
 
364
void Functionwin::move_curve (int y)
 
365
{
 
366
    int i, j, dy;
 
367
    int  *yc = _yc [_fc];
 
368
    char *st = _st [_fc];
 
369
 
 
370
    plot_line (_fc); 
 
371
    if (y > _ymax) y = _ymax;
 
372
    if (y < _ymin) y = _ymin;
 
373
    dy = y - yc [_ic];
 
374
    for (j = 0; j < _n; j++)
 
375
    {
 
376
        if (st [j])
 
377
        {
 
378
            y = yc [j] + dy;
 
379
            if (y > _ymax) y = _ymax;
 
380
            if (y < _ymin) y = _ymin;
 
381
            yc [j] = y;
 
382
        }
 
383
    }
 
384
    plot_line (_fc); 
 
385
    if (_callb)
 
386
    {
 
387
        i = _ic;
 
388
        for (j = 0; j < _n; j++)
 
389
        {
 
390
            if (st [j])
 
391
            {
 
392
                 _ic = j;
 
393
                 _vc = _sc [_fc]->calcval (_ys - 1 - yc [j]);
 
394
                 _callb->handle_callb (CB_FW_UPD, this, 0);
 
395
            }
 
396
        }
 
397
        _ic = i;
 
398
    }
 
399
}
 
400
 
 
401