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

textfield.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/keyinput.hpp"
00060 #include "guichan/mouseinput.hpp"
00061 #include "guichan/widgets/textfield.hpp"
00062 
00063 namespace gcn
00064 {
00065     TextField::TextField()
00066     {
00067         mCaretPosition = 0;
00068         mXScroll = 0;
00069 
00070         setFocusable(true);
00071 
00072         addMouseListener(this);
00073         addKeyListener(this);
00074         adjustHeight();
00075         setBorderSize(1);    
00076     }
00077   
00078     TextField::TextField(const std::string& text)
00079     {
00080         mCaretPosition = 0;
00081         mXScroll = 0;
00082     
00083         mText = text;
00084         adjustSize();
00085         setBorderSize(1);
00086         
00087         setFocusable(true);    
00088   
00089         addMouseListener(this);
00090         addKeyListener(this);    
00091     }
00092 
00093     void TextField::setText(const std::string& text)
00094     {
00095         if(text.size() < mCaretPosition )
00096         {
00097             mCaretPosition = text.size();
00098         }
00099     
00100         mText = text;    
00101     }
00102   
00103     void TextField::draw(Graphics* graphics)
00104     {
00105         Color faceColor = getBackgroundColor();
00106         graphics->setColor(faceColor);
00107         graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
00108     
00109         if (hasFocus())
00110         {      
00111             drawCaret(graphics, getFont()->getWidth(mText.substr(0, mCaretPosition)) - mXScroll);
00112         }
00113     
00114         graphics->setColor(getForegroundColor());
00115         graphics->setFont(getFont());
00116         graphics->drawText(mText, 1 - mXScroll, 1);    
00117     }
00118 
00119     void TextField::drawBorder(Graphics* graphics)
00120     {
00121         Color faceColor = getBaseColor();
00122         Color highlightColor, shadowColor;
00123         int alpha = getBaseColor().a;
00124         int width = getWidth() + getBorderSize() * 2 - 1;
00125         int height = getHeight() + getBorderSize() * 2 - 1;
00126         highlightColor = faceColor + 0x303030;
00127         highlightColor.a = alpha;
00128         shadowColor = faceColor - 0x303030;
00129         shadowColor.a = alpha;
00130         
00131         unsigned int i;
00132         for (i = 0; i < getBorderSize(); ++i)
00133         {
00134             graphics->setColor(shadowColor);
00135             graphics->drawLine(i,i, width - i, i);
00136             graphics->drawLine(i,i + 1, i, height - i - 1);
00137             graphics->setColor(highlightColor);
00138             graphics->drawLine(width - i,i + 1, width - i, height - i); 
00139             graphics->drawLine(i,height - i, width - i - 1, height - i);
00140         }
00141     }
00142     
00143     void TextField::drawCaret(Graphics* graphics, int x)
00144     {
00145         graphics->setColor(getForegroundColor());
00146         graphics->drawLine(x, getHeight() - 2, x, 1);    
00147     }
00148   
00149     void TextField::mousePress(int x, int y, int button)
00150     {
00151         if (hasMouse() && button == MouseInput::LEFT)
00152         {
00153             mCaretPosition = getFont()->getStringIndexAt(mText, x + mXScroll);
00154             fixScroll();
00155         }      
00156     }
00157   
00158     void TextField::keyPress(const Key& key)
00159     {
00160         if (key.getValue() == Key::LEFT && mCaretPosition > 0)
00161         {
00162             --mCaretPosition;
00163         }
00164 
00165         else if (key.getValue() == Key::RIGHT && mCaretPosition < mText.size())
00166         {
00167             ++mCaretPosition;
00168         }
00169 
00170         else if (key.getValue() == Key::DELETE && mCaretPosition < mText.size())
00171         {
00172             mText.erase(mCaretPosition, 1);
00173         }
00174 
00175         else if (key.getValue() == Key::BACKSPACE && mCaretPosition > 0)
00176         {
00177             mText.erase(mCaretPosition - 1, 1);
00178             --mCaretPosition;
00179         }
00180 
00181         else if (key.getValue() == Key::ENTER)
00182         {
00183             generateAction();
00184         }
00185 
00186         else if (key.getValue() == Key::HOME)
00187         {
00188             mCaretPosition = 0;
00189         }    
00190 
00191         else if (key.getValue() == Key::END)
00192         {
00193             mCaretPosition = mText.size();
00194         }    
00195 
00196         else if (key.isCharacter())
00197         {
00198             mText.insert(mCaretPosition, std::string(1,(char)key.getValue()));
00199             ++mCaretPosition;
00200         }
00201 
00202         fixScroll();    
00203     }
00204 
00205     void TextField::adjustSize()
00206     {
00207         setWidth(getFont()->getWidth(mText) + 4);
00208         adjustHeight();
00209 
00210         fixScroll();    
00211     }
00212   
00213     void TextField::adjustHeight()
00214     {
00215         setHeight(getFont()->getHeight() + 2);    
00216     }
00217 
00218     void TextField::fixScroll()
00219     {
00220         if (hasFocus())
00221         {
00222             int caretX = getFont()->getWidth(mText.substr(0, mCaretPosition));
00223 
00224             if (caretX - mXScroll > getWidth() - 4)
00225             {
00226                 mXScroll = caretX - getWidth() + 4;
00227             }
00228             else if (caretX - mXScroll < getFont()->getWidth(" "))
00229             {
00230                 mXScroll = caretX - getFont()->getWidth(" ");
00231         
00232                 if (mXScroll < 0)
00233                 {
00234                     mXScroll = 0;
00235                 }
00236             }
00237         }
00238     }
00239 
00240     void TextField::setCaretPosition(unsigned int position)
00241     {
00242         if (position > mText.size())
00243         {
00244             mCaretPosition = mText.size();
00245         }
00246         else
00247         {    
00248             mCaretPosition = position;
00249         }
00250 
00251         fixScroll();    
00252     }
00253 
00254     unsigned int TextField::getCaretPosition() const
00255     {
00256         return mCaretPosition;    
00257     }
00258 
00259     const std::string& TextField::getText() const
00260     {
00261         return mText;    
00262     }
00263   
00264     void TextField::fontChanged()
00265     {
00266         fixScroll();
00267     }
00268 }

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