Index: /trunk/src/gui/kernel/qapplication_pm.cpp
===================================================================
--- /trunk/src/gui/kernel/qapplication_pm.cpp	(revision 935)
+++ /trunk/src/gui/kernel/qapplication_pm.cpp	(revision 936)
@@ -143,4 +143,5 @@
     QWidgetPrivate *dptr() { return d_func(); }
     QRect frameStrut() const { return d_func()->frameStrut(); }
+    HWND frameWinId() const { return d_func()->frameWinId(); }
     bool        pmEvent(QMSG *m, MRESULT *r) { return QWidget::pmEvent(m, r); }
 //  void        markFrameStrutDirty() { data->fstrut_dirty = 1; }
@@ -1102,5 +1103,5 @@
                         // FID_SYSMENU) should not cause the focus to be lost.
                         if (WinQueryWindow(hwnd, QW_OWNER) ==
-                                ((QETWidget*)widget->window())->dptr()->frameWinId())
+                                ((QETWidget*)widget->window())->frameWinId())
                             break;
                         if (!widget->isWindow())
@@ -1311,4 +1312,37 @@
         }
 
+        case WM_ADJUSTWINDOWPOS: {
+            // If PM tries to activate a modally blocked window, pass activation
+            // over to the top modal window instead of letting it activate the
+            // blocked one. Z-order changes are simply ignored for now.
+            // @todo Blocked widgets should keep Z-order WRT their modal
+            // blockers whenever they move (e.g. bringing the modal one to top
+            // should also put the one it blocks right under it instead of
+            // possibly leaving it somewhere behind other windows).
+            PSWP pswp =(PSWP)qmsg.mp1;
+            if (app_do_modal &&
+                ((pswp->fl & SWP_ACTIVATE) || (pswp->fl & SWP_ZORDER))) {
+                QWidget *top = 0;
+                if (!QApplicationPrivate::tryModalHelper(widget, &top) && top && widget != top) {
+                    if (pswp->fl & SWP_ACTIVATE) {
+                        if (top->isVisible()) {
+                            top->activateWindow();
+                        } else {
+                            // This is the case when native file dialogs are shown
+                            QWidget *p = (top->parentWidget() ? top->parentWidget()->window() : 0);
+                            if (p && p->isVisible()) {
+                                p->activateWindow();
+                            }
+                        }
+                        pswp->fl &= ~SWP_ACTIVATE;
+                    }
+                    if (pswp->fl & SWP_ZORDER) {
+                        pswp->fl &= ~SWP_ZORDER;
+                    }
+                }
+            }
+            break;
+        }
+
         case WM_QUERYTRACKINFO: {
             QWExtra *x = widget->xtra();
@@ -1439,12 +1473,13 @@
     ignoreNextMouseReleaseEvent = false;
 
-    // go through all top-level widgets and disable those that should be
-    // blocked by the modality (this in particular will disable activation
-    // through clicking on the title bar and also state change throuhg titlebar
-    // buttons)
+    // go through all top-level widgets and disable those that should be blocked
+    // by the modality (this will effectively disable frame controls and make
+    // it impossible to manipulate the window state, position and size, even
+    // programmatically, e.g. from the Window List)
     QWidgetList list = QApplication::topLevelWidgets();
     foreach(QWidget *w, list) {
-        if (QApplicationPrivate::isBlockedByModal(w))
+        if (w != widget && QApplicationPrivate::isBlockedByModal(w)) {
             WinEnableWindow(w->d_func()->frameWinId(), FALSE);
+        }
     }
 }
@@ -1453,10 +1488,4 @@
 {
     if (qt_modal_stack) {
-        // go through all affected top-level widgets and re-enable them
-        QWidgetList list = QApplication::topLevelWidgets();
-        foreach(QWidget *w, list) {
-            if (QApplicationPrivate::isBlockedByModal(w))
-                WinEnableWindow(w->d_func()->frameWinId(), TRUE);
-        }
         if (qt_modal_stack->removeAll(widget)) {
             if (qt_modal_stack->isEmpty()) {
@@ -1480,4 +1509,13 @@
         }
         ignoreNextMouseReleaseEvent = true;
+
+        // go through all top-level widgets and re-enable those that are not
+        // blocked any more
+        QWidgetList list = QApplication::topLevelWidgets();
+        foreach(QWidget *w, list) {
+            if (w != widget && !QApplicationPrivate::isBlockedByModal(w)) {
+                WinEnableWindow(w->d_func()->frameWinId(), TRUE);
+            }
+        }
     }
     app_do_modal = qt_modal_stack != 0;
@@ -2732,4 +2770,10 @@
     myCaseEnd()
 
+    myCaseBegin(WM_ADJUSTWINDOWPOS)
+        debug << *((PSWP) qmsg.mp1);
+        debug.space();
+        break;
+    myCaseEnd()
+
     myCaseBegin(WM_WINDOWPOSCHANGED)
         debug << *((PSWP) qmsg.mp1);
@@ -2785,8 +2829,4 @@
     myCaseEnd()
 
-    myCaseBegin(WM_ADJUSTWINDOWPOS)
-        break;
-    myCaseEnd()
-
     myCaseBegin(WM_VRNDISABLED)
         break;
@@ -2797,4 +2837,11 @@
 
     myCaseBegin(WM_SHOW)
+        break;
+    myCaseEnd()
+
+    myCaseBegin(WM_HITTEST)
+        PPOINTS pt = (PPOINTS)&qmsg.mp1;
+        debug.nospace() << "Point(" << pt->x << "," << pt->y << ")";
+        debug.space();
         break;
     myCaseEnd()
