File: listbox_generic.cpp

package info (click to toggle)
clanlib 1.0~svn3827-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,600 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (373 lines) | stat: -rw-r--r-- 9,068 bytes parent folder | download | duplicates (7)
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
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include "precomp.h"

#include <algorithm>

#include "listbox_generic.h"
#include "API/GUI/listbox.h"
#include "API/GUI/listitem.h"
#include "API/GUI/component.h"
#include "API/GUI/stylemanager.h"
#include "API/Core/XML/dom_element.h"
#include "API/Display/keys.h"
#include "API/Core/System/error.h"

const static std::string blank;

inline bool list_item_ptr_less(CL_ListItem const * one, CL_ListItem const * two)
{
	return *one < *two;
}

inline bool list_item_ptr_greater(CL_ListItem const * one, CL_ListItem const * two)
{
	return *two < *one;
}

/////////////////////////////////////////////////////////////////////////////
// Construction:

CL_ListBox_Generic::CL_ListBox_Generic(CL_ListBox *self)
: listbox(self)
{
	scroll_offset = 0;
	multi_selection = false;

	// Create client area (which is the main listbox)
	CL_Component *client_area = new CL_Component(listbox);
	listbox->set_client_area(client_area);

	slots.connect(client_area->sig_mouse_dblclk(), this, &CL_ListBox_Generic::on_mouse_down);
	slots.connect(listbox->sig_set_options(), this, &CL_ListBox_Generic::on_set_options);
	slots.connect(client_area->sig_mouse_down(), this, &CL_ListBox_Generic::on_mouse_down);
	slots.connect(client_area->sig_key_up(), this, &CL_ListBox_Generic::on_key_up);
}

CL_ListBox_Generic::~CL_ListBox_Generic()
{
	std::vector<CL_ListItem *>::iterator it;
	for(it = items.begin(); it != items.end(); ++it)
		if((*it)->delete_item)
			delete (*it);

	delete listbox->get_client_area();
}

/////////////////////////////////////////////////////////////////////////////
// Attributes:

std::vector<std::string> CL_ListBox_Generic::get_selected_items() const
{
	std::vector<std::string> retval;
	std::vector<CL_ListItem *>::const_iterator it;
	
	for(it = items.begin(); it != items.end(); ++it)
		if((*it)->selected)
			retval.push_back((*it)->str);
	
	return retval;
}

const std::string &CL_ListBox_Generic::get_current_text() const
{
	int pos = 0;
	std::vector<CL_ListItem *>::const_iterator it;
	for(it = items.begin(); it != items.end(); ++it, ++pos)
		if((*it)->selected)
			return (*it)->str;

	return blank;
}

CL_ListItem *CL_ListBox_Generic::get_item(int index) const
{
	int pos = 0;
	std::vector<CL_ListItem *>::const_iterator it;
	for(it = items.begin(); it != items.end(); it++, pos++)
		if(pos == index)
			return (*it);

	return 0;
}

int CL_ListBox_Generic::get_item(const CL_Point &pt)
{
	int index = (int)(pt.y / item_height + scroll_offset);

	if(index < 0 || index >= (int)items.size()) return -1;

	//also check the x

	if (!listbox->get_children_rect().is_inside(pt)) return -1;
	return index;
}


const std::string &CL_ListBox_Generic::get_text(int index) const
{
	const CL_ListItem *item = get_item(index);
	if(item)
		return item->str;
	else
		return blank;
}

int CL_ListBox_Generic::get_current_item() const
{
	int pos = 0;
	std::vector<CL_ListItem *>::const_iterator it;
	for(it = items.begin(); it != items.end(); ++it, ++pos)
		if((*it)->selected)
			return pos;

	return -1;
}

/////////////////////////////////////////////////////////////////////////////
// Operations:

int CL_ListBox_Generic::insert_item(CL_ListItem *item, int index, bool delete_item)
{
	item->delete_item = delete_item;

	if(index < 0)
		items.push_back(item);
	else
		throw CL_Error("CL_ListBox::insert_item() using index is not implemented");

	sig_item_added(items.size() - 1);
	return items.size() - 1;
}

int CL_ListBox_Generic::insert_item(const std::string &text, int index)
{
	if(index < 0)
	{
		CL_ListItem *item = new CL_ListItem;
		item->str = text;
		item->delete_item = true;
		items.push_back(item);
	}
	else
		throw CL_Error("CL_ListBox::insert_item() using index is not implemented");

	sig_item_added(items.size() - 1);
	return items.size() - 1;
}

