Index: /trunk/src/gui/kernel/qapplication.h
===================================================================
--- /trunk/src/gui/kernel/qapplication.h	(revision 137)
+++ /trunk/src/gui/kernel/qapplication.h	(revision 138)
@@ -239,4 +239,8 @@
     void winFocus(QWidget *, bool);
     static void winMouseButtonUp();
+#endif
+
+#if defined(Q_WS_PM)
+    void pmFocus(QWidget *, bool);
 #endif
 
Index: /trunk/src/gui/kernel/qapplication_pm.cpp
===================================================================
--- /trunk/src/gui/kernel/qapplication_pm.cpp	(revision 137)
+++ /trunk/src/gui/kernel/qapplication_pm.cpp	(revision 138)
@@ -469,4 +469,29 @@
 #define SV_WORKAREA_XLEFT   54
 
+/*!
+    \internal
+    \since 4.1
+
+    If \a gotFocus is true, \a widget will become the active window.
+    Otherwise the active window is reset to 0.
+*/
+void QApplication::pmFocus(QWidget *widget, bool gotFocus)
+{
+    if (gotFocus) {
+        setActiveWindow(widget);
+        if (QApplicationPrivate::active_window
+            && (QApplicationPrivate::active_window->windowType() == Qt::Dialog)) {
+            // raise the entire application, not just the dialog
+            QWidget* mw = QApplicationPrivate::active_window;
+            while(mw->parentWidget() && (mw->windowType() == Qt::Dialog))
+                mw = mw->parentWidget()->window();
+            if (mw->testAttribute(Qt::WA_WState_Created) && mw != QApplicationPrivate::active_window)
+                WinSetWindowPos(mw->d_func()->frameWinId(), HWND_TOP, 0, 0, 0, 0, SWP_ZORDER );
+        }
+    } else {
+        setActiveWindow(0);
+    }
+}
+
 // QtWndProc() receives all messages from the main event loop
 
@@ -659,4 +684,36 @@
                 if (widget->translateConfigEvent(qmsg))
                     return (MRESULT)TRUE;
+                break;
+            }
+
+            case WM_ACTIVATE: {
+                qApp->pmFocus(widget, SHORT1FROMMP(mp1));
+                break;
+            }
+
+            case WM_SETFOCUS: {
+                HWND hwnd = (HWND)mp1;
+                bool focus = SHORT1FROMMP(mp2);
+                if (!focus) {
+                    if (!QWidget::find(hwnd)) {
+                        // we don't get focus, so unset it now
+                        if (QApplication::activePopupWidget()) {
+                            // Another application was activated while our popups are open,
+                            // then close all popups.  In case some popup refuses to close,
+                            // we give up after 1024 attempts (to avoid an infinite loop).
+                            int maxiter = 1024;
+                            QWidget *popup;
+                            while ( (popup=QApplication::activePopupWidget()) && maxiter-- )
+                                popup->close();
+                        }
+                        // non-Qt ownees of our WC_FRAME window (such as
+                        // FID_SYSMENU) should not cause the focus to be lost.
+                        if (WinQueryWindow(hwnd, QW_OWNER) ==
+                                ((QETWidget*)widget->window())->dptr()->frameWinId())
+                            break;
+                        if (!widget->isWindow())
+                            qApp->pmFocus(widget, focus);
+                    }
+                }
                 break;
             }
@@ -2011,4 +2068,22 @@
     myCaseEnd()
 
+    myCaseBegin(WM_ACTIVATE)
+        bool active = SHORT1FROMMP(qmsg.mp1);
+        HWND hwnd = (HWND)qmsg.mp2;
+        str += QCStr(" Active(") + QCStr(active ? "TRUE) " : "FALSE)");
+        str += QString().sprintf(" hwnd %08lX.", hwnd);
+        str += qWidgetName(qWidgetFromHWND(hwnd));
+        break;
+    myCaseEnd()
+
+    myCaseBegin(WM_SETFOCUS)
+        HWND hwnd = (HWND)qmsg.mp1;
+        bool focus = SHORT1FROMMP(qmsg.mp2);
+        str += QCStr(" Focus(") + QCStr(focus ? "TRUE) " : "FALSE)");
+        str += QString().sprintf(" hwnd %08lX.", hwnd);
+        str += qWidgetName(qWidgetFromHWND(hwnd));
+        break;
+    myCaseEnd()
+
     default:
         break;
@@ -2030,14 +2105,20 @@
 {
     QString fl;
+    myDefFlagEx(swp.fl, SWP_SIZE, fl, "SIZE");
+    myDefFlagEx(swp.fl, SWP_MOVE, fl, "MOVE");
+    myDefFlagEx(swp.fl, SWP_ZORDER, fl, "ZORD");
     myDefFlagEx(swp.fl, SWP_SHOW, fl, "SHOW");
     myDefFlagEx(swp.fl, SWP_HIDE, fl, "HIDE");
+    myDefFlagEx(swp.fl, SWP_NOREDRAW, fl, "NORDR");
+    myDefFlagEx(swp.fl, SWP_NOADJUST, fl, "NOADJ");
     myDefFlagEx(swp.fl, SWP_ACTIVATE, fl, "ACT");
     myDefFlagEx(swp.fl, SWP_DEACTIVATE, fl, "DEACT");
+    myDefFlagEx(swp.fl, SWP_EXTSTATECHANGE, fl, "EXTST");
+    myDefFlagEx(swp.fl, SWP_MINIMIZE, fl, "MIN");
     myDefFlagEx(swp.fl, SWP_MAXIMIZE, fl, "MAX");
-    myDefFlagEx(swp.fl, SWP_MINIMIZE, fl, "MIN");
     myDefFlagEx(swp.fl, SWP_RESTORE, fl, "REST");
-    myDefFlagEx(swp.fl, SWP_MOVE, fl, "MOVE");
-    myDefFlagEx(swp.fl, SWP_SIZE, fl, "SIZE");
-    myDefFlagEx(swp.fl, SWP_ZORDER, fl, "ZORD");
+    myDefFlagEx(swp.fl, SWP_FOCUSACTIVATE, fl, "FCSACT");
+    myDefFlagEx(swp.fl, SWP_FOCUSDEACTIVATE, fl, "FCSDEACT");
+    myDefFlagEx(swp.fl, SWP_NOAUTOCLOSE, fl, "NOACLOSE");
 
     return QString().sprintf("SWP(%ld,%ld %ldx%ld %08lX ",
