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