clicklabel.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <cassert>
00024 
00025 // 3rd party library includes
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "gui/base/gui_font.h" 
00032 #include "util/base/exception.h"
00033 #include "video/image.h" 
00034 
00035 #include "clicklabel.h"
00036 
00037 namespace gcn {
00038     ClickLabel::ClickLabel()  {
00039         mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
00040 //      setAlignment(Graphics::LEFT);
00041         setTextWrapping(false);
00042         setFrameSize(0);
00043         addMouseListener(this);
00044         addKeyListener(this);
00045         addFocusListener(this);
00046 
00047     }
00048     
00049     ClickLabel::ClickLabel(const std::string& caption) {
00050         mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
00051 //      setAlignment(Graphics::LEFT);
00052         setTextWrapping(false);
00053         setCaption(caption);
00054         setFrameSize(0);
00055         addMouseListener(this);
00056         addKeyListener(this);
00057         addFocusListener(this);
00058 
00059         wrapText();
00060     }
00061     
00062     ClickLabel::~ClickLabel() {
00063     }
00064     
00065     void ClickLabel::setCaption(const std::string& caption) {
00066         mCaption = caption;
00067         mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
00068         wrapText();
00069     }
00070 
00071     const std::string& ClickLabel::getCaption() const {
00072         return mCaption;
00073     }
00074 
00075     void ClickLabel::setWidth(int width) {
00076         Widget::setWidth(width);
00077         wrapText();
00078     }
00079     
00080     void ClickLabel::draw(Graphics* graphics) {
00081 
00082         if (mGuiFont != static_cast<FIFE::GuiFont*> (getFont())) {
00083             mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
00084             wrapText();
00085             adjustSize();
00086         }
00087 
00088         int textX = 0;
00089         int textY = 0;
00090 
00091         graphics->setColor(getBackgroundColor());
00092         graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
00093         if (mGuiFont) {
00094             if( isTextWrapping() ) {
00095                 mGuiFont->drawMultiLineString(graphics, mWrappedText, textX, textY);
00096             } else {
00097                 mGuiFont->drawMultiLineString(graphics, mCaption, textX, textY);
00098             }
00099         }
00100     }
00101 
00102     void ClickLabel::setTextWrapping(bool textWrapping) {
00103         bool wrappingEnabled = !mTextWrapping && textWrapping;
00104         mTextWrapping = textWrapping;
00105         if (wrappingEnabled) {
00106             wrapText();
00107         }
00108     }
00109 
00110     bool ClickLabel::isTextWrapping() const {
00111         return mTextWrapping;
00112     }
00113 
00114     void  ClickLabel::adjustSize() {
00115         if (mGuiFont) {
00116             FIFE::Image* image;
00117             if( isTextWrapping() ) {
00118                 image = mGuiFont->getAsImageMultiline(mWrappedText);
00119             } else {
00120                 image = mGuiFont->getAsImageMultiline(mCaption);
00121             }
00122             setWidth( image->getWidth() );
00123             setHeight( image->getHeight() );
00124         }
00125     }
00126 
00127     void ClickLabel::wrapText() {
00128         if( isTextWrapping() && mGuiFont ) {
00129             mWrappedText = mGuiFont->splitTextToWidth(mCaption,getWidth());
00130         }
00131     }
00132 
00133 
00134     void ClickLabel::mousePressed(MouseEvent& mouseEvent)
00135     {
00136         if (mouseEvent.getButton() == MouseEvent::LEFT) {
00137             mMousePressed = true;
00138             mouseEvent.consume();
00139         }
00140     }
00141     
00142     void ClickLabel::mouseExited(MouseEvent& mouseEvent)
00143     {
00144         mHasMouse = false;
00145     }
00146     
00147     void ClickLabel::mouseEntered(MouseEvent& mouseEvent)
00148     {
00149         mHasMouse = true;
00150     }
00151     
00152     void ClickLabel::mouseReleased(MouseEvent& mouseEvent)
00153     {
00154         if (mouseEvent.getButton() == MouseEvent::LEFT && mMousePressed && mHasMouse) {
00155             mMousePressed = false;
00156             distributeActionEvent();
00157             mouseEvent.consume();
00158         } else if (mouseEvent.getButton() == MouseEvent::LEFT) {
00159             mMousePressed = false;
00160             mouseEvent.consume();
00161         }
00162     }
00163     
00164     void ClickLabel::mouseDragged(MouseEvent& mouseEvent)
00165     {
00166         mouseEvent.consume();
00167     }
00168     
00169     void ClickLabel::keyPressed(KeyEvent& keyEvent)
00170     {
00171         Key key = keyEvent.getKey();
00172     
00173         if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) {
00174             mKeyPressed = true;
00175             keyEvent.consume();
00176         }
00177     }
00178     
00179     void ClickLabel::keyReleased(KeyEvent& keyEvent)
00180     {
00181         Key key = keyEvent.getKey();
00182     
00183         if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyPressed) {
00184             mKeyPressed = false;
00185             distributeActionEvent();
00186             keyEvent.consume();
00187         }
00188     }
00189     
00190     void ClickLabel::focusLost(const Event& event)
00191     {
00192         mMousePressed = false;
00193         mKeyPressed = false;
00194     }
00195 }
Generated by  doxygen 1.6.2-20100208