FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
clicklabel.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cassert>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "gui/base/gui_font.h"
32 #include "util/base/exception.h"
33 #include "video/image.h"
34 
35 #include "clicklabel.h"
36 
37 namespace gcn {
38  ClickLabel::ClickLabel() {
39  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
40 // setAlignment(Graphics::LEFT);
41  setTextWrapping(false);
42  setFrameSize(0);
43  addMouseListener(this);
44  addKeyListener(this);
45  addFocusListener(this);
46 
47  }
48 
49  ClickLabel::ClickLabel(const std::string& caption) {
50  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
51 // setAlignment(Graphics::LEFT);
52  setTextWrapping(false);
53  setCaption(caption);
54  setFrameSize(0);
55  addMouseListener(this);
56  addKeyListener(this);
57  addFocusListener(this);
58 
59  wrapText();
60  }
61 
62  ClickLabel::~ClickLabel() {
63  }
64 
65  void ClickLabel::setCaption(const std::string& caption) {
66  mCaption = caption;
67  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
68  wrapText();
69  }
70 
71  const std::string& ClickLabel::getCaption() const {
72  return mCaption;
73  }
74 
75  void ClickLabel::setWidth(int width) {
76  Widget::setWidth(width);
77  wrapText();
78  }
79 
80  void ClickLabel::draw(Graphics* graphics) {
81 
82  if (mGuiFont != static_cast<FIFE::GuiFont*> (getFont())) {
83  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
84  wrapText();
85  adjustSize();
86  }
87 
88  int textX = 0;
89  int textY = 0;
90 
91  graphics->setColor(getBackgroundColor());
92  graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
93  if (mGuiFont) {
94  if( isTextWrapping() ) {
95  mGuiFont->drawMultiLineString(graphics, mWrappedText, textX, textY);
96  } else {
97  mGuiFont->drawMultiLineString(graphics, mCaption, textX, textY);
98  }
99  }
100  }
101 
102  void ClickLabel::setTextWrapping(bool textWrapping) {
103  bool wrappingEnabled = !mTextWrapping && textWrapping;
104  mTextWrapping = textWrapping;
105  if (wrappingEnabled) {
106  wrapText();
107  }
108  }
109 
110  bool ClickLabel::isTextWrapping() const {
111  return mTextWrapping;
112  }
113 
114  void ClickLabel::adjustSize() {
115  if (mGuiFont) {
116  FIFE::Image* image;
117  if( isTextWrapping() ) {
118  image = mGuiFont->getAsImageMultiline(mWrappedText);
119  } else {
120  image = mGuiFont->getAsImageMultiline(mCaption);
121  }
122  setWidth( image->getWidth() );
123  setHeight( image->getHeight() );
124  }
125  }
126 
127  void ClickLabel::wrapText() {
128  if( isTextWrapping() && mGuiFont ) {
129  mWrappedText = mGuiFont->splitTextToWidth(mCaption,getWidth());
130  }
131  }
132 
133 
134  void ClickLabel::mousePressed(MouseEvent& mouseEvent)
135  {
136  if (mouseEvent.getButton() == MouseEvent::LEFT) {
137  mMousePressed = true;
138  mouseEvent.consume();
139  }
140  }
141 
142  void ClickLabel::mouseExited(MouseEvent& mouseEvent)
143  {
144  mHasMouse = false;
145  }
146 
147  void ClickLabel::mouseEntered(MouseEvent& mouseEvent)
148  {
149  mHasMouse = true;
150  }
151 
152  void ClickLabel::mouseReleased(MouseEvent& mouseEvent)
153  {
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();
161  }
162  }
163 
164  void ClickLabel::mouseDragged(MouseEvent& mouseEvent)
165  {
166  mouseEvent.consume();
167  }
168 
169  void ClickLabel::keyPressed(KeyEvent& keyEvent)
170  {
171  Key key = keyEvent.getKey();
172 
173  if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) {
174  mKeyPressed = true;
175  keyEvent.consume();
176  }
177  }
178 
179  void ClickLabel::keyReleased(KeyEvent& keyEvent)
180  {
181  Key key = keyEvent.getKey();
182 
183  if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyPressed) {
184  mKeyPressed = false;
185  distributeActionEvent();
186  keyEvent.consume();
187  }
188  }
189 
190  void ClickLabel::focusLost(const Event& event)
191  {
192  mMousePressed = false;
193  mKeyPressed = false;
194  }
195 }