00001
00002
00003
00004
00005
00006
00007 #include "QWidget.h"
00008
00009
00010 #include <iostream>
00011 using namespace std;
00012
00013
00014
00015
00016
00017 void QWidget::add( QWidget* widget )
00018 {
00019 Children.push_front(widget);
00020 childrenChanged();
00021 }
00022
00023
00024 void QWidget::remove( QWidget* widget )
00025 {
00026 if (FocusPath == widget) FocusPath = 0;
00027 Children.remove(widget);
00028 childrenChanged();
00029 }
00030
00031
00032 void QWidget::freeWidget( QWidget* widget )
00033 {
00034 if (Owner) Owner->freeWidget(widget);
00035 }
00036
00037
00038 void QWidget::clearFocusPath()
00039 {
00040 if (FocusPath != 0)
00041 {
00042 FocusPath->clearFocusPath();
00043 FocusPath = 0;
00044 }
00045 }
00046
00047
00048 bool QWidget::getFocused()
00049 {
00050 return (Owner->getFocusPath() == this);
00051 }
00052
00053
00054 void QWidget::free()
00055 {
00056 if (Owner) Owner->freeWidget(this);
00057 }
00058
00059
00060 bool QWidget::event( QEvent* e )
00061 {
00062
00063
00064 if (!Visible) return false;
00065
00066
00067 if (e->getType() == etDraw)
00068 {
00069 changeContext();
00070 draw(e);
00071 if (onCustomDraw!=0) (*onCustomDraw)(this,e);
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 eventToChildrenReversed(e);
00088
00089
00090 restoreContext();
00091 return false;
00092 }
00093
00094
00095
00096
00097 if (!Enabled) return false;
00098
00099
00100 if ((e->getType() == etKeyboard) ||
00101 (e->getType() == etSpecialKey ) ||
00102 (e->getType() == etKeyboardUp ))
00103 {
00104 return handleKeyboardEvent(e);
00105 }
00106
00107
00108
00109 return handleMouseEvent(e);
00110
00111 }
00112
00113
00114 void QWidget::redraw()
00115 {
00116 Owner->redraw();
00117 }
00118
00119
00120 bool QWidget::eventToChildren( QEvent* e )
00121 {
00122
00123 list<QWidget*>::const_iterator iter;
00124
00125 for (iter=Children.begin(); iter != Children.end(); iter++)
00126 {
00127 if ((*iter)->event(e)) return true;
00128 }
00129 return false;
00130 }
00131
00132
00133 bool QWidget::eventToChildrenReversed( QEvent* e )
00134 {
00135
00136 list<QWidget*>::reverse_iterator iter;
00137
00138 for (iter=Children.rbegin(); iter != Children.rend(); iter++)
00139 {
00140 if ((*iter)->event(e)) return true;
00141 }
00142 return false;
00143 }
00144
00145
00146 void QWidget::bringToFront( QWidget* widget )
00147 {
00148 unsigned int size = Children.size();
00149 Children.remove(widget);
00150 if ((Children.size()+1)== size) Children.push_front(widget);
00151 }
00152
00153
00154 bool QWidget::isInFront( QWidget* widget )
00155 {
00156 return (Children.front() == widget);
00157 }
00158
00159
00160 bool QWidget::handleKeyboardEvent( QEvent* e )
00161 {
00162 processEvent(e);
00163 if (onKey!=0) (*onKey)(this,e);
00164 if (FocusPath) FocusPath->event(e);
00165 return true;
00166 }
00167
00168
00169 bool QWidget::handleMouseEvent( QEvent* e )
00170 {
00171
00172
00173 if ((Dragging) && (e->getType() == etMouseMotion))
00174 {
00175 handleDragging(e);
00176 return true;
00177 }
00178
00179
00180 int saved_x = e->getX();
00181 int saved_y = e->getY();
00182
00183
00184 int x = saved_x - Left;
00185 int y = saved_y - Top;
00186
00187
00188
00189 bool ourRegion = ((x>=0) && (y>=0) && (x<=Width) && (y<=Height));
00190
00191 if (ourRegion && (e->getType() == etMouseDown))
00192 {
00193 doFocus(e);
00194 }
00195
00196
00197 e->setX(x);
00198 e->setY(y);
00199
00200
00201
00202
00203 bool handledByChildren = eventToChildren(e);
00204
00205
00206 e->setX(saved_x);
00207 e->setY(saved_y);
00208
00209
00210 if (handledByChildren) return true;
00211
00212
00213
00214
00215 if ((Dragging) && (e->getType() == etMouseUp)) Dragging = false;
00216
00217
00218 processMouseOwner(e);
00219
00220
00221
00222
00223 if (ourRegion)
00224 {
00225
00226 processEvent(e);
00227
00228
00229 switch (e->getType())
00230 {
00231 case etMouseClick:
00232 if (onClick!=0) (*onClick)(this,e);
00233 break;
00234
00235 case etMouseDown:
00236 if (canDrag(e))
00237 {
00238 Dragging = true;
00239 DragStartX = x;
00240 DragStartY = y;
00241 }
00242 if (onMouseDown!=0) (*onMouseDown)(this,e);
00243 break;
00244
00245 case etMouseUp:
00246
00247 if (onMouseUp!=0) (*onMouseUp)(this,e);
00248 break;
00249
00250 case etMouseMotion:
00251 if (onMouseMove!=0) (*onMouseMove)(this,e);
00252 break;
00253
00254 default: ;
00255 }
00256
00257
00258
00259 return true;
00260 }
00261
00262
00263
00264 return false;
00265
00266 }
00267
00268
00269 void QWidget::handleDragging( QEvent* e )
00270 {
00271
00272 const int snap_width = 5;
00273
00274 int new_x = e->getX()-DragStartX;
00275 int new_y = e->getY()-DragStartY;
00276
00277 int minx = Owner->getClientBorderLeft();
00278 int maxx = Owner->getWidth()-Width-Owner->getClientBorderRight();
00279 int miny = Owner->getClientBorderTop();
00280 int maxy = Owner->getHeight()-Height-Owner->getClientBorderBottom();
00281
00282 if (new_x < snap_width + minx ) new_x = minx;
00283 else
00284 if (new_x > maxx - snap_width) new_x = maxx;
00285
00286 if (new_y < snap_width + miny) new_y = miny;
00287 else
00288 if (new_y > maxy - snap_width) new_y = maxy;
00289
00290 setLeft(new_x);
00291 setTop (new_y);
00292
00293 }
00294
00295
00296 void QWidget::doFocus( QEvent* e )
00297 {
00298 if (Owner) Owner->bringToFront(this);
00299 if ((canFocus(e)) && (Owner!=0))
00300 {
00301 Owner->requestFocus(this,this);
00302 }
00303 }
00304
00305
00306 void QWidget::requestFocus( QWidget* Sender , QWidget* FocusedWidget )
00307 {
00308 if ((FocusPath != 0) && (FocusPath != Sender))
00309 {
00310 FocusPath->clearFocusPath();
00311 }
00312 FocusPath = Sender;
00313 Owner->requestFocus(this,FocusedWidget);
00314 }
00315
00316
00317 void QWidget::changeContext()
00318 {
00319 glPushMatrix();
00320 glTranslatef(Left,Top,0.0);
00321 }
00322
00323
00324 void QWidget::restoreContext()
00325 {
00326 glPopMatrix();
00327 }
00328
00329
00330 void QWidget::freeChildren()
00331 {
00332 while (Children.size() > 0)
00333 {
00334 delete Children.front();
00335 Children.pop_front();
00336 }
00337 }
00338
00339
00340 void QWidget::draw( QEvent* e )
00341 {
00342
00343 }
00344
00345
00346 void QWidget::layout()
00347 {
00348
00349 }
00350
00351
00352 bool QWidget::canDrag( QEvent* e )
00353 {
00354 return false;
00355 }
00356
00357
00358 bool QWidget::processEvent( QEvent* e )
00359 {
00360 return false;
00361 }
00362
00363
00364 bool QWidget::processMouseOwner( QEvent* e )
00365 {
00366 return false;
00367 }
00368
00369
00370 void QWidget::childrenChanged()
00371 {
00372 }
00373
00374
00375 bool QWidget::canFocus( QEvent* e )
00376 {
00377 return false;
00378 }
00379
00380
00381 void QWidget::Init()
00382 {
00383
00384 }
00385
00386
00387 void QWidget::CleanUp()
00388 {
00389
00390 }
00391
00392
00393 int QWidget::getWidth()
00394 {
00395 return Width;
00396 }
00397
00398
00399 void QWidget::setWidth( int newValue )
00400 {
00401 if (newValue < MinWidth) newValue = MinWidth;
00402 if (Width != newValue)
00403 {
00404 Width = newValue;
00405 layout();
00406 if (onResize!=0) (*onResize)(this);
00407 }
00408 }
00409
00410
00411 int QWidget::getHeight()
00412 {
00413 return Height;
00414 }
00415
00416
00417 void QWidget::setHeight( int newValue )
00418 {
00419 if (newValue < MinHeight) newValue = MinHeight;
00420 if (Height != newValue)
00421 {
00422 Height = newValue;
00423 layout();
00424 if (onResize!=0) (*onResize)(this);
00425 }
00426 }
00427
00428
00429 int QWidget::getTop()
00430 {
00431 return Top;
00432 }
00433
00434
00435 void QWidget::setTop( int newValue )
00436 {
00437 if (Top != newValue)
00438 {
00439 Top = newValue;
00440 }
00441 }
00442
00443
00444 int QWidget::getLeft()
00445 {
00446 return Left;
00447 }
00448
00449
00450 void QWidget::setLeft( int newValue )
00451 {
00452 if (Left != newValue)
00453 {
00454 Left = newValue;
00455 }
00456 }
00457
00458
00459 int QWidget::getMinWidth()
00460 {
00461 return MinWidth;
00462 }
00463
00464
00465 void QWidget::setMinWidth( int newValue )
00466 {
00467 MinWidth = newValue;
00468 }
00469
00470
00471 int QWidget::getMinHeight()
00472 {
00473 return MinHeight;
00474 }
00475
00476
00477 void QWidget::setMinHeight( int newValue )
00478 {
00479 MinHeight = newValue;
00480 }
00481
00482
00483 int QWidget::getClientBorderLeft()
00484 {
00485 return ClientBorderLeft;
00486 }
00487
00488
00489 void QWidget::setClientBorderLeft( int newValue )
00490 {
00491 ClientBorderLeft = newValue;
00492 }
00493
00494
00495 int QWidget::getClientBorderRight()
00496 {
00497 return ClientBorderRight;
00498 }
00499
00500
00501 void QWidget::setClientBorderRight( int newValue )
00502 {
00503 ClientBorderRight = newValue;
00504 }
00505
00506
00507 int QWidget::getClientBorderTop()
00508 {
00509 return ClientBorderTop;
00510 }
00511
00512
00513 void QWidget::setClientBorderTop( int newValue )
00514 {
00515 ClientBorderTop = newValue;
00516 }
00517
00518
00519 int QWidget::getClientBorderBottom()
00520 {
00521 return ClientBorderBottom;
00522 }
00523
00524
00525 void QWidget::setClientBorderBottom( int newValue )
00526 {
00527 ClientBorderBottom = newValue;
00528 }
00529
00530
00531 bool QWidget::getEnabled()
00532 {
00533 return Enabled;
00534 }
00535
00536
00537 void QWidget::setEnabled( bool newValue )
00538 {
00539 Enabled = newValue;
00540 }
00541
00542
00543 bool QWidget::getVisible()
00544 {
00545 return Visible;
00546 }
00547
00548
00549 void QWidget::setVisible( bool newValue )
00550 {
00551 Visible = newValue;
00552 }
00553
00554
00555 QLookAndFeel* QWidget::getLookAndFeel()
00556 {
00557 return LookAndFeel;
00558 }
00559
00560
00561 void QWidget::setLookAndFeel( QLookAndFeel* newValue )
00562 {
00563 LookAndFeel = newValue;
00564 }
00565
00566
00567 QWidget* QWidget::getOwner()
00568 {
00569 return Owner;
00570 }
00571
00572
00573 void QWidget::setOwner( QWidget* newValue )
00574 {
00575 Owner = newValue;
00576 }
00577
00578
00579 QWidget* QWidget::getFocusPath()
00580 {
00581 return FocusPath;
00582 }
00583
00584
00585 void QWidget::setFocusPath( QWidget* newValue )
00586 {
00587 FocusPath = newValue;
00588 }
00589
00590
00591
00592
00593 void QWidget::print()
00594 {
00595
00596 cout << "Class : QWidget" << endl;
00597 cout << "Width = " << Width << endl;
00598 cout << "Height = " << Height << endl;
00599 cout << "Top = " << Top << endl;
00600 cout << "Left = " << Left << endl;
00601 cout << "Enabled = " << Enabled << endl;
00602 cout << "Visible = " << Visible << endl;
00603 cout << "Owner = " << Owner << endl;
00604 cout << "Children = " << Children.size() << endl;
00605 }
00606
00607
00608
00609
00610 QWidget::QWidget( QWidget* aOwner )
00611 {
00612 onResize = 0;
00613 onCustomDraw = 0;
00614 onKey = 0;
00615 onMouseMove = 0;
00616 onMouseUp = 0;
00617 onMouseDown = 0;
00618 onClick = 0;
00619 DragStartX = 0;
00620 DragStartY = 0;
00621 Width = 20;
00622 Height = 10;
00623 Top = 0;if (aOwner) Top = aOwner->getClientBorderTop();
00624 Left = 0;if (aOwner) Left = aOwner->getClientBorderLeft();
00625 MinWidth = 0;
00626 MinHeight = 0;
00627 ClientBorderLeft = 0;
00628 ClientBorderRight = 0;
00629 ClientBorderTop = 0;
00630 ClientBorderBottom = 0;
00631 Enabled = true;
00632 Visible = true;
00633 Dragging = false;
00634 LookAndFeel = 0;if (aOwner) LookAndFeel = aOwner->getLookAndFeel();
00635 Owner = aOwner;if (aOwner) aOwner->add(this);
00636 FocusPath = 0;
00637 Init();
00638 layout();
00639 }
00640
00641
00642
00643
00644 QWidget::~QWidget()
00645 {
00646
00647 freeChildren();
00648 CleanUp();
00649 }
00650
00651
00652
00653