util/src/gui/kernel/qwidget.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qapplication.h"
       
    43 #include "qapplication_p.h"
       
    44 #include "qbrush.h"
       
    45 #include "qcursor.h"
       
    46 #include "qdesktopwidget.h"
       
    47 #include "qevent.h"
       
    48 #include "qhash.h"
       
    49 #include "qlayout.h"
       
    50 #include "qmenu.h"
       
    51 #include "qmetaobject.h"
       
    52 #include "qpixmap.h"
       
    53 #include "qpointer.h"
       
    54 #include "qstack.h"
       
    55 #include "qstyle.h"
       
    56 #include "qstylefactory.h"
       
    57 #include "qvariant.h"
       
    58 #include "qwidget.h"
       
    59 #include "qstyleoption.h"
       
    60 #ifndef QT_NO_ACCESSIBILITY
       
    61 # include "qaccessible.h"
       
    62 #endif
       
    63 #if defined(Q_WS_WIN)
       
    64 # include "qt_windows.h"
       
    65 #endif
       
    66 #ifdef Q_WS_MAC
       
    67 # include "qt_mac_p.h"
       
    68 # include "qt_cocoa_helpers_mac_p.h"
       
    69 # include "qmainwindow.h"
       
    70 #endif
       
    71 #if defined(Q_WS_QWS)
       
    72 # include "qwsdisplay_qws.h"
       
    73 # include "qwsmanager_qws.h"
       
    74 # include "qpaintengine.h" // for PorterDuff
       
    75 # include "private/qwindowsurface_qws_p.h"
       
    76 #endif
       
    77 #include "qpainter.h"
       
    78 #include "qtooltip.h"
       
    79 #include "qwhatsthis.h"
       
    80 #include "qdebug.h"
       
    81 #include "private/qstylesheetstyle_p.h"
       
    82 #include "private/qstyle_p.h"
       
    83 #include "private/qinputcontext_p.h"
       
    84 #include "qfileinfo.h"
       
    85 #include "private/qsoftkeymanager_p.h"
       
    86 
       
    87 #if defined (Q_WS_WIN)
       
    88 # include <private/qwininputcontext_p.h>
       
    89 #endif
       
    90 
       
    91 #if defined(Q_WS_X11)
       
    92 # include <private/qpaintengine_x11_p.h>
       
    93 # include "qx11info_x11.h"
       
    94 #endif
       
    95 
       
    96 #include <private/qgraphicseffect_p.h>
       
    97 #include <private/qwindowsurface_p.h>
       
    98 #include <private/qbackingstore_p.h>
       
    99 #ifdef Q_WS_MAC
       
   100 # include <private/qpaintengine_mac_p.h>
       
   101 #endif
       
   102 #include <private/qpaintengine_raster_p.h>
       
   103 
       
   104 #if defined(Q_OS_SYMBIAN)
       
   105 #include "private/qt_s60_p.h"
       
   106 #endif
       
   107 
       
   108 #include "qwidget_p.h"
       
   109 #include "qaction_p.h"
       
   110 #include "qlayout_p.h"
       
   111 #include "QtGui/qgraphicsproxywidget.h"
       
   112 #include "QtGui/qgraphicsscene.h"
       
   113 #include "private/qgraphicsproxywidget_p.h"
       
   114 #include "QtGui/qabstractscrollarea.h"
       
   115 #include "private/qabstractscrollarea_p.h"
       
   116 #include "private/qevent_p.h"
       
   117 
       
   118 #include "private/qgraphicssystem_p.h"
       
   119 #include "private/qgesturemanager_p.h"
       
   120 
       
   121 #ifdef QT_KEYPAD_NAVIGATION
       
   122 #include "qtabwidget.h" // Needed in inTabWidget()
       
   123 #endif // QT_KEYPAD_NAVIGATION
       
   124 
       
   125 // widget/widget data creation count
       
   126 //#define QWIDGET_EXTRA_DEBUG
       
   127 //#define ALIEN_DEBUG
       
   128 
       
   129 QT_BEGIN_NAMESPACE
       
   130 
       
   131 #if !defined(Q_WS_QWS)
       
   132 static bool qt_enable_backingstore = true;
       
   133 #endif
       
   134 #ifdef Q_WS_X11
       
   135 // for compatibility with Qt 4.0
       
   136 Q_GUI_EXPORT void qt_x11_set_global_double_buffer(bool enable)
       
   137 {
       
   138     qt_enable_backingstore = enable;
       
   139 }
       
   140 #endif
       
   141 
       
   142 static inline bool qRectIntersects(const QRect &r1, const QRect &r2)
       
   143 {
       
   144     return (qMax(r1.left(), r2.left()) <= qMin(r1.right(), r2.right()) &&
       
   145             qMax(r1.top(), r2.top()) <= qMin(r1.bottom(), r2.bottom()));
       
   146 }
       
   147 
       
   148 static inline bool hasBackingStoreSupport()
       
   149 {
       
   150 #ifdef Q_WS_MAC
       
   151     return QApplicationPrivate::graphicsSystem() != 0;
       
   152 #else
       
   153     return true;
       
   154 #endif
       
   155 }
       
   156 
       
   157 #ifdef Q_WS_MAC
       
   158 #  define QT_NO_PAINT_DEBUG
       
   159 #endif
       
   160 
       
   161 extern bool qt_sendSpontaneousEvent(QObject*, QEvent*); // qapplication.cpp
       
   162 extern QDesktopWidget *qt_desktopWidget; // qapplication.cpp
       
   163 
       
   164 QWidgetPrivate::QWidgetPrivate(int version)
       
   165     : QObjectPrivate(version)
       
   166       , extra(0)
       
   167       , focus_next(0)
       
   168       , focus_prev(0)
       
   169       , focus_child(0)
       
   170       , layout(0)
       
   171       , needsFlush(0)
       
   172       , redirectDev(0)
       
   173       , widgetItem(0)
       
   174       , extraPaintEngine(0)
       
   175       , polished(0)
       
   176       , graphicsEffect(0)
       
   177 #if !defined(QT_NO_IM)
       
   178       , imHints(Qt::ImhNone)
       
   179 #endif
       
   180       , inheritedFontResolveMask(0)
       
   181       , inheritedPaletteResolveMask(0)
       
   182       , leftmargin(0)
       
   183       , topmargin(0)
       
   184       , rightmargin(0)
       
   185       , bottommargin(0)
       
   186       , leftLayoutItemMargin(0)
       
   187       , topLayoutItemMargin(0)
       
   188       , rightLayoutItemMargin(0)
       
   189       , bottomLayoutItemMargin(0)
       
   190       , hd(0)
       
   191       , size_policy(QSizePolicy::Preferred, QSizePolicy::Preferred)
       
   192       , fg_role(QPalette::NoRole)
       
   193       , bg_role(QPalette::NoRole)
       
   194       , dirtyOpaqueChildren(1)
       
   195       , isOpaque(0)
       
   196       , inDirtyList(0)
       
   197       , isScrolled(0)
       
   198       , isMoved(0)
       
   199       , isGLWidget(0)
       
   200       , usesDoubleBufferedGLContext(0)
       
   201 #if defined(Q_WS_X11)
       
   202       , picture(0)
       
   203 #elif defined(Q_WS_WIN)
       
   204       , noPaintOnScreen(0)
       
   205       , nativeGesturePanEnabled(0)
       
   206 #elif defined(Q_WS_MAC)
       
   207       , needWindowChange(0)
       
   208       , window_event(0)
       
   209       , qd_hd(0)
       
   210 #endif
       
   211 {
       
   212     if (!qApp) {
       
   213         qFatal("QWidget: Must construct a QApplication before a QPaintDevice");
       
   214         return;
       
   215     }
       
   216 
       
   217     if (version != QObjectPrivateVersion)
       
   218         qFatal("Cannot mix incompatible Qt libraries");
       
   219 
       
   220     isWidget = true;
       
   221     memset(high_attributes, 0, sizeof(high_attributes));
       
   222 #ifdef QWIDGET_EXTRA_DEBUG
       
   223     static int count = 0;
       
   224     qDebug() << "widgets" << ++count;
       
   225 #endif
       
   226 }
       
   227 
       
   228 
       
   229 QWidgetPrivate::~QWidgetPrivate()
       
   230 {
       
   231     if (widgetItem)
       
   232         widgetItem->wid = 0;
       
   233 
       
   234     if (extra)
       
   235         deleteExtra();
       
   236 
       
   237 #ifndef QT_NO_GRAPHICSEFFECT
       
   238     delete graphicsEffect;
       
   239 #endif //QT_NO_GRAPHICSEFFECT
       
   240 }
       
   241 
       
   242 QWindowSurface *QWidgetPrivate::createDefaultWindowSurface()
       
   243 {
       
   244     Q_Q(QWidget);
       
   245     if (QApplicationPrivate::graphicsSystem())
       
   246         return QApplicationPrivate::graphicsSystem()->createWindowSurface(q);
       
   247     return createDefaultWindowSurface_sys();
       
   248 }
       
   249 
       
   250 /*!
       
   251     \internal
       
   252 */
       
   253 void QWidgetPrivate::scrollChildren(int dx, int dy)
       
   254 {
       
   255     Q_Q(QWidget);
       
   256     if (q->children().size() > 0) {        // scroll children
       
   257         QPoint pd(dx, dy);
       
   258         QObjectList childObjects = q->children();
       
   259         for (int i = 0; i < childObjects.size(); ++i) { // move all children
       
   260             QWidget *w = qobject_cast<QWidget*>(childObjects.at(i));
       
   261             if (w && !w->isWindow()) {
       
   262                 QPoint oldp = w->pos();
       
   263                 QRect  r(w->pos() + pd, w->size());
       
   264                 w->data->crect = r;
       
   265 #ifndef Q_WS_QWS
       
   266                 if (w->testAttribute(Qt::WA_WState_Created))
       
   267                     w->d_func()->setWSGeometry();
       
   268 #endif
       
   269                 w->d_func()->setDirtyOpaqueRegion();
       
   270                 QMoveEvent e(r.topLeft(), oldp);
       
   271                 QApplication::sendEvent(w, &e);
       
   272             }
       
   273         }
       
   274     }
       
   275 }
       
   276 
       
   277 QInputContext *QWidgetPrivate::inputContext() const
       
   278 {
       
   279 #ifndef QT_NO_IM
       
   280     if (ic)
       
   281         return ic;
       
   282 #endif
       
   283     return qApp->inputContext();
       
   284 }
       
   285 
       
   286 /*!
       
   287     This function returns the QInputContext for this widget. By
       
   288     default the input context is inherited from the widgets
       
   289     parent. For toplevels it is inherited from QApplication.
       
   290 
       
   291     You can override this and set a special input context for this
       
   292     widget by using the setInputContext() method.
       
   293 
       
   294     \sa setInputContext()
       
   295 */
       
   296 QInputContext *QWidget::inputContext()
       
   297 {
       
   298     Q_D(QWidget);
       
   299     if (!testAttribute(Qt::WA_InputMethodEnabled))
       
   300         return 0;
       
   301 
       
   302     return d->inputContext();
       
   303 }
       
   304 
       
   305 /*!
       
   306   This function sets the input context \a context
       
   307   on this widget.
       
   308 
       
   309   \sa inputContext()
       
   310 */
       
   311 void QWidget::setInputContext(QInputContext *context)
       
   312 {
       
   313     Q_D(QWidget);
       
   314     if (!testAttribute(Qt::WA_InputMethodEnabled))
       
   315         return;
       
   316 #ifndef QT_NO_IM
       
   317     if (d->ic)
       
   318         delete d->ic;
       
   319     d->ic = context;
       
   320 #endif
       
   321 }
       
   322 
       
   323 
       
   324 /*!
       
   325     \obsolete
       
   326 
       
   327     This function can be called on the widget that currently has focus
       
   328     to reset the input method operating on it.
       
   329 
       
   330     This function is providing for convenience, instead you should use
       
   331     \l{QInputContext::}{reset()} on the input context that was
       
   332     returned by inputContext().
       
   333 
       
   334     \sa QInputContext, inputContext(), QInputContext::reset()
       
   335 */
       
   336 void QWidget::resetInputContext()
       
   337 {
       
   338     if (!hasFocus())
       
   339         return;
       
   340 #ifndef QT_NO_IM
       
   341     QInputContext *qic = this->inputContext();
       
   342     if(qic)
       
   343         qic->reset();
       
   344 #endif // QT_NO_IM
       
   345 }
       
   346 
       
   347 #ifdef QT_KEYPAD_NAVIGATION
       
   348 QPointer<QWidget> QWidgetPrivate::editingWidget;
       
   349 
       
   350 /*!
       
   351     Returns true if this widget currently has edit focus; otherwise false.
       
   352 
       
   353     This feature is only available in Qt for Embedded Linux.
       
   354 
       
   355     \sa setEditFocus(), QApplication::keypadNavigationEnabled()
       
   356 */
       
   357 bool QWidget::hasEditFocus() const
       
   358 {
       
   359     const QWidget* w = this;
       
   360     while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
       
   361         w = w->d_func()->extra->focus_proxy;
       
   362     return QWidgetPrivate::editingWidget == w;
       
   363 }
       
   364 
       
   365 /*!
       
   366     \fn void QWidget::setEditFocus(bool enable)
       
   367 
       
   368     If \a enable is true, make this widget have edit focus, in which
       
   369     case Qt::Key_Up and Qt::Key_Down will be delivered to the widget
       
   370     normally; otherwise, Qt::Key_Up and Qt::Key_Down are used to
       
   371     change focus.
       
   372 
       
   373     This feature is only available in Qt for Embedded Linux and Qt
       
   374     for Symbian.
       
   375 
       
   376     \sa hasEditFocus(), QApplication::keypadNavigationEnabled()
       
   377 */
       
   378 void QWidget::setEditFocus(bool on)
       
   379 {
       
   380     QWidget *f = this;
       
   381     while (f->d_func()->extra && f->d_func()->extra->focus_proxy)
       
   382         f = f->d_func()->extra->focus_proxy;
       
   383 
       
   384     if (QWidgetPrivate::editingWidget && QWidgetPrivate::editingWidget != f)
       
   385         QWidgetPrivate::editingWidget->setEditFocus(false);
       
   386 
       
   387     if (on && !f->hasFocus())
       
   388         f->setFocus();
       
   389 
       
   390     if ((!on && !QWidgetPrivate::editingWidget)
       
   391         || (on && QWidgetPrivate::editingWidget == f)) {
       
   392         return;
       
   393     }
       
   394 
       
   395     if (!on && QWidgetPrivate::editingWidget == f) {
       
   396         QWidgetPrivate::editingWidget = 0;
       
   397         QEvent event(QEvent::LeaveEditFocus);
       
   398         QApplication::sendEvent(f, &event);
       
   399         QApplication::sendEvent(f->style(), &event);
       
   400     } else if (on) {
       
   401         QWidgetPrivate::editingWidget = f;
       
   402         QEvent event(QEvent::EnterEditFocus);
       
   403         QApplication::sendEvent(f, &event);
       
   404         QApplication::sendEvent(f->style(), &event);
       
   405     }
       
   406 }
       
   407 #endif
       
   408 
       
   409 /*!
       
   410     \property QWidget::autoFillBackground
       
   411     \brief whether the widget background is filled automatically
       
   412     \since 4.1
       
   413 
       
   414     If enabled, this property will cause Qt to fill the background of the
       
   415     widget before invoking the paint event. The color used is defined by the
       
   416     QPalette::Window color role from the widget's \l{QPalette}{palette}.
       
   417 
       
   418     In addition, Windows are always filled with QPalette::Window, unless the
       
   419     WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.
       
   420 
       
   421     This property cannot be turned off (i.e., set to false) if a widget's
       
   422     parent has a static gradient for its background.
       
   423 
       
   424     \warning Use this property with caution in conjunction with
       
   425     \l{Qt Style Sheets}. When a widget has a style sheet with a valid
       
   426     background or a border-image, this property is automatically disabled.
       
   427 
       
   428     By default, this property is false.
       
   429 
       
   430     \sa Qt::WA_OpaquePaintEvent, Qt::WA_NoSystemBackground,
       
   431     {QWidget#Transparency and Double Buffering}{Transparency and Double Buffering}
       
   432 */
       
   433 bool QWidget::autoFillBackground() const
       
   434 {
       
   435     Q_D(const QWidget);
       
   436     return d->extra && d->extra->autoFillBackground;
       
   437 }
       
   438 
       
   439 void QWidget::setAutoFillBackground(bool enabled)
       
   440 {
       
   441     Q_D(QWidget);
       
   442     if (!d->extra)
       
   443         d->createExtra();
       
   444     if (d->extra->autoFillBackground == enabled)
       
   445         return;
       
   446 
       
   447     d->extra->autoFillBackground = enabled;
       
   448     d->updateIsOpaque();
       
   449     update();
       
   450     d->updateIsOpaque();
       
   451 }
       
   452 
       
   453 /*!
       
   454     \class QWidget
       
   455     \brief The QWidget class is the base class of all user interface objects.
       
   456 
       
   457     \ingroup basicwidgets
       
   458 
       
   459 
       
   460     The widget is the atom of the user interface: it receives mouse, keyboard
       
   461     and other events from the window system, and paints a representation of
       
   462     itself on the screen. Every widget is rectangular, and they are sorted in a
       
   463     Z-order. A widget is clipped by its parent and by the widgets in front of
       
   464     it.
       
   465 
       
   466     A widget that is not embedded in a parent widget is called a window.
       
   467     Usually, windows have a frame and a title bar, although it is also possible
       
   468     to create windows without such decoration using suitable
       
   469     \l{Qt::WindowFlags}{window flags}). In Qt, QMainWindow and the various
       
   470     subclasses of QDialog are the most common window types.
       
   471 
       
   472     Every widget's constructor accepts one or two standard arguments:
       
   473 
       
   474     \list 1
       
   475         \i  \c{QWidget *parent = 0} is the parent of the new widget. If it is 0
       
   476             (the default), the new widget will be a window. If not, it will be
       
   477             a child of \e parent, and be constrained by \e parent's geometry
       
   478             (unless you specify Qt::Window as window flag).
       
   479         \i  \c{Qt::WindowFlags f = 0} (where available) sets the window flags;
       
   480             the default is suitable for almost all widgets, but to get, for
       
   481             example, a window without a window system frame, you must use
       
   482             special flags.
       
   483     \endlist
       
   484 
       
   485     QWidget has many member functions, but some of them have little direct
       
   486     functionality; for example, QWidget has a font property, but never uses
       
   487     this itself. There are many subclasses which provide real functionality,
       
   488     such as QLabel, QPushButton, QListWidget, and QTabWidget.
       
   489 
       
   490 
       
   491     \section1 Top-Level and Child Widgets
       
   492 
       
   493     A widget without a parent widget is always an independent window (top-level
       
   494     widget). For these widgets, setWindowTitle() and setWindowIcon() set the
       
   495     title bar and icon respectively.
       
   496 
       
   497     Non-window widgets are child widgets, displayed within their parent
       
   498     widgets. Most widgets in Qt are mainly useful as child widgets. For
       
   499     example, it is possible to display a button as a top-level window, but most
       
   500     people prefer to put their buttons inside other widgets, such as QDialog.
       
   501 
       
   502     \image parent-child-widgets.png A parent widget containing various child widgets.
       
   503 
       
   504     The diagram above shows a QGroupBox widget being used to hold various child
       
   505     widgets in a layout provided by QGridLayout. The QLabel child widgets have
       
   506     been outlined to indicate their full sizes.
       
   507 
       
   508     If you want to use a QWidget to hold child widgets you will usually want to
       
   509     add a layout to the parent QWidget. See \l{Layout Management} for more
       
   510     information.
       
   511 
       
   512 
       
   513     \section1 Composite Widgets
       
   514 
       
   515     When a widget is used as a container to group a number of child widgets, it
       
   516     is known as a composite widget. These can be created by constructing a
       
   517     widget with the required visual properties - a QFrame, for example - and
       
   518     adding child widgets to it, usually managed by a layout. The above diagram
       
   519     shows such a composite widget that was created using \l{Qt Designer}.
       
   520 
       
   521     Composite widgets can also be created by subclassing a standard widget,
       
   522     such as QWidget or QFrame, and adding the necessary layout and child
       
   523     widgets in the constructor of the subclass. Many of the \l{Qt Examples}
       
   524     {examples provided with Qt} use this approach, and it is also covered in
       
   525     the Qt \l{Tutorials}.
       
   526 
       
   527 
       
   528     \section1 Custom Widgets and Painting
       
   529 
       
   530     Since QWidget is a subclass of QPaintDevice, subclasses can be used to
       
   531     display custom content that is composed using a series of painting
       
   532     operations with an instance of the QPainter class. This approach contrasts
       
   533     with the canvas-style approach used by the \l{Graphics View}
       
   534     {Graphics View Framework} where items are added to a scene by the
       
   535     application and are rendered by the framework itself.
       
   536 
       
   537     Each widget performs all painting operations from within its paintEvent()
       
   538     function. This is called whenever the widget needs to be redrawn, either
       
   539     as a result of some external change or when requested by the application.
       
   540 
       
   541     The \l{widgets/analogclock}{Analog Clock example} shows how a simple widget
       
   542     can handle paint events.
       
   543 
       
   544 
       
   545     \section1 Size Hints and Size Policies
       
   546 
       
   547     When implementing a new widget, it is almost always useful to reimplement
       
   548     sizeHint() to provide a reasonable default size for the widget and to set
       
   549     the correct size policy with setSizePolicy().
       
   550 
       
   551     By default, composite widgets which do not provide a size hint will be
       
   552     sized according to the space requirements of their child widgets.
       
   553 
       
   554     The size policy lets you supply good default behavior for the layout
       
   555     management system, so that other widgets can contain and manage yours
       
   556     easily. The default size policy indicates that the size hint represents
       
   557     the preferred size of the widget, and this is often good enough for many
       
   558     widgets.
       
   559 
       
   560     \note The size of top-level widgets are constrained to 2/3 of the desktop's
       
   561     height and width. You can resize() the widget manually if these bounds are
       
   562     inadequate.
       
   563 
       
   564 
       
   565     \section1 Events
       
   566 
       
   567     Widgets respond to events that are typically caused by user actions. Qt
       
   568     delivers events to widgets by calling specific event handler functions with
       
   569     instances of QEvent subclasses containing information about each event.
       
   570 
       
   571     If your widget only contains child widgets, you probably do not need to
       
   572     implement any event handlers. If you want to detect a mouse click in a
       
   573     child widget call the child's underMouse() function inside the widget's
       
   574     mousePressEvent().
       
   575 
       
   576     The \l{widgets/scribble}{Scribble example} implements a wider set of
       
   577     events to handle mouse movement, button presses, and window resizing.
       
   578 
       
   579     You will need to supply the behavior and content for your own widgets, but
       
   580     here is a brief overview of the events that are relevant to QWidget,
       
   581     starting with the most common ones:
       
   582 
       
   583     \list
       
   584         \i  paintEvent() is called whenever the widget needs to be repainted.
       
   585             Every widget displaying custom content must implement it. Painting
       
   586             using a QPainter can only take place in a paintEvent() or a
       
   587             function called by a paintEvent().
       
   588         \i  resizeEvent() is called when the widget has been resized.
       
   589         \i  mousePressEvent() is called when a mouse button is pressed while
       
   590             the mouse cursor is inside the widget, or when the widget has
       
   591             grabbed the mouse using grabMouse(). Pressing the mouse without
       
   592             releasing it is effectively the same as calling grabMouse().
       
   593         \i  mouseReleaseEvent() is called when a mouse button is released. A
       
   594             widget receives mouse release events when it has received the
       
   595             corresponding mouse press event. This means that if the user
       
   596             presses the mouse inside \e your widget, then drags the mouse
       
   597             somewhere else before releasing the mouse button, \e your widget
       
   598             receives the release event. There is one exception: if a popup menu
       
   599             appears while the mouse button is held down, this popup immediately
       
   600             steals the mouse events.
       
   601         \i  mouseDoubleClickEvent() is called when the user double-clicks in
       
   602             the widget. If the user double-clicks, the widget receives a mouse
       
   603             press event, a mouse release event and finally this event instead
       
   604             of a second mouse press event. (Some mouse move events may also be
       
   605             received if the mouse is not held steady during this operation.) It
       
   606             is \e{not possible} to distinguish a click from a double-click
       
   607             until the second click arrives. (This is one reason why most GUI
       
   608             books recommend that double-clicks be an extension of
       
   609             single-clicks, rather than trigger a different action.)
       
   610     \endlist
       
   611 
       
   612     Widgets that accept keyboard input need to reimplement a few more event
       
   613     handlers:
       
   614 
       
   615     \list
       
   616         \i  keyPressEvent() is called whenever a key is pressed, and again when
       
   617             a key has been held down long enough for it to auto-repeat. The
       
   618             \key Tab and \key Shift+Tab keys are only passed to the widget if
       
   619             they are not used by the focus-change mechanisms. To force those
       
   620             keys to be processed by your widget, you must reimplement
       
   621             QWidget::event().
       
   622         \i  focusInEvent() is called when the widget gains keyboard focus
       
   623             (assuming you have called setFocusPolicy()). Well-behaved widgets
       
   624             indicate that they own the keyboard focus in a clear but discreet
       
   625             way.
       
   626         \i  focusOutEvent() is called when the widget loses keyboard focus.
       
   627     \endlist
       
   628 
       
   629     You may be required to also reimplement some of the less common event
       
   630     handlers:
       
   631 
       
   632     \list
       
   633         \i  mouseMoveEvent() is called whenever the mouse moves while a mouse
       
   634             button is held down. This can be useful during drag and drop
       
   635             operations. If you call setMouseTracking(true), you get mouse move
       
   636             events even when no buttons are held down. (See also the \l{Drag
       
   637             and Drop} guide.)
       
   638         \i  keyReleaseEvent() is called whenever a key is released and while it
       
   639             is held down (if the key is auto-repeating). In that case, the
       
   640             widget will receive a pair of key release and key press event for
       
   641             every repeat. The \key Tab and \key Shift+Tab keys are only passed
       
   642             to the widget if they are not used by the focus-change mechanisms.
       
   643             To force those keys to be processed by your widget, you must
       
   644             reimplement QWidget::event().
       
   645         \i  wheelEvent() is called whenever the user turns the mouse wheel
       
   646             while the widget has the focus.
       
   647         \i  enterEvent() is called when the mouse enters the widget's screen
       
   648             space. (This excludes screen space owned by any of the widget's
       
   649             children.)
       
   650         \i  leaveEvent() is called when the mouse leaves the widget's screen
       
   651             space. If the mouse enters a child widget it will not cause a
       
   652             leaveEvent().
       
   653         \i  moveEvent() is called when the widget has been moved relative to
       
   654             its parent.
       
   655         \i  closeEvent() is called when the user closes the widget (or when
       
   656             close() is called).
       
   657     \endlist
       
   658 
       
   659     There are also some rather obscure events described in the documentation
       
   660     for QEvent::Type. To handle these events, you need to reimplement event()
       
   661     directly.
       
   662 
       
   663     The default implementation of event() handles \key Tab and \key Shift+Tab
       
   664     (to move the keyboard focus), and passes on most of the other events to
       
   665     one of the more specialized handlers above.
       
   666 
       
   667     Events and the mechanism used to deliver them are covered in the
       
   668     \l{Events and Event Filters} document.
       
   669 
       
   670     \section1 Groups of Functions and Properties
       
   671 
       
   672     \table
       
   673     \header \i Context \i Functions and Properties
       
   674 
       
   675     \row \i Window functions \i
       
   676         show(),
       
   677         hide(),
       
   678         raise(),
       
   679         lower(),
       
   680         close().
       
   681 
       
   682     \row \i Top-level windows \i
       
   683         \l windowModified, \l windowTitle, \l windowIcon, \l windowIconText,
       
   684         \l isActiveWindow, activateWindow(), \l minimized, showMinimized(),
       
   685         \l maximized, showMaximized(), \l fullScreen, showFullScreen(),
       
   686         showNormal().
       
   687 
       
   688     \row \i Window contents \i
       
   689         update(),
       
   690         repaint(),
       
   691         scroll().
       
   692 
       
   693     \row \i Geometry \i
       
   694         \l pos, x(), y(), \l rect, \l size, width(), height(), move(), resize(),
       
   695         \l sizePolicy, sizeHint(), minimumSizeHint(),
       
   696         updateGeometry(), layout(),
       
   697         \l frameGeometry, \l geometry, \l childrenRect, \l childrenRegion,
       
   698         adjustSize(),
       
   699         mapFromGlobal(), mapToGlobal(),
       
   700         mapFromParent(), mapToParent(),
       
   701         \l maximumSize, \l minimumSize, \l sizeIncrement,
       
   702         \l baseSize, setFixedSize()
       
   703 
       
   704     \row \i Mode \i
       
   705         \l visible, isVisibleTo(),
       
   706         \l enabled, isEnabledTo(),
       
   707         \l modal,
       
   708         isWindow(),
       
   709         \l mouseTracking,
       
   710         \l updatesEnabled,
       
   711         visibleRegion().
       
   712 
       
   713     \row \i Look and feel \i
       
   714         style(),
       
   715         setStyle(),
       
   716         \l styleSheet,
       
   717         \l cursor,
       
   718         \l font,
       
   719         \l palette,
       
   720         backgroundRole(), setBackgroundRole(),
       
   721         fontInfo(), fontMetrics().
       
   722 
       
   723     \row \i Keyboard focus functions \i
       
   724         \l focus, \l focusPolicy,
       
   725         setFocus(), clearFocus(), setTabOrder(), setFocusProxy(),
       
   726         focusNextChild(), focusPreviousChild().
       
   727 
       
   728     \row \i Mouse and keyboard grabbing \i
       
   729         grabMouse(), releaseMouse(),
       
   730         grabKeyboard(), releaseKeyboard(),
       
   731         mouseGrabber(), keyboardGrabber().
       
   732 
       
   733     \row \i Event handlers \i
       
   734         event(),
       
   735         mousePressEvent(),
       
   736         mouseReleaseEvent(),
       
   737         mouseDoubleClickEvent(),
       
   738         mouseMoveEvent(),
       
   739         keyPressEvent(),
       
   740         keyReleaseEvent(),
       
   741         focusInEvent(),
       
   742         focusOutEvent(),
       
   743         wheelEvent(),
       
   744         enterEvent(),
       
   745         leaveEvent(),
       
   746         paintEvent(),
       
   747         moveEvent(),
       
   748         resizeEvent(),
       
   749         closeEvent(),
       
   750         dragEnterEvent(),
       
   751         dragMoveEvent(),
       
   752         dragLeaveEvent(),
       
   753         dropEvent(),
       
   754         childEvent(),
       
   755         showEvent(),
       
   756         hideEvent(),
       
   757         customEvent().
       
   758         changeEvent(),
       
   759 
       
   760     \row \i System functions \i
       
   761         parentWidget(), window(), setParent(), winId(),
       
   762         find(), metric().
       
   763 
       
   764     \row \i Interactive help \i
       
   765         setToolTip(), setWhatsThis()
       
   766 
       
   767     \endtable
       
   768 
       
   769 
       
   770     \section1 Widget Style Sheets
       
   771 
       
   772     In addition to the standard widget styles for each platform, widgets can
       
   773     also be styled according to rules specified in a \l{styleSheet}
       
   774     {style sheet}. This feature enables you to customize the appearance of
       
   775     specific widgets to provide visual cues to users about their purpose. For
       
   776     example, a button could be styled in a particular way to indicate that it
       
   777     performs a destructive action.
       
   778 
       
   779     The use of widget style sheets is described in more detail in the
       
   780     \l{Qt Style Sheets} document.
       
   781 
       
   782 
       
   783     \section1 Transparency and Double Buffering
       
   784 
       
   785     Since Qt 4.0, QWidget automatically double-buffers its painting, so there
       
   786     is no need to write double-buffering code in paintEvent() to avoid
       
   787     flicker.
       
   788 
       
   789     Since Qt 4.1, the Qt::WA_ContentsPropagated widget attribute has been
       
   790     deprecated. Instead, the contents of parent widgets are propagated by
       
   791     default to each of their children as long as Qt::WA_PaintOnScreen is not
       
   792     set. Custom widgets can be written to take advantage of this feature by
       
   793     updating irregular regions (to create non-rectangular child widgets), or
       
   794     painting with colors that have less than full alpha component. The
       
   795     following diagram shows how attributes and properties of a custom widget
       
   796     can be fine-tuned to achieve different effects.
       
   797 
       
   798     \image propagation-custom.png
       
   799 
       
   800     In the above diagram, a semi-transparent rectangular child widget with an
       
   801     area removed is constructed and added to a parent widget (a QLabel showing
       
   802     a pixmap). Then, different properties and widget attributes are set to
       
   803     achieve different effects:
       
   804 
       
   805     \list
       
   806         \i  The left widget has no additional properties or widget attributes
       
   807             set. This default state suits most custom widgets using
       
   808             transparency, are irregularly-shaped, or do not paint over their
       
   809             entire area with an opaque brush.
       
   810         \i  The center widget has the \l autoFillBackground property set. This
       
   811             property is used with custom widgets that rely on the widget to
       
   812             supply a default background, and do not paint over their entire
       
   813             area with an opaque brush.
       
   814         \i  The right widget has the Qt::WA_OpaquePaintEvent widget attribute
       
   815             set. This indicates that the widget will paint over its entire area
       
   816             with opaque colors. The widget's area will initially be
       
   817             \e{uninitialized}, represented in the diagram with a red diagonal
       
   818             grid pattern that shines through the overpainted area. The
       
   819             Qt::WA_OpaquePaintArea attribute is useful for widgets that need to
       
   820             paint their own specialized contents quickly and do not need a
       
   821             default filled background.
       
   822     \endlist
       
   823 
       
   824     To rapidly update custom widgets with simple background colors, such as
       
   825     real-time plotting or graphing widgets, it is better to define a suitable
       
   826     background color (using setBackgroundRole() with the
       
   827     QPalette::Window role), set the \l autoFillBackground property, and only
       
   828     implement the necessary drawing functionality in the widget's paintEvent().
       
   829 
       
   830     To rapidly update custom widgets that constantly paint over their entire
       
   831     areas with opaque content, e.g., video streaming widgets, it is better to
       
   832     set the widget's Qt::WA_OpaquePaintEvent, avoiding any unnecessary overhead
       
   833     associated with repainting the widget's background.
       
   834 
       
   835     If a widget has both the Qt::WA_OpaquePaintEvent widget attribute \e{and}
       
   836     the \l autoFillBackground property set, the Qt::WA_OpaquePaintEvent
       
   837     attribute takes precedence. Depending on your requirements, you should
       
   838     choose either one of them.
       
   839 
       
   840     Since Qt 4.1, the contents of parent widgets are also propagated to
       
   841     standard Qt widgets. This can lead to some unexpected results if the
       
   842     parent widget is decorated in a non-standard way, as shown in the diagram
       
   843     below.
       
   844 
       
   845     \image propagation-standard.png
       
   846 
       
   847     The scope for customizing the painting behavior of standard Qt widgets,
       
   848     without resorting to subclassing, is slightly less than that possible for
       
   849     custom widgets. Usually, the desired appearance of a standard widget can be
       
   850     achieved by setting its \l autoFillBackground property.
       
   851 
       
   852 
       
   853     \section1 Creating Translucent Windows
       
   854 
       
   855     Since Qt 4.5, it has been possible to create windows with translucent regions
       
   856     on window systems that support compositing.
       
   857 
       
   858     To enable this feature in a top-level widget, set its Qt::WA_TranslucentBackground
       
   859     attribute with setAttribute() and ensure that its background is painted with
       
   860     non-opaque colors in the regions you want to be partially transparent.
       
   861 
       
   862     Platform notes:
       
   863 
       
   864     \list
       
   865     \o X11: This feature relies on the use of an X server that supports ARGB visuals
       
   866     and a compositing window manager.
       
   867     \o Windows: The widget needs to have the Qt::FramelessWindowHint window flag set
       
   868     for the translucency to work.
       
   869     \endlist
       
   870 
       
   871 
       
   872     \section1 Native Widgets vs Alien Widgets
       
   873 
       
   874     Introduced in Qt 4.4, alien widgets are widgets unknown to the windowing
       
   875     system. They do not have a native window handle associated with them. This
       
   876     feature significantly speeds up widget painting, resizing, and removes flicker.
       
   877 
       
   878     Should you require the old behavior with native windows, you can choose
       
   879     one of the following options:
       
   880 
       
   881     \list 1
       
   882         \i  Use the \c{QT_USE_NATIVE_WINDOWS=1} in your environment.
       
   883         \i  Set the Qt::AA_NativeWindows attribute on your application. All
       
   884             widgets will be native widgets.
       
   885         \i  Set the Qt::WA_NativeWindow attribute on widgets: The widget itself
       
   886             and all of its ancestors will become native (unless
       
   887             Qt::WA_DontCreateNativeAncestors is set).
       
   888         \i  Call QWidget::winId to enforce a native window (this implies 3).
       
   889         \i  Set the Qt::WA_PaintOnScreen attribute to enforce a native window
       
   890             (this implies 3).
       
   891     \endlist
       
   892 
       
   893     \sa QEvent, QPainter, QGridLayout, QBoxLayout
       
   894 
       
   895     \section1 Softkeys
       
   896 
       
   897     Since Qt 4.6, Softkeys are usually physical keys on a device that have a corresponding label or
       
   898     other visual representation on the screen that is generally located next to its
       
   899     physical counterpart. They are most often found on mobile phone platforms. In
       
   900     modern touch based user interfaces it is also possible to have softkeys that do
       
   901     not correspond to any physical keys. Softkeys differ from other onscreen labels
       
   902     in that they are contextual.
       
   903 
       
   904     In Qt, contextual softkeys are added to a widget by calling addAction() and
       
   905     passing a \c QAction with a softkey role set on it. When the widget
       
   906     containing the softkey actions has focus, its softkeys should appear in
       
   907     the user interface. Softkeys are discovered by traversing the widget
       
   908     hierarchy so it is possible to define a single set of softkeys that are
       
   909     present at all times by calling addAction() for a given top level widget.
       
   910 
       
   911     On some platforms, this concept overlaps with \c QMenuBar such that if no
       
   912     other softkeys are found and the top level widget is a QMainWindow containing
       
   913     a QMenuBar, the menubar actions may appear on one of the softkeys.
       
   914 
       
   915     Note: Currently softkeys are only supported on the Symbian Platform.
       
   916 
       
   917     \sa addAction(), QAction, QMenuBar
       
   918 
       
   919 */
       
   920 
       
   921 QWidgetMapper *QWidgetPrivate::mapper = 0;          // widget with wid
       
   922 QWidgetSet *QWidgetPrivate::allWidgets = 0;         // widgets with no wid
       
   923 
       
   924 
       
   925 /*****************************************************************************
       
   926   QWidget utility functions
       
   927  *****************************************************************************/
       
   928 
       
   929 QRegion qt_dirtyRegion(QWidget *widget)
       
   930 {
       
   931     if (!widget)
       
   932         return QRegion();
       
   933 
       
   934     QWidgetBackingStore *bs = qt_widget_private(widget)->maybeBackingStore();
       
   935     if (!bs)
       
   936         return QRegion();
       
   937 
       
   938     return bs->dirtyRegion(widget);
       
   939 }
       
   940 
       
   941 /*****************************************************************************
       
   942   QWidget member functions
       
   943  *****************************************************************************/
       
   944 
       
   945 /*
       
   946     Widget state flags:
       
   947   \list
       
   948   \i Qt::WA_WState_Created The widget has a valid winId().
       
   949   \i Qt::WA_WState_Visible The widget is currently visible.
       
   950   \i Qt::WA_WState_Hidden The widget is hidden, i.e. it won't
       
   951   become visible unless you call show() on it. Qt::WA_WState_Hidden
       
   952   implies !Qt::WA_WState_Visible.
       
   953   \i Qt::WA_WState_CompressKeys Compress keyboard events.
       
   954   \i Qt::WA_WState_BlockUpdates Repaints and updates are disabled.
       
   955   \i Qt::WA_WState_InPaintEvent Currently processing a paint event.
       
   956   \i Qt::WA_WState_Reparented The widget has been reparented.
       
   957   \i Qt::WA_WState_ConfigPending A configuration (resize/move) event is pending.
       
   958   \i Qt::WA_WState_DND (Deprecated) The widget supports drag and drop, see setAcceptDrops().
       
   959   \endlist
       
   960 */
       
   961 
       
   962 struct QWidgetExceptionCleaner
       
   963 {
       
   964     /* this cleans up when the constructor throws an exception */
       
   965     static inline void cleanup(QWidget *that, QWidgetPrivate *d)
       
   966     {
       
   967 #ifdef QT_NO_EXCEPTIONS
       
   968         Q_UNUSED(that);
       
   969         Q_UNUSED(d);
       
   970 #else
       
   971         QWidgetPrivate::allWidgets->remove(that);
       
   972         if (d->focus_next != that) {
       
   973             if (d->focus_next)
       
   974                 d->focus_next->d_func()->focus_prev = d->focus_prev;
       
   975             if (d->focus_prev)
       
   976                 d->focus_prev->d_func()->focus_next = d->focus_next;
       
   977         }
       
   978 #endif
       
   979     }
       
   980 };
       
   981 
       
   982 /*!
       
   983     Constructs a widget which is a child of \a parent, with  widget
       
   984     flags set to \a f.
       
   985 
       
   986     If \a parent is 0, the new widget becomes a window. If
       
   987     \a parent is another widget, this widget becomes a child window
       
   988     inside \a parent. The new widget is deleted when its \a parent is
       
   989     deleted.
       
   990 
       
   991     The widget flags argument, \a f, is normally 0, but it can be set
       
   992     to customize the frame of a window (i.e. \a
       
   993     parent must be 0). To customize the frame, use a value composed
       
   994     from the bitwise OR of any of the \l{Qt::WindowFlags}{window flags}.
       
   995 
       
   996     If you add a child widget to an already visible widget you must
       
   997     explicitly show the child to make it visible.
       
   998 
       
   999     Note that the X11 version of Qt may not be able to deliver all
       
  1000     combinations of style flags on all systems. This is because on
       
  1001     X11, Qt can only ask the window manager, and the window manager
       
  1002     can override the application's settings. On Windows, Qt can set
       
  1003     whatever flags you want.
       
  1004 
       
  1005     \sa windowFlags
       
  1006 */
       
  1007 
       
  1008 QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
       
  1009     : QObject(*new QWidgetPrivate, 0), QPaintDevice()
       
  1010 {
       
  1011     QT_TRY {
       
  1012         d_func()->init(parent, f);
       
  1013     } QT_CATCH(...) {
       
  1014         QWidgetExceptionCleaner::cleanup(this, d_func());
       
  1015         QT_RETHROW;
       
  1016     }
       
  1017 }
       
  1018 
       
  1019 #ifdef QT3_SUPPORT
       
  1020 /*!
       
  1021     \overload
       
  1022     \obsolete
       
  1023  */
       
  1024 QWidget::QWidget(QWidget *parent, const char *name, Qt::WindowFlags f)
       
  1025     : QObject(*new QWidgetPrivate, 0), QPaintDevice()
       
  1026 {
       
  1027     QT_TRY {
       
  1028         d_func()->init(parent , f);
       
  1029         setObjectName(QString::fromAscii(name));
       
  1030     } QT_CATCH(...) {
       
  1031         QWidgetExceptionCleaner::cleanup(this, d_func());
       
  1032         QT_RETHROW;
       
  1033     }
       
  1034 }
       
  1035 #endif
       
  1036 
       
  1037 /*! \internal
       
  1038 */
       
  1039 QWidget::QWidget(QWidgetPrivate &dd, QWidget* parent, Qt::WindowFlags f)
       
  1040     : QObject(dd, 0), QPaintDevice()
       
  1041 {
       
  1042     Q_D(QWidget);
       
  1043     QT_TRY {
       
  1044         d->init(parent, f);
       
  1045     } QT_CATCH(...) {
       
  1046         QWidgetExceptionCleaner::cleanup(this, d_func());
       
  1047         QT_RETHROW;
       
  1048     }
       
  1049 }
       
  1050 
       
  1051 /*!
       
  1052     \internal
       
  1053 */
       
  1054 int QWidget::devType() const
       
  1055 {
       
  1056     return QInternal::Widget;
       
  1057 }
       
  1058 
       
  1059 
       
  1060 //### w is a "this" ptr, passed as a param because QWorkspace needs special logic
       
  1061 void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w)
       
  1062 {
       
  1063     bool customize =  (flags & (Qt::CustomizeWindowHint
       
  1064             | Qt::FramelessWindowHint
       
  1065             | Qt::WindowTitleHint
       
  1066             | Qt::WindowSystemMenuHint
       
  1067             | Qt::WindowMinimizeButtonHint
       
  1068             | Qt::WindowMaximizeButtonHint
       
  1069             | Qt::WindowCloseButtonHint
       
  1070             | Qt::WindowContextHelpButtonHint));
       
  1071 
       
  1072     uint type = (flags & Qt::WindowType_Mask);
       
  1073 
       
  1074     if ((type == Qt::Widget || type == Qt::SubWindow) && w && !w->parent()) {
       
  1075         type = Qt::Window;
       
  1076         flags |= Qt::Window;
       
  1077     }
       
  1078 
       
  1079     if (flags & Qt::CustomizeWindowHint) {
       
  1080         // modify window flags to make them consistent.
       
  1081         // Only enable this on non-Mac platforms. Since the old way of doing this would
       
  1082         // interpret WindowSystemMenuHint as a close button and we can't change that behavior
       
  1083         // we can't just add this in.
       
  1084 #ifndef Q_WS_MAC
       
  1085         if (flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint)) {
       
  1086             flags |= Qt::WindowSystemMenuHint;
       
  1087 #else
       
  1088         if (flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint
       
  1089                      | Qt::WindowSystemMenuHint)) {
       
  1090 #endif
       
  1091             flags |= Qt::WindowTitleHint;
       
  1092             flags &= ~Qt::FramelessWindowHint;
       
  1093         }
       
  1094     } else if (customize && !(flags & Qt::FramelessWindowHint)) {
       
  1095         // if any of the window hints that affect the titlebar are set
       
  1096         // and the window is supposed to have frame, we add a titlebar
       
  1097         // and system menu by default.
       
  1098         flags |= Qt::WindowSystemMenuHint;
       
  1099         flags |= Qt::WindowTitleHint;
       
  1100     }
       
  1101     if (customize)
       
  1102         ; // don't modify window flags if the user explicitely set them.
       
  1103     else if (type == Qt::Dialog || type == Qt::Sheet)
       
  1104 #ifndef Q_WS_WINCE
       
  1105         flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowContextHelpButtonHint | Qt::WindowCloseButtonHint;
       
  1106 #else
       
  1107         flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
       
  1108 #endif
       
  1109     else if (type == Qt::Tool)
       
  1110         flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
       
  1111     else
       
  1112         flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint;
       
  1113 
       
  1114 
       
  1115 }
       
  1116 
       
  1117 void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
       
  1118 {
       
  1119     Q_Q(QWidget);
       
  1120     if (QApplication::type() == QApplication::Tty)
       
  1121         qFatal("QWidget: Cannot create a QWidget when no GUI is being used");
       
  1122 
       
  1123     Q_ASSERT(allWidgets);
       
  1124     allWidgets->insert(q);
       
  1125 
       
  1126     QWidget *desktopWidget = 0;
       
  1127     if (parentWidget && parentWidget->windowType() == Qt::Desktop) {
       
  1128         desktopWidget = parentWidget;
       
  1129         parentWidget = 0;
       
  1130     }
       
  1131 
       
  1132     q->data = &data;
       
  1133 
       
  1134 #ifndef QT_NO_THREAD
       
  1135     if (!parent) {
       
  1136         Q_ASSERT_X(q->thread() == qApp->thread(), "QWidget",
       
  1137                    "Widgets must be created in the GUI thread.");
       
  1138     }
       
  1139 #endif
       
  1140 
       
  1141 #if defined(Q_WS_X11)
       
  1142     if (desktopWidget) {
       
  1143         // make sure the widget is created on the same screen as the
       
  1144         // programmer specified desktop widget
       
  1145         xinfo = desktopWidget->d_func()->xinfo;
       
  1146     }
       
  1147 #else
       
  1148     Q_UNUSED(desktopWidget);
       
  1149 #endif
       
  1150 
       
  1151     data.fstrut_dirty = true;
       
  1152 
       
  1153     data.winid = 0;
       
  1154     data.widget_attributes = 0;
       
  1155     data.window_flags = f;
       
  1156     data.window_state = 0;
       
  1157     data.focus_policy = 0;
       
  1158     data.context_menu_policy = Qt::DefaultContextMenu;
       
  1159     data.window_modality = Qt::NonModal;
       
  1160 
       
  1161     data.sizehint_forced = 0;
       
  1162     data.is_closing = 0;
       
  1163     data.in_show = 0;
       
  1164     data.in_set_window_state = 0;
       
  1165     data.in_destructor = false;
       
  1166 
       
  1167     // Widgets with Qt::MSWindowsOwnDC (typically QGLWidget) must have a window handle.
       
  1168     if (f & Qt::MSWindowsOwnDC)
       
  1169         q->setAttribute(Qt::WA_NativeWindow);
       
  1170 
       
  1171     q->setAttribute(Qt::WA_QuitOnClose); // might be cleared in adjustQuitOnCloseAttribute()
       
  1172     adjustQuitOnCloseAttribute();
       
  1173 
       
  1174     q->setAttribute(Qt::WA_WState_Hidden);
       
  1175 
       
  1176     //give potential windows a bigger "pre-initial" size; create_sys() will give them a new size later
       
  1177     data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
       
  1178 
       
  1179     focus_next = focus_prev = q;
       
  1180 
       
  1181     if ((f & Qt::WindowType_Mask) == Qt::Desktop)
       
  1182         q->create();
       
  1183     else if (parentWidget)
       
  1184         q->setParent(parentWidget, data.window_flags);
       
  1185     else {
       
  1186         adjustFlags(data.window_flags, q);
       
  1187         resolveLayoutDirection();
       
  1188         // opaque system background?
       
  1189         const QBrush &background = q->palette().brush(QPalette::Window);
       
  1190         setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
       
  1191     }
       
  1192     data.fnt = QFont(data.fnt, q);
       
  1193 #if defined(Q_WS_X11)
       
  1194     data.fnt.x11SetScreen(xinfo.screen());
       
  1195 #endif // Q_WS_X11
       
  1196 
       
  1197     q->setAttribute(Qt::WA_PendingMoveEvent);
       
  1198     q->setAttribute(Qt::WA_PendingResizeEvent);
       
  1199 
       
  1200     if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
       
  1201         QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
       
  1202 
       
  1203     if (QApplicationPrivate::app_compile_version < 0x040200
       
  1204         || QApplicationPrivate::testAttribute(Qt::AA_ImmediateWidgetCreation))
       
  1205         q->create();
       
  1206 
       
  1207 
       
  1208     QEvent e(QEvent::Create);
       
  1209     QApplication::sendEvent(q, &e);
       
  1210     QApplication::postEvent(q, new QEvent(QEvent::PolishRequest));
       
  1211 
       
  1212     extraPaintEngine = 0;
       
  1213 }
       
  1214 
       
  1215 
       
  1216 
       
  1217 void QWidgetPrivate::createRecursively()
       
  1218 {
       
  1219     Q_Q(QWidget);
       
  1220     q->create(0, true, true);
       
  1221     for (int i = 0; i < children.size(); ++i) {
       
  1222         QWidget *child = qobject_cast<QWidget *>(children.at(i));
       
  1223         if (child && !child->isHidden() && !child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
       
  1224             child->d_func()->createRecursively();
       
  1225     }
       
  1226 }
       
  1227 
       
  1228 
       
  1229 
       
  1230 
       
  1231 /*!
       
  1232     Creates a new widget window if \a window is 0, otherwise sets the
       
  1233     widget's window to \a window.
       
  1234 
       
  1235     Initializes the window (sets the geometry etc.) if \a
       
  1236     initializeWindow is true. If \a initializeWindow is false, no
       
  1237     initialization is performed. This parameter only makes sense if \a
       
  1238     window is a valid window.
       
  1239 
       
  1240     Destroys the old window if \a destroyOldWindow is true. If \a
       
  1241     destroyOldWindow is false, you are responsible for destroying the
       
  1242     window yourself (using platform native code).
       
  1243 
       
  1244     The QWidget constructor calls create(0,true,true) to create a
       
  1245     window for this widget.
       
  1246 */
       
  1247 
       
  1248 void QWidget::create(WId window, bool initializeWindow, bool destroyOldWindow)
       
  1249 {
       
  1250     Q_D(QWidget);
       
  1251     if (testAttribute(Qt::WA_WState_Created) && window == 0 && internalWinId())
       
  1252         return;
       
  1253 
       
  1254     if (d->data.in_destructor)
       
  1255         return;
       
  1256 
       
  1257     Qt::WindowType type = windowType();
       
  1258     Qt::WindowFlags &flags = data->window_flags;
       
  1259 
       
  1260     if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
       
  1261         type = Qt::Window;
       
  1262         flags |= Qt::Window;
       
  1263     }
       
  1264 
       
  1265     if (QWidget *parent = parentWidget()) {
       
  1266         if (type & Qt::Window) {
       
  1267             if (!parent->testAttribute(Qt::WA_WState_Created))
       
  1268                 parent->createWinId();
       
  1269         } else if (testAttribute(Qt::WA_NativeWindow) && !parent->internalWinId()
       
  1270                    && !testAttribute(Qt::WA_DontCreateNativeAncestors)) {
       
  1271             // We're about to create a native child widget that doesn't have a native parent;
       
  1272             // enforce a native handle for the parent unless the Qt::WA_DontCreateNativeAncestors
       
  1273             // attribute is set.
       
  1274             d->createWinId(window);
       
  1275             // Nothing more to do.
       
  1276             Q_ASSERT(testAttribute(Qt::WA_WState_Created));
       
  1277             Q_ASSERT(internalWinId());
       
  1278             return;
       
  1279         }
       
  1280     }
       
  1281 
       
  1282 #ifdef QT3_SUPPORT
       
  1283     if (flags & Qt::WStaticContents)
       
  1284         setAttribute(Qt::WA_StaticContents);
       
  1285     if (flags & Qt::WDestructiveClose)
       
  1286         setAttribute(Qt::WA_DeleteOnClose);
       
  1287     if (flags & Qt::WShowModal)
       
  1288         setWindowModality(Qt::ApplicationModal);
       
  1289     if (flags & Qt::WMouseNoMask)
       
  1290         setAttribute(Qt::WA_MouseNoMask);
       
  1291     if (flags & Qt::WGroupLeader)
       
  1292         setAttribute(Qt::WA_GroupLeader);
       
  1293     if (flags & Qt::WNoMousePropagation)
       
  1294         setAttribute(Qt::WA_NoMousePropagation);
       
  1295 #endif
       
  1296 
       
  1297     static int paintOnScreenEnv = -1;
       
  1298     if (paintOnScreenEnv == -1)
       
  1299         paintOnScreenEnv = qgetenv("QT_ONSCREEN_PAINT").toInt() > 0 ? 1 : 0;
       
  1300     if (paintOnScreenEnv == 1)
       
  1301         setAttribute(Qt::WA_PaintOnScreen);
       
  1302 
       
  1303     if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
       
  1304         setAttribute(Qt::WA_NativeWindow);
       
  1305 
       
  1306 #ifdef ALIEN_DEBUG
       
  1307     qDebug() << "QWidget::create:" << this << "parent:" << parentWidget()
       
  1308              << "Alien?" << !testAttribute(Qt::WA_NativeWindow);
       
  1309 #endif
       
  1310 
       
  1311 #if defined (Q_WS_WIN) && !defined(QT_NO_DRAGANDDROP)
       
  1312     // Unregister the dropsite (if already registered) before we
       
  1313     // re-create the widget with a native window.
       
  1314     if (testAttribute(Qt::WA_WState_Created) && !internalWinId() && testAttribute(Qt::WA_NativeWindow)
       
  1315             && d->extra && d->extra->dropTarget) {
       
  1316         d->registerDropSite(false);
       
  1317     }
       
  1318 #endif // defined (Q_WS_WIN) && !defined(QT_NO_DRAGANDDROP)
       
  1319 
       
  1320     d->updateIsOpaque();
       
  1321 
       
  1322     setAttribute(Qt::WA_WState_Created);                        // set created flag
       
  1323     d->create_sys(window, initializeWindow, destroyOldWindow);
       
  1324 
       
  1325     // a real toplevel window needs a backing store
       
  1326     if (isWindow() && windowType() != Qt::Desktop) {
       
  1327         delete d->topData()->backingStore;
       
  1328         // QWidgetBackingStore will check this variable, hence it must be 0
       
  1329         d->topData()->backingStore = 0;
       
  1330         if (hasBackingStoreSupport())
       
  1331             d->topData()->backingStore = new QWidgetBackingStore(this);
       
  1332     }
       
  1333 
       
  1334     d->setModal_sys();
       
  1335 
       
  1336     if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
       
  1337         setAttribute(Qt::WA_DropSiteRegistered, true);
       
  1338 
       
  1339 #ifdef QT_EVAL
       
  1340     extern void qt_eval_init_widget(QWidget *w);
       
  1341     qt_eval_init_widget(this);
       
  1342 #endif
       
  1343 
       
  1344     // need to force the resting of the icon after changing parents
       
  1345     if (testAttribute(Qt::WA_SetWindowIcon))
       
  1346         d->setWindowIcon_sys(true);
       
  1347     if (isWindow() && !d->topData()->iconText.isEmpty())
       
  1348         d->setWindowIconText_helper(d->topData()->iconText);
       
  1349     if (isWindow() && !d->topData()->caption.isEmpty())
       
  1350         d->setWindowTitle_helper(d->topData()->caption);
       
  1351     if (windowType() != Qt::Desktop) {
       
  1352         d->updateSystemBackground();
       
  1353 
       
  1354         if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
       
  1355             d->setWindowIcon_sys();
       
  1356     }
       
  1357 }
       
  1358 
       
  1359 /*!
       
  1360     Destroys the widget.
       
  1361 
       
  1362     All this widget's children are deleted first. The application
       
  1363     exits if this widget is the main widget.
       
  1364 */
       
  1365 
       
  1366 QWidget::~QWidget()
       
  1367 {
       
  1368     Q_D(QWidget);
       
  1369     d->data.in_destructor = true;
       
  1370 
       
  1371 #if defined (QT_CHECK_STATE)
       
  1372     if (paintingActive())
       
  1373         qWarning("QWidget: %s (%s) deleted while being painted", className(), name());
       
  1374 #endif
       
  1375 
       
  1376     // force acceptDrops false before winId is destroyed.
       
  1377     d->registerDropSite(false);
       
  1378 
       
  1379 #ifndef QT_NO_ACTION
       
  1380     // remove all actions from this widget
       
  1381     for (int i = 0; i < d->actions.size(); ++i) {
       
  1382         QActionPrivate *apriv = d->actions.at(i)->d_func();
       
  1383         apriv->widgets.removeAll(this);
       
  1384     }
       
  1385     d->actions.clear();
       
  1386 #endif
       
  1387 
       
  1388 #ifndef QT_NO_SHORTCUT
       
  1389     // Remove all shortcuts grabbed by this
       
  1390     // widget, unless application is closing
       
  1391     if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
       
  1392         qApp->d_func()->shortcutMap.removeShortcut(0, this, QKeySequence());
       
  1393 #endif
       
  1394 
       
  1395     // delete layout while we still are a valid widget
       
  1396     delete d->layout;
       
  1397     // Remove myself from focus list
       
  1398 
       
  1399     Q_ASSERT(d->focus_next->d_func()->focus_prev == this);
       
  1400     Q_ASSERT(d->focus_prev->d_func()->focus_next == this);
       
  1401 
       
  1402     if (d->focus_next != this) {
       
  1403         d->focus_next->d_func()->focus_prev = d->focus_prev;
       
  1404         d->focus_prev->d_func()->focus_next = d->focus_next;
       
  1405         d->focus_next = d->focus_prev = 0;
       
  1406     }
       
  1407 
       
  1408 #ifdef QT3_SUPPORT
       
  1409     if (QApplicationPrivate::main_widget == this) {        // reset main widget
       
  1410         QApplicationPrivate::main_widget = 0;
       
  1411         QApplication::quit();
       
  1412     }
       
  1413 #endif
       
  1414 
       
  1415     QT_TRY {
       
  1416         clearFocus();
       
  1417     } QT_CATCH(...) {
       
  1418         // swallow this problem because we are in a destructor
       
  1419     }
       
  1420 
       
  1421     d->setDirtyOpaqueRegion();
       
  1422 
       
  1423     if (isWindow() && isVisible() && internalWinId()) {
       
  1424         QT_TRY {
       
  1425             d->close_helper(QWidgetPrivate::CloseNoEvent);
       
  1426         } QT_CATCH(...) {
       
  1427             // if we're out of memory, at least hide the window.
       
  1428             QT_TRY {
       
  1429                 hide();
       
  1430             } QT_CATCH(...) {
       
  1431                 // and if that also doesn't work, then give up
       
  1432             }
       
  1433         }
       
  1434     }
       
  1435 
       
  1436 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined (Q_WS_QWS)
       
  1437     else if (!internalWinId() && isVisible()) {
       
  1438         qApp->d_func()->sendSyntheticEnterLeave(this);
       
  1439 #ifdef Q_WS_QWS
       
  1440     } else if (isVisible()) {
       
  1441         qApp->d_func()->sendSyntheticEnterLeave(this);
       
  1442 #endif
       
  1443     }
       
  1444 #endif
       
  1445 
       
  1446 #ifdef Q_OS_SYMBIAN
       
  1447     if (d->extra && d->extra->topextra && d->extra->topextra->backingStore) {
       
  1448         // Okay, we are about to destroy the top-level window that owns
       
  1449         // the backing store. Make sure we delete the backing store right away
       
  1450         // before the window handle is invalid. This is important because
       
  1451         // the backing store will delete its window surface, which may or may
       
  1452         // not have a reference to this widget that will be used later to
       
  1453         // notify the window it no longer has a surface.
       
  1454         delete d->extra->topextra->backingStore;
       
  1455         d->extra->topextra->backingStore = 0;
       
  1456     }
       
  1457 #endif
       
  1458     if (QWidgetBackingStore *bs = d->maybeBackingStore()) {
       
  1459         bs->removeDirtyWidget(this);
       
  1460         if (testAttribute(Qt::WA_StaticContents))
       
  1461             bs->removeStaticWidget(this);
       
  1462     }
       
  1463 
       
  1464     delete d->needsFlush;
       
  1465     d->needsFlush = 0;
       
  1466 
       
  1467     // set all QPointers for this object to zero
       
  1468     QObjectPrivate::clearGuards(this);
       
  1469 
       
  1470     if (d->declarativeData) {
       
  1471         d->declarativeData->destroyed(this);
       
  1472         d->declarativeData = 0;                 // don't activate again in ~QObject
       
  1473     }
       
  1474 
       
  1475     if (!d->children.isEmpty())
       
  1476         d->deleteChildren();
       
  1477 
       
  1478     QApplication::removePostedEvents(this);
       
  1479 
       
  1480     QT_TRY {
       
  1481         destroy();                                        // platform-dependent cleanup
       
  1482     } QT_CATCH(...) {
       
  1483         // if this fails we can't do anything about it but at least we are not allowed to throw.
       
  1484     }
       
  1485     --QWidgetPrivate::instanceCounter;
       
  1486 
       
  1487     if (QWidgetPrivate::allWidgets) // might have been deleted by ~QApplication
       
  1488         QWidgetPrivate::allWidgets->remove(this);
       
  1489 
       
  1490     QEvent e(QEvent::Destroy);
       
  1491     QCoreApplication::sendEvent(this, &e);
       
  1492 }
       
  1493 
       
  1494 int QWidgetPrivate::instanceCounter = 0;  // Current number of widget instances
       
  1495 int QWidgetPrivate::maxInstances = 0;     // Maximum number of widget instances
       
  1496 
       
  1497 void QWidgetPrivate::setWinId(WId id)                // set widget identifier
       
  1498 {
       
  1499     Q_Q(QWidget);
       
  1500     // the user might create a widget with Qt::Desktop window
       
  1501     // attribute (or create another QDesktopWidget instance), which
       
  1502     // will have the same windowid (the root window id) as the
       
  1503     // qt_desktopWidget. We should not add the second desktop widget
       
  1504     // to the mapper.
       
  1505     bool userDesktopWidget = qt_desktopWidget != 0 && qt_desktopWidget != q && q->windowType() == Qt::Desktop;
       
  1506     if (mapper && data.winid && !userDesktopWidget) {
       
  1507         mapper->remove(data.winid);
       
  1508     }
       
  1509 
       
  1510     const WId oldWinId = data.winid;
       
  1511 
       
  1512     data.winid = id;
       
  1513 #if defined(Q_WS_X11)
       
  1514     hd = id; // X11: hd == ident
       
  1515 #endif
       
  1516     if (mapper && id && !userDesktopWidget) {
       
  1517         mapper->insert(data.winid, q);
       
  1518     }
       
  1519 
       
  1520     if(oldWinId != id) {
       
  1521         // Do not emit an event when the old winId is destroyed.  This only
       
  1522         // happens (a) during widget destruction, and (b) immediately prior
       
  1523         // to creation of a new winId, for example as a result of re-parenting.
       
  1524         if(id != 0) {
       
  1525             QEvent e(QEvent::WinIdChange);
       
  1526             QCoreApplication::sendEvent(q, &e);
       
  1527         }
       
  1528     }
       
  1529 }
       
  1530 
       
  1531 void QWidgetPrivate::createTLExtra()
       
  1532 {
       
  1533     if (!extra)
       
  1534         createExtra();
       
  1535     if (!extra->topextra) {
       
  1536         QTLWExtra* x = extra->topextra = new QTLWExtra;
       
  1537         x->icon = 0;
       
  1538         x->iconPixmap = 0;
       
  1539         x->backingStore = 0;
       
  1540         x->windowSurface = 0;
       
  1541         x->sharedPainter = 0;
       
  1542         x->incw = x->inch = 0;
       
  1543         x->basew = x->baseh = 0;
       
  1544         x->frameStrut.setCoords(0, 0, 0, 0);
       
  1545         x->normalGeometry = QRect(0,0,-1,-1);
       
  1546         x->savedFlags = 0;
       
  1547         x->opacity = 255;
       
  1548         x->posFromMove = false;
       
  1549         x->sizeAdjusted = false;
       
  1550         x->inTopLevelResize = false;
       
  1551         x->inRepaint = false;
       
  1552         x->embedded = 0;
       
  1553         createTLSysExtra();
       
  1554 #ifdef QWIDGET_EXTRA_DEBUG
       
  1555         static int count = 0;
       
  1556         qDebug() << "tlextra" << ++count;
       
  1557 #endif
       
  1558     }
       
  1559 }
       
  1560 
       
  1561 /*!
       
  1562   \internal
       
  1563   Creates the widget extra data.
       
  1564 */
       
  1565 
       
  1566 void QWidgetPrivate::createExtra()
       
  1567 {
       
  1568     if (!extra) {                                // if not exists
       
  1569         extra = new QWExtra;
       
  1570         extra->glContext = 0;
       
  1571         extra->topextra = 0;
       
  1572 #ifndef QT_NO_GRAPHICSVIEW
       
  1573         extra->proxyWidget = 0;
       
  1574 #endif
       
  1575 #ifndef QT_NO_CURSOR
       
  1576         extra->curs = 0;
       
  1577 #endif
       
  1578         extra->minw = 0;
       
  1579         extra->minh = 0;
       
  1580         extra->maxw = QWIDGETSIZE_MAX;
       
  1581         extra->maxh = QWIDGETSIZE_MAX;
       
  1582         extra->customDpiX = 0;
       
  1583         extra->customDpiY = 0;
       
  1584         extra->explicitMinSize = 0;
       
  1585         extra->explicitMaxSize = 0;
       
  1586         extra->autoFillBackground = 0;
       
  1587         extra->nativeChildrenForced = 0;
       
  1588         extra->inRenderWithPainter = 0;
       
  1589         extra->hasMask = 0;
       
  1590         createSysExtra();
       
  1591 #ifdef QWIDGET_EXTRA_DEBUG
       
  1592         static int count = 0;
       
  1593         qDebug() << "extra" << ++count;
       
  1594 #endif
       
  1595     }
       
  1596 }
       
  1597 
       
  1598 
       
  1599 /*!
       
  1600   \internal
       
  1601   Deletes the widget extra data.
       
  1602 */
       
  1603 
       
  1604 void QWidgetPrivate::deleteExtra()
       
  1605 {
       
  1606     if (extra) {                                // if exists
       
  1607 #ifndef QT_NO_CURSOR
       
  1608         delete extra->curs;
       
  1609 #endif
       
  1610         deleteSysExtra();
       
  1611 #ifndef QT_NO_STYLE_STYLESHEET
       
  1612         // dereference the stylesheet style
       
  1613         if (QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(extra->style))
       
  1614             proxy->deref();
       
  1615 #endif
       
  1616         if (extra->topextra) {
       
  1617             deleteTLSysExtra();
       
  1618             delete extra->topextra->backingStore;
       
  1619             delete extra->topextra->icon;
       
  1620             delete extra->topextra->iconPixmap;
       
  1621 #if defined(Q_WS_QWS) && !defined(QT_NO_QWS_MANAGER)
       
  1622             delete extra->topextra->qwsManager;
       
  1623 #endif
       
  1624             delete extra->topextra->windowSurface;
       
  1625             delete extra->topextra;
       
  1626         }
       
  1627         delete extra;
       
  1628         // extra->xic destroyed in QWidget::destroy()
       
  1629         extra = 0;
       
  1630     }
       
  1631 }
       
  1632 
       
  1633 /*
       
  1634   Returns true if there are widgets above this which overlap with
       
  1635   \a rect, which is in parent's coordinate system (same as crect).
       
  1636 */
       
  1637 
       
  1638 bool QWidgetPrivate::isOverlapped(const QRect &rect) const
       
  1639 {
       
  1640     Q_Q(const QWidget);
       
  1641 
       
  1642     const QWidget *w = q;
       
  1643     QRect r = rect;
       
  1644     while (w) {
       
  1645         if (w->isWindow())
       
  1646             return false;
       
  1647         QWidgetPrivate *pd = w->parentWidget()->d_func();
       
  1648         bool above = false;
       
  1649         for (int i = 0; i < pd->children.size(); ++i) {
       
  1650             QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
       
  1651             if (!sibling || !sibling->isVisible() || sibling->isWindow())
       
  1652                 continue;
       
  1653             if (!above) {
       
  1654                 above = (sibling == w);
       
  1655                 continue;
       
  1656             }
       
  1657 
       
  1658             if (qRectIntersects(sibling->d_func()->effectiveRectFor(sibling->data->crect), r)) {
       
  1659                 const QWExtra *siblingExtra = sibling->d_func()->extra;
       
  1660                 if (siblingExtra && siblingExtra->hasMask && !sibling->d_func()->graphicsEffect
       
  1661                     && !siblingExtra->mask.translated(sibling->data->crect.topLeft()).intersects(r)) {
       
  1662                     continue;
       
  1663                 }
       
  1664                 return true;
       
  1665             }
       
  1666         }
       
  1667         w = w->parentWidget();
       
  1668         r.translate(pd->data.crect.topLeft());
       
  1669     }
       
  1670     return false;
       
  1671 }
       
  1672 
       
  1673 void QWidgetPrivate::syncBackingStore()
       
  1674 {
       
  1675     if (paintOnScreen()) {
       
  1676         repaint_sys(dirty);
       
  1677         dirty = QRegion();
       
  1678     } else if (QWidgetBackingStore *bs = maybeBackingStore()) {
       
  1679 #ifdef QT_MAC_USE_COCOA
       
  1680         Q_UNUSED(bs);
       
  1681         void qt_mac_set_needs_display(QWidget *, QRegion);
       
  1682         qt_mac_set_needs_display(q_func(), QRegion());
       
  1683 #else
       
  1684         bs->sync();
       
  1685 #endif
       
  1686     }
       
  1687 }
       
  1688 
       
  1689 void QWidgetPrivate::syncBackingStore(const QRegion &region)
       
  1690 {
       
  1691     if (paintOnScreen())
       
  1692         repaint_sys(region);
       
  1693     else if (QWidgetBackingStore *bs = maybeBackingStore()) {
       
  1694 #ifdef QT_MAC_USE_COCOA
       
  1695         Q_UNUSED(bs);
       
  1696         void qt_mac_set_needs_display(QWidget *, QRegion);
       
  1697         qt_mac_set_needs_display(q_func(), region);
       
  1698 #else
       
  1699         bs->sync(q_func(), region);
       
  1700 #endif
       
  1701     }
       
  1702 }
       
  1703 
       
  1704 void QWidgetPrivate::setUpdatesEnabled_helper(bool enable)
       
  1705 {
       
  1706     Q_Q(QWidget);
       
  1707 
       
  1708     if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled())
       
  1709         return; // nothing we can do
       
  1710 
       
  1711     if (enable != q->testAttribute(Qt::WA_UpdatesDisabled))
       
  1712         return; // nothing to do
       
  1713 
       
  1714     q->setAttribute(Qt::WA_UpdatesDisabled, !enable);
       
  1715     if (enable)
       
  1716         q->update();
       
  1717 
       
  1718     Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled;
       
  1719     for (int i = 0; i < children.size(); ++i) {
       
  1720         QWidget *w = qobject_cast<QWidget *>(children.at(i));
       
  1721         if (w && !w->isWindow() && !w->testAttribute(attribute))
       
  1722             w->d_func()->setUpdatesEnabled_helper(enable);
       
  1723     }
       
  1724 }
       
  1725 
       
  1726 /*!
       
  1727     \internal
       
  1728 
       
  1729     Propagate this widget's palette to all children, except style sheet
       
  1730     widgets, and windows that don't enable window propagation (palettes don't
       
  1731     normally propagate to windows).
       
  1732 */
       
  1733 void QWidgetPrivate::propagatePaletteChange()
       
  1734 {
       
  1735     Q_Q(QWidget);
       
  1736     // Propagate a new inherited mask to all children.
       
  1737 #ifndef QT_NO_GRAPHICSVIEW
       
  1738     if (!q->parentWidget() && extra && extra->proxyWidget) {
       
  1739         QGraphicsProxyWidget *p = extra->proxyWidget;
       
  1740         inheritedPaletteResolveMask = p->d_func()->inheritedPaletteResolveMask | p->palette().resolve();
       
  1741     } else
       
  1742 #endif //QT_NO_GRAPHICSVIEW
       
  1743         if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
       
  1744         inheritedPaletteResolveMask = 0;
       
  1745     }
       
  1746     int mask = data.pal.resolve() | inheritedPaletteResolveMask;
       
  1747 
       
  1748     QEvent pc(QEvent::PaletteChange);
       
  1749     QApplication::sendEvent(q, &pc);
       
  1750     for (int i = 0; i < children.size(); ++i) {
       
  1751         QWidget *w = qobject_cast<QWidget*>(children.at(i));
       
  1752         if (w && !w->testAttribute(Qt::WA_StyleSheet)
       
  1753             && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
       
  1754             QWidgetPrivate *wd = w->d_func();
       
  1755             wd->inheritedPaletteResolveMask = mask;
       
  1756             wd->resolvePalette();
       
  1757         }
       
  1758     }
       
  1759 #if defined(QT3_SUPPORT)
       
  1760     q->paletteChange(q->palette()); // compatibility
       
  1761 #endif
       
  1762 }
       
  1763 
       
  1764 /*
       
  1765   Returns the widget's clipping rectangle.
       
  1766 */
       
  1767 QRect QWidgetPrivate::clipRect() const
       
  1768 {
       
  1769     Q_Q(const QWidget);
       
  1770     const QWidget * w = q;
       
  1771     if (!w->isVisible())
       
  1772         return QRect();
       
  1773     QRect r = effectiveRectFor(q->rect());
       
  1774     int ox = 0;
       
  1775     int oy = 0;
       
  1776     while (w
       
  1777             && w->isVisible()
       
  1778             && !w->isWindow()
       
  1779             && w->parentWidget()) {
       
  1780         ox -= w->x();
       
  1781         oy -= w->y();
       
  1782         w = w->parentWidget();
       
  1783         r &= QRect(ox, oy, w->width(), w->height());
       
  1784     }
       
  1785     return r;
       
  1786 }
       
  1787 
       
  1788 /*
       
  1789   Returns the widget's clipping region (without siblings).
       
  1790 */
       
  1791 QRegion QWidgetPrivate::clipRegion() const
       
  1792 {
       
  1793     Q_Q(const QWidget);
       
  1794     if (!q->isVisible())
       
  1795         return QRegion();
       
  1796     QRegion r(q->rect());
       
  1797     const QWidget * w = q;
       
  1798     const QWidget *ignoreUpTo;
       
  1799     int ox = 0;
       
  1800     int oy = 0;
       
  1801     while (w
       
  1802            && w->isVisible()
       
  1803            && !w->isWindow()
       
  1804            && w->parentWidget()) {
       
  1805         ox -= w->x();
       
  1806         oy -= w->y();
       
  1807         ignoreUpTo = w;
       
  1808         w = w->parentWidget();
       
  1809         r &= QRegion(ox, oy, w->width(), w->height());
       
  1810 
       
  1811         int i = 0;
       
  1812         while(w->d_func()->children.at(i++) != static_cast<const QObject *>(ignoreUpTo))
       
  1813             ;
       
  1814         for ( ; i < w->d_func()->children.size(); ++i) {
       
  1815             if(QWidget *sibling = qobject_cast<QWidget *>(w->d_func()->children.at(i))) {
       
  1816                 if(sibling->isVisible() && !sibling->isWindow()) {
       
  1817                     QRect siblingRect(ox+sibling->x(), oy+sibling->y(),
       
  1818                                       sibling->width(), sibling->height());
       
  1819                     if (qRectIntersects(siblingRect, q->rect()))
       
  1820                         r -= QRegion(siblingRect);
       
  1821                 }
       
  1822             }
       
  1823         }
       
  1824     }
       
  1825     return r;
       
  1826 }
       
  1827 
       
  1828 #ifndef QT_NO_GRAPHICSEFFECT
       
  1829 void QWidgetPrivate::invalidateGraphicsEffectsRecursively()
       
  1830 {
       
  1831     Q_Q(QWidget);
       
  1832     QWidget *w = q;
       
  1833     do {
       
  1834         if (w->graphicsEffect()) {
       
  1835             QWidgetEffectSourcePrivate *sourced =
       
  1836                 static_cast<QWidgetEffectSourcePrivate *>(w->graphicsEffect()->source()->d_func());
       
  1837             if (!sourced->updateDueToGraphicsEffect)
       
  1838                 w->graphicsEffect()->source()->d_func()->invalidateCache();
       
  1839         }
       
  1840         w = w->parentWidget();
       
  1841     } while (w);
       
  1842 }
       
  1843 #endif //QT_NO_GRAPHICSEFFECT
       
  1844 
       
  1845 void QWidgetPrivate::setDirtyOpaqueRegion()
       
  1846 {
       
  1847     Q_Q(QWidget);
       
  1848 
       
  1849     dirtyOpaqueChildren = true;
       
  1850 
       
  1851 #ifndef QT_NO_GRAPHICSEFFECT
       
  1852     invalidateGraphicsEffectsRecursively();
       
  1853 #endif //QT_NO_GRAPHICSEFFECT
       
  1854 
       
  1855     if (q->isWindow())
       
  1856         return;
       
  1857 
       
  1858     QWidget *parent = q->parentWidget();
       
  1859     if (!parent)
       
  1860         return;
       
  1861 
       
  1862     // TODO: instead of setting dirtyflag, manipulate the dirtyregion directly?
       
  1863     QWidgetPrivate *pd = parent->d_func();
       
  1864     if (!pd->dirtyOpaqueChildren)
       
  1865         pd->setDirtyOpaqueRegion();
       
  1866 }
       
  1867 
       
  1868 const QRegion &QWidgetPrivate::getOpaqueChildren() const
       
  1869 {
       
  1870     if (!dirtyOpaqueChildren)
       
  1871         return opaqueChildren;
       
  1872 
       
  1873     QWidgetPrivate *that = const_cast<QWidgetPrivate*>(this);
       
  1874     that->opaqueChildren = QRegion();
       
  1875 
       
  1876     for (int i = 0; i < children.size(); ++i) {
       
  1877         QWidget *child = qobject_cast<QWidget *>(children.at(i));
       
  1878         if (!child || !child->isVisible() || child->isWindow())
       
  1879             continue;
       
  1880 
       
  1881         const QPoint offset = child->geometry().topLeft();
       
  1882         QWidgetPrivate *childd = child->d_func();
       
  1883         QRegion r = childd->isOpaque ? child->rect() : childd->getOpaqueChildren();
       
  1884         if (childd->extra && childd->extra->hasMask)
       
  1885             r &= childd->extra->mask;
       
  1886         if (r.isEmpty())
       
  1887             continue;
       
  1888         r.translate(offset);
       
  1889         that->opaqueChildren += r;
       
  1890     }
       
  1891 
       
  1892     that->opaqueChildren &= q_func()->rect();
       
  1893     that->dirtyOpaqueChildren = false;
       
  1894 
       
  1895     return that->opaqueChildren;
       
  1896 }
       
  1897 
       
  1898 void QWidgetPrivate::subtractOpaqueChildren(QRegion &source, const QRect &clipRect) const
       
  1899 {
       
  1900     if (children.isEmpty() || clipRect.isEmpty())
       
  1901         return;
       
  1902 
       
  1903     const QRegion &r = getOpaqueChildren();
       
  1904     if (!r.isEmpty())
       
  1905         source -= (r & clipRect);
       
  1906 }
       
  1907 
       
  1908 //subtract any relatives that are higher up than me --- this is too expensive !!!
       
  1909 void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion, bool *hasDirtySiblingsAbove,
       
  1910                                             bool alsoNonOpaque) const
       
  1911 {
       
  1912     Q_Q(const QWidget);
       
  1913     static int disableSubtractOpaqueSiblings = qgetenv("QT_NO_SUBTRACTOPAQUESIBLINGS").toInt();
       
  1914     if (disableSubtractOpaqueSiblings || q->isWindow())
       
  1915         return;
       
  1916 
       
  1917     QRect clipBoundingRect;
       
  1918     bool dirtyClipBoundingRect = true;
       
  1919 
       
  1920     QRegion parentClip;
       
  1921     bool dirtyParentClip = true;
       
  1922 
       
  1923     QPoint parentOffset = data.crect.topLeft();
       
  1924 
       
  1925     const QWidget *w = q;
       
  1926 
       
  1927     while (w) {
       
  1928         if (w->isWindow())
       
  1929             break;
       
  1930         QWidgetPrivate *pd = w->parentWidget()->d_func();
       
  1931         const int myIndex = pd->children.indexOf(const_cast<QWidget *>(w));
       
  1932         const QRect widgetGeometry = w->d_func()->effectiveRectFor(w->data->crect);
       
  1933         for (int i = myIndex + 1; i < pd->children.size(); ++i) {
       
  1934             QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
       
  1935             if (!sibling || !sibling->isVisible() || sibling->isWindow())
       
  1936                 continue;
       
  1937 
       
  1938             const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
       
  1939             if (!qRectIntersects(siblingGeometry, widgetGeometry))
       
  1940                 continue;
       
  1941 
       
  1942             if (dirtyClipBoundingRect) {
       
  1943                 clipBoundingRect = sourceRegion.boundingRect();
       
  1944                 dirtyClipBoundingRect = false;
       
  1945             }
       
  1946 
       
  1947             if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
       
  1948                 continue;
       
  1949 
       
  1950             if (dirtyParentClip) {
       
  1951                 parentClip = sourceRegion.translated(parentOffset);
       
  1952                 dirtyParentClip = false;
       
  1953             }
       
  1954 
       
  1955             const QPoint siblingPos(sibling->data->crect.topLeft());
       
  1956             const QRect siblingClipRect(sibling->d_func()->clipRect());
       
  1957             QRegion siblingDirty(parentClip);
       
  1958             siblingDirty &= (siblingClipRect.translated(siblingPos));
       
  1959             const bool hasMask = sibling->d_func()->extra && sibling->d_func()->extra->hasMask
       
  1960                                  && !sibling->d_func()->graphicsEffect;
       
  1961             if (hasMask)
       
  1962                 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
       
  1963             if (siblingDirty.isEmpty())
       
  1964                 continue;
       
  1965 
       
  1966             if (sibling->d_func()->isOpaque || alsoNonOpaque) {
       
  1967                 if (hasMask) {
       
  1968                     siblingDirty.translate(-parentOffset);
       
  1969                     sourceRegion -= siblingDirty;
       
  1970                 } else {
       
  1971                     sourceRegion -= siblingGeometry.translated(-parentOffset);
       
  1972                 }
       
  1973             } else {
       
  1974                 if (hasDirtySiblingsAbove)
       
  1975                     *hasDirtySiblingsAbove = true;
       
  1976                 if (sibling->d_func()->children.isEmpty())
       
  1977                     continue;
       
  1978                 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
       
  1979                 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
       
  1980                 sourceRegion -= opaqueSiblingChildren;
       
  1981             }
       
  1982             if (sourceRegion.isEmpty())
       
  1983                 return;
       
  1984 
       
  1985             dirtyClipBoundingRect = true;
       
  1986             dirtyParentClip = true;
       
  1987         }
       
  1988 
       
  1989         w = w->parentWidget();
       
  1990         parentOffset += pd->data.crect.topLeft();
       
  1991         dirtyParentClip = true;
       
  1992     }
       
  1993 }
       
  1994 
       
  1995 void QWidgetPrivate::clipToEffectiveMask(QRegion &region) const
       
  1996 {
       
  1997     Q_Q(const QWidget);
       
  1998 
       
  1999     const QWidget *w = q;
       
  2000     QPoint offset;
       
  2001 
       
  2002 #ifndef QT_NO_GRAPHICSEFFECT
       
  2003     if (graphicsEffect) {
       
  2004         w = q->parentWidget();
       
  2005         offset -= data.crect.topLeft();
       
  2006     }
       
  2007 #endif //QT_NO_GRAPHICSEFFECT
       
  2008 
       
  2009     while (w) {
       
  2010         const QWidgetPrivate *wd = w->d_func();
       
  2011         if (wd->extra && wd->extra->hasMask)
       
  2012             region &= (w != q) ? wd->extra->mask.translated(offset) : wd->extra->mask;
       
  2013         if (w->isWindow())
       
  2014             return;
       
  2015         offset -= wd->data.crect.topLeft();
       
  2016         w = w->parentWidget();
       
  2017     }
       
  2018 }
       
  2019 
       
  2020 bool QWidgetPrivate::paintOnScreen() const
       
  2021 {
       
  2022 #if defined(Q_WS_QWS)
       
  2023     return false;
       
  2024 #elif  defined(QT_NO_BACKINGSTORE)
       
  2025     return true;
       
  2026 #else
       
  2027     Q_Q(const QWidget);
       
  2028     if (q->testAttribute(Qt::WA_PaintOnScreen)
       
  2029             || (!q->isWindow() && q->window()->testAttribute(Qt::WA_PaintOnScreen))) {
       
  2030         return true;
       
  2031     }
       
  2032 
       
  2033     return !qt_enable_backingstore;
       
  2034 #endif
       
  2035 }
       
  2036 
       
  2037 void QWidgetPrivate::updateIsOpaque()
       
  2038 {
       
  2039     // hw: todo: only needed if opacity actually changed
       
  2040     setDirtyOpaqueRegion();
       
  2041 
       
  2042 #ifndef QT_NO_GRAPHICSEFFECT
       
  2043     if (graphicsEffect) {
       
  2044         // ### We should probably add QGraphicsEffect::isOpaque at some point.
       
  2045         setOpaque(false);
       
  2046         return;
       
  2047     }
       
  2048 #endif //QT_NO_GRAPHICSEFFECT
       
  2049 
       
  2050     Q_Q(QWidget);
       
  2051 #ifdef Q_WS_X11
       
  2052     if (q->testAttribute(Qt::WA_X11OpenGLOverlay)) {
       
  2053         setOpaque(false);
       
  2054         return;
       
  2055     }
       
  2056 #endif
       
  2057 
       
  2058 #ifdef Q_WS_S60
       
  2059     if (q->windowType() == Qt::Dialog && q->testAttribute(Qt::WA_TranslucentBackground)
       
  2060                 && S60->avkonComponentsSupportTransparency) {
       
  2061         setOpaque(false);
       
  2062         return;
       
  2063     }
       
  2064 #endif
       
  2065 
       
  2066     if (q->testAttribute(Qt::WA_OpaquePaintEvent) || q->testAttribute(Qt::WA_PaintOnScreen)) {
       
  2067         setOpaque(true);
       
  2068         return;
       
  2069     }
       
  2070 
       
  2071     const QPalette &pal = q->palette();
       
  2072 
       
  2073     if (q->autoFillBackground()) {
       
  2074         const QBrush &autoFillBrush = pal.brush(q->backgroundRole());
       
  2075         if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
       
  2076             setOpaque(true);
       
  2077             return;
       
  2078         }
       
  2079     }
       
  2080 
       
  2081     if (q->isWindow() && !q->testAttribute(Qt::WA_NoSystemBackground)) {
       
  2082         const QBrush &windowBrush = q->palette().brush(QPalette::Window);
       
  2083         if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
       
  2084             setOpaque(true);
       
  2085             return;
       
  2086         }
       
  2087     }
       
  2088     setOpaque(false);
       
  2089 }
       
  2090 
       
  2091 void QWidgetPrivate::setOpaque(bool opaque)
       
  2092 {
       
  2093     if (isOpaque == opaque)
       
  2094         return;
       
  2095     isOpaque = opaque;
       
  2096 #ifdef Q_WS_MAC
       
  2097     macUpdateIsOpaque();
       
  2098 #endif
       
  2099 #ifdef Q_WS_X11
       
  2100     x11UpdateIsOpaque();
       
  2101 #endif
       
  2102 #ifdef Q_WS_WIN
       
  2103     winUpdateIsOpaque();
       
  2104 #endif
       
  2105 #ifdef Q_OS_SYMBIAN
       
  2106     s60UpdateIsOpaque();
       
  2107 #endif
       
  2108 }
       
  2109 
       
  2110 void QWidgetPrivate::updateIsTranslucent()
       
  2111 {
       
  2112 #ifdef Q_WS_MAC
       
  2113     macUpdateIsOpaque();
       
  2114 #endif
       
  2115 #ifdef Q_WS_X11
       
  2116     x11UpdateIsOpaque();
       
  2117 #endif
       
  2118 #ifdef Q_WS_WIN
       
  2119     winUpdateIsOpaque();
       
  2120 #endif
       
  2121 #ifdef Q_OS_SYMBIAN
       
  2122     s60UpdateIsOpaque();
       
  2123 #endif
       
  2124 }
       
  2125 
       
  2126 /*!
       
  2127     \fn void QPixmap::fill(const QWidget *widget, const QPoint &offset)
       
  2128 
       
  2129     Fills the pixmap with the \a widget's background color or pixmap
       
  2130     according to the given offset.
       
  2131 
       
  2132     The QPoint \a offset defines a point in widget coordinates to
       
  2133     which the pixmap's top-left pixel will be mapped to. This is only
       
  2134     significant if the widget has a background pixmap; otherwise the
       
  2135     pixmap will simply be filled with the background color of the
       
  2136     widget.
       
  2137 */
       
  2138 
       
  2139 void QPixmap::fill( const QWidget *widget, const QPoint &off )
       
  2140 {
       
  2141     QPainter p(this);
       
  2142     p.translate(-off);
       
  2143     widget->d_func()->paintBackground(&p, QRect(off, size()));
       
  2144 }
       
  2145 
       
  2146 static inline void fillRegion(QPainter *painter, const QRegion &rgn, const QBrush &brush)
       
  2147 {
       
  2148     Q_ASSERT(painter);
       
  2149 
       
  2150     if (brush.style() == Qt::TexturePattern) {
       
  2151 #ifdef Q_WS_MAC
       
  2152         // Optimize pattern filling on mac by using HITheme directly
       
  2153         // when filling with the standard widget background.
       
  2154         // Defined in qmacstyle_mac.cpp
       
  2155         extern void qt_mac_fill_background(QPainter *painter, const QRegion &rgn, const QBrush &brush);
       
  2156         qt_mac_fill_background(painter, rgn, brush);
       
  2157 #else
       
  2158 #if !defined(QT_NO_STYLE_S60)
       
  2159         // Defined in qs60style.cpp
       
  2160         extern bool qt_s60_fill_background(QPainter *painter, const QRegion &rgn, const QBrush &brush);
       
  2161         if (!qt_s60_fill_background(painter, rgn, brush))
       
  2162 #endif // !defined(QT_NO_STYLE_S60)
       
  2163         {
       
  2164             const QRect rect(rgn.boundingRect());
       
  2165             painter->setClipRegion(rgn);
       
  2166             painter->drawTiledPixmap(rect, brush.texture(), rect.topLeft());
       
  2167         }
       
  2168 #endif // Q_WS_MAC
       
  2169 
       
  2170     } else if (brush.gradient()
       
  2171                && brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode) {
       
  2172         painter->save();
       
  2173         painter->setClipRegion(rgn);
       
  2174         painter->fillRect(0, 0, painter->device()->width(), painter->device()->height(), brush);
       
  2175         painter->restore();
       
  2176     } else {
       
  2177         const QVector<QRect> &rects = rgn.rects();
       
  2178         for (int i = 0; i < rects.size(); ++i)
       
  2179             painter->fillRect(rects.at(i), brush);
       
  2180     }
       
  2181 }
       
  2182 
       
  2183 void QWidgetPrivate::paintBackground(QPainter *painter, const QRegion &rgn, int flags) const
       
  2184 {
       
  2185     Q_Q(const QWidget);
       
  2186 
       
  2187 #ifndef QT_NO_SCROLLAREA
       
  2188     bool resetBrushOrigin = false;
       
  2189     QPointF oldBrushOrigin;
       
  2190     //If we are painting the viewport of a scrollarea, we must apply an offset to the brush in case we are drawing a texture
       
  2191     QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent);
       
  2192     if (scrollArea && scrollArea->viewport() == q) {
       
  2193         QObjectData *scrollPrivate = static_cast<QWidget *>(scrollArea)->d_ptr.data();
       
  2194         QAbstractScrollAreaPrivate *priv = static_cast<QAbstractScrollAreaPrivate *>(scrollPrivate);
       
  2195         oldBrushOrigin = painter->brushOrigin();
       
  2196         resetBrushOrigin = true;
       
  2197         painter->setBrushOrigin(-priv->contentsOffset());
       
  2198 
       
  2199     }
       
  2200 #endif // QT_NO_SCROLLAREA
       
  2201 
       
  2202     const QBrush autoFillBrush = q->palette().brush(q->backgroundRole());
       
  2203 
       
  2204     if ((flags & DrawAsRoot) && !(q->autoFillBackground() && autoFillBrush.isOpaque())) {
       
  2205         const QBrush bg = q->palette().brush(QPalette::Window);
       
  2206 #ifdef Q_WS_QWS
       
  2207         if (!(flags & DontSetCompositionMode) && painter->paintEngine()->hasFeature(QPaintEngine::PorterDuff))
       
  2208             painter->setCompositionMode(QPainter::CompositionMode_Source); //copy alpha straight in
       
  2209 #endif
       
  2210         fillRegion(painter, rgn, bg);
       
  2211     }
       
  2212 
       
  2213     if (q->autoFillBackground())
       
  2214         fillRegion(painter, rgn, autoFillBrush);
       
  2215 
       
  2216     if (q->testAttribute(Qt::WA_StyledBackground)) {
       
  2217         painter->setClipRegion(rgn);
       
  2218         QStyleOption opt;
       
  2219         opt.initFrom(q);
       
  2220         q->style()->drawPrimitive(QStyle::PE_Widget, &opt, painter, q);
       
  2221     }
       
  2222 
       
  2223 #ifndef QT_NO_SCROLLAREA
       
  2224     if (resetBrushOrigin)
       
  2225         painter->setBrushOrigin(oldBrushOrigin);
       
  2226 #endif // QT_NO_SCROLLAREA
       
  2227 }
       
  2228 
       
  2229 /*
       
  2230   \internal
       
  2231   This function is called when a widget is hidden or destroyed.
       
  2232   It resets some application global pointers that should only refer active,
       
  2233   visible widgets.
       
  2234 */
       
  2235 
       
  2236 #ifdef Q_WS_MAC
       
  2237     extern QPointer<QWidget> qt_button_down;
       
  2238 #else
       
  2239     extern QWidget *qt_button_down;
       
  2240 #endif
       
  2241 
       
  2242 void QWidgetPrivate::deactivateWidgetCleanup()
       
  2243 {
       
  2244     Q_Q(QWidget);
       
  2245     // If this was the active application window, reset it
       
  2246     if (QApplication::activeWindow() == q)
       
  2247         QApplication::setActiveWindow(0);
       
  2248     // If the is the active mouse press widget, reset it
       
  2249     if (q == qt_button_down)
       
  2250         qt_button_down = 0;
       
  2251 }
       
  2252 
       
  2253 
       
  2254 /*!
       
  2255     Returns a pointer to the widget with window identifer/handle \a
       
  2256     id.
       
  2257 
       
  2258     The window identifier type depends on the underlying window
       
  2259     system, see \c qwindowdefs.h for the actual definition. If there
       
  2260     is no widget with this identifier, 0 is returned.
       
  2261 */
       
  2262 
       
  2263 QWidget *QWidget::find(WId id)
       
  2264 {
       
  2265     return QWidgetPrivate::mapper ? QWidgetPrivate::mapper->value(id, 0) : 0;
       
  2266 }
       
  2267 
       
  2268 
       
  2269 
       
  2270 /*!
       
  2271     \fn WId QWidget::internalWinId() const
       
  2272     \internal
       
  2273     Returns the window system identifier of the widget, or 0 if the widget is not created yet.
       
  2274 
       
  2275 */
       
  2276 
       
  2277 /*!
       
  2278     \fn WId QWidget::winId() const
       
  2279 
       
  2280     Returns the window system identifier of the widget.
       
  2281 
       
  2282     Portable in principle, but if you use it you are probably about to
       
  2283     do something non-portable. Be careful.
       
  2284 
       
  2285     If a widget is non-native (alien) and winId() is invoked on it, that widget
       
  2286     will be provided a native handle.
       
  2287 
       
  2288     On Mac OS X, the type returned depends on which framework Qt was linked
       
  2289     against. If Qt is using Carbon, the {WId} is actually an HIViewRef. If Qt
       
  2290     is using Cocoa, {WId} is a pointer to an NSView.
       
  2291 
       
  2292     This value may change at run-time. An event with type QEvent::WinIdChange
       
  2293     will be sent to the widget following a change in window system identifier.
       
  2294 
       
  2295     \sa find()
       
  2296 */
       
  2297 WId QWidget::winId() const
       
  2298 {
       
  2299     if (!testAttribute(Qt::WA_WState_Created) || !internalWinId()) {
       
  2300         QWidget *that = const_cast<QWidget*>(this);
       
  2301         that->setAttribute(Qt::WA_NativeWindow);
       
  2302         that->d_func()->createWinId();
       
  2303         return that->data->winid;
       
  2304     }
       
  2305     return data->winid;
       
  2306 }
       
  2307 
       
  2308 
       
  2309 void QWidgetPrivate::createWinId(WId winid)
       
  2310 {
       
  2311     Q_Q(QWidget);
       
  2312     const bool forceNativeWindow = q->testAttribute(Qt::WA_NativeWindow);
       
  2313     if (!q->testAttribute(Qt::WA_WState_Created) || (forceNativeWindow && !q->internalWinId())) {
       
  2314         if (!q->isWindow()) {
       
  2315             QWidget *parent = q->parentWidget();
       
  2316             QWidgetPrivate *pd = parent->d_func();
       
  2317             if (forceNativeWindow && !q->testAttribute(Qt::WA_DontCreateNativeAncestors))
       
  2318                 parent->setAttribute(Qt::WA_NativeWindow);
       
  2319             if (!parent->internalWinId()) {
       
  2320                 pd->createWinId();
       
  2321             }
       
  2322 
       
  2323             for (int i = 0; i < pd->children.size(); ++i) {
       
  2324                 QWidget *w = qobject_cast<QWidget *>(pd->children.at(i));
       
  2325                 if (w && !w->isWindow() && (!w->testAttribute(Qt::WA_WState_Created)
       
  2326                                             || (!w->internalWinId() && w->testAttribute(Qt::WA_NativeWindow)))) {
       
  2327                     if (w!=q) {
       
  2328                         w->create();
       
  2329                     } else {
       
  2330                         w->create(winid);
       
  2331                         // if the window has already been created, we
       
  2332                         // need to raise it to its proper stacking position
       
  2333                         if (winid)
       
  2334                             w->raise();
       
  2335                     }
       
  2336                 }
       
  2337             }
       
  2338         } else {
       
  2339             q->create();
       
  2340         }
       
  2341     }
       
  2342 }
       
  2343 
       
  2344 
       
  2345 /*!
       
  2346 \internal
       
  2347 Ensures that the widget has a window system identifier, i.e. that it is known to the windowing system.
       
  2348 
       
  2349 */
       
  2350 
       
  2351 void QWidget::createWinId()
       
  2352 {
       
  2353     Q_D(QWidget);
       
  2354 //    qWarning("QWidget::createWinId is obsolete, please fix your code.");
       
  2355     d->createWinId();
       
  2356 }
       
  2357 
       
  2358 /*!
       
  2359     \since 4.4
       
  2360 
       
  2361     Returns the effective window system identifier of the widget, i.e. the
       
  2362     native parent's window system identifier.
       
  2363 
       
  2364     If the widget is native, this function returns the native widget ID.
       
  2365     Otherwise, the window ID of the first native parent widget, i.e., the
       
  2366     top-level widget that contains this widget, is returned.
       
  2367 
       
  2368     \note We recommend that you do not store this value as it is likely to
       
  2369     change at run-time.
       
  2370 
       
  2371     \sa nativeParentWidget()
       
  2372 */
       
  2373 WId QWidget::effectiveWinId() const
       
  2374 {
       
  2375     WId id = internalWinId();
       
  2376     if (id || !testAttribute(Qt::WA_WState_Created))
       
  2377         return id;
       
  2378     QWidget *realParent = nativeParentWidget();
       
  2379     Q_ASSERT(realParent);
       
  2380     Q_ASSERT(realParent->internalWinId());
       
  2381     return realParent->internalWinId();
       
  2382 }
       
  2383 
       
  2384 #ifndef QT_NO_STYLE_STYLESHEET
       
  2385 
       
  2386 /*!
       
  2387     \property QWidget::styleSheet
       
  2388     \brief the widget's style sheet
       
  2389     \since 4.2
       
  2390 
       
  2391     The style sheet contains a textual description of customizations to the
       
  2392     widget's style, as described in the \l{Qt Style Sheets} document.
       
  2393 
       
  2394     Since Qt 4.5, Qt style sheets fully supports Mac OS X.
       
  2395 
       
  2396     \warning Qt style sheets are currently not supported for custom QStyle
       
  2397     subclasses. We plan to address this in some future release.
       
  2398 
       
  2399     \sa setStyle(), QApplication::styleSheet, {Qt Style Sheets}
       
  2400 */
       
  2401 QString QWidget::styleSheet() const
       
  2402 {
       
  2403     Q_D(const QWidget);
       
  2404     if (!d->extra)
       
  2405         return QString();
       
  2406     return d->extra->styleSheet;
       
  2407 }
       
  2408 
       
  2409 void QWidget::setStyleSheet(const QString& styleSheet)
       
  2410 {
       
  2411     Q_D(QWidget);
       
  2412     d->createExtra();
       
  2413 
       
  2414     QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(d->extra->style);
       
  2415     d->extra->styleSheet = styleSheet;
       
  2416     if (styleSheet.isEmpty()) { // stylesheet removed
       
  2417         if (!proxy)
       
  2418             return;
       
  2419 
       
  2420         d->inheritStyle();
       
  2421         return;
       
  2422     }
       
  2423 
       
  2424     if (proxy) { // style sheet update
       
  2425         proxy->repolish(this);
       
  2426         return;
       
  2427     }
       
  2428 
       
  2429     if (testAttribute(Qt::WA_SetStyle)) {
       
  2430         d->setStyle_helper(new QStyleSheetStyle(d->extra->style), true);
       
  2431     } else {
       
  2432         d->setStyle_helper(new QStyleSheetStyle(0), true);
       
  2433     }
       
  2434 }
       
  2435 
       
  2436 #endif // QT_NO_STYLE_STYLESHEET
       
  2437 
       
  2438 /*!
       
  2439     \sa QWidget::setStyle(), QApplication::setStyle(), QApplication::style()
       
  2440 */
       
  2441 
       
  2442 QStyle *QWidget::style() const
       
  2443 {
       
  2444     Q_D(const QWidget);
       
  2445 
       
  2446     if (d->extra && d->extra->style)
       
  2447         return d->extra->style;
       
  2448     return QApplication::style();
       
  2449 }
       
  2450 
       
  2451 /*!
       
  2452     Sets the widget's GUI style to \a style. The ownership of the style
       
  2453     object is not transferred.
       
  2454 
       
  2455     If no style is set, the widget uses the application's style,
       
  2456     QApplication::style() instead.
       
  2457 
       
  2458     Setting a widget's style has no effect on existing or future child
       
  2459     widgets.
       
  2460 
       
  2461     \warning This function is particularly useful for demonstration
       
  2462     purposes, where you want to show Qt's styling capabilities. Real
       
  2463     applications should avoid it and use one consistent GUI style
       
  2464     instead.
       
  2465 
       
  2466     \warning Qt style sheets are currently not supported for custom QStyle
       
  2467     subclasses. We plan to address this in some future release.
       
  2468 
       
  2469     \sa style(), QStyle, QApplication::style(), QApplication::setStyle()
       
  2470 */
       
  2471 
       
  2472 void QWidget::setStyle(QStyle *style)
       
  2473 {
       
  2474     Q_D(QWidget);
       
  2475     setAttribute(Qt::WA_SetStyle, style != 0);
       
  2476     d->createExtra();
       
  2477 #ifndef QT_NO_STYLE_STYLESHEET
       
  2478     if (QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(style)) {
       
  2479         //if for some reason someone try to set a QStyleSheetStyle, ref it
       
  2480         //(this may happen for exemple in QButtonDialogBox which propagates its style)
       
  2481         proxy->ref();
       
  2482         d->setStyle_helper(style, false);
       
  2483     } else if (qobject_cast<QStyleSheetStyle *>(d->extra->style) || !qApp->styleSheet().isEmpty()) {
       
  2484         // if we have an application stylesheet or have a proxy already, propagate
       
  2485         d->setStyle_helper(new QStyleSheetStyle(style), true);
       
  2486     } else
       
  2487 #endif
       
  2488         d->setStyle_helper(style, false);
       
  2489 }
       
  2490 
       
  2491 void QWidgetPrivate::setStyle_helper(QStyle *newStyle, bool propagate, bool
       
  2492 #ifdef Q_WS_MAC
       
  2493         metalHack
       
  2494 #endif
       
  2495         )
       
  2496 {
       
  2497     Q_Q(QWidget);
       
  2498     QStyle *oldStyle  = q->style();
       
  2499 #ifndef QT_NO_STYLE_STYLESHEET
       
  2500     QStyle *origStyle = 0;
       
  2501 #endif
       
  2502 
       
  2503 #ifdef Q_WS_MAC
       
  2504     // the metalhack boolean allows Qt/Mac to do a proper re-polish depending
       
  2505     // on how the Qt::WA_MacBrushedMetal attribute is set. It is only ever
       
  2506     // set when changing that attribute and passes the widget's CURRENT style.
       
  2507     // therefore no need to do a reassignment.
       
  2508     if (!metalHack)
       
  2509 #endif
       
  2510     {
       
  2511         createExtra();
       
  2512 
       
  2513 #ifndef QT_NO_STYLE_STYLESHEET
       
  2514         origStyle = extra->style;
       
  2515 #endif
       
  2516         extra->style = newStyle;
       
  2517     }
       
  2518 
       
  2519     // repolish
       
  2520     if (q->windowType() != Qt::Desktop) {
       
  2521         if (polished) {
       
  2522             oldStyle->unpolish(q);
       
  2523 #ifdef Q_WS_MAC
       
  2524             if (metalHack)
       
  2525                 macUpdateMetalAttribute();
       
  2526 #endif
       
  2527             q->style()->polish(q);
       
  2528 #ifdef Q_WS_MAC
       
  2529         } else if (metalHack) {
       
  2530             macUpdateMetalAttribute();
       
  2531 #endif
       
  2532         }
       
  2533     }
       
  2534 
       
  2535     if (propagate) {
       
  2536         for (int i = 0; i < children.size(); ++i) {
       
  2537             QWidget *c = qobject_cast<QWidget*>(children.at(i));
       
  2538             if (c)
       
  2539                 c->d_func()->inheritStyle();
       
  2540         }
       
  2541     }
       
  2542 
       
  2543     QEvent e(QEvent::StyleChange);
       
  2544     QApplication::sendEvent(q, &e);
       
  2545 #ifdef QT3_SUPPORT
       
  2546     q->styleChange(*oldStyle);
       
  2547 #endif
       
  2548 
       
  2549 #ifndef QT_NO_STYLE_STYLESHEET
       
  2550     if (!qobject_cast<QStyleSheetStyle*>(newStyle)) {
       
  2551         if (const QStyleSheetStyle* cssStyle = qobject_cast<QStyleSheetStyle*>(origStyle)) {
       
  2552             cssStyle->clearWidgetFont(q);
       
  2553         }
       
  2554     }
       
  2555 #endif
       
  2556 
       
  2557 #ifndef QT_NO_STYLE_STYLESHEET
       
  2558     // dereference the old stylesheet style
       
  2559     if (QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(origStyle))
       
  2560         proxy->deref();
       
  2561 #endif
       
  2562 }
       
  2563 
       
  2564 // Inherits style from the current parent and propagates it as necessary
       
  2565 void QWidgetPrivate::inheritStyle()
       
  2566 {
       
  2567 #ifndef QT_NO_STYLE_STYLESHEET
       
  2568     Q_Q(QWidget);
       
  2569 
       
  2570     QStyleSheetStyle *proxy = extra ? qobject_cast<QStyleSheetStyle *>(extra->style) : 0;
       
  2571 
       
  2572     if (!q->styleSheet().isEmpty()) {
       
  2573         Q_ASSERT(proxy);
       
  2574         proxy->repolish(q);
       
  2575         return;
       
  2576     }
       
  2577 
       
  2578     QStyle *origStyle = proxy ? proxy->base : (extra ? (QStyle*)extra->style : 0);
       
  2579     QWidget *parent = q->parentWidget();
       
  2580     QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style : 0;
       
  2581     // If we have stylesheet on app or parent has stylesheet style, we need
       
  2582     // to be running a proxy
       
  2583     if (!qApp->styleSheet().isEmpty() || qobject_cast<QStyleSheetStyle *>(parentStyle)) {
       
  2584         QStyle *newStyle = parentStyle;
       
  2585         if (q->testAttribute(Qt::WA_SetStyle))
       
  2586             newStyle = new QStyleSheetStyle(origStyle);
       
  2587         else if (QStyleSheetStyle *newProxy = qobject_cast<QStyleSheetStyle *>(parentStyle))
       
  2588             newProxy->ref();
       
  2589 
       
  2590         setStyle_helper(newStyle, true);
       
  2591         return;
       
  2592     }
       
  2593 
       
  2594     // So, we have no stylesheet on parent/app and we have an empty stylesheet
       
  2595     // we just need our original style back
       
  2596     if (origStyle == (extra ? (QStyle*)extra->style : 0)) // is it any different?
       
  2597         return;
       
  2598 
       
  2599     // We could have inherited the proxy from our parent (which has a custom style)
       
  2600     // In such a case we need to start following the application style (i.e revert
       
  2601     // the propagation behavior of QStyleSheetStyle)
       
  2602     if (!q->testAttribute(Qt::WA_SetStyle))
       
  2603         origStyle = 0;
       
  2604 
       
  2605     setStyle_helper(origStyle, true);
       
  2606 #endif // QT_NO_STYLE_STYLESHEET
       
  2607 }
       
  2608 
       
  2609 #ifdef QT3_SUPPORT
       
  2610 /*!
       
  2611     \overload
       
  2612 
       
  2613     Sets the widget's GUI style to \a style using the QStyleFactory.
       
  2614 */
       
  2615 QStyle* QWidget::setStyle(const QString &style)
       
  2616 {
       
  2617     QStyle *s = QStyleFactory::create(style);
       
  2618     setStyle(s);
       
  2619     return s;
       
  2620 }
       
  2621 #endif
       
  2622 
       
  2623 /*!
       
  2624     \fn bool QWidget::isWindow() const
       
  2625 
       
  2626     Returns true if the widget is an independent window, otherwise
       
  2627     returns false.
       
  2628 
       
  2629     A window is a widget that isn't visually the child of any other
       
  2630     widget and that usually has a frame and a
       
  2631     \l{QWidget::setWindowTitle()}{window title}.
       
  2632 
       
  2633     A window can have a \l{QWidget::parentWidget()}{parent widget}.
       
  2634     It will then be grouped with its parent and deleted when the
       
  2635     parent is deleted, minimized when the parent is minimized etc. If
       
  2636     supported by the window manager, it will also have a common
       
  2637     taskbar entry with its parent.
       
  2638 
       
  2639     QDialog and QMainWindow widgets are by default windows, even if a
       
  2640     parent widget is specified in the constructor. This behavior is
       
  2641     specified by the Qt::Window flag.
       
  2642 
       
  2643     \sa window(), isModal(), parentWidget()
       
  2644 */
       
  2645 
       
  2646 /*!
       
  2647     \property QWidget::modal
       
  2648     \brief whether the widget is a modal widget
       
  2649 
       
  2650     This property only makes sense for windows. A modal widget
       
  2651     prevents widgets in all other windows from getting any input.
       
  2652 
       
  2653     By default, this property is false.
       
  2654 
       
  2655     \sa isWindow(), windowModality, QDialog
       
  2656 */
       
  2657 
       
  2658 /*!
       
  2659     \property QWidget::windowModality
       
  2660     \brief which windows are blocked by the modal widget
       
  2661     \since 4.1
       
  2662 
       
  2663     This property only makes sense for windows. A modal widget
       
  2664     prevents widgets in other windows from getting input. The value of
       
  2665     this property controls which windows are blocked when the widget
       
  2666     is visible. Changing this property while the window is visible has
       
  2667     no effect; you must hide() the widget first, then show() it again.
       
  2668 
       
  2669     By default, this property is Qt::NonModal.
       
  2670 
       
  2671     \sa isWindow(), QWidget::modal, QDialog
       
  2672 */
       
  2673 
       
  2674 Qt::WindowModality QWidget::windowModality() const
       
  2675 {
       
  2676     return static_cast<Qt::WindowModality>(data->window_modality);
       
  2677 }
       
  2678 
       
  2679 void QWidget::setWindowModality(Qt::WindowModality windowModality)
       
  2680 {
       
  2681     data->window_modality = windowModality;
       
  2682     // setModal_sys() will be called by setAttribute()
       
  2683     setAttribute(Qt::WA_ShowModal, (data->window_modality != Qt::NonModal));
       
  2684     setAttribute(Qt::WA_SetWindowModality, true);
       
  2685 }
       
  2686 
       
  2687 /*!
       
  2688     \fn bool QWidget::underMouse() const
       
  2689 
       
  2690     Returns true if the widget is under the mouse cursor; otherwise
       
  2691     returns false.
       
  2692 
       
  2693     This value is not updated properly during drag and drop
       
  2694     operations.
       
  2695 
       
  2696     \sa enterEvent(), leaveEvent()
       
  2697 */
       
  2698 
       
  2699 /*!
       
  2700     \property QWidget::minimized
       
  2701     \brief whether this widget is minimized (iconified)
       
  2702 
       
  2703     This property is only relevant for windows.
       
  2704 
       
  2705     By default, this property is false.
       
  2706 
       
  2707     \sa showMinimized(), visible, show(), hide(), showNormal(), maximized
       
  2708 */
       
  2709 bool QWidget::isMinimized() const
       
  2710 { return data->window_state & Qt::WindowMinimized; }
       
  2711 
       
  2712 /*!
       
  2713     Shows the widget minimized, as an icon.
       
  2714 
       
  2715     Calling this function only affects \l{isWindow()}{windows}.
       
  2716 
       
  2717     \sa showNormal(), showMaximized(), show(), hide(), isVisible(),
       
  2718         isMinimized()
       
  2719 */
       
  2720 void QWidget::showMinimized()
       
  2721 {
       
  2722     bool isMin = isMinimized();
       
  2723     if (isMin && isVisible())
       
  2724         return;
       
  2725 
       
  2726     ensurePolished();
       
  2727 #ifdef QT3_SUPPORT
       
  2728     if (parent())
       
  2729         QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
       
  2730 #endif
       
  2731 
       
  2732     if (!isMin)
       
  2733         setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
       
  2734     show();
       
  2735 }
       
  2736 
       
  2737 /*!
       
  2738     \property QWidget::maximized
       
  2739     \brief whether this widget is maximized
       
  2740 
       
  2741     This property is only relevant for windows.
       
  2742 
       
  2743     \note Due to limitations on some window systems, this does not always
       
  2744     report the expected results (e.g., if the user on X11 maximizes the
       
  2745     window via the window manager, Qt has no way of distinguishing this
       
  2746     from any other resize). This is expected to improve as window manager
       
  2747     protocols evolve.
       
  2748 
       
  2749     By default, this property is false.
       
  2750 
       
  2751     \sa windowState(), showMaximized(), visible, show(), hide(), showNormal(), minimized
       
  2752 */
       
  2753 bool QWidget::isMaximized() const
       
  2754 { return data->window_state & Qt::WindowMaximized; }
       
  2755 
       
  2756 
       
  2757 
       
  2758 /*!
       
  2759     Returns the current window state. The window state is a OR'ed
       
  2760     combination of Qt::WindowState: Qt::WindowMinimized,
       
  2761     Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.
       
  2762 
       
  2763   \sa Qt::WindowState setWindowState()
       
  2764  */
       
  2765 Qt::WindowStates QWidget::windowState() const
       
  2766 {
       
  2767     return Qt::WindowStates(data->window_state);
       
  2768 }
       
  2769 
       
  2770 /*!\internal
       
  2771 
       
  2772    The function sets the window state on child widgets similar to
       
  2773    setWindowState(). The difference is that the window state changed
       
  2774    event has the isOverride() flag set. It exists mainly to keep
       
  2775    Q3Workspace working.
       
  2776  */
       
  2777 void QWidget::overrideWindowState(Qt::WindowStates newstate)
       
  2778 {
       
  2779     QWindowStateChangeEvent e(Qt::WindowStates(data->window_state), true);
       
  2780     data->window_state  = newstate;
       
  2781     QApplication::sendEvent(this, &e);
       
  2782 }
       
  2783 
       
  2784 /*!
       
  2785     \fn void QWidget::setWindowState(Qt::WindowStates windowState)
       
  2786 
       
  2787     Sets the window state to \a windowState. The window state is a OR'ed
       
  2788     combination of Qt::WindowState: Qt::WindowMinimized,
       
  2789     Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.
       
  2790 
       
  2791     If the window is not visible (i.e. isVisible() returns false), the
       
  2792     window state will take effect when show() is called. For visible
       
  2793     windows, the change is immediate. For example, to toggle between
       
  2794     full-screen and normal mode, use the following code:
       
  2795 
       
  2796     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 0
       
  2797 
       
  2798     In order to restore and activate a minimized window (while
       
  2799     preserving its maximized and/or full-screen state), use the following:
       
  2800 
       
  2801     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 1
       
  2802 
       
  2803     Calling this function will hide the widget. You must call show() to make
       
  2804     the widget visible again.
       
  2805 
       
  2806     \note On some window systems Qt::WindowActive is not immediate, and may be
       
  2807     ignored in certain cases.
       
  2808 
       
  2809     When the window state changes, the widget receives a changeEvent()
       
  2810     of type QEvent::WindowStateChange.
       
  2811 
       
  2812     \sa Qt::WindowState windowState()
       
  2813 */
       
  2814 
       
  2815 /*!
       
  2816     \property QWidget::fullScreen
       
  2817     \brief whether the widget is shown in full screen mode
       
  2818 
       
  2819     A widget in full screen mode occupies the whole screen area and does not
       
  2820     display window decorations, such as a title bar.
       
  2821 
       
  2822     By default, this property is false.
       
  2823 
       
  2824     \sa windowState(), minimized, maximized
       
  2825 */
       
  2826 bool QWidget::isFullScreen() const
       
  2827 { return data->window_state & Qt::WindowFullScreen; }
       
  2828 
       
  2829 /*!
       
  2830     Shows the widget in full-screen mode.
       
  2831 
       
  2832     Calling this function only affects \l{isWindow()}{windows}.
       
  2833 
       
  2834     To return from full-screen mode, call showNormal().
       
  2835 
       
  2836     Full-screen mode works fine under Windows, but has certain
       
  2837     problems under X. These problems are due to limitations of the
       
  2838     ICCCM protocol that specifies the communication between X11
       
  2839     clients and the window manager. ICCCM simply does not understand
       
  2840     the concept of non-decorated full-screen windows. Therefore, the
       
  2841     best we can do is to request a borderless window and place and
       
  2842     resize it to fill the entire screen. Depending on the window
       
  2843     manager, this may or may not work. The borderless window is
       
  2844     requested using MOTIF hints, which are at least partially
       
  2845     supported by virtually all modern window managers.
       
  2846 
       
  2847     An alternative would be to bypass the window manager entirely and
       
  2848     create a window with the Qt::X11BypassWindowManagerHint flag. This
       
  2849     has other severe problems though, like totally broken keyboard focus
       
  2850     and very strange effects on desktop changes or when the user raises
       
  2851     other windows.
       
  2852 
       
  2853     X11 window managers that follow modern post-ICCCM specifications
       
  2854     support full-screen mode properly.
       
  2855 
       
  2856     \sa showNormal(), showMaximized(), show(), hide(), isVisible()
       
  2857 */
       
  2858 void QWidget::showFullScreen()
       
  2859 {
       
  2860     ensurePolished();
       
  2861 #ifdef QT3_SUPPORT
       
  2862     if (parent())
       
  2863         QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
       
  2864 #endif
       
  2865 
       
  2866     setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
       
  2867                    | Qt::WindowFullScreen);
       
  2868     show();
       
  2869     activateWindow();
       
  2870 }
       
  2871 
       
  2872 /*!
       
  2873     Shows the widget maximized.
       
  2874 
       
  2875     Calling this function only affects \l{isWindow()}{windows}.
       
  2876 
       
  2877     On X11, this function may not work properly with certain window
       
  2878     managers. See the \l{Window Geometry} documentation for an explanation.
       
  2879 
       
  2880     \sa setWindowState(), showNormal(), showMinimized(), show(), hide(), isVisible()
       
  2881 */
       
  2882 void QWidget::showMaximized()
       
  2883 {
       
  2884     ensurePolished();
       
  2885 #ifdef QT3_SUPPORT
       
  2886     if (parent())
       
  2887         QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
       
  2888 #endif
       
  2889 
       
  2890     setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
       
  2891                    | Qt::WindowMaximized);
       
  2892     show();
       
  2893 }
       
  2894 
       
  2895 /*!
       
  2896     Restores the widget after it has been maximized or minimized.
       
  2897 
       
  2898     Calling this function only affects \l{isWindow()}{windows}.
       
  2899 
       
  2900     \sa setWindowState(), showMinimized(), showMaximized(), show(), hide(), isVisible()
       
  2901 */
       
  2902 void QWidget::showNormal()
       
  2903 {
       
  2904     ensurePolished();
       
  2905 #ifdef QT3_SUPPORT
       
  2906     if (parent())
       
  2907         QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
       
  2908 #endif
       
  2909 
       
  2910     setWindowState(windowState() & ~(Qt::WindowMinimized
       
  2911                                      | Qt::WindowMaximized
       
  2912                                      | Qt::WindowFullScreen));
       
  2913     show();
       
  2914 }
       
  2915 
       
  2916 /*!
       
  2917     Returns true if this widget would become enabled if \a ancestor is
       
  2918     enabled; otherwise returns false.
       
  2919 
       
  2920 
       
  2921 
       
  2922     This is the case if neither the widget itself nor every parent up
       
  2923     to but excluding \a ancestor has been explicitly disabled.
       
  2924 
       
  2925     isEnabledTo(0) is equivalent to isEnabled().
       
  2926 
       
  2927     \sa setEnabled() enabled
       
  2928 */
       
  2929 
       
  2930 bool QWidget::isEnabledTo(QWidget* ancestor) const
       
  2931 {
       
  2932     const QWidget * w = this;
       
  2933     while (!w->testAttribute(Qt::WA_ForceDisabled)
       
  2934             && !w->isWindow()
       
  2935             && w->parentWidget()
       
  2936             && w->parentWidget() != ancestor)
       
  2937         w = w->parentWidget();
       
  2938     return !w->testAttribute(Qt::WA_ForceDisabled);
       
  2939 }
       
  2940 
       
  2941 #ifndef QT_NO_ACTION
       
  2942 /*!
       
  2943     Appends the action \a action to this widget's list of actions.
       
  2944 
       
  2945     All QWidgets have a list of \l{QAction}s, however they can be
       
  2946     represented graphically in many different ways. The default use of
       
  2947     the QAction list (as returned by actions()) is to create a context
       
  2948     QMenu.
       
  2949 
       
  2950     A QWidget should only have one of each action and adding an action
       
  2951     it already has will not cause the same action to be in the widget twice.
       
  2952 
       
  2953     The ownership of \a action is not transferred to this QWidget.
       
  2954 
       
  2955     \sa removeAction(), insertAction(), actions(), QMenu
       
  2956 */
       
  2957 void QWidget::addAction(QAction *action)
       
  2958 {
       
  2959     insertAction(0, action);
       
  2960 }
       
  2961 
       
  2962 /*!
       
  2963     Appends the actions \a actions to this widget's list of actions.
       
  2964 
       
  2965     \sa removeAction(), QMenu, addAction()
       
  2966 */
       
  2967 void QWidget::addActions(QList<QAction*> actions)
       
  2968 {
       
  2969     for(int i = 0; i < actions.count(); i++)
       
  2970         insertAction(0, actions.at(i));
       
  2971 }
       
  2972 
       
  2973 /*!
       
  2974     Inserts the action \a action to this widget's list of actions,
       
  2975     before the action \a before. It appends the action if \a before is 0 or
       
  2976     \a before is not a valid action for this widget.
       
  2977 
       
  2978     A QWidget should only have one of each action.
       
  2979 
       
  2980     \sa removeAction(), addAction(), QMenu, contextMenuPolicy, actions()
       
  2981 */
       
  2982 void QWidget::insertAction(QAction *before, QAction *action)
       
  2983 {
       
  2984     if(!action) {
       
  2985         qWarning("QWidget::insertAction: Attempt to insert null action");
       
  2986         return;
       
  2987     }
       
  2988 
       
  2989     Q_D(QWidget);
       
  2990     if(d->actions.contains(action))
       
  2991         removeAction(action);
       
  2992 
       
  2993     int pos = d->actions.indexOf(before);
       
  2994     if (pos < 0) {
       
  2995         before = 0;
       
  2996         pos = d->actions.size();
       
  2997     }
       
  2998     d->actions.insert(pos, action);
       
  2999 
       
  3000     QActionPrivate *apriv = action->d_func();
       
  3001     apriv->widgets.append(this);
       
  3002 
       
  3003     QActionEvent e(QEvent::ActionAdded, action, before);
       
  3004     QApplication::sendEvent(this, &e);
       
  3005 }
       
  3006 
       
  3007 /*!
       
  3008     Inserts the actions \a actions to this widget's list of actions,
       
  3009     before the action \a before. It appends the action if \a before is 0 or
       
  3010     \a before is not a valid action for this widget.
       
  3011 
       
  3012     A QWidget can have at most one of each action.
       
  3013 
       
  3014     \sa removeAction(), QMenu, insertAction(), contextMenuPolicy
       
  3015 */
       
  3016 void QWidget::insertActions(QAction *before, QList<QAction*> actions)
       
  3017 {
       
  3018     for(int i = 0; i < actions.count(); ++i)
       
  3019         insertAction(before, actions.at(i));
       
  3020 }
       
  3021 
       
  3022 /*!
       
  3023     Removes the action \a action from this widget's list of actions.
       
  3024     \sa insertAction(), actions(), insertAction()
       
  3025 */
       
  3026 void QWidget::removeAction(QAction *action)
       
  3027 {
       
  3028     if (!action)
       
  3029         return;
       
  3030 
       
  3031     Q_D(QWidget);
       
  3032 
       
  3033     QActionPrivate *apriv = action->d_func();
       
  3034     apriv->widgets.removeAll(this);
       
  3035 
       
  3036     if (d->actions.removeAll(action)) {
       
  3037         QActionEvent e(QEvent::ActionRemoved, action);
       
  3038         QApplication::sendEvent(this, &e);
       
  3039     }
       
  3040 }
       
  3041 
       
  3042 /*!
       
  3043     Returns the (possibly empty) list of this widget's actions.
       
  3044 
       
  3045     \sa contextMenuPolicy, insertAction(), removeAction()
       
  3046 */
       
  3047 QList<QAction*> QWidget::actions() const
       
  3048 {
       
  3049     Q_D(const QWidget);
       
  3050     return d->actions;
       
  3051 }
       
  3052 #endif // QT_NO_ACTION
       
  3053 
       
  3054 /*!
       
  3055   \fn bool QWidget::isEnabledToTLW() const
       
  3056   \obsolete
       
  3057 
       
  3058   This function is deprecated. It is equivalent to isEnabled()
       
  3059 */
       
  3060 
       
  3061 /*!
       
  3062     \property QWidget::enabled
       
  3063     \brief whether the widget is enabled
       
  3064 
       
  3065     An enabled widget handles keyboard and mouse events; a disabled
       
  3066     widget does not.
       
  3067 
       
  3068     Some widgets display themselves differently when they are
       
  3069     disabled. For example a button might draw its label grayed out. If
       
  3070     your widget needs to know when it becomes enabled or disabled, you
       
  3071     can use the changeEvent() with type QEvent::EnabledChange.
       
  3072 
       
  3073     Disabling a widget implicitly disables all its children. Enabling
       
  3074     respectively enables all child widgets unless they have been
       
  3075     explicitly disabled.
       
  3076 
       
  3077     By default, this property is true.
       
  3078 
       
  3079     \sa isEnabledTo(), QKeyEvent, QMouseEvent, changeEvent()
       
  3080 */
       
  3081 void QWidget::setEnabled(bool enable)
       
  3082 {
       
  3083     Q_D(QWidget);
       
  3084     setAttribute(Qt::WA_ForceDisabled, !enable);
       
  3085     d->setEnabled_helper(enable);
       
  3086 }
       
  3087 
       
  3088 void QWidgetPrivate::setEnabled_helper(bool enable)
       
  3089 {
       
  3090     Q_Q(QWidget);
       
  3091 
       
  3092     if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->isEnabled())
       
  3093         return; // nothing we can do
       
  3094 
       
  3095     if (enable != q->testAttribute(Qt::WA_Disabled))
       
  3096         return; // nothing to do
       
  3097 
       
  3098     q->setAttribute(Qt::WA_Disabled, !enable);
       
  3099     updateSystemBackground();
       
  3100 
       
  3101     if (!enable && q->window()->focusWidget() == q) {
       
  3102         bool parentIsEnabled = (!q->parentWidget() || q->parentWidget()->isEnabled());
       
  3103         if (!parentIsEnabled || !q->focusNextChild())
       
  3104             q->clearFocus();
       
  3105     }
       
  3106 
       
  3107     Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceDisabled : Qt::WA_Disabled;
       
  3108     for (int i = 0; i < children.size(); ++i) {
       
  3109         QWidget *w = qobject_cast<QWidget *>(children.at(i));
       
  3110         if (w && !w->testAttribute(attribute))
       
  3111             w->d_func()->setEnabled_helper(enable);
       
  3112     }
       
  3113 #if defined(Q_WS_X11)
       
  3114     if (q->testAttribute(Qt::WA_SetCursor) || q->isWindow()) {
       
  3115         // enforce the windows behavior of clearing the cursor on
       
  3116         // disabled widgets
       
  3117         qt_x11_enforce_cursor(q);
       
  3118     }
       
  3119 #endif
       
  3120 #if defined(Q_WS_MAC)
       
  3121     setEnabled_helper_sys(enable);
       
  3122 #endif
       
  3123 #ifndef QT_NO_IM
       
  3124     if (q->testAttribute(Qt::WA_InputMethodEnabled) && q->hasFocus()) {
       
  3125         QWidget *focusWidget = effectiveFocusWidget();
       
  3126         QInputContext *qic = focusWidget->d_func()->inputContext();
       
  3127         if (enable) {
       
  3128             if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
       
  3129                 qic->setFocusWidget(focusWidget);
       
  3130         } else {
       
  3131             qic->reset();
       
  3132             qic->setFocusWidget(0);
       
  3133         }
       
  3134     }
       
  3135 #endif //QT_NO_IM
       
  3136     QEvent e(QEvent::EnabledChange);
       
  3137     QApplication::sendEvent(q, &e);
       
  3138 #ifdef QT3_SUPPORT
       
  3139     q->enabledChange(!enable); // compatibility
       
  3140 #endif
       
  3141 }
       
  3142 
       
  3143 /*!
       
  3144     \property QWidget::acceptDrops
       
  3145     \brief whether drop events are enabled for this widget
       
  3146 
       
  3147     Setting this property to true announces to the system that this
       
  3148     widget \e may be able to accept drop events.
       
  3149 
       
  3150     If the widget is the desktop (windowType() == Qt::Desktop), this may
       
  3151     fail if another application is using the desktop; you can call
       
  3152     acceptDrops() to test if this occurs.
       
  3153 
       
  3154     \warning Do not modify this property in a drag and drop event handler.
       
  3155 
       
  3156     By default, this property is false.
       
  3157 
       
  3158     \sa {Drag and Drop}
       
  3159 */
       
  3160 bool QWidget::acceptDrops() const
       
  3161 {
       
  3162     return testAttribute(Qt::WA_AcceptDrops);
       
  3163 }
       
  3164 
       
  3165 void QWidget::setAcceptDrops(bool on)
       
  3166 {
       
  3167     setAttribute(Qt::WA_AcceptDrops, on);
       
  3168 
       
  3169 }
       
  3170 
       
  3171 /*!
       
  3172     \fn void QWidget::enabledChange(bool)
       
  3173 
       
  3174     \internal
       
  3175     \obsolete
       
  3176 */
       
  3177 
       
  3178 /*!
       
  3179     \fn void QWidget::paletteChange(const QPalette &)
       
  3180 
       
  3181     \internal
       
  3182     \obsolete
       
  3183 */
       
  3184 
       
  3185 /*!
       
  3186     \fn void QWidget::fontChange(const QFont &)
       
  3187 
       
  3188     \internal
       
  3189     \obsolete
       
  3190 */
       
  3191 
       
  3192 /*!
       
  3193     \fn void QWidget::windowActivationChange(bool)
       
  3194 
       
  3195     \internal
       
  3196     \obsolete
       
  3197 */
       
  3198 
       
  3199 /*!
       
  3200     \fn void QWidget::languageChange()
       
  3201 
       
  3202     \obsolete
       
  3203 */
       
  3204 
       
  3205 /*!
       
  3206     \fn void QWidget::styleChange(QStyle& style)
       
  3207 
       
  3208     \internal
       
  3209     \obsolete
       
  3210 */
       
  3211 
       
  3212 /*!
       
  3213     Disables widget input events if \a disable is true; otherwise
       
  3214     enables input events.
       
  3215 
       
  3216     See the \l enabled documentation for more information.
       
  3217 
       
  3218     \sa isEnabledTo(), QKeyEvent, QMouseEvent, changeEvent()
       
  3219 */
       
  3220 void QWidget::setDisabled(bool disable)
       
  3221 {
       
  3222     setEnabled(!disable);
       
  3223 }
       
  3224 
       
  3225 /*!
       
  3226     \property QWidget::frameGeometry
       
  3227     \brief geometry of the widget relative to its parent including any
       
  3228     window frame
       
  3229 
       
  3230     See the \l{Window Geometry} documentation for an overview of geometry
       
  3231     issues with windows.
       
  3232 
       
  3233     By default, this property contains a value that depends on the user's
       
  3234     platform and screen geometry.
       
  3235 
       
  3236     \sa geometry() x() y() pos()
       
  3237 */
       
  3238 QRect QWidget::frameGeometry() const
       
  3239 {
       
  3240     Q_D(const QWidget);
       
  3241     if (isWindow() && ! (windowType() == Qt::Popup)) {
       
  3242         QRect fs = d->frameStrut();
       
  3243         return QRect(data->crect.x() - fs.left(),
       
  3244                      data->crect.y() - fs.top(),
       
  3245                      data->crect.width() + fs.left() + fs.right(),
       
  3246                      data->crect.height() + fs.top() + fs.bottom());
       
  3247     }
       
  3248     return data->crect;
       
  3249 }
       
  3250 
       
  3251 /*!
       
  3252     \property QWidget::x
       
  3253 
       
  3254     \brief the x coordinate of the widget relative to its parent including
       
  3255     any window frame
       
  3256 
       
  3257     See the \l{Window Geometry} documentation for an overview of geometry
       
  3258     issues with windows.
       
  3259 
       
  3260     By default, this property has a value of 0.
       
  3261 
       
  3262     \sa frameGeometry, y, pos
       
  3263 */
       
  3264 int QWidget::x() const
       
  3265 {
       
  3266     Q_D(const QWidget);
       
  3267     if (isWindow() && ! (windowType() == Qt::Popup))
       
  3268         return data->crect.x() - d->frameStrut().left();
       
  3269     return data->crect.x();
       
  3270 }
       
  3271 
       
  3272 /*!
       
  3273     \property QWidget::y
       
  3274     \brief the y coordinate of the widget relative to its parent and
       
  3275     including any window frame
       
  3276 
       
  3277     See the \l{Window Geometry} documentation for an overview of geometry
       
  3278     issues with windows.
       
  3279 
       
  3280     By default, this property has a value of 0.
       
  3281 
       
  3282     \sa frameGeometry, x, pos
       
  3283 */
       
  3284 int QWidget::y() const
       
  3285 {
       
  3286     Q_D(const QWidget);
       
  3287     if (isWindow() && ! (windowType() == Qt::Popup))
       
  3288         return data->crect.y() - d->frameStrut().top();
       
  3289     return data->crect.y();
       
  3290 }
       
  3291 
       
  3292 /*!
       
  3293     \property QWidget::pos
       
  3294     \brief the position of the widget within its parent widget
       
  3295 
       
  3296     If the widget is a window, the position is that of the widget on
       
  3297     the desktop, including its frame.
       
  3298 
       
  3299     When changing the position, the widget, if visible, receives a
       
  3300     move event (moveEvent()) immediately. If the widget is not
       
  3301     currently visible, it is guaranteed to receive an event before it
       
  3302     is shown.
       
  3303 
       
  3304     By default, this property contains a position that refers to the
       
  3305     origin.
       
  3306 
       
  3307     \warning Calling move() or setGeometry() inside moveEvent() can
       
  3308     lead to infinite recursion.
       
  3309 
       
  3310     See the \l{Window Geometry} documentation for an overview of geometry
       
  3311     issues with windows.
       
  3312 
       
  3313     \sa frameGeometry, size x(), y()
       
  3314 */
       
  3315 QPoint QWidget::pos() const
       
  3316 {
       
  3317     Q_D(const QWidget);
       
  3318     if (isWindow() && ! (windowType() == Qt::Popup)) {
       
  3319         QRect fs = d->frameStrut();
       
  3320         return QPoint(data->crect.x() - fs.left(), data->crect.y() - fs.top());
       
  3321     }
       
  3322     return data->crect.topLeft();
       
  3323 }
       
  3324 
       
  3325 /*!
       
  3326     \property QWidget::geometry
       
  3327     \brief the geometry of the widget relative to its parent and
       
  3328     excluding the window frame
       
  3329 
       
  3330     When changing the geometry, the widget, if visible, receives a
       
  3331     move event (moveEvent()) and/or a resize event (resizeEvent())
       
  3332     immediately. If the widget is not currently visible, it is
       
  3333     guaranteed to receive appropriate events before it is shown.
       
  3334 
       
  3335     The size component is adjusted if it lies outside the range
       
  3336     defined by minimumSize() and maximumSize().
       
  3337 
       
  3338     \warning Calling setGeometry() inside resizeEvent() or moveEvent()
       
  3339     can lead to infinite recursion.
       
  3340 
       
  3341     See the \l{Window Geometry} documentation for an overview of geometry
       
  3342     issues with windows.
       
  3343 
       
  3344     By default, this property contains a value that depends on the user's
       
  3345     platform and screen geometry.
       
  3346 
       
  3347     \sa frameGeometry(), rect(), move(), resize(), moveEvent(),
       
  3348         resizeEvent(), minimumSize(), maximumSize()
       
  3349 */
       
  3350 
       
  3351 /*!
       
  3352     \property QWidget::normalGeometry
       
  3353 
       
  3354     \brief the geometry of the widget as it will appear when shown as
       
  3355     a normal (not maximized or full screen) top-level widget
       
  3356 
       
  3357     For child widgets this property always holds an empty rectangle.
       
  3358 
       
  3359     By default, this property contains an empty rectangle.
       
  3360 
       
  3361     \sa QWidget::windowState(), QWidget::geometry
       
  3362 */
       
  3363 
       
  3364 /*!
       
  3365     \property QWidget::size
       
  3366     \brief the size of the widget excluding any window frame
       
  3367 
       
  3368     If the widget is visible when it is being resized, it receives a resize event
       
  3369     (resizeEvent()) immediately. If the widget is not currently
       
  3370     visible, it is guaranteed to receive an event before it is shown.
       
  3371 
       
  3372     The size is adjusted if it lies outside the range defined by
       
  3373     minimumSize() and maximumSize().
       
  3374 
       
  3375     By default, this property contains a value that depends on the user's
       
  3376     platform and screen geometry.
       
  3377 
       
  3378     \warning Calling resize() or setGeometry() inside resizeEvent() can
       
  3379     lead to infinite recursion.
       
  3380 
       
  3381     \note Setting the size to \c{QSize(0, 0)} will cause the widget to not
       
  3382     appear on screen. This also applies to windows.
       
  3383 
       
  3384     \sa pos, geometry, minimumSize, maximumSize, resizeEvent(), adjustSize()
       
  3385 */
       
  3386 
       
  3387 /*!
       
  3388     \property QWidget::width
       
  3389     \brief the width of the widget excluding any window frame
       
  3390 
       
  3391     See the \l{Window Geometry} documentation for an overview of geometry
       
  3392     issues with windows.
       
  3393 
       
  3394     \note Do not use this function to find the width of a screen on
       
  3395     a \l{QDesktopWidget}{multiple screen desktop}. Read
       
  3396     \l{QDesktopWidget#Screen Geometry}{this note} for details.
       
  3397 
       
  3398     By default, this property contains a value that depends on the user's
       
  3399     platform and screen geometry.
       
  3400 
       
  3401     \sa geometry, height, size
       
  3402 */
       
  3403 
       
  3404 /*!
       
  3405     \property QWidget::height
       
  3406     \brief the height of the widget excluding any window frame
       
  3407 
       
  3408     See the \l{Window Geometry} documentation for an overview of geometry
       
  3409     issues with windows.
       
  3410 
       
  3411     \note Do not use this function to find the height of a screen
       
  3412     on a \l{QDesktopWidget}{multiple screen desktop}. Read
       
  3413     \l{QDesktopWidget#Screen Geometry}{this note} for details.
       
  3414 
       
  3415     By default, this property contains a value that depends on the user's
       
  3416     platform and screen geometry.
       
  3417 
       
  3418     \sa geometry, width, size
       
  3419 */
       
  3420 
       
  3421 /*!
       
  3422     \property QWidget::rect
       
  3423     \brief the internal geometry of the widget excluding any window
       
  3424     frame
       
  3425 
       
  3426     The rect property equals QRect(0, 0, width(), height()).
       
  3427 
       
  3428     See the \l{Window Geometry} documentation for an overview of geometry
       
  3429     issues with windows.
       
  3430 
       
  3431     By default, this property contains a value that depends on the user's
       
  3432     platform and screen geometry.
       
  3433 
       
  3434     \sa size
       
  3435 */
       
  3436 
       
  3437 
       
  3438 QRect QWidget::normalGeometry() const
       
  3439 {
       
  3440     Q_D(const QWidget);
       
  3441     if (!d->extra || !d->extra->topextra)
       
  3442         return QRect();
       
  3443 
       
  3444     if (!isMaximized() && !isFullScreen())
       
  3445         return geometry();
       
  3446 
       
  3447     return d->topData()->normalGeometry;
       
  3448 }
       
  3449 
       
  3450 
       
  3451 /*!
       
  3452     \property QWidget::childrenRect
       
  3453     \brief the bounding rectangle of the widget's children
       
  3454 
       
  3455     Hidden children are excluded.
       
  3456 
       
  3457     By default, for a widget with no children, this property contains a
       
  3458     rectangle with zero width and height located at the origin.
       
  3459 
       
  3460     \sa childrenRegion() geometry()
       
  3461 */
       
  3462 
       
  3463 QRect QWidget::childrenRect() const
       
  3464 {
       
  3465     Q_D(const QWidget);
       
  3466     QRect r(0, 0, 0, 0);
       
  3467     for (int i = 0; i < d->children.size(); ++i) {
       
  3468         QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
       
  3469         if (w && !w->isWindow() && !w->isHidden())
       
  3470             r |= w->geometry();
       
  3471     }
       
  3472     return r;
       
  3473 }
       
  3474 
       
  3475 /*!
       
  3476     \property QWidget::childrenRegion
       
  3477     \brief the combined region occupied by the widget's children
       
  3478 
       
  3479     Hidden children are excluded.
       
  3480 
       
  3481     By default, for a widget with no children, this property contains an
       
  3482     empty region.
       
  3483 
       
  3484     \sa childrenRect() geometry() mask()
       
  3485 */
       
  3486 
       
  3487 QRegion QWidget::childrenRegion() const
       
  3488 {
       
  3489     Q_D(const QWidget);
       
  3490     QRegion r;
       
  3491     for (int i = 0; i < d->children.size(); ++i) {
       
  3492         QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
       
  3493         if (w && !w->isWindow() && !w->isHidden()) {
       
  3494             QRegion mask = w->mask();
       
  3495             if (mask.isEmpty())
       
  3496                 r |= w->geometry();
       
  3497             else
       
  3498                 r |= mask.translated(w->pos());
       
  3499         }
       
  3500     }
       
  3501     return r;
       
  3502 }
       
  3503 
       
  3504 
       
  3505 /*!
       
  3506     \property QWidget::minimumSize
       
  3507     \brief the widget's minimum size
       
  3508 
       
  3509     The widget cannot be resized to a smaller size than the minimum
       
  3510     widget size. The widget's size is forced to the minimum size if
       
  3511     the current size is smaller.
       
  3512 
       
  3513     The minimum size set by this function will override the minimum size
       
  3514     defined by QLayout. In order to unset the minimum size, use a
       
  3515     value of \c{QSize(0, 0)}.
       
  3516 
       
  3517     By default, this property contains a size with zero width and height.
       
  3518 
       
  3519     \sa minimumWidth, minimumHeight, maximumSize, sizeIncrement
       
  3520 */
       
  3521 
       
  3522 QSize QWidget::minimumSize() const
       
  3523 {
       
  3524     Q_D(const QWidget);
       
  3525     return d->extra ? QSize(d->extra->minw, d->extra->minh) : QSize(0, 0);
       
  3526 }
       
  3527 
       
  3528 /*!
       
  3529     \property QWidget::maximumSize
       
  3530     \brief the widget's maximum size in pixels
       
  3531 
       
  3532     The widget cannot be resized to a larger size than the maximum
       
  3533     widget size.
       
  3534 
       
  3535     By default, this property contains a size in which both width and height
       
  3536     have values of 16777215.
       
  3537 
       
  3538     \note The definition of the \c QWIDGETSIZE_MAX macro limits the maximum size
       
  3539     of widgets.
       
  3540 
       
  3541     \sa maximumWidth, maximumHeight, minimumSize, sizeIncrement
       
  3542 */
       
  3543 
       
  3544 QSize QWidget::maximumSize() const
       
  3545 {
       
  3546     Q_D(const QWidget);
       
  3547     return d->extra ? QSize(d->extra->maxw, d->extra->maxh)
       
  3548                  : QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
       
  3549 }
       
  3550 
       
  3551 
       
  3552 /*!
       
  3553     \property QWidget::minimumWidth
       
  3554     \brief the widget's minimum width in pixels
       
  3555 
       
  3556     This property corresponds to the width held by the \l minimumSize property.
       
  3557 
       
  3558     By default, this property has a value of 0.
       
  3559 
       
  3560     \sa minimumSize, minimumHeight
       
  3561 */
       
  3562 
       
  3563 /*!
       
  3564     \property QWidget::minimumHeight
       
  3565     \brief the widget's minimum height in pixels
       
  3566 
       
  3567     This property corresponds to the height held by the \l minimumSize property.
       
  3568 
       
  3569     By default, this property has a value of 0.
       
  3570 
       
  3571     \sa minimumSize, minimumWidth
       
  3572 */
       
  3573 
       
  3574 /*!
       
  3575     \property QWidget::maximumWidth
       
  3576     \brief the widget's maximum width in pixels
       
  3577 
       
  3578     This property corresponds to the width held by the \l maximumSize property.
       
  3579 
       
  3580     By default, this property contains a value of 16777215.
       
  3581 
       
  3582     \note The definition of the \c QWIDGETSIZE_MAX macro limits the maximum size
       
  3583     of widgets.
       
  3584 
       
  3585     \sa maximumSize, maximumHeight
       
  3586 */
       
  3587 
       
  3588 /*!
       
  3589     \property QWidget::maximumHeight
       
  3590     \brief the widget's maximum height in pixels
       
  3591 
       
  3592     This property corresponds to the height held by the \l maximumSize property.
       
  3593 
       
  3594     By default, this property contains a value of 16777215.
       
  3595 
       
  3596     \note The definition of the \c QWIDGETSIZE_MAX macro limits the maximum size
       
  3597     of widgets.
       
  3598 
       
  3599     \sa maximumSize, maximumWidth
       
  3600 */
       
  3601 
       
  3602 /*!
       
  3603     \property QWidget::sizeIncrement
       
  3604     \brief the size increment of the widget
       
  3605 
       
  3606     When the user resizes the window, the size will move in steps of
       
  3607     sizeIncrement().width() pixels horizontally and
       
  3608     sizeIncrement.height() pixels vertically, with baseSize() as the
       
  3609     basis. Preferred widget sizes are for non-negative integers \e i
       
  3610     and \e j:
       
  3611     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 2
       
  3612 
       
  3613     Note that while you can set the size increment for all widgets, it
       
  3614     only affects windows.
       
  3615 
       
  3616     By default, this property contains a size with zero width and height.
       
  3617 
       
  3618     \warning The size increment has no effect under Windows, and may
       
  3619     be disregarded by the window manager on X11.
       
  3620 
       
  3621     \sa size, minimumSize, maximumSize
       
  3622 */
       
  3623 QSize QWidget::sizeIncrement() const
       
  3624 {
       
  3625     Q_D(const QWidget);
       
  3626     return (d->extra && d->extra->topextra)
       
  3627         ? QSize(d->extra->topextra->incw, d->extra->topextra->inch)
       
  3628         : QSize(0, 0);
       
  3629 }
       
  3630 
       
  3631 /*!
       
  3632     \property QWidget::baseSize
       
  3633     \brief the base size of the widget
       
  3634 
       
  3635     The base size is used to calculate a proper widget size if the
       
  3636     widget defines sizeIncrement().
       
  3637 
       
  3638     By default, for a newly-created widget, this property contains a size with
       
  3639     zero width and height.
       
  3640 
       
  3641     \sa setSizeIncrement()
       
  3642 */
       
  3643 
       
  3644 QSize QWidget::baseSize() const
       
  3645 {
       
  3646     Q_D(const QWidget);
       
  3647     return (d->extra != 0 && d->extra->topextra != 0)
       
  3648         ? QSize(d->extra->topextra->basew, d->extra->topextra->baseh)
       
  3649         : QSize(0, 0);
       
  3650 }
       
  3651 
       
  3652 bool QWidgetPrivate::setMinimumSize_helper(int &minw, int &minh)
       
  3653 {
       
  3654     Q_Q(QWidget);
       
  3655 
       
  3656 #ifdef Q_WS_QWS
       
  3657     if (q->isWindow()) {
       
  3658         const QRect maxWindowRect = QApplication::desktop()->availableGeometry(QApplication::desktop()->screenNumber(q));
       
  3659         if (!maxWindowRect.isEmpty()) {
       
  3660             // ### This is really just a work-around. Layout shouldn't be
       
  3661             // asking for minimum sizes bigger than the screen.
       
  3662             if (minw > maxWindowRect.width())
       
  3663                 minw = maxWindowRect.width();
       
  3664             if (minh > maxWindowRect.height())
       
  3665                 minh = maxWindowRect.height();
       
  3666         }
       
  3667     }
       
  3668 #endif
       
  3669     int mw = minw, mh = minh;
       
  3670     if (mw == QWIDGETSIZE_MAX)
       
  3671         mw = 0;
       
  3672     if (mh == QWIDGETSIZE_MAX)
       
  3673         mh = 0;
       
  3674     if (minw > QWIDGETSIZE_MAX || minh > QWIDGETSIZE_MAX) {
       
  3675         qWarning("QWidget::setMinimumSize: (%s/%s) "
       
  3676                 "The largest allowed size is (%d,%d)",
       
  3677                  q->objectName().toLocal8Bit().data(), q->metaObject()->className(), QWIDGETSIZE_MAX,
       
  3678                 QWIDGETSIZE_MAX);
       
  3679         minw = mw = qMin<int>(minw, QWIDGETSIZE_MAX);
       
  3680         minh = mh = qMin<int>(minh, QWIDGETSIZE_MAX);
       
  3681     }
       
  3682     if (minw < 0 || minh < 0) {
       
  3683         qWarning("QWidget::setMinimumSize: (%s/%s) Negative sizes (%d,%d) "
       
  3684                 "are not possible",
       
  3685                 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), minw, minh);
       
  3686         minw = mw = qMax(minw, 0);
       
  3687         minh = mh = qMax(minh, 0);
       
  3688     }
       
  3689     createExtra();
       
  3690     if (extra->minw == mw && extra->minh == mh)
       
  3691         return false;
       
  3692     extra->minw = mw;
       
  3693     extra->minh = mh;
       
  3694     extra->explicitMinSize = (mw ? Qt::Horizontal : 0) | (mh ? Qt::Vertical : 0);
       
  3695     return true;
       
  3696 }
       
  3697 
       
  3698 /*!
       
  3699     \overload
       
  3700 
       
  3701     This function corresponds to setMinimumSize(QSize(minw, minh)).
       
  3702     Sets the minimum width to \a minw and the minimum height to \a
       
  3703     minh.
       
  3704 */
       
  3705 
       
  3706 void QWidget::setMinimumSize(int minw, int minh)
       
  3707 {
       
  3708     Q_D(QWidget);
       
  3709     if (!d->setMinimumSize_helper(minw, minh))
       
  3710         return;
       
  3711 
       
  3712     if (isWindow())
       
  3713         d->setConstraints_sys();
       
  3714     if (minw > width() || minh > height()) {
       
  3715         bool resized = testAttribute(Qt::WA_Resized);
       
  3716         bool maximized = isMaximized();
       
  3717         resize(qMax(minw,width()), qMax(minh,height()));
       
  3718         setAttribute(Qt::WA_Resized, resized); //not a user resize
       
  3719         if (maximized)
       
  3720             data->window_state = data->window_state | Qt::WindowMaximized;
       
  3721     }
       
  3722 #ifndef QT_NO_GRAPHICSVIEW
       
  3723     if (d->extra) {
       
  3724         if (d->extra->proxyWidget)
       
  3725             d->extra->proxyWidget->setMinimumSize(minw, minh);
       
  3726     }
       
  3727 #endif
       
  3728     d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
       
  3729 }
       
  3730 
       
  3731 bool QWidgetPrivate::setMaximumSize_helper(int &maxw, int &maxh)
       
  3732 {
       
  3733     Q_Q(QWidget);
       
  3734     if (maxw > QWIDGETSIZE_MAX || maxh > QWIDGETSIZE_MAX) {
       
  3735         qWarning("QWidget::setMaximumSize: (%s/%s) "
       
  3736                 "The largest allowed size is (%d,%d)",
       
  3737                  q->objectName().toLocal8Bit().data(), q->metaObject()->className(), QWIDGETSIZE_MAX,
       
  3738                 QWIDGETSIZE_MAX);
       
  3739         maxw = qMin<int>(maxw, QWIDGETSIZE_MAX);
       
  3740         maxh = qMin<int>(maxh, QWIDGETSIZE_MAX);
       
  3741     }
       
  3742     if (maxw < 0 || maxh < 0) {
       
  3743         qWarning("QWidget::setMaximumSize: (%s/%s) Negative sizes (%d,%d) "
       
  3744                 "are not possible",
       
  3745                 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), maxw, maxh);
       
  3746         maxw = qMax(maxw, 0);
       
  3747         maxh = qMax(maxh, 0);
       
  3748     }
       
  3749     createExtra();
       
  3750     if (extra->maxw == maxw && extra->maxh == maxh)
       
  3751         return false;
       
  3752     extra->maxw = maxw;
       
  3753     extra->maxh = maxh;
       
  3754     extra->explicitMaxSize = (maxw != QWIDGETSIZE_MAX ? Qt::Horizontal : 0) |
       
  3755                              (maxh != QWIDGETSIZE_MAX ? Qt::Vertical : 0);
       
  3756     return true;
       
  3757 }
       
  3758 
       
  3759 /*!
       
  3760     \overload
       
  3761 
       
  3762     This function corresponds to setMaximumSize(QSize(\a maxw, \a
       
  3763     maxh)). Sets the maximum width to \a maxw and the maximum height
       
  3764     to \a maxh.
       
  3765 */
       
  3766 void QWidget::setMaximumSize(int maxw, int maxh)
       
  3767 {
       
  3768     Q_D(QWidget);
       
  3769     if (!d->setMaximumSize_helper(maxw, maxh))
       
  3770         return;
       
  3771 
       
  3772     if (isWindow())
       
  3773         d->setConstraints_sys();
       
  3774     if (maxw < width() || maxh < height()) {
       
  3775         bool resized = testAttribute(Qt::WA_Resized);
       
  3776         resize(qMin(maxw,width()), qMin(maxh,height()));
       
  3777         setAttribute(Qt::WA_Resized, resized); //not a user resize
       
  3778     }
       
  3779 
       
  3780 #ifndef QT_NO_GRAPHICSVIEW
       
  3781     if (d->extra) {
       
  3782         if (d->extra->proxyWidget)
       
  3783             d->extra->proxyWidget->setMaximumSize(maxw, maxh);
       
  3784     }
       
  3785 #endif
       
  3786 
       
  3787     d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
       
  3788 }
       
  3789 
       
  3790 /*!
       
  3791     \overload
       
  3792 
       
  3793     Sets the x (width) size increment to \a w and the y (height) size
       
  3794     increment to \a h.
       
  3795 */
       
  3796 void QWidget::setSizeIncrement(int w, int h)
       
  3797 {
       
  3798     Q_D(QWidget);
       
  3799     d->createTLExtra();
       
  3800     QTLWExtra* x = d->topData();
       
  3801     if (x->incw == w && x->inch == h)
       
  3802         return;
       
  3803     x->incw = w;
       
  3804     x->inch = h;
       
  3805     if (isWindow())
       
  3806         d->setConstraints_sys();
       
  3807 }
       
  3808 
       
  3809 /*!
       
  3810     \overload
       
  3811 
       
  3812     This corresponds to setBaseSize(QSize(\a basew, \a baseh)). Sets
       
  3813     the widgets base size to width \a basew and height \a baseh.
       
  3814 */
       
  3815 void QWidget::setBaseSize(int basew, int baseh)
       
  3816 {
       
  3817     Q_D(QWidget);
       
  3818     d->createTLExtra();
       
  3819     QTLWExtra* x = d->topData();
       
  3820     if (x->basew == basew && x->baseh == baseh)
       
  3821         return;
       
  3822     x->basew = basew;
       
  3823     x->baseh = baseh;
       
  3824     if (isWindow())
       
  3825         d->setConstraints_sys();
       
  3826 }
       
  3827 
       
  3828 /*!
       
  3829     Sets both the minimum and maximum sizes of the widget to \a s,
       
  3830     thereby preventing it from ever growing or shrinking.
       
  3831 
       
  3832     This will override the default size constraints set by QLayout.
       
  3833 
       
  3834     To remove constraints, set the size to QWIDGETSIZE_MAX.
       
  3835 
       
  3836     Alternatively, if you want the widget to have a
       
  3837     fixed size based on its contents, you can call
       
  3838     QLayout::setSizeConstraint(QLayout::SetFixedSize);
       
  3839 
       
  3840     \sa maximumSize, minimumSize
       
  3841 */
       
  3842 
       
  3843 void QWidget::setFixedSize(const QSize & s)
       
  3844 {
       
  3845     setFixedSize(s.width(), s.height());
       
  3846 }
       
  3847 
       
  3848 
       
  3849 /*!
       
  3850     \fn void QWidget::setFixedSize(int w, int h)
       
  3851     \overload
       
  3852 
       
  3853     Sets the width of the widget to \a w and the height to \a h.
       
  3854 */
       
  3855 
       
  3856 void QWidget::setFixedSize(int w, int h)
       
  3857 {
       
  3858     Q_D(QWidget);
       
  3859 #ifdef Q_WS_QWS
       
  3860     // temporary fix for 4.3.x.
       
  3861     // Should move the embedded spesific contraints in setMinimumSize_helper into QLayout
       
  3862     int tmpW = w;
       
  3863     int tmpH = h;
       
  3864     bool minSizeSet = d->setMinimumSize_helper(tmpW, tmpH);
       
  3865 #else
       
  3866     bool minSizeSet = d->setMinimumSize_helper(w, h);
       
  3867 #endif
       
  3868     bool maxSizeSet = d->setMaximumSize_helper(w, h);
       
  3869     if (!minSizeSet && !maxSizeSet)
       
  3870         return;
       
  3871 
       
  3872     if (isWindow())
       
  3873         d->setConstraints_sys();
       
  3874     else
       
  3875         d->updateGeometry_helper(true);
       
  3876 
       
  3877     if (w != QWIDGETSIZE_MAX || h != QWIDGETSIZE_MAX)
       
  3878         resize(w, h);
       
  3879 }
       
  3880 
       
  3881 void QWidget::setMinimumWidth(int w)
       
  3882 {
       
  3883     Q_D(QWidget);
       
  3884     d->createExtra();
       
  3885     uint expl = d->extra->explicitMinSize | (w ? Qt::Horizontal : 0);
       
  3886     setMinimumSize(w, minimumSize().height());
       
  3887     d->extra->explicitMinSize = expl;
       
  3888 }
       
  3889 
       
  3890 void QWidget::setMinimumHeight(int h)
       
  3891 {
       
  3892     Q_D(QWidget);
       
  3893     d->createExtra();
       
  3894     uint expl = d->extra->explicitMinSize | (h ? Qt::Vertical : 0);
       
  3895     setMinimumSize(minimumSize().width(), h);
       
  3896     d->extra->explicitMinSize = expl;
       
  3897 }
       
  3898 
       
  3899 void QWidget::setMaximumWidth(int w)
       
  3900 {
       
  3901     Q_D(QWidget);
       
  3902     d->createExtra();
       
  3903     uint expl = d->extra->explicitMaxSize | (w == QWIDGETSIZE_MAX ? 0 : Qt::Horizontal);
       
  3904     setMaximumSize(w, maximumSize().height());
       
  3905     d->extra->explicitMaxSize = expl;
       
  3906 }
       
  3907 
       
  3908 void QWidget::setMaximumHeight(int h)
       
  3909 {
       
  3910     Q_D(QWidget);
       
  3911     d->createExtra();
       
  3912     uint expl = d->extra->explicitMaxSize | (h == QWIDGETSIZE_MAX ? 0 : Qt::Vertical);
       
  3913     setMaximumSize(maximumSize().width(), h);
       
  3914     d->extra->explicitMaxSize = expl;
       
  3915 }
       
  3916 
       
  3917 /*!
       
  3918     Sets both the minimum and maximum width of the widget to \a w
       
  3919     without changing the heights. Provided for convenience.
       
  3920 
       
  3921     \sa sizeHint() minimumSize() maximumSize() setFixedSize()
       
  3922 */
       
  3923 
       
  3924 void QWidget::setFixedWidth(int w)
       
  3925 {
       
  3926     Q_D(QWidget);
       
  3927     d->createExtra();
       
  3928     uint explMin = d->extra->explicitMinSize | Qt::Horizontal;
       
  3929     uint explMax = d->extra->explicitMaxSize | Qt::Horizontal;
       
  3930     setMinimumSize(w, minimumSize().height());
       
  3931     setMaximumSize(w, maximumSize().height());
       
  3932     d->extra->explicitMinSize = explMin;
       
  3933     d->extra->explicitMaxSize = explMax;
       
  3934 }
       
  3935 
       
  3936 
       
  3937 /*!
       
  3938     Sets both the minimum and maximum heights of the widget to \a h
       
  3939     without changing the widths. Provided for convenience.
       
  3940 
       
  3941     \sa sizeHint() minimumSize() maximumSize() setFixedSize()
       
  3942 */
       
  3943 
       
  3944 void QWidget::setFixedHeight(int h)
       
  3945 {
       
  3946     Q_D(QWidget);
       
  3947     d->createExtra();
       
  3948     uint explMin = d->extra->explicitMinSize | Qt::Vertical;
       
  3949     uint explMax = d->extra->explicitMaxSize | Qt::Vertical;
       
  3950     setMinimumSize(minimumSize().width(), h);
       
  3951     setMaximumSize(maximumSize().width(), h);
       
  3952     d->extra->explicitMinSize = explMin;
       
  3953     d->extra->explicitMaxSize = explMax;
       
  3954 }
       
  3955 
       
  3956 
       
  3957 /*!
       
  3958     Translates the widget coordinate \a pos to the coordinate system
       
  3959     of \a parent. The \a parent must not be 0 and must be a parent
       
  3960     of the calling widget.
       
  3961 
       
  3962     \sa mapFrom() mapToParent() mapToGlobal() underMouse()
       
  3963 */
       
  3964 
       
  3965 QPoint QWidget::mapTo(QWidget * parent, const QPoint & pos) const
       
  3966 {
       
  3967     QPoint p = pos;
       
  3968     if (parent) {
       
  3969         const QWidget * w = this;
       
  3970         while (w != parent) {
       
  3971             Q_ASSERT_X(w, "QWidget::mapTo(QWidget *parent, const QPoint &pos)",
       
  3972                        "parent must be in parent hierarchy");
       
  3973             p = w->mapToParent(p);
       
  3974             w = w->parentWidget();
       
  3975         }
       
  3976     }
       
  3977     return p;
       
  3978 }
       
  3979 
       
  3980 
       
  3981 /*!
       
  3982     Translates the widget coordinate \a pos from the coordinate system
       
  3983     of \a parent to this widget's coordinate system. The \a parent
       
  3984     must not be 0 and must be a parent of the calling widget.
       
  3985 
       
  3986     \sa mapTo() mapFromParent() mapFromGlobal() underMouse()
       
  3987 */
       
  3988 
       
  3989 QPoint QWidget::mapFrom(QWidget * parent, const QPoint & pos) const
       
  3990 {
       
  3991     QPoint p(pos);
       
  3992     if (parent) {
       
  3993         const QWidget * w = this;
       
  3994         while (w != parent) {
       
  3995             Q_ASSERT_X(w, "QWidget::mapFrom(QWidget *parent, const QPoint &pos)",
       
  3996                        "parent must be in parent hierarchy");
       
  3997 
       
  3998             p = w->mapFromParent(p);
       
  3999             w = w->parentWidget();
       
  4000         }
       
  4001     }
       
  4002     return p;
       
  4003 }
       
  4004 
       
  4005 
       
  4006 /*!
       
  4007     Translates the widget coordinate \a pos to a coordinate in the
       
  4008     parent widget.
       
  4009 
       
  4010     Same as mapToGlobal() if the widget has no parent.
       
  4011 
       
  4012     \sa mapFromParent() mapTo() mapToGlobal() underMouse()
       
  4013 */
       
  4014 
       
  4015 QPoint QWidget::mapToParent(const QPoint &pos) const
       
  4016 {
       
  4017     return pos + data->crect.topLeft();
       
  4018 }
       
  4019 
       
  4020 /*!
       
  4021     Translates the parent widget coordinate \a pos to widget
       
  4022     coordinates.
       
  4023 
       
  4024     Same as mapFromGlobal() if the widget has no parent.
       
  4025 
       
  4026     \sa mapToParent() mapFrom() mapFromGlobal() underMouse()
       
  4027 */
       
  4028 
       
  4029 QPoint QWidget::mapFromParent(const QPoint &pos) const
       
  4030 {
       
  4031     return pos - data->crect.topLeft();
       
  4032 }
       
  4033 
       
  4034 
       
  4035 /*!
       
  4036     Returns the window for this widget, i.e. the next ancestor widget
       
  4037     that has (or could have) a window-system frame.
       
  4038 
       
  4039     If the widget is a window, the widget itself is returned.
       
  4040 
       
  4041     Typical usage is changing the window title:
       
  4042 
       
  4043     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 3
       
  4044 
       
  4045     \sa isWindow()
       
  4046 */
       
  4047 
       
  4048 QWidget *QWidget::window() const
       
  4049 {
       
  4050     QWidget *w = (QWidget *)this;
       
  4051     QWidget *p = w->parentWidget();
       
  4052     while (!w->isWindow() && p) {
       
  4053         w = p;
       
  4054         p = p->parentWidget();
       
  4055     }
       
  4056     return w;
       
  4057 }
       
  4058 
       
  4059 /*!
       
  4060     \since 4.4
       
  4061 
       
  4062     Returns the native parent for this widget, i.e. the next ancestor widget
       
  4063     that has a system identifier, or 0 if it does not have any native parent.
       
  4064 
       
  4065     \sa effectiveWinId()
       
  4066 */
       
  4067 QWidget *QWidget::nativeParentWidget() const
       
  4068 {
       
  4069     QWidget *parent = parentWidget();
       
  4070     while (parent && !parent->internalWinId())
       
  4071         parent = parent->parentWidget();
       
  4072     return parent;
       
  4073 }
       
  4074 
       
  4075 /*! \fn QWidget *QWidget::topLevelWidget() const
       
  4076     \obsolete
       
  4077 
       
  4078     Use window() instead.
       
  4079 */
       
  4080 
       
  4081 #ifdef QT3_SUPPORT
       
  4082 /*!
       
  4083     Returns the color role used for painting the widget's background.
       
  4084 
       
  4085     Use QPalette(backgroundRole(()) instead.
       
  4086 */
       
  4087 Qt::BackgroundMode QWidget::backgroundMode() const
       
  4088 {
       
  4089     if (testAttribute(Qt::WA_NoSystemBackground))
       
  4090         return Qt::NoBackground;
       
  4091     switch(backgroundRole()) {
       
  4092     case QPalette::WindowText:
       
  4093         return Qt::PaletteForeground;
       
  4094     case QPalette::Button:
       
  4095         return Qt::PaletteButton;
       
  4096     case QPalette::Light:
       
  4097         return Qt::PaletteLight;
       
  4098     case QPalette::Midlight:
       
  4099         return Qt::PaletteMidlight;
       
  4100     case QPalette::Dark:
       
  4101         return Qt::PaletteDark;
       
  4102     case QPalette::Mid:
       
  4103         return Qt::PaletteMid;
       
  4104     case QPalette::Text:
       
  4105         return Qt::PaletteText;
       
  4106     case QPalette::BrightText:
       
  4107         return Qt::PaletteBrightText;
       
  4108     case QPalette::Base:
       
  4109         return Qt::PaletteBase;
       
  4110     case QPalette::Window:
       
  4111         return Qt::PaletteBackground;
       
  4112     case QPalette::Shadow:
       
  4113         return Qt::PaletteShadow;
       
  4114     case QPalette::Highlight:
       
  4115         return Qt::PaletteHighlight;
       
  4116     case QPalette::HighlightedText:
       
  4117         return Qt::PaletteHighlightedText;
       
  4118     case QPalette::ButtonText:
       
  4119         return Qt::PaletteButtonText;
       
  4120     case QPalette::Link:
       
  4121         return Qt::PaletteLink;
       
  4122     case QPalette::LinkVisited:
       
  4123         return Qt::PaletteLinkVisited;
       
  4124     default:
       
  4125         break;
       
  4126     }
       
  4127     return Qt::NoBackground;
       
  4128 }
       
  4129 
       
  4130 /*!
       
  4131     \fn void QWidget::setBackgroundMode(Qt::BackgroundMode
       
  4132     widgetBackground, Qt::BackgroundMode paletteBackground)
       
  4133 
       
  4134     Sets the color role used for painting the widget's background to
       
  4135     background mode \a widgetBackground. The \a paletteBackground mode
       
  4136     parameter is ignored.
       
  4137 */
       
  4138 void QWidget::setBackgroundMode(Qt::BackgroundMode m, Qt::BackgroundMode)
       
  4139 {
       
  4140     Q_D(QWidget);
       
  4141     if(m == Qt::NoBackground) {
       
  4142         setAttribute(Qt::WA_NoSystemBackground, true);
       
  4143         return;
       
  4144     }
       
  4145     setAttribute(Qt::WA_NoSystemBackground, false);
       
  4146     d->fg_role = QPalette::NoRole;
       
  4147     QPalette::ColorRole role = d->bg_role;
       
  4148     switch(m) {
       
  4149     case Qt::FixedColor:
       
  4150     case Qt::FixedPixmap:
       
  4151         break;
       
  4152     case Qt::PaletteForeground:
       
  4153         role = QPalette::WindowText;
       
  4154         break;
       
  4155     case Qt::PaletteButton:
       
  4156         role = QPalette::Button;
       
  4157         break;
       
  4158     case Qt::PaletteLight:
       
  4159         role = QPalette::Light;
       
  4160         break;
       
  4161     case Qt::PaletteMidlight:
       
  4162         role = QPalette::Midlight;
       
  4163         break;
       
  4164     case Qt::PaletteDark:
       
  4165         role = QPalette::Dark;
       
  4166         break;
       
  4167     case Qt::PaletteMid:
       
  4168         role = QPalette::Mid;
       
  4169         break;
       
  4170     case Qt::PaletteText:
       
  4171         role = QPalette::Text;
       
  4172         break;
       
  4173     case Qt::PaletteBrightText:
       
  4174         role = QPalette::BrightText;
       
  4175         break;
       
  4176     case Qt::PaletteBase:
       
  4177         role = QPalette::Base;
       
  4178         break;
       
  4179     case Qt::PaletteBackground:
       
  4180         role = QPalette::Window;
       
  4181         break;
       
  4182     case Qt::PaletteShadow:
       
  4183         role = QPalette::Shadow;
       
  4184         break;
       
  4185     case Qt::PaletteHighlight:
       
  4186         role = QPalette::Highlight;
       
  4187         break;
       
  4188     case Qt::PaletteHighlightedText:
       
  4189         role = QPalette::HighlightedText;
       
  4190         break;
       
  4191     case Qt::PaletteButtonText:
       
  4192         role = QPalette::ButtonText;
       
  4193         break;
       
  4194     case Qt::PaletteLink:
       
  4195         role = QPalette::Link;
       
  4196         break;
       
  4197     case Qt::PaletteLinkVisited:
       
  4198         role = QPalette::LinkVisited;
       
  4199         break;
       
  4200     case Qt::X11ParentRelative:
       
  4201         d->fg_role = role = QPalette::NoRole;
       
  4202     default:
       
  4203         break;
       
  4204     }
       
  4205     setBackgroundRole(role);
       
  4206 }
       
  4207 
       
  4208 /*!
       
  4209     The widget mapper is no longer part of the public API.
       
  4210 */
       
  4211 QT3_SUPPORT QWidgetMapper *QWidget::wmapper() { return QWidgetPrivate::mapper; }
       
  4212 
       
  4213 #endif
       
  4214 
       
  4215 
       
  4216 /*!
       
  4217   Returns the background role of the widget.
       
  4218 
       
  4219   The background role defines the brush from the widget's \l palette that
       
  4220   is used to render the background.
       
  4221 
       
  4222   If no explicit background role is set, the widget inherts its parent
       
  4223   widget's background role.
       
  4224 
       
  4225   \sa setBackgroundRole(), foregroundRole()
       
  4226  */
       
  4227 QPalette::ColorRole QWidget::backgroundRole() const
       
  4228 {
       
  4229 
       
  4230     const QWidget *w = this;
       
  4231     do {
       
  4232         QPalette::ColorRole role = w->d_func()->bg_role;
       
  4233         if (role != QPalette::NoRole)
       
  4234             return role;
       
  4235         if (w->isWindow() || w->windowType() == Qt::SubWindow)
       
  4236             break;
       
  4237         w = w->parentWidget();
       
  4238     } while (w);
       
  4239     return QPalette::Window;
       
  4240 }
       
  4241 
       
  4242 /*!
       
  4243   Sets the background role of the widget to \a role.
       
  4244 
       
  4245   The background role defines the brush from the widget's \l palette that
       
  4246   is used to render the background.
       
  4247 
       
  4248   If \a role is QPalette::NoRole, then the widget inherits its
       
  4249   parent's background role.
       
  4250 
       
  4251   Note that styles are free to choose any color from the palette.
       
  4252   You can modify the palette or set a style sheet if you don't
       
  4253   achieve the result you want with setBackgroundRole().
       
  4254 
       
  4255   \sa backgroundRole(), foregroundRole()
       
  4256  */
       
  4257 
       
  4258 void QWidget::setBackgroundRole(QPalette::ColorRole role)
       
  4259 {
       
  4260     Q_D(QWidget);
       
  4261     d->bg_role = role;
       
  4262     d->updateSystemBackground();
       
  4263     d->propagatePaletteChange();
       
  4264     d->updateIsOpaque();
       
  4265 }
       
  4266 
       
  4267 /*!
       
  4268   Returns the foreground role.
       
  4269 
       
  4270   The foreground role defines the color from the widget's \l palette that
       
  4271   is used to draw the foreground.
       
  4272 
       
  4273   If no explicit foreground role is set, the function returns a role
       
  4274   that contrasts with the background role.
       
  4275 
       
  4276   \sa setForegroundRole(), backgroundRole()
       
  4277  */
       
  4278 QPalette::ColorRole QWidget::foregroundRole() const
       
  4279 {
       
  4280     Q_D(const QWidget);
       
  4281     QPalette::ColorRole rl = QPalette::ColorRole(d->fg_role);
       
  4282     if (rl != QPalette::NoRole)
       
  4283         return rl;
       
  4284     QPalette::ColorRole role = QPalette::WindowText;
       
  4285     switch (backgroundRole()) {
       
  4286     case QPalette::Button:
       
  4287         role = QPalette::ButtonText;
       
  4288         break;
       
  4289     case QPalette::Base:
       
  4290         role = QPalette::Text;
       
  4291         break;
       
  4292     case QPalette::Dark:
       
  4293     case QPalette::Shadow:
       
  4294         role = QPalette::Light;
       
  4295         break;
       
  4296     case QPalette::Highlight:
       
  4297         role = QPalette::HighlightedText;
       
  4298         break;
       
  4299     case QPalette::ToolTipBase:
       
  4300         role = QPalette::ToolTipText;
       
  4301         break;
       
  4302     default:
       
  4303         ;
       
  4304     }
       
  4305     return role;
       
  4306 }
       
  4307 
       
  4308 /*!
       
  4309   Sets the foreground role of the widget to \a role.
       
  4310 
       
  4311   The foreground role defines the color from the widget's \l palette that
       
  4312   is used to draw the foreground.
       
  4313 
       
  4314   If \a role is QPalette::NoRole, the widget uses a foreground role
       
  4315   that contrasts with the background role.
       
  4316 
       
  4317   Note that styles are free to choose any color from the palette.
       
  4318   You can modify the palette or set a style sheet if you don't
       
  4319   achieve the result you want with setForegroundRole().
       
  4320 
       
  4321   \sa foregroundRole(), backgroundRole()
       
  4322  */
       
  4323 void QWidget::setForegroundRole(QPalette::ColorRole role)
       
  4324 {
       
  4325     Q_D(QWidget);
       
  4326     d->fg_role = role;
       
  4327     d->updateSystemBackground();
       
  4328     d->propagatePaletteChange();
       
  4329 }
       
  4330 
       
  4331 /*!
       
  4332     \property QWidget::palette
       
  4333     \brief the widget's palette
       
  4334 
       
  4335     This property describes the widget's palette. The palette is used by the
       
  4336     widget's style when rendering standard components, and is available as a
       
  4337     means to ensure that custom widgets can maintain consistency with the
       
  4338     native platform's look and feel. It's common that different platforms, or
       
  4339     different styles, have different palettes.
       
  4340 
       
  4341     When you assign a new palette to a widget, the color roles from this
       
  4342     palette are combined with the widget's default palette to form the
       
  4343     widget's final palette. The palette entry for the widget's background role
       
  4344     is used to fill the widget's background (see QWidget::autoFillBackground),
       
  4345     and the foreground role initializes QPainter's pen.
       
  4346 
       
  4347     The default depends on the system environment. QApplication maintains a
       
  4348     system/theme palette which serves as a default for all widgets.  There may
       
  4349     also be special palette defaults for certain types of widgets (e.g., on
       
  4350     Windows XP and Vista, all classes that derive from QMenuBar have a special
       
  4351     default palette). You can also define default palettes for widgets
       
  4352     yourself by passing a custom palette and the name of a widget to
       
  4353     QApplication::setPalette(). Finally, the style always has the option of
       
  4354     polishing the palette as it's assigned (see QStyle::polish()).
       
  4355 
       
  4356     QWidget propagates explicit palette roles from parent to child. If you
       
  4357     assign a brush or color to a specific role on a palette and assign that
       
  4358     palette to a widget, that role will propagate to all the widget's
       
  4359     children, overriding any system defaults for that role. Note that palettes
       
  4360     by default don't propagate to windows (see isWindow()) unless the
       
  4361     Qt::WA_WindowPropagation attribute is enabled.
       
  4362 
       
  4363     QWidget's palette propagation is similar to its font propagation.
       
  4364 
       
  4365     The current style, which is used to render the content of all standard Qt
       
  4366     widgets, is free to choose colors and brushes from the widget palette, or
       
  4367     in some cases, to ignore the palette (partially, or completely). In
       
  4368     particular, certain styles like GTK style, Mac style, Windows XP, and
       
  4369     Vista style, depend on third party APIs to render the content of widgets,
       
  4370     and these styles typically do not follow the palette. Because of this,
       
  4371     assigning roles to a widget's palette is not guaranteed to change the
       
  4372     appearance of the widget. Instead, you may choose to apply a \l
       
  4373     styleSheet. You can refer to our Knowledge Base article
       
  4374     \l{http://qt.nokia.com/developer/knowledgebase/22}{here} for more
       
  4375     information.
       
  4376 
       
  4377     \warning Do not use this function in conjunction with \l{Qt Style Sheets}.
       
  4378     When using style sheets, the palette of a widget can be customized using
       
  4379     the "color", "background-color", "selection-color",
       
  4380     "selection-background-color" and "alternate-background-color".
       
  4381 
       
  4382     \sa QApplication::palette(), QWidget::font()
       
  4383 */
       
  4384 const QPalette &QWidget::palette() const
       
  4385 {
       
  4386     if (!isEnabled()) {
       
  4387         data->pal.setCurrentColorGroup(QPalette::Disabled);
       
  4388     } else if ((!isVisible() || isActiveWindow())
       
  4389 #if defined(Q_OS_WIN) && !defined(Q_WS_WINCE)
       
  4390         && !QApplicationPrivate::isBlockedByModal(const_cast<QWidget *>(this))
       
  4391 #endif
       
  4392         ) {
       
  4393         data->pal.setCurrentColorGroup(QPalette::Active);
       
  4394     } else {
       
  4395 #ifdef Q_WS_MAC
       
  4396         extern bool qt_mac_can_clickThrough(const QWidget *); //qwidget_mac.cpp
       
  4397         if (qt_mac_can_clickThrough(this))
       
  4398             data->pal.setCurrentColorGroup(QPalette::Active);
       
  4399         else
       
  4400 #endif
       
  4401             data->pal.setCurrentColorGroup(QPalette::Inactive);
       
  4402     }
       
  4403     return data->pal;
       
  4404 }
       
  4405 
       
  4406 void QWidget::setPalette(const QPalette &palette)
       
  4407 {
       
  4408     Q_D(QWidget);
       
  4409     setAttribute(Qt::WA_SetPalette, palette.resolve() != 0);
       
  4410 
       
  4411     // Determine which palette is inherited from this widget's ancestors and
       
  4412     // QApplication::palette, resolve this against \a palette (attributes from
       
  4413     // the inherited palette are copied over this widget's palette). Then
       
  4414     // propagate this palette to this widget's children.
       
  4415     QPalette naturalPalette = d->naturalWidgetPalette(d->inheritedPaletteResolveMask);
       
  4416     QPalette resolvedPalette = palette.resolve(naturalPalette);
       
  4417     d->setPalette_helper(resolvedPalette);
       
  4418 }
       
  4419 
       
  4420 /*!
       
  4421     \internal
       
  4422 
       
  4423     Returns the palette that the widget \a w inherits from its ancestors and
       
  4424     QApplication::palette. \a inheritedMask is the combination of the widget's
       
  4425     ancestors palette request masks (i.e., which attributes from the parent
       
  4426     widget's palette are implicitly imposed on this widget by the user). Note
       
  4427     that this font does not take into account the palette set on \a w itself.
       
  4428 */
       
  4429 QPalette QWidgetPrivate::naturalWidgetPalette(uint inheritedMask) const
       
  4430 {
       
  4431     Q_Q(const QWidget);
       
  4432     QPalette naturalPalette = QApplication::palette(q);
       
  4433     if (!q->testAttribute(Qt::WA_StyleSheet)
       
  4434         && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
       
  4435 #ifndef QT_NO_GRAPHICSVIEW
       
  4436             || (extra && extra->proxyWidget)
       
  4437 #endif //QT_NO_GRAPHICSVIEW
       
  4438             )) {
       
  4439         if (QWidget *p = q->parentWidget()) {
       
  4440             if (!p->testAttribute(Qt::WA_StyleSheet)) {
       
  4441                 if (!naturalPalette.isCopyOf(QApplication::palette())) {
       
  4442                     QPalette inheritedPalette = p->palette();
       
  4443                     inheritedPalette.resolve(inheritedMask);
       
  4444                     naturalPalette = inheritedPalette.resolve(naturalPalette);
       
  4445                 } else {
       
  4446                     naturalPalette = p->palette();
       
  4447                 }
       
  4448             }
       
  4449         }
       
  4450 #ifndef QT_NO_GRAPHICSVIEW
       
  4451         else if (extra && extra->proxyWidget) {
       
  4452             QPalette inheritedPalette = extra->proxyWidget->palette();
       
  4453             inheritedPalette.resolve(inheritedMask);
       
  4454             naturalPalette = inheritedPalette.resolve(naturalPalette);
       
  4455         }
       
  4456 #endif //QT_NO_GRAPHICSVIEW
       
  4457     }
       
  4458     naturalPalette.resolve(0);
       
  4459     return naturalPalette;
       
  4460 }
       
  4461 /*!
       
  4462     \internal
       
  4463 
       
  4464     Determine which palette is inherited from this widget's ancestors and
       
  4465     QApplication::palette, resolve this against this widget's palette
       
  4466     (attributes from the inherited palette are copied over this widget's
       
  4467     palette). Then propagate this palette to this widget's children.
       
  4468 */
       
  4469 void QWidgetPrivate::resolvePalette()
       
  4470 {
       
  4471     QPalette naturalPalette = naturalWidgetPalette(inheritedPaletteResolveMask);
       
  4472     QPalette resolvedPalette = data.pal.resolve(naturalPalette);
       
  4473     setPalette_helper(resolvedPalette);
       
  4474 }
       
  4475 
       
  4476 void QWidgetPrivate::setPalette_helper(const QPalette &palette)
       
  4477 {
       
  4478     Q_Q(QWidget);
       
  4479     if (data.pal == palette && data.pal.resolve() == palette.resolve())
       
  4480         return;
       
  4481     data.pal = palette;
       
  4482     updateSystemBackground();
       
  4483     propagatePaletteChange();
       
  4484     updateIsOpaque();
       
  4485     q->update();
       
  4486     updateIsOpaque();
       
  4487 }
       
  4488 
       
  4489 /*!
       
  4490     \property QWidget::font
       
  4491     \brief the font currently set for the widget
       
  4492 
       
  4493     This property describes the widget's requested font. The font is used by
       
  4494     the widget's style when rendering standard components, and is available as
       
  4495     a means to ensure that custom widgets can maintain consistency with the
       
  4496     native platform's look and feel. It's common that different platforms, or
       
  4497     different styles, define different fonts for an application.
       
  4498 
       
  4499     When you assign a new font to a widget, the properties from this font are
       
  4500     combined with the widget's default font to form the widget's final
       
  4501     font. You can call fontInfo() to get a copy of the widget's final
       
  4502     font. The final font is also used to initialize QPainter's font.
       
  4503 
       
  4504     The default depends on the system environment. QApplication maintains a
       
  4505     system/theme font which serves as a default for all widgets.  There may
       
  4506     also be special font defaults for certain types of widgets. You can also
       
  4507     define default fonts for widgets yourself by passing a custom font and the
       
  4508     name of a widget to QApplication::setFont(). Finally, the font is matched
       
  4509     against Qt's font database to find the best match.
       
  4510 
       
  4511     QWidget propagates explicit font properties from parent to child. If you
       
  4512     change a specific property on a font and assign that font to a widget,
       
  4513     that property will propagate to all the widget's children, overriding any
       
  4514     system defaults for that property. Note that fonts by default don't
       
  4515     propagate to windows (see isWindow()) unless the Qt::WA_WindowPropagation
       
  4516     attribute is enabled.
       
  4517 
       
  4518     QWidget's font propagation is similar to its palette propagation.
       
  4519 
       
  4520     The current style, which is used to render the content of all standard Qt
       
  4521     widgets, is free to choose to use the widget font, or in some cases, to
       
  4522     ignore it (partially, or completely). In particular, certain styles like
       
  4523     GTK style, Mac style, Windows XP, and Vista style, apply special
       
  4524     modifications to the widget font to match the platform's native look and
       
  4525     feel. Because of this, assigning properties to a widget's font is not
       
  4526     guaranteed to change the appearance of the widget. Instead, you may choose
       
  4527     to apply a \l styleSheet.
       
  4528 
       
  4529     \note If \l{Qt Style Sheets} are used on the same widget as setFont(),
       
  4530     style sheets will take precedence if the settings conflict.
       
  4531 
       
  4532     \sa fontInfo(), fontMetrics()
       
  4533 */
       
  4534 
       
  4535 void QWidget::setFont(const QFont &font)
       
  4536 {
       
  4537     Q_D(QWidget);
       
  4538 
       
  4539 #ifndef QT_NO_STYLE_STYLESHEET
       
  4540     const QStyleSheetStyle* style;
       
  4541     if (d->extra && (style = qobject_cast<const QStyleSheetStyle*>(d->extra->style))) {
       
  4542         style->saveWidgetFont(this, font);
       
  4543     }
       
  4544 #endif
       
  4545 
       
  4546     setAttribute(Qt::WA_SetFont, font.resolve() != 0);
       
  4547 
       
  4548     // Determine which font is inherited from this widget's ancestors and
       
  4549     // QApplication::font, resolve this against \a font (attributes from the
       
  4550     // inherited font are copied over). Then propagate this font to this
       
  4551     // widget's children.
       
  4552     QFont naturalFont = d->naturalWidgetFont(d->inheritedFontResolveMask);
       
  4553     QFont resolvedFont = font.resolve(naturalFont);
       
  4554     d->setFont_helper(resolvedFont);
       
  4555 }
       
  4556 
       
  4557 /*
       
  4558     \internal
       
  4559 
       
  4560     Returns the font that the widget \a w inherits from its ancestors and
       
  4561     QApplication::font. \a inheritedMask is the combination of the widget's
       
  4562     ancestors font request masks (i.e., which attributes from the parent
       
  4563     widget's font are implicitly imposed on this widget by the user). Note
       
  4564     that this font does not take into account the font set on \a w itself.
       
  4565 
       
  4566     ### Stylesheet has a different font propagation mechanism. When a stylesheet
       
  4567         is applied, fonts are not propagated anymore
       
  4568 */
       
  4569 QFont QWidgetPrivate::naturalWidgetFont(uint inheritedMask) const
       
  4570 {
       
  4571     Q_Q(const QWidget);
       
  4572     QFont naturalFont = QApplication::font(q);
       
  4573     if (!q->testAttribute(Qt::WA_StyleSheet)
       
  4574         && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
       
  4575 #ifndef QT_NO_GRAPHICSVIEW
       
  4576             || (extra && extra->proxyWidget)
       
  4577 #endif //QT_NO_GRAPHICSVIEW
       
  4578             )) {
       
  4579         if (QWidget *p = q->parentWidget()) {
       
  4580             if (!p->testAttribute(Qt::WA_StyleSheet)) {
       
  4581                 if (!naturalFont.isCopyOf(QApplication::font())) {
       
  4582                     QFont inheritedFont = p->font();
       
  4583                     inheritedFont.resolve(inheritedMask);
       
  4584                     naturalFont = inheritedFont.resolve(naturalFont);
       
  4585                 } else {
       
  4586                     naturalFont = p->font();
       
  4587                 }
       
  4588             }
       
  4589         }
       
  4590 #ifndef QT_NO_GRAPHICSVIEW
       
  4591         else if (extra && extra->proxyWidget) {
       
  4592             QFont inheritedFont = extra->proxyWidget->font();
       
  4593             inheritedFont.resolve(inheritedMask);
       
  4594             naturalFont = inheritedFont.resolve(naturalFont);
       
  4595         }
       
  4596 #endif //QT_NO_GRAPHICSVIEW
       
  4597     }
       
  4598     naturalFont.resolve(0);
       
  4599     return naturalFont;
       
  4600 }
       
  4601 
       
  4602 /*!
       
  4603     \internal
       
  4604 
       
  4605     Determine which font is implicitly imposed on this widget by its ancestors
       
  4606     and QApplication::font, resolve this against its own font (attributes from
       
  4607     the implicit font are copied over). Then propagate this font to this
       
  4608     widget's children.
       
  4609 */
       
  4610 void QWidgetPrivate::resolveFont()
       
  4611 {
       
  4612     QFont naturalFont = naturalWidgetFont(inheritedFontResolveMask);
       
  4613     QFont resolvedFont = data.fnt.resolve(naturalFont);
       
  4614     setFont_helper(resolvedFont);
       
  4615 }
       
  4616 
       
  4617 /*!
       
  4618     \internal
       
  4619 
       
  4620     Assign \a font to this widget, and propagate it to all children, except
       
  4621     style sheet widgets (handled differently) and windows that don't enable
       
  4622     window propagation.  \a implicitMask is the union of all ancestor widgets'
       
  4623     font request masks, and determines which attributes from this widget's
       
  4624     font should propagate.
       
  4625 */
       
  4626 void QWidgetPrivate::updateFont(const QFont &font)
       
  4627 {
       
  4628     Q_Q(QWidget);
       
  4629 #ifndef QT_NO_STYLE_STYLESHEET
       
  4630     const QStyleSheetStyle* cssStyle;
       
  4631     cssStyle = extra ? qobject_cast<const QStyleSheetStyle*>(extra->style) : 0;
       
  4632 #endif
       
  4633 
       
  4634 #ifdef QT3_SUPPORT
       
  4635     QFont old = data.fnt;
       
  4636 #endif
       
  4637     data.fnt = QFont(font, q);
       
  4638 #if defined(Q_WS_X11)
       
  4639     // make sure the font set on this widget is associated with the correct screen
       
  4640     data.fnt.x11SetScreen(xinfo.screen());
       
  4641 #endif
       
  4642     // Combine new mask with natural mask and propagate to children.
       
  4643 #ifndef QT_NO_GRAPHICSVIEW
       
  4644     if (!q->parentWidget() && extra && extra->proxyWidget) {
       
  4645         QGraphicsProxyWidget *p = extra->proxyWidget;
       
  4646         inheritedFontResolveMask = p->d_func()->inheritedFontResolveMask | p->font().resolve();
       
  4647     } else
       
  4648 #endif //QT_NO_GRAPHICSVIEW
       
  4649     if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
       
  4650         inheritedFontResolveMask = 0;
       
  4651     }
       
  4652     uint newMask = data.fnt.resolve() | inheritedFontResolveMask;
       
  4653 
       
  4654     for (int i = 0; i < children.size(); ++i) {
       
  4655         QWidget *w = qobject_cast<QWidget*>(children.at(i));
       
  4656         if (w) {
       
  4657             if (0) {
       
  4658 #ifndef QT_NO_STYLE_STYLESHEET
       
  4659             } else if (w->testAttribute(Qt::WA_StyleSheet)) {
       
  4660                 // Style sheets follow a different font propagation scheme.
       
  4661                 if (cssStyle)
       
  4662                     cssStyle->updateStyleSheetFont(w);
       
  4663 #endif
       
  4664             } else if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
       
  4665                 // Propagate font changes.
       
  4666                 QWidgetPrivate *wd = w->d_func();
       
  4667                 wd->inheritedFontResolveMask = newMask;
       
  4668                 wd->resolveFont();
       
  4669             }
       
  4670         }
       
  4671     }
       
  4672 
       
  4673 #ifndef QT_NO_STYLE_STYLESHEET
       
  4674     if (cssStyle) {
       
  4675         cssStyle->updateStyleSheetFont(q);
       
  4676     }
       
  4677 #endif
       
  4678 
       
  4679     QEvent e(QEvent::FontChange);
       
  4680     QApplication::sendEvent(q, &e);
       
  4681 #ifdef QT3_SUPPORT
       
  4682     q->fontChange(old);
       
  4683 #endif
       
  4684 }
       
  4685 
       
  4686 void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction)
       
  4687 {
       
  4688     Q_Q(QWidget);
       
  4689 
       
  4690     if ( (direction == Qt::RightToLeft) == q->testAttribute(Qt::WA_RightToLeft))
       
  4691         return;
       
  4692     q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft));
       
  4693     if (!children.isEmpty()) {
       
  4694         for (int i = 0; i < children.size(); ++i) {
       
  4695             QWidget *w = qobject_cast<QWidget*>(children.at(i));
       
  4696             if (w && !w->isWindow() && !w->testAttribute(Qt::WA_SetLayoutDirection))
       
  4697                 w->d_func()->setLayoutDirection_helper(direction);
       
  4698         }
       
  4699     }
       
  4700     QEvent e(QEvent::LayoutDirectionChange);
       
  4701     QApplication::sendEvent(q, &e);
       
  4702 }
       
  4703 
       
  4704 void QWidgetPrivate::resolveLayoutDirection()
       
  4705 {
       
  4706     Q_Q(const QWidget);
       
  4707     if (!q->testAttribute(Qt::WA_SetLayoutDirection))
       
  4708         setLayoutDirection_helper(q->isWindow() ? QApplication::layoutDirection() : q->parentWidget()->layoutDirection());
       
  4709 }
       
  4710 
       
  4711 /*!
       
  4712     \property QWidget::layoutDirection
       
  4713 
       
  4714     \brief the layout direction for this widget
       
  4715 
       
  4716     By default, this property is set to Qt::LeftToRight.
       
  4717 
       
  4718     When the layout direction is set on a widget, it will propagate to
       
  4719     the widget's children, but not to a child that is a window and not
       
  4720     to a child for which setLayoutDirection() has been explicitly
       
  4721     called. Also, child widgets added \e after setLayoutDirection()
       
  4722     has been called for the parent do not inherit the parent's layout
       
  4723     direction.
       
  4724 
       
  4725     \sa QApplication::layoutDirection
       
  4726 */
       
  4727 void QWidget::setLayoutDirection(Qt::LayoutDirection direction)
       
  4728 {
       
  4729     Q_D(QWidget);
       
  4730 
       
  4731     setAttribute(Qt::WA_SetLayoutDirection);
       
  4732     d->setLayoutDirection_helper(direction);
       
  4733 }
       
  4734 
       
  4735 Qt::LayoutDirection QWidget::layoutDirection() const
       
  4736 {
       
  4737     return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
       
  4738 }
       
  4739 
       
  4740 void QWidget::unsetLayoutDirection()
       
  4741 {
       
  4742     Q_D(QWidget);
       
  4743     setAttribute(Qt::WA_SetLayoutDirection, false);
       
  4744     d->resolveLayoutDirection();
       
  4745 }
       
  4746 
       
  4747 /*!
       
  4748     \fn QFontMetrics QWidget::fontMetrics() const
       
  4749 
       
  4750     Returns the font metrics for the widget's current font.
       
  4751     Equivalent to QFontMetrics(widget->font()).
       
  4752 
       
  4753     \sa font(), fontInfo(), setFont()
       
  4754 */
       
  4755 
       
  4756 /*!
       
  4757     \fn QFontInfo QWidget::fontInfo() const
       
  4758 
       
  4759     Returns the font info for the widget's current font.
       
  4760     Equivalent to QFontInto(widget->font()).
       
  4761 
       
  4762     \sa font(), fontMetrics(), setFont()
       
  4763 */
       
  4764 
       
  4765 
       
  4766 /*!
       
  4767     \property QWidget::cursor
       
  4768     \brief the cursor shape for this widget
       
  4769 
       
  4770     The mouse cursor will assume this shape when it's over this
       
  4771     widget. See the \link Qt::CursorShape list of predefined cursor
       
  4772     objects\endlink for a range of useful shapes.
       
  4773 
       
  4774     An editor widget might use an I-beam cursor:
       
  4775     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 6
       
  4776 
       
  4777     If no cursor has been set, or after a call to unsetCursor(), the
       
  4778     parent's cursor is used.
       
  4779 
       
  4780     By default, this property contains a cursor with the Qt::ArrowCursor
       
  4781     shape.
       
  4782 
       
  4783     Some underlying window implementations will reset the cursor if it
       
  4784     leaves a widget even if the mouse is grabbed. If you want to have
       
  4785     a cursor set for all widgets, even when outside the window, consider
       
  4786     QApplication::setOverrideCursor().
       
  4787 
       
  4788     \sa QApplication::setOverrideCursor()
       
  4789 */
       
  4790 
       
  4791 #ifndef QT_NO_CURSOR
       
  4792 QCursor QWidget::cursor() const
       
  4793 {
       
  4794     Q_D(const QWidget);
       
  4795     if (testAttribute(Qt::WA_SetCursor))
       
  4796         return (d->extra && d->extra->curs)
       
  4797             ? *d->extra->curs
       
  4798             : QCursor(Qt::ArrowCursor);
       
  4799     if (isWindow() || !parentWidget())
       
  4800         return QCursor(Qt::ArrowCursor);
       
  4801     return parentWidget()->cursor();
       
  4802 }
       
  4803 
       
  4804 void QWidget::setCursor(const QCursor &cursor)
       
  4805 {
       
  4806     Q_D(QWidget);
       
  4807 // On Mac we must set the cursor even if it is the ArrowCursor.
       
  4808 #if !defined(Q_WS_MAC) && !defined(Q_WS_QWS)
       
  4809     if (cursor.shape() != Qt::ArrowCursor
       
  4810         || (d->extra && d->extra->curs))
       
  4811 #endif
       
  4812     {
       
  4813         d->createExtra();
       
  4814         QCursor *newCursor = new QCursor(cursor);
       
  4815         delete d->extra->curs;
       
  4816         d->extra->curs = newCursor;
       
  4817     }
       
  4818     setAttribute(Qt::WA_SetCursor);
       
  4819     d->setCursor_sys(cursor);
       
  4820 
       
  4821     QEvent event(QEvent::CursorChange);
       
  4822     QApplication::sendEvent(this, &event);
       
  4823 }
       
  4824 
       
  4825 void QWidget::unsetCursor()
       
  4826 {
       
  4827     Q_D(QWidget);
       
  4828     if (d->extra) {
       
  4829         delete d->extra->curs;
       
  4830         d->extra->curs = 0;
       
  4831     }
       
  4832     if (!isWindow())
       
  4833         setAttribute(Qt::WA_SetCursor, false);
       
  4834     d->unsetCursor_sys();
       
  4835 
       
  4836     QEvent event(QEvent::CursorChange);
       
  4837     QApplication::sendEvent(this, &event);
       
  4838 }
       
  4839 
       
  4840 #endif
       
  4841 
       
  4842 /*!
       
  4843     \enum QWidget::RenderFlag
       
  4844 
       
  4845     This enum describes how to render the widget when calling QWidget::render().
       
  4846 
       
  4847     \value DrawWindowBackground If you enable this option, the widget's background
       
  4848     is rendered into the target even if autoFillBackground is not set. By default,
       
  4849     this option is enabled.
       
  4850 
       
  4851     \value DrawChildren If you enable this option, the widget's children
       
  4852     are rendered recursively into the target. By default, this option is enabled.
       
  4853 
       
  4854     \value IgnoreMask If you enable this option, the widget's QWidget::mask()
       
  4855     is ignored when rendering into the target. By default, this option is disabled.
       
  4856 
       
  4857     \since 4.3
       
  4858 */
       
  4859 
       
  4860 /*!
       
  4861     \since 4.3
       
  4862 
       
  4863     Renders the \a sourceRegion of this widget into the \a target
       
  4864     using \a renderFlags to determine how to render. Rendering
       
  4865     starts at \a targetOffset in the \a target. For example:
       
  4866 
       
  4867     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 7
       
  4868 
       
  4869     If \a sourceRegion is a null region, this function will use QWidget::rect() as
       
  4870     the region, i.e. the entire widget.
       
  4871 
       
  4872     Ensure that you call QPainter::end() for the \a target device's
       
  4873     active painter (if any) before rendering. For example:
       
  4874 
       
  4875     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 8
       
  4876 
       
  4877     \note To obtain the contents of an OpenGL widget, use QGLWidget::grabFrameBuffer()
       
  4878     or QGLWidget::renderPixmap() instead.
       
  4879 */
       
  4880 void QWidget::render(QPaintDevice *target, const QPoint &targetOffset,
       
  4881                      const QRegion &sourceRegion, RenderFlags renderFlags)
       
  4882 {
       
  4883     Q_D(QWidget);
       
  4884     if (!target) {
       
  4885         qWarning("QWidget::render: null pointer to paint device");
       
  4886         return;
       
  4887     }
       
  4888 
       
  4889     const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
       
  4890     QRegion paintRegion = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
       
  4891                                                : sourceRegion;
       
  4892     if (paintRegion.isEmpty())
       
  4893         return;
       
  4894 
       
  4895 #ifndef Q_WS_MAC
       
  4896     QPainter *oldSharedPainter = inRenderWithPainter ? d->sharedPainter() : 0;
       
  4897 
       
  4898     // Use the target's shared painter if set (typically set when doing
       
  4899     // "other->render(widget);" in the widget's paintEvent.
       
  4900     if (target->devType() == QInternal::Widget) {
       
  4901         QWidgetPrivate *targetPrivate = static_cast<QWidget *>(target)->d_func();
       
  4902         if (targetPrivate->extra && targetPrivate->extra->inRenderWithPainter) {
       
  4903             QPainter *targetPainter = targetPrivate->sharedPainter();
       
  4904             if (targetPainter && targetPainter->isActive())
       
  4905                 d->setSharedPainter(targetPainter);
       
  4906         }
       
  4907     }
       
  4908 #endif
       
  4909 
       
  4910     // Use the target's redirected device if set and adjust offset and paint
       
  4911     // region accordingly. This is typically the case when people call render
       
  4912     // from the paintEvent.
       
  4913     QPoint offset = targetOffset;
       
  4914     offset -= paintRegion.boundingRect().topLeft();
       
  4915     QPoint redirectionOffset;
       
  4916     QPaintDevice *redirected = 0;
       
  4917 
       
  4918     if (target->devType() == QInternal::Widget)
       
  4919         redirected = static_cast<QWidget *>(target)->d_func()->redirected(&redirectionOffset);
       
  4920     if (!redirected)
       
  4921         redirected = QPainter::redirected(target, &redirectionOffset);
       
  4922 
       
  4923     if (redirected) {
       
  4924         target = redirected;
       
  4925         offset -= redirectionOffset;
       
  4926     }
       
  4927 
       
  4928     if (!inRenderWithPainter) { // Clip handled by shared painter (in qpainter.cpp).
       
  4929         if (QPaintEngine *targetEngine = target->paintEngine()) {
       
  4930             const QRegion targetSystemClip = targetEngine->systemClip();
       
  4931             if (!targetSystemClip.isEmpty())
       
  4932                 paintRegion &= targetSystemClip.translated(-offset);
       
  4933         }
       
  4934     }
       
  4935 
       
  4936     // Set backingstore flags.
       
  4937     int flags = QWidgetPrivate::DrawPaintOnScreen | QWidgetPrivate::DrawInvisible;
       
  4938     if (renderFlags & DrawWindowBackground)
       
  4939         flags |= QWidgetPrivate::DrawAsRoot;
       
  4940 
       
  4941     if (renderFlags & DrawChildren)
       
  4942         flags |= QWidgetPrivate::DrawRecursive;
       
  4943     else
       
  4944         flags |= QWidgetPrivate::DontSubtractOpaqueChildren;
       
  4945 
       
  4946 #ifdef Q_WS_QWS
       
  4947     flags |= QWidgetPrivate::DontSetCompositionMode;
       
  4948 #endif
       
  4949 
       
  4950     if (target->devType() == QInternal::Printer) {
       
  4951         QPainter p(target);
       
  4952         d->render_helper(&p, targetOffset, paintRegion, renderFlags);
       
  4953         return;
       
  4954     }
       
  4955 
       
  4956 #ifndef Q_WS_MAC
       
  4957     // Render via backingstore.
       
  4958     d->drawWidget(target, paintRegion, offset, flags, d->sharedPainter());
       
  4959 
       
  4960     // Restore shared painter.
       
  4961     if (oldSharedPainter)
       
  4962         d->setSharedPainter(oldSharedPainter);
       
  4963 #else
       
  4964     // Render via backingstore (no shared painter).
       
  4965     d->drawWidget(target, paintRegion, offset, flags, 0);
       
  4966 #endif
       
  4967 }
       
  4968 
       
  4969 /*!
       
  4970     \overload
       
  4971 
       
  4972     Renders the widget into the \a painter's QPainter::device().
       
  4973 
       
  4974     Transformations and settings applied to the \a painter will be used
       
  4975     when rendering.
       
  4976 
       
  4977     \note The \a painter must be active. On Mac OS X the widget will be
       
  4978     rendered into a QPixmap and then drawn by the \a painter.
       
  4979 
       
  4980     \sa QPainter::device()
       
  4981 */
       
  4982 void QWidget::render(QPainter *painter, const QPoint &targetOffset,
       
  4983                      const QRegion &sourceRegion, RenderFlags renderFlags)
       
  4984 {
       
  4985     if (!painter) {
       
  4986         qWarning("QWidget::render: Null pointer to painter");
       
  4987         return;
       
  4988     }
       
  4989 
       
  4990     if (!painter->isActive()) {
       
  4991         qWarning("QWidget::render: Cannot render with an inactive painter");
       
  4992         return;
       
  4993     }
       
  4994 
       
  4995     const qreal opacity = painter->opacity();
       
  4996     if (qFuzzyIsNull(opacity))
       
  4997         return; // Fully transparent.
       
  4998 
       
  4999     Q_D(QWidget);
       
  5000     const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
       
  5001     const QRegion toBePainted = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
       
  5002                                                      : sourceRegion;
       
  5003     if (toBePainted.isEmpty())
       
  5004         return;
       
  5005 
       
  5006     if (!d->extra)
       
  5007         d->createExtra();
       
  5008     d->extra->inRenderWithPainter = true;
       
  5009 
       
  5010 #ifdef Q_WS_MAC
       
  5011     d->render_helper(painter, targetOffset, toBePainted, renderFlags);
       
  5012 #else
       
  5013     QPaintEngine *engine = painter->paintEngine();
       
  5014     Q_ASSERT(engine);
       
  5015     QPaintEnginePrivate *enginePriv = engine->d_func();
       
  5016     Q_ASSERT(enginePriv);
       
  5017     QPaintDevice *target = engine->paintDevice();
       
  5018     Q_ASSERT(target);
       
  5019 
       
  5020     // Render via a pixmap when dealing with non-opaque painters or printers.
       
  5021     if (!inRenderWithPainter && (opacity < 1.0 || (target->devType() == QInternal::Printer))) {
       
  5022         d->render_helper(painter, targetOffset, toBePainted, renderFlags);
       
  5023         d->extra->inRenderWithPainter = false;
       
  5024         return;
       
  5025     }
       
  5026 
       
  5027     // Set new shared painter.
       
  5028     QPainter *oldPainter = d->sharedPainter();
       
  5029     d->setSharedPainter(painter);
       
  5030 
       
  5031     // Save current system clip, viewport and transform,
       
  5032     const QTransform oldTransform = enginePriv->systemTransform;
       
  5033     const QRegion oldSystemClip = enginePriv->systemClip;
       
  5034     const QRegion oldSystemViewport = enginePriv->systemViewport;
       
  5035 
       
  5036     // This ensures that all painting triggered by render() is clipped to the current engine clip.
       
  5037     if (painter->hasClipping()) {
       
  5038         const QRegion painterClip = painter->deviceTransform().map(painter->clipRegion());
       
  5039         enginePriv->setSystemViewport(oldSystemClip.isEmpty() ? painterClip : oldSystemClip & painterClip);
       
  5040     } else {
       
  5041         enginePriv->setSystemViewport(oldSystemClip);
       
  5042     }
       
  5043 
       
  5044     render(target, targetOffset, toBePainted, renderFlags);
       
  5045 
       
  5046     // Restore system clip, viewport and transform.
       
  5047     enginePriv->systemClip = oldSystemClip;
       
  5048     enginePriv->setSystemViewport(oldSystemViewport);
       
  5049     enginePriv->setSystemTransform(oldTransform);
       
  5050 
       
  5051     // Restore shared painter.
       
  5052     d->setSharedPainter(oldPainter);
       
  5053 #endif
       
  5054 
       
  5055     d->extra->inRenderWithPainter = false;
       
  5056 }
       
  5057 
       
  5058 /*!
       
  5059     \brief The graphicsEffect function returns a pointer to the
       
  5060     widget's graphics effect.
       
  5061 
       
  5062     If the widget has no graphics effect, 0 is returned.
       
  5063 
       
  5064     \since 4.6
       
  5065 
       
  5066     \sa setGraphicsEffect()
       
  5067 */
       
  5068 #ifndef QT_NO_GRAPHICSEFFECT
       
  5069 QGraphicsEffect *QWidget::graphicsEffect() const
       
  5070 {
       
  5071     Q_D(const QWidget);
       
  5072     return d->graphicsEffect;
       
  5073 }
       
  5074 #endif //QT_NO_GRAPHICSEFFECT
       
  5075 
       
  5076 /*!
       
  5077 
       
  5078   \brief The setGraphicsEffect function is for setting the widget's graphics effect.
       
  5079 
       
  5080     Sets \a effect as the widget's effect. If there already is an effect installed
       
  5081     on this widget, QWidget will delete the existing effect before installing
       
  5082     the new \a effect.
       
  5083 
       
  5084     If \a effect is the installed on a different widget, setGraphicsEffect() will remove
       
  5085     the effect from the widget and install it on this widget.
       
  5086 
       
  5087     QWidget takes ownership of \a effect.
       
  5088 
       
  5089     \note This function will apply the effect on itself and all its children.
       
  5090 
       
  5091     \since 4.6
       
  5092 
       
  5093     \sa graphicsEffect()
       
  5094 */
       
  5095 #ifndef QT_NO_GRAPHICSEFFECT
       
  5096 void QWidget::setGraphicsEffect(QGraphicsEffect *effect)
       
  5097 {
       
  5098     Q_D(QWidget);
       
  5099     if (d->graphicsEffect == effect)
       
  5100         return;
       
  5101 
       
  5102     if (d->graphicsEffect) {
       
  5103         d->invalidateBuffer(rect());
       
  5104         delete d->graphicsEffect;
       
  5105         d->graphicsEffect = 0;
       
  5106     }
       
  5107 
       
  5108     if (effect) {
       
  5109         // Set new effect.
       
  5110         QGraphicsEffectSourcePrivate *sourced = new QWidgetEffectSourcePrivate(this);
       
  5111         QGraphicsEffectSource *source = new QGraphicsEffectSource(*sourced);
       
  5112         d->graphicsEffect = effect;
       
  5113         effect->d_func()->setGraphicsEffectSource(source);
       
  5114         update();
       
  5115     }
       
  5116 
       
  5117     d->updateIsOpaque();
       
  5118 }
       
  5119 #endif //QT_NO_GRAPHICSEFFECT
       
  5120 
       
  5121 bool QWidgetPrivate::isAboutToShow() const
       
  5122 {
       
  5123     if (data.in_show)
       
  5124         return true;
       
  5125 
       
  5126     Q_Q(const QWidget);
       
  5127     if (q->isHidden())
       
  5128         return false;
       
  5129 
       
  5130     // The widget will be shown if any of its ancestors are about to show.
       
  5131     QWidget *parent = q->parentWidget();
       
  5132     return parent ? parent->d_func()->isAboutToShow() : false;
       
  5133 }
       
  5134 
       
  5135 QRegion QWidgetPrivate::prepareToRender(const QRegion &region, QWidget::RenderFlags renderFlags)
       
  5136 {
       
  5137     Q_Q(QWidget);
       
  5138     const bool isVisible = q->isVisible();
       
  5139 
       
  5140     // Make sure the widget is laid out correctly.
       
  5141     if (!isVisible && !isAboutToShow()) {
       
  5142         QWidget *topLevel = q->window();
       
  5143         (void)topLevel->d_func()->topData(); // Make sure we at least have top-data.
       
  5144         topLevel->ensurePolished();
       
  5145 
       
  5146         // Invalidate the layout of hidden ancestors (incl. myself) and pretend
       
  5147         // they're not explicitly hidden.
       
  5148         QWidget *widget = q;
       
  5149         QWidgetList hiddenWidgets;
       
  5150         while (widget) {
       
  5151             if (widget->isHidden()) {
       
  5152                 widget->setAttribute(Qt::WA_WState_Hidden, false);
       
  5153                 hiddenWidgets.append(widget);
       
  5154                 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
       
  5155                     widget->d_func()->updateGeometry_helper(true);
       
  5156             }
       
  5157             widget = widget->parentWidget();
       
  5158         }
       
  5159 
       
  5160         // Activate top-level layout.
       
  5161         if (topLevel->d_func()->layout)
       
  5162             topLevel->d_func()->layout->activate();
       
  5163 
       
  5164         // Adjust size if necessary.
       
  5165         QTLWExtra *topLevelExtra = topLevel->d_func()->maybeTopData();
       
  5166         if (topLevelExtra && !topLevelExtra->sizeAdjusted
       
  5167             && !topLevel->testAttribute(Qt::WA_Resized)) {
       
  5168             topLevel->adjustSize();
       
  5169             topLevel->setAttribute(Qt::WA_Resized, false);
       
  5170         }
       
  5171 
       
  5172         // Activate child layouts.
       
  5173         topLevel->d_func()->activateChildLayoutsRecursively();
       
  5174 
       
  5175         // We're not cheating with WA_WState_Hidden anymore.
       
  5176         for (int i = 0; i < hiddenWidgets.size(); ++i) {
       
  5177             QWidget *widget = hiddenWidgets.at(i);
       
  5178             widget->setAttribute(Qt::WA_WState_Hidden);
       
  5179             if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
       
  5180                 widget->parentWidget()->d_func()->layout->invalidate();
       
  5181         }
       
  5182     } else if (isVisible) {
       
  5183         q->window()->d_func()->sendPendingMoveAndResizeEvents(true, true);
       
  5184     }
       
  5185 
       
  5186     // Calculate the region to be painted.
       
  5187     QRegion toBePainted = !region.isEmpty() ? region : QRegion(q->rect());
       
  5188     if (!(renderFlags & QWidget::IgnoreMask) && extra && extra->hasMask)
       
  5189         toBePainted &= extra->mask;
       
  5190     return toBePainted;
       
  5191 }
       
  5192 
       
  5193 void QWidgetPrivate::render_helper(QPainter *painter, const QPoint &targetOffset, const QRegion &toBePainted,
       
  5194                                    QWidget::RenderFlags renderFlags)
       
  5195 {
       
  5196     Q_ASSERT(painter);
       
  5197     Q_ASSERT(!toBePainted.isEmpty());
       
  5198 
       
  5199     Q_Q(QWidget);
       
  5200 #ifndef Q_WS_MAC
       
  5201     const QTransform originalTransform = painter->worldTransform();
       
  5202     const bool useDeviceCoordinates = originalTransform.isScaling();
       
  5203     if (!useDeviceCoordinates) {
       
  5204 #endif
       
  5205         // Render via a pixmap.
       
  5206         const QRect rect = toBePainted.boundingRect();
       
  5207         const QSize size = rect.size();
       
  5208         if (size.isNull())
       
  5209             return;
       
  5210 
       
  5211         QPixmap pixmap(size);
       
  5212         if (!(renderFlags & QWidget::DrawWindowBackground) || !isOpaque)
       
  5213             pixmap.fill(Qt::transparent);
       
  5214         q->render(&pixmap, QPoint(), toBePainted, renderFlags);
       
  5215 
       
  5216         const bool restore = !(painter->renderHints() & QPainter::SmoothPixmapTransform);
       
  5217         painter->setRenderHints(QPainter::SmoothPixmapTransform, true);
       
  5218 
       
  5219         painter->drawPixmap(targetOffset, pixmap);
       
  5220 
       
  5221         if (restore)
       
  5222             painter->setRenderHints(QPainter::SmoothPixmapTransform, false);
       
  5223 
       
  5224 #ifndef Q_WS_MAC
       
  5225     } else {
       
  5226         // Render via a pixmap in device coordinates (to avoid pixmap scaling).
       
  5227         QTransform transform = originalTransform;
       
  5228         transform.translate(targetOffset.x(), targetOffset.y());
       
  5229 
       
  5230         QPaintDevice *device = painter->device();
       
  5231         Q_ASSERT(device);
       
  5232 
       
  5233         // Calculate device rect.
       
  5234         const QRectF rect(toBePainted.boundingRect());
       
  5235         QRect deviceRect = transform.mapRect(QRectF(0, 0, rect.width(), rect.height())).toAlignedRect();
       
  5236         deviceRect &= QRect(0, 0, device->width(), device->height());
       
  5237 
       
  5238         QPixmap pixmap(deviceRect.size());
       
  5239         pixmap.fill(Qt::transparent);
       
  5240 
       
  5241         // Create a pixmap device coordinate painter.
       
  5242         QPainter pixmapPainter(&pixmap);
       
  5243         pixmapPainter.setRenderHints(painter->renderHints());
       
  5244         transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
       
  5245         pixmapPainter.setTransform(transform);
       
  5246 
       
  5247         q->render(&pixmapPainter, QPoint(), toBePainted, renderFlags);
       
  5248         pixmapPainter.end();
       
  5249 
       
  5250         // And then draw the pixmap.
       
  5251         painter->setTransform(QTransform());
       
  5252         painter->drawPixmap(deviceRect.topLeft(), pixmap);
       
  5253         painter->setTransform(originalTransform);
       
  5254     }
       
  5255 #endif
       
  5256 }
       
  5257 
       
  5258 void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QPoint &offset, int flags,
       
  5259                                 QPainter *sharedPainter, QWidgetBackingStore *backingStore)
       
  5260 {
       
  5261     if (rgn.isEmpty())
       
  5262         return;
       
  5263 
       
  5264     Q_Q(QWidget);
       
  5265 #ifndef QT_NO_GRAPHICSEFFECT
       
  5266     if (graphicsEffect && graphicsEffect->isEnabled()) {
       
  5267         QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
       
  5268         QWidgetEffectSourcePrivate *sourced = static_cast<QWidgetEffectSourcePrivate *>
       
  5269                                                          (source->d_func());
       
  5270         if (!sourced->context) {
       
  5271             QWidgetPaintContext context(pdev, rgn, offset, flags, sharedPainter, backingStore);
       
  5272             sourced->context = &context;
       
  5273             if (!sharedPainter) {
       
  5274                 QPaintEngine *paintEngine = pdev->paintEngine();
       
  5275                 paintEngine->d_func()->systemClip = rgn.translated(offset);
       
  5276                 QPainter p(pdev);
       
  5277                 p.translate(offset);
       
  5278                 context.painter = &p;
       
  5279                 graphicsEffect->draw(&p);
       
  5280                 paintEngine->d_func()->systemClip = QRegion();
       
  5281             } else {
       
  5282                 context.painter = sharedPainter;
       
  5283                 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
       
  5284                     sourced->invalidateCache();
       
  5285                     sourced->lastEffectTransform = sharedPainter->worldTransform();
       
  5286                 }
       
  5287                 sharedPainter->save();
       
  5288                 sharedPainter->translate(offset);
       
  5289                 graphicsEffect->draw(sharedPainter);
       
  5290                 sharedPainter->restore();
       
  5291             }
       
  5292             sourced->context = 0;
       
  5293             return;
       
  5294         }
       
  5295     }
       
  5296 #endif //QT_NO_GRAFFICSEFFECT
       
  5297 
       
  5298     const bool asRoot = flags & DrawAsRoot;
       
  5299     const bool alsoOnScreen = flags & DrawPaintOnScreen;
       
  5300     const bool recursive = flags & DrawRecursive;
       
  5301     const bool alsoInvisible = flags & DrawInvisible;
       
  5302 
       
  5303     Q_ASSERT(sharedPainter ? sharedPainter->isActive() : true);
       
  5304 
       
  5305     QRegion toBePainted(rgn);
       
  5306     if (asRoot && !alsoInvisible)
       
  5307         toBePainted &= clipRect(); //(rgn & visibleRegion());
       
  5308     if (!(flags & DontSubtractOpaqueChildren))
       
  5309         subtractOpaqueChildren(toBePainted, q->rect());
       
  5310 
       
  5311     if (!toBePainted.isEmpty()) {
       
  5312         bool onScreen = paintOnScreen();
       
  5313         if (!onScreen || alsoOnScreen) {
       
  5314             //update the "in paint event" flag
       
  5315             if (q->testAttribute(Qt::WA_WState_InPaintEvent))
       
  5316                 qWarning("QWidget::repaint: Recursive repaint detected");
       
  5317             q->setAttribute(Qt::WA_WState_InPaintEvent);
       
  5318 
       
  5319             //clip away the new area
       
  5320 #ifndef QT_NO_PAINT_DEBUG
       
  5321             bool flushed = QWidgetBackingStore::flushPaint(q, toBePainted);
       
  5322 #endif
       
  5323             QPaintEngine *paintEngine = pdev->paintEngine();
       
  5324             if (paintEngine) {
       
  5325                 setRedirected(pdev, -offset);
       
  5326 
       
  5327                 if (sharedPainter)
       
  5328                     paintEngine->d_func()->systemClip = toBePainted;
       
  5329                 else
       
  5330                     paintEngine->d_func()->systemRect = q->data->crect;
       
  5331 
       
  5332                 //paint the background
       
  5333                 if ((asRoot || q->autoFillBackground() || onScreen || q->testAttribute(Qt::WA_StyledBackground))
       
  5334                     && !q->testAttribute(Qt::WA_OpaquePaintEvent) && !q->testAttribute(Qt::WA_NoSystemBackground)) {
       
  5335 
       
  5336                     QPainter p(q);
       
  5337                     paintBackground(&p, toBePainted, (asRoot || onScreen) ? flags | DrawAsRoot : 0);
       
  5338                 }
       
  5339 
       
  5340                 if (!sharedPainter)
       
  5341                     paintEngine->d_func()->systemClip = toBePainted.translated(offset);
       
  5342 
       
  5343                 if (!onScreen && !asRoot && !isOpaque && q->testAttribute(Qt::WA_TintedBackground)) {
       
  5344                     QPainter p(q);
       
  5345                     QColor tint = q->palette().window().color();
       
  5346                     tint.setAlphaF(qreal(.6));
       
  5347                     p.fillRect(toBePainted.boundingRect(), tint);
       
  5348                 }
       
  5349             }
       
  5350 
       
  5351 #if 0
       
  5352             qDebug() << "painting" << q << "opaque ==" << isOpaque();
       
  5353             qDebug() << "clipping to" << toBePainted << "location == " << offset
       
  5354                      << "geometry ==" << QRect(q->mapTo(q->window(), QPoint(0, 0)), q->size());
       
  5355 #endif
       
  5356 
       
  5357             //actually send the paint event
       
  5358             QPaintEvent e(toBePainted);
       
  5359             QCoreApplication::sendSpontaneousEvent(q, &e);
       
  5360 #if !defined(Q_WS_MAC) && !defined(Q_WS_QWS)
       
  5361             if (backingStore && !onScreen && !asRoot && (q->internalWinId() || !q->nativeParentWidget()->isWindow()))
       
  5362                 backingStore->markDirtyOnScreen(toBePainted, q, offset);
       
  5363 #endif
       
  5364 
       
  5365             //restore
       
  5366             if (paintEngine) {
       
  5367                 restoreRedirected();
       
  5368                 if (!sharedPainter)
       
  5369                     paintEngine->d_func()->systemRect = QRect();
       
  5370                 else
       
  5371                     paintEngine->d_func()->currentClipWidget = 0;
       
  5372                 paintEngine->d_func()->systemClip = QRegion();
       
  5373             }
       
  5374             q->setAttribute(Qt::WA_WState_InPaintEvent, false);
       
  5375             if (q->paintingActive() && !q->testAttribute(Qt::WA_PaintOutsidePaintEvent))
       
  5376                 qWarning("QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
       
  5377 
       
  5378             if (paintEngine && paintEngine->autoDestruct()) {
       
  5379                 delete paintEngine;
       
  5380             }
       
  5381 
       
  5382 #ifndef QT_NO_PAINT_DEBUG
       
  5383             if (flushed)
       
  5384                 QWidgetBackingStore::unflushPaint(q, toBePainted);
       
  5385 #endif
       
  5386         } else if (q->isWindow()) {
       
  5387             QPaintEngine *engine = pdev->paintEngine();
       
  5388             if (engine) {
       
  5389                 QPainter p(pdev);
       
  5390                 p.setClipRegion(toBePainted);
       
  5391                 const QBrush bg = q->palette().brush(QPalette::Window);
       
  5392                 if (bg.style() == Qt::TexturePattern)
       
  5393                     p.drawTiledPixmap(q->rect(), bg.texture());
       
  5394                 else
       
  5395                     p.fillRect(q->rect(), bg);
       
  5396 
       
  5397                 if (engine->autoDestruct())
       
  5398                     delete engine;
       
  5399             }
       
  5400         }
       
  5401     }
       
  5402 
       
  5403     if (recursive && !children.isEmpty()) {
       
  5404         paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot
       
  5405 #ifdef Q_BACKINGSTORE_SUBSURFACES
       
  5406                                 , q->windowSurface()
       
  5407 #endif
       
  5408                                 , sharedPainter, backingStore);
       
  5409     }
       
  5410 }
       
  5411 
       
  5412 void QWidgetPrivate::paintSiblingsRecursive(QPaintDevice *pdev, const QObjectList& siblings, int index, const QRegion &rgn,
       
  5413                                             const QPoint &offset, int flags
       
  5414 #ifdef Q_BACKINGSTORE_SUBSURFACES
       
  5415                                             , const QWindowSurface *currentSurface
       
  5416 #endif
       
  5417                                             , QPainter *sharedPainter, QWidgetBackingStore *backingStore)
       
  5418 {
       
  5419     QWidget *w = 0;
       
  5420     QRect boundingRect;
       
  5421     bool dirtyBoundingRect = true;
       
  5422     const bool exludeOpaqueChildren = (flags & DontDrawOpaqueChildren);
       
  5423 
       
  5424     do {
       
  5425         QWidget *x =  qobject_cast<QWidget*>(siblings.at(index));
       
  5426         if (x && !(exludeOpaqueChildren && x->d_func()->isOpaque) && !x->isHidden() && !x->isWindow()) {
       
  5427             if (dirtyBoundingRect) {
       
  5428                 boundingRect = rgn.boundingRect();
       
  5429                 dirtyBoundingRect = false;
       
  5430             }
       
  5431 
       
  5432             if (qRectIntersects(boundingRect, x->d_func()->effectiveRectFor(x->data->crect))) {
       
  5433 #ifdef Q_BACKINGSTORE_SUBSURFACES
       
  5434                 if (x->windowSurface() == currentSurface)
       
  5435 #endif
       
  5436                 {
       
  5437                     w = x;
       
  5438                     break;
       
  5439                 }
       
  5440             }
       
  5441         }
       
  5442         --index;
       
  5443     } while (index >= 0);
       
  5444 
       
  5445     if (!w)
       
  5446         return;
       
  5447 
       
  5448     QWidgetPrivate *wd = w->d_func();
       
  5449     const QPoint widgetPos(w->data->crect.topLeft());
       
  5450     const bool hasMask = wd->extra && wd->extra->hasMask && !wd->graphicsEffect;
       
  5451     if (index > 0) {
       
  5452         QRegion wr(rgn);
       
  5453         if (wd->isOpaque)
       
  5454             wr -= hasMask ? wd->extra->mask.translated(widgetPos) : w->data->crect;
       
  5455         paintSiblingsRecursive(pdev, siblings, --index, wr, offset, flags
       
  5456 #ifdef Q_BACKINGSTORE_SUBSURFACES
       
  5457                                , currentSurface
       
  5458 #endif
       
  5459                                , sharedPainter, backingStore);
       
  5460     }
       
  5461 
       
  5462     if (w->updatesEnabled()
       
  5463 #ifndef QT_NO_GRAPHICSVIEW
       
  5464             && (!w->d_func()->extra || !w->d_func()->extra->proxyWidget)
       
  5465 #endif //QT_NO_GRAPHICSVIEW
       
  5466        ) {
       
  5467         QRegion wRegion(rgn);
       
  5468         wRegion &= wd->effectiveRectFor(w->data->crect);
       
  5469         wRegion.translate(-widgetPos);
       
  5470         if (hasMask)
       
  5471             wRegion &= wd->extra->mask;
       
  5472         wd->drawWidget(pdev, wRegion, offset + widgetPos, flags, sharedPainter, backingStore);
       
  5473     }
       
  5474 }
       
  5475 
       
  5476 #ifndef QT_NO_GRAPHICSEFFECT
       
  5477 QRectF QWidgetEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system) const
       
  5478 {
       
  5479     if (system != Qt::DeviceCoordinates)
       
  5480         return m_widget->rect();
       
  5481 
       
  5482     if (!context) {
       
  5483         // Device coordinates without context not yet supported.
       
  5484         qWarning("QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
       
  5485         return QRectF();
       
  5486     }
       
  5487 
       
  5488     return context->painter->worldTransform().mapRect(m_widget->rect());
       
  5489 }
       
  5490 
       
  5491 void QWidgetEffectSourcePrivate::draw(QPainter *painter)
       
  5492 {
       
  5493     if (!context || context->painter != painter) {
       
  5494         m_widget->render(painter);
       
  5495         return;
       
  5496     }
       
  5497 
       
  5498     // The region saved in the context is neither clipped to the rect
       
  5499     // nor the mask, so we have to clip it here before calling drawWidget.
       
  5500     QRegion toBePainted = context->rgn;
       
  5501     toBePainted &= m_widget->rect();
       
  5502     QWidgetPrivate *wd = qt_widget_private(m_widget);
       
  5503     if (wd->extra && wd->extra->hasMask)
       
  5504         toBePainted &= wd->extra->mask;
       
  5505 
       
  5506     wd->drawWidget(context->pdev, toBePainted, context->offset, context->flags,
       
  5507                    context->sharedPainter, context->backingStore);
       
  5508 }
       
  5509 
       
  5510 QPixmap QWidgetEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset,
       
  5511                                            QGraphicsEffect::PixmapPadMode mode) const
       
  5512 {
       
  5513     const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
       
  5514     if (!context && deviceCoordinates) {
       
  5515         // Device coordinates without context not yet supported.
       
  5516         qWarning("QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
       
  5517         return QPixmap();
       
  5518     }
       
  5519 
       
  5520     QPoint pixmapOffset;
       
  5521     QRectF sourceRect = m_widget->rect();
       
  5522 
       
  5523     if (deviceCoordinates) {
       
  5524         const QTransform &painterTransform = context->painter->worldTransform();
       
  5525         sourceRect = painterTransform.mapRect(sourceRect);
       
  5526         pixmapOffset = painterTransform.map(pixmapOffset);
       
  5527     }
       
  5528 
       
  5529 
       
  5530     QRect effectRect;
       
  5531 
       
  5532     if (mode == QGraphicsEffect::PadToEffectiveBoundingRect) {
       
  5533         effectRect = m_widget->graphicsEffect()->boundingRectFor(sourceRect).toAlignedRect();
       
  5534 
       
  5535     } else if (mode == QGraphicsEffect::PadToTransparentBorder) {
       
  5536         effectRect = sourceRect.adjusted(-1, -1, 1, 1).toAlignedRect();
       
  5537 
       
  5538     } else {
       
  5539         effectRect = sourceRect.toAlignedRect();
       
  5540 
       
  5541     }
       
  5542 
       
  5543     if (offset)
       
  5544         *offset = effectRect.topLeft();
       
  5545 
       
  5546     if (deviceCoordinates) {
       
  5547         // Clip to device rect.
       
  5548         int left, top, right, bottom;
       
  5549         effectRect.getCoords(&left, &top, &right, &bottom);
       
  5550         if (left < 0) {
       
  5551             if (offset)
       
  5552                 offset->rx() += -left;
       
  5553             effectRect.setX(0);
       
  5554         }
       
  5555         if (top < 0) {
       
  5556             if (offset)
       
  5557                 offset->ry() += -top;
       
  5558             effectRect.setY(0);
       
  5559         }
       
  5560         // NB! We use +-1 for historical reasons (see QRect documentation).
       
  5561         QPaintDevice *device = context->painter->device();
       
  5562         const int deviceWidth = device->width();
       
  5563         const int deviceHeight = device->height();
       
  5564         if (right + 1 > deviceWidth)
       
  5565             effectRect.setRight(deviceWidth - 1);
       
  5566         if (bottom + 1 > deviceHeight)
       
  5567             effectRect.setBottom(deviceHeight -1);
       
  5568     }
       
  5569 
       
  5570     pixmapOffset -= effectRect.topLeft();
       
  5571 
       
  5572     QPixmap pixmap(effectRect.size());
       
  5573     pixmap.fill(Qt::transparent);
       
  5574     m_widget->render(&pixmap, pixmapOffset);
       
  5575     return pixmap;
       
  5576 }
       
  5577 #endif //QT_NO_GRAPHICSEFFECT
       
  5578 
       
  5579 #ifndef QT_NO_GRAPHICSVIEW
       
  5580 /*!
       
  5581     \internal
       
  5582 
       
  5583     Finds the nearest widget embedded in a graphics proxy widget along the chain formed by this
       
  5584     widget and its ancestors. The search starts at \a origin (inclusive).
       
  5585     If successful, the function returns the proxy that embeds the widget, or 0 if no embedded
       
  5586     widget was found.
       
  5587 */
       
  5588 QGraphicsProxyWidget * QWidgetPrivate::nearestGraphicsProxyWidget(const QWidget *origin)
       
  5589 {
       
  5590     if (origin) {
       
  5591         QWExtra *extra = origin->d_func()->extra;
       
  5592         if (extra && extra->proxyWidget)
       
  5593             return extra->proxyWidget;
       
  5594         return nearestGraphicsProxyWidget(origin->parentWidget());
       
  5595     }
       
  5596     return 0;
       
  5597 }
       
  5598 #endif
       
  5599 
       
  5600 /*!
       
  5601     \property QWidget::locale
       
  5602     \brief the widget's locale
       
  5603     \since 4.3
       
  5604 
       
  5605     As long as no special locale has been set, this is either
       
  5606     the parent's locale or (if this widget is a top level widget),
       
  5607     the default locale.
       
  5608 
       
  5609     If the widget displays dates or numbers, these should be formatted
       
  5610     using the widget's locale.
       
  5611 
       
  5612     \sa QLocale QLocale::setDefault()
       
  5613 */
       
  5614 
       
  5615 void QWidgetPrivate::setLocale_helper(const QLocale &loc, bool forceUpdate)
       
  5616 {
       
  5617     Q_Q(QWidget);
       
  5618     if (locale == loc && !forceUpdate)
       
  5619         return;
       
  5620 
       
  5621     locale = loc;
       
  5622 
       
  5623     if (!children.isEmpty()) {
       
  5624         for (int i = 0; i < children.size(); ++i) {
       
  5625             QWidget *w = qobject_cast<QWidget*>(children.at(i));
       
  5626             if (!w)
       
  5627                 continue;
       
  5628             if (w->testAttribute(Qt::WA_SetLocale))
       
  5629                 continue;
       
  5630             if (w->isWindow() && !w->testAttribute(Qt::WA_WindowPropagation))
       
  5631                 continue;
       
  5632             w->d_func()->setLocale_helper(loc, forceUpdate);
       
  5633         }
       
  5634     }
       
  5635     QEvent e(QEvent::LocaleChange);
       
  5636     QApplication::sendEvent(q, &e);
       
  5637 }
       
  5638 
       
  5639 void QWidget::setLocale(const QLocale &locale)
       
  5640 {
       
  5641     Q_D(QWidget);
       
  5642 
       
  5643     setAttribute(Qt::WA_SetLocale);
       
  5644     d->setLocale_helper(locale);
       
  5645 }
       
  5646 
       
  5647 QLocale QWidget::locale() const
       
  5648 {
       
  5649     Q_D(const QWidget);
       
  5650 
       
  5651     return d->locale;
       
  5652 }
       
  5653 
       
  5654 void QWidgetPrivate::resolveLocale()
       
  5655 {
       
  5656     Q_Q(const QWidget);
       
  5657 
       
  5658     if (!q->testAttribute(Qt::WA_SetLocale)) {
       
  5659         setLocale_helper(q->isWindow()
       
  5660                             ? QLocale()
       
  5661                             : q->parentWidget()->locale());
       
  5662     }
       
  5663 }
       
  5664 
       
  5665 void QWidget::unsetLocale()
       
  5666 {
       
  5667     Q_D(QWidget);
       
  5668     setAttribute(Qt::WA_SetLocale, false);
       
  5669     d->resolveLocale();
       
  5670 }
       
  5671 
       
  5672 static QString constructWindowTitleFromFilePath(const QString &filePath)
       
  5673 {
       
  5674     QFileInfo fi(filePath);
       
  5675     QString windowTitle = fi.fileName() + QLatin1String("[*]");
       
  5676 #ifndef Q_WS_MAC
       
  5677     QString appName = QApplication::applicationName();
       
  5678     if (!appName.isEmpty())
       
  5679         windowTitle += QLatin1Char(' ') + QChar(0x2014) + QLatin1Char(' ') + appName;
       
  5680 #endif
       
  5681     return windowTitle;
       
  5682 }
       
  5683 
       
  5684 /*!
       
  5685     \property QWidget::windowTitle
       
  5686     \brief the window title (caption)
       
  5687 
       
  5688     This property only makes sense for top-level widgets, such as
       
  5689     windows and dialogs. If no caption has been set, the title is based of the
       
  5690     \l windowFilePath. If neither of these is set, then the title is
       
  5691     an empty string.
       
  5692 
       
  5693     If you use the \l windowModified mechanism, the window title must
       
  5694     contain a "[*]" placeholder, which indicates where the '*' should
       
  5695     appear. Normally, it should appear right after the file name
       
  5696     (e.g., "document1.txt[*] - Text Editor"). If the \l
       
  5697     windowModified property is false (the default), the placeholder
       
  5698     is simply removed.
       
  5699 
       
  5700     \sa windowIcon, windowIconText, windowModified, windowFilePath
       
  5701 */
       
  5702 QString QWidget::windowTitle() const
       
  5703 {
       
  5704     Q_D(const QWidget);
       
  5705     if (d->extra && d->extra->topextra) {
       
  5706         if (!d->extra->topextra->caption.isEmpty())
       
  5707             return d->extra->topextra->caption;
       
  5708         if (!d->extra->topextra->filePath.isEmpty())
       
  5709             return constructWindowTitleFromFilePath(d->extra->topextra->filePath);
       
  5710     }
       
  5711     return QString();
       
  5712 }
       
  5713 
       
  5714 /*!
       
  5715     Returns a modified window title with the [*] place holder
       
  5716     replaced according to the rules described in QWidget::setWindowTitle
       
  5717 
       
  5718     This function assumes that "[*]" can be quoted by another
       
  5719     "[*]", so it will replace two place holders by one and
       
  5720     a single last one by either "*" or nothing depending on
       
  5721     the modified flag.
       
  5722 
       
  5723     \internal
       
  5724 */
       
  5725 QString qt_setWindowTitle_helperHelper(const QString &title, const QWidget *widget)
       
  5726 {
       
  5727     Q_ASSERT(widget);
       
  5728 
       
  5729 #ifdef QT_EVAL
       
  5730     extern QString qt_eval_adapt_window_title(const QString &title);
       
  5731     QString cap = qt_eval_adapt_window_title(title);
       
  5732 #else
       
  5733     QString cap = title;
       
  5734 #endif
       
  5735 
       
  5736     if (cap.isEmpty())
       
  5737         return cap;
       
  5738 
       
  5739     QLatin1String placeHolder("[*]");
       
  5740     int placeHolderLength = 3; // QLatin1String doesn't have length()
       
  5741 
       
  5742     int index = cap.indexOf(placeHolder);
       
  5743 
       
  5744     // here the magic begins
       
  5745     while (index != -1) {
       
  5746         index += placeHolderLength;
       
  5747         int count = 1;
       
  5748         while (cap.indexOf(placeHolder, index) == index) {
       
  5749             ++count;
       
  5750             index += placeHolderLength;
       
  5751         }
       
  5752 
       
  5753         if (count%2) { // odd number of [*] -> replace last one
       
  5754             int lastIndex = cap.lastIndexOf(placeHolder, index - 1);
       
  5755             if (widget->isWindowModified()
       
  5756              && widget->style()->styleHint(QStyle::SH_TitleBar_ModifyNotification, 0, widget))
       
  5757                 cap.replace(lastIndex, 3, QWidget::tr("*"));
       
  5758             else
       
  5759                 cap.remove(lastIndex, 3);
       
  5760         }
       
  5761 
       
  5762         index = cap.indexOf(placeHolder, index);
       
  5763     }
       
  5764 
       
  5765     cap.replace(QLatin1String("[*][*]"), placeHolder);
       
  5766 
       
  5767     return cap;
       
  5768 }
       
  5769 
       
  5770 void QWidgetPrivate::setWindowTitle_helper(const QString &title)
       
  5771 {
       
  5772     Q_Q(QWidget);
       
  5773     if (q->testAttribute(Qt::WA_WState_Created))
       
  5774         setWindowTitle_sys(qt_setWindowTitle_helperHelper(title, q));
       
  5775 }
       
  5776 
       
  5777 void QWidgetPrivate::setWindowIconText_helper(const QString &title)
       
  5778 {
       
  5779     Q_Q(QWidget);
       
  5780     if (q->testAttribute(Qt::WA_WState_Created))
       
  5781         setWindowIconText_sys(qt_setWindowTitle_helperHelper(title, q));
       
  5782 }
       
  5783 
       
  5784 void QWidget::setWindowIconText(const QString &iconText)
       
  5785 {
       
  5786     if (QWidget::windowIconText() == iconText)
       
  5787         return;
       
  5788 
       
  5789     Q_D(QWidget);
       
  5790     d->topData()->iconText = iconText;
       
  5791     d->setWindowIconText_helper(iconText);
       
  5792 
       
  5793     QEvent e(QEvent::IconTextChange);
       
  5794     QApplication::sendEvent(this, &e);
       
  5795 }
       
  5796 
       
  5797 void QWidget::setWindowTitle(const QString &title)
       
  5798 {
       
  5799     if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull())
       
  5800         return;
       
  5801 
       
  5802     Q_D(QWidget);
       
  5803     d->topData()->caption = title;
       
  5804     d->setWindowTitle_helper(title);
       
  5805 
       
  5806     QEvent e(QEvent::WindowTitleChange);
       
  5807     QApplication::sendEvent(this, &e);
       
  5808 }
       
  5809 
       
  5810 
       
  5811 /*!
       
  5812     \property QWidget::windowIcon
       
  5813     \brief the widget's icon
       
  5814 
       
  5815     This property only makes sense for windows. If no icon
       
  5816     has been set, windowIcon() returns the application icon
       
  5817     (QApplication::windowIcon()).
       
  5818 
       
  5819     \sa windowIconText, windowTitle
       
  5820 */
       
  5821 QIcon QWidget::windowIcon() const
       
  5822 {
       
  5823     const QWidget *w = this;
       
  5824     while (w) {
       
  5825         const QWidgetPrivate *d = w->d_func();
       
  5826         if (d->extra && d->extra->topextra && d->extra->topextra->icon)
       
  5827             return *d->extra->topextra->icon;
       
  5828         w = w->parentWidget();
       
  5829     }
       
  5830     return QApplication::windowIcon();
       
  5831 }
       
  5832 
       
  5833 void QWidgetPrivate::setWindowIcon_helper()
       
  5834 {
       
  5835     QEvent e(QEvent::WindowIconChange);
       
  5836     QApplication::sendEvent(q_func(), &e);
       
  5837     for (int i = 0; i < children.size(); ++i) {
       
  5838         QWidget *w = qobject_cast<QWidget *>(children.at(i));
       
  5839         if (w && !w->isWindow())
       
  5840             QApplication::sendEvent(w, &e);
       
  5841     }
       
  5842 }
       
  5843 
       
  5844 void QWidget::setWindowIcon(const QIcon &icon)
       
  5845 {
       
  5846     Q_D(QWidget);
       
  5847 
       
  5848     setAttribute(Qt::WA_SetWindowIcon, !icon.isNull());
       
  5849     d->createTLExtra();
       
  5850 
       
  5851     if (!d->extra->topextra->icon)
       
  5852         d->extra->topextra->icon = new QIcon();
       
  5853     *d->extra->topextra->icon = icon;
       
  5854 
       
  5855     delete d->extra->topextra->iconPixmap;
       
  5856     d->extra->topextra->iconPixmap = 0;
       
  5857 
       
  5858     d->setWindowIcon_sys();
       
  5859     d->setWindowIcon_helper();
       
  5860 }
       
  5861 
       
  5862 
       
  5863 /*!
       
  5864     \property QWidget::windowIconText
       
  5865     \brief the widget's icon text
       
  5866 
       
  5867     This property only makes sense for windows. If no icon
       
  5868     text has been set, this functions returns an empty string.
       
  5869 
       
  5870     \sa windowIcon, windowTitle
       
  5871 */
       
  5872 
       
  5873 QString QWidget::windowIconText() const
       
  5874 {
       
  5875     Q_D(const QWidget);
       
  5876     return (d->extra && d->extra->topextra) ? d->extra->topextra->iconText : QString();
       
  5877 }
       
  5878 
       
  5879 /*!
       
  5880     \property QWidget::windowFilePath
       
  5881     \since 4.4
       
  5882     \brief the file path associated with a widget
       
  5883 
       
  5884     This property only makes sense for windows. It associates a file path with
       
  5885     a window. If you set the file path, but have not set the window title, Qt
       
  5886     sets the window title to contain a string created using the following
       
  5887     components.
       
  5888 
       
  5889     On Mac OS X:
       
  5890 
       
  5891     \list
       
  5892     \o The file name of the specified path, obtained using QFileInfo::fileName().
       
  5893     \endlist
       
  5894 
       
  5895     On Windows and X11:
       
  5896 
       
  5897     \list
       
  5898     \o The file name of the specified path, obtained using QFileInfo::fileName().
       
  5899     \o An optional \c{*} character, if the \l windowModified property is set.
       
  5900     \o The \c{0x2014} unicode character, padded either side by spaces.
       
  5901     \o The application name, obtained from the application's
       
  5902     \l{QCoreApplication::}{applicationName} property.
       
  5903     \endlist
       
  5904 
       
  5905     If the window title is set at any point, then the window title takes precedence and
       
  5906     will be shown instead of the file path string.
       
  5907 
       
  5908     Additionally, on Mac OS X, this has an added benefit that it sets the
       
  5909     \l{http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/XHIGWindows/chapter_17_section_3.html}{proxy icon}
       
  5910     for the window, assuming that the file path exists.
       
  5911 
       
  5912     If no file path is set, this property contains an empty string.
       
  5913 
       
  5914     By default, this property contains an empty string.
       
  5915 
       
  5916     \sa windowTitle, windowIcon
       
  5917 */
       
  5918 
       
  5919 QString QWidget::windowFilePath() const
       
  5920 {
       
  5921     Q_D(const QWidget);
       
  5922     return (d->extra && d->extra->topextra) ? d->extra->topextra->filePath : QString();
       
  5923 }
       
  5924 
       
  5925 void QWidget::setWindowFilePath(const QString &filePath)
       
  5926 {
       
  5927     if (filePath == windowFilePath())
       
  5928         return;
       
  5929 
       
  5930     Q_D(QWidget);
       
  5931 
       
  5932     d->createTLExtra();
       
  5933     d->extra->topextra->filePath = filePath;
       
  5934     d->setWindowFilePath_helper(filePath);
       
  5935 }
       
  5936 
       
  5937 void QWidgetPrivate::setWindowFilePath_helper(const QString &filePath)
       
  5938 {
       
  5939     if (extra->topextra && extra->topextra->caption.isEmpty()) {
       
  5940 #ifdef Q_WS_MAC
       
  5941         setWindowTitle_helper(QFileInfo(filePath).fileName());
       
  5942 #else
       
  5943         Q_Q(QWidget);
       
  5944         Q_UNUSED(filePath);
       
  5945         setWindowTitle_helper(q->windowTitle());
       
  5946 #endif
       
  5947     }
       
  5948 #ifdef Q_WS_MAC
       
  5949     setWindowFilePath_sys(filePath);
       
  5950 #endif
       
  5951 }
       
  5952 
       
  5953 /*!
       
  5954     Returns the window's role, or an empty string.
       
  5955 
       
  5956     \sa windowIcon, windowTitle
       
  5957 */
       
  5958 
       
  5959 QString QWidget::windowRole() const
       
  5960 {
       
  5961     Q_D(const QWidget);
       
  5962     return (d->extra && d->extra->topextra) ? d->extra->topextra->role : QString();
       
  5963 }
       
  5964 
       
  5965 /*!
       
  5966     Sets the window's role to \a role. This only makes sense for
       
  5967     windows on X11.
       
  5968 */
       
  5969 void QWidget::setWindowRole(const QString &role)
       
  5970 {
       
  5971 #if defined(Q_WS_X11)
       
  5972     Q_D(QWidget);
       
  5973     d->topData()->role = role;
       
  5974     d->setWindowRole();
       
  5975 #else
       
  5976     Q_UNUSED(role)
       
  5977 #endif
       
  5978 }
       
  5979 
       
  5980 /*!
       
  5981     \property QWidget::mouseTracking
       
  5982     \brief whether mouse tracking is enabled for the widget
       
  5983 
       
  5984     If mouse tracking is disabled (the default), the widget only
       
  5985     receives mouse move events when at least one mouse button is
       
  5986     pressed while the mouse is being moved.
       
  5987 
       
  5988     If mouse tracking is enabled, the widget receives mouse move
       
  5989     events even if no buttons are pressed.
       
  5990 
       
  5991     \sa mouseMoveEvent()
       
  5992 */
       
  5993 
       
  5994 
       
  5995 /*!
       
  5996     Sets the widget's focus proxy to widget \a w. If \a w is 0, the
       
  5997     function resets this widget to have no focus proxy.
       
  5998 
       
  5999     Some widgets can "have focus", but create a child widget, such as
       
  6000     QLineEdit, to actually handle the focus. In this case, the widget
       
  6001     can set the line edit to be its focus proxy.
       
  6002 
       
  6003     setFocusProxy() sets the widget which will actually get focus when
       
  6004     "this widget" gets it. If there is a focus proxy, setFocus() and
       
  6005     hasFocus() operate on the focus proxy.
       
  6006 
       
  6007     \sa focusProxy()
       
  6008 */
       
  6009 
       
  6010 void QWidget::setFocusProxy(QWidget * w)
       
  6011 {
       
  6012     Q_D(QWidget);
       
  6013     if (!w && !d->extra)
       
  6014         return;
       
  6015 
       
  6016     for (QWidget* fp  = w; fp; fp = fp->focusProxy()) {
       
  6017         if (fp == this) {
       
  6018             qWarning("QWidget: %s (%s) already in focus proxy chain", metaObject()->className(), objectName().toLocal8Bit().constData());
       
  6019             return;
       
  6020         }
       
  6021     }
       
  6022 
       
  6023     d->createExtra();
       
  6024     d->extra->focus_proxy = w;
       
  6025 }
       
  6026 
       
  6027 
       
  6028 /*!
       
  6029     Returns the focus proxy, or 0 if there is no focus proxy.
       
  6030 
       
  6031     \sa setFocusProxy()
       
  6032 */
       
  6033 
       
  6034 QWidget * QWidget::focusProxy() const
       
  6035 {
       
  6036     Q_D(const QWidget);
       
  6037     return d->extra ? (QWidget *)d->extra->focus_proxy : 0;
       
  6038 }
       
  6039 
       
  6040 
       
  6041 /*!
       
  6042     \property QWidget::focus
       
  6043     \brief whether this widget (or its focus proxy) has the keyboard
       
  6044     input focus
       
  6045 
       
  6046     By default, this property is false.
       
  6047 
       
  6048     \note Obtaining the value of this property for a widget is effectively equivalent
       
  6049     to checking whether QApplication::focusWidget() refers to the widget.
       
  6050 
       
  6051     \sa setFocus(), clearFocus(), setFocusPolicy(), QApplication::focusWidget()
       
  6052 */
       
  6053 bool QWidget::hasFocus() const
       
  6054 {
       
  6055     const QWidget* w = this;
       
  6056     while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
       
  6057         w = w->d_func()->extra->focus_proxy;
       
  6058     if (QWidget *window = w->window()) {
       
  6059 #ifndef QT_NO_GRAPHICSVIEW
       
  6060         QWExtra *e = window->d_func()->extra;
       
  6061         if (e && e->proxyWidget && e->proxyWidget->hasFocus() && window->focusWidget() == w)
       
  6062             return true;
       
  6063 #endif
       
  6064     }
       
  6065     return (QApplication::focusWidget() == w);
       
  6066 }
       
  6067 
       
  6068 /*!
       
  6069     Gives the keyboard input focus to this widget (or its focus
       
  6070     proxy) if this widget or one of its parents is the \link
       
  6071     isActiveWindow() active window\endlink. The \a reason argument will
       
  6072     be passed into any focus event sent from this function, it is used
       
  6073     to give an explanation of what caused the widget to get focus.
       
  6074     If the window is not active, the widget will be given the focus when
       
  6075     the window becomes active.
       
  6076 
       
  6077     First, a focus out event is sent to the focus widget (if any) to
       
  6078     tell it that it is about to lose the focus. Then a focus in event
       
  6079     is sent to this widget to tell it that it just received the focus.
       
  6080     (Nothing happens if the focus in and focus out widgets are the
       
  6081     same.)
       
  6082 
       
  6083     \note On embedded platforms, setFocus() will not cause an input panel
       
  6084     to be opened by the input method. If you want this to happen, you
       
  6085     have to send a QEvent::RequestSoftwareInputPanel event to the
       
  6086     widget yourself.
       
  6087 
       
  6088     setFocus() gives focus to a widget regardless of its focus policy,
       
  6089     but does not clear any keyboard grab (see grabKeyboard()).
       
  6090 
       
  6091     Be aware that if the widget is hidden, it will not accept focus
       
  6092     until it is shown.
       
  6093 
       
  6094     \warning If you call setFocus() in a function which may itself be
       
  6095     called from focusOutEvent() or focusInEvent(), you may get an
       
  6096     infinite recursion.
       
  6097 
       
  6098     \sa hasFocus(), clearFocus(), focusInEvent(), focusOutEvent(),
       
  6099     setFocusPolicy(), focusWidget(), QApplication::focusWidget(), grabKeyboard(),
       
  6100     grabMouse(), {Keyboard Focus}, QEvent::RequestSoftwareInputPanel
       
  6101 */
       
  6102 
       
  6103 void QWidget::setFocus(Qt::FocusReason reason)
       
  6104 {
       
  6105     if (!isEnabled())
       
  6106         return;
       
  6107 
       
  6108     QWidget *f = this;
       
  6109     while (f->d_func()->extra && f->d_func()->extra->focus_proxy)
       
  6110         f = f->d_func()->extra->focus_proxy;
       
  6111 
       
  6112     if (QApplication::focusWidget() == f
       
  6113 #if defined(Q_WS_WIN)
       
  6114         && GetFocus() == f->internalWinId()
       
  6115 #endif
       
  6116        )
       
  6117         return;
       
  6118 
       
  6119 #ifndef QT_NO_GRAPHICSVIEW
       
  6120     QWidget *previousProxyFocus = 0;
       
  6121     if (QWExtra *topData = window()->d_func()->extra) {
       
  6122         if (topData->proxyWidget && topData->proxyWidget->hasFocus()) {
       
  6123             previousProxyFocus = topData->proxyWidget->widget()->focusWidget();
       
  6124             if (previousProxyFocus && previousProxyFocus->focusProxy())
       
  6125                 previousProxyFocus = previousProxyFocus->focusProxy();
       
  6126             if (previousProxyFocus == this && !topData->proxyWidget->d_func()->proxyIsGivingFocus)
       
  6127                 return;
       
  6128         }
       
  6129     }
       
  6130 #endif
       
  6131 
       
  6132     QWidget *w = f;
       
  6133     if (isHidden()) {
       
  6134         while (w && w->isHidden()) {
       
  6135             w->d_func()->focus_child = f;
       
  6136             w = w->isWindow() ? 0 : w->parentWidget();
       
  6137         }
       
  6138     } else {
       
  6139         while (w) {
       
  6140             w->d_func()->focus_child = f;
       
  6141             w = w->isWindow() ? 0 : w->parentWidget();
       
  6142         }
       
  6143     }
       
  6144 
       
  6145 #ifndef QT_NO_GRAPHICSVIEW
       
  6146     // Update proxy state
       
  6147     if (QWExtra *topData = window()->d_func()->extra) {
       
  6148         if (topData->proxyWidget && !topData->proxyWidget->hasFocus()) {
       
  6149             topData->proxyWidget->d_func()->focusFromWidgetToProxy = 1;
       
  6150             topData->proxyWidget->setFocus(reason);
       
  6151             topData->proxyWidget->d_func()->focusFromWidgetToProxy = 0;
       
  6152         }
       
  6153     }
       
  6154 #endif
       
  6155 
       
  6156     if (f->isActiveWindow()) {
       
  6157         QApplicationPrivate::setFocusWidget(f, reason);
       
  6158 #ifndef QT_NO_ACCESSIBILITY
       
  6159 # ifdef Q_OS_WIN
       
  6160         // The negation of the condition in setFocus_sys
       
  6161         if (!(testAttribute(Qt::WA_WState_Created) && window()->windowType() != Qt::Popup && internalWinId()))
       
  6162             //setFocusWidget will already post a focus event for us (that the AT client receives) on Windows
       
  6163 # endif
       
  6164             QAccessible::updateAccessibility(f, 0, QAccessible::Focus);
       
  6165 #endif
       
  6166 #ifndef QT_NO_GRAPHICSVIEW
       
  6167         if (QWExtra *topData = window()->d_func()->extra) {
       
  6168             if (topData->proxyWidget) {
       
  6169                 if (previousProxyFocus && previousProxyFocus != f) {
       
  6170                     // Send event to self
       
  6171                     QFocusEvent event(QEvent::FocusOut, reason);
       
  6172                     QPointer<QWidget> that = previousProxyFocus;
       
  6173                     QApplication::sendEvent(previousProxyFocus, &event);
       
  6174                     if (that)
       
  6175                         QApplication::sendEvent(that->style(), &event);
       
  6176                 }
       
  6177                 if (!isHidden()) {
       
  6178                     // Send event to self
       
  6179                     QFocusEvent event(QEvent::FocusIn, reason);
       
  6180                     QPointer<QWidget> that = f;
       
  6181                     QApplication::sendEvent(f, &event);
       
  6182                     if (that)
       
  6183                         QApplication::sendEvent(that->style(), &event);
       
  6184                 }
       
  6185             }
       
  6186         }
       
  6187 #endif
       
  6188     }
       
  6189 }
       
  6190 
       
  6191 /*!
       
  6192     \fn void QWidget::setFocus()
       
  6193     \overload
       
  6194 
       
  6195     Gives the keyboard input focus to this widget (or its focus
       
  6196     proxy) if this widget or one of its parents is the
       
  6197     \l{isActiveWindow()}{active window}.
       
  6198 */
       
  6199 
       
  6200 /*!
       
  6201     Takes keyboard input focus from the widget.
       
  6202 
       
  6203     If the widget has active focus, a \link focusOutEvent() focus out
       
  6204     event\endlink is sent to this widget to tell it that it is about
       
  6205     to lose the focus.
       
  6206 
       
  6207     This widget must enable focus setting in order to get the keyboard
       
  6208     input focus, i.e. it must call setFocusPolicy().
       
  6209 
       
  6210     \sa hasFocus(), setFocus(), focusInEvent(), focusOutEvent(),
       
  6211     setFocusPolicy(), QApplication::focusWidget()
       
  6212 */
       
  6213 
       
  6214 void QWidget::clearFocus()
       
  6215 {
       
  6216     QWidget *w = this;
       
  6217     while (w) {
       
  6218         if (w->d_func()->focus_child == this)
       
  6219             w->d_func()->focus_child = 0;
       
  6220         w = w->parentWidget();
       
  6221     }
       
  6222 #ifndef QT_NO_GRAPHICSVIEW
       
  6223     QWExtra *topData = d_func()->extra;
       
  6224     if (topData && topData->proxyWidget)
       
  6225         topData->proxyWidget->clearFocus();
       
  6226 #endif
       
  6227 
       
  6228     if (hasFocus()) {
       
  6229         // Update proxy state
       
  6230         QApplicationPrivate::setFocusWidget(0, Qt::OtherFocusReason);
       
  6231 #if defined(Q_WS_WIN)
       
  6232         if (!(windowType() == Qt::Popup) && GetFocus() == internalWinId())
       
  6233             SetFocus(0);
       
  6234         else
       
  6235 #endif
       
  6236         {
       
  6237 #ifndef QT_NO_ACCESSIBILITY
       
  6238             QAccessible::updateAccessibility(this, 0, QAccessible::Focus);
       
  6239 #endif
       
  6240         }
       
  6241     }
       
  6242 }
       
  6243 
       
  6244 
       
  6245 /*!
       
  6246     \fn bool QWidget::focusNextChild()
       
  6247 
       
  6248     Finds a new widget to give the keyboard focus to, as appropriate
       
  6249     for \key Tab, and returns true if it can find a new widget, or
       
  6250     false if it can't.
       
  6251 
       
  6252     \sa focusPreviousChild()
       
  6253 */
       
  6254 
       
  6255 /*!
       
  6256     \fn bool QWidget::focusPreviousChild()
       
  6257 
       
  6258     Finds a new widget to give the keyboard focus to, as appropriate
       
  6259     for \key Shift+Tab, and returns true if it can find a new widget,
       
  6260     or false if it can't.
       
  6261 
       
  6262     \sa focusNextChild()
       
  6263 */
       
  6264 
       
  6265 /*!
       
  6266     Finds a new widget to give the keyboard focus to, as appropriate
       
  6267     for Tab and Shift+Tab, and returns true if it can find a new
       
  6268     widget, or false if it can't.
       
  6269 
       
  6270     If \a next is true, this function searches forward, if \a next
       
  6271     is false, it searches backward.
       
  6272 
       
  6273     Sometimes, you will want to reimplement this function. For
       
  6274     example, a web browser might reimplement it to move its "current
       
  6275     active link" forward or backward, and call
       
  6276     focusNextPrevChild() only when it reaches the last or
       
  6277     first link on the "page".
       
  6278 
       
  6279     Child widgets call focusNextPrevChild() on their parent widgets,
       
  6280     but only the window that contains the child widgets decides where
       
  6281     to redirect focus. By reimplementing this function for an object,
       
  6282     you thus gain control of focus traversal for all child widgets.
       
  6283 
       
  6284     \sa focusNextChild(), focusPreviousChild()
       
  6285 */
       
  6286 
       
  6287 bool QWidget::focusNextPrevChild(bool next)
       
  6288 {
       
  6289     Q_D(QWidget);
       
  6290     QWidget* p = parentWidget();
       
  6291     bool isSubWindow = (windowType() == Qt::SubWindow);
       
  6292     if (!isWindow() && !isSubWindow && p)
       
  6293         return p->focusNextPrevChild(next);
       
  6294 #ifndef QT_NO_GRAPHICSVIEW
       
  6295     if (d->extra && d->extra->proxyWidget)
       
  6296         return d->extra->proxyWidget->focusNextPrevChild(next);
       
  6297 #endif
       
  6298     QWidget *w = QApplicationPrivate::focusNextPrevChild_helper(this, next);
       
  6299     if (!w) return false;
       
  6300 
       
  6301     w->setFocus(next ? Qt::TabFocusReason : Qt::BacktabFocusReason);
       
  6302     return true;
       
  6303 }
       
  6304 
       
  6305 /*!
       
  6306     Returns the last child of this widget that setFocus had been
       
  6307     called on.  For top level widgets this is the widget that will get
       
  6308     focus in case this window gets activated
       
  6309 
       
  6310     This is not the same as QApplication::focusWidget(), which returns
       
  6311     the focus widget in the currently active window.
       
  6312 */
       
  6313 
       
  6314 QWidget *QWidget::focusWidget() const
       
  6315 {
       
  6316     return const_cast<QWidget *>(d_func()->focus_child);
       
  6317 }
       
  6318 
       
  6319 /*!
       
  6320     Returns the next widget in this widget's focus chain.
       
  6321 
       
  6322     \sa previousInFocusChain()
       
  6323 */
       
  6324 QWidget *QWidget::nextInFocusChain() const
       
  6325 {
       
  6326     return const_cast<QWidget *>(d_func()->focus_next);
       
  6327 }
       
  6328 
       
  6329 /*!
       
  6330     \brief The previousInFocusChain function returns the previous
       
  6331     widget in this widget's focus chain.
       
  6332 
       
  6333     \sa nextInFocusChain()
       
  6334 
       
  6335     \since 4.6
       
  6336 */
       
  6337 QWidget *QWidget::previousInFocusChain() const
       
  6338 {
       
  6339     return const_cast<QWidget *>(d_func()->focus_prev);
       
  6340 }
       
  6341 
       
  6342 /*!
       
  6343     \property QWidget::isActiveWindow
       
  6344     \brief whether this widget's window is the active window
       
  6345 
       
  6346     The active window is the window that contains the widget that has
       
  6347     keyboard focus (The window may still have focus if it has no
       
  6348     widgets or none of its widgets accepts keyboard focus).
       
  6349 
       
  6350     When popup windows are visible, this property is true for both the
       
  6351     active window \e and for the popup.
       
  6352 
       
  6353     By default, this property is false.
       
  6354 
       
  6355     \sa activateWindow(), QApplication::activeWindow()
       
  6356 */
       
  6357 bool QWidget::isActiveWindow() const
       
  6358 {
       
  6359     QWidget *tlw = window();
       
  6360     if(tlw == QApplication::activeWindow() || (isVisible() && (tlw->windowType() == Qt::Popup)))
       
  6361         return true;
       
  6362 
       
  6363 #ifndef QT_NO_GRAPHICSVIEW
       
  6364     if (QWExtra *tlwExtra = tlw->d_func()->extra) {
       
  6365         if (isVisible() && tlwExtra->proxyWidget)
       
  6366             return tlwExtra->proxyWidget->isActiveWindow();
       
  6367     }
       
  6368 #endif
       
  6369 
       
  6370 #ifdef Q_WS_MAC
       
  6371     extern bool qt_mac_is_macdrawer(const QWidget *); //qwidget_mac.cpp
       
  6372     if(qt_mac_is_macdrawer(tlw) &&
       
  6373        tlw->parentWidget() && tlw->parentWidget()->isActiveWindow())
       
  6374         return true;
       
  6375 
       
  6376     extern bool qt_mac_insideKeyWindow(const QWidget *); //qwidget_mac.cpp
       
  6377     if (QApplication::testAttribute(Qt::AA_MacPluginApplication) && qt_mac_insideKeyWindow(tlw))
       
  6378         return true;
       
  6379 #endif
       
  6380     if(style()->styleHint(QStyle::SH_Widget_ShareActivation, 0, this)) {
       
  6381         if(tlw->windowType() == Qt::Tool &&
       
  6382            !tlw->isModal() &&
       
  6383            (!tlw->parentWidget() || tlw->parentWidget()->isActiveWindow()))
       
  6384            return true;
       
  6385         QWidget *w = QApplication::activeWindow();
       
  6386         while(w && tlw->windowType() == Qt::Tool &&
       
  6387               !w->isModal() && w->parentWidget()) {
       
  6388             w = w->parentWidget()->window();
       
  6389             if(w == tlw)
       
  6390                 return true;
       
  6391         }
       
  6392     }
       
  6393 #if defined(Q_WS_WIN32)
       
  6394     HWND active = GetActiveWindow();
       
  6395     if (!tlw->testAttribute(Qt::WA_WState_Created))
       
  6396         return false;
       
  6397     return active == tlw->internalWinId() || ::IsChild(active, tlw->internalWinId());
       
  6398 #else
       
  6399     return false;
       
  6400 #endif
       
  6401 }
       
  6402 
       
  6403 /*!
       
  6404     Puts the \a second widget after the \a first widget in the focus order.
       
  6405 
       
  6406     Note that since the tab order of the \a second widget is changed, you
       
  6407     should order a chain like this:
       
  6408 
       
  6409     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 9
       
  6410 
       
  6411     \e not like this:
       
  6412 
       
  6413     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 10
       
  6414 
       
  6415     If \a first or \a second has a focus proxy, setTabOrder()
       
  6416     correctly substitutes the proxy.
       
  6417 
       
  6418     \sa setFocusPolicy(), setFocusProxy(), {Keyboard Focus}
       
  6419 */
       
  6420 void QWidget::setTabOrder(QWidget* first, QWidget *second)
       
  6421 {
       
  6422     if (!first || !second || first->focusPolicy() == Qt::NoFocus || second->focusPolicy() == Qt::NoFocus)
       
  6423         return;
       
  6424 
       
  6425     if (first->window() != second->window()) {
       
  6426         qWarning("QWidget::setTabOrder: 'first' and 'second' must be in the same window");
       
  6427         return;
       
  6428     }
       
  6429 
       
  6430     QWidget *fp = first->focusProxy();
       
  6431     if (fp) {
       
  6432         // If first is redirected, set first to the last child of first
       
  6433         // that can take keyboard focus so that second is inserted after
       
  6434         // that last child, and the focus order within first is (more
       
  6435         // likely to be) preserved.
       
  6436         QList<QWidget *> l = qFindChildren<QWidget *>(first);
       
  6437         for (int i = l.size()-1; i >= 0; --i) {
       
  6438             QWidget * next = l.at(i);
       
  6439             if (next->window() == fp->window()) {
       
  6440                 fp = next;
       
  6441                 if (fp->focusPolicy() != Qt::NoFocus)
       
  6442                     break;
       
  6443             }
       
  6444         }
       
  6445         first = fp;
       
  6446     }
       
  6447 
       
  6448     if (fp == second)
       
  6449         return;
       
  6450 
       
  6451     if (QWidget *sp = second->focusProxy())
       
  6452         second = sp;
       
  6453 
       
  6454 //    QWidget *fp = first->d_func()->focus_prev;
       
  6455     QWidget *fn = first->d_func()->focus_next;
       
  6456 
       
  6457     if (fn == second)
       
  6458         return;
       
  6459 
       
  6460     QWidget *sp = second->d_func()->focus_prev;
       
  6461     QWidget *sn = second->d_func()->focus_next;
       
  6462 
       
  6463     fn->d_func()->focus_prev = second;
       
  6464     first->d_func()->focus_next = second;
       
  6465 
       
  6466     second->d_func()->focus_next = fn;
       
  6467     second->d_func()->focus_prev = first;
       
  6468 
       
  6469     sp->d_func()->focus_next = sn;
       
  6470     sn->d_func()->focus_prev = sp;
       
  6471 
       
  6472 
       
  6473     Q_ASSERT(first->d_func()->focus_next->d_func()->focus_prev == first);
       
  6474     Q_ASSERT(first->d_func()->focus_prev->d_func()->focus_next == first);
       
  6475 
       
  6476     Q_ASSERT(second->d_func()->focus_next->d_func()->focus_prev == second);
       
  6477     Q_ASSERT(second->d_func()->focus_prev->d_func()->focus_next == second);
       
  6478 }
       
  6479 
       
  6480 /*!\internal
       
  6481 
       
  6482   Moves the relevant subwidgets of this widget from the \a oldtlw's
       
  6483   tab chain to that of the new parent, if there's anything to move and
       
  6484   we're really moving
       
  6485 
       
  6486   This function is called from QWidget::reparent() *after* the widget
       
  6487   has been reparented.
       
  6488 
       
  6489   \sa reparent()
       
  6490 */
       
  6491 
       
  6492 void QWidgetPrivate::reparentFocusWidgets(QWidget * oldtlw)
       
  6493 {
       
  6494     Q_Q(QWidget);
       
  6495     if (oldtlw == q->window())
       
  6496         return; // nothing to do
       
  6497 
       
  6498     if(focus_child)
       
  6499         focus_child->clearFocus();
       
  6500 
       
  6501     // separate the focus chain into new (children of myself) and old (the rest)
       
  6502     QWidget *firstOld = 0;
       
  6503     //QWidget *firstNew = q; //invariant
       
  6504     QWidget *o = 0; // last in the old list
       
  6505     QWidget *n = q; // last in the new list
       
  6506 
       
  6507     bool prevWasNew = true;
       
  6508     QWidget *w = focus_next;
       
  6509 
       
  6510     //Note: for efficiency, we do not maintain the list invariant inside the loop
       
  6511     //we append items to the relevant list, and we optimize by not changing pointers
       
  6512     //when subsequent items are going into the same list.
       
  6513     while (w  != q) {
       
  6514         bool currentIsNew =  q->isAncestorOf(w);
       
  6515         if (currentIsNew) {
       
  6516             if (!prevWasNew) {
       
  6517                 //prev was old -- append to new list
       
  6518                 n->d_func()->focus_next = w;
       
  6519                 w->d_func()->focus_prev = n;
       
  6520             }
       
  6521             n = w;
       
  6522         } else {
       
  6523             if (prevWasNew) {
       
  6524                 //prev was new -- append to old list, if there is one
       
  6525                 if (o) {
       
  6526                     o->d_func()->focus_next = w;
       
  6527                     w->d_func()->focus_prev = o;
       
  6528                 } else {
       
  6529                     // "create" the old list
       
  6530                     firstOld = w;
       
  6531                 }
       
  6532             }
       
  6533             o = w;
       
  6534         }
       
  6535         w = w->d_func()->focus_next;
       
  6536         prevWasNew = currentIsNew;
       
  6537     }
       
  6538 
       
  6539     //repair the old list:
       
  6540     if (firstOld) {
       
  6541         o->d_func()->focus_next = firstOld;
       
  6542         firstOld->d_func()->focus_prev = o;
       
  6543     }
       
  6544 
       
  6545     if (!q->isWindow()) {
       
  6546         QWidget *topLevel = q->window();
       
  6547         //insert new chain into toplevel's chain
       
  6548 
       
  6549         QWidget *prev = topLevel->d_func()->focus_prev;
       
  6550 
       
  6551         topLevel->d_func()->focus_prev = n;
       
  6552         prev->d_func()->focus_next = q;
       
  6553 
       
  6554         focus_prev = prev;
       
  6555         n->d_func()->focus_next = topLevel;
       
  6556     } else {
       
  6557         //repair the new list
       
  6558             n->d_func()->focus_next = q;
       
  6559             focus_prev = n;
       
  6560     }
       
  6561 
       
  6562 }
       
  6563 
       
  6564 /*!\internal
       
  6565 
       
  6566   Measures the shortest distance from a point to a rect.
       
  6567 
       
  6568   This function is called from QDesktopwidget::screen(QPoint) to find the
       
  6569   closest screen for a point.
       
  6570   In directional KeypadNavigation, it is called to find the closest
       
  6571   widget to the current focus widget center.
       
  6572 */
       
  6573 int QWidgetPrivate::pointToRect(const QPoint &p, const QRect &r)
       
  6574 {
       
  6575     int dx = 0;
       
  6576     int dy = 0;
       
  6577     if (p.x() < r.left())
       
  6578         dx = r.left() - p.x();
       
  6579     else if (p.x() > r.right())
       
  6580         dx = p.x() - r.right();
       
  6581     if (p.y() < r.top())
       
  6582         dy = r.top() - p.y();
       
  6583     else if (p.y() > r.bottom())
       
  6584         dy = p.y() - r.bottom();
       
  6585     return dx + dy;
       
  6586 }
       
  6587 
       
  6588 /*!
       
  6589     \property QWidget::frameSize
       
  6590     \brief the size of the widget including any window frame
       
  6591 
       
  6592     By default, this property contains a value that depends on the user's
       
  6593     platform and screen geometry.
       
  6594 */
       
  6595 QSize QWidget::frameSize() const
       
  6596 {
       
  6597     Q_D(const QWidget);
       
  6598     if (isWindow() && !(windowType() == Qt::Popup)) {
       
  6599         QRect fs = d->frameStrut();
       
  6600         return QSize(data->crect.width() + fs.left() + fs.right(),
       
  6601                       data->crect.height() + fs.top() + fs.bottom());
       
  6602     }
       
  6603     return data->crect.size();
       
  6604 }
       
  6605 
       
  6606 /*! \fn void QWidget::move(int x, int y)
       
  6607 
       
  6608     \overload
       
  6609 
       
  6610     This corresponds to move(QPoint(\a x, \a y)).
       
  6611 */
       
  6612 
       
  6613 void QWidget::move(const QPoint &p)
       
  6614 {
       
  6615     Q_D(QWidget);
       
  6616     setAttribute(Qt::WA_Moved);
       
  6617     if (isWindow())
       
  6618         d->topData()->posFromMove = true;
       
  6619     if (testAttribute(Qt::WA_WState_Created)) {
       
  6620         d->setGeometry_sys(p.x() + geometry().x() - QWidget::x(),
       
  6621                        p.y() + geometry().y() - QWidget::y(),
       
  6622                        width(), height(), true);
       
  6623         d->setDirtyOpaqueRegion();
       
  6624     } else {
       
  6625         data->crect.moveTopLeft(p); // no frame yet
       
  6626         setAttribute(Qt::WA_PendingMoveEvent);
       
  6627     }
       
  6628 }
       
  6629 
       
  6630 /*! \fn void QWidget::resize(int w, int h)
       
  6631     \overload
       
  6632 
       
  6633     This corresponds to resize(QSize(\a w, \a h)).
       
  6634 */
       
  6635 
       
  6636 void QWidget::resize(const QSize &s)
       
  6637 {
       
  6638     Q_D(QWidget);
       
  6639     setAttribute(Qt::WA_Resized);
       
  6640     if (testAttribute(Qt::WA_WState_Created)) {
       
  6641         d->setGeometry_sys(geometry().x(), geometry().y(), s.width(), s.height(), false);
       
  6642         d->setDirtyOpaqueRegion();
       
  6643     } else {
       
  6644         data->crect.setSize(s.boundedTo(maximumSize()).expandedTo(minimumSize()));
       
  6645         setAttribute(Qt::WA_PendingResizeEvent);
       
  6646     }
       
  6647 }
       
  6648 
       
  6649 void QWidget::setGeometry(const QRect &r)
       
  6650 {
       
  6651     Q_D(QWidget);
       
  6652     setAttribute(Qt::WA_Resized);
       
  6653     setAttribute(Qt::WA_Moved);
       
  6654     if (isWindow())
       
  6655         d->topData()->posFromMove = false;
       
  6656     if (testAttribute(Qt::WA_WState_Created)) {
       
  6657         d->setGeometry_sys(r.x(), r.y(), r.width(), r.height(), true);
       
  6658         d->setDirtyOpaqueRegion();
       
  6659     } else {
       
  6660         data->crect.setTopLeft(r.topLeft());
       
  6661         data->crect.setSize(r.size().boundedTo(maximumSize()).expandedTo(minimumSize()));
       
  6662         setAttribute(Qt::WA_PendingMoveEvent);
       
  6663         setAttribute(Qt::WA_PendingResizeEvent);
       
  6664     }
       
  6665 }
       
  6666 
       
  6667 /*!
       
  6668     \since 4.2
       
  6669     Saves the current geometry and state for top-level widgets.
       
  6670 
       
  6671     To save the geometry when the window closes, you can
       
  6672     implement a close event like this:
       
  6673 
       
  6674     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 11
       
  6675 
       
  6676     See the \l{Window Geometry} documentation for an overview of geometry
       
  6677     issues with windows.
       
  6678 
       
  6679     Use QMainWindow::saveState() to save the geometry and the state of
       
  6680     toolbars and dock widgets.
       
  6681 
       
  6682     \sa restoreGeometry(), QMainWindow::saveState(), QMainWindow::restoreState()
       
  6683 */
       
  6684 QByteArray QWidget::saveGeometry() const
       
  6685 {
       
  6686     QByteArray array;
       
  6687     QDataStream stream(&array, QIODevice::WriteOnly);
       
  6688     stream.setVersion(QDataStream::Qt_4_0);
       
  6689     const quint32 magicNumber = 0x1D9D0CB;
       
  6690     quint16 majorVersion = 1;
       
  6691     quint16 minorVersion = 0;
       
  6692     stream << magicNumber
       
  6693            << majorVersion
       
  6694            << minorVersion
       
  6695            << frameGeometry()
       
  6696            << normalGeometry()
       
  6697            << qint32(QApplication::desktop()->screenNumber(this))
       
  6698            << quint8(windowState() & Qt::WindowMaximized)
       
  6699            << quint8(windowState() & Qt::WindowFullScreen);
       
  6700     return array;
       
  6701 }
       
  6702 
       
  6703 /*!
       
  6704     \since 4.2
       
  6705 
       
  6706     Restores the geometry and state top-level widgets stored in the
       
  6707     byte array \a geometry. Returns true on success; otherwise
       
  6708     returns false.
       
  6709 
       
  6710     If the restored geometry is off-screen, it will be modified to be
       
  6711     inside the available screen geometry.
       
  6712 
       
  6713     To restore geometry saved using QSettings, you can use code like
       
  6714     this:
       
  6715 
       
  6716     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 12
       
  6717 
       
  6718     See the \l{Window Geometry} documentation for an overview of geometry
       
  6719     issues with windows.
       
  6720 
       
  6721     Use QMainWindow::restoreState() to restore the geometry and the
       
  6722     state of toolbars and dock widgets.
       
  6723 
       
  6724     \sa saveGeometry(), QSettings, QMainWindow::saveState(), QMainWindow::restoreState()
       
  6725 */
       
  6726 bool QWidget::restoreGeometry(const QByteArray &geometry)
       
  6727 {
       
  6728     if (geometry.size() < 4)
       
  6729         return false;
       
  6730     QDataStream stream(geometry);
       
  6731     stream.setVersion(QDataStream::Qt_4_0);
       
  6732 
       
  6733     const quint32 magicNumber = 0x1D9D0CB;
       
  6734     quint32 storedMagicNumber;
       
  6735     stream >> storedMagicNumber;
       
  6736     if (storedMagicNumber != magicNumber)
       
  6737         return false;
       
  6738 
       
  6739     const quint16 currentMajorVersion = 1;
       
  6740     quint16 majorVersion = 0;
       
  6741     quint16 minorVersion = 0;
       
  6742 
       
  6743     stream >> majorVersion >> minorVersion;
       
  6744 
       
  6745     if (majorVersion != currentMajorVersion)
       
  6746         return false;
       
  6747     // (Allow all minor versions.)
       
  6748 
       
  6749     QRect restoredFrameGeometry;
       
  6750      QRect restoredNormalGeometry;
       
  6751     qint32 restoredScreenNumber;
       
  6752     quint8 maximized;
       
  6753     quint8 fullScreen;
       
  6754 
       
  6755     stream >> restoredFrameGeometry
       
  6756            >> restoredNormalGeometry
       
  6757            >> restoredScreenNumber
       
  6758            >> maximized
       
  6759            >> fullScreen;
       
  6760 
       
  6761     const int frameHeight = 20;
       
  6762     if (!restoredFrameGeometry.isValid())
       
  6763         restoredFrameGeometry = QRect(QPoint(0,0), sizeHint());
       
  6764 
       
  6765     if (!restoredNormalGeometry.isValid())
       
  6766         restoredNormalGeometry = QRect(QPoint(0, frameHeight), sizeHint());
       
  6767     if (!restoredNormalGeometry.isValid()) {
       
  6768         // use the widget's adjustedSize if the sizeHint() doesn't help
       
  6769         restoredNormalGeometry.setSize(restoredNormalGeometry
       
  6770                                        .size()
       
  6771                                        .expandedTo(d_func()->adjustedSize()));
       
  6772     }
       
  6773 
       
  6774     const QDesktopWidget * const desktop = QApplication::desktop();
       
  6775     if (restoredScreenNumber >= desktop->numScreens())
       
  6776         restoredScreenNumber = desktop->primaryScreen();
       
  6777 
       
  6778     const QRect availableGeometry = desktop->availableGeometry(restoredScreenNumber);
       
  6779 
       
  6780     // Modify the restored geometry if we are about to restore to coordinates
       
  6781     // that would make the window "lost". This happens if:
       
  6782     // - The restored geometry is completely oustside the available geometry
       
  6783     // - The title bar is outside the available geometry.
       
  6784     // - (Mac only) The window is higher than the available geometry. It must
       
  6785     //   be possible to bring the size grip on screen by moving the window.
       
  6786 #ifdef Q_WS_MAC
       
  6787     restoredFrameGeometry.setHeight(qMin(restoredFrameGeometry.height(), availableGeometry.height()));
       
  6788     restoredNormalGeometry.setHeight(qMin(restoredNormalGeometry.height(), availableGeometry.height() - frameHeight));
       
  6789 #endif
       
  6790 
       
  6791     if (!restoredFrameGeometry.intersects(availableGeometry)) {
       
  6792         restoredFrameGeometry.moveBottom(qMin(restoredFrameGeometry.bottom(), availableGeometry.bottom()));
       
  6793         restoredFrameGeometry.moveLeft(qMax(restoredFrameGeometry.left(), availableGeometry.left()));
       
  6794         restoredFrameGeometry.moveRight(qMin(restoredFrameGeometry.right(), availableGeometry.right()));
       
  6795     }
       
  6796     restoredFrameGeometry.moveTop(qMax(restoredFrameGeometry.top(), availableGeometry.top()));
       
  6797 
       
  6798     if (!restoredNormalGeometry.intersects(availableGeometry)) {
       
  6799         restoredNormalGeometry.moveBottom(qMin(restoredNormalGeometry.bottom(), availableGeometry.bottom()));
       
  6800         restoredNormalGeometry.moveLeft(qMax(restoredNormalGeometry.left(), availableGeometry.left()));
       
  6801         restoredNormalGeometry.moveRight(qMin(restoredNormalGeometry.right(), availableGeometry.right()));
       
  6802     }
       
  6803     restoredNormalGeometry.moveTop(qMax(restoredNormalGeometry.top(), availableGeometry.top() + frameHeight));
       
  6804 
       
  6805     if (maximized || fullScreen) {
       
  6806         // set geomerty before setting the window state to make
       
  6807         // sure the window is maximized to the right screen.
       
  6808         setGeometry(restoredNormalGeometry);
       
  6809         Qt::WindowStates ws = windowState();
       
  6810         if (maximized)
       
  6811             ws |= Qt::WindowMaximized;
       
  6812         if (fullScreen)
       
  6813             ws |= Qt::WindowFullScreen;
       
  6814        setWindowState(ws);
       
  6815        d_func()->topData()->normalGeometry = restoredNormalGeometry;
       
  6816     } else {
       
  6817         QPoint offset;
       
  6818 #ifdef Q_WS_X11
       
  6819         if (isFullScreen())
       
  6820             offset = d_func()->topData()->fullScreenOffset;
       
  6821 #endif
       
  6822         setWindowState(windowState() & ~(Qt::WindowMaximized | Qt::WindowFullScreen));
       
  6823         move(restoredFrameGeometry.topLeft() + offset);
       
  6824         resize(restoredNormalGeometry.size());
       
  6825     }
       
  6826     return true;
       
  6827 }
       
  6828 
       
  6829 /*!\fn void QWidget::setGeometry(int x, int y, int w, int h)
       
  6830     \overload
       
  6831 
       
  6832     This corresponds to setGeometry(QRect(\a x, \a y, \a w, \a h)).
       
  6833 */
       
  6834 
       
  6835 /*!
       
  6836   Sets the margins around the contents of the widget to have the sizes
       
  6837   \a left, \a top, \a right, and \a bottom. The margins are used by
       
  6838   the layout system, and may be used by subclasses to specify the area
       
  6839   to draw in (e.g. excluding the frame).
       
  6840 
       
  6841   Changing the margins will trigger a resizeEvent().
       
  6842 
       
  6843   \sa contentsRect(), getContentsMargins()
       
  6844 */
       
  6845 void QWidget::setContentsMargins(int left, int top, int right, int bottom)
       
  6846 {
       
  6847     Q_D(QWidget);
       
  6848     if (left == d->leftmargin && top == d->topmargin
       
  6849          && right == d->rightmargin && bottom == d->bottommargin)
       
  6850         return;
       
  6851     d->leftmargin = left;
       
  6852     d->topmargin = top;
       
  6853     d->rightmargin = right;
       
  6854     d->bottommargin = bottom;
       
  6855 
       
  6856     if (QLayout *l=d->layout)
       
  6857         l->update(); //force activate; will do updateGeometry
       
  6858     else
       
  6859         updateGeometry();
       
  6860 
       
  6861     // ### Qt 5: compat, remove
       
  6862     if (isVisible()) {
       
  6863         update();
       
  6864         QResizeEvent e(data->crect.size(), data->crect.size());
       
  6865         QApplication::sendEvent(this, &e);
       
  6866     } else {
       
  6867         setAttribute(Qt::WA_PendingResizeEvent, true);
       
  6868     }
       
  6869 
       
  6870     QEvent e(QEvent::ContentsRectChange);
       
  6871     QApplication::sendEvent(this, &e);
       
  6872 }
       
  6873 
       
  6874 /*!
       
  6875   \overload
       
  6876   \since 4.6
       
  6877 
       
  6878   \brief The setContentsMargins function sets the margins around the
       
  6879   widget's contents.
       
  6880 
       
  6881   Sets the margins around the contents of the widget to have the
       
  6882   sizes determined by \a margins. The margins are
       
  6883   used by the layout system, and may be used by subclasses to
       
  6884   specify the area to draw in (e.g. excluding the frame).
       
  6885 
       
  6886   Changing the margins will trigger a resizeEvent().
       
  6887 
       
  6888   \sa contentsRect(), getContentsMargins()
       
  6889 */
       
  6890 void QWidget::setContentsMargins(const QMargins &margins)
       
  6891 {
       
  6892     setContentsMargins(margins.left(), margins.top(),
       
  6893                        margins.right(), margins.bottom());
       
  6894 }
       
  6895 
       
  6896 /*!
       
  6897   Returns the widget's contents margins for \a left, \a top, \a
       
  6898   right, and \a bottom.
       
  6899 
       
  6900   \sa setContentsMargins(), contentsRect()
       
  6901  */
       
  6902 void QWidget::getContentsMargins(int *left, int *top, int *right, int *bottom) const
       
  6903 {
       
  6904     Q_D(const QWidget);
       
  6905     if (left)
       
  6906         *left = d->leftmargin;
       
  6907     if (top)
       
  6908         *top = d->topmargin;
       
  6909     if (right)
       
  6910         *right = d->rightmargin;
       
  6911     if (bottom)
       
  6912         *bottom = d->bottommargin;
       
  6913 }
       
  6914 
       
  6915 /*!
       
  6916   \since 4.6
       
  6917 
       
  6918   \brief The contentsMargins function returns the widget's contents margins.
       
  6919 
       
  6920   \sa getContentsMargins(), setContentsMargins(), contentsRect()
       
  6921  */
       
  6922 QMargins QWidget::contentsMargins() const
       
  6923 {
       
  6924     Q_D(const QWidget);
       
  6925     return QMargins(d->leftmargin, d->topmargin, d->rightmargin, d->bottommargin);
       
  6926 }
       
  6927 
       
  6928 
       
  6929 /*!
       
  6930     Returns the area inside the widget's margins.
       
  6931 
       
  6932     \sa setContentsMargins(), getContentsMargins()
       
  6933 */
       
  6934 QRect QWidget::contentsRect() const
       
  6935 {
       
  6936     Q_D(const QWidget);
       
  6937     return QRect(QPoint(d->leftmargin, d->topmargin),
       
  6938                  QPoint(data->crect.width() - 1 - d->rightmargin,
       
  6939                         data->crect.height() - 1 - d->bottommargin));
       
  6940 
       
  6941 }
       
  6942 
       
  6943 
       
  6944 
       
  6945 /*!
       
  6946   \fn void QWidget::customContextMenuRequested(const QPoint &pos)
       
  6947 
       
  6948   This signal is emitted when the widget's \l contextMenuPolicy is
       
  6949   Qt::CustomContextMenu, and the user has requested a context menu on
       
  6950   the widget. The position \a pos is the position of the context menu
       
  6951   event that the widget receives. Normally this is in widget
       
  6952   coordinates. The exception to this rule is QAbstractScrollArea and
       
  6953   its subclasses that map the context menu event to coordinates of the
       
  6954   \link QAbstractScrollArea::viewport() viewport() \endlink .
       
  6955 
       
  6956 
       
  6957   \sa mapToGlobal() QMenu contextMenuPolicy
       
  6958 */
       
  6959 
       
  6960 
       
  6961 /*!
       
  6962     \property QWidget::contextMenuPolicy
       
  6963     \brief how the widget shows a context menu
       
  6964 
       
  6965     The default value of this property is Qt::DefaultContextMenu,
       
  6966     which means the contextMenuEvent() handler is called. Other values
       
  6967     are Qt::NoContextMenu, Qt::PreventContextMenu,
       
  6968     Qt::ActionsContextMenu, and Qt::CustomContextMenu. With
       
  6969     Qt::CustomContextMenu, the signal customContextMenuRequested() is
       
  6970     emitted.
       
  6971 
       
  6972     \sa contextMenuEvent(), customContextMenuRequested(), actions()
       
  6973 */
       
  6974 
       
  6975 Qt::ContextMenuPolicy QWidget::contextMenuPolicy() const
       
  6976 {
       
  6977     return (Qt::ContextMenuPolicy)data->context_menu_policy;
       
  6978 }
       
  6979 
       
  6980 void QWidget::setContextMenuPolicy(Qt::ContextMenuPolicy policy)
       
  6981 {
       
  6982     data->context_menu_policy = (uint) policy;
       
  6983 }
       
  6984 
       
  6985 /*!
       
  6986     \property QWidget::focusPolicy
       
  6987     \brief the way the widget accepts keyboard focus
       
  6988 
       
  6989     The policy is Qt::TabFocus if the widget accepts keyboard
       
  6990     focus by tabbing, Qt::ClickFocus if the widget accepts
       
  6991     focus by clicking, Qt::StrongFocus if it accepts both, and
       
  6992     Qt::NoFocus (the default) if it does not accept focus at
       
  6993     all.
       
  6994 
       
  6995     You must enable keyboard focus for a widget if it processes
       
  6996     keyboard events. This is normally done from the widget's
       
  6997     constructor. For instance, the QLineEdit constructor calls
       
  6998     setFocusPolicy(Qt::StrongFocus).
       
  6999 
       
  7000     If the widget has a focus proxy, then the focus policy will
       
  7001     be propagated to it.
       
  7002 
       
  7003     \sa focusInEvent(), focusOutEvent(), keyPressEvent(), keyReleaseEvent(), enabled
       
  7004 */
       
  7005 
       
  7006 
       
  7007 Qt::FocusPolicy QWidget::focusPolicy() const
       
  7008 {
       
  7009     return (Qt::FocusPolicy)data->focus_policy;
       
  7010 }
       
  7011 
       
  7012 void QWidget::setFocusPolicy(Qt::FocusPolicy policy)
       
  7013 {
       
  7014     data->focus_policy = (uint) policy;
       
  7015     Q_D(QWidget);
       
  7016     if (d->extra && d->extra->focus_proxy)
       
  7017         d->extra->focus_proxy->setFocusPolicy(policy);
       
  7018 }
       
  7019 
       
  7020 /*!
       
  7021     \property QWidget::updatesEnabled
       
  7022     \brief whether updates are enabled
       
  7023 
       
  7024     An updates enabled widget receives paint events and has a system
       
  7025     background; a disabled widget does not. This also implies that
       
  7026     calling update() and repaint() has no effect if updates are
       
  7027     disabled.
       
  7028 
       
  7029     By default, this property is true.
       
  7030 
       
  7031     setUpdatesEnabled() is normally used to disable updates for a
       
  7032     short period of time, for instance to avoid screen flicker during
       
  7033     large changes. In Qt, widgets normally do not generate screen
       
  7034     flicker, but on X11 the server might erase regions on the screen
       
  7035     when widgets get hidden before they can be replaced by other
       
  7036     widgets. Disabling updates solves this.
       
  7037 
       
  7038     Example:
       
  7039     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 13
       
  7040 
       
  7041     Disabling a widget implicitly disables all its children. Enabling a widget
       
  7042     enables all child widgets \e except top-level widgets or those that
       
  7043     have been explicitly disabled. Re-enabling updates implicitly calls
       
  7044     update() on the widget.
       
  7045 
       
  7046     \sa paintEvent()
       
  7047 */
       
  7048 void QWidget::setUpdatesEnabled(bool enable)
       
  7049 {
       
  7050     Q_D(QWidget);
       
  7051     setAttribute(Qt::WA_ForceUpdatesDisabled, !enable);
       
  7052     d->setUpdatesEnabled_helper(enable);
       
  7053 }
       
  7054 
       
  7055 /*!  \fn void QWidget::show()
       
  7056 
       
  7057     Shows the widget and its child widgets. This function is
       
  7058     equivalent to setVisible(true).
       
  7059 
       
  7060     \sa raise(), showEvent(), hide(), setVisible(), showMinimized(), showMaximized(),
       
  7061     showNormal(), isVisible()
       
  7062 */
       
  7063 
       
  7064 
       
  7065 /*! \internal
       
  7066 
       
  7067    Makes the widget visible in the isVisible() meaning of the word.
       
  7068    It is only called for toplevels or widgets with visible parents.
       
  7069  */
       
  7070 void QWidgetPrivate::show_recursive()
       
  7071 {
       
  7072     Q_Q(QWidget);
       
  7073     // polish if necessary
       
  7074 
       
  7075     if (!q->testAttribute(Qt::WA_WState_Created))
       
  7076         createRecursively();
       
  7077     q->ensurePolished();
       
  7078 
       
  7079 #ifdef QT3_SUPPORT
       
  7080     if(sendChildEvents)
       
  7081         QApplication::sendPostedEvents(q, QEvent::ChildInserted);
       
  7082 #endif
       
  7083     if (!q->isWindow() && q->parentWidget()->d_func()->layout && !q->parentWidget()->data->in_show)
       
  7084         q->parentWidget()->d_func()->layout->activate();
       
  7085     // activate our layout before we and our children become visible
       
  7086     if (layout)
       
  7087         layout->activate();
       
  7088 
       
  7089     show_helper();
       
  7090 }
       
  7091 
       
  7092 void QWidgetPrivate::sendPendingMoveAndResizeEvents(bool recursive, bool disableUpdates)
       
  7093 {
       
  7094     Q_Q(QWidget);
       
  7095 
       
  7096     disableUpdates = disableUpdates && q->updatesEnabled();
       
  7097     if (disableUpdates)
       
  7098         q->setAttribute(Qt::WA_UpdatesDisabled);
       
  7099 
       
  7100     if (q->testAttribute(Qt::WA_PendingMoveEvent)) {
       
  7101         QMoveEvent e(data.crect.topLeft(), data.crect.topLeft());
       
  7102         QApplication::sendEvent(q, &e);
       
  7103         q->setAttribute(Qt::WA_PendingMoveEvent, false);
       
  7104     }
       
  7105 
       
  7106     if (q->testAttribute(Qt::WA_PendingResizeEvent)) {
       
  7107         QResizeEvent e(data.crect.size(), QSize());
       
  7108         QApplication::sendEvent(q, &e);
       
  7109         q->setAttribute(Qt::WA_PendingResizeEvent, false);
       
  7110     }
       
  7111 
       
  7112     if (disableUpdates)
       
  7113         q->setAttribute(Qt::WA_UpdatesDisabled, false);
       
  7114 
       
  7115     if (!recursive)
       
  7116         return;
       
  7117 
       
  7118     for (int i = 0; i < children.size(); ++i) {
       
  7119         if (QWidget *child = qobject_cast<QWidget *>(children.at(i)))
       
  7120             child->d_func()->sendPendingMoveAndResizeEvents(recursive, disableUpdates);
       
  7121     }
       
  7122 }
       
  7123 
       
  7124 void QWidgetPrivate::activateChildLayoutsRecursively()
       
  7125 {
       
  7126     sendPendingMoveAndResizeEvents(false, true);
       
  7127 
       
  7128     for (int i = 0; i < children.size(); ++i) {
       
  7129         QWidget *child = qobject_cast<QWidget *>(children.at(i));
       
  7130         if (!child || child->isHidden() || child->isWindow())
       
  7131             continue;
       
  7132 
       
  7133         child->ensurePolished();
       
  7134 
       
  7135         // Activate child's layout
       
  7136         QWidgetPrivate *childPrivate = child->d_func();
       
  7137         if (childPrivate->layout)
       
  7138             childPrivate->layout->activate();
       
  7139 
       
  7140         // Pretend we're visible.
       
  7141         const bool wasVisible = child->isVisible();
       
  7142         if (!wasVisible)
       
  7143             child->setAttribute(Qt::WA_WState_Visible);
       
  7144 
       
  7145         // Do the same for all my children.
       
  7146         childPrivate->activateChildLayoutsRecursively();
       
  7147 
       
  7148         // We're not cheating anymore.
       
  7149         if (!wasVisible)
       
  7150             child->setAttribute(Qt::WA_WState_Visible, false);
       
  7151     }
       
  7152 }
       
  7153 
       
  7154 void QWidgetPrivate::show_helper()
       
  7155 {
       
  7156     Q_Q(QWidget);
       
  7157     data.in_show = true; // qws optimization
       
  7158     // make sure we receive pending move and resize events
       
  7159     sendPendingMoveAndResizeEvents();
       
  7160 
       
  7161     // become visible before showing all children
       
  7162     q->setAttribute(Qt::WA_WState_Visible);
       
  7163 
       
  7164     // finally show all children recursively
       
  7165     showChildren(false);
       
  7166 
       
  7167 #ifdef QT3_SUPPORT
       
  7168     if (q->parentWidget() && sendChildEvents)
       
  7169         QApplication::sendPostedEvents(q->parentWidget(),
       
  7170                                         QEvent::ChildInserted);
       
  7171 #endif
       
  7172 
       
  7173 
       
  7174     // popup handling: new popups and tools need to be raised, and
       
  7175     // existing popups must be closed. Also propagate the current
       
  7176     // windows's KeyboardFocusChange status.
       
  7177     if (q->isWindow()) {
       
  7178         if ((q->windowType() == Qt::Tool) || (q->windowType() == Qt::Popup) || q->windowType() == Qt::ToolTip) {
       
  7179             q->raise();
       
  7180             if (q->parentWidget() && q->parentWidget()->window()->testAttribute(Qt::WA_KeyboardFocusChange))
       
  7181                 q->setAttribute(Qt::WA_KeyboardFocusChange);
       
  7182         } else {
       
  7183             while (QApplication::activePopupWidget()) {
       
  7184                 if (!QApplication::activePopupWidget()->close())
       
  7185                     break;
       
  7186             }
       
  7187         }
       
  7188     }
       
  7189 
       
  7190     // Automatic embedding of child windows of widgets already embedded into
       
  7191     // QGraphicsProxyWidget when they are shown the first time.
       
  7192     bool isEmbedded = false;
       
  7193 #ifndef QT_NO_GRAPHICSVIEW
       
  7194     if (q->isWindow()) {
       
  7195         isEmbedded = q->graphicsProxyWidget() ? true : false;
       
  7196         if (!isEmbedded && !bypassGraphicsProxyWidget(q)) {
       
  7197             QGraphicsProxyWidget *ancestorProxy = nearestGraphicsProxyWidget(q->parentWidget());
       
  7198             if (ancestorProxy) {
       
  7199                 isEmbedded = true;
       
  7200                 ancestorProxy->d_func()->embedSubWindow(q);
       
  7201             }
       
  7202         }
       
  7203     }
       
  7204 #else
       
  7205     Q_UNUSED(isEmbedded);
       
  7206 #endif
       
  7207 
       
  7208     // On Windows, show the popup now so that our own focus handling
       
  7209     // stores the correct old focus widget even if it's stolen in the
       
  7210     // showevent
       
  7211 #if defined(Q_WS_WIN) || defined(Q_WS_MAC) || defined(Q_OS_SYMBIAN)
       
  7212     if (!isEmbedded && q->windowType() == Qt::Popup)
       
  7213         qApp->d_func()->openPopup(q);
       
  7214 #endif
       
  7215 
       
  7216     // send the show event before showing the window
       
  7217     QShowEvent showEvent;
       
  7218     QApplication::sendEvent(q, &showEvent);
       
  7219 
       
  7220     if (!isEmbedded && q->isModal() && q->isWindow())
       
  7221         // QApplicationPrivate::enterModal *before* show, otherwise the initial
       
  7222         // stacking might be wrong
       
  7223         QApplicationPrivate::enterModal(q);
       
  7224 
       
  7225 
       
  7226     show_sys();
       
  7227 
       
  7228 #if !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN)
       
  7229     if (!isEmbedded && q->windowType() == Qt::Popup)
       
  7230         qApp->d_func()->openPopup(q);
       
  7231 #endif
       
  7232 
       
  7233 #ifndef QT_NO_ACCESSIBILITY
       
  7234     if (q->windowType() != Qt::ToolTip)     // Tooltips are read aloud twice in MS narrator.
       
  7235         QAccessible::updateAccessibility(q, 0, QAccessible::ObjectShow);
       
  7236 #endif
       
  7237 
       
  7238     if (QApplicationPrivate::hidden_focus_widget == q) {
       
  7239         QApplicationPrivate::hidden_focus_widget = 0;
       
  7240         q->setFocus(Qt::OtherFocusReason);
       
  7241     }
       
  7242 
       
  7243     // Process events when showing a Qt::SplashScreen widget before the event loop
       
  7244     // is spinnning; otherwise it might not show up on particular platforms.
       
  7245     // This makes QSplashScreen behave the same on all platforms.
       
  7246     if (!qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen)
       
  7247         QApplication::processEvents();
       
  7248 
       
  7249     data.in_show = false;  // reset qws optimization
       
  7250 }
       
  7251 
       
  7252 /*! \fn void QWidget::hide()
       
  7253 
       
  7254     Hides the widget. This function is equivalent to
       
  7255     setVisible(false).
       
  7256 
       
  7257 
       
  7258     \note If you are working with QDialog or its subclasses and you invoke
       
  7259     the show() function after this function, the dialog will be displayed in
       
  7260     its original position.
       
  7261 
       
  7262     \sa hideEvent(), isHidden(), show(), setVisible(), isVisible(), close()
       
  7263 */
       
  7264 
       
  7265 /*!\internal
       
  7266  */
       
  7267 void QWidgetPrivate::hide_helper()
       
  7268 {
       
  7269     Q_Q(QWidget);
       
  7270 
       
  7271     bool isEmbedded = false;
       
  7272 #if !defined QT_NO_GRAPHICSVIEW
       
  7273     isEmbedded = q->isWindow() && nearestGraphicsProxyWidget(q->parentWidget()) != 0;
       
  7274 #else
       
  7275     Q_UNUSED(isEmbedded);
       
  7276 #endif
       
  7277 
       
  7278     if (!isEmbedded && (q->windowType() == Qt::Popup))
       
  7279         qApp->d_func()->closePopup(q);
       
  7280 
       
  7281     // Move test modal here.  Otherwise, a modal dialog could get
       
  7282     // destroyed and we lose all access to its parent because we haven't
       
  7283     // left modality.  (Eg. modal Progress Dialog)
       
  7284     if (!isEmbedded && q->isModal() && q->isWindow())
       
  7285         QApplicationPrivate::leaveModal(q);
       
  7286 
       
  7287 #if defined(Q_WS_WIN)
       
  7288     if (q->isWindow() && !(q->windowType() == Qt::Popup) && q->parentWidget()
       
  7289         && !q->parentWidget()->isHidden() && q->isActiveWindow())
       
  7290         q->parentWidget()->activateWindow();        // Activate parent
       
  7291 #endif
       
  7292 
       
  7293     q->setAttribute(Qt::WA_Mapped, false);
       
  7294     hide_sys();
       
  7295 
       
  7296     bool wasVisible = q->testAttribute(Qt::WA_WState_Visible);
       
  7297 
       
  7298     if (wasVisible) {
       
  7299         q->setAttribute(Qt::WA_WState_Visible, false);
       
  7300 
       
  7301     }
       
  7302 
       
  7303     QHideEvent hideEvent;
       
  7304     QApplication::sendEvent(q, &hideEvent);
       
  7305     hideChildren(false);
       
  7306 
       
  7307     // next bit tries to move the focus if the focus widget is now
       
  7308     // hidden.
       
  7309     if (wasVisible) {
       
  7310 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined (Q_WS_QWS)
       
  7311         qApp->d_func()->sendSyntheticEnterLeave(q);
       
  7312 #endif
       
  7313 
       
  7314         QWidget *fw = QApplication::focusWidget();
       
  7315         while (fw &&  !fw->isWindow()) {
       
  7316             if (fw == q) {
       
  7317                 q->focusNextPrevChild(true);
       
  7318                 break;
       
  7319             }
       
  7320             fw = fw->parentWidget();
       
  7321         }
       
  7322     }
       
  7323 
       
  7324     if (QWidgetBackingStore *bs = maybeBackingStore())
       
  7325         bs->removeDirtyWidget(q);
       
  7326 
       
  7327 #ifndef QT_NO_ACCESSIBILITY
       
  7328     if (wasVisible)
       
  7329         QAccessible::updateAccessibility(q, 0, QAccessible::ObjectHide);
       
  7330 #endif
       
  7331 }
       
  7332 
       
  7333 /*!
       
  7334     \fn bool QWidget::isHidden() const
       
  7335 
       
  7336     Returns true if the widget is hidden, otherwise returns false.
       
  7337 
       
  7338     A hidden widget will only become visible when show() is called on
       
  7339     it. It will not be automatically shown when the parent is shown.
       
  7340 
       
  7341     To check visiblity, use !isVisible() instead (notice the exclamation mark).
       
  7342 
       
  7343     isHidden() implies !isVisible(), but a widget can be not visible
       
  7344     and not hidden at the same time. This is the case for widgets that are children of
       
  7345     widgets that are not visible.
       
  7346 
       
  7347 
       
  7348     Widgets are hidden if:
       
  7349     \list
       
  7350         \o they were created as independent windows,
       
  7351         \o they were created as children of visible widgets,
       
  7352         \o hide() or setVisible(false) was called.
       
  7353     \endlist
       
  7354 */
       
  7355 
       
  7356 
       
  7357 void QWidget::setVisible(bool visible)
       
  7358 {
       
  7359     if (visible) { // show
       
  7360         if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden))
       
  7361             return;
       
  7362 
       
  7363         Q_D(QWidget);
       
  7364 
       
  7365         // Designer uses a trick to make grabWidget work without showing
       
  7366         if (!isWindow() && parentWidget() && parentWidget()->isVisible()
       
  7367             && !parentWidget()->testAttribute(Qt::WA_WState_Created))
       
  7368             parentWidget()->window()->d_func()->createRecursively();
       
  7369 
       
  7370         //we have to at least create toplevels before applyX11SpecificCommandLineArguments
       
  7371         //but not children of non-visible parents
       
  7372         QWidget *pw = parentWidget();
       
  7373         if (!testAttribute(Qt::WA_WState_Created)
       
  7374             && (isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
       
  7375             create();
       
  7376         }
       
  7377 
       
  7378 #if defined(Q_WS_X11)
       
  7379         if (windowType() == Qt::Window)
       
  7380             QApplicationPrivate::applyX11SpecificCommandLineArguments(this);
       
  7381 #elif defined(Q_WS_QWS)
       
  7382         if (windowType() == Qt::Window)
       
  7383             QApplicationPrivate::applyQWSSpecificCommandLineArguments(this);
       
  7384 #endif
       
  7385 
       
  7386         bool wasResized = testAttribute(Qt::WA_Resized);
       
  7387         Qt::WindowStates initialWindowState = windowState();
       
  7388 
       
  7389         // polish if necessary
       
  7390         ensurePolished();
       
  7391 
       
  7392         // remember that show was called explicitly
       
  7393         setAttribute(Qt::WA_WState_ExplicitShowHide);
       
  7394         // whether we need to inform the parent widget immediately
       
  7395         bool needUpdateGeometry = !isWindow() && testAttribute(Qt::WA_WState_Hidden);
       
  7396         // we are no longer hidden
       
  7397         setAttribute(Qt::WA_WState_Hidden, false);
       
  7398 
       
  7399         if (needUpdateGeometry)
       
  7400             d->updateGeometry_helper(true);
       
  7401 
       
  7402 #ifdef QT3_SUPPORT
       
  7403         QApplication::sendPostedEvents(this, QEvent::ChildInserted);
       
  7404 #endif
       
  7405         // activate our layout before we and our children become visible
       
  7406         if (d->layout)
       
  7407             d->layout->activate();
       
  7408 
       
  7409         if (!isWindow()) {
       
  7410             QWidget *parent = parentWidget();
       
  7411             while (parent && parent->isVisible() && parent->d_func()->layout  && !parent->data->in_show) {
       
  7412                 parent->d_func()->layout->activate();
       
  7413                 if (parent->isWindow())
       
  7414                     break;
       
  7415                 parent = parent->parentWidget();
       
  7416             }
       
  7417             if (parent)
       
  7418                 parent->d_func()->setDirtyOpaqueRegion();
       
  7419         }
       
  7420 
       
  7421         // adjust size if necessary
       
  7422         if (!wasResized
       
  7423             && (isWindow() || !parentWidget()->d_func()->layout))  {
       
  7424             if (isWindow()) {
       
  7425                 adjustSize();
       
  7426                 if (windowState() != initialWindowState)
       
  7427                     setWindowState(initialWindowState);
       
  7428             } else {
       
  7429                 adjustSize();
       
  7430             }
       
  7431             setAttribute(Qt::WA_Resized, false);
       
  7432         }
       
  7433 
       
  7434         setAttribute(Qt::WA_KeyboardFocusChange, false);
       
  7435 
       
  7436         if (isWindow() || parentWidget()->isVisible()) {
       
  7437             // remove posted quit events when showing a new window
       
  7438             QCoreApplication::removePostedEvents(qApp, QEvent::Quit);
       
  7439 
       
  7440             d->show_helper();
       
  7441 
       
  7442 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined (Q_WS_QWS)
       
  7443             qApp->d_func()->sendSyntheticEnterLeave(this);
       
  7444 #endif
       
  7445         }
       
  7446 
       
  7447         QEvent showToParentEvent(QEvent::ShowToParent);
       
  7448         QApplication::sendEvent(this, &showToParentEvent);
       
  7449     } else { // hide
       
  7450         if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden))
       
  7451             return;
       
  7452 #if defined(Q_WS_WIN)
       
  7453         // reset WS_DISABLED style in a Blocked window
       
  7454         if(isWindow() && testAttribute(Qt::WA_WState_Created)
       
  7455            && QApplicationPrivate::isBlockedByModal(this))
       
  7456         {
       
  7457             LONG dwStyle = GetWindowLong(winId(), GWL_STYLE);
       
  7458             dwStyle &= ~WS_DISABLED;
       
  7459             SetWindowLong(winId(), GWL_STYLE, dwStyle);
       
  7460         }
       
  7461 #endif
       
  7462         if (QApplicationPrivate::hidden_focus_widget == this)
       
  7463             QApplicationPrivate::hidden_focus_widget = 0;
       
  7464 
       
  7465         Q_D(QWidget);
       
  7466 
       
  7467         // hw: The test on getOpaqueRegion() needs to be more intelligent
       
  7468         // currently it doesn't work if the widget is hidden (the region will
       
  7469         // be clipped). The real check should be testing the cached region
       
  7470         // (and dirty flag) directly.
       
  7471         if (!isWindow() && parentWidget()) // && !d->getOpaqueRegion().isEmpty())
       
  7472             parentWidget()->d_func()->setDirtyOpaqueRegion();
       
  7473 
       
  7474         setAttribute(Qt::WA_WState_Hidden);
       
  7475         setAttribute(Qt::WA_WState_ExplicitShowHide);
       
  7476         if (testAttribute(Qt::WA_WState_Created))
       
  7477             d->hide_helper();
       
  7478 
       
  7479         // invalidate layout similar to updateGeometry()
       
  7480         if (!isWindow() && parentWidget()) {
       
  7481             if (parentWidget()->d_func()->layout)
       
  7482                 parentWidget()->d_func()->layout->invalidate();
       
  7483             else if (parentWidget()->isVisible())
       
  7484                 QApplication::postEvent(parentWidget(), new QEvent(QEvent::LayoutRequest));
       
  7485         }
       
  7486 
       
  7487         QEvent hideToParentEvent(QEvent::HideToParent);
       
  7488         QApplication::sendEvent(this, &hideToParentEvent);
       
  7489     }
       
  7490 }
       
  7491 
       
  7492 /*!\fn void QWidget::setHidden(bool hidden)
       
  7493 
       
  7494     Convenience function, equivalent to setVisible(!\a hidden).
       
  7495 */
       
  7496 
       
  7497 /*!\fn void QWidget::setShown(bool shown)
       
  7498 
       
  7499     Use setVisible(\a shown) instead.
       
  7500 */
       
  7501 
       
  7502 
       
  7503 void QWidgetPrivate::_q_showIfNotHidden()
       
  7504 {
       
  7505     Q_Q(QWidget);
       
  7506     if ( !(q->isHidden() && q->testAttribute(Qt::WA_WState_ExplicitShowHide)) )
       
  7507         q->setVisible(true);
       
  7508 }
       
  7509 
       
  7510 void QWidgetPrivate::showChildren(bool spontaneous)
       
  7511 {
       
  7512     QList<QObject*> childList = children;
       
  7513     for (int i = 0; i < childList.size(); ++i) {
       
  7514         QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
       
  7515         if (!widget
       
  7516             || widget->isWindow()
       
  7517             || widget->testAttribute(Qt::WA_WState_Hidden))
       
  7518             continue;
       
  7519         if (spontaneous) {
       
  7520             widget->setAttribute(Qt::WA_Mapped);
       
  7521             widget->d_func()->showChildren(true);
       
  7522             QShowEvent e;
       
  7523             QApplication::sendSpontaneousEvent(widget, &e);
       
  7524         } else {
       
  7525             if (widget->testAttribute(Qt::WA_WState_ExplicitShowHide))
       
  7526                 widget->d_func()->show_recursive();
       
  7527             else
       
  7528                 widget->show();
       
  7529         }
       
  7530     }
       
  7531 }
       
  7532 
       
  7533 void QWidgetPrivate::hideChildren(bool spontaneous)
       
  7534 {
       
  7535     QList<QObject*> childList = children;
       
  7536     for (int i = 0; i < childList.size(); ++i) {
       
  7537         QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
       
  7538         if (!widget || widget->isWindow() || widget->testAttribute(Qt::WA_WState_Hidden))
       
  7539             continue;
       
  7540 #ifdef QT_MAC_USE_COCOA
       
  7541         // Before doing anything we need to make sure that we don't leave anything in a non-consistent state.
       
  7542         // When hiding a widget we need to make sure that no mouse_down events are active, because
       
  7543         // the mouse_up event will never be received by a hidden widget or one of its descendants.
       
  7544         // The solution is simple, before going through with this we check if there are any mouse_down events in
       
  7545         // progress, if so we check if it is related to this widget or not. If so, we just reset the mouse_down and
       
  7546         // then we continue.
       
  7547         // In X11 and Windows we send a mouse_release event, however we don't do that here because we were already
       
  7548         // ignoring that from before. I.e. Carbon did not send the mouse release event, so we will not send the
       
  7549         // mouse release event. There are two ways to interpret this:
       
  7550         // 1. If we don't send the mouse release event, the widget might get into an inconsistent state, i.e. it
       
  7551         // might be waiting for a release event that will never arrive.
       
  7552         // 2. If we send the mouse release event, then the widget might decide to trigger an action that is not
       
  7553         // supposed to trigger because it is not visible.
       
  7554         if(widget == qt_button_down)
       
  7555             qt_button_down = 0;
       
  7556 #endif // QT_MAC_USE_COCOA
       
  7557         if (spontaneous)
       
  7558             widget->setAttribute(Qt::WA_Mapped, false);
       
  7559         else
       
  7560             widget->setAttribute(Qt::WA_WState_Visible, false);
       
  7561         widget->d_func()->hideChildren(spontaneous);
       
  7562         QHideEvent e;
       
  7563         if (spontaneous) {
       
  7564             QApplication::sendSpontaneousEvent(widget, &e);
       
  7565         } else {
       
  7566             QApplication::sendEvent(widget, &e);
       
  7567             if (widget->internalWinId()
       
  7568                 && widget->testAttribute(Qt::WA_DontCreateNativeAncestors)) {
       
  7569                 // hide_sys() on an ancestor won't have any affect on this
       
  7570                 // widget, so it needs an explicit hide_sys() of its own
       
  7571                 widget->d_func()->hide_sys();
       
  7572             }
       
  7573         }
       
  7574 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined (Q_WS_QWS)
       
  7575         qApp->d_func()->sendSyntheticEnterLeave(widget);
       
  7576 #endif
       
  7577 #ifndef QT_NO_ACCESSIBILITY
       
  7578         if (!spontaneous)
       
  7579             QAccessible::updateAccessibility(widget, 0, QAccessible::ObjectHide);
       
  7580 #endif
       
  7581     }
       
  7582 }
       
  7583 
       
  7584 bool QWidgetPrivate::close_helper(CloseMode mode)
       
  7585 {
       
  7586     if (data.is_closing)
       
  7587         return true;
       
  7588 
       
  7589     Q_Q(QWidget);
       
  7590     data.is_closing = 1;
       
  7591 
       
  7592     QPointer<QWidget> that = q;
       
  7593     QPointer<QWidget> parentWidget = q->parentWidget();
       
  7594 
       
  7595 #ifdef QT3_SUPPORT
       
  7596     bool isMain = (QApplicationPrivate::main_widget == q);
       
  7597 #endif
       
  7598     bool quitOnClose = q->testAttribute(Qt::WA_QuitOnClose);
       
  7599     if (mode != CloseNoEvent) {
       
  7600         QCloseEvent e;
       
  7601         if (mode == CloseWithSpontaneousEvent)
       
  7602             QApplication::sendSpontaneousEvent(q, &e);
       
  7603         else
       
  7604             QApplication::sendEvent(q, &e);
       
  7605         if (!that.isNull() && !e.isAccepted()) {
       
  7606             data.is_closing = 0;
       
  7607             return false;
       
  7608         }
       
  7609     }
       
  7610 
       
  7611     if (!that.isNull() && !q->isHidden())
       
  7612         q->hide();
       
  7613 
       
  7614 #ifdef QT3_SUPPORT
       
  7615     if (isMain)
       
  7616         QApplication::quit();
       
  7617 #endif
       
  7618     // Attempt to close the application only if this widget has the
       
  7619     // WA_QuitOnClose flag set set and has a non-visible parent
       
  7620     quitOnClose = quitOnClose && (parentWidget.isNull() || !parentWidget->isVisible() || parentWidget->testAttribute(Qt::WA_DontShowOnScreen));
       
  7621 
       
  7622     if (quitOnClose) {
       
  7623         // If there is no non-withdrawn primary window left (except
       
  7624         // the ones without QuitOnClose or with WA_DontShowOnScreen),
       
  7625         // we emit the lastWindowClosed signal
       
  7626         QWidgetList list = QApplication::topLevelWidgets();
       
  7627         bool lastWindowClosed = true;
       
  7628         for (int i = 0; i < list.size(); ++i) {
       
  7629             QWidget *w = list.at(i);
       
  7630             if ((w->isVisible() && !w->testAttribute(Qt::WA_DontShowOnScreen))
       
  7631                     && !w->parentWidget() && w->testAttribute(Qt::WA_QuitOnClose)) {
       
  7632                 lastWindowClosed = false;
       
  7633                 break;
       
  7634             }
       
  7635         }
       
  7636         if (lastWindowClosed)
       
  7637             QApplicationPrivate::emitLastWindowClosed();
       
  7638     }
       
  7639 
       
  7640     if (!that.isNull()) {
       
  7641         data.is_closing = 0;
       
  7642         if (q->testAttribute(Qt::WA_DeleteOnClose)) {
       
  7643             q->setAttribute(Qt::WA_DeleteOnClose, false);
       
  7644             q->deleteLater();
       
  7645         }
       
  7646     }
       
  7647     return true;
       
  7648 }
       
  7649 
       
  7650 
       
  7651 /*!
       
  7652     Closes this widget. Returns true if the widget was closed;
       
  7653     otherwise returns false.
       
  7654 
       
  7655     First it sends the widget a QCloseEvent. The widget is \link
       
  7656     hide() hidden\endlink if it \link QCloseEvent::accept()
       
  7657     accepts\endlink the close event. If it \link QCloseEvent::ignore()
       
  7658     ignores\endlink the event, nothing happens. The default
       
  7659     implementation of QWidget::closeEvent() accepts the close event.
       
  7660 
       
  7661     If the widget has the Qt::WA_DeleteOnClose flag, the widget
       
  7662     is also deleted. A close events is delivered to the widget no
       
  7663     matter if the widget is visible or not.
       
  7664 
       
  7665     The \l QApplication::lastWindowClosed() signal is emitted when the
       
  7666     last visible primary window (i.e. window with no parent) with the
       
  7667     Qt::WA_QuitOnClose attribute set is closed. By default this
       
  7668     attribute is set for all widgets except transient windows such as
       
  7669     splash screens, tool windows, and popup menus.
       
  7670 
       
  7671 */
       
  7672 
       
  7673 bool QWidget::close()
       
  7674 {
       
  7675     return d_func()->close_helper(QWidgetPrivate::CloseWithEvent);
       
  7676 }
       
  7677 
       
  7678 /*!
       
  7679     \property QWidget::visible
       
  7680     \brief whether the widget is visible
       
  7681 
       
  7682     Calling setVisible(true) or show() sets the widget to visible
       
  7683     status if all its parent widgets up to the window are visible. If
       
  7684     an ancestor is not visible, the widget won't become visible until
       
  7685     all its ancestors are shown. If its size or position has changed,
       
  7686     Qt guarantees that a widget gets move and resize events just
       
  7687     before it is shown. If the widget has not been resized yet, Qt
       
  7688     will adjust the widget's size to a useful default using
       
  7689     adjustSize().
       
  7690 
       
  7691     Calling setVisible(false) or hide() hides a widget explicitly. An
       
  7692     explicitly hidden widget will never become visible, even if all
       
  7693     its ancestors become visible, unless you show it.
       
  7694 
       
  7695     A widget receives show and hide events when its visibility status
       
  7696     changes. Between a hide and a show event, there is no need to
       
  7697     waste CPU cycles preparing or displaying information to the user.
       
  7698     A video application, for example, might simply stop generating new
       
  7699     frames.
       
  7700 
       
  7701     A widget that happens to be obscured by other windows on the
       
  7702     screen is considered to be visible. The same applies to iconified
       
  7703     windows and windows that exist on another virtual
       
  7704     desktop (on platforms that support this concept). A widget
       
  7705     receives spontaneous show and hide events when its mapping status
       
  7706     is changed by the window system, e.g. a spontaneous hide event
       
  7707     when the user minimizes the window, and a spontaneous show event
       
  7708     when the window is restored again.
       
  7709 
       
  7710     You almost never have to reimplement the setVisible() function. If
       
  7711     you need to change some settings before a widget is shown, use
       
  7712     showEvent() instead. If you need to do some delayed initialization
       
  7713     use the Polish event delivered to the event() function.
       
  7714 
       
  7715     \sa show(), hide(), isHidden(), isVisibleTo(), isMinimized(),
       
  7716     showEvent(), hideEvent()
       
  7717 */
       
  7718 
       
  7719 
       
  7720 /*!
       
  7721     Returns true if this widget would become visible if \a ancestor is
       
  7722     shown; otherwise returns false.
       
  7723 
       
  7724     The true case occurs if neither the widget itself nor any parent
       
  7725     up to but excluding \a ancestor has been explicitly hidden.
       
  7726 
       
  7727     This function will still return true if the widget is obscured by
       
  7728     other windows on the screen, but could be physically visible if it
       
  7729     or they were to be moved.
       
  7730 
       
  7731     isVisibleTo(0) is identical to isVisible().
       
  7732 
       
  7733     \sa show() hide() isVisible()
       
  7734 */
       
  7735 
       
  7736 bool QWidget::isVisibleTo(QWidget* ancestor) const
       
  7737 {
       
  7738     if (!ancestor)
       
  7739         return isVisible();
       
  7740     const QWidget * w = this;
       
  7741     while (!w->isHidden()
       
  7742             && !w->isWindow()
       
  7743             && w->parentWidget()
       
  7744             && w->parentWidget() != ancestor)
       
  7745         w = w->parentWidget();
       
  7746     return !w->isHidden();
       
  7747 }
       
  7748 
       
  7749 #ifdef QT3_SUPPORT
       
  7750 /*!
       
  7751     Use visibleRegion() instead.
       
  7752 */
       
  7753 QRect QWidget::visibleRect() const
       
  7754 {
       
  7755     return d_func()->clipRect();
       
  7756 }
       
  7757 #endif
       
  7758 
       
  7759 /*!
       
  7760     Returns the unobscured region where paint events can occur.
       
  7761 
       
  7762     For visible widgets, this is an approximation of the area not
       
  7763     covered by other widgets; otherwise, this is an empty region.
       
  7764 
       
  7765     The repaint() function calls this function if necessary, so in
       
  7766     general you do not need to call it.
       
  7767 
       
  7768 */
       
  7769 QRegion QWidget::visibleRegion() const
       
  7770 {
       
  7771     Q_D(const QWidget);
       
  7772 
       
  7773     QRect clipRect = d->clipRect();
       
  7774     if (clipRect.isEmpty())
       
  7775         return QRegion();
       
  7776     QRegion r(clipRect);
       
  7777     d->subtractOpaqueChildren(r, clipRect);
       
  7778     d->subtractOpaqueSiblings(r);
       
  7779 #ifdef Q_WS_QWS
       
  7780     const QWSWindowSurface *surface = static_cast<const QWSWindowSurface*>(windowSurface());
       
  7781     if (surface) {
       
  7782         const QPoint offset = mapTo(surface->window(), QPoint());
       
  7783         r &= surface->clipRegion().translated(-offset);
       
  7784     }
       
  7785 #endif
       
  7786     return r;
       
  7787 }
       
  7788 
       
  7789 
       
  7790 QSize QWidgetPrivate::adjustedSize() const
       
  7791 {
       
  7792     Q_Q(const QWidget);
       
  7793 
       
  7794     QSize s = q->sizeHint();
       
  7795 
       
  7796     if (q->isWindow()) {
       
  7797         Qt::Orientations exp;
       
  7798         if (layout) {
       
  7799             if (layout->hasHeightForWidth())
       
  7800                 s.setHeight(layout->totalHeightForWidth(s.width()));
       
  7801             exp = layout->expandingDirections();
       
  7802         } else
       
  7803         {
       
  7804             if (q->sizePolicy().hasHeightForWidth())
       
  7805                 s.setHeight(q->heightForWidth(s.width()));
       
  7806             exp = q->sizePolicy().expandingDirections();
       
  7807         }
       
  7808         if (exp & Qt::Horizontal)
       
  7809             s.setWidth(qMax(s.width(), 200));
       
  7810         if (exp & Qt::Vertical)
       
  7811             s.setHeight(qMax(s.height(), 100));
       
  7812 #if defined(Q_WS_X11)
       
  7813         QRect screen = QApplication::desktop()->screenGeometry(q->x11Info().screen());
       
  7814 #else // all others
       
  7815         QRect screen = QApplication::desktop()->screenGeometry(q->pos());
       
  7816 #endif
       
  7817 #if defined (Q_WS_WINCE) || defined (Q_OS_SYMBIAN)
       
  7818         s.setWidth(qMin(s.width(), screen.width()));
       
  7819         s.setHeight(qMin(s.height(), screen.height()));
       
  7820 #else
       
  7821         s.setWidth(qMin(s.width(), screen.width()*2/3));
       
  7822         s.setHeight(qMin(s.height(), screen.height()*2/3));
       
  7823 #endif
       
  7824         if (QTLWExtra *extra = maybeTopData())
       
  7825             extra->sizeAdjusted = true;
       
  7826     }
       
  7827 
       
  7828     if (!s.isValid()) {
       
  7829         QRect r = q->childrenRect(); // get children rectangle
       
  7830         if (r.isNull())
       
  7831             return s;
       
  7832         s = r.size() + QSize(2 * r.x(), 2 * r.y());
       
  7833     }
       
  7834 
       
  7835     return s;
       
  7836 }
       
  7837 
       
  7838 /*!
       
  7839     Adjusts the size of the widget to fit its contents.
       
  7840 
       
  7841     This function uses sizeHint() if it is valid, i.e., the size hint's width
       
  7842     and height are \>= 0. Otherwise, it sets the size to the children
       
  7843     rectangle that covers all child widgets (the union of all child widget
       
  7844     rectangles).
       
  7845 
       
  7846     For windows, the screen size is also taken into account. If the sizeHint()
       
  7847     is less than (200, 100) and the size policy is \l{QSizePolicy::Expanding}
       
  7848     {expanding}, the window will be at least (200, 100). The maximum size of
       
  7849     a window is 2/3 of the screen's width and height.
       
  7850 
       
  7851     \sa sizeHint(), childrenRect()
       
  7852 */
       
  7853 
       
  7854 void QWidget::adjustSize()
       
  7855 {
       
  7856     Q_D(QWidget);
       
  7857     ensurePolished();
       
  7858     QSize s = d->adjustedSize();
       
  7859 
       
  7860     if (d->layout)
       
  7861         d->layout->activate();
       
  7862 
       
  7863     if (s.isValid())
       
  7864         resize(s);
       
  7865 }
       
  7866 
       
  7867 
       
  7868 /*!
       
  7869     \property QWidget::sizeHint
       
  7870     \brief the recommended size for the widget
       
  7871 
       
  7872     If the value of this property is an invalid size, no size is
       
  7873     recommended.
       
  7874 
       
  7875     The default implementation of sizeHint() returns an invalid size
       
  7876     if there is no layout for this widget, and returns the layout's
       
  7877     preferred size otherwise.
       
  7878 
       
  7879     \sa QSize::isValid(), minimumSizeHint(), sizePolicy(),
       
  7880     setMinimumSize(), updateGeometry()
       
  7881 */
       
  7882 
       
  7883 QSize QWidget::sizeHint() const
       
  7884 {
       
  7885     Q_D(const QWidget);
       
  7886     if (d->layout)
       
  7887         return d->layout->totalSizeHint();
       
  7888     return QSize(-1, -1);
       
  7889 }
       
  7890 
       
  7891 /*!
       
  7892     \property QWidget::minimumSizeHint
       
  7893     \brief the recommended minimum size for the widget
       
  7894 
       
  7895     If the value of this property is an invalid size, no minimum size
       
  7896     is recommended.
       
  7897 
       
  7898     The default implementation of minimumSizeHint() returns an invalid
       
  7899     size if there is no layout for this widget, and returns the
       
  7900     layout's minimum size otherwise. Most built-in widgets reimplement
       
  7901     minimumSizeHint().
       
  7902 
       
  7903     \l QLayout will never resize a widget to a size smaller than the
       
  7904     minimum size hint unless minimumSize() is set or the size policy is
       
  7905     set to QSizePolicy::Ignore. If minimumSize() is set, the minimum
       
  7906     size hint will be ignored.
       
  7907 
       
  7908     \sa QSize::isValid(), resize(), setMinimumSize(), sizePolicy()
       
  7909 */
       
  7910 QSize QWidget::minimumSizeHint() const
       
  7911 {
       
  7912     Q_D(const QWidget);
       
  7913     if (d->layout)
       
  7914         return d->layout->totalMinimumSize();
       
  7915     return QSize(-1, -1);
       
  7916 }
       
  7917 
       
  7918 
       
  7919 /*!
       
  7920     \fn QWidget *QWidget::parentWidget() const
       
  7921 
       
  7922     Returns the parent of this widget, or 0 if it does not have any
       
  7923     parent widget.
       
  7924 */
       
  7925 
       
  7926 
       
  7927 /*!
       
  7928     Returns true if this widget is a parent, (or grandparent and so on
       
  7929     to any level), of the given \a child, and both widgets are within
       
  7930     the same window; otherwise returns false.
       
  7931 */
       
  7932 
       
  7933 bool QWidget::isAncestorOf(const QWidget *child) const
       
  7934 {
       
  7935     while (child) {
       
  7936         if (child == this)
       
  7937             return true;
       
  7938         if (child->isWindow())
       
  7939             return false;
       
  7940         child = child->parentWidget();
       
  7941     }
       
  7942     return false;
       
  7943 }
       
  7944 
       
  7945 #if defined(Q_WS_WIN)
       
  7946 inline void setDisabledStyle(QWidget *w, bool setStyle)
       
  7947 {
       
  7948     // set/reset WS_DISABLED style.
       
  7949     if(w && w->isWindow() && w->isVisible() && w->isEnabled()) {
       
  7950         LONG dwStyle = GetWindowLong(w->winId(), GWL_STYLE);
       
  7951         LONG newStyle = dwStyle;
       
  7952         if (setStyle)
       
  7953             newStyle |= WS_DISABLED;
       
  7954         else
       
  7955             newStyle &= ~WS_DISABLED;
       
  7956         if (newStyle != dwStyle) {
       
  7957             SetWindowLong(w->winId(), GWL_STYLE, newStyle);
       
  7958             // we might need to repaint in some situations (eg. menu)
       
  7959             w->repaint();
       
  7960         }
       
  7961     }
       
  7962 }
       
  7963 #endif
       
  7964 
       
  7965 /*****************************************************************************
       
  7966   QWidget event handling
       
  7967  *****************************************************************************/
       
  7968 
       
  7969 /*!
       
  7970     This is the main event handler; it handles event \a event. You can
       
  7971     reimplement this function in a subclass, but we recommend using
       
  7972     one of the specialized event handlers instead.
       
  7973 
       
  7974     Key press and release events are treated differently from other
       
  7975     events. event() checks for Tab and Shift+Tab and tries to move the
       
  7976     focus appropriately. If there is no widget to move the focus to
       
  7977     (or the key press is not Tab or Shift+Tab), event() calls
       
  7978     keyPressEvent().
       
  7979 
       
  7980     Mouse and tablet event handling is also slightly special: only
       
  7981     when the widget is \l enabled, event() will call the specialized
       
  7982     handlers such as mousePressEvent(); otherwise it will discard the
       
  7983     event.
       
  7984 
       
  7985     This function returns true if the event was recognized, otherwise
       
  7986     it returns false.  If the recognized event was accepted (see \l
       
  7987     QEvent::accepted), any further processing such as event
       
  7988     propagation to the parent widget stops.
       
  7989 
       
  7990     \sa closeEvent(), focusInEvent(), focusOutEvent(), enterEvent(),
       
  7991     keyPressEvent(), keyReleaseEvent(), leaveEvent(),
       
  7992     mouseDoubleClickEvent(), mouseMoveEvent(), mousePressEvent(),
       
  7993     mouseReleaseEvent(), moveEvent(), paintEvent(), resizeEvent(),
       
  7994     QObject::event(), QObject::timerEvent()
       
  7995 */
       
  7996 
       
  7997 bool QWidget::event(QEvent *event)
       
  7998 {
       
  7999     Q_D(QWidget);
       
  8000 
       
  8001     // ignore mouse events when disabled
       
  8002     if (!isEnabled()) {
       
  8003         switch(event->type()) {
       
  8004         case QEvent::TabletPress:
       
  8005         case QEvent::TabletRelease:
       
  8006         case QEvent::TabletMove:
       
  8007         case QEvent::MouseButtonPress:
       
  8008         case QEvent::MouseButtonRelease:
       
  8009         case QEvent::MouseButtonDblClick:
       
  8010         case QEvent::MouseMove:
       
  8011         case QEvent::TouchBegin:
       
  8012         case QEvent::TouchUpdate:
       
  8013         case QEvent::TouchEnd:
       
  8014         case QEvent::ContextMenu:
       
  8015 #ifndef QT_NO_WHEELEVENT
       
  8016         case QEvent::Wheel:
       
  8017 #endif
       
  8018             return false;
       
  8019         default:
       
  8020             break;
       
  8021         }
       
  8022     }
       
  8023     switch (event->type()) {
       
  8024     case QEvent::MouseMove:
       
  8025         mouseMoveEvent((QMouseEvent*)event);
       
  8026         break;
       
  8027 
       
  8028     case QEvent::MouseButtonPress:
       
  8029         // Don't reset input context here. Whether reset or not is
       
  8030         // a responsibility of input method. reset() will be
       
  8031         // called by mouseHandler() of input method if necessary
       
  8032         // via mousePressEvent() of text widgets.
       
  8033 #if 0
       
  8034         resetInputContext();
       
  8035 #endif
       
  8036         mousePressEvent((QMouseEvent*)event);
       
  8037         break;
       
  8038 
       
  8039     case QEvent::MouseButtonRelease:
       
  8040         mouseReleaseEvent((QMouseEvent*)event);
       
  8041         break;
       
  8042 
       
  8043     case QEvent::MouseButtonDblClick:
       
  8044         mouseDoubleClickEvent((QMouseEvent*)event);
       
  8045         break;
       
  8046 #ifndef QT_NO_WHEELEVENT
       
  8047     case QEvent::Wheel:
       
  8048         wheelEvent((QWheelEvent*)event);
       
  8049         break;
       
  8050 #endif
       
  8051 #ifndef QT_NO_TABLETEVENT
       
  8052     case QEvent::TabletMove:
       
  8053     case QEvent::TabletPress:
       
  8054     case QEvent::TabletRelease:
       
  8055         tabletEvent((QTabletEvent*)event);
       
  8056         break;
       
  8057 #endif
       
  8058 #ifdef QT3_SUPPORT
       
  8059     case QEvent::Accel:
       
  8060         event->ignore();
       
  8061         return false;
       
  8062 #endif
       
  8063     case QEvent::KeyPress: {
       
  8064         QKeyEvent *k = (QKeyEvent *)event;
       
  8065         bool res = false;
       
  8066         if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {  //### Add MetaModifier?
       
  8067             if (k->key() == Qt::Key_Backtab
       
  8068                 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier)))
       
  8069                 res = focusNextPrevChild(false);
       
  8070             else if (k->key() == Qt::Key_Tab)
       
  8071                 res = focusNextPrevChild(true);
       
  8072             if (res)
       
  8073                 break;
       
  8074         }
       
  8075         keyPressEvent(k);
       
  8076 #ifdef QT_KEYPAD_NAVIGATION
       
  8077         if (!k->isAccepted() && QApplication::keypadNavigationEnabled()
       
  8078             && !(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::ShiftModifier))) {
       
  8079             if (QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder) {
       
  8080                 if (k->key() == Qt::Key_Up)
       
  8081                     res = focusNextPrevChild(false);
       
  8082                 else if (k->key() == Qt::Key_Down)
       
  8083                     res = focusNextPrevChild(true);
       
  8084             } else if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
       
  8085                 if (k->key() == Qt::Key_Up)
       
  8086                     res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionNorth);
       
  8087                 else if (k->key() == Qt::Key_Right)
       
  8088                     res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionEast);
       
  8089                 else if (k->key() == Qt::Key_Down)
       
  8090                     res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionSouth);
       
  8091                 else if (k->key() == Qt::Key_Left)
       
  8092                     res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionWest);
       
  8093             }
       
  8094             if (res) {
       
  8095                 k->accept();
       
  8096                 break;
       
  8097             }
       
  8098         }
       
  8099 #endif
       
  8100 #ifndef QT_NO_WHATSTHIS
       
  8101         if (!k->isAccepted()
       
  8102             && k->modifiers() & Qt::ShiftModifier && k->key() == Qt::Key_F1
       
  8103             && d->whatsThis.size()) {
       
  8104             QWhatsThis::showText(mapToGlobal(inputMethodQuery(Qt::ImMicroFocus).toRect().center()), d->whatsThis, this);
       
  8105             k->accept();
       
  8106         }
       
  8107 #endif
       
  8108     }
       
  8109         break;
       
  8110 
       
  8111     case QEvent::KeyRelease:
       
  8112         keyReleaseEvent((QKeyEvent*)event);
       
  8113         // fall through
       
  8114     case QEvent::ShortcutOverride:
       
  8115         break;
       
  8116 
       
  8117     case QEvent::InputMethod:
       
  8118         inputMethodEvent((QInputMethodEvent *) event);
       
  8119         break;
       
  8120 
       
  8121     case QEvent::PolishRequest:
       
  8122         ensurePolished();
       
  8123         break;
       
  8124 
       
  8125     case QEvent::Polish: {
       
  8126         style()->polish(this);
       
  8127         setAttribute(Qt::WA_WState_Polished);
       
  8128         if (!QApplication::font(this).isCopyOf(QApplication::font()))
       
  8129             d->resolveFont();
       
  8130         if (!QApplication::palette(this).isCopyOf(QApplication::palette()))
       
  8131             d->resolvePalette();
       
  8132 #ifdef QT3_SUPPORT
       
  8133         if(d->sendChildEvents)
       
  8134             QApplication::sendPostedEvents(this, QEvent::ChildInserted);
       
  8135 #endif
       
  8136     }
       
  8137         break;
       
  8138 
       
  8139     case QEvent::ApplicationWindowIconChange:
       
  8140         if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon)) {
       
  8141             d->setWindowIcon_sys();
       
  8142             d->setWindowIcon_helper();
       
  8143         }
       
  8144         break;
       
  8145     case QEvent::FocusIn:
       
  8146 #ifdef QT_SOFTKEYS_ENABLED
       
  8147         QSoftKeyManager::updateSoftKeys();
       
  8148 #endif
       
  8149         focusInEvent((QFocusEvent*)event);
       
  8150         break;
       
  8151 
       
  8152     case QEvent::FocusOut:
       
  8153         focusOutEvent((QFocusEvent*)event);
       
  8154         break;
       
  8155 
       
  8156     case QEvent::Enter:
       
  8157 #ifndef QT_NO_STATUSTIP
       
  8158         if (d->statusTip.size()) {
       
  8159             QStatusTipEvent tip(d->statusTip);
       
  8160             QApplication::sendEvent(const_cast<QWidget *>(this), &tip);
       
  8161         }
       
  8162 #endif
       
  8163         enterEvent(event);
       
  8164         break;
       
  8165 
       
  8166     case QEvent::Leave:
       
  8167 #ifndef QT_NO_STATUSTIP
       
  8168         if (d->statusTip.size()) {
       
  8169             QString empty;
       
  8170             QStatusTipEvent tip(empty);
       
  8171             QApplication::sendEvent(const_cast<QWidget *>(this), &tip);
       
  8172         }
       
  8173 #endif
       
  8174         leaveEvent(event);
       
  8175         break;
       
  8176 
       
  8177     case QEvent::HoverEnter:
       
  8178     case QEvent::HoverLeave:
       
  8179         update();
       
  8180         break;
       
  8181 
       
  8182     case QEvent::Paint:
       
  8183         // At this point the event has to be delivered, regardless
       
  8184         // whether the widget isVisible() or not because it
       
  8185         // already went through the filters
       
  8186         paintEvent((QPaintEvent*)event);
       
  8187         break;
       
  8188 
       
  8189     case QEvent::Move:
       
  8190         moveEvent((QMoveEvent*)event);
       
  8191         break;
       
  8192 
       
  8193     case QEvent::Resize:
       
  8194         resizeEvent((QResizeEvent*)event);
       
  8195         break;
       
  8196 
       
  8197     case QEvent::Close:
       
  8198         closeEvent((QCloseEvent *)event);
       
  8199         break;
       
  8200 
       
  8201 #ifndef QT_NO_CONTEXTMENU
       
  8202     case QEvent::ContextMenu:
       
  8203         switch (data->context_menu_policy) {
       
  8204         case Qt::PreventContextMenu:
       
  8205             break;
       
  8206         case Qt::DefaultContextMenu:
       
  8207             contextMenuEvent(static_cast<QContextMenuEvent *>(event));
       
  8208             break;
       
  8209         case Qt::CustomContextMenu:
       
  8210             emit customContextMenuRequested(static_cast<QContextMenuEvent *>(event)->pos());
       
  8211             break;
       
  8212 #ifndef QT_NO_MENU
       
  8213         case Qt::ActionsContextMenu:
       
  8214             if (d->actions.count()) {
       
  8215                 QMenu::exec(d->actions, static_cast<QContextMenuEvent *>(event)->globalPos(),
       
  8216                             0, this);
       
  8217                 break;
       
  8218             }
       
  8219             // fall through
       
  8220 #endif
       
  8221         default:
       
  8222             event->ignore();
       
  8223             break;
       
  8224         }
       
  8225         break;
       
  8226 #endif // QT_NO_CONTEXTMENU
       
  8227 
       
  8228 #ifndef QT_NO_DRAGANDDROP
       
  8229     case QEvent::Drop:
       
  8230         dropEvent((QDropEvent*) event);
       
  8231         break;
       
  8232 
       
  8233     case QEvent::DragEnter:
       
  8234         dragEnterEvent((QDragEnterEvent*) event);
       
  8235         break;
       
  8236 
       
  8237     case QEvent::DragMove:
       
  8238         dragMoveEvent((QDragMoveEvent*) event);
       
  8239         break;
       
  8240 
       
  8241     case QEvent::DragLeave:
       
  8242         dragLeaveEvent((QDragLeaveEvent*) event);
       
  8243         break;
       
  8244 #endif
       
  8245 
       
  8246     case QEvent::Show:
       
  8247         showEvent((QShowEvent*) event);
       
  8248         break;
       
  8249 
       
  8250     case QEvent::Hide:
       
  8251         hideEvent((QHideEvent*) event);
       
  8252         break;
       
  8253 
       
  8254     case QEvent::ShowWindowRequest:
       
  8255         if (!isHidden())
       
  8256             d->show_sys();
       
  8257         break;
       
  8258 
       
  8259     case QEvent::ApplicationFontChange:
       
  8260         d->resolveFont();
       
  8261         break;
       
  8262     case QEvent::ApplicationPaletteChange:
       
  8263         if (!(windowType() == Qt::Desktop))
       
  8264             d->resolvePalette();
       
  8265         break;
       
  8266 
       
  8267     case QEvent::ToolBarChange:
       
  8268     case QEvent::ActivationChange:
       
  8269     case QEvent::EnabledChange:
       
  8270     case QEvent::FontChange:
       
  8271     case QEvent::StyleChange:
       
  8272     case QEvent::PaletteChange:
       
  8273     case QEvent::WindowTitleChange:
       
  8274     case QEvent::IconTextChange:
       
  8275     case QEvent::ModifiedChange:
       
  8276     case QEvent::MouseTrackingChange:
       
  8277     case QEvent::ParentChange:
       
  8278     case QEvent::WindowStateChange:
       
  8279     case QEvent::LocaleChange:
       
  8280     case QEvent::MacSizeChange:
       
  8281     case QEvent::ContentsRectChange:
       
  8282         changeEvent(event);
       
  8283         break;
       
  8284 
       
  8285     case QEvent::WindowActivate:
       
  8286     case QEvent::WindowDeactivate: {
       
  8287 #ifdef QT3_SUPPORT
       
  8288         windowActivationChange(event->type() != QEvent::WindowActivate);
       
  8289 #endif
       
  8290         if (isVisible() && !palette().isEqual(QPalette::Active, QPalette::Inactive))
       
  8291             update();
       
  8292         QList<QObject*> childList = d->children;
       
  8293         for (int i = 0; i < childList.size(); ++i) {
       
  8294             QWidget *w = qobject_cast<QWidget *>(childList.at(i));
       
  8295             if (w && w->isVisible() && !w->isWindow())
       
  8296                 QApplication::sendEvent(w, event);
       
  8297         }
       
  8298 
       
  8299 #ifdef QT_SOFTKEYS_ENABLED
       
  8300         if (isWindow())
       
  8301             QSoftKeyManager::updateSoftKeys();
       
  8302 #endif
       
  8303 
       
  8304         break; }
       
  8305 
       
  8306     case QEvent::LanguageChange:
       
  8307 #ifdef QT3_SUPPORT
       
  8308         languageChange();
       
  8309 #endif
       
  8310         changeEvent(event);
       
  8311         {
       
  8312             QList<QObject*> childList = d->children;
       
  8313             for (int i = 0; i < childList.size(); ++i) {
       
  8314                 QObject *o = childList.at(i);
       
  8315                 if (o)
       
  8316                     QApplication::sendEvent(o, event);
       
  8317             }
       
  8318         }
       
  8319         update();
       
  8320         break;
       
  8321 
       
  8322     case QEvent::ApplicationLayoutDirectionChange:
       
  8323         d->resolveLayoutDirection();
       
  8324         break;
       
  8325 
       
  8326     case QEvent::LayoutDirectionChange:
       
  8327         if (d->layout)
       
  8328             d->layout->invalidate();
       
  8329         update();
       
  8330         changeEvent(event);
       
  8331         break;
       
  8332     case QEvent::UpdateRequest:
       
  8333         d->syncBackingStore();
       
  8334         break;
       
  8335     case QEvent::UpdateLater:
       
  8336         update(static_cast<QUpdateLaterEvent*>(event)->region());
       
  8337         break;
       
  8338 
       
  8339     case QEvent::WindowBlocked:
       
  8340     case QEvent::WindowUnblocked:
       
  8341         {
       
  8342             QList<QObject*> childList = d->children;
       
  8343             for (int i = 0; i < childList.size(); ++i) {
       
  8344                 QObject *o = childList.at(i);
       
  8345                 if (o && o != QApplication::activeModalWidget()) {
       
  8346                     if (qobject_cast<QWidget *>(o) && static_cast<QWidget *>(o)->isWindow()) {
       
  8347                         // do not forward the event to child windows,
       
  8348                         // QApplication does this for us
       
  8349                         continue;
       
  8350                     }
       
  8351                     QApplication::sendEvent(o, event);
       
  8352                 }
       
  8353             }
       
  8354 #if defined(Q_WS_WIN)
       
  8355             setDisabledStyle(this, (event->type() == QEvent::WindowBlocked));
       
  8356 #endif
       
  8357         }
       
  8358         break;
       
  8359 #ifndef QT_NO_TOOLTIP
       
  8360     case QEvent::ToolTip:
       
  8361         if (!d->toolTip.isEmpty())
       
  8362             QToolTip::showText(static_cast<QHelpEvent*>(event)->globalPos(), d->toolTip, this);
       
  8363         else
       
  8364             event->ignore();
       
  8365         break;
       
  8366 #endif
       
  8367 #ifndef QT_NO_WHATSTHIS
       
  8368     case QEvent::WhatsThis:
       
  8369         if (d->whatsThis.size())
       
  8370             QWhatsThis::showText(static_cast<QHelpEvent *>(event)->globalPos(), d->whatsThis, this);
       
  8371         else
       
  8372             event->ignore();
       
  8373         break;
       
  8374     case QEvent::QueryWhatsThis:
       
  8375         if (d->whatsThis.isEmpty())
       
  8376             event->ignore();
       
  8377         break;
       
  8378 #endif
       
  8379 #ifndef QT_NO_ACCESSIBILITY
       
  8380     case QEvent::AccessibilityDescription:
       
  8381     case QEvent::AccessibilityHelp: {
       
  8382         QAccessibleEvent *ev = static_cast<QAccessibleEvent *>(event);
       
  8383         if (ev->child())
       
  8384             return false;
       
  8385         switch (ev->type()) {
       
  8386 #ifndef QT_NO_TOOLTIP
       
  8387         case QEvent::AccessibilityDescription:
       
  8388             ev->setValue(d->toolTip);
       
  8389             break;
       
  8390 #endif
       
  8391 #ifndef QT_NO_WHATSTHIS
       
  8392         case QEvent::AccessibilityHelp:
       
  8393             ev->setValue(d->whatsThis);
       
  8394             break;
       
  8395 #endif
       
  8396         default:
       
  8397             return false;
       
  8398         }
       
  8399         break; }
       
  8400 #endif
       
  8401     case QEvent::EmbeddingControl:
       
  8402         d->topData()->frameStrut.setCoords(0 ,0, 0, 0);
       
  8403         data->fstrut_dirty = false;
       
  8404 #if defined(Q_WS_WIN) || defined(Q_WS_X11)
       
  8405         d->topData()->embedded = 1;
       
  8406 #endif
       
  8407         break;
       
  8408 #ifndef QT_NO_ACTION
       
  8409     case QEvent::ActionAdded:
       
  8410     case QEvent::ActionRemoved:
       
  8411     case QEvent::ActionChanged:
       
  8412 #ifdef QT_SOFTKEYS_ENABLED
       
  8413         QSoftKeyManager::updateSoftKeys();
       
  8414 #endif
       
  8415         actionEvent((QActionEvent*)event);
       
  8416         break;
       
  8417 #endif
       
  8418 
       
  8419     case QEvent::KeyboardLayoutChange:
       
  8420         {
       
  8421             changeEvent(event);
       
  8422 
       
  8423             // inform children of the change
       
  8424             QList<QObject*> childList = d->children;
       
  8425             for (int i = 0; i < childList.size(); ++i) {
       
  8426                 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
       
  8427                 if (w && w->isVisible() && !w->isWindow())
       
  8428                     QApplication::sendEvent(w, event);
       
  8429             }
       
  8430             break;
       
  8431         }
       
  8432 #ifdef Q_WS_MAC
       
  8433     case QEvent::MacGLWindowChange:
       
  8434         d->needWindowChange = false;
       
  8435         break;
       
  8436 #endif
       
  8437     case QEvent::TouchBegin:
       
  8438     case QEvent::TouchUpdate:
       
  8439     case QEvent::TouchEnd:
       
  8440     {
       
  8441 #ifndef Q_WS_MAC
       
  8442         QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
       
  8443         const QTouchEvent::TouchPoint &touchPoint = touchEvent->touchPoints().first();
       
  8444         if (touchPoint.isPrimary() || touchEvent->deviceType() == QTouchEvent::TouchPad)
       
  8445             break;
       
  8446 
       
  8447         // fake a mouse event!
       
  8448         QEvent::Type eventType = QEvent::None;
       
  8449         switch (touchEvent->type()) {
       
  8450         case QEvent::TouchBegin:
       
  8451             eventType = QEvent::MouseButtonPress;
       
  8452             break;
       
  8453         case QEvent::TouchUpdate:
       
  8454             eventType = QEvent::MouseMove;
       
  8455             break;
       
  8456         case QEvent::TouchEnd:
       
  8457             eventType = QEvent::MouseButtonRelease;
       
  8458             break;
       
  8459         default:
       
  8460             Q_ASSERT(!true);
       
  8461             break;
       
  8462         }
       
  8463         if (eventType == QEvent::None)
       
  8464             break;
       
  8465 
       
  8466         QMouseEvent mouseEvent(eventType,
       
  8467                                touchPoint.pos().toPoint(),
       
  8468                                touchPoint.screenPos().toPoint(),
       
  8469                                Qt::LeftButton,
       
  8470                                Qt::LeftButton,
       
  8471                                touchEvent->modifiers());
       
  8472         (void) QApplication::sendEvent(this, &mouseEvent);
       
  8473 #endif // Q_WS_MAC
       
  8474         break;
       
  8475     }
       
  8476     case QEvent::Gesture:
       
  8477         event->ignore();
       
  8478         break;
       
  8479 #ifndef QT_NO_PROPERTIES
       
  8480     case QEvent::DynamicPropertyChange: {
       
  8481         const QByteArray &propName = static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName();
       
  8482         if (!qstrncmp(propName, "_q_customDpi", 12) && propName.length() == 13) {
       
  8483             uint value = property(propName.constData()).toUInt();
       
  8484             if (!d->extra)
       
  8485                 d->createExtra();
       
  8486             const char axis = propName.at(12);
       
  8487             if (axis == 'X')
       
  8488                 d->extra->customDpiX = value;
       
  8489             else if (axis == 'Y')
       
  8490                 d->extra->customDpiY = value;
       
  8491             d->updateFont(d->data.fnt);
       
  8492         }
       
  8493         // fall through
       
  8494     }
       
  8495 #endif
       
  8496     default:
       
  8497         return QObject::event(event);
       
  8498     }
       
  8499     return true;
       
  8500 }
       
  8501 
       
  8502 /*!
       
  8503   This event handler can be reimplemented to handle state changes.
       
  8504 
       
  8505   The state being changed in this event can be retrieved through event \a
       
  8506   event.
       
  8507 
       
  8508   Change events include: QEvent::ToolBarChange,
       
  8509   QEvent::ActivationChange, QEvent::EnabledChange, QEvent::FontChange,
       
  8510   QEvent::StyleChange, QEvent::PaletteChange,
       
  8511   QEvent::WindowTitleChange, QEvent::IconTextChange,
       
  8512   QEvent::ModifiedChange, QEvent::MouseTrackingChange,
       
  8513   QEvent::ParentChange, QEvent::WindowStateChange,
       
  8514   QEvent::LanguageChange, QEvent::LocaleChange,
       
  8515   QEvent::LayoutDirectionChange.
       
  8516 
       
  8517 */
       
  8518 void QWidget::changeEvent(QEvent * event)
       
  8519 {
       
  8520     switch(event->type()) {
       
  8521     case QEvent::EnabledChange:
       
  8522         update();
       
  8523 #ifndef QT_NO_ACCESSIBILITY
       
  8524         QAccessible::updateAccessibility(this, 0, QAccessible::StateChanged);
       
  8525 #endif
       
  8526         break;
       
  8527 
       
  8528     case QEvent::FontChange:
       
  8529     case QEvent::StyleChange: {
       
  8530         Q_D(QWidget);
       
  8531         update();
       
  8532         updateGeometry();
       
  8533         if (d->layout)
       
  8534             d->layout->invalidate();
       
  8535 #ifdef Q_WS_QWS
       
  8536         if (isWindow())
       
  8537             d->data.fstrut_dirty = true;
       
  8538 #endif
       
  8539         break;
       
  8540     }
       
  8541 
       
  8542     case QEvent::PaletteChange:
       
  8543         update();
       
  8544         break;
       
  8545 
       
  8546 #ifdef Q_WS_MAC
       
  8547     case QEvent::MacSizeChange:
       
  8548         updateGeometry();
       
  8549         break;
       
  8550     case QEvent::ToolTipChange:
       
  8551     case QEvent::MouseTrackingChange:
       
  8552         qt_mac_update_mouseTracking(this);
       
  8553         break;
       
  8554 #endif
       
  8555 
       
  8556     default:
       
  8557         break;
       
  8558     }
       
  8559 }
       
  8560 
       
  8561 /*!
       
  8562     This event handler, for event \a event, can be reimplemented in a
       
  8563     subclass to receive mouse move events for the widget.
       
  8564 
       
  8565     If mouse tracking is switched off, mouse move events only occur if
       
  8566     a mouse button is pressed while the mouse is being moved. If mouse
       
  8567     tracking is switched on, mouse move events occur even if no mouse
       
  8568     button is pressed.
       
  8569 
       
  8570     QMouseEvent::pos() reports the position of the mouse cursor,
       
  8571     relative to this widget. For press and release events, the
       
  8572     position is usually the same as the position of the last mouse
       
  8573     move event, but it might be different if the user's hand shakes.
       
  8574     This is a feature of the underlying window system, not Qt.
       
  8575 
       
  8576     If you want to show a tooltip immediately, while the mouse is
       
  8577     moving (e.g., to get the mouse coordinates with QMouseEvent::pos()
       
  8578     and show them as a tooltip), you must first enable mouse tracking
       
  8579     as described above. Then, to ensure that the tooltip is updated
       
  8580     immediately, you must call QToolTip::showText() instead of
       
  8581     setToolTip() in your implementation of mouseMoveEvent().
       
  8582 
       
  8583     \sa setMouseTracking(), mousePressEvent(), mouseReleaseEvent(),
       
  8584     mouseDoubleClickEvent(), event(), QMouseEvent, {Scribble Example}
       
  8585 */
       
  8586 
       
  8587 void QWidget::mouseMoveEvent(QMouseEvent *event)
       
  8588 {
       
  8589     event->ignore();
       
  8590 }
       
  8591 
       
  8592 /*!
       
  8593     This event handler, for event \a event, can be reimplemented in a
       
  8594     subclass to receive mouse press events for the widget.
       
  8595 
       
  8596     If you create new widgets in the mousePressEvent() the
       
  8597     mouseReleaseEvent() may not end up where you expect, depending on
       
  8598     the underlying window system (or X11 window manager), the widgets'
       
  8599     location and maybe more.
       
  8600 
       
  8601     The default implementation implements the closing of popup widgets
       
  8602     when you click outside the window. For other widget types it does
       
  8603     nothing.
       
  8604 
       
  8605     \sa mouseReleaseEvent(), mouseDoubleClickEvent(),
       
  8606     mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}
       
  8607 */
       
  8608 
       
  8609 void QWidget::mousePressEvent(QMouseEvent *event)
       
  8610 {
       
  8611     event->ignore();
       
  8612     if ((windowType() == Qt::Popup)) {
       
  8613         event->accept();
       
  8614         QWidget* w;
       
  8615         while ((w = QApplication::activePopupWidget()) && w != this){
       
  8616             w->close();
       
  8617             if (QApplication::activePopupWidget() == w) // widget does not want to dissappear
       
  8618                 w->hide(); // hide at least
       
  8619         }
       
  8620         if (!rect().contains(event->pos())){
       
  8621             close();
       
  8622         }
       
  8623     }
       
  8624 }
       
  8625 
       
  8626 /*!
       
  8627     This event handler, for event \a event, can be reimplemented in a
       
  8628     subclass to receive mouse release events for the widget.
       
  8629 
       
  8630     \sa mousePressEvent(), mouseDoubleClickEvent(),
       
  8631     mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}
       
  8632 */
       
  8633 
       
  8634 void QWidget::mouseReleaseEvent(QMouseEvent *event)
       
  8635 {
       
  8636     event->ignore();
       
  8637 }
       
  8638 
       
  8639 /*!
       
  8640     This event handler, for event \a event, can be reimplemented in a
       
  8641     subclass to receive mouse double click events for the widget.
       
  8642 
       
  8643     The default implementation generates a normal mouse press event.
       
  8644 
       
  8645     \note The widget will also receive mouse press and mouse release
       
  8646     events in addition to the double click event. It is up to the
       
  8647     developer to ensure that the application interprets these events
       
  8648     correctly.
       
  8649 
       
  8650     \sa mousePressEvent(), mouseReleaseEvent() mouseMoveEvent(),
       
  8651     event(), QMouseEvent
       
  8652 */
       
  8653 
       
  8654 void QWidget::mouseDoubleClickEvent(QMouseEvent *event)
       
  8655 {
       
  8656     mousePressEvent(event);                        // try mouse press event
       
  8657 }
       
  8658 
       
  8659 #ifndef QT_NO_WHEELEVENT
       
  8660 /*!
       
  8661     This event handler, for event \a event, can be reimplemented in a
       
  8662     subclass to receive wheel events for the widget.
       
  8663 
       
  8664     If you reimplement this handler, it is very important that you
       
  8665     \link QWheelEvent ignore()\endlink the event if you do not handle
       
  8666     it, so that the widget's parent can interpret it.
       
  8667 
       
  8668     The default implementation ignores the event.
       
  8669 
       
  8670     \sa QWheelEvent::ignore(), QWheelEvent::accept(), event(),
       
  8671     QWheelEvent
       
  8672 */
       
  8673 
       
  8674 void QWidget::wheelEvent(QWheelEvent *event)
       
  8675 {
       
  8676     event->ignore();
       
  8677 }
       
  8678 #endif // QT_NO_WHEELEVENT
       
  8679 
       
  8680 #ifndef QT_NO_TABLETEVENT
       
  8681 /*!
       
  8682     This event handler, for event \a event, can be reimplemented in a
       
  8683     subclass to receive tablet events for the widget.
       
  8684 
       
  8685     If you reimplement this handler, it is very important that you
       
  8686     \link QTabletEvent ignore()\endlink the event if you do not handle
       
  8687     it, so that the widget's parent can interpret it.
       
  8688 
       
  8689     The default implementation ignores the event.
       
  8690 
       
  8691     \sa QTabletEvent::ignore(), QTabletEvent::accept(), event(),
       
  8692     QTabletEvent
       
  8693 */
       
  8694 
       
  8695 void QWidget::tabletEvent(QTabletEvent *event)
       
  8696 {
       
  8697     event->ignore();
       
  8698 }
       
  8699 #endif // QT_NO_TABLETEVENT
       
  8700 
       
  8701 /*!
       
  8702     This event handler, for event \a event, can be reimplemented in a
       
  8703     subclass to receive key press events for the widget.
       
  8704 
       
  8705     A widget must call setFocusPolicy() to accept focus initially and
       
  8706     have focus in order to receive a key press event.
       
  8707 
       
  8708     If you reimplement this handler, it is very important that you
       
  8709     call the base class implementation if you do not act upon the key.
       
  8710 
       
  8711     The default implementation closes popup widgets if the user
       
  8712     presses Esc. Otherwise the event is ignored, so that the widget's
       
  8713     parent can interpret it.
       
  8714 
       
  8715     Note that QKeyEvent starts with isAccepted() == true, so you do not
       
  8716     need to call QKeyEvent::accept() - just do not call the base class
       
  8717     implementation if you act upon the key.
       
  8718 
       
  8719     \sa keyReleaseEvent(), setFocusPolicy(),
       
  8720     focusInEvent(), focusOutEvent(), event(), QKeyEvent, {Tetrix Example}
       
  8721 */
       
  8722 
       
  8723 void QWidget::keyPressEvent(QKeyEvent *event)
       
  8724 {
       
  8725     if ((windowType() == Qt::Popup) && event->key() == Qt::Key_Escape) {
       
  8726         event->accept();
       
  8727         close();
       
  8728     } else {
       
  8729         event->ignore();
       
  8730     }
       
  8731 }
       
  8732 
       
  8733 /*!
       
  8734     This event handler, for event \a event, can be reimplemented in a
       
  8735     subclass to receive key release events for the widget.
       
  8736 
       
  8737     A widget must \link setFocusPolicy() accept focus\endlink
       
  8738     initially and \link hasFocus() have focus\endlink in order to
       
  8739     receive a key release event.
       
  8740 
       
  8741     If you reimplement this handler, it is very important that you
       
  8742     call the base class implementation if you do not act upon the key.
       
  8743 
       
  8744     The default implementation ignores the event, so that the widget's
       
  8745     parent can interpret it.
       
  8746 
       
  8747     Note that QKeyEvent starts with isAccepted() == true, so you do not
       
  8748     need to call QKeyEvent::accept() - just do not call the base class
       
  8749     implementation if you act upon the key.
       
  8750 
       
  8751     \sa keyPressEvent(), QKeyEvent::ignore(), setFocusPolicy(),
       
  8752     focusInEvent(), focusOutEvent(), event(), QKeyEvent
       
  8753 */
       
  8754 
       
  8755 void QWidget::keyReleaseEvent(QKeyEvent *event)
       
  8756 {
       
  8757     event->ignore();
       
  8758 }
       
  8759 
       
  8760 /*!
       
  8761     \fn void QWidget::focusInEvent(QFocusEvent *event)
       
  8762 
       
  8763     This event handler can be reimplemented in a subclass to receive
       
  8764     keyboard focus events (focus received) for the widget. The event
       
  8765     is passed in the \a event parameter
       
  8766 
       
  8767     A widget normally must setFocusPolicy() to something other than
       
  8768     Qt::NoFocus in order to receive focus events. (Note that the
       
  8769     application programmer can call setFocus() on any widget, even
       
  8770     those that do not normally accept focus.)
       
  8771 
       
  8772     The default implementation updates the widget (except for windows
       
  8773     that do not specify a focusPolicy()).
       
  8774 
       
  8775     \sa focusOutEvent(), setFocusPolicy(), keyPressEvent(),
       
  8776     keyReleaseEvent(), event(), QFocusEvent
       
  8777 */
       
  8778 
       
  8779 void QWidget::focusInEvent(QFocusEvent *)
       
  8780 {
       
  8781     if (focusPolicy() != Qt::NoFocus || !isWindow()) {
       
  8782         update();
       
  8783     }
       
  8784 }
       
  8785 
       
  8786 /*!
       
  8787     \fn void QWidget::focusOutEvent(QFocusEvent *event)
       
  8788 
       
  8789     This event handler can be reimplemented in a subclass to receive
       
  8790     keyboard focus events (focus lost) for the widget. The events is
       
  8791     passed in the \a event parameter.
       
  8792 
       
  8793     A widget normally must setFocusPolicy() to something other than
       
  8794     Qt::NoFocus in order to receive focus events. (Note that the
       
  8795     application programmer can call setFocus() on any widget, even
       
  8796     those that do not normally accept focus.)
       
  8797 
       
  8798     The default implementation updates the widget (except for windows
       
  8799     that do not specify a focusPolicy()).
       
  8800 
       
  8801     \sa focusInEvent(), setFocusPolicy(), keyPressEvent(),
       
  8802     keyReleaseEvent(), event(), QFocusEvent
       
  8803 */
       
  8804 
       
  8805 void QWidget::focusOutEvent(QFocusEvent *)
       
  8806 {
       
  8807     if (focusPolicy() != Qt::NoFocus || !isWindow())
       
  8808         update();
       
  8809 }
       
  8810 
       
  8811 /*!
       
  8812     \fn void QWidget::enterEvent(QEvent *event)
       
  8813 
       
  8814     This event handler can be reimplemented in a subclass to receive
       
  8815     widget enter events which are passed in the \a event parameter.
       
  8816 
       
  8817     An event is sent to the widget when the mouse cursor enters the
       
  8818     widget.
       
  8819 
       
  8820     \sa leaveEvent(), mouseMoveEvent(), event()
       
  8821 */
       
  8822 
       
  8823 void QWidget::enterEvent(QEvent *)
       
  8824 {
       
  8825 }
       
  8826 
       
  8827 /*!
       
  8828     \fn void QWidget::leaveEvent(QEvent *event)
       
  8829 
       
  8830     This event handler can be reimplemented in a subclass to receive
       
  8831     widget leave events which are passed in the \a event parameter.
       
  8832 
       
  8833     A leave event is sent to the widget when the mouse cursor leaves
       
  8834     the widget.
       
  8835 
       
  8836     \sa enterEvent(), mouseMoveEvent(), event()
       
  8837 */
       
  8838 
       
  8839 void QWidget::leaveEvent(QEvent *)
       
  8840 {
       
  8841 }
       
  8842 
       
  8843 /*!
       
  8844     \fn void QWidget::paintEvent(QPaintEvent *event)
       
  8845 
       
  8846     This event handler can be reimplemented in a subclass to receive paint
       
  8847     events passed in \a event.
       
  8848 
       
  8849     A paint event is a request to repaint all or part of a widget. It can
       
  8850     happen for one of the following reasons:
       
  8851 
       
  8852     \list
       
  8853         \o repaint() or update() was invoked,
       
  8854         \o the widget was obscured and has now been uncovered, or
       
  8855         \o many other reasons.
       
  8856     \endlist
       
  8857 
       
  8858     Many widgets can simply repaint their entire surface when asked to, but
       
  8859     some slow widgets need to optimize by painting only the requested region:
       
  8860     QPaintEvent::region(). This speed optimization does not change the result,
       
  8861     as painting is clipped to that region during event processing. QListView
       
  8862     and QTableView do this, for example.
       
  8863 
       
  8864     Qt also tries to speed up painting by merging multiple paint events into
       
  8865     one. When update() is called several times or the window system sends
       
  8866     several paint events, Qt merges these events into one event with a larger
       
  8867     region (see QRegion::united()). The repaint() function does not permit this
       
  8868     optimization, so we suggest using update() whenever possible.
       
  8869 
       
  8870     When the paint event occurs, the update region has normally been erased, so
       
  8871     you are painting on the widget's background.
       
  8872 
       
  8873     The background can be set using setBackgroundRole() and setPalette().
       
  8874 
       
  8875     Since Qt 4.0, QWidget automatically double-buffers its painting, so there
       
  8876     is no need to write double-buffering code in paintEvent() to avoid flicker.
       
  8877 
       
  8878     \bold{Note for the X11 platform}: It is possible to toggle global double
       
  8879     buffering by calling \c qt_x11_set_global_double_buffer(). For example,
       
  8880 
       
  8881     \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 14
       
  8882 
       
  8883     \note Generally, you should refrain from calling update() or repaint()
       
  8884     \bold{inside} a paintEvent(). For example, calling update() or repaint() on
       
  8885     children inside a paintevent() results in undefined behavior; the child may
       
  8886     or may not get a paint event.
       
  8887 
       
  8888     \warning If you are using a custom paint engine without Qt's backingstore,
       
  8889     Qt::WA_PaintOnScreen must be set. Otherwise, QWidget::paintEngine() will
       
  8890     never be called; the backingstore will be used instead.
       
  8891 
       
  8892     \sa event(), repaint(), update(), QPainter, QPixmap, QPaintEvent,
       
  8893     {Analog Clock Example}
       
  8894 */
       
  8895 
       
  8896 void QWidget::paintEvent(QPaintEvent *)
       
  8897 {
       
  8898 }
       
  8899 
       
  8900 
       
  8901 /*!
       
  8902     \fn void QWidget::moveEvent(QMoveEvent *event)
       
  8903 
       
  8904     This event handler can be reimplemented in a subclass to receive
       
  8905     widget move events which are passed in the \a event parameter.
       
  8906     When the widget receives this event, it is already at the new
       
  8907     position.
       
  8908 
       
  8909     The old position is accessible through QMoveEvent::oldPos().
       
  8910 
       
  8911     \sa resizeEvent(), event(), move(), QMoveEvent
       
  8912 */
       
  8913 
       
  8914 void QWidget::moveEvent(QMoveEvent *)
       
  8915 {
       
  8916 }
       
  8917 
       
  8918 
       
  8919 /*!
       
  8920     This event handler can be reimplemented in a subclass to receive
       
  8921     widget resize events which are passed in the \a event parameter.
       
  8922     When resizeEvent() is called, the widget already has its new
       
  8923     geometry. The old size is accessible through
       
  8924     QResizeEvent::oldSize().
       
  8925 
       
  8926     The widget will be erased and receive a paint event immediately
       
  8927     after processing the resize event. No drawing need be (or should
       
  8928     be) done inside this handler.
       
  8929 
       
  8930 
       
  8931     \sa moveEvent(), event(), resize(), QResizeEvent, paintEvent(),
       
  8932         {Scribble Example}
       
  8933 */
       
  8934 
       
  8935 void QWidget::resizeEvent(QResizeEvent * /* event */)
       
  8936 {
       
  8937 }
       
  8938 
       
  8939 #ifndef QT_NO_ACTION
       
  8940 /*!
       
  8941     \fn void QWidget::actionEvent(QActionEvent *event)
       
  8942 
       
  8943     This event handler is called with the given \a event whenever the
       
  8944     widget's actions are changed.
       
  8945 
       
  8946     \sa addAction(), insertAction(), removeAction(), actions(), QActionEvent
       
  8947 */
       
  8948 void QWidget::actionEvent(QActionEvent *)
       
  8949 {
       
  8950 
       
  8951 }
       
  8952 #endif
       
  8953 
       
  8954 /*!
       
  8955     This event handler is called with the given \a event when Qt receives a window
       
  8956     close request for a top-level widget from the window system.
       
  8957 
       
  8958     By default, the event is accepted and the widget is closed. You can reimplement
       
  8959     this function to change the way the widget responds to window close requests.
       
  8960     For example, you can prevent the window from closing by calling \l{QEvent::}{ignore()}
       
  8961     on all events.
       
  8962 
       
  8963     Main window applications typically use reimplementations of this function to check
       
  8964     whether the user's work has been saved and ask for permission before closing.
       
  8965     For example, the \l{Application Example} uses a helper function to determine whether
       
  8966     or not to close the window:
       
  8967 
       
  8968     \snippet mainwindows/application/mainwindow.cpp 3
       
  8969     \snippet mainwindows/application/mainwindow.cpp 4
       
  8970 
       
  8971     \sa event(), hide(), close(), QCloseEvent, {Application Example}
       
  8972 */
       
  8973 
       
  8974 void QWidget::closeEvent(QCloseEvent *event)
       
  8975 {
       
  8976     event->accept();
       
  8977 }
       
  8978 
       
  8979 #ifndef QT_NO_CONTEXTMENU
       
  8980 /*!
       
  8981     This event handler, for event \a event, can be reimplemented in a
       
  8982     subclass to receive widget context menu events.
       
  8983 
       
  8984     The handler is called when the widget's \l contextMenuPolicy is
       
  8985     Qt::DefaultContextMenu.
       
  8986 
       
  8987     The default implementation ignores the context event.
       
  8988     See the \l QContextMenuEvent documentation for more details.
       
  8989 
       
  8990     \sa event(), QContextMenuEvent customContextMenuRequested()
       
  8991 */
       
  8992 
       
  8993 void QWidget::contextMenuEvent(QContextMenuEvent *event)
       
  8994 {
       
  8995     event->ignore();
       
  8996 }
       
  8997 #endif // QT_NO_CONTEXTMENU
       
  8998 
       
  8999 
       
  9000 /*!
       
  9001     This event handler, for event \a event, can be reimplemented in a
       
  9002     subclass to receive Input Method composition events. This handler
       
  9003     is called when the state of the input method changes.
       
  9004 
       
  9005     Note that when creating custom text editing widgets, the
       
  9006     Qt::WA_InputMethodEnabled window attribute must be set explicitly
       
  9007     (using the setAttribute() function) in order to receive input
       
  9008     method events.
       
  9009 
       
  9010     The default implementation calls event->ignore(), which rejects the
       
  9011     Input Method event. See the \l QInputMethodEvent documentation for more
       
  9012     details.
       
  9013 
       
  9014     \sa event(), QInputMethodEvent
       
  9015 */
       
  9016 void QWidget::inputMethodEvent(QInputMethodEvent *event)
       
  9017 {
       
  9018     event->ignore();
       
  9019 }
       
  9020 
       
  9021 /*!
       
  9022     This method is only relevant for input widgets. It is used by the
       
  9023     input method to query a set of properties of the widget to be
       
  9024     able to support complex input method operations as support for
       
  9025     surrounding text and reconversions.
       
  9026 
       
  9027     \a query specifies which property is queried.
       
  9028 
       
  9029     \sa inputMethodEvent(), QInputMethodEvent, QInputContext, inputMethodHints
       
  9030 */
       
  9031 QVariant QWidget::inputMethodQuery(Qt::InputMethodQuery query) const
       
  9032 {
       
  9033     switch(query) {
       
  9034     case Qt::ImMicroFocus:
       
  9035         return QRect(width()/2, 0, 1, height());
       
  9036     case Qt::ImFont:
       
  9037         return font();
       
  9038     case Qt::ImAnchorPosition:
       
  9039         // Fallback.
       
  9040         return inputMethodQuery(Qt::ImCursorPosition);
       
  9041     default:
       
  9042         return QVariant();
       
  9043     }
       
  9044 }
       
  9045 
       
  9046 /*!
       
  9047     \property QWidget::inputMethodHints
       
  9048     \brief What input method specific hints the widget has.
       
  9049 
       
  9050     This is only relevant for input widgets. It is used by
       
  9051     the input method to retrieve hints as to how the input method
       
  9052     should operate. For example, if the Qt::ImhFormattedNumbersOnly flag
       
  9053     is set, the input method may change its visual components to reflect
       
  9054     that only numbers can be entered.
       
  9055 
       
  9056     \note The flags are only hints, so the particular input method
       
  9057           implementation is free to ignore them. If you want to be
       
  9058           sure that a certain type of characters are entered,
       
  9059           you should also set a QValidator on the widget.
       
  9060 
       
  9061     The default value is Qt::ImhNone.
       
  9062 
       
  9063     \since 4.6
       
  9064 
       
  9065     \sa inputMethodQuery(), QInputContext
       
  9066 */
       
  9067 Qt::InputMethodHints QWidget::inputMethodHints() const
       
  9068 {
       
  9069     Q_D(const QWidget);
       
  9070 #ifndef QT_NO_IM
       
  9071     return d->imHints;
       
  9072 #else //QT_NO_IM
       
  9073     return 0;
       
  9074 #endif //QT_NO_IM
       
  9075 }
       
  9076 
       
  9077 void QWidget::setInputMethodHints(Qt::InputMethodHints hints)
       
  9078 {
       
  9079 #ifndef QT_NO_IM
       
  9080     Q_D(QWidget);
       
  9081     d->imHints = hints;
       
  9082     // Optimisation to update input context only it has already been created.
       
  9083     if (d->ic || qApp->d_func()->inputContext) {
       
  9084         QInputContext *ic = inputContext();
       
  9085         if (ic)
       
  9086             ic->update();
       
  9087     }
       
  9088 #endif //QT_NO_IM
       
  9089 }
       
  9090 
       
  9091 
       
  9092 #ifndef QT_NO_DRAGANDDROP
       
  9093 
       
  9094 /*!
       
  9095     \fn void QWidget::dragEnterEvent(QDragEnterEvent *event)
       
  9096 
       
  9097     This event handler is called when a drag is in progress and the
       
  9098     mouse enters this widget. The event is passed in the \a event parameter.
       
  9099 
       
  9100     If the event is ignored, the widget won't receive any \l{dragMoveEvent()}{drag
       
  9101     move events}.
       
  9102 
       
  9103     See the \link dnd.html Drag-and-drop documentation\endlink for an
       
  9104     overview of how to provide drag-and-drop in your application.
       
  9105 
       
  9106     \sa QDrag, QDragEnterEvent
       
  9107 */
       
  9108 void QWidget::dragEnterEvent(QDragEnterEvent *)
       
  9109 {
       
  9110 }
       
  9111 
       
  9112 /*!
       
  9113     \fn void QWidget::dragMoveEvent(QDragMoveEvent *event)
       
  9114 
       
  9115     This event handler is called if a drag is in progress, and when
       
  9116     any of the following conditions occur: the cursor enters this widget,
       
  9117     the cursor moves within this widget, or a modifier key is pressed on
       
  9118     the keyboard while this widget has the focus. The event is passed
       
  9119     in the \a event parameter.
       
  9120 
       
  9121     See the \link dnd.html Drag-and-drop documentation\endlink for an
       
  9122     overview of how to provide drag-and-drop in your application.
       
  9123 
       
  9124     \sa QDrag, QDragMoveEvent
       
  9125 */
       
  9126 void QWidget::dragMoveEvent(QDragMoveEvent *)
       
  9127 {
       
  9128 }
       
  9129 
       
  9130 /*!
       
  9131     \fn void QWidget::dragLeaveEvent(QDragLeaveEvent *event)
       
  9132 
       
  9133     This event handler is called when a drag is in progress and the
       
  9134     mouse leaves this widget. The event is passed in the \a event
       
  9135     parameter.
       
  9136 
       
  9137     See the \link dnd.html Drag-and-drop documentation\endlink for an
       
  9138     overview of how to provide drag-and-drop in your application.
       
  9139 
       
  9140     \sa QDrag, QDragLeaveEvent
       
  9141 */
       
  9142 void QWidget::dragLeaveEvent(QDragLeaveEvent *)
       
  9143 {
       
  9144 }
       
  9145 
       
  9146 /*!
       
  9147     \fn void QWidget::dropEvent(QDropEvent *event)
       
  9148 
       
  9149     This event handler is called when the drag is dropped on this
       
  9150     widget. The event is passed in the \a event parameter.
       
  9151 
       
  9152     See the \link dnd.html Drag-and-drop documentation\endlink for an
       
  9153     overview of how to provide drag-and-drop in your application.
       
  9154 
       
  9155     \sa QDrag, QDropEvent
       
  9156 */
       
  9157 void QWidget::dropEvent(QDropEvent *)
       
  9158 {
       
  9159 }
       
  9160 
       
  9161 #endif // QT_NO_DRAGANDDROP
       
  9162 
       
  9163 /*!
       
  9164     \fn void QWidget::showEvent(QShowEvent *event)
       
  9165 
       
  9166     This event handler can be reimplemented in a subclass to receive
       
  9167     widget show events which are passed in the \a event parameter.
       
  9168 
       
  9169     Non-spontaneous show events are sent to widgets immediately
       
  9170     before they are shown. The spontaneous show events of windows are
       
  9171     delivered afterwards.
       
  9172 
       
  9173     Note: A widget receives spontaneous show and hide events when its
       
  9174     mapping status is changed by the window system, e.g. a spontaneous
       
  9175     hide event when the user minimizes the window, and a spontaneous
       
  9176     show event when the window is restored again. After receiving a
       
  9177     spontaneous hide event, a widget is still considered visible in
       
  9178     the sense of isVisible().
       
  9179 
       
  9180     \sa visible, event(), QShowEvent
       
  9181 */
       
  9182 void QWidget::showEvent(QShowEvent *)
       
  9183 {
       
  9184 }
       
  9185 
       
  9186 /*!
       
  9187     \fn void QWidget::hideEvent(QHideEvent *event)
       
  9188 
       
  9189     This event handler can be reimplemented in a subclass to receive
       
  9190     widget hide events. The event is passed in the \a event parameter.
       
  9191 
       
  9192     Hide events are sent to widgets immediately after they have been
       
  9193     hidden.
       
  9194 
       
  9195     Note: A widget receives spontaneous show and hide events when its
       
  9196     mapping status is changed by the window system, e.g. a spontaneous
       
  9197     hide event when the user minimizes the window, and a spontaneous
       
  9198     show event when the window is restored again. After receiving a
       
  9199     spontaneous hide event, a widget is still considered visible in
       
  9200     the sense of isVisible().
       
  9201 
       
  9202     \sa visible, event(), QHideEvent
       
  9203 */
       
  9204 void QWidget::hideEvent(QHideEvent *)
       
  9205 {
       
  9206 }
       
  9207 
       
  9208 /*
       
  9209     \fn QWidget::x11Event(MSG *)
       
  9210 
       
  9211     This special event handler can be reimplemented in a subclass to receive
       
  9212     native X11 events.
       
  9213 
       
  9214     In your reimplementation of this function, if you want to stop Qt from
       
  9215     handling the event, return true. If you return false, this native event
       
  9216     is passed back to Qt, which translates it into a Qt event and sends it to
       
  9217     the widget.
       
  9218 
       
  9219     \note Events are only delivered to this event handler if the widget is
       
  9220     native.
       
  9221 
       
  9222     \warning This function is not portable.
       
  9223 
       
  9224     \sa QApplication::x11EventFilter(), QWidget::winId()
       
  9225 */
       
  9226 
       
  9227 
       
  9228 #if defined(Q_WS_MAC)
       
  9229 
       
  9230 /*!
       
  9231     \fn bool QWidget::macEvent(EventHandlerCallRef caller, EventRef event)
       
  9232 
       
  9233     This special event handler can be reimplemented in a subclass to
       
  9234     receive native Macintosh events.
       
  9235 
       
  9236     The parameters are a bit different depending if Qt is build against Carbon
       
  9237     or Cocoa.  In Carbon, \a caller and \a event are the corresponding
       
  9238     EventHandlerCallRef and EventRef that correspond to the Carbon event
       
  9239     handlers that are installed. In Cocoa, \a caller is always 0 and the
       
  9240     EventRef is the EventRef generated from the NSEvent.
       
  9241 
       
  9242     In your reimplementation of this function, if you want to stop the
       
  9243     event being handled by Qt, return true. If you return false, this
       
  9244     native event is passed back to Qt, which translates the event into
       
  9245     a Qt event and sends it to the widget.
       
  9246 
       
  9247     \warning This function is not portable.
       
  9248 
       
  9249     \warning This function was not called inside of Qt until Qt 4.4.
       
  9250     If you need compatibility with earlier versions of Qt, consider QApplication::macEventFilter() instead.
       
  9251 
       
  9252     \sa QApplication::macEventFilter()
       
  9253 */
       
  9254 
       
  9255 bool QWidget::macEvent(EventHandlerCallRef, EventRef)
       
  9256 {
       
  9257     return false;
       
  9258 }
       
  9259 
       
  9260 #endif
       
  9261 #if defined(Q_WS_WIN)
       
  9262 
       
  9263 /*!
       
  9264     This special event handler can be reimplemented in a subclass to
       
  9265     receive native Windows events which are passed in the \a message
       
  9266     parameter.
       
  9267 
       
  9268     In your reimplementation of this function, if you want to stop the
       
  9269     event being handled by Qt, return true and set \a result to the value
       
  9270     that the window procedure should return. If you return false, this
       
  9271     native event is passed back to Qt, which translates the event into
       
  9272     a Qt event and sends it to the widget.
       
  9273 
       
  9274     \warning This function is not portable.
       
  9275 
       
  9276     \sa QApplication::winEventFilter()
       
  9277 */
       
  9278 bool QWidget::winEvent(MSG *message, long *result)
       
  9279 {
       
  9280     Q_UNUSED(message);
       
  9281     Q_UNUSED(result);
       
  9282     return false;
       
  9283 }
       
  9284 
       
  9285 #endif
       
  9286 #if defined(Q_WS_X11)
       
  9287 
       
  9288 /*!
       
  9289     \fn bool QWidget::x11Event(XEvent *event)
       
  9290 
       
  9291     This special event handler can be reimplemented in a subclass to receive
       
  9292     native X11 events passed in the \a event parameter.
       
  9293 
       
  9294     In your reimplementation of this function, if you want to stop Qt from
       
  9295     handling the event, return true. If you return false, this native event
       
  9296     is passed back to Qt, which translates it into a Qt event and sends it to
       
  9297     the widget.
       
  9298 
       
  9299     \note Events are only delivered to this event handler if the widget is
       
  9300     native.
       
  9301 
       
  9302     \warning This function is not portable.
       
  9303 
       
  9304     \sa QApplication::x11EventFilter(), QWidget::winId()
       
  9305 */
       
  9306 bool QWidget::x11Event(XEvent *)
       
  9307 {
       
  9308     return false;
       
  9309 }
       
  9310 
       
  9311 #endif
       
  9312 #if defined(Q_WS_QWS)
       
  9313 
       
  9314 /*!
       
  9315     \fn bool QWidget::qwsEvent(QWSEvent *event)
       
  9316 
       
  9317     This special event handler can be reimplemented in a subclass to
       
  9318     receive native Qt for Embedded Linux events which are passed in the
       
  9319     \a event parameter.
       
  9320 
       
  9321     In your reimplementation of this function, if you want to stop the
       
  9322     event being handled by Qt, return true. If you return false, this
       
  9323     native event is passed back to Qt, which translates the event into
       
  9324     a Qt event and sends it to the widget.
       
  9325 
       
  9326     \warning This function is not portable.
       
  9327 
       
  9328     \sa QApplication::qwsEventFilter()
       
  9329 */
       
  9330 bool QWidget::qwsEvent(QWSEvent *)
       
  9331 {
       
  9332     return false;
       
  9333 }
       
  9334 
       
  9335 #endif
       
  9336 
       
  9337 
       
  9338 /*!
       
  9339     Ensures that the widget has been polished by QStyle (i.e., has a
       
  9340     proper font and palette).
       
  9341 
       
  9342     QWidget calls this function after it has been fully constructed
       
  9343     but before it is shown the very first time. You can call this
       
  9344     function if you want to ensure that the widget is polished before
       
  9345     doing an operation, e.g., the correct font size might be needed in
       
  9346     the widget's sizeHint() reimplementation. Note that this function
       
  9347     \e is called from the default implementation of sizeHint().
       
  9348 
       
  9349     Polishing is useful for final initialization that must happen after
       
  9350     all constructors (from base classes as well as from subclasses)
       
  9351     have been called.
       
  9352 
       
  9353     If you need to change some settings when a widget is polished,
       
  9354     reimplement event() and handle the QEvent::Polish event type.
       
  9355 
       
  9356     \bold{Note:} The function is declared const so that it can be called from
       
  9357     other const functions (e.g., sizeHint()).
       
  9358 
       
  9359     \sa event()
       
  9360 */
       
  9361 void QWidget::ensurePolished() const
       
  9362 {
       
  9363     Q_D(const QWidget);
       
  9364 
       
  9365     const QMetaObject *m = metaObject();
       
  9366     if (m == d->polished)
       
  9367         return;
       
  9368     d->polished = m;
       
  9369 
       
  9370     QEvent e(QEvent::Polish);
       
  9371     QCoreApplication::sendEvent(const_cast<QWidget *>(this), &e);
       
  9372 
       
  9373     // polish children after 'this'
       
  9374     QList<QObject*> children = d->children;
       
  9375     for (int i = 0; i < children.size(); ++i) {
       
  9376         QObject *o = children.at(i);
       
  9377         if(!o->isWidgetType())
       
  9378             continue;
       
  9379         if (QWidget *w = qobject_cast<QWidget *>(o))
       
  9380             w->ensurePolished();
       
  9381     }
       
  9382 
       
  9383     if (d->parent && d->sendChildEvents) {
       
  9384         QChildEvent e(QEvent::ChildPolished, const_cast<QWidget *>(this));
       
  9385         QCoreApplication::sendEvent(d->parent, &e);
       
  9386     }
       
  9387 }
       
  9388 
       
  9389 /*!
       
  9390     Returns the mask currently set on a widget. If no mask is set the
       
  9391     return value will be an empty region.
       
  9392 
       
  9393     \sa setMask(), clearMask(), QRegion::isEmpty(), {Shaped Clock Example}
       
  9394 */
       
  9395 QRegion QWidget::mask() const
       
  9396 {
       
  9397     Q_D(const QWidget);
       
  9398     return d->extra ? d->extra->mask : QRegion();
       
  9399 }
       
  9400 
       
  9401 /*!
       
  9402     Returns the layout manager that is installed on this widget, or 0
       
  9403     if no layout manager is installed.
       
  9404 
       
  9405     The layout manager sets the geometry of the widget's children
       
  9406     that have been added to the layout.
       
  9407 
       
  9408     \sa setLayout(), sizePolicy(), {Layout Management}
       
  9409 */
       
  9410 QLayout *QWidget::layout() const
       
  9411 {
       
  9412     return d_func()->layout;
       
  9413 }
       
  9414 
       
  9415 
       
  9416 /*!
       
  9417     \fn void QWidget::setLayout(QLayout *layout)
       
  9418 
       
  9419     Sets the layout manager for this widget to \a layout.
       
  9420 
       
  9421     If there already is a layout manager installed on this widget,
       
  9422     QWidget won't let you install another. You must first delete the
       
  9423     existing layout manager (returned by layout()) before you can
       
  9424     call setLayout() with the new layout.
       
  9425 
       
  9426     If \a layout is the layout manger on a different widget, setLayout()
       
  9427     will reparent the layout and make it the layout manager for this widget.
       
  9428 
       
  9429     Example:
       
  9430 
       
  9431     \snippet examples/uitools/textfinder/textfinder.cpp 3b
       
  9432 
       
  9433     An alternative to calling this function is to pass this widget to
       
  9434     the layout's constructor.
       
  9435 
       
  9436     The QWidget will take ownership of \a layout.
       
  9437 
       
  9438     \sa layout(), {Layout Management}
       
  9439 */
       
  9440 
       
  9441 void QWidget::setLayout(QLayout *l)
       
  9442 {
       
  9443     if (!l) {
       
  9444         qWarning("QWidget::setLayout: Cannot set layout to 0");
       
  9445         return;
       
  9446     }
       
  9447     if (layout()) {
       
  9448         if (layout() != l)
       
  9449             qWarning("QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", which already has a"
       
  9450                      " layout", l->objectName().toLocal8Bit().data(), metaObject()->className(),
       
  9451                      objectName().toLocal8Bit().data());
       
  9452         return;
       
  9453     }
       
  9454 
       
  9455     QObject *oldParent = l->parent();
       
  9456     if (oldParent && oldParent != this) {
       
  9457         if (oldParent->isWidgetType()) {
       
  9458             // Steal the layout off a widget parent. Takes effect when
       
  9459             // morphing laid-out container widgets in Designer.
       
  9460             QWidget *oldParentWidget = static_cast<QWidget *>(oldParent);
       
  9461             oldParentWidget->takeLayout();
       
  9462         } else {
       
  9463             qWarning("QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
       
  9464                      l->objectName().toLocal8Bit().data(), metaObject()->className(),
       
  9465                      objectName().toLocal8Bit().data());
       
  9466             return;
       
  9467         }
       
  9468     }
       
  9469 
       
  9470     Q_D(QWidget);
       
  9471     l->d_func()->topLevel = true;
       
  9472     d->layout = l;
       
  9473     if (oldParent != this) {
       
  9474         l->setParent(this);
       
  9475         l->d_func()->reparentChildWidgets(this);
       
  9476         l->invalidate();
       
  9477     }
       
  9478 
       
  9479     if (isWindow() && d->maybeTopData())
       
  9480         d->topData()->sizeAdjusted = false;
       
  9481 }
       
  9482 
       
  9483 /*!
       
  9484     \fn QLayout *QWidget::takeLayout()
       
  9485 
       
  9486     Remove the layout from the widget.
       
  9487     \since 4.5
       
  9488 */
       
  9489 
       
  9490 QLayout *QWidget::takeLayout()
       
  9491 {
       
  9492     Q_D(QWidget);
       
  9493     QLayout *l =  layout();
       
  9494     if (!l)
       
  9495         return 0;
       
  9496     d->layout = 0;
       
  9497     l->setParent(0);
       
  9498     return l;
       
  9499 }
       
  9500 
       
  9501 /*!
       
  9502     \property QWidget::sizePolicy
       
  9503     \brief the default layout behavior of the widget
       
  9504 
       
  9505     If there is a QLayout that manages this widget's children, the
       
  9506     size policy specified by that layout is used. If there is no such
       
  9507     QLayout, the result of this function is used.
       
  9508 
       
  9509     The default policy is Preferred/Preferred, which means that the
       
  9510     widget can be freely resized, but prefers to be the size
       
  9511     sizeHint() returns. Button-like widgets set the size policy to
       
  9512     specify that they may stretch horizontally, but are fixed
       
  9513     vertically. The same applies to lineedit controls (such as
       
  9514     QLineEdit, QSpinBox or an editable QComboBox) and other
       
  9515     horizontally orientated widgets (such as QProgressBar).
       
  9516     QToolButton's are normally square, so they allow growth in both
       
  9517     directions. Widgets that support different directions (such as
       
  9518     QSlider, QScrollBar or QHeader) specify stretching in the
       
  9519     respective direction only. Widgets that can provide scroll bars
       
  9520     (usually subclasses of QScrollArea) tend to specify that they can
       
  9521     use additional space, and that they can make do with less than
       
  9522     sizeHint().
       
  9523 
       
  9524     \sa sizeHint() QLayout QSizePolicy updateGeometry()
       
  9525 */
       
  9526 QSizePolicy QWidget::sizePolicy() const
       
  9527 {
       
  9528     Q_D(const QWidget);
       
  9529     return d->size_policy;
       
  9530 }
       
  9531 
       
  9532 void QWidget::setSizePolicy(QSizePolicy policy)
       
  9533 {
       
  9534     Q_D(QWidget);
       
  9535     setAttribute(Qt::WA_WState_OwnSizePolicy);
       
  9536     if (policy == d->size_policy)
       
  9537         return;
       
  9538     d->size_policy = policy;
       
  9539 
       
  9540 #ifndef QT_NO_GRAPHICSVIEW
       
  9541     if (QWExtra *extra = d->extra) {
       
  9542         if (extra->proxyWidget)
       
  9543             extra->proxyWidget->setSizePolicy(policy);
       
  9544     }
       
  9545 #endif
       
  9546 
       
  9547     updateGeometry();
       
  9548 
       
  9549     if (isWindow() && d->maybeTopData())
       
  9550         d->topData()->sizeAdjusted = false;
       
  9551 }
       
  9552 
       
  9553 /*!
       
  9554     \fn void QWidget::setSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical)
       
  9555     \overload
       
  9556 
       
  9557     Sets the size policy of the widget to \a horizontal and \a
       
  9558     vertical, with standard stretch and no height-for-width.
       
  9559 
       
  9560     \sa QSizePolicy::QSizePolicy()
       
  9561 */
       
  9562 
       
  9563 /*!
       
  9564     Returns the preferred height for this widget, given the width \a w.
       
  9565 
       
  9566     If this widget has a layout, the default implementation returns
       
  9567     the layout's preferred height.  if there is no layout, the default
       
  9568     implementation returns -1 indicating that the preferred height
       
  9569     does not depend on the width.
       
  9570 */
       
  9571 
       
  9572 int QWidget::heightForWidth(int w) const
       
  9573 {
       
  9574     if (layout() && layout()->hasHeightForWidth())
       
  9575         return layout()->totalHeightForWidth(w);
       
  9576     return -1;
       
  9577 }
       
  9578 
       
  9579 /*!
       
  9580     \fn QWidget *QWidget::childAt(int x, int y) const
       
  9581 
       
  9582     Returns the visible child widget at the position (\a{x}, \a{y})
       
  9583     in the widget's coordinate system. If there is no visible child
       
  9584     widget at the specified position, the function returns 0.
       
  9585 */
       
  9586 
       
  9587 /*!
       
  9588     \overload
       
  9589 
       
  9590     Returns the visible child widget at point \a p in the widget's own
       
  9591     coordinate system.
       
  9592 */
       
  9593 
       
  9594 QWidget *QWidget::childAt(const QPoint &p) const
       
  9595 {
       
  9596     return d_func()->childAt_helper(p, false);
       
  9597 }
       
  9598 
       
  9599 QWidget *QWidgetPrivate::childAt_helper(const QPoint &p, bool ignoreChildrenInDestructor) const
       
  9600 {
       
  9601     Q_Q(const QWidget);
       
  9602 #ifdef Q_WS_MAC
       
  9603     bool includeFrame = q->isWindow() && qobject_cast<const QMainWindow *>(q)
       
  9604                         && static_cast<const QMainWindow *>(q)->unifiedTitleAndToolBarOnMac();
       
  9605 #endif
       
  9606 
       
  9607     if (
       
  9608 #ifdef Q_WS_MAC
       
  9609             !includeFrame &&
       
  9610 #endif
       
  9611             !q->rect().contains(p))
       
  9612         return 0;
       
  9613 
       
  9614     for (int i = children.size(); i > 0 ;) {
       
  9615         --i;
       
  9616         QWidget *w = qobject_cast<QWidget *>(children.at(i));
       
  9617         if (w && !w->isWindow() && !w->isHidden()
       
  9618                 && (w->geometry().contains(p)
       
  9619 #ifdef Q_WS_MAC
       
  9620                     || (includeFrame && w->geometry().contains(qt_mac_nativeMapFromParent(w, p)))
       
  9621 #endif
       
  9622                )) {
       
  9623             if (ignoreChildrenInDestructor && w->data->in_destructor)
       
  9624                 continue;
       
  9625             if (w->testAttribute(Qt::WA_TransparentForMouseEvents))
       
  9626                 continue;
       
  9627             QPoint childPoint = w->mapFromParent(p);
       
  9628 #ifdef Q_WS_MAC
       
  9629             if (includeFrame && !w->geometry().contains(p))
       
  9630                 childPoint = qt_mac_nativeMapFromParent(w, p);
       
  9631 #endif
       
  9632             if (QWidget *t = w->d_func()->childAt_helper(childPoint, ignoreChildrenInDestructor))
       
  9633                 return t;
       
  9634             // if WMouseNoMask is set the widget mask is ignored, if
       
  9635             // the widget has no mask then the WMouseNoMask flag has no
       
  9636             // effect
       
  9637             if (w->testAttribute(Qt::WA_MouseNoMask) || w->mask().contains(childPoint)
       
  9638                 || w->mask().isEmpty())
       
  9639                 return w;
       
  9640         }
       
  9641     }
       
  9642     return 0;
       
  9643 }
       
  9644 
       
  9645 void QWidgetPrivate::updateGeometry_helper(bool forceUpdate)
       
  9646 {
       
  9647     Q_Q(QWidget);
       
  9648     if (widgetItem)
       
  9649         widgetItem->invalidateSizeCache();
       
  9650     QWidget *parent;
       
  9651     if (forceUpdate || !extra || extra->minw != extra->maxw || extra->minh != extra->maxh) {
       
  9652         if (!q->isWindow() && !q->isHidden() && (parent = q->parentWidget())) {
       
  9653             if (parent->d_func()->layout)
       
  9654                 parent->d_func()->layout->invalidate();
       
  9655             else if (parent->isVisible())
       
  9656                 QApplication::postEvent(parent, new QEvent(QEvent::LayoutRequest));
       
  9657         }
       
  9658     }
       
  9659 }
       
  9660 
       
  9661 /*!
       
  9662     Notifies the layout system that this widget has changed and may
       
  9663     need to change geometry.
       
  9664 
       
  9665     Call this function if the sizeHint() or sizePolicy() have changed.
       
  9666 
       
  9667     For explicitly hidden widgets, updateGeometry() is a no-op. The
       
  9668     layout system will be notified as soon as the widget is shown.
       
  9669 */
       
  9670 
       
  9671 void QWidget::updateGeometry()
       
  9672 {
       
  9673     Q_D(QWidget);
       
  9674     d->updateGeometry_helper(false);
       
  9675 }
       
  9676 
       
  9677 /*! \property QWidget::windowFlags
       
  9678 
       
  9679     Window flags are a combination of a type (e.g. Qt::Dialog) and
       
  9680     zero or more hints to the window system (e.g.
       
  9681     Qt::FramelessWindowHint).
       
  9682 
       
  9683     If the widget had type Qt::Widget or Qt::SubWindow and becomes a
       
  9684     window (Qt::Window, Qt::Dialog, etc.), it is put at position (0,
       
  9685     0) on the desktop. If the widget is a window and becomes a
       
  9686     Qt::Widget or Qt::SubWindow, it is put at position (0, 0)
       
  9687     relative to its parent widget.
       
  9688 
       
  9689     \note This function calls setParent() when changing the flags for
       
  9690     a window, causing the widget to be hidden. You must call show() to make
       
  9691     the widget visible again..
       
  9692 
       
  9693     \sa windowType(), {Window Flags Example}
       
  9694 */
       
  9695 void QWidget::setWindowFlags(Qt::WindowFlags flags)
       
  9696 {
       
  9697     if (data->window_flags == flags)
       
  9698         return;
       
  9699 
       
  9700     Q_D(QWidget);
       
  9701 
       
  9702     if ((data->window_flags | flags) & Qt::Window) {
       
  9703         // the old type was a window and/or the new type is a window
       
  9704         QPoint oldPos = pos();
       
  9705         bool visible = isVisible();
       
  9706         setParent(parentWidget(), flags);
       
  9707 
       
  9708         // if both types are windows or neither of them are, we restore
       
  9709         // the old position
       
  9710         if (!((data->window_flags ^ flags) & Qt::Window)
       
  9711             && (visible || testAttribute(Qt::WA_Moved))) {
       
  9712             move(oldPos);
       
  9713         }
       
  9714         // for backward-compatibility we change Qt::WA_QuitOnClose attribute value only when the window was recreated.
       
  9715         d->adjustQuitOnCloseAttribute();
       
  9716     } else {
       
  9717         data->window_flags = flags;
       
  9718     }
       
  9719 }
       
  9720 
       
  9721 /*!
       
  9722     Sets the window flags for the widget to \a flags,
       
  9723     \e without telling the window system.
       
  9724 
       
  9725     \warning Do not call this function unless you really know what
       
  9726     you're doing.
       
  9727 
       
  9728     \sa setWindowFlags()
       
  9729 */
       
  9730 void QWidget::overrideWindowFlags(Qt::WindowFlags flags)
       
  9731 {
       
  9732     data->window_flags = flags;
       
  9733 }
       
  9734 
       
  9735 /*!
       
  9736     \fn Qt::WindowType QWidget::windowType() const
       
  9737 
       
  9738     Returns the window type of this widget. This is identical to
       
  9739     windowFlags() & Qt::WindowType_Mask.
       
  9740 
       
  9741     \sa windowFlags
       
  9742 */
       
  9743 
       
  9744 /*!
       
  9745     Sets the parent of the widget to \a parent, and resets the window
       
  9746     flags. The widget is moved to position (0, 0) in its new parent.
       
  9747 
       
  9748     If the new parent widget is in a different window, the
       
  9749     reparented widget and its children are appended to the end of the
       
  9750     \l{setFocusPolicy()}{tab chain} of the new parent
       
  9751     widget, in the same internal order as before. If one of the moved
       
  9752     widgets had keyboard focus, setParent() calls clearFocus() for that
       
  9753     widget.
       
  9754 
       
  9755     If the new parent widget is in the same window as the
       
  9756     old parent, setting the parent doesn't change the tab order or
       
  9757     keyboard focus.
       
  9758 
       
  9759     If the "new" parent widget is the old parent widget, this function
       
  9760     does nothing.
       
  9761 
       
  9762     \note The widget becomes invisible as part of changing its parent,
       
  9763     even if it was previously visible. You must call show() to make the
       
  9764     widget visible again.
       
  9765 
       
  9766     \warning It is very unlikely that you will ever need this
       
  9767     function. If you have a widget that changes its content
       
  9768     dynamically, it is far easier to use \l QStackedWidget.
       
  9769 
       
  9770     \sa setWindowFlags()
       
  9771 */
       
  9772 void QWidget::setParent(QWidget *parent)
       
  9773 {
       
  9774     if (parent == parentWidget())
       
  9775         return;
       
  9776     setParent((QWidget*)parent, windowFlags() & ~Qt::WindowType_Mask);
       
  9777 }
       
  9778 
       
  9779 /*!
       
  9780     \overload
       
  9781 
       
  9782     This function also takes widget flags, \a f as an argument.
       
  9783 */
       
  9784 
       
  9785 void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
       
  9786 {
       
  9787     Q_D(QWidget);
       
  9788     bool resized = testAttribute(Qt::WA_Resized);
       
  9789     bool wasCreated = testAttribute(Qt::WA_WState_Created);
       
  9790     QWidget *oldtlw = window();
       
  9791 
       
  9792     QWidget *desktopWidget = 0;
       
  9793     if (parent && parent->windowType() == Qt::Desktop)
       
  9794         desktopWidget = parent;
       
  9795     bool newParent = (parent != parentWidget()) || !wasCreated || desktopWidget;
       
  9796 
       
  9797 #if defined(Q_WS_X11) || defined(Q_WS_WIN)
       
  9798     if (newParent && parent && !desktopWidget) {
       
  9799         if (testAttribute(Qt::WA_NativeWindow) && !qApp->testAttribute(Qt::AA_DontCreateNativeWidgetSiblings))
       
  9800             parent->d_func()->enforceNativeChildren();
       
  9801         else if (parent->d_func()->nativeChildrenForced() || parent->testAttribute(Qt::WA_PaintOnScreen))
       
  9802             setAttribute(Qt::WA_NativeWindow);
       
  9803     }
       
  9804 #endif
       
  9805 
       
  9806     if (wasCreated) {
       
  9807         if (!testAttribute(Qt::WA_WState_Hidden)) {
       
  9808             hide();
       
  9809             setAttribute(Qt::WA_WState_ExplicitShowHide, false);
       
  9810         }
       
  9811         if (newParent) {
       
  9812             QEvent e(QEvent::ParentAboutToChange);
       
  9813             QApplication::sendEvent(this, &e);
       
  9814         }
       
  9815     }
       
  9816     if (newParent && isAncestorOf(focusWidget()))
       
  9817         focusWidget()->clearFocus();
       
  9818 
       
  9819     d->setParent_sys(parent, f);
       
  9820     if (desktopWidget)
       
  9821         parent = 0;
       
  9822 
       
  9823 #ifdef Q_BACKINGSTORE_SUBSURFACES
       
  9824     QTLWExtra *extra = d->maybeTopData();
       
  9825     QWindowSurface *windowSurface = (extra ? extra->windowSurface : 0);
       
  9826     if (newParent && windowSurface) {
       
  9827         QWidgetBackingStore *oldBs = oldtlw->d_func()->maybeBackingStore();
       
  9828         if (oldBs)
       
  9829             oldBs->subSurfaces.removeAll(windowSurface);
       
  9830 
       
  9831         if (parent) {
       
  9832             QWidgetBackingStore *newBs = parent->d_func()->maybeBackingStore();
       
  9833             if (newBs)
       
  9834                 newBs->subSurfaces.append(windowSurface);
       
  9835         }
       
  9836     }
       
  9837 #endif
       
  9838 
       
  9839     if (QWidgetBackingStore *oldBs = oldtlw->d_func()->maybeBackingStore()) {
       
  9840         if (newParent)
       
  9841             oldBs->removeDirtyWidget(this);
       
  9842         // Move the widget and all its static children from
       
  9843         // the old backing store to the new one.
       
  9844         oldBs->moveStaticWidgets(this);
       
  9845     }
       
  9846 
       
  9847     if ((QApplicationPrivate::app_compile_version < 0x040200
       
  9848          || QApplicationPrivate::testAttribute(Qt::AA_ImmediateWidgetCreation))
       
  9849         && !testAttribute(Qt::WA_WState_Created))
       
  9850         create();
       
  9851 
       
  9852     d->reparentFocusWidgets(oldtlw);
       
  9853     setAttribute(Qt::WA_Resized, resized);
       
  9854     if (!testAttribute(Qt::WA_StyleSheet)
       
  9855         && (!parent || !parent->testAttribute(Qt::WA_StyleSheet))) {
       
  9856         d->resolveFont();
       
  9857         d->resolvePalette();
       
  9858     }
       
  9859     d->resolveLayoutDirection();
       
  9860     d->resolveLocale();
       
  9861 
       
  9862     // Note: GL widgets under WGL or EGL will always need a ParentChange
       
  9863     // event to handle recreation/rebinding of the GL context, hence the
       
  9864     // (f & Qt::MSWindowsOwnDC) clause (which is set on QGLWidgets on all
       
  9865     // platforms).
       
  9866     if (newParent
       
  9867 #if defined(Q_WS_WIN) || defined(QT_OPENGL_ES)
       
  9868         || (f & Qt::MSWindowsOwnDC)
       
  9869 #endif
       
  9870         ) {
       
  9871         // propagate enabled updates enabled state to non-windows
       
  9872         if (!isWindow()) {
       
  9873             if (!testAttribute(Qt::WA_ForceDisabled))
       
  9874                 d->setEnabled_helper(parent ? parent->isEnabled() : true);
       
  9875             if (!testAttribute(Qt::WA_ForceUpdatesDisabled))
       
  9876                 d->setUpdatesEnabled_helper(parent ? parent->updatesEnabled() : true);
       
  9877         }
       
  9878         d->inheritStyle();
       
  9879 
       
  9880         // send and post remaining QObject events
       
  9881         if (parent && d->sendChildEvents) {
       
  9882             QChildEvent e(QEvent::ChildAdded, this);
       
  9883             QApplication::sendEvent(parent, &e);
       
  9884 #ifdef QT3_SUPPORT
       
  9885             if (parent->d_func()->pendingChildInsertedEvents.isEmpty()) {
       
  9886                 QApplication::postEvent(parent,
       
  9887                                         new QEvent(QEvent::ChildInsertedRequest),
       
  9888                                         Qt::HighEventPriority);
       
  9889             }
       
  9890             parent->d_func()->pendingChildInsertedEvents.append(this);
       
  9891 #endif
       
  9892         }
       
  9893 
       
  9894 //### already hidden above ---> must probably do something smart on the mac
       
  9895 // #ifdef Q_WS_MAC
       
  9896 //             extern bool qt_mac_is_macdrawer(const QWidget *); //qwidget_mac.cpp
       
  9897 //             if(!qt_mac_is_macdrawer(q)) //special case
       
  9898 //                 q->setAttribute(Qt::WA_WState_Hidden);
       
  9899 // #else
       
  9900 //             q->setAttribute(Qt::WA_WState_Hidden);
       
  9901 //#endif
       
  9902 
       
  9903         if (parent && d->sendChildEvents && d->polished) {
       
  9904             QChildEvent e(QEvent::ChildPolished, this);
       
  9905             QCoreApplication::sendEvent(parent, &e);
       
  9906         }
       
  9907 
       
  9908         QEvent e(QEvent::ParentChange);
       
  9909         QApplication::sendEvent(this, &e);
       
  9910     }
       
  9911 
       
  9912     if (!wasCreated) {
       
  9913         if (isWindow() || parentWidget()->isVisible())
       
  9914             setAttribute(Qt::WA_WState_Hidden, true);
       
  9915         else if (!testAttribute(Qt::WA_WState_ExplicitShowHide))
       
  9916             setAttribute(Qt::WA_WState_Hidden, false);
       
  9917     }
       
  9918 
       
  9919     d->updateIsOpaque();
       
  9920 
       
  9921 #ifndef QT_NO_GRAPHICSVIEW
       
  9922     // Embed the widget into a proxy if the parent is embedded.
       
  9923     // ### Doesn't handle reparenting out of an embedded widget.
       
  9924     if (oldtlw->graphicsProxyWidget()) {
       
  9925         if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(oldtlw))
       
  9926             ancestorProxy->d_func()->unembedSubWindow(this);
       
  9927     }
       
  9928     if (isWindow() && parent && !graphicsProxyWidget() && !bypassGraphicsProxyWidget(this)) {
       
  9929         if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(parent))
       
  9930             ancestorProxy->d_func()->embedSubWindow(this);
       
  9931     }
       
  9932 #endif
       
  9933 }
       
  9934 
       
  9935 /*!
       
  9936     Scrolls the widget including its children \a dx pixels to the
       
  9937     right and \a dy downward. Both \a dx and \a dy may be negative.
       
  9938 
       
  9939     After scrolling, the widgets will receive paint events for
       
  9940     the areas that need to be repainted. For widgets that Qt knows to
       
  9941     be opaque, this is only the newly exposed parts.
       
  9942     For example, if an opaque widget is scrolled 8 pixels to the left,
       
  9943     only an 8-pixel wide stripe at the right edge needs updating.
       
  9944 
       
  9945     Since widgets propagate the contents of their parents by default,
       
  9946     you need to set the \l autoFillBackground property, or use
       
  9947     setAttribute() to set the Qt::WA_OpaquePaintEvent attribute, to make
       
  9948     a widget opaque.
       
  9949 
       
  9950     For widgets that use contents propagation, a scroll will cause an
       
  9951     update of the entire scroll area.
       
  9952 
       
  9953     \sa {Transparency and Double Buffering}
       
  9954 */
       
  9955 
       
  9956 void QWidget::scroll(int dx, int dy)
       
  9957 {
       
  9958     if ((!updatesEnabled() && children().size() == 0) || !isVisible())
       
  9959         return;
       
  9960     if (dx == 0 && dy == 0)
       
  9961         return;
       
  9962     Q_D(QWidget);
       
  9963 #ifndef QT_NO_GRAPHICSVIEW
       
  9964     if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(this)) {
       
  9965         // Graphics View maintains its own dirty region as a list of rects;
       
  9966         // until we can connect item updates directly to the view, we must
       
  9967         // separately add a translated dirty region.
       
  9968         if (!d->dirty.isEmpty()) {
       
  9969             foreach (const QRect &rect, (d->dirty.translated(dx, dy)).rects())
       
  9970                 proxy->update(rect);
       
  9971         }
       
  9972         proxy->scroll(dx, dy, proxy->subWidgetRect(this));
       
  9973         return;
       
  9974     }
       
  9975 #endif
       
  9976     d->setDirtyOpaqueRegion();
       
  9977     d->scroll_sys(dx, dy);
       
  9978 }
       
  9979 
       
  9980 /*!
       
  9981     \overload
       
  9982 
       
  9983     This version only scrolls \a r and does not move the children of
       
  9984     the widget.
       
  9985 
       
  9986     If \a r is empty or invalid, the result is undefined.
       
  9987 
       
  9988     \sa QScrollArea
       
  9989 */
       
  9990 void QWidget::scroll(int dx, int dy, const QRect &r)
       
  9991 {
       
  9992 
       
  9993     if ((!updatesEnabled() && children().size() == 0) || !isVisible())
       
  9994         return;
       
  9995     if (dx == 0 && dy == 0)
       
  9996         return;
       
  9997     Q_D(QWidget);
       
  9998 #ifndef QT_NO_GRAPHICSVIEW
       
  9999     if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(this)) {
       
 10000         // Graphics View maintains its own dirty region as a list of rects;
       
 10001         // until we can connect item updates directly to the view, we must
       
 10002         // separately add a translated dirty region.
       
 10003         if (!d->dirty.isEmpty()) {
       
 10004             foreach (const QRect &rect, (d->dirty.translated(dx, dy) & r).rects())
       
 10005                 proxy->update(rect);
       
 10006         }
       
 10007         proxy->scroll(dx, dy, r.translated(proxy->subWidgetRect(this).topLeft().toPoint()));
       
 10008         return;
       
 10009     }
       
 10010 #endif
       
 10011     d->scroll_sys(dx, dy, r);
       
 10012 }
       
 10013 
       
 10014 /*!
       
 10015     Repaints the widget directly by calling paintEvent() immediately,
       
 10016     unless updates are disabled or the widget is hidden.
       
 10017 
       
 10018     We suggest only using repaint() if you need an immediate repaint,
       
 10019     for example during animation. In almost all circumstances update()
       
 10020     is better, as it permits Qt to optimize for speed and minimize
       
 10021     flicker.
       
 10022 
       
 10023     \warning If you call repaint() in a function which may itself be
       
 10024     called from paintEvent(), you may get infinite recursion. The
       
 10025     update() function never causes recursion.
       
 10026 
       
 10027     \sa update(), paintEvent(), setUpdatesEnabled()
       
 10028 */
       
 10029 
       
 10030 void QWidget::repaint()
       
 10031 {
       
 10032     repaint(rect());
       
 10033 }
       
 10034 
       
 10035 /*! \overload
       
 10036 
       
 10037     This version repaints a rectangle (\a x, \a y, \a w, \a h) inside
       
 10038     the widget.
       
 10039 
       
 10040     If \a w is negative, it is replaced with \c{width() - x}, and if
       
 10041     \a h is negative, it is replaced width \c{height() - y}.
       
 10042 */
       
 10043 void QWidget::repaint(int x, int y, int w, int h)
       
 10044 {
       
 10045     if (x > data->crect.width() || y > data->crect.height())
       
 10046         return;
       
 10047 
       
 10048     if (w < 0)
       
 10049         w = data->crect.width()  - x;
       
 10050     if (h < 0)
       
 10051         h = data->crect.height() - y;
       
 10052 
       
 10053     repaint(QRect(x, y, w, h));
       
 10054 }
       
 10055 
       
 10056 /*! \overload
       
 10057 
       
 10058     This version repaints a rectangle \a rect inside the widget.
       
 10059 */
       
 10060 void QWidget::repaint(const QRect &rect)
       
 10061 {
       
 10062     Q_D(QWidget);
       
 10063 
       
 10064     if (testAttribute(Qt::WA_WState_ConfigPending)) {
       
 10065         update(rect);
       
 10066         return;
       
 10067     }
       
 10068 
       
 10069     if (!isVisible() || !updatesEnabled() || rect.isEmpty())
       
 10070         return;
       
 10071 
       
 10072     if (hasBackingStoreSupport()) {
       
 10073         QTLWExtra *tlwExtra = window()->d_func()->maybeTopData();
       
 10074         if (tlwExtra && !tlwExtra->inTopLevelResize && tlwExtra->backingStore) {
       
 10075             tlwExtra->inRepaint = true;
       
 10076             tlwExtra->backingStore->markDirty(rect, this, true);
       
 10077             tlwExtra->inRepaint = false;
       
 10078         }
       
 10079     } else {
       
 10080         d->repaint_sys(rect);
       
 10081     }
       
 10082 }
       
 10083 
       
 10084 /*!
       
 10085     \overload
       
 10086 
       
 10087     This version repaints a region \a rgn inside the widget.
       
 10088 */
       
 10089 void QWidget::repaint(const QRegion &rgn)
       
 10090 {
       
 10091     Q_D(QWidget);
       
 10092 
       
 10093     if (testAttribute(Qt::WA_WState_ConfigPending)) {
       
 10094         update(rgn);
       
 10095         return;
       
 10096     }
       
 10097 
       
 10098     if (!isVisible() || !updatesEnabled() || rgn.isEmpty())
       
 10099         return;
       
 10100 
       
 10101     if (hasBackingStoreSupport()) {
       
 10102         QTLWExtra *tlwExtra = window()->d_func()->maybeTopData();
       
 10103         if (tlwExtra && !tlwExtra->inTopLevelResize && tlwExtra->backingStore) {
       
 10104             tlwExtra->inRepaint = true;
       
 10105             tlwExtra->backingStore->markDirty(rgn, this, true);
       
 10106             tlwExtra->inRepaint = false;
       
 10107         }
       
 10108     } else {
       
 10109         d->repaint_sys(rgn);
       
 10110     }
       
 10111 }
       
 10112 
       
 10113 /*!
       
 10114     Updates the widget unless updates are disabled or the widget is
       
 10115     hidden.
       
 10116 
       
 10117     This function does not cause an immediate repaint; instead it
       
 10118     schedules a paint event for processing when Qt returns to the main
       
 10119     event loop. This permits Qt to optimize for more speed and less
       
 10120     flicker than a call to repaint() does.
       
 10121 
       
 10122     Calling update() several times normally results in just one
       
 10123     paintEvent() call.
       
 10124 
       
 10125     Qt normally erases the widget's area before the paintEvent() call.
       
 10126     If the Qt::WA_OpaquePaintEvent widget attribute is set, the widget is
       
 10127     responsible for painting all its pixels with an opaque color.
       
 10128 
       
 10129     \sa repaint() paintEvent(), setUpdatesEnabled(), {Analog Clock Example}
       
 10130 */
       
 10131 void QWidget::update()
       
 10132 {
       
 10133     update(rect());
       
 10134 }
       
 10135 
       
 10136 /*! \fn void QWidget::update(int x, int y, int w, int h)
       
 10137     \overload
       
 10138 
       
 10139     This version updates a rectangle (\a x, \a y, \a w, \a h) inside
       
 10140     the widget.
       
 10141 */
       
 10142 
       
 10143 /*!
       
 10144     \overload
       
 10145 
       
 10146     This version updates a rectangle \a rect inside the widget.
       
 10147 */
       
 10148 void QWidget::update(const QRect &rect)
       
 10149 {
       
 10150     if (!isVisible() || !updatesEnabled() || rect.isEmpty())
       
 10151         return;
       
 10152 
       
 10153     if (testAttribute(Qt::WA_WState_InPaintEvent)) {
       
 10154         QApplication::postEvent(this, new QUpdateLaterEvent(rect));
       
 10155         return;
       
 10156     }
       
 10157 
       
 10158     if (hasBackingStoreSupport()) {
       
 10159         QTLWExtra *tlwExtra = window()->d_func()->maybeTopData();
       
 10160         if (tlwExtra && !tlwExtra->inTopLevelResize && tlwExtra->backingStore)
       
 10161             tlwExtra->backingStore->markDirty(rect, this);
       
 10162     } else {
       
 10163         d_func()->repaint_sys(rect);
       
 10164     }
       
 10165 }
       
 10166 
       
 10167 /*!
       
 10168     \overload
       
 10169 
       
 10170     This version repaints a region \a rgn inside the widget.
       
 10171 */
       
 10172 void QWidget::update(const QRegion &rgn)
       
 10173 {
       
 10174     if (!isVisible() || !updatesEnabled() || rgn.isEmpty())
       
 10175         return;
       
 10176 
       
 10177     if (testAttribute(Qt::WA_WState_InPaintEvent)) {
       
 10178         QApplication::postEvent(this, new QUpdateLaterEvent(rgn));
       
 10179         return;
       
 10180     }
       
 10181 
       
 10182     if (hasBackingStoreSupport()) {
       
 10183         QTLWExtra *tlwExtra = window()->d_func()->maybeTopData();
       
 10184         if (tlwExtra && !tlwExtra->inTopLevelResize && tlwExtra->backingStore)
       
 10185             tlwExtra->backingStore->markDirty(rgn, this);
       
 10186     } else {
       
 10187         d_func()->repaint_sys(rgn);
       
 10188     }
       
 10189 }
       
 10190 
       
 10191 #ifdef QT3_SUPPORT
       
 10192 /*!
       
 10193     Clear the rectangle at point (\a x, \a y) of width \a w and height
       
 10194     \a h.
       
 10195 
       
 10196     \warning This is best done in a paintEvent().
       
 10197 */
       
 10198 void QWidget::erase_helper(int x, int y, int w, int h)
       
 10199 {
       
 10200     if (testAttribute(Qt::WA_NoSystemBackground) || testAttribute(Qt::WA_UpdatesDisabled) ||  !testAttribute(Qt::WA_WState_Visible))
       
 10201         return;
       
 10202     if (w < 0)
       
 10203         w = data->crect.width()  - x;
       
 10204     if (h < 0)
       
 10205         h = data->crect.height() - y;
       
 10206     if (w != 0 && h != 0) {
       
 10207         QPainter p(this);
       
 10208         p.eraseRect(QRect(x, y, w, h));
       
 10209     }
       
 10210 }
       
 10211 
       
 10212 /*!
       
 10213     \overload
       
 10214 
       
 10215     Clear the given region, \a rgn.
       
 10216 
       
 10217     Drawing may only take place in a QPaintEvent. Overload
       
 10218     paintEvent() to do your erasing and call update() to schedule a
       
 10219     replaint whenever necessary. See also QPainter.
       
 10220 */
       
 10221 void QWidget::erase(const QRegion& rgn)
       
 10222 {
       
 10223     if (testAttribute(Qt::WA_NoSystemBackground) || testAttribute(Qt::WA_UpdatesDisabled) || !testAttribute(Qt::WA_WState_Visible))
       
 10224         return;
       
 10225 
       
 10226     QPainter p(this);
       
 10227     p.setClipRegion(rgn);
       
 10228     p.eraseRect(rgn.boundingRect());
       
 10229 }
       
 10230 
       
 10231 void QWidget::drawText_helper(int x, int y, const QString &str)
       
 10232 {
       
 10233     if(!testAttribute(Qt::WA_WState_Visible))
       
 10234         return;
       
 10235     QPainter paint(this);
       
 10236     paint.drawText(x, y, str);
       
 10237 }
       
 10238 
       
 10239 
       
 10240 /*!
       
 10241     Closes the widget.
       
 10242 
       
 10243     Use the no-argument overload instead.
       
 10244 */
       
 10245 bool QWidget::close(bool alsoDelete)
       
 10246 {
       
 10247     QPointer<QWidget> that = this;
       
 10248     bool accepted = close();
       
 10249     if (alsoDelete && accepted && that)
       
 10250         deleteLater();
       
 10251     return accepted;
       
 10252 }
       
 10253 
       
 10254 void QWidget::setIcon(const QPixmap &i)
       
 10255 {
       
 10256     setWindowIcon(i);
       
 10257 }
       
 10258 
       
 10259 /*!
       
 10260     Return's the widget's icon.
       
 10261 
       
 10262     Use windowIcon() instead.
       
 10263 */
       
 10264 const QPixmap *QWidget::icon() const
       
 10265 {
       
 10266     Q_D(const QWidget);
       
 10267     return (d->extra && d->extra->topextra) ? d->extra->topextra->iconPixmap : 0;
       
 10268 }
       
 10269 
       
 10270 #endif // QT3_SUPPORT
       
 10271 
       
 10272 /*!
       
 10273     Sets the attribute \a attribute on this widget if \a on is true;
       
 10274     otherwise clears the attribute.
       
 10275 
       
 10276     \sa testAttribute()
       
 10277 */
       
 10278 void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on)
       
 10279 {
       
 10280     if (testAttribute(attribute) == on)
       
 10281         return;
       
 10282 
       
 10283     Q_D(QWidget);
       
 10284     Q_ASSERT_X(sizeof(d->high_attributes)*8 >= (Qt::WA_AttributeCount - sizeof(uint)*8),
       
 10285                "QWidget::setAttribute(WidgetAttribute, bool)",
       
 10286                "QWidgetPrivate::high_attributes[] too small to contain all attributes in WidgetAttribute");
       
 10287 
       
 10288 #ifdef Q_WS_WIN
       
 10289     // ### Don't use PaintOnScreen+paintEngine() to do native painting in 5.0
       
 10290     if (attribute == Qt::WA_PaintOnScreen && on && !inherits("QGLWidget")) {
       
 10291         // see qwidget_win.cpp, ::paintEngine for details
       
 10292         paintEngine();
       
 10293         if (d->noPaintOnScreen)
       
 10294             return;
       
 10295     }
       
 10296 #endif
       
 10297 
       
 10298     if (attribute < int(8*sizeof(uint))) {
       
 10299         if (on)
       
 10300             data->widget_attributes |= (1<<attribute);
       
 10301         else
       
 10302             data->widget_attributes &= ~(1<<attribute);
       
 10303     } else {
       
 10304         const int x = attribute - 8*sizeof(uint);
       
 10305         const int int_off = x / (8*sizeof(uint));
       
 10306         if (on)
       
 10307             d->high_attributes[int_off] |= (1<<(x-(int_off*8*sizeof(uint))));
       
 10308         else
       
 10309             d->high_attributes[int_off] &= ~(1<<(x-(int_off*8*sizeof(uint))));
       
 10310     }
       
 10311 
       
 10312     switch (attribute) {
       
 10313 
       
 10314 #ifndef QT_NO_DRAGANDDROP
       
 10315     case Qt::WA_AcceptDrops:  {
       
 10316         if (on && !testAttribute(Qt::WA_DropSiteRegistered))
       
 10317             setAttribute(Qt::WA_DropSiteRegistered, true);
       
 10318         else if (!on && (isWindow() || !parentWidget() || !parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)))
       
 10319             setAttribute(Qt::WA_DropSiteRegistered, false);
       
 10320         QEvent e(QEvent::AcceptDropsChange);
       
 10321         QApplication::sendEvent(this, &e);
       
 10322         break;
       
 10323     }
       
 10324     case Qt::WA_DropSiteRegistered:  {
       
 10325         d->registerDropSite(on);
       
 10326         for (int i = 0; i < d->children.size(); ++i) {
       
 10327             QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
       
 10328             if (w && !w->isWindow() && !w->testAttribute(Qt::WA_AcceptDrops) && w->testAttribute(Qt::WA_DropSiteRegistered) != on)
       
 10329                 w->setAttribute(Qt::WA_DropSiteRegistered, on);
       
 10330         }
       
 10331         break;
       
 10332     }
       
 10333 #endif
       
 10334 
       
 10335     case Qt::WA_NoChildEventsForParent:
       
 10336         d->sendChildEvents = !on;
       
 10337         break;
       
 10338     case Qt::WA_NoChildEventsFromChildren:
       
 10339         d->receiveChildEvents = !on;
       
 10340         break;
       
 10341     case Qt::WA_MacBrushedMetal:
       
 10342 #ifdef Q_WS_MAC
       
 10343         d->setStyle_helper(style(), false, true);  // Make sure things get unpolished/polished correctly.
       
 10344         // fall through since changing the metal attribute affects the opaque size grip.
       
 10345     case Qt::WA_MacOpaqueSizeGrip:
       
 10346         d->macUpdateOpaqueSizeGrip();
       
 10347         break;
       
 10348     case Qt::WA_MacShowFocusRect:
       
 10349         if (hasFocus()) {
       
 10350             clearFocus();
       
 10351             setFocus();
       
 10352         }
       
 10353         break;
       
 10354     case Qt::WA_Hover:
       
 10355         qt_mac_update_mouseTracking(this);
       
 10356         break;
       
 10357 #endif
       
 10358     case Qt::WA_MacAlwaysShowToolWindow:
       
 10359 #ifdef Q_WS_MAC
       
 10360         d->macUpdateHideOnSuspend();
       
 10361 #endif
       
 10362         break;
       
 10363     case Qt::WA_MacNormalSize:
       
 10364     case Qt::WA_MacSmallSize:
       
 10365     case Qt::WA_MacMiniSize:
       
 10366 #ifdef Q_WS_MAC
       
 10367         {
       
 10368             // We can only have one of these set at a time
       
 10369             static const int MacSizes[] = { Qt::WA_MacNormalSize, Qt::WA_MacSmallSize,
       
 10370                                             Qt::WA_MacMiniSize, 0 };
       
 10371             for (int i = 0; MacSizes[i] != 0; ++i) {
       
 10372                 if (MacSizes[i] == attribute)
       
 10373                     continue;
       
 10374                 int macsize_x = MacSizes[i] - 8*sizeof(uint);
       
 10375                 int macsize_int_off = macsize_x / (8*sizeof(uint));
       
 10376                 d->high_attributes[macsize_int_off] &= ~(1<<(macsize_x-(macsize_int_off*8*sizeof(uint))));
       
 10377             }
       
 10378             d->macUpdateSizeAttribute();
       
 10379         }
       
 10380 #endif
       
 10381         break;
       
 10382     case Qt::WA_ShowModal:
       
 10383         if (!on) {
       
 10384             if (isVisible())
       
 10385                 QApplicationPrivate::leaveModal(this);
       
 10386             // reset modality type to Modeless when clearing WA_ShowModal
       
 10387             data->window_modality = Qt::NonModal;
       
 10388         } else if (data->window_modality == Qt::NonModal) {
       
 10389             // determine the modality type if it hasn't been set prior
       
 10390             // to setting WA_ShowModal. set the default to WindowModal
       
 10391             // if we are the child of a group leader; otherwise use
       
 10392             // ApplicationModal.
       
 10393             QWidget *w = parentWidget();
       
 10394             if (w)
       
 10395                 w = w->window();
       
 10396             while (w && !w->testAttribute(Qt::WA_GroupLeader)) {
       
 10397                 w = w->parentWidget();
       
 10398                 if (w)
       
 10399                     w = w->window();
       
 10400             }
       
 10401             data->window_modality = (w && w->testAttribute(Qt::WA_GroupLeader))
       
 10402                                     ? Qt::WindowModal
       
 10403                                     : Qt::ApplicationModal;
       
 10404             // Some window managers does not allow us to enter modal after the
       
 10405             // window is showing. Therefore, to be consistent, we cannot call
       
 10406             // QApplicationPrivate::enterModal(this) here. The window must be
       
 10407             // hidden before changing modality.
       
 10408         }
       
 10409         if (testAttribute(Qt::WA_WState_Created)) {
       
 10410             // don't call setModal_sys() before create_sys()
       
 10411             d->setModal_sys();
       
 10412         }
       
 10413         break;
       
 10414     case Qt::WA_MouseTracking: {
       
 10415         QEvent e(QEvent::MouseTrackingChange);
       
 10416         QApplication::sendEvent(this, &e);
       
 10417         break; }
       
 10418     case Qt::WA_NativeWindow: {
       
 10419 #ifndef QT_NO_IM
       
 10420         QWidget *focusWidget = d->effectiveFocusWidget();
       
 10421         QInputContext *ic = 0;
       
 10422         if (on && !internalWinId() && hasFocus()
       
 10423             && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
       
 10424             ic = focusWidget->d_func()->inputContext();
       
 10425             if (ic) {
       
 10426                 ic->reset();
       
 10427                 ic->setFocusWidget(0);
       
 10428             }
       
 10429         }
       
 10430         if (!qApp->testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget())
       
 10431             parentWidget()->d_func()->enforceNativeChildren();
       
 10432         if (on && !internalWinId() && testAttribute(Qt::WA_WState_Created))
       
 10433             d->createWinId();
       
 10434         if (ic && isEnabled() && focusWidget->isEnabled()
       
 10435             && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
       
 10436             ic->setFocusWidget(focusWidget);
       
 10437         }
       
 10438 #endif //QT_NO_IM
       
 10439         break;
       
 10440     }
       
 10441     case Qt::WA_PaintOnScreen:
       
 10442         d->updateIsOpaque();
       
 10443 #if defined(Q_WS_WIN) || defined(Q_WS_X11)
       
 10444         // Recreate the widget if it's already created as an alien widget and
       
 10445         // WA_PaintOnScreen is enabled. Paint on screen widgets must have win id.
       
 10446         // So must their children.
       
 10447         if (on) {
       
 10448             setAttribute(Qt::WA_NativeWindow);
       
 10449             d->enforceNativeChildren();
       
 10450         }
       
 10451 #endif
       
 10452         // fall through
       
 10453     case Qt::WA_OpaquePaintEvent:
       
 10454         d->updateIsOpaque();
       
 10455         break;
       
 10456     case Qt::WA_NoSystemBackground:
       
 10457         d->updateIsOpaque();
       
 10458         // fall through...
       
 10459     case Qt::WA_UpdatesDisabled:
       
 10460         d->updateSystemBackground();
       
 10461         break;
       
 10462     case Qt::WA_TransparentForMouseEvents:
       
 10463 #ifdef Q_WS_MAC
       
 10464         d->macUpdateIgnoreMouseEvents();
       
 10465 #endif
       
 10466         break;
       
 10467     case Qt::WA_InputMethodEnabled: {
       
 10468 #ifndef QT_NO_IM
       
 10469         QWidget *focusWidget = d->effectiveFocusWidget();
       
 10470         QInputContext *ic = focusWidget->d_func()->ic;
       
 10471         if (!ic && (!on || hasFocus()))
       
 10472             ic = focusWidget->d_func()->inputContext();
       
 10473         if (ic) {
       
 10474             if (on && hasFocus() && ic->focusWidget() != focusWidget && isEnabled()
       
 10475                 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
       
 10476                 ic->setFocusWidget(focusWidget);
       
 10477             } else if (!on && ic->focusWidget() == focusWidget) {
       
 10478                 ic->reset();
       
 10479                 ic->setFocusWidget(0);
       
 10480             }
       
 10481         }
       
 10482 #endif //QT_NO_IM
       
 10483         break;
       
 10484     }
       
 10485     case Qt::WA_WindowPropagation:
       
 10486         d->resolvePalette();
       
 10487         d->resolveFont();
       
 10488         d->resolveLocale();
       
 10489         break;
       
 10490 #ifdef Q_WS_X11
       
 10491     case Qt::WA_NoX11EventCompression:
       
 10492         if (!d->extra)
       
 10493             d->createExtra();
       
 10494         d->extra->compress_events = on;
       
 10495         break;
       
 10496     case Qt::WA_X11OpenGLOverlay:
       
 10497         d->updateIsOpaque();
       
 10498         break;
       
 10499 #endif
       
 10500     case Qt::WA_DontShowOnScreen: {
       
 10501         if (on && isVisible()) {
       
 10502             // Make sure we keep the current state and only hide the widget
       
 10503             // from the desktop. show_sys will only update platform specific
       
 10504             // attributes at this point.
       
 10505             d->hide_sys();
       
 10506 #ifdef Q_WS_QWS
       
 10507             // Release the region for this window from qws if the widget has
       
 10508             // been shown before the attribute was set.
       
 10509             if (QWSWindowSurface *surface = static_cast<QWSWindowSurface *>(windowSurface())) {
       
 10510                 QWidget::qwsDisplay()->requestRegion(surface->winId(), surface->key(),
       
 10511                                                      surface->permanentState(), QRegion());
       
 10512             }
       
 10513 #endif
       
 10514             d->show_sys();
       
 10515         }
       
 10516         break;
       
 10517     }
       
 10518 
       
 10519 #ifdef Q_WS_X11
       
 10520     case Qt::WA_X11NetWmWindowTypeDesktop:
       
 10521     case Qt::WA_X11NetWmWindowTypeDock:
       
 10522     case Qt::WA_X11NetWmWindowTypeToolBar:
       
 10523     case Qt::WA_X11NetWmWindowTypeMenu:
       
 10524     case Qt::WA_X11NetWmWindowTypeUtility:
       
 10525     case Qt::WA_X11NetWmWindowTypeSplash:
       
 10526     case Qt::WA_X11NetWmWindowTypeDialog:
       
 10527     case Qt::WA_X11NetWmWindowTypeDropDownMenu:
       
 10528     case Qt::WA_X11NetWmWindowTypePopupMenu:
       
 10529     case Qt::WA_X11NetWmWindowTypeToolTip:
       
 10530     case Qt::WA_X11NetWmWindowTypeNotification:
       
 10531     case Qt::WA_X11NetWmWindowTypeCombo:
       
 10532     case Qt::WA_X11NetWmWindowTypeDND:
       
 10533         if (testAttribute(Qt::WA_WState_Created))
       
 10534             d->setNetWmWindowTypes();
       
 10535         break;
       
 10536 #endif
       
 10537 
       
 10538     case Qt::WA_StaticContents:
       
 10539         if (QWidgetBackingStore *bs = d->maybeBackingStore()) {
       
 10540             if (on)
       
 10541                 bs->addStaticWidget(this);
       
 10542             else
       
 10543                 bs->removeStaticWidget(this);
       
 10544         }
       
 10545         break;
       
 10546     case Qt::WA_TranslucentBackground:
       
 10547         if (on) {
       
 10548             setAttribute(Qt::WA_NoSystemBackground);
       
 10549             d->updateIsTranslucent();
       
 10550         }
       
 10551 
       
 10552         break;
       
 10553     case Qt::WA_AcceptTouchEvents:
       
 10554 #if defined(Q_WS_WIN) || defined(Q_WS_MAC) || defined(Q_WS_S60)
       
 10555         if (on)
       
 10556             d->registerTouchWindow();
       
 10557 #endif
       
 10558         break;
       
 10559     default:
       
 10560         break;
       
 10561     }
       
 10562 }
       
 10563 
       
 10564 /*! \fn bool QWidget::testAttribute(Qt::WidgetAttribute attribute) const
       
 10565 
       
 10566   Returns true if attribute \a attribute is set on this widget;
       
 10567   otherwise returns false.
       
 10568 
       
 10569   \sa setAttribute()
       
 10570  */
       
 10571 bool QWidget::testAttribute_helper(Qt::WidgetAttribute attribute) const
       
 10572 {
       
 10573     Q_D(const QWidget);
       
 10574     const int x = attribute - 8*sizeof(uint);
       
 10575     const int int_off = x / (8*sizeof(uint));
       
 10576     return (d->high_attributes[int_off] & (1<<(x-(int_off*8*sizeof(uint)))));
       
 10577 }
       
 10578 
       
 10579 /*!
       
 10580   \property QWidget::windowOpacity
       
 10581 
       
 10582   \brief The level of opacity for the window.
       
 10583 
       
 10584   The valid range of opacity is from 1.0 (completely opaque) to
       
 10585   0.0 (completely transparent).
       
 10586 
       
 10587   By default the value of this property is 1.0.
       
 10588 
       
 10589   This feature is available on Embedded Linux, Mac OS X, Windows,
       
 10590   and X11 platforms that support the Composite extension.
       
 10591 
       
 10592   This feature is not available on Windows CE.
       
 10593 
       
 10594   Note that under X11 you need to have a composite manager running,
       
 10595   and the X11 specific _NET_WM_WINDOW_OPACITY atom needs to be
       
 10596   supported by the window manager you are using.
       
 10597 
       
 10598   \warning Changing this property from opaque to transparent might issue a
       
 10599   paint event that needs to be processed before the window is displayed
       
 10600   correctly. This affects mainly the use of QPixmap::grabWindow(). Also note
       
 10601   that semi-transparent windows update and resize significantly slower than
       
 10602   opaque windows.
       
 10603 
       
 10604   \sa setMask()
       
 10605 */
       
 10606 qreal QWidget::windowOpacity() const
       
 10607 {
       
 10608     Q_D(const QWidget);
       
 10609     return (isWindow() && d->maybeTopData()) ? d->maybeTopData()->opacity / 255. : 1.0;
       
 10610 }
       
 10611 
       
 10612 void QWidget::setWindowOpacity(qreal opacity)
       
 10613 {
       
 10614     Q_D(QWidget);
       
 10615     if (!isWindow())
       
 10616         return;
       
 10617 
       
 10618     opacity = qBound(qreal(0.0), opacity, qreal(1.0));
       
 10619     QTLWExtra *extra = d->topData();
       
 10620     extra->opacity = uint(opacity * 255);
       
 10621     setAttribute(Qt::WA_WState_WindowOpacitySet);
       
 10622 
       
 10623 #ifndef Q_WS_QWS
       
 10624     if (!testAttribute(Qt::WA_WState_Created))
       
 10625         return;
       
 10626 #endif
       
 10627 
       
 10628 #ifndef QT_NO_GRAPHICSVIEW
       
 10629     if (QGraphicsProxyWidget *proxy = graphicsProxyWidget()) {
       
 10630         // Avoid invalidating the cache if set.
       
 10631         if (proxy->cacheMode() == QGraphicsItem::NoCache)
       
 10632             proxy->update();
       
 10633         else if (QGraphicsScene *scene = proxy->scene())
       
 10634             scene->update(proxy->sceneBoundingRect());
       
 10635         return;
       
 10636     }
       
 10637 #endif
       
 10638 
       
 10639     d->setWindowOpacity_sys(opacity);
       
 10640 }
       
 10641 
       
 10642 /*!
       
 10643     \property QWidget::windowModified
       
 10644     \brief whether the document shown in the window has unsaved changes
       
 10645 
       
 10646     A modified window is a window whose content has changed but has
       
 10647     not been saved to disk. This flag will have different effects
       
 10648     varied by the platform. On Mac OS X the close button will have a
       
 10649     modified look; on other platforms, the window title will have an
       
 10650     '*' (asterisk).
       
 10651 
       
 10652     The window title must contain a "[*]" placeholder, which
       
 10653     indicates where the '*' should appear. Normally, it should appear
       
 10654     right after the file name (e.g., "document1.txt[*] - Text
       
 10655     Editor"). If the window isn't modified, the placeholder is simply
       
 10656     removed.
       
 10657 
       
 10658     Note that if a widget is set as modified, all its ancestors will
       
 10659     also be set as modified. However, if you call \c
       
 10660     {setWindowModified(false)} on a widget, this will not propagate to
       
 10661     its parent because other children of the parent might have been
       
 10662     modified.
       
 10663 
       
 10664     \sa windowTitle, {Application Example}, {SDI Example}, {MDI Example}
       
 10665 */
       
 10666 bool QWidget::isWindowModified() const
       
 10667 {
       
 10668     return testAttribute(Qt::WA_WindowModified);
       
 10669 }
       
 10670 
       
 10671 void QWidget::setWindowModified(bool mod)
       
 10672 {
       
 10673     Q_D(QWidget);
       
 10674     setAttribute(Qt::WA_WindowModified, mod);
       
 10675 
       
 10676 #ifndef Q_WS_MAC
       
 10677     if (!windowTitle().contains(QLatin1String("[*]")) && mod)
       
 10678         qWarning("QWidget::setWindowModified: The window title does not contain a '[*]' placeholder");
       
 10679 #endif
       
 10680     d->setWindowTitle_helper(windowTitle());
       
 10681     d->setWindowIconText_helper(windowIconText());
       
 10682 #ifdef Q_WS_MAC
       
 10683     d->setWindowModified_sys(mod);
       
 10684 #endif
       
 10685 
       
 10686     QEvent e(QEvent::ModifiedChange);
       
 10687     QApplication::sendEvent(this, &e);
       
 10688 }
       
 10689 
       
 10690 #ifndef QT_NO_TOOLTIP
       
 10691 /*!
       
 10692   \property QWidget::toolTip
       
 10693 
       
 10694   \brief the widget's tooltip
       
 10695 
       
 10696   Note that by default tooltips are only shown for widgets that are
       
 10697   children of the active window. You can change this behavior by
       
 10698   setting the attribute Qt::WA_AlwaysShowToolTips on the \e window,
       
 10699   not on the widget with the tooltip.
       
 10700 
       
 10701   If you want to control a tooltip's behavior, you can intercept the
       
 10702   event() function and catch the QEvent::ToolTip event (e.g., if you
       
 10703   want to customize the area for which the tooltip should be shown).
       
 10704 
       
 10705   By default, this property contains an empty string.
       
 10706 
       
 10707   \sa QToolTip statusTip whatsThis
       
 10708 */
       
 10709 void QWidget::setToolTip(const QString &s)
       
 10710 {
       
 10711     Q_D(QWidget);
       
 10712     d->toolTip = s;
       
 10713 
       
 10714     QEvent event(QEvent::ToolTipChange);
       
 10715     QApplication::sendEvent(this, &event);
       
 10716 }
       
 10717 
       
 10718 QString QWidget::toolTip() const
       
 10719 {
       
 10720     Q_D(const QWidget);
       
 10721     return d->toolTip;
       
 10722 }
       
 10723 #endif // QT_NO_TOOLTIP
       
 10724 
       
 10725 
       
 10726 #ifndef QT_NO_STATUSTIP
       
 10727 /*!
       
 10728   \property QWidget::statusTip
       
 10729   \brief the widget's status tip
       
 10730 
       
 10731   By default, this property contains an empty string.
       
 10732 
       
 10733   \sa toolTip whatsThis
       
 10734 */
       
 10735 void QWidget::setStatusTip(const QString &s)
       
 10736 {
       
 10737     Q_D(QWidget);
       
 10738     d->statusTip = s;
       
 10739 }
       
 10740 
       
 10741 QString QWidget::statusTip() const
       
 10742 {
       
 10743     Q_D(const QWidget);
       
 10744     return d->statusTip;
       
 10745 }
       
 10746 #endif // QT_NO_STATUSTIP
       
 10747 
       
 10748 #ifndef QT_NO_WHATSTHIS
       
 10749 /*!
       
 10750   \property QWidget::whatsThis
       
 10751 
       
 10752   \brief the widget's What's This help text.
       
 10753 
       
 10754   By default, this property contains an empty string.
       
 10755 
       
 10756   \sa QWhatsThis QWidget::toolTip QWidget::statusTip
       
 10757 */
       
 10758 void QWidget::setWhatsThis(const QString &s)
       
 10759 {
       
 10760     Q_D(QWidget);
       
 10761     d->whatsThis = s;
       
 10762 }
       
 10763 
       
 10764 QString QWidget::whatsThis() const
       
 10765 {
       
 10766     Q_D(const QWidget);
       
 10767     return d->whatsThis;
       
 10768 }
       
 10769 #endif // QT_NO_WHATSTHIS
       
 10770 
       
 10771 #ifndef QT_NO_ACCESSIBILITY
       
 10772 /*!
       
 10773   \property QWidget::accessibleName
       
 10774 
       
 10775   \brief the widget's name as seen by assistive technologies
       
 10776 
       
 10777   This property is used by accessible clients to identify, find, or announce
       
 10778   the widget for accessible clients.
       
 10779 
       
 10780   By default, this property contains an empty string.
       
 10781 
       
 10782   \sa QAccessibleInterface::text()
       
 10783 */
       
 10784 void QWidget::setAccessibleName(const QString &name)
       
 10785 {
       
 10786     Q_D(QWidget);
       
 10787     d->accessibleName = name;
       
 10788 }
       
 10789 
       
 10790 QString QWidget::accessibleName() const
       
 10791 {
       
 10792     Q_D(const QWidget);
       
 10793     return d->accessibleName;
       
 10794 }
       
 10795 
       
 10796 /*!
       
 10797   \property QWidget::accessibleDescription
       
 10798 
       
 10799   \brief the widget's description as seen by assistive technologies
       
 10800 
       
 10801   By default, this property contains an empty string.
       
 10802 
       
 10803   \sa QAccessibleInterface::text()
       
 10804 */
       
 10805 void QWidget::setAccessibleDescription(const QString &description)
       
 10806 {
       
 10807     Q_D(QWidget);
       
 10808     d->accessibleDescription = description;
       
 10809 }
       
 10810 
       
 10811 QString QWidget::accessibleDescription() const
       
 10812 {
       
 10813     Q_D(const QWidget);
       
 10814     return d->accessibleDescription;
       
 10815 }
       
 10816 #endif // QT_NO_ACCESSIBILITY
       
 10817 
       
 10818 #ifndef QT_NO_SHORTCUT
       
 10819 /*!
       
 10820     Adds a shortcut to Qt's shortcut system that watches for the given
       
 10821     \a key sequence in the given \a context. If the \a context is
       
 10822     Qt::ApplicationShortcut, the shortcut applies to the application as a
       
 10823     whole. Otherwise, it is either local to this widget, Qt::WidgetShortcut,
       
 10824     or to the window itself, Qt::WindowShortcut.
       
 10825 
       
 10826     If the same \a key sequence has been grabbed by several widgets,
       
 10827     when the \a key sequence occurs a QEvent::Shortcut event is sent
       
 10828     to all the widgets to which it applies in a non-deterministic
       
 10829     order, but with the ``ambiguous'' flag set to true.
       
 10830 
       
 10831     \warning You should not normally need to use this function;
       
 10832     instead create \l{QAction}s with the shortcut key sequences you
       
 10833     require (if you also want equivalent menu options and toolbar
       
 10834     buttons), or create \l{QShortcut}s if you just need key sequences.
       
 10835     Both QAction and QShortcut handle all the event filtering for you,
       
 10836     and provide signals which are triggered when the user triggers the
       
 10837     key sequence, so are much easier to use than this low-level
       
 10838     function.
       
 10839 
       
 10840     \sa releaseShortcut() setShortcutEnabled()
       
 10841 */
       
 10842 int QWidget::grabShortcut(const QKeySequence &key, Qt::ShortcutContext context)
       
 10843 {
       
 10844     Q_ASSERT(qApp);
       
 10845     if (key.isEmpty())
       
 10846         return 0;
       
 10847     setAttribute(Qt::WA_GrabbedShortcut);
       
 10848     return qApp->d_func()->shortcutMap.addShortcut(this, key, context);
       
 10849 }
       
 10850 
       
 10851 /*!
       
 10852     Removes the shortcut with the given \a id from Qt's shortcut
       
 10853     system. The widget will no longer receive QEvent::Shortcut events
       
 10854     for the shortcut's key sequence (unless it has other shortcuts
       
 10855     with the same key sequence).
       
 10856 
       
 10857     \warning You should not normally need to use this function since
       
 10858     Qt's shortcut system removes shortcuts automatically when their
       
 10859     parent widget is destroyed. It is best to use QAction or
       
 10860     QShortcut to handle shortcuts, since they are easier to use than
       
 10861     this low-level function. Note also that this is an expensive
       
 10862     operation.
       
 10863 
       
 10864     \sa grabShortcut() setShortcutEnabled()
       
 10865 */
       
 10866 void QWidget::releaseShortcut(int id)
       
 10867 {
       
 10868     Q_ASSERT(qApp);
       
 10869     if (id)
       
 10870         qApp->d_func()->shortcutMap.removeShortcut(id, this, 0);
       
 10871 }
       
 10872 
       
 10873 /*!
       
 10874     If \a enable is true, the shortcut with the given \a id is
       
 10875     enabled; otherwise the shortcut is disabled.
       
 10876 
       
 10877     \warning You should not normally need to use this function since
       
 10878     Qt's shortcut system enables/disables shortcuts automatically as
       
 10879     widgets become hidden/visible and gain or lose focus. It is best
       
 10880     to use QAction or QShortcut to handle shortcuts, since they are
       
 10881     easier to use than this low-level function.
       
 10882 
       
 10883     \sa grabShortcut() releaseShortcut()
       
 10884 */
       
 10885 void QWidget::setShortcutEnabled(int id, bool enable)
       
 10886 {
       
 10887     Q_ASSERT(qApp);
       
 10888     if (id)
       
 10889         qApp->d_func()->shortcutMap.setShortcutEnabled(enable, id, this, 0);
       
 10890 }
       
 10891 
       
 10892 /*!
       
 10893     \since 4.2
       
 10894 
       
 10895     If \a enable is true, auto repeat of the shortcut with the
       
 10896     given \a id is enabled; otherwise it is disabled.
       
 10897 
       
 10898     \sa grabShortcut() releaseShortcut()
       
 10899 */
       
 10900 void QWidget::setShortcutAutoRepeat(int id, bool enable)
       
 10901 {
       
 10902     Q_ASSERT(qApp);
       
 10903     if (id)
       
 10904         qApp->d_func()->shortcutMap.setShortcutAutoRepeat(enable, id, this, 0);
       
 10905 }
       
 10906 #endif // QT_NO_SHORTCUT
       
 10907 /*!
       
 10908     Updates the widget's micro focus.
       
 10909 
       
 10910     \sa QInputContext
       
 10911 */
       
 10912 void QWidget::updateMicroFocus()
       
 10913 {
       
 10914 #if !defined(QT_NO_IM) && (defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_OS_SYMBIAN))
       
 10915     Q_D(QWidget);
       
 10916     // and optimisation to update input context only it has already been created.
       
 10917     if (d->ic || qApp->d_func()->inputContext) {
       
 10918         QInputContext *ic = inputContext();
       
 10919         if (ic)
       
 10920             ic->update();
       
 10921     }
       
 10922 #endif
       
 10923 #ifndef QT_NO_ACCESSIBILITY
       
 10924     // ##### is this correct
       
 10925     QAccessible::updateAccessibility(this, 0, QAccessible::StateChanged);
       
 10926 #endif
       
 10927 }
       
 10928 
       
 10929 
       
 10930 #if defined (Q_WS_WIN)
       
 10931 /*!
       
 10932     Returns the window system handle of the widget, for low-level
       
 10933     access. Using this function is not portable.
       
 10934 
       
 10935     An HDC acquired with getDC() has to be released with releaseDC().
       
 10936 
       
 10937     \warning Using this function is not portable.
       
 10938 */
       
 10939 HDC QWidget::getDC() const
       
 10940 {
       
 10941     Q_D(const QWidget);
       
 10942     if (d->hd)
       
 10943         return (HDC) d->hd;
       
 10944     return GetDC(winId());
       
 10945 }
       
 10946 
       
 10947 /*!
       
 10948     Releases the HDC \a hdc acquired by a previous call to getDC().
       
 10949 
       
 10950     \warning Using this function is not portable.
       
 10951 */
       
 10952 void QWidget::releaseDC(HDC hdc) const
       
 10953 {
       
 10954     Q_D(const QWidget);
       
 10955     // If its the widgets own dc, it will be released elsewhere. If
       
 10956     // its a different HDC we release it and issue a warning if it
       
 10957     // fails.
       
 10958     if (hdc != d->hd && !ReleaseDC(winId(), hdc))
       
 10959         qErrnoWarning("QWidget::releaseDC(): failed to release HDC");
       
 10960 }
       
 10961 #else
       
 10962 /*!
       
 10963     Returns the window system handle of the widget, for low-level
       
 10964     access. Using this function is not portable.
       
 10965 
       
 10966     The HANDLE type varies with platform; see \c qwindowdefs.h for
       
 10967     details.
       
 10968 */
       
 10969 Qt::HANDLE QWidget::handle() const
       
 10970 {
       
 10971     Q_D(const QWidget);
       
 10972     if (!internalWinId() && testAttribute(Qt::WA_WState_Created))
       
 10973         (void)winId(); // enforce native window
       
 10974     return d->hd;
       
 10975 }
       
 10976 #endif
       
 10977 
       
 10978 
       
 10979 /*!
       
 10980     Raises this widget to the top of the parent widget's stack.
       
 10981 
       
 10982     After this call the widget will be visually in front of any
       
 10983     overlapping sibling widgets.
       
 10984 
       
 10985     \note When using activateWindow(), you can call this function to
       
 10986     ensure that the window is stacked on top.
       
 10987 
       
 10988     \sa lower(), stackUnder()
       
 10989 */
       
 10990 
       
 10991 void QWidget::raise()
       
 10992 {
       
 10993     Q_D(QWidget);
       
 10994     if (!isWindow()) {
       
 10995         QWidget *p = parentWidget();
       
 10996         const int parentChildCount = p->d_func()->children.size();
       
 10997         if (parentChildCount < 2)
       
 10998             return;
       
 10999         const int from = p->d_func()->children.indexOf(this);
       
 11000         Q_ASSERT(from >= 0);
       
 11001         // Do nothing if the widget is already in correct stacking order _and_ created.
       
 11002         if (from != parentChildCount -1)
       
 11003             p->d_func()->children.move(from, parentChildCount - 1);
       
 11004         if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
       
 11005             create();
       
 11006         else if (from == parentChildCount - 1)
       
 11007             return;
       
 11008 
       
 11009         QRegion region(rect());
       
 11010         d->subtractOpaqueSiblings(region);
       
 11011         d->invalidateBuffer(region);
       
 11012     }
       
 11013     if (testAttribute(Qt::WA_WState_Created))
       
 11014         d->raise_sys();
       
 11015 
       
 11016     QEvent e(QEvent::ZOrderChange);
       
 11017     QApplication::sendEvent(this, &e);
       
 11018 }
       
 11019 
       
 11020 /*!
       
 11021     Lowers the widget to the bottom of the parent widget's stack.
       
 11022 
       
 11023     After this call the widget will be visually behind (and therefore
       
 11024     obscured by) any overlapping sibling widgets.
       
 11025 
       
 11026     \sa raise(), stackUnder()
       
 11027 */
       
 11028 
       
 11029 void QWidget::lower()
       
 11030 {
       
 11031     Q_D(QWidget);
       
 11032     if (!isWindow()) {
       
 11033         QWidget *p = parentWidget();
       
 11034         const int parentChildCount = p->d_func()->children.size();
       
 11035         if (parentChildCount < 2)
       
 11036             return;
       
 11037         const int from = p->d_func()->children.indexOf(this);
       
 11038         Q_ASSERT(from >= 0);
       
 11039         // Do nothing if the widget is already in correct stacking order _and_ created.
       
 11040         if (from != 0)
       
 11041             p->d_func()->children.move(from, 0);
       
 11042         if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
       
 11043             create();
       
 11044         else if (from == 0)
       
 11045             return;
       
 11046     }
       
 11047     if (testAttribute(Qt::WA_WState_Created))
       
 11048         d->lower_sys();
       
 11049 
       
 11050     QEvent e(QEvent::ZOrderChange);
       
 11051     QApplication::sendEvent(this, &e);
       
 11052 }
       
 11053 
       
 11054 
       
 11055 /*!
       
 11056     Places the widget under \a w in the parent widget's stack.
       
 11057 
       
 11058     To make this work, the widget itself and \a w must be siblings.
       
 11059 
       
 11060     \sa raise(), lower()
       
 11061 */
       
 11062 void QWidget::stackUnder(QWidget* w)
       
 11063 {
       
 11064     Q_D(QWidget);
       
 11065     QWidget *p = parentWidget();
       
 11066     if (!w || isWindow() || p != w->parentWidget() || this == w)
       
 11067         return;
       
 11068     if (p) {
       
 11069         int from = p->d_func()->children.indexOf(this);
       
 11070         int to = p->d_func()->children.indexOf(w);
       
 11071         Q_ASSERT(from >= 0);
       
 11072         Q_ASSERT(to >= 0);
       
 11073         if (from < to)
       
 11074             --to;
       
 11075         // Do nothing if the widget is already in correct stacking order _and_ created.
       
 11076         if (from != to)
       
 11077             p->d_func()->children.move(from, to);
       
 11078         if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
       
 11079             create();
       
 11080         else if (from == to)
       
 11081             return;
       
 11082     }
       
 11083     if (testAttribute(Qt::WA_WState_Created))
       
 11084         d->stackUnder_sys(w);
       
 11085 
       
 11086     QEvent e(QEvent::ZOrderChange);
       
 11087     QApplication::sendEvent(this, &e);
       
 11088 }
       
 11089 
       
 11090 void QWidget::styleChange(QStyle&) { }
       
 11091 void QWidget::enabledChange(bool) { }  // compat
       
 11092 void QWidget::paletteChange(const QPalette &) { }  // compat
       
 11093 void QWidget::fontChange(const QFont &) { }  // compat
       
 11094 void QWidget::windowActivationChange(bool) { }  // compat
       
 11095 void QWidget::languageChange() { }  // compat
       
 11096 
       
 11097 
       
 11098 /*!
       
 11099     \enum QWidget::BackgroundOrigin
       
 11100 
       
 11101     \compat
       
 11102 
       
 11103     \value WidgetOrigin
       
 11104     \value ParentOrigin
       
 11105     \value WindowOrigin
       
 11106     \value AncestorOrigin
       
 11107 
       
 11108 */
       
 11109 
       
 11110 /*!
       
 11111     \fn bool QWidget::isVisibleToTLW() const
       
 11112 
       
 11113     Use isVisible() instead.
       
 11114 */
       
 11115 
       
 11116 /*!
       
 11117     \fn void QWidget::iconify()
       
 11118 
       
 11119     Use showMinimized() instead.
       
 11120 */
       
 11121 
       
 11122 /*!
       
 11123     \fn void QWidget::constPolish() const
       
 11124 
       
 11125     Use ensurePolished() instead.
       
 11126 */
       
 11127 
       
 11128 /*!
       
 11129     \fn void QWidget::reparent(QWidget *parent, Qt::WindowFlags f, const QPoint &p, bool showIt)
       
 11130 
       
 11131     Use setParent() to change the parent or the widget's widget flags;
       
 11132     use move() to move the widget, and use show() to show the widget.
       
 11133 */
       
 11134 
       
 11135 /*!
       
 11136     \fn void QWidget::reparent(QWidget *parent, const QPoint &p, bool showIt)
       
 11137 
       
 11138     Use setParent() to change the parent; use move() to move the
       
 11139     widget, and use show() to show the widget.
       
 11140 */
       
 11141 
       
 11142 /*!
       
 11143     \fn void QWidget::recreate(QWidget *parent, Qt::WindowFlags f, const QPoint & p, bool showIt)
       
 11144 
       
 11145     Use setParent() to change the parent or the widget's widget flags;
       
 11146     use move() to move the widget, and use show() to show the widget.
       
 11147 */
       
 11148 
       
 11149 /*!
       
 11150     \fn bool QWidget::hasMouse() const
       
 11151 
       
 11152     Use testAttribute(Qt::WA_UnderMouse) instead.
       
 11153 */
       
 11154 
       
 11155 /*!
       
 11156     \fn bool QWidget::ownCursor() const
       
 11157 
       
 11158     Use testAttribute(Qt::WA_SetCursor) instead.
       
 11159 */
       
 11160 
       
 11161 /*!
       
 11162     \fn bool QWidget::ownFont() const
       
 11163 
       
 11164     Use testAttribute(Qt::WA_SetFont) instead.
       
 11165 */
       
 11166 
       
 11167 /*!
       
 11168     \fn void QWidget::unsetFont()
       
 11169 
       
 11170     Use setFont(QFont()) instead.
       
 11171 */
       
 11172 
       
 11173 /*!
       
 11174     \fn bool QWidget::ownPalette() const
       
 11175 
       
 11176     Use testAttribute(Qt::WA_SetPalette) instead.
       
 11177 */
       
 11178 
       
 11179 /*!
       
 11180     \fn void QWidget::unsetPalette()
       
 11181 
       
 11182     Use setPalette(QPalette()) instead.
       
 11183 */
       
 11184 
       
 11185 /*!
       
 11186     \fn void QWidget::setEraseColor(const QColor &color)
       
 11187 
       
 11188     Use the palette instead.
       
 11189 
       
 11190     \oldcode
       
 11191     widget->setEraseColor(color);
       
 11192     \newcode
       
 11193     QPalette palette;
       
 11194     palette.setColor(widget->backgroundRole(), color);
       
 11195     widget->setPalette(palette);
       
 11196     \endcode
       
 11197 */
       
 11198 
       
 11199 /*!
       
 11200     \fn void QWidget::setErasePixmap(const QPixmap &pixmap)
       
 11201 
       
 11202     Use the palette instead.
       
 11203 
       
 11204     \oldcode
       
 11205     widget->setErasePixmap(pixmap);
       
 11206     \newcode
       
 11207     QPalette palette;
       
 11208     palette.setBrush(widget->backgroundRole(), QBrush(pixmap));
       
 11209     widget->setPalette(palette);
       
 11210     \endcode
       
 11211 */
       
 11212 
       
 11213 /*!
       
 11214     \fn void QWidget::setPaletteForegroundColor(const QColor &color)
       
 11215 
       
 11216     Use the palette directly.
       
 11217 
       
 11218     \oldcode
       
 11219     widget->setPaletteForegroundColor(color);
       
 11220     \newcode
       
 11221     QPalette palette;
       
 11222     palette.setColor(widget->foregroundRole(), color);
       
 11223     widget->setPalette(palette);
       
 11224     \endcode
       
 11225 */
       
 11226 
       
 11227 /*!
       
 11228     \fn void QWidget::setPaletteBackgroundColor(const QColor &color)
       
 11229 
       
 11230     Use the palette directly.
       
 11231 
       
 11232     \oldcode
       
 11233     widget->setPaletteBackgroundColor(color);
       
 11234     \newcode
       
 11235     QPalette palette;
       
 11236     palette.setColor(widget->backgroundRole(), color);
       
 11237     widget->setPalette(palette);
       
 11238     \endcode
       
 11239 */
       
 11240 
       
 11241 /*!
       
 11242     \fn void QWidget::setPaletteBackgroundPixmap(const QPixmap &pixmap)
       
 11243 
       
 11244     Use the palette directly.
       
 11245 
       
 11246     \oldcode
       
 11247     widget->setPaletteBackgroundPixmap(pixmap);
       
 11248     \newcode
       
 11249     QPalette palette;
       
 11250     palette.setBrush(widget->backgroundRole(), QBrush(pixmap));
       
 11251     widget->setPalette(palette);
       
 11252     \endcode
       
 11253 */
       
 11254 
       
 11255 /*!
       
 11256     \fn void QWidget::setBackgroundPixmap(const QPixmap &pixmap)
       
 11257 
       
 11258     Use the palette instead.
       
 11259 
       
 11260     \oldcode
       
 11261     widget->setBackgroundPixmap(pixmap);
       
 11262     \newcode
       
 11263     QPalette palette;
       
 11264     palette.setBrush(widget->backgroundRole(), QBrush(pixmap));
       
 11265     widget->setPalette(palette);
       
 11266     \endcode
       
 11267 */
       
 11268 
       
 11269 /*!
       
 11270     \fn void QWidget::setBackgroundColor(const QColor &color)
       
 11271 
       
 11272     Use the palette instead.
       
 11273 
       
 11274     \oldcode
       
 11275     widget->setBackgroundColor(color);
       
 11276     \newcode
       
 11277     QPalette palette;
       
 11278     palette.setColor(widget->backgroundRole(), color);
       
 11279     widget->setPalette(palette);
       
 11280     \endcode
       
 11281 */
       
 11282 
       
 11283 /*!
       
 11284     \fn QColorGroup QWidget::colorGroup() const
       
 11285 
       
 11286     Use QColorGroup(palette()) instead.
       
 11287 */
       
 11288 
       
 11289 /*!
       
 11290     \fn QWidget *QWidget::parentWidget(bool sameWindow) const
       
 11291 
       
 11292     Use the no-argument overload instead.
       
 11293 */
       
 11294 
       
 11295 /*!
       
 11296     \fn void QWidget::setKeyCompression(bool b)
       
 11297 
       
 11298     Use setAttribute(Qt::WA_KeyCompression, b) instead.
       
 11299 */
       
 11300 
       
 11301 /*!
       
 11302     \fn void QWidget::setFont(const QFont &f, bool b)
       
 11303 
       
 11304     Use the single-argument overload instead.
       
 11305 */
       
 11306 
       
 11307 /*!
       
 11308     \fn void QWidget::setPalette(const QPalette &p, bool b)
       
 11309 
       
 11310     Use the single-argument overload instead.
       
 11311 */
       
 11312 
       
 11313 /*!
       
 11314     \fn void QWidget::setBackgroundOrigin(BackgroundOrigin background)
       
 11315 
       
 11316     \obsolete
       
 11317 */
       
 11318 
       
 11319 /*!
       
 11320     \fn BackgroundOrigin QWidget::backgroundOrigin() const
       
 11321 
       
 11322     \obsolete
       
 11323 
       
 11324     Always returns \c WindowOrigin.
       
 11325 */
       
 11326 
       
 11327 /*!
       
 11328     \fn QPoint QWidget::backgroundOffset() const
       
 11329 
       
 11330     \obsolete
       
 11331 
       
 11332     Always returns QPoint().
       
 11333 */
       
 11334 
       
 11335 /*!
       
 11336     \fn void QWidget::repaint(bool b)
       
 11337 
       
 11338     The boolean parameter \a b is ignored. Use the no-argument overload instead.
       
 11339 */
       
 11340 
       
 11341 /*!
       
 11342     \fn void QWidget::repaint(int x, int y, int w, int h, bool b)
       
 11343 
       
 11344     The boolean parameter \a b is ignored. Use the four-argument overload instead.
       
 11345 */
       
 11346 
       
 11347 /*!
       
 11348     \fn void QWidget::repaint(const QRect &r, bool b)
       
 11349 
       
 11350     The boolean parameter \a b is ignored. Use the single rect-argument overload instead.
       
 11351 */
       
 11352 
       
 11353 /*!
       
 11354     \fn void QWidget::repaint(const QRegion &rgn, bool b)
       
 11355 
       
 11356     The boolean parameter \a b is ignored. Use the single region-argument overload instead.
       
 11357 */
       
 11358 
       
 11359 /*!
       
 11360     \fn void QWidget::erase()
       
 11361 
       
 11362     Drawing may only take place in a QPaintEvent. Overload
       
 11363     paintEvent() to do your erasing and call update() to schedule a
       
 11364     replaint whenever necessary. See also QPainter.
       
 11365 */
       
 11366 
       
 11367 /*!
       
 11368     \fn void QWidget::erase(int x, int y, int w, int h)
       
 11369 
       
 11370     Drawing may only take place in a QPaintEvent. Overload
       
 11371     paintEvent() to do your erasing and call update() to schedule a
       
 11372     replaint whenever necessary. See also QPainter.
       
 11373 */
       
 11374 
       
 11375 /*!
       
 11376     \fn void QWidget::erase(const QRect &rect)
       
 11377 
       
 11378     Drawing may only take place in a QPaintEvent. Overload
       
 11379     paintEvent() to do your erasing and call update() to schedule a
       
 11380     replaint whenever necessary. See also QPainter.
       
 11381 */
       
 11382 
       
 11383 /*!
       
 11384     \fn void QWidget::drawText(const QPoint &p, const QString &s)
       
 11385 
       
 11386     Drawing may only take place in a QPaintEvent. Overload
       
 11387     paintEvent() to do your drawing and call update() to schedule a
       
 11388     replaint whenever necessary. See also QPainter.
       
 11389 */
       
 11390 
       
 11391 /*!
       
 11392     \fn void QWidget::drawText(int x, int y, const QString &s)
       
 11393 
       
 11394     Drawing may only take place in a QPaintEvent. Overload
       
 11395     paintEvent() to do your drawing and call update() to schedule a
       
 11396     replaint whenever necessary. See also QPainter.
       
 11397 */
       
 11398 
       
 11399 /*!
       
 11400     \fn QWidget *QWidget::childAt(const QPoint &p, bool includeThis) const
       
 11401 
       
 11402     Use the single point argument overload instead.
       
 11403 */
       
 11404 
       
 11405 /*!
       
 11406     \fn void QWidget::setCaption(const QString &c)
       
 11407 
       
 11408     Use setWindowTitle() instead.
       
 11409 */
       
 11410 
       
 11411 /*!
       
 11412     \fn void QWidget::setIcon(const QPixmap &i)
       
 11413 
       
 11414     Use setWindowIcon() instead.
       
 11415 */
       
 11416 
       
 11417 /*!
       
 11418     \fn void QWidget::setIconText(const QString &it)
       
 11419 
       
 11420     Use setWindowIconText() instead.
       
 11421 */
       
 11422 
       
 11423 /*!
       
 11424     \fn QString QWidget::caption() const
       
 11425 
       
 11426     Use windowTitle() instead.
       
 11427 */
       
 11428 
       
 11429 /*!
       
 11430     \fn QString QWidget::iconText() const
       
 11431 
       
 11432     Use windowIconText() instead.
       
 11433 */
       
 11434 
       
 11435 /*!
       
 11436     \fn bool QWidget::isTopLevel() const
       
 11437     \obsolete
       
 11438 
       
 11439     Use isWindow() instead.
       
 11440 */
       
 11441 
       
 11442 /*!
       
 11443     \fn bool QWidget::isRightToLeft() const
       
 11444     \internal
       
 11445 */
       
 11446 
       
 11447 /*!
       
 11448     \fn bool QWidget::isLeftToRight() const
       
 11449     \internal
       
 11450 */
       
 11451 
       
 11452 /*!
       
 11453     \fn void QWidget::setInputMethodEnabled(bool enabled)
       
 11454 
       
 11455     Use setAttribute(Qt::WA_InputMethodEnabled, \a enabled) instead.
       
 11456 */
       
 11457 
       
 11458 /*!
       
 11459     \fn bool QWidget::isInputMethodEnabled() const
       
 11460 
       
 11461     Use testAttribute(Qt::WA_InputMethodEnabled) instead.
       
 11462 */
       
 11463 
       
 11464 /*!
       
 11465     \fn void QWidget::setActiveWindow()
       
 11466 
       
 11467     Use activateWindow() instead.
       
 11468 */
       
 11469 
       
 11470 /*!
       
 11471     \fn bool QWidget::isShown() const
       
 11472 
       
 11473     Use !isHidden() instead (notice the exclamation mark), or use isVisible() to check whether the widget is visible.
       
 11474 */
       
 11475 
       
 11476 /*!
       
 11477     \fn bool QWidget::isDialog() const
       
 11478 
       
 11479     Use windowType() == Qt::Dialog instead.
       
 11480 */
       
 11481 
       
 11482 /*!
       
 11483     \fn bool QWidget::isPopup() const
       
 11484 
       
 11485     Use windowType() == Qt::Popup instead.
       
 11486 */
       
 11487 
       
 11488 /*!
       
 11489     \fn bool QWidget::isDesktop() const
       
 11490 
       
 11491     Use windowType() == Qt::Desktop instead.
       
 11492 */
       
 11493 
       
 11494 /*!
       
 11495     \fn void QWidget::polish()
       
 11496 
       
 11497     Use ensurePolished() instead.
       
 11498 */
       
 11499 
       
 11500 /*!
       
 11501     \fn QWidget *QWidget::childAt(int x, int y, bool includeThis) const
       
 11502 
       
 11503     Use the childAt() overload that doesn't have an \a includeThis parameter.
       
 11504 
       
 11505     \oldcode
       
 11506         return widget->childAt(x, y, true);
       
 11507     \newcode
       
 11508         QWidget *child = widget->childAt(x, y, true);
       
 11509         if (child)
       
 11510             return child;
       
 11511         if (widget->rect().contains(x, y))
       
 11512             return widget;
       
 11513     \endcode
       
 11514 */
       
 11515 
       
 11516 /*!
       
 11517     \fn void QWidget::setSizePolicy(QSizePolicy::Policy hor, QSizePolicy::Policy ver, bool hfw)
       
 11518     \compat
       
 11519 
       
 11520     Use the \l sizePolicy property and heightForWidth() function instead.
       
 11521 */
       
 11522 
       
 11523 /*!
       
 11524     \fn bool QWidget::isUpdatesEnabled() const
       
 11525     \compat
       
 11526 
       
 11527     Use the \l updatesEnabled property instead.
       
 11528 */
       
 11529 
       
 11530 /*!
       
 11531      \macro QWIDGETSIZE_MAX
       
 11532      \relates QWidget
       
 11533 
       
 11534      Defines the maximum size for a QWidget object.
       
 11535 
       
 11536      The largest allowed size for a widget is QSize(QWIDGETSIZE_MAX,
       
 11537      QWIDGETSIZE_MAX), i.e. QSize (16777215,16777215).
       
 11538 
       
 11539      \sa QWidget::setMaximumSize()
       
 11540 */
       
 11541 
       
 11542 /*!
       
 11543     \fn QWidget::setupUi(QWidget *widget)
       
 11544 
       
 11545     Sets up the user interface for the specified \a widget.
       
 11546 
       
 11547     \note This function is available with widgets that derive from user
       
 11548     interface descriptions created using \l{uic}.
       
 11549 
       
 11550     \sa {Using a Designer UI File in Your Application}
       
 11551 */
       
 11552 
       
 11553 QRect QWidgetPrivate::frameStrut() const
       
 11554 {
       
 11555     Q_Q(const QWidget);
       
 11556     if (!q->isWindow() || (q->windowType() == Qt::Desktop) || q->testAttribute(Qt::WA_DontShowOnScreen)) {
       
 11557         // x2 = x1 + w - 1, so w/h = 1
       
 11558         return QRect(0, 0, 1, 1);
       
 11559     }
       
 11560 
       
 11561     if (data.fstrut_dirty
       
 11562 #ifndef Q_WS_WIN
       
 11563         // ### Fix properly for 4.3
       
 11564         && q->isVisible()
       
 11565 #endif
       
 11566         && q->testAttribute(Qt::WA_WState_Created))
       
 11567         const_cast<QWidgetPrivate *>(this)->updateFrameStrut();
       
 11568 
       
 11569     return maybeTopData() ? maybeTopData()->frameStrut : QRect();
       
 11570 }
       
 11571 
       
 11572 #ifdef QT_KEYPAD_NAVIGATION
       
 11573 /*!
       
 11574     \internal
       
 11575 
       
 11576     Changes the focus  from the current focusWidget to a widget in
       
 11577     the \a direction.
       
 11578 
       
 11579     Returns true, if there was a widget in that direction
       
 11580 */
       
 11581 bool QWidgetPrivate::navigateToDirection(Direction direction)
       
 11582 {
       
 11583     QWidget *targetWidget = widgetInNavigationDirection(direction);
       
 11584     if (targetWidget)
       
 11585         targetWidget->setFocus();
       
 11586     return (targetWidget != 0);
       
 11587 }
       
 11588 
       
 11589 /*!
       
 11590     \internal
       
 11591 
       
 11592     Searches for a widget that is positioned in the \a direction, starting
       
 11593     from the current focusWidget.
       
 11594 
       
 11595     Returns the pointer to a found widget or 0, if there was no widget in
       
 11596     that direction.
       
 11597 */
       
 11598 QWidget *QWidgetPrivate::widgetInNavigationDirection(Direction direction)
       
 11599 {
       
 11600     const QWidget *sourceWidget = QApplication::focusWidget();
       
 11601     if (!sourceWidget)
       
 11602         return 0;
       
 11603     const QRect sourceRect = sourceWidget->rect().translated(sourceWidget->mapToGlobal(QPoint()));
       
 11604     const int sourceX =
       
 11605             (direction == DirectionNorth || direction == DirectionSouth) ?
       
 11606                 (sourceRect.left() + (sourceRect.right() - sourceRect.left()) / 2)
       
 11607                 :(direction == DirectionEast ? sourceRect.right() : sourceRect.left());
       
 11608     const int sourceY =
       
 11609             (direction == DirectionEast || direction == DirectionWest) ?
       
 11610                 (sourceRect.top() + (sourceRect.bottom() - sourceRect.top()) / 2)
       
 11611                 :(direction == DirectionSouth ? sourceRect.bottom() : sourceRect.top());
       
 11612     const QPoint sourcePoint(sourceX, sourceY);
       
 11613     const QPoint sourceCenter = sourceRect.center();
       
 11614     const QWidget *sourceWindow = sourceWidget->window();
       
 11615 
       
 11616     QWidget *targetWidget = 0;
       
 11617     int shortestDistance = INT_MAX;
       
 11618     foreach(QWidget *targetCandidate, QApplication::allWidgets()) {
       
 11619 
       
 11620         const QRect targetCandidateRect = targetCandidate->rect().translated(targetCandidate->mapToGlobal(QPoint()));
       
 11621 
       
 11622         // For focus proxies, the child widget handling the focus can have keypad navigation focus,
       
 11623         // but the owner of the proxy cannot.
       
 11624         // Additionally, empty widgets should be ignored.
       
 11625         if (targetCandidate->focusProxy() || targetCandidateRect.isEmpty())
       
 11626             continue;
       
 11627 
       
 11628         // Only navigate to a target widget that...
       
 11629         if (       targetCandidate != sourceWidget
       
 11630                    // ...takes the focus,
       
 11631                 && targetCandidate->focusPolicy() & Qt::TabFocus
       
 11632                    // ...is above if DirectionNorth,
       
 11633                 && !(direction == DirectionNorth && targetCandidateRect.bottom() > sourceRect.top())
       
 11634                    // ...is on the right if DirectionEast,
       
 11635                 && !(direction == DirectionEast  && targetCandidateRect.left()   < sourceRect.right())
       
 11636                    // ...is below if DirectionSouth,
       
 11637                 && !(direction == DirectionSouth && targetCandidateRect.top()    < sourceRect.bottom())
       
 11638                    // ...is on the left if DirectionWest,
       
 11639                 && !(direction == DirectionWest  && targetCandidateRect.right()  > sourceRect.left())
       
 11640                    // ...is enabled,
       
 11641                 && targetCandidate->isEnabled()
       
 11642                    // ...is visible,
       
 11643                 && targetCandidate->isVisible()
       
 11644                    // ...is in the same window,
       
 11645                 && targetCandidate->window() == sourceWindow) {
       
 11646             const int targetCandidateDistance = pointToRect(sourcePoint, targetCandidateRect);
       
 11647             if (targetCandidateDistance < shortestDistance) {
       
 11648                 shortestDistance = targetCandidateDistance;
       
 11649                 targetWidget = targetCandidate;
       
 11650             }
       
 11651         }
       
 11652     }
       
 11653     return targetWidget;
       
 11654 }
       
 11655 
       
 11656 /*!
       
 11657     \internal
       
 11658 
       
 11659     Tells us if it there is currently a reachable widget by keypad navigation in
       
 11660     a certain \a orientation.
       
 11661     If no navigation is possible, occuring key events in that \a orientation may
       
 11662     be used to interact with the value in the focussed widget, even though it
       
 11663     currently has not the editFocus.
       
 11664 
       
 11665     \sa QWidgetPrivate::widgetInNavigationDirection(), QWidget::hasEditFocus()
       
 11666 */
       
 11667 bool QWidgetPrivate::canKeypadNavigate(Qt::Orientation orientation)
       
 11668 {
       
 11669     return orientation == Qt::Horizontal?
       
 11670             (QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionEast)
       
 11671                     || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionWest))
       
 11672             :(QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionNorth)
       
 11673                     || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionSouth));
       
 11674 }
       
 11675 /*!
       
 11676     \internal
       
 11677 
       
 11678     Checks, if the \a widget is inside a QTabWidget. If is is inside
       
 11679     one, left/right key events will be used to switch between tabs in keypad
       
 11680     navigation. If there is no QTabWidget, the horizontal key events can be used
       
 11681 to
       
 11682     interact with the value in the focussed widget, even though it currently has
       
 11683     not the editFocus.
       
 11684 
       
 11685     \sa QWidget::hasEditFocus()
       
 11686 */
       
 11687 bool QWidgetPrivate::inTabWidget(QWidget *widget)
       
 11688 {
       
 11689     for (QWidget *tabWidget = widget; tabWidget; tabWidget = tabWidget->parentWidget())
       
 11690         if (qobject_cast<const QTabWidget*>(tabWidget))
       
 11691             return true;
       
 11692     return false;
       
 11693 }
       
 11694 #endif
       
 11695 
       
 11696 /*!
       
 11697     \preliminary
       
 11698     \since 4.2
       
 11699     \obsolete
       
 11700 
       
 11701     Sets the window surface to be the \a surface specified.
       
 11702     The QWidget takes will ownership of the \a surface.
       
 11703     widget itself is deleted.
       
 11704 */
       
 11705 void QWidget::setWindowSurface(QWindowSurface *surface)
       
 11706 {
       
 11707     // ### createWinId() ??
       
 11708 
       
 11709 #ifndef Q_BACKINGSTORE_SUBSURFACES
       
 11710     if (!isTopLevel())
       
 11711         return;
       
 11712 #endif
       
 11713 
       
 11714     Q_D(QWidget);
       
 11715 
       
 11716     QTLWExtra *topData = d->topData();
       
 11717     if (topData->windowSurface == surface)
       
 11718         return;
       
 11719 
       
 11720     QWindowSurface *oldSurface = topData->windowSurface;
       
 11721     delete topData->windowSurface;
       
 11722     topData->windowSurface = surface;
       
 11723 
       
 11724     QWidgetBackingStore *bs = d->maybeBackingStore();
       
 11725     if (!bs)
       
 11726         return;
       
 11727 
       
 11728     if (isTopLevel()) {
       
 11729         if (bs->windowSurface != oldSurface && bs->windowSurface != surface)
       
 11730             delete bs->windowSurface;
       
 11731         bs->windowSurface = surface;
       
 11732     }
       
 11733 #ifdef Q_BACKINGSTORE_SUBSURFACES
       
 11734     else {
       
 11735         bs->subSurfaces.append(surface);
       
 11736     }
       
 11737     bs->subSurfaces.removeOne(oldSurface);
       
 11738 #endif
       
 11739 }
       
 11740 
       
 11741 /*!
       
 11742     \preliminary
       
 11743     \since 4.2
       
 11744 
       
 11745     Returns the QWindowSurface this widget will be drawn into.
       
 11746 */
       
 11747 QWindowSurface *QWidget::windowSurface() const
       
 11748 {
       
 11749     Q_D(const QWidget);
       
 11750     QTLWExtra *extra = d->maybeTopData();
       
 11751     if (extra && extra->windowSurface)
       
 11752         return extra->windowSurface;
       
 11753 
       
 11754     QWidgetBackingStore *bs = d->maybeBackingStore();
       
 11755 
       
 11756 #ifdef Q_BACKINGSTORE_SUBSURFACES
       
 11757     if (bs && bs->subSurfaces.isEmpty())
       
 11758         return bs->windowSurface;
       
 11759 
       
 11760     if (!isTopLevel()) {
       
 11761         const QWidget *w = parentWidget();
       
 11762         while (w) {
       
 11763             QTLWExtra *extra = w->d_func()->maybeTopData();
       
 11764             if (extra && extra->windowSurface)
       
 11765                 return extra->windowSurface;
       
 11766             if (w->isTopLevel())
       
 11767                 break;
       
 11768             w = w->parentWidget();
       
 11769         }
       
 11770     }
       
 11771 #endif // Q_BACKINGSTORE_SUBSURFACES
       
 11772 
       
 11773     return bs ? bs->windowSurface : 0;
       
 11774 }
       
 11775 
       
 11776 void QWidgetPrivate::getLayoutItemMargins(int *left, int *top, int *right, int *bottom) const
       
 11777 {
       
 11778     if (left)
       
 11779         *left = (int)leftLayoutItemMargin;
       
 11780     if (top)
       
 11781         *top = (int)topLayoutItemMargin;
       
 11782     if (right)
       
 11783         *right = (int)rightLayoutItemMargin;
       
 11784     if (bottom)
       
 11785         *bottom = (int)bottomLayoutItemMargin;
       
 11786 }
       
 11787 
       
 11788 void QWidgetPrivate::setLayoutItemMargins(int left, int top, int right, int bottom)
       
 11789 {
       
 11790     if (leftLayoutItemMargin == left
       
 11791         && topLayoutItemMargin == top
       
 11792         && rightLayoutItemMargin == right
       
 11793         && bottomLayoutItemMargin == bottom)
       
 11794         return;
       
 11795 
       
 11796     Q_Q(QWidget);
       
 11797     leftLayoutItemMargin = (signed char)left;
       
 11798     topLayoutItemMargin = (signed char)top;
       
 11799     rightLayoutItemMargin = (signed char)right;
       
 11800     bottomLayoutItemMargin = (signed char)bottom;
       
 11801     q->updateGeometry();
       
 11802 }
       
 11803 
       
 11804 void QWidgetPrivate::setLayoutItemMargins(QStyle::SubElement element, const QStyleOption *opt)
       
 11805 {
       
 11806     Q_Q(QWidget);
       
 11807     QStyleOption myOpt;
       
 11808     if (!opt) {
       
 11809         myOpt.initFrom(q);
       
 11810         myOpt.rect.setRect(0, 0, 32768, 32768);     // arbitrary
       
 11811         opt = &myOpt;
       
 11812     }
       
 11813 
       
 11814     QRect liRect = q->style()->subElementRect(element, opt, q);
       
 11815     if (liRect.isValid()) {
       
 11816         leftLayoutItemMargin = (signed char)(opt->rect.left() - liRect.left());
       
 11817         topLayoutItemMargin = (signed char)(opt->rect.top() - liRect.top());
       
 11818         rightLayoutItemMargin = (signed char)(liRect.right() - opt->rect.right());
       
 11819         bottomLayoutItemMargin = (signed char)(liRect.bottom() - opt->rect.bottom());
       
 11820     } else {
       
 11821         leftLayoutItemMargin = 0;
       
 11822         topLayoutItemMargin = 0;
       
 11823         rightLayoutItemMargin = 0;
       
 11824         bottomLayoutItemMargin = 0;
       
 11825     }
       
 11826 }
       
 11827 // resets the Qt::WA_QuitOnClose attribute to the default value for transient widgets.
       
 11828 void QWidgetPrivate::adjustQuitOnCloseAttribute()
       
 11829 {
       
 11830     Q_Q(QWidget);
       
 11831 
       
 11832     if (!q->parentWidget()) {
       
 11833         Qt::WindowType type = q->windowType();
       
 11834         if (type == Qt::Widget || type == Qt::SubWindow)
       
 11835             type = Qt::Window;
       
 11836         if (type != Qt::Widget && type != Qt::Window && type != Qt::Dialog)
       
 11837             q->setAttribute(Qt::WA_QuitOnClose, false);
       
 11838     }
       
 11839 }
       
 11840 
       
 11841 
       
 11842 
       
 11843 Q_GUI_EXPORT QWidgetData *qt_qwidget_data(QWidget *widget)
       
 11844 {
       
 11845     return widget->data;
       
 11846 }
       
 11847 
       
 11848 Q_GUI_EXPORT QWidgetPrivate *qt_widget_private(QWidget *widget)
       
 11849 {
       
 11850     return widget->d_func();
       
 11851 }
       
 11852 
       
 11853 
       
 11854 #ifndef QT_NO_GRAPHICSVIEW
       
 11855 /*!
       
 11856    \since 4.5
       
 11857 
       
 11858    Returns the proxy widget for the corresponding embedded widget in a graphics
       
 11859    view; otherwise returns 0.
       
 11860 
       
 11861    \sa QGraphicsProxyWidget::createProxyForChildWidget(),
       
 11862        QGraphicsScene::addWidget()
       
 11863  */
       
 11864 QGraphicsProxyWidget *QWidget::graphicsProxyWidget() const
       
 11865 {
       
 11866     Q_D(const QWidget);
       
 11867     if (d->extra) {
       
 11868         return d->extra->proxyWidget;
       
 11869     }
       
 11870     return 0;
       
 11871 }
       
 11872 #endif
       
 11873 
       
 11874 
       
 11875 /*!
       
 11876     \typedef QWidgetList
       
 11877     \relates QWidget
       
 11878 
       
 11879     Synonym for QList<QWidget *>.
       
 11880 */
       
 11881 
       
 11882 /*!
       
 11883     Subscribes the widget to a given \a gesture with specific \a flags.
       
 11884 
       
 11885     \sa ungrabGesture(), QGestureEvent
       
 11886     \since 4.6
       
 11887 */
       
 11888 void QWidget::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags)
       
 11889 {
       
 11890     Q_D(QWidget);
       
 11891     d->gestureContext.insert(gesture, flags);
       
 11892     (void)QGestureManager::instance(); // create a gesture manager
       
 11893 }
       
 11894 
       
 11895 /*!
       
 11896     Unsubscribes the widget from a given \a gesture type
       
 11897 
       
 11898     \sa grabGesture(), QGestureEvent
       
 11899     \since 4.6
       
 11900 */
       
 11901 void QWidget::ungrabGesture(Qt::GestureType gesture)
       
 11902 {
       
 11903     Q_D(QWidget);
       
 11904     if (d->gestureContext.remove(gesture)) {
       
 11905         QGestureManager *manager = QGestureManager::instance();
       
 11906         manager->cleanupCachedGestures(this, gesture);
       
 11907     }
       
 11908 }
       
 11909 
       
 11910 
       
 11911 /*!
       
 11912     \typedef WId
       
 11913     \relates QWidget
       
 11914 
       
 11915     Platform dependent window identifier.
       
 11916 */
       
 11917 
       
 11918 /*!
       
 11919     \fn void QWidget::destroy(bool destroyWindow, bool destroySubWindows)
       
 11920 
       
 11921     Frees up window system resources. Destroys the widget window if \a
       
 11922     destroyWindow is true.
       
 11923 
       
 11924     destroy() calls itself recursively for all the child widgets,
       
 11925     passing \a destroySubWindows for the \a destroyWindow parameter.
       
 11926     To have more control over destruction of subwidgets, destroy
       
 11927     subwidgets selectively first.
       
 11928 
       
 11929     This function is usually called from the QWidget destructor.
       
 11930 */
       
 11931 
       
 11932 /*!
       
 11933     \fn QPaintEngine *QWidget::paintEngine() const
       
 11934 
       
 11935     Returns the widget's paint engine.
       
 11936 
       
 11937     Note that this function should not be called explicitly by the
       
 11938     user, since it's meant for reimplementation purposes only. The
       
 11939     function is called by Qt internally, and the default
       
 11940     implementation may not always return a valid pointer.
       
 11941 */
       
 11942 
       
 11943 /*!
       
 11944     \fn QPoint QWidget::mapToGlobal(const QPoint &pos) const
       
 11945 
       
 11946     Translates the widget coordinate \a pos to global screen
       
 11947     coordinates. For example, \c{mapToGlobal(QPoint(0,0))} would give
       
 11948     the global coordinates of the top-left pixel of the widget.
       
 11949 
       
 11950     \sa mapFromGlobal() mapTo() mapToParent()
       
 11951 */
       
 11952 
       
 11953 /*!
       
 11954     \fn QPoint QWidget::mapFromGlobal(const QPoint &pos) const
       
 11955 
       
 11956     Translates the global screen coordinate \a pos to widget
       
 11957     coordinates.
       
 11958 
       
 11959     \sa mapToGlobal() mapFrom() mapFromParent()
       
 11960 */
       
 11961 
       
 11962 /*!
       
 11963     \fn void QWidget::grabMouse()
       
 11964 
       
 11965     Grabs the mouse input.
       
 11966 
       
 11967     This widget receives all mouse events until releaseMouse() is
       
 11968     called; other widgets get no mouse events at all. Keyboard
       
 11969     events are not affected. Use grabKeyboard() if you want to grab
       
 11970     that.
       
 11971 
       
 11972     \warning Bugs in mouse-grabbing applications very often lock the
       
 11973     terminal. Use this function with extreme caution, and consider
       
 11974     using the \c -nograb command line option while debugging.
       
 11975 
       
 11976     It is almost never necessary to grab the mouse when using Qt, as
       
 11977     Qt grabs and releases it sensibly. In particular, Qt grabs the
       
 11978     mouse when a mouse button is pressed and keeps it until the last
       
 11979     button is released.
       
 11980 
       
 11981     \note Only visible widgets can grab mouse input. If isVisible()
       
 11982     returns false for a widget, that widget cannot call grabMouse().
       
 11983 
       
 11984     \note \bold{(Mac OS X developers)} For \e Cocoa, calling
       
 11985     grabMouse() on a widget only works when the mouse is inside the
       
 11986     frame of that widget.  For \e Carbon, it works outside the widget's
       
 11987     frame as well, like for Windows and X11.
       
 11988 
       
 11989     \sa releaseMouse() grabKeyboard() releaseKeyboard()
       
 11990 */
       
 11991 
       
 11992 /*!
       
 11993     \fn void QWidget::grabMouse(const QCursor &cursor)
       
 11994     \overload grabMouse()
       
 11995 
       
 11996     Grabs the mouse input and changes the cursor shape.
       
 11997 
       
 11998     The cursor will assume shape \a cursor (for as long as the mouse
       
 11999     focus is grabbed) and this widget will be the only one to receive
       
 12000     mouse events until releaseMouse() is called().
       
 12001 
       
 12002     \warning Grabbing the mouse might lock the terminal.
       
 12003 
       
 12004     \note \bold{(Mac OS X developers)} See the note in QWidget::grabMouse().
       
 12005 
       
 12006     \sa releaseMouse(), grabKeyboard(), releaseKeyboard(), setCursor()
       
 12007 */
       
 12008 
       
 12009 /*!
       
 12010     \fn void QWidget::releaseMouse()
       
 12011 
       
 12012     Releases the mouse grab.
       
 12013 
       
 12014     \sa grabMouse(), grabKeyboard(), releaseKeyboard()
       
 12015 */
       
 12016 
       
 12017 /*!
       
 12018     \fn void QWidget::grabKeyboard()
       
 12019 
       
 12020     Grabs the keyboard input.
       
 12021 
       
 12022     This widget receives all keyboard events until releaseKeyboard()
       
 12023     is called; other widgets get no keyboard events at all. Mouse
       
 12024     events are not affected. Use grabMouse() if you want to grab that.
       
 12025 
       
 12026     The focus widget is not affected, except that it doesn't receive
       
 12027     any keyboard events. setFocus() moves the focus as usual, but the
       
 12028     new focus widget receives keyboard events only after
       
 12029     releaseKeyboard() is called.
       
 12030 
       
 12031     If a different widget is currently grabbing keyboard input, that
       
 12032     widget's grab is released first.
       
 12033 
       
 12034     \sa releaseKeyboard() grabMouse() releaseMouse() focusWidget()
       
 12035 */
       
 12036 
       
 12037 /*!
       
 12038     \fn void QWidget::releaseKeyboard()
       
 12039 
       
 12040     Releases the keyboard grab.
       
 12041 
       
 12042     \sa grabKeyboard(), grabMouse(), releaseMouse()
       
 12043 */
       
 12044 
       
 12045 /*!
       
 12046     \fn QWidget *QWidget::mouseGrabber()
       
 12047 
       
 12048     Returns the widget that is currently grabbing the mouse input.
       
 12049 
       
 12050     If no widget in this application is currently grabbing the mouse,
       
 12051     0 is returned.
       
 12052 
       
 12053     \sa grabMouse(), keyboardGrabber()
       
 12054 */
       
 12055 
       
 12056 /*!
       
 12057     \fn QWidget *QWidget::keyboardGrabber()
       
 12058 
       
 12059     Returns the widget that is currently grabbing the keyboard input.
       
 12060 
       
 12061     If no widget in this application is currently grabbing the
       
 12062     keyboard, 0 is returned.
       
 12063 
       
 12064     \sa grabMouse(), mouseGrabber()
       
 12065 */
       
 12066 
       
 12067 /*!
       
 12068     \fn void QWidget::activateWindow()
       
 12069 
       
 12070     Sets the top-level widget containing this widget to be the active
       
 12071     window.
       
 12072 
       
 12073     An active window is a visible top-level window that has the
       
 12074     keyboard input focus.
       
 12075 
       
 12076     This function performs the same operation as clicking the mouse on
       
 12077     the title bar of a top-level window. On X11, the result depends on
       
 12078     the Window Manager. If you want to ensure that the window is
       
 12079     stacked on top as well you should also call raise(). Note that the
       
 12080     window must be visible, otherwise activateWindow() has no effect.
       
 12081 
       
 12082     On Windows, if you are calling this when the application is not
       
 12083     currently the active one then it will not make it the active
       
 12084     window.  It will change the color of the taskbar entry to indicate
       
 12085     that the window has changed in some way. This is because Microsoft
       
 12086     does not allow an application to interrupt what the user is currently
       
 12087     doing in another application.
       
 12088 
       
 12089     \sa isActiveWindow(), window(), show()
       
 12090 */
       
 12091 
       
 12092 /*!
       
 12093     \fn int QWidget::metric(PaintDeviceMetric m) const
       
 12094 
       
 12095     Internal implementation of the virtual QPaintDevice::metric()
       
 12096     function.
       
 12097 
       
 12098     \a m is the metric to get.
       
 12099 */
       
 12100 
       
 12101 /*!
       
 12102     \fn void QWidget::setMask(const QRegion &region)
       
 12103     \overload
       
 12104 
       
 12105     Causes only the parts of the widget which overlap \a region to be
       
 12106     visible. If the region includes pixels outside the rect() of the
       
 12107     widget, window system controls in that area may or may not be
       
 12108     visible, depending on the platform.
       
 12109 
       
 12110     Note that this effect can be slow if the region is particularly
       
 12111     complex.
       
 12112 
       
 12113     \sa windowOpacity
       
 12114 */
       
 12115 void QWidget::setMask(const QRegion &newMask)
       
 12116 {
       
 12117     Q_D(QWidget);
       
 12118 
       
 12119     d->createExtra();
       
 12120     if (newMask == d->extra->mask)
       
 12121         return;
       
 12122 
       
 12123 #ifndef QT_NO_BACKINGSTORE
       
 12124     const QRegion oldMask(d->extra->mask);
       
 12125 #endif
       
 12126 
       
 12127     d->extra->mask = newMask;
       
 12128     d->extra->hasMask = !newMask.isEmpty();
       
 12129 
       
 12130 #ifndef QT_MAC_USE_COCOA
       
 12131     if (!testAttribute(Qt::WA_WState_Created))
       
 12132         return;
       
 12133 #endif
       
 12134 
       
 12135     d->setMask_sys(newMask);
       
 12136 
       
 12137 #ifndef QT_NO_BACKINGSTORE
       
 12138     if (!isVisible())
       
 12139         return;
       
 12140 
       
 12141     if (!d->extra->hasMask) {
       
 12142         // Mask was cleared; update newly exposed area.
       
 12143         QRegion expose(rect());
       
 12144         expose -= oldMask;
       
 12145         if (!expose.isEmpty()) {
       
 12146             d->setDirtyOpaqueRegion();
       
 12147             update(expose);
       
 12148         }
       
 12149         return;
       
 12150     }
       
 12151 
       
 12152     if (!isWindow()) {
       
 12153         // Update newly exposed area on the parent widget.
       
 12154         QRegion parentExpose(rect());
       
 12155         parentExpose -= newMask;
       
 12156         if (!parentExpose.isEmpty()) {
       
 12157             d->setDirtyOpaqueRegion();
       
 12158             parentExpose.translate(data->crect.topLeft());
       
 12159             parentWidget()->update(parentExpose);
       
 12160         }
       
 12161 
       
 12162         // Update newly exposed area on this widget
       
 12163         if (!oldMask.isEmpty())
       
 12164             update(newMask - oldMask);
       
 12165     }
       
 12166 #endif
       
 12167 }
       
 12168 
       
 12169 /*!
       
 12170     \fn void QWidget::setMask(const QBitmap &bitmap)
       
 12171 
       
 12172     Causes only the pixels of the widget for which \a bitmap has a
       
 12173     corresponding 1 bit to be visible. If the region includes pixels
       
 12174     outside the rect() of the widget, window system controls in that
       
 12175     area may or may not be visible, depending on the platform.
       
 12176 
       
 12177     Note that this effect can be slow if the region is particularly
       
 12178     complex.
       
 12179 
       
 12180     The following code shows how an image with an alpha channel can be
       
 12181     used to generate a mask for a widget:
       
 12182 
       
 12183     \snippet doc/src/snippets/widget-mask/main.cpp 0
       
 12184 
       
 12185     The label shown by this code is masked using the image it contains,
       
 12186     giving the appearance that an irregularly-shaped image is being drawn
       
 12187     directly onto the screen.
       
 12188 
       
 12189     Masked widgets receive mouse events only on their visible
       
 12190     portions.
       
 12191 
       
 12192     \sa clearMask(), windowOpacity(), {Shaped Clock Example}
       
 12193 */
       
 12194 void QWidget::setMask(const QBitmap &bitmap)
       
 12195 {
       
 12196     setMask(QRegion(bitmap));
       
 12197 }
       
 12198 
       
 12199 /*!
       
 12200     \fn void QWidget::clearMask()
       
 12201 
       
 12202     Removes any mask set by setMask().
       
 12203 
       
 12204     \sa setMask()
       
 12205 */
       
 12206 void QWidget::clearMask()
       
 12207 {
       
 12208     setMask(QRegion());
       
 12209 }
       
 12210 
       
 12211 /*! \fn const QX11Info &QWidget::x11Info() const
       
 12212     Returns information about the configuration of the X display used to display
       
 12213     the widget.
       
 12214 
       
 12215     \warning This function is only available on X11.
       
 12216 */
       
 12217 
       
 12218 /*! \fn Qt::HANDLE QWidget::x11PictureHandle() const
       
 12219     Returns the X11 Picture handle of the widget for XRender
       
 12220     support. Use of this function is not portable. This function will
       
 12221     return 0 if XRender support is not compiled into Qt, if the
       
 12222     XRender extension is not supported on the X11 display, or if the
       
 12223     handle could not be created.
       
 12224 */
       
 12225 
       
 12226 #ifdef Q_OS_SYMBIAN
       
 12227 void QWidgetPrivate::_q_delayedDestroy(WId winId)
       
 12228 {
       
 12229     delete winId;
       
 12230 }
       
 12231 #endif
       
 12232 
       
 12233 QT_END_NAMESPACE
       
 12234 
       
 12235 #include "moc_qwidget.cpp"
       
 12236