listbox.cpp

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

Generated on Sat Jul 29 19:38:48 2006 for Guichan by  doxygen 1.4.7