summaryrefslogtreecommitdiff
path: root/src/gui/widgets/label.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-10-14 18:50:27 +0300
committerAndrei Karas <akaras@inbox.ru>2012-10-14 18:50:27 +0300
commit066488a6f4d086b57f4fe32e8799c207552cccb7 (patch)
tree2c73d80030ff0da73fc5f5b4b664a34030a51633 /src/gui/widgets/label.cpp
parent0b3aafe2859d9ea1c4009eed8a87f69c41cbc14b (diff)
downloadmv-066488a6f4d086b57f4fe32e8799c207552cccb7.tar.gz
mv-066488a6f4d086b57f4fe32e8799c207552cccb7.tar.bz2
mv-066488a6f4d086b57f4fe32e8799c207552cccb7.tar.xz
mv-066488a6f4d086b57f4fe32e8799c207552cccb7.zip
Add theming to labels.
New theme file: label.xml Parameters: padding
Diffstat (limited to 'src/gui/widgets/label.cpp')
-rw-r--r--src/gui/widgets/label.cpp69
1 files changed, 64 insertions, 5 deletions
diff --git a/src/gui/widgets/label.cpp b/src/gui/widgets/label.cpp
index e6f603fb2..188da1bf4 100644
--- a/src/gui/widgets/label.cpp
+++ b/src/gui/widgets/label.cpp
@@ -23,21 +23,80 @@
#include "gui/theme.h"
+#include <guichan/font.hpp>
+
#include "debug.h"
+Skin *Label::mSkin = nullptr;
+int Label::mInstances = 0;
+
Label::Label() :
- gcn::Label()
+ gcn::Label(),
+ mPadding(0)
{
- mForegroundColor = Theme::getThemeColor(Theme::LABEL);
+ init();
}
Label::Label(const std::string &caption) :
- gcn::Label(caption)
+ gcn::Label(caption),
+ mPadding(0)
+{
+ init();
+}
+
+Label::~Label()
+{
+ mInstances --;
+ if (mInstances == 0 && Theme::instance())
+ {
+ Theme::instance()->unload(mSkin);
+ }
+}
+
+void Label::init()
{
mForegroundColor = Theme::getThemeColor(Theme::LABEL);
+ if (mInstances == 0)
+ {
+ if (Theme::instance())
+ mSkin = Theme::instance()->load("label.xml", "");
+ mInstances ++;
+ }
+ if (mSkin)
+ mPadding = mSkin->getPadding();
+ else
+ mPadding = 0;
+}
+
+void Label::draw(gcn::Graphics* graphics)
+{
+ int textX;
+ const int textY = getHeight() / 2 - getFont()->getHeight() / 2;
+
+ switch (getAlignment())
+ {
+ case Graphics::LEFT:
+ default:
+ textX = mPadding;
+ break;
+ case Graphics::CENTER:
+ textX = getWidth() / 2;
+ break;
+ case Graphics::RIGHT:
+ if (getWidth() > mPadding)
+ textX = getWidth() - mPadding;
+ else
+ textX = 0;
+ break;
+ }
+
+ graphics->setFont(getFont());
+ graphics->setColor(mForegroundColor);
+ graphics->drawText(getCaption(), textX, textY, getAlignment());
}
-void Label::draw(gcn::Graphics *graphics)
+void Label::adjustSize()
{
- gcn::Label::draw(graphics);
+ setWidth(getFont()->getWidth(getCaption()) + 2 * mPadding);
+ setHeight(getFont()->getHeight() + 2 * mPadding);
}