/* _______ __ __ __ ______ __ __ _______ __ __
* / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
* / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
* / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
* / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
* /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
* \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
*
* Copyright (c) 2004, 2005, 2006, 2007 Olof Naessén and Per Larsson
*
* Js_./
* Per Larsson a.k.a finalman _RqZ{a<^_aa
* Olof Naessén a.k.a jansem/yakslem _asww7!uY`> )\a//
* _Qhm`] _f "'c 1!5m
* Visit: http://guichan.darkbits.org )Qk
ws?a-?' ._/L #'
* binary forms, with or without )4d[#7r, . ' )d`)[
* modification, are permitted provided _Q-5'5W..j/?' -?!\)cam'
* that the following conditions are met: j<. a J@\
* this list of conditions and the j(]1usetColor(getBackgroundColor());
graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
if (mListModel == NULL)
{
return;
}
graphics->setColor(getForegroundColor());
graphics->setFont(getFont());
int i, fontHeight;
int y = 0;
fontHeight = getFont()->getHeight();
/**
* @todo Check cliprects so we do not have to iterate over elements in the list model
*/
for (i = 0; i < mListModel->getNumberOfElements(); ++i)
{
if (i == mSelected)
{
graphics->drawRectangle(Rectangle(0, y, getWidth(), fontHeight));
}
graphics->drawText(mListModel->getElementAt(i), 1, y);
y += fontHeight;
}
}
void ListBox::drawBorder(Graphics* graphics)
{
Color faceColor = getBaseColor();
Color highlightColor, shadowColor;
int alpha = getBaseColor().a;
int width = getWidth() + getBorderSize() * 2 - 1;
int height = getHeight() + getBorderSize() * 2 - 1;
highlightColor = faceColor + 0x303030;
highlightColor.a = alpha;
shadowColor = faceColor - 0x303030;
shadowColor.a = alpha;
unsigned int i;
for (i = 0; i < getBorderSize(); ++i)
{
graphics->setColor(shadowColor);
graphics->drawLine(i,i, width - i, i);
graphics->drawLine(i,i + 1, i, height - i - 1);
graphics->setColor(highlightColor);
graphics->drawLine(width - i,i + 1, width - i, height - i);
graphics->drawLine(i,height - i, width - i - 1, height - i);
}
}
void ListBox::logic()
{
adjustSize();
}
int ListBox::getSelected()
{
return mSelected;
}
void ListBox::setSelected(int selected)
{
if (mListModel == NULL)
{
mSelected = -1;
}
else
{
if (selected < 0)
{
mSelected = -1;
}
else if (selected >= mListModel->getNumberOfElements())
{
mSelected = mListModel->getNumberOfElements() - 1;
}
else
{
mSelected = selected;
}
Widget *par = getParent();
if (par == NULL)
{
return;
}
Rectangle scroll;
if (mSelected < 0)
{
scroll.y = 0;
}
else
{
scroll.y = getFont()->getHeight() * mSelected;
}
scroll.height = getFont()->getHeight();
par->showWidgetPart(this, scroll);
}
}
void ListBox::keyPressed(KeyEvent& keyEvent)
{
Key key = keyEvent.getKey();
if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
{
generateAction();
keyEvent.consume();
}
else if (key.getValue() == Key::UP)
{
setSelected(mSelected - 1);
if (mSelected == -1)
{
if (isWrappingKeyboardSelection())
{
setSelected(getListModel()->getNumberOfElements() - 1);
}
else
{
setSelected(0);
}
}
keyEvent.consume();
}
else if (key.getValue() == Key::DOWN)
{
if (isWrappingKeyboardSelection()
&& getSelected() == getListModel()->getNumberOfElements() - 1)
{
setSelected(0);
}
else
{
setSelected(getSelected() + 1);
}
keyEvent.consume();
}
}
void ListBox::mousePressed(MouseEvent& mouseEvent)
{
if (mouseEvent.getButton() == MouseEvent::LEFT)
{
setSelected(mouseEvent.getY() / getFont()->getHeight());
generateAction();
}
}
void ListBox::mouseWheelMovedUp(MouseEvent& mouseEvent)
{
if (isFocused())
{
if (getSelected() > 0 )
{
setSelected(getSelected() - 1);
}
mouseEvent.consume();
}
}
void ListBox::mouseWheelMovedDown(MouseEvent& mouseEvent)
{
if (isFocused())
{
setSelected(getSelected() + 1);
mouseEvent.consume();
}
}
void ListBox::mouseDragged(MouseEvent& mouseEvent)
{
mouseEvent.consume();
}
void ListBox::setListModel(ListModel *listModel)
{
mSelected = -1;
mListModel = listModel;
adjustSize();
}
ListModel* ListBox::getListModel()
{
return mListModel;
}
void ListBox::adjustSize()
{
if (mListModel != NULL)
{
setHeight(getFont()->getHeight() * mListModel->getNumberOfElements());
}
}
bool ListBox::isWrappingKeyboardSelection()
{
return mWrappingKeyboardSelection;
}
void ListBox::setWrappingKeyboardSelection(bool wrapping)
{
mWrappingKeyboardSelection = wrapping;
}
}