Debian Codegraph @

src/widgets/combo.cxx

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