src/gui/kernel/qeventdispatcher_mac.mm
changeset 37 758a864f9613
parent 33 3e2da88830cd
--- a/src/gui/kernel/qeventdispatcher_mac.mm	Fri Sep 17 08:34:18 2010 +0300
+++ b/src/gui/kernel/qeventdispatcher_mac.mm	Mon Oct 04 01:19:32 2010 +0300
@@ -785,7 +785,7 @@
     // the stacking order of the windows while doing so, we put
     // up a block that is used in QCocoaWindow and QCocoaPanel:
     int stackSize = cocoaModalSessionStack.size();
-    for (int i=stackSize-1; i>=0; --i) {
+    for (int i=0; i<stackSize; ++i) {
         QCocoaModalSessionInfo &info = cocoaModalSessionStack[i];
         if (info.session) {
             [NSApp endModalSession:info.session];
@@ -822,14 +822,15 @@
             QBoolBlocker block1(blockSendPostedEvents, true);
             info.nswindow = window;
             [(NSWindow*) info.nswindow retain];
-            // When creating a modal session cocoa will rearrange the windows.
-            // In order to avoid windows to be put behind another we need to
-            // keep the window level.
-            int level = [window level];
+            int levelBeforeEnterModal = [window level];
             info.session = [NSApp beginModalSessionForWindow:window];
-            [window setLevel:level];
+            // Make sure we don't stack the window lower that it was before
+            // entering modal, in case it e.g. had the stays-on-top flag set:
+            if (levelBeforeEnterModal > [window level])
+                [window setLevel:levelBeforeEnterModal];
         }
         currentModalSessionCached = info.session;
+        cleanupModalSessionsNeeded = false;
     }
     return currentModalSessionCached;
 }
@@ -881,6 +882,10 @@
     for (int i=stackSize-1; i>=0; --i) {
         QCocoaModalSessionInfo &info = cocoaModalSessionStack[i];
         if (info.widget) {
+            // This session has a widget, and is therefore not marked
+            // as stopped. So just make it current. There might still be other
+            // stopped sessions on the stack, but those will be stopped on
+            // a later "cleanup" call.
             currentModalSessionCached = info.session;
             break;
         }
@@ -926,6 +931,7 @@
             if (i == stackSize-1) {
                 // The top sessions ended. Interrupt the event dispatcher
                 // to start spinning the correct session immidiatly: 
+                currentModalSessionCached = 0;
                 cleanupModalSessionsNeeded = true;
                 QEventDispatcherMac::instance()->interrupt();
             }