2
Copyright (C) 2006 Parallel Realities
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
as published by the Free Software Foundation; either version 2
7
of the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
See the GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include "../headers.h"
25
widgetType = LISTVIEW;
40
items.setName("ListView List");
48
void ListView::use(int x, int y)
52
void ListView::gotoPage(int pageNumber)
54
startIndex = selectedIndex = 0;
56
for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
58
if (item->pageNumber == pageNumber)
68
void ListView::calculateNumberOfPages()
74
for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
76
if (itemHeight + item->texture->ih + 2 > height)
82
item->pageNumber = numberOfPages;
84
itemHeight += (item->texture->ih + 2);
88
void ListView::addItem(ListViewItem *item)
94
calculateNumberOfPages();
97
void ListView::removeItem(ListViewItem *item)
101
if (selectedIndex == items.getSize())
106
calculateNumberOfPages();
109
ListViewItem *ListView::getSelectedItem()
113
for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
115
if (i == selectedIndex)
123
printf("WARNING: ListView::getSelectedItem - Returning NULL! (%s)\n", getName());
125
// shouldn't get here!
129
Linkable *ListView::getSelectedObject()
133
for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
135
if (i == selectedIndex)
143
printf("WARNING: ListView::getSelectedObject - Returning NULL! (%s)\n", getName());
145
// shouldn't get here!
149
int ListView::getNumberOfPages()
151
return numberOfPages;
154
int ListView::getCurrentPage()
159
int ListView::getSelectedIndex()
161
return selectedIndex;
164
void ListView::setSelectedIndex(int index)
166
selectedIndex = index;
169
void ListView::setSelectedObject(Linkable *object)
173
for (Linkable *link = items.getFirstElement() ; link != NULL ; link = link->next)
185
int ListView::getItemCount()
187
return items.getSize();
190
List *ListView::getList()
195
void ListView::setProperties(Properties *properties)
197
setBaseValues(properties);
199
transparent = properties->getInt("transparent", 0);
200
border = properties->getInt("border", 1);
202
Grid *grid = UIManager::getInstance()->getGrid(properties->getString("grid", "unknown"));
204
width = grid->getCellWidth();
205
height = grid->getCellHeight();
207
int colSpan = properties->getInt("colSpan", 1);
208
int rowSpan = properties->getInt("rowSpan", 1);
213
width -= (grid->cellSpacing * 2);
214
height -= (grid->cellSpacing * 2);
217
bool ListView::itemSelectionChanged()
221
itemSelected = false;
228
void ListView::mousePressed(SDL_MouseButtonEvent mouse)
230
if (mouse.button == SDL_BUTTON_WHEELUP)
232
Math::limit(&(--currentPage), 1, numberOfPages);
233
gotoPage(currentPage);
237
else if (mouse.button == SDL_BUTTON_WHEELDOWN)
239
Math::limit(&(++currentPage), 1, numberOfPages);
240
gotoPage(currentPage);
247
for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
249
if (item->pageNumber == currentPage)
251
if (Collision::collision(mouse.x, mouse.y, 1, 1, item->x, item->y, item->texture->iw, item->texture->ih))
253
selectedIndex = index;
264
void ListView::keyPressed(int key, bool shiftHeld)
266
if (key == SDLK_HOME)
269
gotoPage(currentPage);
271
else if (key == SDLK_UP)
273
Math::limit(&(--selectedIndex), startIndex, endIndex);
277
else if (key == SDLK_DOWN)
279
Math::limit(&(++selectedIndex), startIndex, endIndex);
283
else if (key == SDLK_PAGEUP)
285
Math::limit(&(--currentPage), 1, numberOfPages);
286
gotoPage(currentPage);
290
else if (key == SDLK_PAGEDOWN)
292
Math::limit(&(++currentPage), 1, numberOfPages);
293
gotoPage(currentPage);
297
else if (key == SDLK_END)
299
currentPage = numberOfPages;
300
gotoPage(currentPage);
304
void ListView::resync()
306
if (getItemCount() > 0)
308
if (getSelectedIndex() > getItemCount() - 1)
310
setSelectedIndex(getItemCount() - 1);
317
void ListView::mouseReleased(SDL_MouseButtonEvent mouse)
322
void ListView::mouseMoved(int x, int y)
324
if (buttonState == 0)
331
for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
333
if (item->pageNumber == currentPage)
335
if (Collision::collision(x, y, 1, 1, item->x, item->y, item->texture->iw, item->texture->ih))
337
selectedIndex = index;