00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
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
00154 graphics->setColor(faceColor);
00155
00156 graphics->fillRectangle(Rectangle(0,0,getWidth(),d.y - 1));
00157
00158 graphics->fillRectangle(Rectangle(0,d.y - 1, d.x - 1, getHeight() - d.y + 1));
00159
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
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
00176 d.x -= 1;
00177 d.y -= 1;
00178 d.width += 2;
00179 d.height += 2;
00180
00181
00182 graphics->setColor(shadowColor);
00183
00184 graphics->drawLine(d.x,
00185 d.y,
00186 d.x + d.width - 2,
00187 d.y);
00188
00189
00190 graphics->drawLine(d.x,
00191 d.y + 1,
00192 d.x,
00193 d.y + d.height - 1);
00194
00195 graphics->setColor(highlightColor);
00196
00197 graphics->drawLine(d.x + d.width - 1,
00198 d.y,
00199 d.x + d.width - 1,
00200 d.y + d.height - 2);
00201
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 }