~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to src/widgets/combo.cxx

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ----------------------------------------------------------------------------
 
2
// Copyright (C) 2014
 
3
//              David Freese, W1HKJ
 
4
//
 
5
//
 
6
// This is free software; you can redistribute it and/or modify
 
7
// it under the terms of the GNU General Public License as published by
 
8
// the Free Software Foundation; either version 3 of the License, or
 
9
// (at your option) any later version.
 
10
//
 
11
// This software is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
// GNU General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public License
 
17
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
// ----------------------------------------------------------------------------
 
19
 
 
20
#include <string>
 
21
 
 
22
#include <cstring>
 
23
#include <cstdlib>
 
24
#include <FL/Fl.H>
 
25
#include <FL/fl_draw.H>
 
26
 
 
27
#include "combo.h"
 
28
#include "util.h"
 
29
 
 
30
void popbrwsr_cb (Fl_Widget *v, long d);
 
31
 
 
32
Fl_PopBrowser::Fl_PopBrowser (int X, int Y, int W, int H, const char *label)
 
33
 : Fl_Window (X, Y, W, H, label)
 
34
{
 
35
        hRow  = H;
 
36
        wRow  = W;
 
37
        clear_border();
 
38
        box(FL_BORDER_BOX);
 
39
        align(FL_ALIGN_INSIDE);
 
40
 
 
41
        popbrwsr = new Fl_Select_Browser(0,0,wRow,hRow, "");
 
42
        popbrwsr->callback ( (Fl_Callback*)popbrwsr_cb);
 
43
        popbrwsr->align(FL_ALIGN_INSIDE);
 
44
        parentCB = 0;
 
45
        end();
 
46
        set_modal();
 
47
}
 
48
 
 
49
Fl_PopBrowser::~Fl_PopBrowser ()
 
50
{
 
51
        delete popbrwsr;
 
52
}
 
53
 
 
54
int Fl_PopBrowser::handle(int event)
 
55
{
 
56
        Fl_ComboBox *cbx = (Fl_ComboBox*)this->parent();
 
57
        if (!Fl::event_inside( child(0) ) && event == FL_PUSH) {
 
58
                pophide();
 
59
                return 1;
 
60
        }
 
61
        if (event == FL_KEYDOWN) {
 
62
                int  kbd = Fl::event_key();
 
63
                char key = Fl::event_text()[0];
 
64
                if (kbd == FL_Down) {
 
65
                        if (popbrwsr->value() < popbrwsr->size())
 
66
                                popbrwsr->select(popbrwsr->value() + 1);
 
67
                        popbrwsr->bottomline(popbrwsr->value());
 
68
                        return 1;
 
69
                }
 
70
                if (kbd == FL_Up && popbrwsr->value() > 1) {
 
71
                        popbrwsr->select(popbrwsr->value() - 1);
 
72
                        return 1;
 
73
                }
 
74
                if (key == '\r' || kbd == FL_Enter) { // kbd test for OS X
 
75
                        int n = popbrwsr->value() - 1;
 
76
                        pophide();
 
77
                        cbx->index(n);
 
78
                        cbx->do_callback();
 
79
                        return 1;
 
80
                }
 
81
                if (key == '\b' || kbd == FL_BackSpace) { // kbd test for OS X
 
82
                        if (!keystrokes.empty())
 
83
                                keystrokes.erase(keystrokes.length() - 1, 1);
 
84
                        return 1;
 
85
                }
 
86
                if (key == 0x1b || kbd == FL_Escape) { // kbd test for OS X
 
87
                        pophide();
 
88
                        return 0;
 
89
                } 
 
90
                if (key >= ' ' || key <= 0x7f) {
 
91
                        keystrokes += key;
 
92
                        for (int i = 0; i < cbx->listsize; i++) {
 
93
                                if (strncasecmp(keystrokes.c_str(), 
 
94
                                        cbx->datalist[i]->s,
 
95
                                        keystrokes.length()) == 0) {
 
96
                                        popbrwsr->select(i+1);
 
97
                                        popbrwsr->show(i+1);
 
98
                                        return 1;
 
99
                                }
 
100
                        }
 
101
                        return 0;
 
102
                }
 
103
        }
 
104
        return Fl_Group::handle(event);
 
105
}
 
