window.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/window.hpp"
00062 
00063 #include "guichan/exception.hpp"
00064 #include "guichan/font.hpp"
00065 #include "guichan/graphics.hpp"
00066 #include "guichan/mouseinput.hpp"
00067 
00068 namespace gcn
00069 {
00070     Window::Window()
00071     {
00072         mMouseDrag = false;
00073         setBorderSize(1);
00074         setPadding(2);
00075         setTitleBarHeight(16);
00076         setAlignment(Graphics::CENTER);
00077         addMouseListener(this);
00078         setMovable(true);
00079         setOpaque(true);
00080     }
00081 
00082     Window::Window(const std::string& caption)
00083     {
00084         mMouseDrag = false;
00085         setCaption(caption);
00086         setBorderSize(1);
00087         setPadding(2);
00088         setTitleBarHeight(16);
00089         setAlignment(Graphics::CENTER);
00090         addMouseListener(this);
00091         setMovable(true);
00092         setOpaque(true);
00093     }
00094 
00095     Window::~Window()
00096     {
00097     }
00098 
00099     void Window::setPadding(unsigned int padding)
00100     {
00101         mPadding = padding;
00102     }
00103 
00104     unsigned int Window::getPadding() const
00105     {
00106         return mPadding;
00107     }
00108 
00109     void Window::setTitleBarHeight(unsigned int height)
00110     {
00111         mTitleBarHeight = height;
00112     }
00113 
00114     unsigned int Window::getTitleBarHeight()
00115     {
00116         return mTitleBarHeight;
00117     }
00118 
00119     void Window::setCaption(const std::string& caption)
00120     {
00121         mCaption = caption;
00122     }
00123 
00124     const std::string& Window::getCaption() const
00125     {
00126         return mCaption;
00127     }
00128 
00129     void Window::setAlignment(unsigned int alignment)
00130     {
00131         mAlignment = alignment;
00132     }
00133 
00134     unsigned int Window::getAlignment() const
00135     {
00136         return mAlignment;
00137     }
00138 
00139     void Window::draw(Graphics* graphics)
00140     {
00141         Color faceColor = getBaseColor();
00142         Color highlightColor, shadowColor;
00143         int alpha = getBaseColor().a;
00144         int width = getWidth() + getBorderSize() * 2 - 1;
00145         int height = getHeight() + getBorderSize() * 2 - 1;
00146         highlightColor = faceColor + 0x303030;
00147         highlightColor.a = alpha;
00148         shadowColor = faceColor - 0x303030;
00149         shadowColor.a = alpha;
00150 
00151         Rectangle d = getChildrenArea();
00152 
00153         // Fill the background around the content
00154         graphics->setColor(faceColor);
00155         // Fill top
00156         graphics->fillRectangle(Rectangle(0,0,getWidth(),d.y - 1));
00157         // Fill left
00158         graphics->fillRectangle(Rectangle(0,d.y - 1, d.x - 1, getHeight() - d.y + 1));
00159         // Fill right
00160         graphics->fillRectangle(Rectangle(d.x + d.width + 1,
00161                                           d.y - 1,
00162                                           getWidth() - d.x - d.width - 1,
00163                                           getHeight() - d.y + 1));
00164         // Fill bottom
00165         graphics->fillRectangle(Rectangle(d.x - 1,
00166                                           d.y + d.height + 1,
00167                                           d.width + 2,
00168                                           getHeight() - d.height - d.y - 1));
00169 
00170         if (isOpaque())
00171         {
00172             graphics->fillRectangle(d);
00173         }
00174 
00175         // Construct a rectangle one pixel bigger than the content
00176         d.x -= 1;
00177         d.y -= 1;
00178         d.width += 2;
00179         d.height += 2;
00180 
00181         // Draw a border around the content
00182         graphics->setColor(shadowColor);
00183         // Top line
00184         graphics->drawLine(d.x,
00185                            d.y,
00186                            d.x + d.width - 2,
00187                            d.y);
00188 
00189         // Left line
00190         graphics->drawLine(d.x,
00191                            d.y + 1,
00192                            d.x,
00193                            d.y + d.height - 1);
00194 
00195         graphics->setColor(highlightColor);
00196         // Right line
00197         graphics->drawLine(d.x + d.width - 1,
00198                            d.y,
00199                            d.x + d.width - 1,
00200                            d.y + d.height - 2);
00201         // Bottom line
00202         graphics->drawLine(d.x + 1,
00203                            d.y + d.height - 1,
00204                            d.x + d.width - 1,
00205                            d.y + d.height - 1);
00206 
00207         drawChildren(graphics);
00208 
00209         int textX;
00210         int textY;
00211         textY = (getTitleBarHeight() - getFont()->getHeight()) / 2;
00212         switch (getAlignment())
00213         {
00214           case Graphics::LEFT:
00215               textX = 4;
00216               break;
00217           case Graphics::CENTER:
00218               textX = getWidth() / 2;
00219               break;
00220           case Graphics::RIGHT:
00221               textX = getWidth() - 4;
00222               break;
00223           default:
00224               throw GCN_EXCEPTION("Unknown alignment.");
00225         }
00226 
00227         graphics->setColor(getForegroundColor());
00228         graphics->setFont(getFont());
00229         graphics->drawText(getCaption(), textX, textY, getAlignment());
00230     }
00231 
00232     void Window::drawBorder(Graphics* graphics)
00233     {
00234         Color faceColor = getBaseColor();
00235         Color highlightColor, shadowColor;
00236         int alpha = getBaseColor().a;
00237         int width = getWidth() + getBorderSize() * 2 - 1;
00238         int height = getHeight() + getBorderSize() * 2 - 1;
00239         highlightColor = faceColor + 0x303030;
00240         highlightColor.a = alpha;
00241         shadowColor = faceColor - 0x303030;
00242         shadowColor.a = alpha;
00243 
00244         unsigned int i;
00245         for (i = 0; i < getBorderSize(); ++i)
00246         {
00247             graphics->setColor(highlightColor);
00248             graphics->drawLine(i,i, width - i, i);
00249             graphics->drawLine(i,i + 1, i, height - i - 1);
00250             graphics->setColor(shadowColor);
00251             graphics->drawLine(width - i,i + 1, width - i, height - i);
00252             graphics->drawLine(i,height - i, width - i - 1, height - i);
00253         }
00254     }
00255 
00256     void Window::mousePress(int x, int y, int button)
00257     {
00258         if (getParent() != NULL)
00259         {
00260             getParent()->moveToTop(this);
00261         }
00262 
00263         if (isMovable() && hasMouse()
00264             && y < (int)(getTitleBarHeight() + getPadding()) && button == 1)
00265         {
00266             mMouseDrag = true;
00267             mMouseXOffset = x;
00268             mMouseYOffset = y;
00269         }
00270     }
00271 
00272     void Window::mouseRelease(int x, int y, int button)
00273     {
00274         if (button == 1)
00275         {
00276             mMouseDrag = false;
00277         }
00278     }
00279 
00280     void Window::mouseMotion(int x, int y)
00281     {
00282         if (mMouseDrag && isMovable())
00283         {
00284             setPosition(x - mMouseXOffset + getX(),
00285                         y - mMouseYOffset + getY());
00286         }
00287     }
00288 
00289     Rectangle Window::getChildrenArea()
00290     {
00291         return Rectangle(getPadding(),
00292                          getTitleBarHeight(),
00293                          getWidth() - getPadding() * 2,
00294                          getHeight() - getPadding() - getTitleBarHeight());
00295     }
00296 
00297     void Window::setMovable(bool movable)
00298     {
00299         mMovable = movable;
00300     }
00301 
00302     bool Window::isMovable() const
00303     {
00304         return mMovable;
00305     }
00306 
00307     void Window::setOpaque(bool opaque)
00308     {
00309         mOpaque = opaque;
00310     }
00311 
00312     bool Window::isOpaque()
00313     {
00314         return mOpaque;
00315     }
00316 
00317     void Window::resizeToContent()
00318     {
00319         WidgetListIterator it;
00320 
00321         int w = 0, h = 0;
00322         for (it = mWidgets.begin(); it != mWidgets.end(); it++)
00323         {
00324             if ((*it)->getX() + (*it)->getWidth() > w)
00325             {
00326                 w = (*it)->getX() + (*it)->getWidth();
00327             }
00328 
00329             if ((*it)->getY() + (*it)->getHeight() > h)
00330             {
00331                 h = (*it)->getY() + (*it)->getHeight();
00332             }
00333         }
00334 
00335         setSize(w + 2* getPadding(), h + getPadding() + getTitleBarHeight());
00336     }
00337 }

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