~ubuntu-branches/ubuntu/intrepid/aeolus/intrepid

« back to all changes in this revision

Viewing changes to multislider.cc

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2007-05-14 22:18:54 UTC
  • Revision ID: james.westby@ubuntu.com-20070514221854-274rj6fqs5tegu7q
Tags: upstream-0.6.6+2
ImportĀ upstreamĀ versionĀ 0.6.6+2

Show diffs side-by-side

added added

removed removed

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