/* _______ __ __ __ ______ __ __ _______ __ __
* / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
* / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
* / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
* / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
* /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
* \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
*
* Copyright (c) 2004, 2005 darkbits Js_./
* Per Larsson a.k.a finalman _RqZ{a<^_aa
* Olof Naess� 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(]1u
#include "guichan/basiccontainer.h"
#include "guichan/widgets/listbox.h"
#include "guichan/widgets/scrollarea.h"
namespace gcn
{
ListBox::ListBox()
{
mSelected = -1;
mListModel = NULL;
setWidth(100);
setFocusable(true);
addMouseListener(this);
addKeyListener(this);
}
ListBox::ListBox(ListModel *listModel)
{
mSelected = -1;
setWidth(100);
setListModel(listModel);
setFocusable(true);
addMouseListener(this);
addKeyListener(this);
}
void ListBox::draw(Graphics* graphics)
{
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->setColor(Color(40, 60, 120));
graphics->fillRectangle(Rectangle(1, y + 1, getWidth() - 2, fontHeight - 2));
}
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;
}
ScrollArea* scrollArea = dynamic_cast(par);
if (scrollArea != NULL)
{
Rectangle scroll;
scroll.y = getFont()->getHeight() * mSelected;
scroll.height = getFont()->getHeight();
scrollArea->scrollToRectangle(scroll);
}
}
}
bool ListBox::keyPress(const Key& key)
{
bool ret = false;
if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
{
generateAction();
ret = true;
}
else if (key.getValue() == Key::UP)
{
setSelected(mSelected - 1);
if (mSelected == -1)
{
setSelected(0);
}
ret = true;
}
else if (key.getValue() == Key::DOWN)
{
setSelected(mSelected + 1);
ret = true;
}
return ret;
}
void ListBox::mousePress(int x, int y, int button)
{
if (button == MouseInput::LEFT && hasMouse())
{
setSelected(y / getFont()->getHeight());
generateAction();
}
}
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());
}
}
}