Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

button.cpp

00001 /*      _______   __   __   __   ______   __   __   _______   __   __                 
00002  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\                
00003  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /                 
00004  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /                  
00005  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /                   
00006  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /                    
00007  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/                      
00008  *
00009  * Copyright (c) 2004, 2005 darkbits                        Js_./
00010  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
00011  * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
00012  *                                                 _Qhm`] _f "'c  1!5m
00013  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
00014  *                                               .)j(] .d_/ '-(  P .   S
00015  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
00016  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
00017  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
00018  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
00019  * that the following conditions are met:       j<<WP+k/);.        _W=j f
00020  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
00021  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
00022  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
00023  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
00024  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
00025  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
00026  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
00027  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
00028  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
00029  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
00030  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
00031  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
00032  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
00033  *    from this software without specific        (js, \[QQW$QWW#?!V"".
00034  *    prior written permission.                    ]y:.<\..          .
00035  *                                                 -]n w/ '         [.
00036  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
00037  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
00038  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
00039  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
00040  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
00041  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
00042  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
00043  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
00044  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
00045  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
00046  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
00047  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00049  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00050  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00051  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00052  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00053  */
00054 
00055 /*
00056  * For comments regarding functions please see the header file. 
00057  */
00058 
00059 #include "guichan/widgets/button.hpp"
00060 #include "guichan/exception.hpp"
00061 #include "guichan/mouseinput.hpp"
00062 
00063 namespace gcn
00064 {
00065     Button::Button()
00066     {
00067         mAlignment = Graphics::CENTER;
00068         addMouseListener(this);
00069         addKeyListener(this);
00070         adjustSize();
00071         setBorderSize(1);        
00072     }
00073   
00074     Button::Button(const std::string& caption)
00075     {
00076         mCaption = caption;
00077         mAlignment = Graphics::CENTER;
00078         setFocusable(true);
00079         adjustSize();
00080         setBorderSize(1);
00081         
00082         mMouseDown = false;
00083         mKeyDown = false;
00084 
00085         addMouseListener(this);
00086         addKeyListener(this);    
00087     }
00088 
00089     void Button::setCaption(const std::string& caption)
00090     {
00091         mCaption = caption;    
00092     }
00093 
00094     const std::string& Button::getCaption() const
00095     {
00096         return mCaption;        
00097     }
00098 
00099     void Button::setAlignment(unsigned int alignment)
00100     {
00101         mAlignment = alignment;
00102     }
00103 
00104     unsigned int Button::getAlignment() const
00105     {
00106         return mAlignment;
00107     }
00108     
00109     void Button::draw(Graphics* graphics)
00110     {
00111         Color faceColor = getBaseColor();
00112         Color highlightColor, shadowColor;
00113         int alpha = getBaseColor().a;
00114         
00115         if (isPressed())
00116         {
00117             faceColor = faceColor - 0x303030;
00118             faceColor.a = alpha;
00119             highlightColor = faceColor - 0x303030;
00120             highlightColor.a = alpha;
00121             shadowColor = faceColor + 0x303030;      
00122             shadowColor.a = alpha;
00123         }
00124         else
00125         {
00126             highlightColor = faceColor + 0x303030;
00127             highlightColor.a = alpha;
00128             shadowColor = faceColor - 0x303030;
00129             shadowColor.a = alpha;
00130         }
00131 
00132         graphics->setColor(faceColor);
00133         graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
00134     
00135         graphics->setColor(highlightColor);
00136         graphics->drawLine(0, 0, getWidth() - 1, 0);
00137         graphics->drawLine(0, 1, 0, getHeight() - 1);
00138     
00139         graphics->setColor(shadowColor);
00140         graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
00141         graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1);
00142 
00143         graphics->setColor(getForegroundColor());
00144 
00145         int textX;
00146         int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00147         
00148         switch (getAlignment())
00149         {
00150           case Graphics::LEFT:
00151               textX = 4;
00152               break;
00153           case Graphics::CENTER:
00154               textX = getWidth() / 2;
00155               break;
00156           case Graphics::RIGHT:
00157               textX = getWidth() - 4;
00158               break;
00159           default:
00160               throw GCN_EXCEPTION("Unknown alignment.");
00161         }
00162 
00163         graphics->setFont(getFont());
00164         
00165         if (isPressed())
00166         {
00167             graphics->drawText(getCaption(), textX + 1, textY + 1, getAlignment());
00168         }
00169         else
00170         {
00171             graphics->drawText(getCaption(), textX, textY, getAlignment());
00172             
00173             if (hasFocus())
00174             {
00175                 graphics->drawRectangle(Rectangle(2, 2, getWidth() - 4,
00176                                                   getHeight() - 4));
00177             }      
00178         }    
00179     }
00180 
00181     void Button::drawBorder(Graphics* graphics)
00182     {
00183         Color faceColor = getBaseColor();
00184         Color highlightColor, shadowColor;
00185         int alpha = getBaseColor().a;
00186         int width = getWidth() + getBorderSize() * 2 - 1;
00187         int height = getHeight() + getBorderSize() * 2 - 1;
00188         highlightColor = faceColor + 0x303030;
00189         highlightColor.a = alpha;
00190         shadowColor = faceColor - 0x303030;
00191         shadowColor.a = alpha;
00192         
00193         unsigned int i;
00194         for (i = 0; i < getBorderSize(); ++i)
00195         {
00196             graphics->setColor(shadowColor);
00197             graphics->drawLine(i,i, width - i, i);
00198             graphics->drawLine(i,i + 1, i, height - i - 1);
00199             graphics->setColor(highlightColor);
00200             graphics->drawLine(width - i,i + 1, width - i, height - i); 
00201             graphics->drawLine(i,height - i, width - i - 1, height - i); 
00202         }
00203     }
00204     
00205     void Button::adjustSize()
00206     {
00207         setWidth(getFont()->getWidth(mCaption) + 8);
00208         setHeight(getFont()->getHeight() + 8);
00209     }
00210 
00211     bool Button::isPressed() const
00212     {
00213         return (hasMouse() && mMouseDown) || mKeyDown;
00214     }
00215     
00216     void Button::mouseClick(int x, int y, int button, int count)
00217     {
00218         if (button == MouseInput::LEFT)
00219         {
00220             generateAction();
00221         }
00222     }
00223 
00224     void Button::mousePress(int x, int y, int button)
00225     {
00226         if (button == MouseInput::LEFT && hasMouse())
00227         {      
00228             mMouseDown = true;
00229         }
00230     }
00231 
00232     void Button::mouseRelease(int x, int y, int button)
00233     {
00234         if (button == MouseInput::LEFT)
00235         {      
00236             mMouseDown = false;
00237         }
00238     }
00239   
00240     void Button::keyPress(const Key& key)
00241     {
00242         if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
00243         {
00244             mKeyDown = true;
00245         }
00246 
00247         mMouseDown = false;    
00248     }
00249 
00250     void Button::keyRelease(const Key& key)
00251     {
00252         if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyDown)
00253         {
00254             mKeyDown = false;
00255             generateAction();
00256         }
00257     }
00258 
00259     void Button::lostFocus()
00260     {
00261         mMouseDown = false;
00262         mKeyDown = false;
00263     }  
00264 }

Generated on Tue May 17 21:23:26 2005 for Guichan by  doxygen 1.4.1