106
 
 
107
void Fl_PopBrowser::add(char *s, void *d)
 
108
{
 
109
        popbrwsr->add(s,d);
 
110
}
 
111
 
 
112
void Fl_PopBrowser::clear()
 
113
{
 
114
        popbrwsr->clear();
 
115
}
 
116
 
 
117
void Fl_PopBrowser::sort()
 
118
{
 
119
        return;
 
120
}
 
121
 
 
122
void Fl_PopBrowser::popshow (int x, int y)
 
123
{
 
124
        int nRows = parentCB->numrows();
 
125
        int fh = fl_height();
 
126
        int height = nRows * fh + 4;
 
127
 
 
128
        if (popbrwsr->size() == 0) return;
 
129
        if (nRows > parentCB->lsize()) nRows = parentCB->lsize();
 
130
 
 
131
// locate first occurance of inp string value in the list
 
132
// and display that if found
 
133
        int i = parentCB->index();
 
134
        if (!(i >= 0 && i < parentCB->listsize)) {
 
135
                for (i = 0; i < parentCB->listsize; i++)
 
136
                        if (parentCB->type_ == COMBOBOX) {
 
137
                                if (!strcmp(parentCB->val->value(), parentCB->datalist[i]->s))
 
138
                                        break;
 
139
                        } else {
 
140
                                if (!strcmp(parentCB->valbox->label(), parentCB->datalist[i]->s))
 
141
                                        break;
 
142
                        }
 
143
                if (i == parentCB->listsize)
 
144
                        i = 0;
 
145
        }
 
146
 
 
147
// resize and reposition the popup to insure that it is within the bounds
 
148
// of the uppermost parent widget
 
149
// preferred position is just below and at the same x position as the
 
150
// parent widget
 
151
 
 
152
        Fl_Widget *gparent = parentCB;
 
153
        int hp = gparent->h();
 
154
 
 
155
        while ((gparent = gparent->parent())) {
 
156
                hp = gparent->h();
 
157
                parentWindow = gparent;
 
158
        }
 
159
 
 
160
        int nu = nRows, nl = nRows;
 
161
        int hu = nu * fh + 4, hl = nl * fh + 4;
 
162
        int yu = parentCB->y() - hu;
 
163
        int yl = y;
 
164
 
 
165
        while (nl > 1 && (yl + hl > hp)) { nl--; hl -= fh; }
 
166
        while (nu > 1 && yu < 0) { nu--; yu += fh; hu -= fh; }
 
167
 
 
168
        y = yl; height = hl;
 
169
        if (nl < nu) {
 
170
                y = yu;
 
171
                height = hu;
 
172
        }
 
173
 
 
174
        popbrwsr->size (wRow, height);
 
175
        resize (x, y, wRow, height);
 
176
 
 
177
        popbrwsr->topline (i);
 
178
 
 
179
        keystrokes.empty();
 
180
        popbrwsr->show();
 
181
        show();
 
182
        for (const Fl_Widget* o = popbrwsr; o; o = o->parent())
 
183
                ((Fl_Widget *)o)->set_visible();
 
184
 
 
185
        parentWindow->damage(FL_DAMAGE_ALL);
 
186
        parentWindow->redraw();
 
187
 
 
188
        Fl::grab(this);
 
189
}
 
190
 
 
191
void Fl_PopBrowser::pophide ()
 
192
{
 
193
        hide ();
 
194
 
 
195
        parentWindow->damage(FL_DAMAGE_ALL);
 
196
        parentWindow->redraw();
 
197
 
 
198
        Fl::grab(0);
 
199
        Fl::focus(((Fl_ComboBox*)parent())->btn);
 
200
}   
 
