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

radiobutton.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/radiobutton.hpp"
00060 
00061 namespace gcn
00062 {
00063     RadioButton::GroupMap RadioButton::mGroupMap;
00064     
00065     RadioButton::RadioButton()
00066     {
00067         setMarked(false);
00068 
00069         setFocusable(true);
00070         addMouseListener(this);
00071         addKeyListener(this);        
00072     }
00073     
00074     RadioButton::RadioButton(const std::string &caption,
00075                              const std::string &group,
00076                              bool marked)
00077     {
00078         setCaption(caption);
00079         setGroup(group);
00080         setMarked(marked);
00081 
00082         setFocusable(true);
00083         addMouseListener(this);
00084         addKeyListener(this);
00085 
00086         adjustSize();
00087     }
00088       
00089     RadioButton::~RadioButton()
00090     {
00091         // Remove us from the group list
00092         setGroup("");
00093     }
00094     
00095     void RadioButton::draw(Graphics* graphics)
00096     {
00097         drawBox(graphics);
00098         
00099         graphics->setFont(getFont());
00100         graphics->setColor(getForegroundColor());
00101     
00102         int h = getHeight() + getHeight() / 2;
00103         
00104         graphics->drawText(getCaption(), h - 2, 0);
00105         
00106         if (hasFocus())
00107         {        
00108             graphics->drawRectangle(Rectangle(h - 4, 0, getWidth() - h + 3, getHeight()));
00109         }     
00110     }
00111     
00112     void RadioButton::drawBorder(Graphics* graphics)
00113     {
00114         Color faceColor = getBaseColor();
00115         Color highlightColor, shadowColor;
00116         int alpha = getBaseColor().a;
00117         int width = getWidth() + getBorderSize() * 2 - 1;
00118         int height = getHeight() + getBorderSize() * 2 - 1;
00119         highlightColor = faceColor + 0x303030;
00120         highlightColor.a = alpha;
00121         shadowColor = faceColor - 0x303030;
00122         shadowColor.a = alpha;
00123         
00124         unsigned int i;
00125         for (i = 0; i < getBorderSize(); ++i)
00126         {
00127             graphics->setColor(shadowColor);
00128             graphics->drawLine(i,i, width - i, i);
00129             graphics->drawLine(i,i + 1, i, height - i - 1);
00130             graphics->setColor(highlightColor);
00131             graphics->drawLine(width - i,i + 1, width - i, height - i); 
00132             graphics->drawLine(i,height - i, width - i - 1, height - i); 
00133         }
00134     }
00135     
00136     void RadioButton::drawBox(Graphics *graphics)
00137     {
00138         int h;
00139 
00140         if (getHeight()%2 == 0)
00141         {
00142             h = getHeight() - 2;
00143         }
00144         else
00145         {
00146             h = getHeight() - 1;
00147         }
00148 
00149         int alpha = getBaseColor().a;
00150         Color faceColor = getBaseColor();
00151         faceColor.a = alpha;    
00152         Color highlightColor = faceColor + 0x303030;
00153         highlightColor.a = alpha;    
00154         Color shadowColor = faceColor - 0x303030;      
00155         shadowColor.a = alpha;    
00156 
00157         graphics->setColor(getBackgroundColor());
00158 
00159         int i;
00160         int hh = (h + 1) / 2;
00161     
00162         for (i = 1; i <= hh; ++i)
00163         {
00164             graphics->drawLine(hh - i + 1,
00165                                i,
00166                                hh + i - 1,
00167                                i);
00168         }
00169      
00170         for (i = 1; i < hh; ++i)
00171         {
00172             graphics->drawLine(hh - i + 1,
00173                                h - i,
00174                                hh + i - 1,
00175                                h - i);
00176         }
00177      
00178         graphics->setColor(shadowColor);
00179         graphics->drawLine(hh, 0, 0, hh);
00180         graphics->drawLine(hh + 1, 1, h - 1, hh - 1);
00181      
00182         graphics->setColor(highlightColor);
00183         graphics->drawLine(1, hh + 1, hh, h);
00184         graphics->drawLine(hh + 1, h - 1, h, hh);
00185          
00186         graphics->setColor(getForegroundColor());
00187 
00188         int hhh = hh - 3;
00189         if (isMarked())
00190         {
00191             for (i = 0; i < hhh; ++i)
00192             {         
00193                 graphics->drawLine(hh - i, 4 + i, hh + i, 4 + i);
00194             }
00195             for (i = 0; i < hhh; ++i)
00196             {         
00197                 graphics->drawLine(hh - i, h - 4 - i, hh + i, h - 4 -  i);
00198             }
00199 
00200         }
00201     }
00202     
00203     bool RadioButton::isMarked() const
00204     {
00205         return mMarked;
00206     }
00207     
00208     void RadioButton::setMarked(bool marked)
00209     {
00210         if (marked && mGroup != "")
00211         {
00212             GroupIterator iter, iterEnd;
00213             iterEnd = mGroupMap.upper_bound(mGroup);
00214 
00215             for (iter = mGroupMap.lower_bound(mGroup);
00216                  iter != iterEnd;
00217                  iter++)
00218             {
00219                 if (iter->second->isMarked())
00220                 {
00221                     iter->second->setMarked(false);
00222                 }
00223             }             
00224         }
00225 
00226         mMarked = marked;
00227     }
00228 
00229     const std::string &RadioButton::getCaption() const
00230     {
00231         return mCaption;
00232     }
00233 
00234     void RadioButton::setCaption(const std::string caption)
00235     {
00236         mCaption = caption;
00237     }
00238 
00239     void RadioButton::keyPress(const Key& key)
00240     {
00241         if (key.getValue() == Key::ENTER ||
00242             key.getValue() == Key::SPACE)
00243         {
00244             setMarked(true);
00245             generateAction();
00246         }    
00247     }
00248 
00249     void RadioButton::mouseClick(int x, int y, int button, int count)
00250     {
00251         if (button == MouseInput::LEFT)
00252         {
00253             setMarked(true);
00254             generateAction();
00255         }  
00256     }
00257 
00258     void RadioButton::setGroup(const std::string &group)
00259     {
00260         if (mGroup != "")
00261         {
00262             GroupIterator iter, iterEnd;
00263             iterEnd = mGroupMap.upper_bound(mGroup);
00264 
00265             for (iter = mGroupMap.lower_bound(mGroup);
00266                  iter != iterEnd;
00267                  iter++)
00268             {
00269                 if (iter->second == this)
00270                 {
00271                     mGroupMap.erase(iter);
00272                     break;
00273                 }
00274             }             
00275         }
00276 
00277         if (group != "")
00278         {
00279             mGroupMap.insert(
00280                 std::pair<std::string, RadioButton *>(group, this));
00281         }
00282 
00283         mGroup = group;
00284     }
00285 
00286     const std::string &RadioButton::getGroup() const
00287     {
00288         return mGroup;
00289     }
00290 
00291     void RadioButton::adjustSize()
00292     {
00293         int height = getFont()->getHeight();
00294 
00295         setHeight(height);
00296         setWidth(getFont()->getWidth(getCaption()) + height + height/2);
00297     }      
00298 }

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