00001
00002
00003
00004
00005
00006
00007 #include "QWindow.h"
00008 #include <iostream>
00009 using namespace std;
00010
00011
00012
00013 #include "QWindow_Handlers.h"
00014
00015 void QWindow::drawFrame()
00016 {
00017
00018 const int border = TitleBarBorder;
00019
00020 int fleft = 0;
00021 int ftop = 0;
00022
00023 int fwidth = Width;
00024 int fheight = Height;
00025
00026
00027 LookAndFeel->getBackGroundColor()->setAsCurrent();
00028 glBegin(GL_POLYGON);
00029 LookAndFeel->drawRect(fleft,ftop,Width,Height);
00030 glEnd();
00031
00032
00033 LookAndFeel->drawBevel(fleft,ftop,fwidth,fheight,bsUp);
00034 LookAndFeel->drawBevel(fleft+border,ftop+border,fwidth-2*border,fheight-2*border,bsDown);
00035
00036
00037
00038 if (Owner->getFocusPath() == this)
00039 {
00040 TitleBarColor->setAsCurrent();
00041 Font->setBold(true);
00042 }
00043 else
00044 {
00045 TitleBarColorInactive->setAsCurrent();
00046 Font->setBold(false);
00047 }
00048
00049
00050 glBegin(GL_POLYGON);
00051 LookAndFeel->drawRect(fleft+border,ftop+border,fwidth-2*border,TitleBarHeight);
00052 glEnd();
00053
00054
00055 int maxLen = Font->getLengthForWidth(Caption.c_str(),MaximizeButton->getLeft()-(fleft+border+2));
00056 Font->drawAtPos(Caption.c_str(),fleft+border+2,ftop+border+2,maxLen);
00057
00058
00059 Font->setBold(false);
00060
00061
00062 if (Resizable)
00063 {
00064 glColor3f(1,1,1);
00065 glBegin(GL_LINES);
00066 glVertex2i(fwidth-border,fheight-12);
00067 glVertex2i(fwidth-12,fheight-border);
00068
00069 glVertex2i(fwidth-border,fheight-8);
00070 glVertex2i(fwidth-8,fheight-border);
00071
00072 glEnd();
00073 }
00074
00075
00076
00077 if (Resizing)
00078 {
00079 char buf[101];
00080 snprintf(buf,100,"resize to %dx%d",Width,Height);
00081 drawCoordInfos(fwidth+10,fheight-10,buf);
00082 }
00083 else
00084 if (Dragging)
00085 {
00086 char buf[101];
00087 snprintf(buf,100,"move to (%d,%d)",Left,Top);
00088 drawCoordInfos(fleft+5,ftop-25,buf);
00089 }
00090
00091 }
00092
00093
00094 void QWindow::drawCoordInfos( int x , int y , const char* Text )
00095 {
00096 float textwidth = Font->getTextWidth(Text);
00097 glEnable(GL_BLEND);
00098 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00099 glColor4f(0.2,0.2,0.2,0.5);
00100 glBegin(GL_POLYGON);
00101 LookAndFeel->drawRect(x-5,y-2,textwidth+10,Font->getSize()+5);
00102 glEnd();
00103 glDisable(GL_BLEND);
00104 Font->drawAtPos(Text,x,y,-1);
00105 }
00106
00107
00108 void QWindow::drawCloseButton()
00109 {
00110
00111
00112 glColor4f(1,1,1,1);
00113
00114 LookAndFeel->drawCloseButton(CloseButton->getState() == bsDown,
00115 CloseButton->getWidth(), CloseButton->getHeight());
00116 }
00117
00118
00119 void QWindow::drawMaximizeButton()
00120 {
00121
00122
00123 glColor4f(1,1,1,1);
00124
00125 LookAndFeel->drawMaximizeButton(MaximizeButton->getState() == bsDown,
00126 MaximizeButton->getWidth(), MaximizeButton->getHeight());
00127 }
00128
00129
00130 void QWindow::maximize()
00131 {
00132 if (Maximized)
00133 {
00134 setLeft(OldLeft);
00135 setTop(OldTop);
00136 setWidth(OldWidth);
00137 setHeight(OldHeight);
00138 Maximized = false;
00139 }
00140 else
00141 {
00142 OldLeft = Left;
00143 OldTop = Top;
00144 OldWidth = Width;
00145 OldHeight = Height;
00146 setLeft(0);
00147 setTop(0);
00148 setWidth(Owner->getWidth() - Owner->getClientBorderLeft() - Owner->getClientBorderRight());
00149 setHeight(Owner->getHeight() - Owner->getClientBorderTop() - Owner->getClientBorderBottom());
00150 Maximized = true;
00151 }
00152 }
00153
00154
00155 void QWindow::draw( QEvent* e )
00156 {
00157 drawFrame();
00158
00159 }
00160
00161
00162 void QWindow::layout()
00163 {
00164 CloseButton->setLeft(Width - CloseButton->getWidth()-TitleBarBorder-3);
00165 MaximizeButton->setLeft(CloseButton->getLeft() - MaximizeButton->getWidth()-3);
00166 }
00167
00168
00169 bool QWindow::canDrag( QEvent* e )
00170 {
00171
00172 return (e->getY() - Top) < TitleBarHeight;
00173 }
00174
00175
00176 bool QWindow::processEvent( QEvent* e )
00177 {
00178
00179 if ( Resizable &&
00180 (e->getButton() == mbLeft) &&
00181 (e->getType() == etMouseDown) &&
00182 ((Width + Left - e->getX()) < 10) &&
00183 ((Height + Top - e->getY()) < 10)
00184 )
00185 {
00186 ResizeStartX = (Width - e->getX());
00187 ResizeStartY = (Height - e->getY());
00188 Resizing = true;
00189 return true;
00190 }
00191 return false;
00192 }
00193
00194
00195 bool QWindow::processMouseOwner( QEvent* e )
00196 {
00197
00198 if (
00199 (e->getButton() == mbLeft) &&
00200 (e->getType() == etMouseMotion)
00201 )
00202 {
00203 if (Resizing)
00204 {
00205
00206 int newWidth = ResizeStartX + e->getX();
00207 int newHeight = ResizeStartY + e->getY();
00208
00209 if (newWidth < MinResizeWidth) newWidth = MinResizeWidth;
00210 if (newHeight < MinResizeHeight) newHeight = MinResizeHeight;
00211
00212 if (Owner)
00213 {
00214 if (newWidth < Owner->getWidth() - Left)
00215 setWidth(newWidth);
00216 else
00217 setWidth( Owner->getWidth() - Left);
00218
00219 if (newHeight < Owner->getHeight() - Top)
00220 setHeight(newHeight);
00221 else
00222 setHeight( Owner->getHeight() - Top);
00223 }
00224 else
00225 {
00226 setWidth(newWidth);
00227 setHeight(newHeight);
00228 }
00229
00230 }
00231 }
00232 else
00233 Resizing = false;
00234
00235 return false;
00236 }
00237
00238
00239 void QWindow::childrenChanged()
00240 {
00241 }
00242
00243
00244 bool QWindow::canFocus( QEvent* e )
00245 {
00246 return true;
00247 }
00248
00249
00250 void QWindow::Init()
00251 {
00252 Caption = "Window";
00253
00254 TitleBarColor->set(0.0, 0.0, 0.9, 1.0);
00255 TitleBarColorInactive->set(0.2,0.2,0.25,1.0);
00256
00257 Font->getColor()->set(1,1,1,1);
00258 Font->setSize(16);
00259
00260
00261 CloseButton->onClick = CloseButtonHandler;
00262 CloseButton->onCustomDraw = drawCloseButtonHandler;
00263 CloseButton->setHeight(TitleBarHeight-4);
00264 CloseButton->setWidth (CloseButton->getHeight());
00265 CloseButton->setTop(TitleBarBorder+2);
00266
00267 MaximizeButton->onClick = MaximizeButtonHandler;
00268 MaximizeButton->onCustomDraw = drawMaximizeButtonHandler;
00269 MaximizeButton->setHeight(CloseButton->getHeight());
00270 MaximizeButton->setWidth(CloseButton->getWidth());
00271 MaximizeButton->setTop(CloseButton->getTop());
00272
00273 ClientBorderTop = TitleBarBorder+TitleBarHeight+2;
00274 ClientBorderLeft = TitleBarBorder+1;
00275 ClientBorderRight = TitleBarBorder+1;
00276 ClientBorderBottom = TitleBarBorder+1;
00277
00278 MinWidth = MinResizeWidth;
00279 MinHeight = MinResizeHeight;
00280
00281 }
00282
00283
00284 void QWindow::CleanUp()
00285 {
00286
00287 }
00288
00289
00290 int QWindow::getTitleBarHeight()
00291 {
00292 return TitleBarHeight;
00293 }
00294
00295
00296 void QWindow::setTitleBarHeight( int newValue )
00297 {
00298 TitleBarHeight = newValue;
00299 }
00300
00301
00302 int QWindow::getTitleBarBorder()
00303 {
00304 return TitleBarBorder;
00305 }
00306
00307
00308 void QWindow::setTitleBarBorder( int newValue )
00309 {
00310 TitleBarBorder = newValue;
00311 }
00312
00313
00314 string QWindow::getCaption()
00315 {
00316 return Caption;
00317 }
00318
00319
00320 void QWindow::setCaption( string newValue )
00321 {
00322 Caption = newValue;
00323 }
00324
00325
00326 int QWindow::getMinResizeWidth()
00327 {
00328 return MinResizeWidth;
00329 }
00330
00331
00332 void QWindow::setMinResizeWidth( int newValue )
00333 {
00334 MinResizeWidth = newValue;
00335 }
00336
00337
00338 int QWindow::getMinResizeHeight()
00339 {
00340 return MinResizeHeight;
00341 }
00342
00343
00344 void QWindow::setMinResizeHeight( int newValue )
00345 {
00346 MinResizeHeight = newValue;
00347 }
00348
00349
00350 bool QWindow::getMaximized()
00351 {
00352 return Maximized;
00353 }
00354
00355
00356 void QWindow::setMaximized( bool newValue )
00357 {
00358 Maximized = newValue;
00359 }
00360
00361
00362 bool QWindow::getResizable()
00363 {
00364 return Resizable;
00365 }
00366
00367
00368 void QWindow::setResizable( bool newValue )
00369 {
00370 Resizable = newValue;
00371 }
00372
00373
00374 QButton* QWindow::getCloseButton()
00375 {
00376 return CloseButton;
00377 }
00378
00379
00380 QButton* QWindow::getMaximizeButton()
00381 {
00382 return MaximizeButton;
00383 }
00384
00385
00386 QFont* QWindow::getFont()
00387 {
00388 return Font;
00389 }
00390
00391
00392 QColor* QWindow::getTitleBarColor()
00393 {
00394 return TitleBarColor;
00395 }
00396
00397
00398 QColor* QWindow::getTitleBarColorInactive()
00399 {
00400 return TitleBarColorInactive;
00401 }
00402
00403
00404
00405
00406 void QWindow::print()
00407 {
00408 cout << "Class : QWindow" << endl;
00409 cout << "TitleBarHeight = " << TitleBarHeight << endl;
00410 cout << "TitleBarBorder = " << TitleBarBorder << endl;
00411 cout << "Caption = " << Caption << endl;
00412 cout << "MinResizeWidth = " << MinResizeWidth << endl;
00413 cout << "MinResizeHeight = " << MinResizeHeight << endl;
00414 cout << "Maximized = " << Maximized << endl;
00415 cout << "Resizable = " << Resizable << endl;
00416 cout << "CloseButton = " << CloseButton << endl;
00417 cout << "MaximizeButton = " << MaximizeButton << endl;
00418 }
00419
00420
00421
00422
00423 QWindow::QWindow( QWidget* aOwner ):QWidget(aOwner)
00424 {
00425 Resizing = false;
00426 OldLeft = 0;
00427 OldTop = 0;
00428 OldWidth = 0;
00429 OldHeight = 0;
00430 ResizeStartX = 0;
00431 ResizeStartY = 0;
00432 TitleBarHeight = 23;
00433 TitleBarBorder = 2;
00434 MinResizeWidth = 60;
00435 MinResizeHeight = 40;
00436 Maximized = false;
00437 Resizable = true;
00438 CloseButton = new QButton(this);
00439 MaximizeButton = new QButton(this);
00440 Font = new QFont;
00441 TitleBarColor = new QColor;
00442 TitleBarColorInactive = new QColor;
00443 Init();
00444 layout();
00445 }
00446
00447
00448
00449
00450 QWindow::~QWindow()
00451 {
00452 delete TitleBarColorInactive;
00453 delete TitleBarColor;
00454 delete Font;
00455 CleanUp();
00456 }
00457
00458
00459
00460