201
 
 
202
void Fl_PopBrowser::popbrwsr_cb_i (Fl_Widget *v, long d)
 
203
{
 
204
// update the return values
 
205
        Fl_Select_Browser *SB = (Fl_Select_Browser *)(v);
 
206
        Fl_PopBrowser *PB = (Fl_PopBrowser *)(SB->parent());
 
207
        Fl_ComboBox *CB = (Fl_ComboBox *)(PB->parent());
 
208
 
 
209
        int row = SB->value();
 
210
 
 
211
        if (row == 0) return;
 
212
        SB->deselect();
 
213
 
 
214
        CB->value(SB->text(row));
 
215
        CB->idx = row - 1;
 
216
 
 
217
        PB->pophide();
 
218
 
 
219
        CB->do_callback();
 
220
 
 
221
        return;
 
222
}
 
223
 
 
224
void popbrwsr_cb (Fl_Widget *v, long d)
 
225
{
 
226
        ((Fl_PopBrowser *)(v))->popbrwsr_cb_i (v, d);
 
227
        return;
 
228
}
 
229
 
 
230
 
 
231
void Fl_ComboBox::fl_popbrwsr(Fl_Widget *p)
 
232
{
 
233
        int xpos = p->x(), ypos = p->h() + p->y();
 
234
// pass the calling widget to the popup browser so that the
 
235
// correct callback function can be called when the user selects an item
 
236
// from the browser list
 
237
        Brwsr->parentCB = (Fl_ComboBox *) p;
 
238
        Brwsr->clear_kbd();
 
239
        Brwsr->popshow(xpos, ypos);
 
240
        return;
 
241
}
 
242
 
 
243
void btnComboBox_cb (Fl_Widget *v, void *d)
 
244
{
 
245
        Fl_ComboBox *p = (Fl_ComboBox *)(v->parent());
 
246
        p->fl_popbrwsr (p);
 
247
        return;
 
248
}
 
249
 
 
250
Fl_ComboBox::Fl_ComboBox (int X,int Y,int W,int H, const char *lbl, int wtype)
 
251
 : Fl_Group (X, Y, W, H, lbl),
 
252
 type_(wtype)
 
253
{
 
254
        width = W; height = H;
 
255
 
 
256
        if (type_ == LISTBOX) {
 
257
                valbox = new Fl_Box (FL_DOWN_BOX, X, Y, W-H, H, "");
 
258
                valbox->align(FL_ALIGN_INSIDE | FL_ALIGN_LEFT);
 
259
                valbox->color(FL_BACKGROUND2_COLOR);
 
260
        } else {
 
261
                val = new Fl_Input (X, Y, W-H, H, "");
 
262
                val->align(FL_ALIGN_INSIDE | FL_ALIGN_LEFT);
 
263
                readonly();
 
264
        }
 
265
 
 
266
        btn = new Fl_Button (X + W - H + 1, Y, H - 1, H, "@2>");
 
267
        btn->callback ((Fl_Callback *)btnComboBox_cb, 0);
 
268
 
 
269
        Brwsr = 0;
 
270
        datalist = new datambr *[FL_COMBO_LIST_INCR];
 
271
        maxsize = FL_COMBO_LIST_INCR;
 
272
        for (int i = 0; i < FL_COMBO_LIST_INCR; i++) datalist[i] = 0;
 
273
        listsize = 0;
 
274
        listtype = 0;
 
275
 
 
276
        Brwsr = new Fl_PopBrowser(X, Y, W, H, "");
 
277
        Brwsr->align(FL_ALIGN_INSIDE);
 
278
 
 
279
        idx = 0;
 
280
 
 
281
        end();
 
282
 
 
283
        numrows_ = 8;
 
284
}
 
285
 
 
286
Fl_ComboBox::~Fl_ComboBox()
 
