forked from yuryfdr/pbtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpblistbox.h
165 lines (153 loc) · 4.97 KB
/
pblistbox.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (C) 2011-2012 Yury P. Fedorchenko (yuryfdr at users.sf.net) */
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef PBLISTBOX_H
#define PBLISTBOX_H
#include "pbwidget.h"
class PBListBoxItem:public PBWidget {
protected:
std::string _marker;
int _maxHeight;
int _align;
public:
PBListBoxItem(const std::string & name, PBWidget * parent, int al = 1):PBWidget(name, parent)
, _align(al) {
}
virtual bool eventInside(int X, int Y) {
if (!_visible)
return false;
return PBWidget::eventInside(X,Y);
}
virtual void draw();
virtual int h() const;
int getMaxHeight() const { return _maxHeight; }
void setMaxHeight(int val) { _maxHeight = val; }
std::string getMarker()const { return _marker; }
virtual void setMarker(const std::string & val) { _marker = val; }
};
typedef std::vector < PBListBoxItem * >::iterator lbitem_it;
typedef std::vector < PBListBoxItem * >::const_iterator lbitem_cit;
class PBListBox:public PBWidget {
protected:
std::vector < PBListBoxItem * >_items;
bool _graylinks;
bool _scrollOnRL;
int totalHeight;
int scrollDelta;
protected:
std::string _caption;
virtual int getCaptionHeight() const;
void focusFirstIfCan();
public:
virtual void placeWidgets();
PBListBox(const std::string & name, PBWidget * parent):PBWidget(name, parent)
, _graylinks(false), _scrollOnRL(false), scrollDelta(0) { }
~PBListBox();
bool isGray() const { return _graylinks; }
virtual int handle(int type, int par1, int par2);
virtual void draw();
virtual std::string getCaption() const { return _caption;}
virtual void setCaption(const std::string & val) { _caption = val; };
/**
scroll item without making it focused
return true if update needed (if scroll value is changed)
*/
virtual bool scrollTo(int nmb){
if(nmb>=0&&nmb<_items.size())
return scrollTo(_items[nmb]);
return false;
}
virtual bool scrollTo(PBListBoxItem *);
/**
select item (make it focused)
*/
virtual void selectItem(PBListBoxItem *,bool update=true);
virtual void selectItem(int nmb,bool update=true);
/**
return items count
*/
int itemsCount()const{return _items.size();}
virtual PBListBoxItem *addItem(const std::string & text, const std::string & tag = "", int align = 1);
virtual PBListBoxItem *addItem(boost::shared_ptr<PBImage> image, const std::string & tag = "",int align =1);
PBListBoxItem *insertBefore(const PBListBoxItem* itm,const std::string & text);
PBListBoxItem *insertAfter(const PBListBoxItem* itm,const std::string & text);
PBListBoxItem *erase(const PBListBoxItem* itm,bool delete_item=true);
template<typename Function>
void forEachItem(Function itmz){
for(lbitem_it it = _items.begin();it!=_items.end();++it){
itmz( *it );
}
}
template<typename Function>
int findItem(int ib,Function itmz){
if(ib<0||ib>=(int)_items.size())return -1;
for(lbitem_it it = _items.begin()+ib;it!=_items.end();++it){
if(itmz( *it ))return it-_items.begin();
}
return -1;
}
template<typename Function>
int findItemRev(int ib,Function itmz){
if(ib<0||ib>=(int)_items.size())return -1;
for(lbitem_it it = _items.begin()+ib;it!=_items.begin();--it){
if(itmz( *it ))return it-_items.begin();
}
return -1;
}
virtual void clear();
virtual int getSelectedIndex() const;
virtual PBListBoxItem* getSelectedItem(){
return getItem(getSelectedIndex());
};
PBListBoxItem* getItem(int i){
if(i>=0 && i<(int)_items.size()){
return _items[i];
}
return NULL;
}
PBListBoxItem* nextItem(PBListBoxItem* itm,bool loop=false){
for(int i=0;i<(int)_items.size();++i){
if(itm==_items[i]){
if(i==_items.size()-1){
if(loop)return _items[0];
else return NULL;
}
return _items[i+1];
}
}
return NULL;
}
PBListBoxItem* prevItem(PBListBoxItem* itm,bool loop=false){
for(int i=0;i<(int)_items.size();++i){
if(itm==_items[i]){
if(i==0){
if(loop)return _items[_items.size()-1];
else return NULL;
}
return _items[i-1];
}
}
return NULL;
}
int getIndex(PBListBoxItem* itm)const{
for(int i=0;i<(int)_items.size();++i){
if(itm==_items[i])return i;
}
return -1;
}
sigc::signal<void,PBListBox*,int> onItemAction;//long OK or tap
};
#endif