31 #include "gui/base/gui_font.h"
32 #include "util/base/exception.h"
33 #include "video/image.h"
35 #include "clicklabel.h"
38 ClickLabel::ClickLabel() {
39 mGuiFont =
static_cast<FIFE::GuiFont*
> (getFont());
41 setTextWrapping(
false);
43 addMouseListener(
this);
45 addFocusListener(
this);
49 ClickLabel::ClickLabel(
const std::string& caption) {
50 mGuiFont =
static_cast<FIFE::GuiFont*
> (getFont());
52 setTextWrapping(
false);
55 addMouseListener(
this);
57 addFocusListener(
this);
62 ClickLabel::~ClickLabel() {
65 void ClickLabel::setCaption(
const std::string& caption) {
67 mGuiFont =
static_cast<FIFE::GuiFont*
> (getFont());
71 const std::string& ClickLabel::getCaption()
const {
75 void ClickLabel::setWidth(
int width) {
76 Widget::setWidth(width);
80 void ClickLabel::draw(Graphics* graphics) {
82 if (mGuiFont != static_cast<FIFE::GuiFont*> (getFont())) {
83 mGuiFont =
static_cast<FIFE::GuiFont*
> (getFont());
91 graphics->setColor(getBackgroundColor());
92 graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
94 if( isTextWrapping() ) {
95 mGuiFont->drawMultiLineString(graphics, mWrappedText, textX, textY);
97 mGuiFont->drawMultiLineString(graphics, mCaption, textX, textY);
102 void ClickLabel::setTextWrapping(
bool textWrapping) {
103 bool wrappingEnabled = !mTextWrapping && textWrapping;
104 mTextWrapping = textWrapping;
105 if (wrappingEnabled) {
110 bool ClickLabel::isTextWrapping()
const {
111 return mTextWrapping;
114 void ClickLabel::adjustSize() {
117 if( isTextWrapping() ) {
118 image = mGuiFont->getAsImageMultiline(mWrappedText);
120 image = mGuiFont->getAsImageMultiline(mCaption);
122 setWidth( image->getWidth() );
123 setHeight( image->getHeight() );
127 void ClickLabel::wrapText() {
128 if( isTextWrapping() && mGuiFont ) {
129 mWrappedText = mGuiFont->splitTextToWidth(mCaption,getWidth());
134 void ClickLabel::mousePressed(MouseEvent& mouseEvent)
136 if (mouseEvent.getButton() == MouseEvent::LEFT) {
137 mMousePressed =
true;
138 mouseEvent.consume();
142 void ClickLabel::mouseExited(MouseEvent& mouseEvent)
147 void ClickLabel::mouseEntered(MouseEvent& mouseEvent)
152 void ClickLabel::mouseReleased(MouseEvent& mouseEvent)
154 if (mouseEvent.getButton() == MouseEvent::LEFT && mMousePressed && mHasMouse) {
155 mMousePressed =
false;
156 distributeActionEvent();
157 mouseEvent.consume();
158 }
else if (mouseEvent.getButton() == MouseEvent::LEFT) {
159 mMousePressed =
false;
160 mouseEvent.consume();
164 void ClickLabel::mouseDragged(MouseEvent& mouseEvent)
166 mouseEvent.consume();
169 void ClickLabel::keyPressed(KeyEvent& keyEvent)
171 Key key = keyEvent.getKey();
173 if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) {
179 void ClickLabel::keyReleased(KeyEvent& keyEvent)
181 Key key = keyEvent.getKey();
183 if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyPressed) {
185 distributeActionEvent();
190 void ClickLabel::focusLost(
const Event& event)
192 mMousePressed =
false;