combobox-listbox

* Update to combobox, Fl_ComboBox
  * Addition of listbox, Fl_ListBox
pull/1/head
David Freese 2014-08-29 03:04:34 -05:00
rodzic 481be5c0d3
commit 8fef9bbc7e
2 zmienionych plików z 297 dodań i 134 usunięć

Wyświetl plik

@ -2,14 +2,13 @@
// Copyright (C) 2014
// David Freese, W1HKJ
//
// This file is part of fldigi
//
// fldigi is free software; you can redistribute it and/or modify
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// fldigi is distributed in the hope that it will be useful,
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
@ -18,41 +17,91 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#include <string>
#include <cstring>
#include <cstdlib>
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Output.H>
#include "config.h"
#include "combo.h"
void popbrwsr_cb (Fl_Widget *v, long d);
Fl_PopBrowser::Fl_PopBrowser (int X, int Y, int W, int H, retvals R)
: Fl_Window (X, Y, W, H, "")
Fl_PopBrowser::Fl_PopBrowser (int X, int Y, int W, int H, const char *label)
: Fl_Window (X, Y, W, H, label)
{
Rvals = R;
hRow = H;
wRow = W;
clear_border();
box(FL_BORDER_BOX);
popbrwsr = new Fl_Select_Browser(0,0,wRow,hRow,0);
align(FL_ALIGN_INSIDE);
popbrwsr = new Fl_Select_Browser(0,0,wRow,hRow, "");
popbrwsr->callback ( (Fl_Callback*)popbrwsr_cb);
parent = 0;
popbrwsr->align(FL_ALIGN_INSIDE);
parentCB = 0;
end();
set_modal();
}
Fl_PopBrowser::~Fl_PopBrowser ()
{
delete popbrwsr;
}
int Fl_PopBrowser::handle(int event)
{
Fl_ComboBox *cbx = (Fl_ComboBox*)this->parent();
if (!Fl::event_inside( child(0) ) && event == FL_PUSH) {
pophide();
return 1;
}
if (event == FL_KEYDOWN) {
int kbd = Fl::event_key();
char key = Fl::event_text()[0];
if (kbd == FL_Down) {
if (popbrwsr->value() < popbrwsr->size())
popbrwsr->select(popbrwsr->value() + 1);
popbrwsr->bottomline(popbrwsr->value());
return 1;
}
if (kbd == FL_Up && popbrwsr->value() > 1) {
popbrwsr->select(popbrwsr->value() - 1);
return 1;
}
if (key == '\r' || kbd == FL_Enter) { // kbd test for OS X
int n = popbrwsr->value() - 1;
pophide();
cbx->index(n);
cbx->do_callback();
return 1;
}
if (key == '\b' || kbd == FL_BackSpace) { // kbd test for OS X
if (!keystrokes.empty())
keystrokes.erase(keystrokes.length() - 1, 1);
return 1;
}
if (key == 0x1b || kbd == FL_Escape) { // kbd test for OS X
pophide();
return 0;
}
if (key >= ' ' || key <= 0x7f) {
keystrokes += key;
for (int i = 0; i < cbx->listsize; i++) {
if (strncasecmp(keystrokes.c_str(),
cbx->datalist[i]->s,
keystrokes.length()) == 0) {
popbrwsr->select(i+1);
popbrwsr->show(i+1);
return 1;
}
}
return 0;
}
}
return Fl_Group::handle(event);
}
@ -73,21 +122,26 @@ void Fl_PopBrowser::sort()
void Fl_PopBrowser::popshow (int x, int y)
{
int nRows = parent->numrows();
int nRows = parentCB->numrows();
int fh = fl_height();
int height = nRows * fh + 4;
if (popbrwsr->size() == 0) return;
if (nRows > parent->lsize()) nRows = parent->lsize();
if (nRows > parentCB->lsize()) nRows = parentCB->lsize();
// locate first occurance of Output string value in the list
// locate first occurance of inp string value in the list
// and display that if found
int i = parent->index();
if (!(i >= 0 && i < parent->listsize)) {
for (i = 0; i < parent->listsize; i++)
if (!strcmp(parent->Output->value(), parent->datalist[i]->s))
break;
if (i == parent->listsize)
int i = parentCB->index();
if (!(i >= 0 && i < parentCB->listsize)) {
for (i = 0; i < parentCB->listsize; i++)
if (parentCB->type_ == COMBOBOX) {
if (!strcmp(parentCB->val->value(), parentCB->datalist[i]->s))
break;
} else {
if (!strcmp(parentCB->valbox->label(), parentCB->datalist[i]->s))
break;
}
if (i == parentCB->listsize)
i = 0;
}
@ -96,35 +150,41 @@ void Fl_PopBrowser::popshow (int x, int y)
// preferred position is just below and at the same x position as the
// parent widget
Fl_Widget *gparent = parent;
int xp = gparent->x(),
yp = gparent->y(),
hp = gparent->h();
Fl_Widget *gparent = parentCB;
int hp = gparent->h();
while ((gparent = gparent->parent())) {
xp = gparent->x();
yp = gparent->y();
hp = gparent->h();
parentWindow = gparent;
}
int nu = nRows, nl = nRows;
int hu = nu * fh + 4, hl = nl * fh + 4;
int yu = parent->y() - hu;
int yu = parentCB->y() - hu;
int yl = y;
while (nl > 1 && (yl + hl > hp)) { nl--; hl -= fh; }
while (nu > 1 && yu < 0) { nu--; yu += fh; hu -= fh; }
if (nl >= nu) { y = yl; height = hl; }
else { y = yu; height = hu; }
x += xp;
y += yp;
y = yl; height = hl;
if (nl < nu) {
y = yu;
height = hu;
}
popbrwsr->size (wRow, height);
resize (x, y, wRow, height);
popbrwsr->topline (i);
keystrokes.empty();
popbrwsr->show();
show();
for (const Fl_Widget* o = popbrwsr; o; o = o->parent())
((Fl_Widget *)o)->set_visible();
parentWindow->damage(FL_DAMAGE_ALL);
parentWindow->redraw();
Fl::grab(this);
}
@ -132,28 +192,33 @@ void Fl_PopBrowser::popshow (int x, int y)
void Fl_PopBrowser::pophide ()
{
hide ();
Fl::release();
parentWindow->damage(FL_DAMAGE_ALL);
parentWindow->redraw();
Fl::grab(0);
Fl::focus(((Fl_ComboBox*)parent())->btn);
}
void Fl_PopBrowser::popbrwsr_cb_i (Fl_Widget *v, long d)
{
Fl_PopBrowser *me = (Fl_PopBrowser *)(v->parent());
Fl_Input *tgt = me->Rvals.Inp;
// update the return values
int row = (me->popbrwsr)->value();
if (row == 0) return;
me->popbrwsr->deselect();
Fl_Select_Browser *SB = (Fl_Select_Browser *)(v);
Fl_PopBrowser *PB = (Fl_PopBrowser *)(SB->parent());
Fl_ComboBox *CB = (Fl_ComboBox *)(PB->parent());
int row = SB->value();
if (row == 0) return;
SB->deselect();
CB->value(SB->text(row));
CB->idx = row - 1;
PB->pophide();
CB->do_callback();
if (tgt) {
tgt->value ((me->popbrwsr)->text (row));
me->Rvals.retval = (me->popbrwsr)->data (row);
*(me->Rvals.idx) = row - 1;
}
me->pophide();
// user selected an item from the browser list, so execute the
// callback if one is registered.
if (me->parent)
(me->parent)->do_callback();
return;
}
@ -167,32 +232,40 @@ void popbrwsr_cb (Fl_Widget *v, long d)
void Fl_ComboBox::fl_popbrwsr(Fl_Widget *p)
{
int xpos = p->x(), ypos = p->h() + p->y();
if (Brwsr == 0) {
Brwsr = new Fl_PopBrowser(xpos, ypos, width, height, R);
}
// pass the calling widget to the popup browser so that the
// correct callback function can be called when the user selects an item
// from the browser list
Brwsr->parent = (Fl_ComboBox *) p;
Brwsr->parentCB = (Fl_ComboBox *) p;
Brwsr->clear_kbd();
Brwsr->popshow(xpos, ypos);
return;
}
void btnComboBox_cb (Fl_Widget *v, void *d)
{
Fl_Widget *p = v->parent();
((Fl_ComboBox *)p)->fl_popbrwsr (p);
Fl_ComboBox *p = (Fl_ComboBox *)(v->parent());
p->fl_popbrwsr (p);
return;
}
Fl_ComboBox::Fl_ComboBox (int X,int Y,int W,int H, const char *L)
: Fl_Group (X, Y, W, H, L)
Fl_ComboBox::Fl_ComboBox (int X,int Y,int W,int H, const char *lbl, int wtype)
: Fl_Group (X, Y, W, H, lbl),
type_(wtype)
{
width = W; height = H - 4;
Btn = new Fl_Button (X + W - 18, Y + 1, 18, H - 2, "@#-32>");
Btn->callback ((Fl_Callback *)btnComboBox_cb, 0);
Output = new Fl_Input (X, Y, W-18, H);
width = W; height = H;
if (type_ == LISTBOX) {
valbox = new Fl_Box (FL_DOWN_BOX, X, Y, W-H, H, "");
valbox->align(FL_ALIGN_INSIDE | FL_ALIGN_LEFT);
valbox->color(FL_BACKGROUND2_COLOR);
} else {
val = new Fl_Input (X, Y, W-H, H, "");
val->align(FL_ALIGN_INSIDE | FL_ALIGN_LEFT);
readonly();
}
btn = new Fl_Button (X + W - H + 1, Y, H - 1, H, "@2>");
btn->callback ((Fl_Callback *)btnComboBox_cb, 0);
Brwsr = 0;
datalist = new datambr *[FL_COMBO_LIST_INCR];
@ -200,16 +273,20 @@ Fl_ComboBox::Fl_ComboBox (int X,int Y,int W,int H, const char *L)
for (int i = 0; i < FL_COMBO_LIST_INCR; i++) datalist[i] = 0;
listsize = 0;
listtype = 0;
Brwsr = new Fl_PopBrowser(X, Y, W, H, "");
Brwsr->align(FL_ALIGN_INSIDE);
idx = 0;
end();
R.Inp = Output;
R.retval = retdata;
R.idx = &idx;
numrows_ = 8;
}
Fl_ComboBox::~Fl_ComboBox()
{
if (Brwsr) delete Brwsr;
delete Brwsr;
for (int i = 0; i < listsize; i++) {
if (datalist[i]) {
if (datalist[i]->s) delete [] datalist[i]->s;
@ -219,17 +296,42 @@ Fl_ComboBox::~Fl_ComboBox()
delete [] datalist;
}
int Fl_ComboBox::handle(int event)
{
if (event == FL_KEYDOWN) {
int kbd = Fl::event_key();
if (kbd == FL_Down) {
fl_popbrwsr (this);
return 1;
}
}
return Fl_Group::handle(event);
}
void Fl_ComboBox::type (int t)
{
listtype = t;
}
void Fl_ComboBox::readonly()
void Fl_ComboBox::readonly(bool yes)
{
Output->type(FL_NORMAL_OUTPUT);
if (type_ == LISTBOX) return;
val->readonly(yes);
if (yes)
val->selection_color(fl_rgb_color(173,216,230));
else
val->selection_color(FL_SELECTION_COLOR);
}
// ComboBox value is contained in the Output widget
// ComboBox value is contained in the val widget
const char *Fl_ComboBox::value()
{
if (type_ == LISTBOX)
return valbox->label();
else
return val->value();
}
void Fl_ComboBox::value( const char *s )
{
@ -245,8 +347,14 @@ void Fl_ComboBox::value( const char *s )
break;
}
}
if ( i < listsize)
Output->value(datalist[i]->s);
if ( i < listsize) {
idx = i;
if (type_ == LISTBOX) {
valbox->label(datalist[idx]->s);
valbox->redraw_label();
} else
val->value(datalist[idx]->s);
}
}
void Fl_ComboBox::put_value(const char *s)
@ -256,14 +364,14 @@ void Fl_ComboBox::put_value(const char *s)
void Fl_ComboBox::index(int i)
{
if (i >= 0 && i < listsize)
Output->value( datalist[idx = i]->s);
}
const char *Fl_ComboBox::value()
{
return (Output->value ());
if (i >= 0 && i < listsize) {
idx = i;
if (type_ == LISTBOX) {
valbox->label(datalist[idx]->s);
valbox->redraw_label();
} else
val->value( datalist[idx]->s);
}
}
int Fl_ComboBox::index() {
@ -274,49 +382,59 @@ void * Fl_ComboBox::data() {
return retdata;
}
void Fl_ComboBox::add( const char *s, void * d)
void Fl_ComboBox::insert(const char *str, void *d)
{
if (Brwsr == 0) {
Brwsr = new Fl_PopBrowser(0, 0, width, height, R);
}
// test for uniqueness of entry if required
if ((listtype & FL_COMBO_UNIQUE) == FL_COMBO_UNIQUE) {
if ((listtype & FL_COMBO_UNIQUE_NOCASE) == FL_COMBO_UNIQUE_NOCASE) {
for (int i = 0; i < listsize; i++) {
if (strcasecmp (s, datalist[i]->s) == 0)
return;
}
} else {
for (int i = 0; i < listsize; i++) {
if (strcmp (s, datalist[i]->s) == 0)
return;
}
}
}
// not unique or not in list, so add this entry
datalist[listsize] = new datambr;
datalist[listsize]->s = new char [strlen(s) + 1];
datalist[listsize]->s = new char [strlen(str) + 1];
datalist[listsize]->s[0] = 0;
strcpy (datalist[listsize]->s, s);
datalist[listsize]->d = d;
strcpy (datalist[listsize]->s, str);
datalist[listsize]->d = 0;
Brwsr->add(datalist[listsize]->s,d);
listsize++;
if (listsize == maxsize) {
int nusize = maxsize + FL_COMBO_LIST_INCR;
datambr **temparray = new datambr *[nusize];
for (int i = 0; i < listsize; i++) temparray[i] = datalist[i];
for (int i = 0; i < listsize; i++)
temparray[i] = datalist[i];
delete [] datalist;
datalist = temparray;
maxsize = nusize;
}
}
void Fl_ComboBox::add( const char *s, void * d)
{
std::string str = s;
std::string sinsert;
size_t p = str.find("|");
bool found = false;
if (p != std::string::npos) {
while (p != std::string::npos) {
sinsert = str.substr(0, p);
found = false;
// test for in list
if ((listtype & FL_COMBO_UNIQUE_NOCASE) == FL_COMBO_UNIQUE_NOCASE) {
for (int i = 0; i < listsize; i++) {
if (sinsert == datalist[i]->s) {
found = true;
break;
}
}
}
// not in list, so add this entry
if (!found) insert(sinsert.c_str(), 0);
str.erase(0, p+1);
p = str.find("|");
}
if (str.length())
insert(str.c_str(), 0);
} else
insert( s, d );
}
void Fl_ComboBox::clear()
{
if (Brwsr == 0)
Brwsr = new Fl_PopBrowser(0, 0, width, height, R);
else
Brwsr->clear();
Brwsr->clear();
if (listsize == 0) return;
for (int i = 0; i < listsize; i++) {
@ -352,25 +470,34 @@ void Fl_ComboBox::sort() {
void Fl_ComboBox::textfont (int fnt)
{
Output->textfont (fnt);
if (type_ == LISTBOX)
valbox->labelfont (fnt);
else
val->textfont (fnt);
}
void Fl_ComboBox::textsize (uchar n)
{
Output->textsize (n);
if (type_ == LISTBOX)
valbox->labelsize(n);
else
val->textsize (n);
}
void Fl_ComboBox::textcolor( Fl_Color c)
{
Output->textcolor (c);
if (type_ == LISTBOX)
valbox->labelcolor (c);
else
val->textcolor (c);
}
void Fl_ComboBox::color(Fl_Color c)
{
_color = c;
Output->color(c);
if (type_ == LISTBOX)
valbox->color(c);
else
val->color(c);
if (Brwsr) Brwsr->color(c);
}

Wyświetl plik

@ -2,14 +2,13 @@
// Copyright (C) 2014
// David Freese, W1HKJ
//
// This file is part of fldigi
//
// fldigi is free software; you can redistribute it and/or modify
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// fldigi is distributed in the hope that it will be useful,
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
@ -21,11 +20,15 @@
#ifndef _FL_COMBOBOX_H
#define _FL_COMBOBOX_H
#include <string>
#include <FL/Fl_Window.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Select_Browser.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Return_Button.H>
#include <FL/Fl_Box.H>
#define FL_COMBO_UNIQUE 1
#define FL_COMBO_UNIQUE_NOCASE 2
@ -33,27 +36,24 @@
class Fl_ComboBox;
enum {COMBOBOX, LISTBOX};
struct datambr {
char *s;
void *d;
};
struct retvals {
Fl_Input *Inp;
void * retval;
int * idx;};
class Fl_PopBrowser : public Fl_Window {
friend void popbrwsr_cb(Fl_Widget *, long);
friend void popbrwsr_cb(Fl_Widget *, long);
protected:
Fl_Select_Browser *popbrwsr;
retvals Rvals;
int hRow;
int wRow;
public:
Fl_PopBrowser (int x, int y, int w, int h, retvals R);
std::string keystrokes;
public:
Fl_PopBrowser (int x, int y, int w, int h, const char *label);
~Fl_PopBrowser ();
void popshow (int, int);
void pophide ();
@ -63,38 +63,41 @@ class Fl_PopBrowser : public Fl_Window {
void clear ();
void sort ();
int handle (int);
void clear_kbd() { keystrokes.clear(); }
Fl_ComboBox *parent;
Fl_ComboBox *parentCB;
Fl_Widget *parentWindow;
};
class Fl_ComboBox : public Fl_Group {
friend int DataCompare (const void *, const void *);
friend class Fl_PopBrowser;
protected:
Fl_Button *Btn;
Fl_Input *Output;
Fl_Button *btn;
Fl_Box *valbox;
Fl_Input *val;
Fl_PopBrowser *Brwsr;
datambr **datalist;
int listsize;
int maxsize;
int listtype;
int numrows_;
int type_;
private:
int width;
int height;
void *retdata;
int idx;
retvals R;
Fl_Color _color;
Fl_Color _color;
void insert(const char *, void *);
public:
public:
Fl_ComboBox (int x, int y, int w, int h, const char * = 0);
Fl_ComboBox (int x, int y, int w, int h, const char *lbl = 0,
int wtype = COMBOBOX);
~Fl_ComboBox();
const char *value ();
void value (const char *);
void put_value( const char *);
@ -111,12 +114,45 @@ class Fl_ComboBox : public Fl_Group {
void textsize (uchar);
void textcolor (Fl_Color c);
void color (Fl_Color c);
void readonly();
void readonly(bool yes = true);
int numrows() { return numrows_; }
void numrows(int n) { numrows_ = n; }
int lsize() { return listsize; }
void set_focus() { Fl::focus(btn); };
void labelfont(Fl_Font fnt) { Fl_Group::labelfont(fnt); }
Fl_Font labelfont() { return Fl_Group::labelfont(); }
void labelsize(Fl_Fontsize n) { Fl_Group::labelsize(n); }
Fl_Fontsize labelsize() { return Fl_Group::labelsize(); }
int handle(int);
// Custom resize behavior -- input stretches, button doesn't
inline int val_x() { return x(); }
inline int val_y() { return y(); }
inline int val_w() { return w() - h(); }
inline int val_h() { return h(); }
inline int btn_x() { return x() + w() - h(); }
inline int btn_y() { return y(); }
inline int btn_w() { return h(); }
inline int btn_h() { return h(); }
void resize(int X, int Y, int W, int H) {
Fl_Group::resize(X,Y,W,H);
if (type_ == LISTBOX)
valbox->resize(val_x(), val_y(), val_w(), val_h());
else
val->resize(val_x(), val_y(), val_w(), val_h());
btn->resize(btn_x(), btn_y(), btn_w(), btn_h());
}
};
class Fl_ListBox : public Fl_ComboBox {
public:
Fl_ListBox (int x, int y, int w, int h, const char *lbl = 0) :
Fl_ComboBox(x,y,w,h,lbl,LISTBOX) {};
~Fl_ListBox() {};
};
#endif