Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: c72cd591851654a83966b89d7f27201a30f22a46 (plain)
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
/*
   ui/listview.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "ui/listview.h"

namespace ui
{

ListView::ListView(Widget *parent) : Widget(parent)
{
	listview_scroll = 0.0f;
	listview_selecteditem = 0;
	
	set_label("listview");
	set_border(true);
	set_background(true);
	
	listview_scrollbar = new ScrollBar(this);
	listview_scrollbar->set_background(false);
	listview_scrollbar->set_border(false);
}

ListView::~ListView()
{
}

void ListView::set_scroll(float scroll)
{
	listview_scroll = scroll;
	if (listview_scroll < 0)
		listview_scroll = 0;
	
	// calculate maximal scroll size
	float nb_items = 0;
	for (Children::iterator it = children().begin(); it != children().end(); it++) {
		if ((*it) != static_cast<Widget *>(listview_scrollbar)) {
			nb_items += 1.0f;
		}	
	}
	if (nb_items > 0.0f) {
		nb_items -= 1.0f;
	}
	if (listview_scroll > nb_items) {
		listview_scroll = nb_items;
	}
}

void ListView::inc_scroll(float scroll)
{
	set_scroll(listview_scroll + scroll);
}

void ListView::dec_scroll(float scroll)
{
	set_scroll(listview_scroll - scroll);
}
	
void ListView::resize()
{
	listview_scrollbar->set_size(font()->height(), height());
	listview_scrollbar->set_location(width() - listview_scrollbar->width(), 0);
	
	float total_height = 0;
	float visible_height = 0;
	float nb_items = 0;
	
	// reposition all children within the container
	for (Children::iterator it = children().begin(); it != children().end(); it++) {
		if ((*it) != static_cast<Widget *>(listview_scrollbar)) {
			(*it)->set_width(width() - listview_scrollbar->width());
			
			if (nb_items - listview_scroll < 0) {
				// child widget is invisible
				(*it)->hide();
			} else if ((visible_height + (*it)->height()) > height()) {
				// child widget is invisible
				(*it)->hide();
			} else {
				// child widget is visible
				(*it)->show();
				(*it)->set_location(0, visible_height);
				visible_height += (*it)->height();
			}
			
			total_height += (*it)->height();
			nb_items += 1.0f;
			
		}
	}
	
	if (total_height > height()) {
		if (nb_items > 0.0f)
			nb_items -= 1.0f;
		listview_scrollbar->set_range(0.0f, nb_items);
		listview_scrollbar->set_value(listview_scroll);
		listview_scrollbar->show();
	} else {
		for (Children::iterator it = children().begin(); it != children().end(); it++) {
			if ((*it) != static_cast<Widget *>(listview_scrollbar)) {
				(*it)->set_width(width());
			}
		}
		listview_scrollbar->hide();
	}
}

void ListView::deselect()
{
	listview_selecteditem = 0;
}

void ListView::select(ListItem *item)
{
	if (!item || is_child(item)) {
		listview_selecteditem = item;
	}
}

void ListView::clear()
{
	listview_scroll = 0;
	listview_selecteditem = 0;
	remove_children();
	
	listview_scrollbar = new ScrollBar(this);
	listview_scrollbar->set_background(false);
	listview_scrollbar->set_border(false);
	listview_scrollbar->set_range(0.0f, 0.0f);
	listview_scrollbar->set_value(listview_scroll);
}

ListItem *ListView::add_item(const std::string & text)
{
	return new ui::ListItem(this, text.c_str());
}

bool ListView::on_emit(Widget *sender, const Event event, void *data)
{
	if (sender == listview_scrollbar) {
		if (event == Widget::EventScrollBarChanged)  {
			listview_scroll = listview_scrollbar->value();
			resize();
		}
		return true;
		
	} else if ((sender->parent() == this) && (event == Widget::EventListItemClicked)) {
		listview_selecteditem = static_cast<ListItem *>(sender);
		emit(EventListViewChanged);
		return true;
	}
	
	return false;
}

bool ListView::on_mousewheel(const math::Vector2f & direction)
{
	if (direction.y() > 0 )
	{
		dec_scroll(1.0f);
		resize();
		return true;
	} 
	else if (direction.y() < 0) 
	{
		inc_scroll(1.0f);
		resize();

		return true;
	}
	return false;
}

bool compare_listitems_ascending(const Widget *first, const Widget *second)
{
	const ListItem *firstitem = dynamic_cast<const ListItem *>(first);
	const ListItem *seconditem = dynamic_cast<const ListItem *>(second);
	
	if (!firstitem)
		return true;
	else if (!seconditem)
		return false;
	
	if (firstitem->sortkey() < seconditem->sortkey()) {
		return true;
	} else if (firstitem->sortkey() > seconditem->sortkey()) {
		return false;
	} else {
		return (firstitem->sortkey().length() < seconditem->sortkey().length());
	}
}


bool compare_listitems_descending(const Widget *first, const Widget *second)
{
	const ListItem *firstitem = dynamic_cast<const ListItem *>(first);
	const ListItem *seconditem = dynamic_cast<const ListItem *>(second);

	if (!firstitem)
		return true;
	else if (!seconditem)
		return false;
	
	if (firstitem->sortkey() < seconditem->sortkey()) {
		return false;
	} else if (firstitem->sortkey() > seconditem->sortkey()) {
		return true;
	} else {
		return (firstitem->sortkey().length() > seconditem->sortkey().length());
	}
}

void ListView::sort(bool ascending)
{
	if (ascending) {
		children().sort(compare_listitems_ascending);
	} else {
		children().sort(compare_listitems_descending);
	}
}

}