287
{
 
288
        delete Brwsr;
 
289
        for (int i = 0; i < listsize; i++) {
 
290
                if (datalist[i]) {
 
291
                        if (datalist[i]->s) delete [] datalist[i]->s;
 
292
                        delete datalist[i];
 
293
                }
 
294
        }
 
295
        delete [] datalist;
 
296
}
 
297
 
 
298
int Fl_ComboBox::handle(int event)
 
299
{
 
300
        if (event == FL_KEYDOWN) {
 
301
                int  kbd = Fl::event_key();
 
302
                if (kbd == FL_Down) {
 
303
                        fl_popbrwsr (this);
 
304
                        return 1;
 
305
                }
 
306
        }
 
307
        return Fl_Group::handle(event);
 
308
}
 
309
 
 
310
void Fl_ComboBox::type (int t)
 
311
{
 
312
        listtype = t;
 
313
}
 
314
 
 
315
void Fl_ComboBox::readonly(bool yes)
 
316
{
 
317
        if (type_ == LISTBOX) return;
 
318
        val->readonly(yes);
 
319
        if (yes)
 
320
                val->selection_color(fl_rgb_color(173,216,230));
 
321
        else
 
322
                val->selection_color(FL_SELECTION_COLOR);
 
323
}
 
324
 
 
325
// ComboBox value is contained in the val widget
 
326
 
 
327
const char *Fl_ComboBox::value()
 
328
{
 
329
        if (type_ == LISTBOX)
 
330
                return valbox->label();
 
331
        else
 
332
                return val->value();
 
333
}
 
334
 
 
335
void Fl_ComboBox::value( const char *s )
 
336
{
 
337
        int i;
 
338
        if ((listtype & FL_COMBO_UNIQUE_NOCASE) == FL_COMBO_UNIQUE_NOCASE) {
 
339
                for (i = 0; i < listsize; i++) {
 
340
                        if (strcasecmp (s, datalist[i]->s) == 0)
 
341
                                break;
 
342
                }
 
343
        } else {
 
344
                for (i = 0; i < listsize; i++) {
 
345
                        if (strcmp (s, datalist[i]->s) == 0)
 
346
                                break;
 
347
                }
 
348
        }
 
349
        if ( i < listsize) {
 
350
                idx = i;
 
351
                if (type_ == LISTBOX) {
 
352
                        valbox->label(datalist[idx]->s);
 
353
                        valbox->redraw_label();
 
354
                } else
 
355
                        val->value(datalist[idx]->s);
 
356
        }
 
357
}
 
358
 
 
359
void Fl_ComboBox::put_value(const char *s)
 
360
{
 
361
        value(s);
 
362
}
 
363
 
 
364
void Fl_ComboBox::index(int i)
 
365
{
 
366
        if (i >= 0 && i < listsize) {
 
367
                idx = i;
 
368
                if (type_ == LISTBOX) {
 
369
                        valbox->label(datalist[idx]->s);
 
370
                        valbox->redraw_label();
 
371
                } else
 
372
                        val->value( datalist[idx]->s);
 
373
        }
 
374
}
 
375
 
 
376
int Fl_ComboBox::index() {
 
377
        return idx;
 
378
}
 
379
 
 
380
void * Fl_ComboBox::data() {
 
381
        return retdata; 
 
382
}
 
383
 
 
384
void Fl_ComboBox::insert(const char *str, void *d)
 
385
{
 
386
        datalist[listsize] = new datambr;
 
387
        datalist[listsize]->s = new char [strlen(str) + 1];
 
388
        datalist[listsize]->s[0] = 0;
 
389
        strcpy (datalist[listsize]->s, str);
 
390
        datalist[listsize]->d = 0;
 
391
        Brwsr->add(datalist[listsize]->s,d);
 
392
        listsize++;
 
393
        if (listsize == maxsize) {
 
394
                int nusize = maxsize + FL_COMBO_LIST_INCR;
 
395
                datambr **temparray = new datambr *[nusize];
 
396
                for (int i = 0; i < listsize; i++)
 
397
                        temparray[i] = datalist[i];
 
398
                delete [] datalist;
 
399
                datalist = temparray;
 
400
                maxsize = nusize;
 
401
        }
 
402
}
 
