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

listbox.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 <typeinfo>
00060 
00061 #include "guichan/basiccontainer.hpp"
00062 #include "guichan/widgets/listbox.hpp"
00063 #include "guichan/widgets/scrollarea.hpp"
00064 
00065 namespace gcn
00066 {
00067     ListBox::ListBox()
00068     {    
00069         mSelected = -1;
00070         mListModel = NULL;
00071         setWidth(100);
00072         setFocusable(true);
00073     
00074         addMouseListener(this);
00075         addKeyListener(this);
00076     }
00077 
00078     ListBox::ListBox(ListModel *listModel)
00079     {
00080         mSelected = -1;
00081         setWidth(100);
00082         setListModel(listModel);
00083         setFocusable(true);
00084     
00085         addMouseListener(this);
00086         addKeyListener(this);
00087     }
00088 
00089     void ListBox::draw(Graphics* graphics)
00090     {
00091         graphics->setColor(getBackgroundColor());
00092         graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
00093 
00094         if (mListModel == NULL)      
00095         {
00096             return;
00097         }
00098     
00099         graphics->setColor(getForegroundColor());
00100         graphics->setFont(getFont());    
00101     
00102         int i, fontHeight;
00103         int y = 0;
00104     
00105         fontHeight = getFont()->getHeight();
00106     
00110         for (i = 0; i < mListModel->getNumberOfElements(); ++i)
00111         {      
00112             if (i == mSelected)
00113             {
00114                 graphics->drawRectangle(Rectangle(0, y, getWidth(), fontHeight));
00115             }
00116       
00117             graphics->drawText(mListModel->getElementAt(i), 1, y);      
00118 
00119             y += fontHeight;
00120         }    
00121     }
00122 
00123     void ListBox::drawBorder(Graphics* graphics)
00124     {
00125         Color faceColor = getBaseColor();
00126         Color highlightColor, shadowColor;
00127         int alpha = getBaseColor().a;
00128         int width = getWidth() + getBorderSize() * 2 - 1;
00129         int height = getHeight() + getBorderSize() * 2 - 1;
00130         highlightColor = faceColor + 0x303030;
00131         highlightColor.a = alpha;
00132         shadowColor = faceColor - 0x303030;
00133         shadowColor.a = alpha;
00134         
00135         unsigned int i;
00136         for (i = 0; i < getBorderSize(); ++i)
00137         {
00138             graphics->setColor(shadowColor);
00139             graphics->drawLine(i,i, width - i, i);
00140             graphics->drawLine(i,i + 1, i, height - i - 1);
00141             graphics->setColor(highlightColor);
00142             graphics->drawLine(width - i,i + 1, width - i, height - i); 
00143             graphics->drawLine(i,height - i, width - i - 1, height - i); 
00144         }
00145     }
00146     
00147     void ListBox::logic()
00148     {
00149         adjustSize();    
00150     }
00151 
00152     int ListBox::getSelected()
00153     {
00154         return mSelected;
00155     }
00156 
00157     void ListBox::setSelected(int selected)
00158     {
00159         if (mListModel == NULL)
00160         {
00161             mSelected = -1;
00162         }
00163         else
00164         {
00165             if (selected < 0)
00166             {
00167                 mSelected = -1;
00168             }
00169             else if (selected >= mListModel->getNumberOfElements())
00170             {
00171                 mSelected = mListModel->getNumberOfElements() - 1;
00172             }
00173             else
00174             {
00175                 mSelected = selected;
00176             }
00177 
00178             Widget *par = getParent();
00179             if (par == NULL)
00180             {
00181                 return;
00182             }            
00183             
00184             ScrollArea* scrollArea = dynamic_cast<ScrollArea *>(par);
00185             if (scrollArea != NULL)
00186             {
00187                 Rectangle scroll;
00188                 scroll.y = getFont()->getHeight() * mSelected;
00189                 scroll.height = getFont()->getHeight();
00190                 scrollArea->scrollToRectangle(scroll);
00191             }
00192         }
00193     }
00194 
00195     void ListBox::keyPress(const Key& key)
00196     {    
00197         if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
00198         {
00199             generateAction();
00200         }
00201         else if (key.getValue() == Key::UP)
00202         {      
00203             setSelected(mSelected - 1);
00204 
00205             if (mSelected == -1)
00206             {
00207                 setSelected(0);
00208             }
00209         }
00210         else if (key.getValue() == Key::DOWN)
00211         {
00212             setSelected(mSelected + 1);
00213         }
00214     }
00215 
00216     void ListBox::mousePress(int x, int y, int button)
00217     {
00218         if (button == MouseInput::LEFT && hasMouse())
00219         {
00220             setSelected(y / getFont()->getHeight());
00221             generateAction();
00222         }
00223     }
00224 
00225     void ListBox::setListModel(ListModel *listModel)
00226     {
00227         mSelected = -1;
00228         mListModel = listModel;
00229         adjustSize();    
00230     }
00231   
00232     ListModel* ListBox::getListModel()
00233     {
00234         return mListModel;    
00235     }
00236 
00237     void ListBox::adjustSize()
00238     {
00239         if (mListModel != NULL)
00240         {      
00241             setHeight(getFont()->getHeight() * mListModel->getNumberOfElements());
00242         }    
00243     }  
00244 }

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