~ubuntu-branches/ubuntu/oneiric/blobandconquer/oneiric

« back to all changes in this revision

Viewing changes to src/gui/CListView.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Guus Sliepen
  • Date: 2008-06-15 12:04:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080615120429-5ss7cbb4z9mpywj5
Tags: 0.95-1
New upstream release. Closes: #486310

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (C) 2006 Parallel Realities
3
 
 
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.
8
 
 
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.
12
 
 
13
 
See the GNU General Public License for more details.
14
 
 
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.
18
 
 
19
 
*/
20
 
 
21
 
#include "../headers.h"
22
 
 
23
 
ListView::ListView()
24
 
{
25
 
        widgetType = LISTVIEW;
26
 
 
27
 
        startIndex = 0;
28
 
        selectedIndex = 0;
29
 
        endIndex = 0;
30
 
 
31
 
        clicked = false;
32
 
        itemSelected = false;
33
 
        transparent = false;
34
 
        border = true;
35
 
        
36
 
        numberOfPages = 1;
37
 
        currentPage = 1;
38
 
        buttonState = 0;
39
 
        
40
 
        items.setName("ListView List");
41
 
}
42
 
 
43
 
ListView::~ListView()
44
 
{
45
 
        items.clear();
46
 
}
47
 
 
48
 
void ListView::use(int x, int y)
49
 
{
50
 
}
51
 
 
52
 
void ListView::gotoPage(int pageNumber)
53
 
{
54
 
        startIndex = selectedIndex = 0;
55
 
        
56
 
        for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
57
 
        {
58
 
                if (item->pageNumber == pageNumber)
59
 
                {
60
 
                        return;
61
 
                }
62
 
                
63
 
                startIndex++;
64
 
                selectedIndex++;
65
 
        }
66
 
}
67
 
 
68
 
void ListView::calculateNumberOfPages()
69
 
{
70
 
        int itemHeight = 2;
71
 
        
72
 
        numberOfPages = 1;
73
 
        
74
 
        for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
75
 
        {
76
 
                if (itemHeight + item->texture->ih + 2 > height)
77
 
                {
78
 
                        numberOfPages++;
79
 
                        itemHeight = 2;
80
 
                }
81
 
                
82
 
                item->pageNumber = numberOfPages;
83
 
                
84
 
                itemHeight += (item->texture->ih + 2);
85
 
        }
86
 
}
87
 
 
88
 
void ListView::addItem(ListViewItem *item)
89
 
{
90
 
        items.add(item);
91
 
        
92
 
        itemSelected = true;
93
 
        
94
 
        calculateNumberOfPages();
95
 
}
96
 
 
97
 
void ListView::removeItem(ListViewItem *item)
98
 
{
99
 
        items.remove(item);
100
 
        
101
 
        if (selectedIndex == items.getSize())
102
 
        {
103
 
                selectedIndex--;
104
 
        }
105
 
        
106
 
        calculateNumberOfPages();
107
 
}
108
 
 
109
 
ListViewItem *ListView::getSelectedItem()
110
 
{
111
 
        int i = 0;
112
 
 
113
 
        for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
114
 
        {
115
 
                if (i == selectedIndex)
116
 
                {
117
 
                        return item;
118
 
                }
119
 
 
120
 
                i++;
121
 
        }
122
 
        
123
 
        printf("WARNING: ListView::getSelectedItem - Returning NULL! (%s)\n", getName());
124
 
        
125
 
        // shouldn't get here!
126
 
        return NULL;
127
 
}
128
 
 
129
 
Linkable *ListView::getSelectedObject()
130
 
{
131
 
        int i = 0;
132
 
 
133
 
        for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
134
 
        {
135
 
                if (i == selectedIndex)
136
 
                {
137
 
                        return item->object;
138
 
                }
139
 
 
140
 
                i++;
141
 
        }
142
 
        
143
 
        printf("WARNING: ListView::getSelectedObject - Returning NULL! (%s)\n", getName());
144
 
        
145
 
        // shouldn't get here!
146
 
        return NULL;
147
 
}
148
 
 
149
 
int ListView::getNumberOfPages()
150
 
{
151
 
        return numberOfPages;
152
 
}
153
 
 
154
 
int ListView::getCurrentPage()
155
 
{
156
 
        return currentPage;
157
 
}
158
 
 
159
 
int ListView::getSelectedIndex()
160
 
{
161
 
        return selectedIndex;
162
 
}
163
 
 
164
 
void ListView::setSelectedIndex(int index)
165
 
{
166
 
        selectedIndex = index;
167
 
}
168
 
 
169
 
void ListView::setSelectedObject(Linkable *object)
170
 
{
171
 
        int i = 0;
172
 
 
173
 
        for (Linkable *link = items.getFirstElement() ; link != NULL ; link = link->next)
174
 
        {
175
 
                if (link == object)
176
 
                {
177
 
                        selectedIndex = i;
178
 
                        itemSelected = true;
179
 
                }
180
 
 
181
 
                i++;
182
 
        }
183
 
}
184
 
 
185
 
int ListView::getItemCount()
186
 
{
187
 
        return items.getSize();
188
 
}
189
 
 
190
 
List *ListView::getList()
191
 
