-
Notifications
You must be signed in to change notification settings - Fork 511
/
textlist.cpp
379 lines (294 loc) · 9.28 KB
/
textlist.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "textlist.h"
#include "tw/colors.h"
#include "tw/event.h"
#include "tw/scrollbar.h"
#include "tw/keycodes.h"
#include <vector>
#include <map>
#include <string>
using namespace std;
using namespace TwConsts;
namespace
{
const int rowHeight = 20; // 15
};
//==============================================================================
class TTextList::Data
{
public:
Data(TWidget *w) : m_w(w), m_selAction(0), m_dblClickAction(0), m_scrollbar(new TScrollbar(w)), m_yoffset(0)
{
}
~Data()
{
if (m_selAction)
delete m_selAction;
if (m_dblClickAction)
delete m_dblClickAction;
std::map<string, TTextListItem *>::iterator it = m_items.begin();
for (; it != m_items.end(); ++it)
delete it->second;
}
int posToItem(const TPoint &p);
void updateScrollBarStatus()
{
if (!m_scrollbar)
return;
unsigned int ly = m_w->getLy();
if ((m_items.size() * rowHeight) > ly) {
m_scrollbar->setValue(m_yoffset, 0, (m_items.size() + 1) * rowHeight - ly, ly);
m_scrollbar->show(); //m_scrollbar->setValue(m_yoffset,0, yrange-ly, ly);
} else {
m_yoffset = 0;
m_scrollbar->hide(); //m_scrollbar->setValue(0,0, 0, 0);
}
m_scrollbar->invalidate();
}
TWidget *m_w;
std::map<string, TTextListItem *> m_items;
vector<string> m_selectedItems;
TGenericTextListAction *m_selAction;
TGenericTextListAction *m_dblClickAction;
TScrollbar *m_scrollbar;
int m_yoffset;
};
//------------------------------------------------------------------------------
int TTextList::Data::posToItem(const TPoint &p)
{
/* TDimension d = getSize();
int i = getItemCount()-1;
10 +
for(int y=10; y<d.ly && i>=0; y+=rowHeight, --i)
*/
int y = m_w->getSize().ly - p.y + m_yoffset;
int item = y / rowHeight;
return item;
}
//==============================================================================
TTextListItem::TTextListItem(const string &id, const string &caption)
: m_id(id), m_caption(caption)
{
}
//==============================================================================
TTextList::TTextList(TWidget *parent, string name)
: TWidget(parent, name), m_data(0)
{
m_data = new Data(this);
m_data->m_scrollbar->setAction(new TScrollbarAction<TTextList>(this, &TTextList::scrollTo));
}
//------------------------------------------------------------------------------
TTextList::~TTextList()
{
delete m_data;
}
//------------------------------------------------------------------------------
void TTextList::addItem(TTextListItem *item)
{
std::map<string, TTextListItem *>::iterator it = m_data->m_items.find(item->getId());
if (it == m_data->m_items.end()) {
m_data->m_items.insert(make_pair(item->getId(), item));
m_data->updateScrollBarStatus();
}
}
//------------------------------------------------------------------------------
void TTextList::removeItem(const string &itemId)
{
std::map<string, TTextListItem *>::iterator it = m_data->m_items.find(itemId);
if (it != m_data->m_items.end()) {
m_data->m_items.erase(it);
m_data->updateScrollBarStatus();
}
}
//------------------------------------------------------------------------------
void TTextList::clearAll()
{
m_data->m_items.clear();
m_data->m_selectedItems.clear();
invalidate();
m_data->updateScrollBarStatus();
}
//------------------------------------------------------------------------------
int TTextList::getItemCount() const
{
return m_data->m_items.size();
}
//------------------------------------------------------------------------------
TTextListItem *TTextList::getItem(int i) const
{
if (i >= 0 && i < (int)m_data->m_items.size()) {
std::map<string, TTextListItem *>::iterator it = m_data->m_items.begin();
advance(it, i);
return it->second;
}
return 0;
}
//------------------------------------------------------------------------------
int TTextList::itemToIndex(const string &itemId)
{
std::map<string, TTextListItem *>::iterator it = m_data->m_items.find(itemId);
if (it == m_data->m_items.end())
return -1;
else
return distance(m_data->m_items.begin(), it);
}
//------------------------------------------------------------------------------
int TTextList::getSelectedItemCount() const
{
return m_data->m_selectedItems.size();
}
//------------------------------------------------------------------------------
TTextListItem *TTextList::getSelectedItem(int i) const
{
if (i < 0 || (int)m_data->m_selectedItems.size() <= i)
return 0;
string itemId = m_data->m_selectedItems[i];
std::map<string, TTextListItem *>::iterator it = m_data->m_items.find(itemId);
if (it != m_data->m_items.end())
return it->second;
else
return 0;
}
//------------------------------------------------------------------------------
string TTextList::getSelectedItemId(int i) const
{
if ((int)m_data->m_selectedItems.size() <= i)
return "";
assert(i >= 0 && i < (int)m_data->m_selectedItems.size());
return m_data->m_selectedItems[i];
}
//------------------------------------------------------------------------------
void TTextList::select(int i, bool on)
{
assert(i >= 0 && i < (int)m_data->m_items.size());
std::map<string, TTextListItem *>::iterator it = m_data->m_items.begin();
advance(it, i);
string id = it->first;
vector<string>::iterator it2 =
find(m_data->m_selectedItems.begin(), m_data->m_selectedItems.end(), id);
if (on) {
if (it2 == m_data->m_selectedItems.end())
m_data->m_selectedItems.push_back(id);
} else {
if (it2 != m_data->m_selectedItems.end())
m_data->m_selectedItems.erase(it2);
}
if (m_data->m_selAction)
m_data->m_selAction->sendCommand(i);
invalidate();
}
//------------------------------------------------------------------------------
void TTextList::select(const string &itemId, bool on)
{
std::map<string, TTextListItem *>::iterator it =
m_data->m_items.find(itemId);
if (it != m_data->m_items.end()) {
int i = distance(m_data->m_items.begin(), it);
select(i, on);
}
}
//------------------------------------------------------------------------------
void TTextList::unselectAll()
{
m_data->m_selectedItems.clear();
invalidate();
}
//------------------------------------------------------------------------------
bool TTextList::isSelected(int i) const
{
assert(i >= 0 && i < (int)m_data->m_items.size());
std::map<string, TTextListItem *>::iterator it = m_data->m_items.begin();
advance(it, i);
string id = it->first;
vector<string>::iterator it2 =
find(m_data->m_selectedItems.begin(), m_data->m_selectedItems.end(), id);
return it2 != m_data->m_selectedItems.end();
}
//------------------------------------------------------------------------------
bool TTextList::isSelected(const string &item) const
{
assert(false);
return false;
}
//------------------------------------------------------------------------------
void TTextList::scrollTo(int y)
{
y = (y / rowHeight) * rowHeight;
if (m_data->m_yoffset == y)
return;
m_data->m_yoffset = y;
m_data->updateScrollBarStatus();
invalidate();
}
//------------------------------------------------------------------------------
void TTextList::draw()
{
drawRect(TRect(TPoint(0, 0), getSize()));
TDimension d = getSize();
int y = d.ly - 1 - rowHeight;
for (int i = m_data->m_yoffset / rowHeight; i < getItemCount() && y >= 0; ++i, y -= rowHeight) {
if (isSelected(i)) {
setColor(Blue, 2);
fillRect(2, y - 2, getSize().lx - 1 - 2, y + rowHeight - 1 - 2);
}
setColor(Black);
drawText(TPoint(10, y), getItem(i)->getCaption());
}
}
//------------------------------------------------------------------------------
void TTextList::setSelAction(TGenericTextListAction *action)
{
if (m_data->m_selAction)
delete m_data->m_selAction;
m_data->m_selAction = action;
}
//------------------------------------------------------------------------------
void TTextList::setDblClickAction(TGenericTextListAction *action)
{
if (m_data->m_dblClickAction)
delete m_data->m_dblClickAction;
m_data->m_dblClickAction = action;
}
//------------------------------------------------------------------------------
void TTextList::configureNotify(const TDimension &d)
{
m_data->m_scrollbar->setGeometry(d.lx - 20, 1, d.lx - 2, d.ly - 2);
m_data->updateScrollBarStatus();
}
//------------------------------------------------------------------------------
void TTextList::leftButtonDown(const TMouseEvent &e)
{
int i = m_data->posToItem(e.m_pos);
if (i >= 0 && i < (int)m_data->m_items.size()) {
if (!e.isShiftPressed())
unselectAll();
select(i, !isSelected(i));
}
}
//------------------------------------------------------------------------------
void TTextList::leftButtonDoubleClick(const TMouseEvent &e)
{
int i = m_data->posToItem(e.m_pos);
if (i >= 0 && i < (int)m_data->m_items.size()) {
if (m_data->m_dblClickAction)
m_data->m_dblClickAction->sendCommand(i);
}
}
//------------------------------------------------------------------------------
void TTextList::keyDown(int key, unsigned long mod, const TPoint &pos)
{
if ((key == TK_UpArrow) || (key == TK_DownArrow)) {
int lastSelected = -1;
for (int i = 0; i < getItemCount(); ++i)
if (isSelected(i))
lastSelected = i;
if (lastSelected == -1)
return;
int newSelected = (key == TK_UpArrow) ? lastSelected - 1 : lastSelected + 1;
if (newSelected >= 0 && newSelected < (int)m_data->m_items.size()) {
// if (!e.isShiftPressed())
unselectAll();
select(newSelected, !isSelected(newSelected));
}
} else
TWidget::keyDown(key, mod, pos);
}