| 1 |
/* _______ __ __ __ ______ __ __ _______ __ __ |
| 2 |
* / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ |
| 3 |
* / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / |
| 4 |
* / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / |
| 5 |
* / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / |
| 6 |
* /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / |
| 7 |
* \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ |
| 8 |
* |
| 9 |
* Copyright (c) 2004, 2005 darkbits Js_./ |
| 10 |
* Per Larsson a.k.a finalman _RqZ{a<^_aa |
| 11 |
* Olof Naess� a.k.a jansem/yakslem _asww7!uY`> )\a// |
| 12 |
* _Qhm`] _f "'c 1!5m |
| 13 |
* Visit: http://guichan.darkbits.org )Qk<P ` _: :+' .' "{[ |
| 14 |
* .)j(] .d_/ '-( P . S |
| 15 |
* License: (BSD) <Td/Z <fP"5(\"??"\a. .L |
| 16 |
* Redistribution and use in source and _dV>ws?a-?' ._/L #' |
| 17 |
* binary forms, with or without )4d[#7r, . ' )d`)[ |
| 18 |
* modification, are permitted provided _Q-5'5W..j/?' -?!\)cam' |
| 19 |
* that the following conditions are met: j<<WP+k/);. _W=j f |
| 20 |
* 1. Redistributions of source code must .$%w\/]Q . ."' . mj$ |
| 21 |
* retain the above copyright notice, ]E.pYY(Q]>. a J@\ |
| 22 |
* this list of conditions and the j(]1u<sE"L,. . ./^ ]{a |
| 23 |
* following disclaimer. 4'_uomm\. )L);-4 (3= |
| 24 |
* 2. Redistributions in binary form must )_]X{Z('a_"a7'<a"a, ]"[ |
| 25 |
* reproduce the above copyright notice, #}<]m7`Za??4,P-"'7. ).m |
| 26 |
* this list of conditions and the ]d2e)Q(<Q( ?94 b- LQ/ |
| 27 |
* following disclaimer in the <B!</]C)d_, '(<' .f. =C+m |
| 28 |
* documentation and/or other materials .Z!=J ]e []('-4f _ ) -.)m]' |
| 29 |
* provided with the distribution. .w[5]' _[ /.)_-"+? _/ <W" |
| 30 |
* 3. Neither the name of Guichan nor the :$we` _! + _/ . j? |
| 31 |
* names of its contributors may be used =3)= _f (_yQmWW$#( " |
| 32 |
* to endorse or promote products derived - W, sQQQQmZQ#Wwa].. |
| 33 |
* from this software without specific (js, \[QQW$QWW#?!V"". |
| 34 |
* prior written permission. ]y:.<\.. . |
| 35 |
* -]n w/ ' [. |
| 36 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT )/ )/ ! |
| 37 |
* HOLDERS AND CONTRIBUTORS "AS IS" AND ANY < (; sac , ' |
| 38 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, ]^ .- % |
| 39 |
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF c < r |
| 40 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR aga< <La |
| 41 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 5% )P'-3L |
| 42 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR _bQf` y`..)a |
| 43 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ,J?4P'.P"_(\?d'., |
| 44 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES _Pa,)!f/<[]/ ?" |
| 45 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT _2-..:. .r+_,.. . |
| 46 |
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ?a.<%"' " -'.a_ _, |
| 47 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ^ |
| 48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 49 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 50 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 51 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 52 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 53 |
*/ |
| 54 |
|
| 55 |
/* |
| 56 |
* For comments regarding functions please see the header file. |
| 57 |
*/ |
| 58 |
|
| 59 |
#include <typeinfo> |
| 60 |
|
| 61 |
#include "guichan/basiccontainer.h" |
| 62 |
#include "guichan/widgets/listbox.h" |
| 63 |
#include "guichan/widgets/scrollarea.h" |
| 64 |
|
| 65 |
|
| 66 |
static const std::string selectEventId = "select"; |
| 67 |
|
| 68 |
namespace gcn |
| 69 |
{ |
| 70 |
ListBox::ListBox() |
| 71 |
{ |
| 72 |
mSelected = -1; |
| 73 |
mListModel = NULL; |
| 74 |
setWidth(100); |
| 75 |
setFocusable(true); |
| 76 |
|
| 77 |
addMouseListener(this); |
| 78 |
addKeyListener(this); |
| 79 |
} |
| 80 |
|
| 81 |
ListBox::ListBox(ListModel *listModel) |
| 82 |
{ |
| 83 |
mSelected = -1; |
| 84 |
setWidth(100); |
| 85 |
setListModel(listModel); |
| 86 |
setFocusable(true); |
| 87 |
|
| 88 |
addMouseListener(this); |
| 89 |
addKeyListener(this); |
| 90 |
} |
| 91 |
|
| 92 |
void ListBox::draw(Graphics* graphics) |
| 93 |
{ |
| 94 |
if (mListModel == NULL) |
| 95 |
{ |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
graphics->setColor(getForegroundColor()); |
| 100 |
graphics->setFont(getFont()); |
| 101 |
|
| 102 |
int i, fontHeight; |
| 103 |
int y = 0; |
| 104 |
|
| 105 |
fontHeight = getFont()->getHeight(); |
| 106 |
|
| 107 |
/** |
| 108 |
* @todo Check cliprects so we do not have to iterate over elements in the list model |
| 109 |
*/ |
| 110 |
for (i = 0; i < mListModel->getNumberOfElements(); ++i) |
| 111 |
{ |
| 112 |
if (i == mSelected) |
| 113 |
{ |
| 114 |
graphics->drawRectangle(Rectangle(0, y, getWidth(), fontHeight)); |
| 115 |
graphics->setColor(Color(40, 60, 120)); |
| 116 |
graphics->fillRectangle(Rectangle(1, y + 1, getWidth() - 2, fontHeight - 2)); |
| 117 |
} |
| 118 |
|
| 119 |
graphics->drawText(mListModel->getElementAt(i), 1, y); |
| 120 |
|
| 121 |
y += fontHeight; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void ListBox::drawBorder(Graphics* graphics) |
| 126 |
{ |
| 127 |
Color faceColor = getBaseColor(); |
| 128 |
Color highlightColor, shadowColor; |
| 129 |
int alpha = getBaseColor().a; |
| 130 |
int width = getWidth() + getBorderSize() * 2 - 1; |
| 131 |
int height = getHeight() + getBorderSize() * 2 - 1; |
| 132 |
highlightColor = faceColor + 0x303030; |
| 133 |
highlightColor.a = alpha; |
| 134 |
shadowColor = faceColor - 0x303030; |
| 135 |
shadowColor.a = alpha; |
| 136 |
|
| 137 |
unsigned int i; |
| 138 |
for (i = 0; i < getBorderSize(); ++i) |
| 139 |
{ |
| 140 |
graphics->setColor(shadowColor); |
| 141 |
graphics->drawLine(i,i, width - i, i); |
| 142 |
graphics->drawLine(i,i + 1, i, height - i - 1); |
| 143 |
graphics->setColor(highlightColor); |
| 144 |
graphics->drawLine(width - i,i + 1, width - i, height - i); |
| 145 |
graphics->drawLine(i,height - i, width - i - 1, height - i); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void ListBox::logic() |
| 150 |
{ |
| 151 |
adjustSize(); |
| 152 |
} |
| 153 |
|
| 154 |
int ListBox::getSelected() |
| 155 |
{ |
| 156 |
return mSelected; |
| 157 |
} |
| 158 |
|
| 159 |
void ListBox::setSelected(int selected) |
| 160 |
{ |
| 161 |
if (mListModel == NULL) |
| 162 |
{ |
| 163 |
mSelected = -1; |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
if (selected < 0) |
| 168 |
{ |
| 169 |
mSelected = -1; |
| 170 |
} |
| 171 |
else if (selected >= mListModel->getNumberOfElements()) |
| 172 |
{ |
| 173 |
mSelected = mListModel->getNumberOfElements() - 1; |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
mSelected = selected; |
| 178 |
} |
| 179 |
|
| 180 |
Widget *par = getParent(); |
| 181 |
if (par == NULL) |
| 182 |
{ |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
ScrollArea* scrollArea = dynamic_cast<ScrollArea *>(par); |
| 187 |
if (scrollArea != NULL) |
| 188 |
{ |
| 189 |
Rectangle scroll; |
| 190 |
scroll.y = getFont()->getHeight() * mSelected; |
| 191 |
scroll.height = getFont()->getHeight(); |
| 192 |
scrollArea->scrollToRectangle(scroll); |
| 193 |
} |
| 194 |
} |
| 195 |
setDirty(true); |
| 196 |
} |
| 197 |
|
| 198 |
bool ListBox::keyPress(const Key& key) |
| 199 |
{ |
| 200 |
bool ret = false; |
| 201 |
|
| 202 |
if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) |
| 203 |
{ |
| 204 |
generateAction(); |
| 205 |
ret = true; |
| 206 |
} |
| 207 |
else if (key.getValue() == Key::UP) |
| 208 |
{ |
| 209 |
selectNextPrevElement(-1); |
| 210 |
ret = true; |
| 211 |
} |
| 212 |
else if (key.getValue() == Key::DOWN) |
| 213 |
{ |
| 214 |
selectNextPrevElement(+1); |
| 215 |
ret = true; |
| 216 |
} |
| 217 |
else if (key.getValue() == Key::PAGE_UP) |
| 218 |
{ |
| 219 |
ret = selectNextPrevPage(-1); |
| 220 |
} |
| 221 |
else if (key.getValue() == Key::PAGE_DOWN) |
| 222 |
{ |
| 223 |
ret = selectNextPrevPage(+1); |
| 224 |
} |
| 225 |
|
| 226 |
return ret; |
| 227 |
} |
| 228 |
|
| 229 |
bool ListBox::selectNextPrevPage(int sign) |
| 230 |
{ |
| 231 |
BasicContainer *parent = getParent(); |
| 232 |
if (parent == NULL) |
| 233 |
{ |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
int drawWidth = 0, drawHeight = 0; |
| 238 |
parent->getDrawSize(drawWidth, drawHeight, this); |
| 239 |
int pageSize = std::max(drawHeight / getFont()->getHeight(), 1); |
| 240 |
selectNextPrevElement(sign * pageSize); |
| 241 |
return true; |
| 242 |
} |
| 243 |
|
| 244 |
void ListBox::selectNextPrevElement(int sign) |
| 245 |
{ |
| 246 |
int index = mSelected + sign; |
| 247 |
if (index < 0) |
| 248 |
{ |
| 249 |
index = 0; |
| 250 |
} |
| 251 |
setSelected(index); |
| 252 |
generateAction(selectEventId); |
| 253 |
} |
| 254 |
|
| 255 |
void ListBox::mousePress(int x, int y, int button) |
| 256 |
{ |
| 257 |
if (button == MouseInput::LEFT && hasMouse()) |
| 258 |
{ |
| 259 |
setSelected(y / getFont()->getHeight()); |
| 260 |
generateAction(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
void ListBox::setListModel(ListModel *listModel) |
| 265 |
{ |
| 266 |
mSelected = -1; |
| 267 |
mListModel = listModel; |
| 268 |
adjustSize(); |
| 269 |
} |
| 270 |
|
| 271 |
ListModel* ListBox::getListModel() |
| 272 |
{ |
| 273 |
return mListModel; |
| 274 |
} |
| 275 |
|
| 276 |
void ListBox::adjustSize() |
| 277 |
{ |
| 278 |
if (mListModel != NULL) |
| 279 |
{ |
| 280 |
setHeight(getFont()->getHeight() * mListModel->getNumberOfElements()); |
| 281 |
} |
| 282 |
} |
| 283 |
} |