403
 
 
404
void Fl_ComboBox::add( const char *s, void * d)
 
405
{
 
406
        std::string str = s;
 
407
        std::string sinsert;
 
408
        size_t p = str.find("|");
 
409
        bool found = false;
 
410
        if (p != std::string::npos) {
 
411
                while (p != std::string::npos) {
 
412
                        sinsert = str.substr(0, p);
 
413
                        found = false;
 
414
// test for in list
 
415
                        if ((listtype & FL_COMBO_UNIQUE_NOCASE) == FL_COMBO_UNIQUE_NOCASE) {
 
416
                                for (int i = 0; i < listsize; i++) {
 
417
                                        if (sinsert == datalist[i]->s) {
 
418
                                                found = true;
 
419
                                                break;
 
420
                                        }
 
421
                                }
 
422
                        }
 
423
// not in list, so add this entry
 
424
                        if (!found) insert(sinsert.c_str(), 0);
 
425
                        str.erase(0, p+1);
 
426
                        p = str.find("|");
 
427
                }
 
428
                if (str.length()) 
 
429
                        insert(str.c_str(), 0);
 
430
        } else
 
431
                insert( s, d );
 
432
}
 
433
 
 
434
void Fl_ComboBox::clear()
 
435
{
 
436
        Brwsr->clear();
 
437
        
 
438
        if (listsize == 0) return;
 
439
        for (int i = 0; i < listsize; i++) {
 
440
                delete [] datalist[i]->s;
 
441
                delete datalist[i];
 
442
        }
 
443
        listsize = 0;
 
444
}
 
445
 
 
446
int DataCompare( const void *x1, const void *x2 )
 
447
{
 
448
        int cmp;
 
449
        datambr *X1, *X2;
 
450
        X1 = *(datambr **)(x1);
 
451
        X2 = *(datambr **)(x2);
 
452
        cmp = strcasecmp (X1->s, X2->s);
 
453
        if (cmp < 0)
 
454
                return -1;
 
455
        if (cmp > 0)
 
456
                return 1;
 
457
        return 0;
 
458
}
 
459
 
 
460
void Fl_ComboBox::sort() {
 
461
        Brwsr->clear ();
 
462
        qsort (&datalist[0],
 
463
                 listsize,
 
464
                 sizeof (datambr *),
 
465
                 DataCompare);
 
466
        for (int i = 0; i < listsize; i++)
 
467
                Brwsr->add (datalist[i]->s, datalist[i]->d);
 
468
}
 
469
 
 
470
void Fl_ComboBox::textfont (int fnt)
 
471
{
 
472
        if (type_ == LISTBOX)
 
473
                valbox->labelfont (fnt);
 
474
        else
 
475
                val->textfont (fnt);
 
476
}
 
477
 
 
478
void Fl_ComboBox::textsize (uchar n)
 
479
{
 
480
        if (type_ == LISTBOX)
 
481
                valbox->labelsize(n);
 
482
        else
 
483
                val->textsize (n);
 
484
}
 
485
 
 
486
void Fl_ComboBox::textcolor( Fl_Color c)
 
487
{
 
488
        if (type_ == LISTBOX)
 
489
                valbox->labelcolor (c);
 
490
        else
 
491
                val->textcolor (c);
 
492
}
 
493
 
 
494
void Fl_ComboBox::color(Fl_Color c)
 
495
{
 
496
        _color = c;
 
497
        if (type_ == LISTBOX)
 
498
                valbox->color(c);
 
499
        else
 
500
                val->color(c);
 
501
        if (Brwsr) Brwsr->color(c);
 
502
}