void CL_ListBox_Generic::remove_item(int index)
{
	if(index < 0 || index > (int)items.size())
		return;

	int pos = 0;
	std::vector<CL_ListItem *>::iterator it;
	for (it = items.begin(); it != items.end(); ++it, ++pos)
	{
		if(pos == index)
		{
			if((*it)->delete_item)
				delete (*it);
			items.erase(it);
			break;
		}
	}

	int max_visible_items = listbox->get_client_area()->get_height() / item_height;
	if((int)items.size() - scroll_offset < max_visible_items)
		scroll_offset = 0;

	sig_item_removed(index);
}

void CL_ListBox_Generic::change_item(CL_ListItem *item, int index)
{
	if(index < 0 || index >= (int)items.size())
		return;

	delete items[index];
	items[index] = item;
}

void CL_ListBox_Generic::change_item(const std::string &text, int index)
{
	items[index]->str = text;
}

void CL_ListBox_Generic::set_current_item(int index)
{
	if(index < 0 || index >= (int)items.size())
		return;

	std::vector<CL_ListItem *>::iterator it;
	for(it = items.begin(); it != items.end(); ++it)
		if((*it)->selected)
			(*it)->selected = false;
	items[index]->selected = true;
	
	sig_highlighted(index);
	sig_selection_changed();
}

void CL_ListBox_Generic::clear_selection()
{
	std::vector<CL_ListItem *>::iterator it;
	for(it = items.begin(); it != items.end(); ++it)
		(*it)->selected = false;
}

void CL_ListBox_Generic::sort(bool ascending)
{
	if (ascending)
		std::sort(items.begin(), items.end(), list_item_ptr_less);
	else
		std::sort(items.begin(), items.end(), list_item_ptr_greater);
}

void CL_ListBox_Generic::clear()
{
	std::vector<CL_ListItem *>::iterator it;
	for(it = items.begin(); it != items.end(); ++it)
		if((*it)->delete_item)
			delete (*it);

	items.clear();
	sig_clear();
}

void CL_ListBox_Generic::set_item_height(int new_item_height)
{
	item_height = new_item_height;
}

void CL_ListBox_Generic::set_top_item(int index)
{
	int max_visible_items = listbox->get_client_area()->get_height() / item_height;
	int maximum_offset = items.size() - max_visible_items;

	if (index < 0)
		index = 0;
	if (maximum_offset < 0)
		maximum_offset = 0;
	
	if (index >= maximum_offset)
		scroll_offset = maximum_offset;
	else
		scroll_offset = index;
}

void CL_ListBox_Generic::set_selected(int index, bool select)
{
	if(index < 0 || index >= (int)items.size())
		return;

	items[index]->selected = select;
}

void CL_ListBox_Generic::select_all(bool select)
{
	std::vector<CL_ListItem *>::iterator it;
	for(it = items.begin(); it != items.end(); ++it)
		(*it)->selected = select;
}

void CL_ListBox_Generic::invert_selection()
{
	std::vector<CL_ListItem *>::iterator it;
	for(it = items.begin(); it != items.end(); ++it)
		(*it)->selected = !((*it)->selected);
}

void CL_ListBox_Generic::set_multi_selection(bool enable)
{
	multi_selection = enable;
}

/////////////////////////////////////////////////////////////////////////////
// Callbacks:

void CL_ListBox_Generic::on_set_options(const CL_DomElement &options)
{
	CL_DomNode node = options.get_first_child();
	while (!node.is_null())
	{
		if (node.is_element())
		{
			CL_DomElement element = node.to_element();
			if (element.get_tag_name() == "item")
			{
				CL_ListItem *item = new CL_ListItem;
				item->str = element.get_attribute("value");
				items.push_back(item);
			}
		}
		node = node.get_next_sibling();
	}
}

void CL_ListBox_Generic::on_mouse_down(const CL_InputEvent &key)
{
	if(items.size() == 0)
		return;

	if(key.id == CL_MOUSE_LEFT)
	{
		int index = (int)(key.mouse_pos.y / item_height + scroll_offset);

		if(index < 0 || index >= (int)items.size())
			return;

		if(multi_selection)
		{
			if (key.repeat_count != 2)
			items[index]->selected = !items[index]->selected;
		}
		else
		{
			std::vector<CL_ListItem *>::iterator it;
			for(it = items.begin(); it != items.end(); ++it)
				if((*it)->selected)
					(*it)->selected = false;
			items[index]->selected = true;
		}

		sig_highlighted(index);
		sig_selection_changed();
		if(key.repeat_count == 2)
			sig_activated(get_current_item());
	}
}

void CL_ListBox_Generic::on_key_up(const CL_InputEvent &key)
{
	if (key.id == CL_KEY_SPACE || key.id == CL_KEY_RETURN)
		sig_activated(get_current_item());
}