{
192
 
        return &items;
193
 
}
194
 
 
195
 
void ListView::setProperties(Properties *properties)
196
 
{
197
 
        setBaseValues(properties);
198
 
        
199
 
        transparent = properties->getInt("transparent", 0);
200
 
        border = properties->getInt("border", 1);
201
 
        
202
 
        Grid *grid = UIManager::getInstance()->getGrid(properties->getString("grid", "unknown"));
203
 
 
204
 
        width = grid->getCellWidth();
205
 
        height = grid->getCellHeight();
206
 
        
207
 
        int colSpan = properties->getInt("colSpan", 1);
208
 
        int rowSpan = properties->getInt("rowSpan", 1);
209
 
        
210
 
        width *= colSpan;
211
 
        height *= rowSpan;
212
 
        
213
 
        width -= (grid->cellSpacing * 2);
214
 
        height -= (grid->cellSpacing * 2);
215
 
}
216
 
 
217
 
bool ListView::itemSelectionChanged()
218
 
{
219
 
        if (itemSelected)
220
 
        {
221
 
                itemSelected = false;
222
 
                return true;
223
 
        }
224
 
        
225
 
        return false;
226
 
}
227
 
 
228
 
void ListView::mousePressed(SDL_MouseButtonEvent mouse)
229
 
{
230
 
        if (mouse.button == SDL_BUTTON_WHEELUP)
231
 
        {
232
 
                Math::limit(&(--currentPage), 1, numberOfPages);
233
 
                gotoPage(currentPage);
234
 
                
235
 
                itemSelected = true;
236
 
        }
237
 
        else if (mouse.button == SDL_BUTTON_WHEELDOWN)
238
 
        {
239
 
                Math::limit(&(++currentPage), 1, numberOfPages);
240
 
                gotoPage(currentPage);
241
 
                
242
 
                itemSelected = true;
243
 
        }
244
 
 
245
 
        int index = 0;
246
 
 
247
 
        for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
248
 
        {
249
 
                if (item->pageNumber == currentPage)
250
 
                {
251
 
                        if (Collision::collision(mouse.x, mouse.y, 1, 1, item->x, item->y, item->texture->iw, item->texture->ih))
252
 
                        {
253
 
                                selectedIndex = index;
254
 
                                itemSelected = true;
255
 
                        }
256
 
                }
257
 
 
258
 
                index++;
259
 
        }
260
 
        
261
 
        buttonState = 1;
262
 
}
263
 
 
264
 
void ListView::keyPressed(int key, bool shiftHeld)
265
 
{
266
 
        if (key == SDLK_HOME)
267
 
        {
268
 
                currentPage = 1;
269
 
                gotoPage(currentPage);
270
 
        }
271
 
        else if (key == SDLK_UP)
272
 
        {
273
 
                Math::limit(&(--selectedIndex), startIndex, endIndex);
274
 
 
275
 
                itemSelected = true;
276
 
        }
277
 
        else if (key == SDLK_DOWN)
278
 
        {
279
 
                Math::limit(&(++selectedIndex), startIndex, endIndex);
280
 
                
281
 
                itemSelected = true;
282
 
        }
283
 
        else if (key == SDLK_PAGEUP)
284
 
        {
285
 
                Math::limit(&(--currentPage), 1, numberOfPages);
286
 
                gotoPage(currentPage);
287
 
                
288
 
                itemSelected = true;
289
 
        }
290
 
        else if (key == SDLK_PAGEDOWN)
291
 
        {
292
 
                Math::limit(&(++currentPage), 1, numberOfPages);
293
 
                gotoPage(currentPage);
294
 
                
295
 
                itemSelected = true;
296
 
        }
297
 
        else if (key == SDLK_END)
298
 
        {
299
 
                currentPage = numberOfPages;
300
 
                gotoPage(currentPage);
301
 
        }
302
 
}
303
 
 
304
 
void ListView::resync()
305
 
{
306
 
        if (getItemCount() > 0)
307
 
        {
308
 
                if (getSelectedIndex() > getItemCount() - 1)
309
 
                {
310
 
                        setSelectedIndex(getItemCount() - 1);
311
 
                }
312
 
        }
313
 
        
314
 
        itemSelected = true;
315
 
}
316
 
 
317
 
void ListView::mouseReleased(SDL_MouseButtonEvent mouse)
318
 
{
319
 
        buttonState = 0;
320
 
}
321
 
 
322
 
void ListView::mouseMoved(int x, int y)
323
 
{
324
 
        if (buttonState == 0)
325
 
        {
326
 
                return;
327
 
        }
328
 
        
329
 
        int index = 0;
330
 
 
331
 
        for (ListViewItem *item = (ListViewItem*)items.getFirstElement() ; item != NULL ; item = (ListViewItem*)item->next)
332
 
        {
333
 
                if (item->pageNumber == currentPage)
334
 
                {
335
 
                        if (Collision::collision(x, y, 1, 1, item->x, item->y, item->texture->iw, item->texture->ih))
336
 
                        {
337
 
                                selectedIndex = index;
338
 
                                itemSelected = true;
339
 
                        }
340
 
                }
341
 
 
342
 
                index++;
343
 
        }
344
 
}