summaryrefslogtreecommitdiff
path: root/src/gui/widgets/listbox.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-10-16 03:22:50 +0300
committerAndrei Karas <akaras@inbox.ru>2012-10-16 03:25:37 +0300
commit82cc576b7896f39bcf71aa85c8c4b3ef786c065b (patch)
tree2207f3bb4a0b1fa896fe183da245287525d65117 /src/gui/widgets/listbox.cpp
parent6bbe09af6faa77df40112e7cf876877ef11ce74b (diff)
downloadmv-82cc576b7896f39bcf71aa85c8c4b3ef786c065b.tar.gz
mv-82cc576b7896f39bcf71aa85c8c4b3ef786c065b.tar.bz2
mv-82cc576b7896f39bcf71aa85c8c4b3ef786c065b.tar.xz
mv-82cc576b7896f39bcf71aa85c8c4b3ef786c065b.zip
Add listbox padding.
New theme file: listbox.xml Theme option: padding
Diffstat (limited to 'src/gui/widgets/listbox.cpp')
-rw-r--r--src/gui/widgets/listbox.cpp46
1 files changed, 39 insertions, 7 deletions
diff --git a/src/gui/widgets/listbox.cpp b/src/gui/widgets/listbox.cpp
index 3592b12da..8e56cfe9f 100644
--- a/src/gui/widgets/listbox.cpp
+++ b/src/gui/widgets/listbox.cpp
@@ -40,6 +40,8 @@
#include "debug.h"
float ListBox::mAlpha = 1.0;
+Skin *ListBox::mSkin = nullptr;
+int ListBox::mInstances = 0;
ListBox::ListBox(gcn::ListModel *const listModel):
gcn::ListBox(listModel),
@@ -48,11 +50,27 @@ ListBox::ListBox(gcn::ListModel *const listModel):
mOldSelected(-1)
{
mForegroundColor = Theme::getThemeColor(Theme::LISTBOX);
+
+ if (mInstances == 0)
+ {
+ if (Theme::instance())
+ mSkin = Theme::instance()->load("listbox.xml", "");
+ }
+ mInstances ++;
+
+ if (mSkin)
+ mPadding = mSkin->getPadding();
+ else
+ mPadding = 0;
+
adjustSize();
}
ListBox::~ListBox()
{
+ mInstances --;
+ if (mInstances == 0 && Theme::instance())
+ Theme::instance()->unload(mSkin);
}
void ListBox::updateAlpha()
@@ -80,8 +98,8 @@ void ListBox::draw(gcn::Graphics *graphics)
// Draw filled rectangle around the selected list element
if (mSelected >= 0)
{
- graphics->fillRectangle(gcn::Rectangle(0, height * mSelected,
- getWidth(), height));
+ graphics->fillRectangle(gcn::Rectangle(mPadding, height * mSelected,
+ getWidth() - 2 * mPadding, height));
}
// Draw the list elements
@@ -89,7 +107,8 @@ void ListBox::draw(gcn::Graphics *graphics)
for (int i = 0, y = 0; i < mListModel->getNumberOfElements();
++i, y += height)
{
- graphics->drawText(mListModel->getElementAt(i), 1, y);
+ graphics->drawText(mListModel->getElementAt(i),
+ mPadding, y + mPadding);
}
}
@@ -149,7 +168,7 @@ void ListBox::mousePressed(gcn::MouseEvent &event)
{
if (mDistributeMousePressed)
{
- gcn::ListBox::mousePressed(event);
+ mousePressed1(event);
}
else
{
@@ -163,7 +182,7 @@ void ListBox::mousePressed(gcn::MouseEvent &event)
if (gui)
gui->resetClickCount();
if (mOldSelected == mSelected)
- gcn::ListBox::mousePressed(event);
+ mousePressed1(event);
else
mouseDragged(event);
mOldSelected = mSelected;
@@ -176,6 +195,16 @@ void ListBox::mousePressed(gcn::MouseEvent &event)
}
}
+void ListBox::mousePressed1(gcn::MouseEvent &mouseEvent)
+{
+ if (mouseEvent.getButton() == gcn::MouseEvent::LEFT)
+ {
+ setSelected(std::max(0, mouseEvent.getY() - mPadding)
+ / getRowHeight());
+ distributeActionEvent();
+ }
+}
+
void ListBox::mouseDragged(gcn::MouseEvent &event)
{
if (event.getButton() != gcn::MouseEvent::LEFT || getRowHeight() == 0)
@@ -183,7 +212,7 @@ void ListBox::mouseDragged(gcn::MouseEvent &event)
// Make list selection update on drag, but guard against negative y
if (getRowHeight())
- setSelected(std::max(0, event.getY()) / getRowHeight());
+ setSelected(std::max(0, event.getY() - mPadding) / getRowHeight());
}
void ListBox::refocus()
@@ -198,7 +227,10 @@ void ListBox::refocus()
void ListBox::adjustSize()
{
if (mListModel)
- setHeight(getRowHeight() * mListModel->getNumberOfElements());
+ {
+ setHeight(getRowHeight() * mListModel->getNumberOfElements()
+ + 2 * mPadding);
+ }
}
void ListBox::logic()