]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/toplevel.cpp
Better placement of the AutoComplete listbox
[wxWidgets.git] / src / os2 / toplevel.cpp
index 4e74fa9343af89846361d405d6a6dc50fd9cbfc4..badfc618355142823be9e0c2b028482fb9accefa 100644 (file)
@@ -36,6 +36,7 @@
     #include "wx/intl.h"
     #include "wx/frame.h"
     #include "wx/control.h"
     #include "wx/intl.h"
     #include "wx/frame.h"
     #include "wx/control.h"
+    #include "wx/containr.h"        // wxSetFocusToChild()
 #endif //WX_PRECOMP
 
 #include "wx/os2/private.h"
 #endif //WX_PRECOMP
 
 #include "wx/os2/private.h"
@@ -59,6 +60,14 @@ extern void          wxAssociateWinWithHandle( HWND         hWnd
 bool                 wxTopLevelWindowOS2::m_sbInitialized = FALSE;
 wxWindow*            wxTopLevelWindowOS2::m_spHiddenParent = NULL;
 
 bool                 wxTopLevelWindowOS2::m_sbInitialized = FALSE;
 wxWindow*            wxTopLevelWindowOS2::m_spHiddenParent = NULL;
 
+// ============================================================================
+// wxTopLevelWindowOS2 implementation
+// ============================================================================
+
+BEGIN_EVENT_TABLE(wxTopLevelWindowOS2, wxTopLevelWindowBase)
+    EVT_ACTIVATE(wxTopLevelWindowOS2::OnActivate)
+END_EVENT_TABLE()
+
 // ============================================================================
 // wxTopLevelWindowMSW implementation
 // ============================================================================
 // ============================================================================
 // wxTopLevelWindowMSW implementation
 // ============================================================================
@@ -70,21 +79,21 @@ MRESULT EXPENTRY wxDlgProc( HWND WXUNUSED(hWnd)
                            ,MPARAM WXUNUSED(lParam)
                           )
 {
                            ,MPARAM WXUNUSED(lParam)
                           )
 {
-    if (uMessage == WM_INITDLG)
-    {
-        //
-        // For this message, returning TRUE tells system to set focus to the
-        // first control in the dialog box.
-        //
-        return (MRESULT)TRUE;
-    }
-    else
+    switch(uMessage)
     {
     {
-        //
-        // For all the other ones, FALSE means that we didn't process the
-        // message
-        //
-        return (MRESULT)FALSE;
+        case WM_INITDLG:
+            //
+            // For this message, returning TRUE tells system to set focus to
+            // the first control in the dialog box, but we set the focus
+            // ourselves, however in OS/2 we must return true to enable the dialog
+            //
+            return (MRESULT)TRUE;
+        default:
+            //
+            // For all the other ones, FALSE means that we didn't process the
+            // message
+            //
+            return (MRESULT)FALSE;
     }
 } // end of wxDlgProc
 
     }
 } // end of wxDlgProc
 
@@ -111,8 +120,65 @@ void wxTopLevelWindowOS2::Init()
     m_hFrame    = NULLHANDLE;
     memset(&m_vSwp, 0, sizeof(SWP));
     memset(&m_vSwpClient, 0, sizeof(SWP));
     m_hFrame    = NULLHANDLE;
     memset(&m_vSwp, 0, sizeof(SWP));
     memset(&m_vSwpClient, 0, sizeof(SWP));
+    m_pWinLastFocused = (wxWindow *)NULL;
 } // end of wxTopLevelWindowIOS2::Init
 
 } // end of wxTopLevelWindowIOS2::Init
 
+void wxTopLevelWindowOS2::OnActivate(
+  wxActivateEvent&                  rEvent
+)
+{
+    if (rEvent.GetActive())
+    {
+        //
+        // Restore focus to the child which was last focused
+        //
+        wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), m_hWnd);
+
+        wxWindow*                   pParent = m_pWinLastFocused ? m_pWinLastFocused->GetParent()
+                                                                : NULL;
+        if (!pParent)
+        {
+            pParent = this;
+        }
+
+        wxSetFocusToChild( pParent
+                          ,&m_pWinLastFocused
+                         );
+    }
+    else // deactivating
+    {
+        //
+        // Remember the last focused child if it is our child
+        //
+        m_pWinLastFocused = FindFocus();
+
+        //
+        // So we NULL it out if it's a child from some other frame
+        //
+        wxWindow*                   pWin = m_pWinLastFocused;
+
+        while (pWin)
+        {
+            if (pWin->IsTopLevel())
+            {
+                if (pWin != this)
+                {
+                    m_pWinLastFocused = NULL;
+                }
+                break;
+            }
+            pWin = pWin->GetParent();
+        }
+
+        wxLogTrace(_T("focus"),
+                   _T("wxTLW %08x deactivated, last focused: %08x."),
+                   m_hWnd,
+                   m_pWinLastFocused ? GetHwndOf(m_pWinLastFocused)
+                                     : NULL);
+        rEvent.Skip();
+    }
+} // end of wxTopLevelWindowOS2::OnActivate
+
 WXDWORD wxTopLevelWindowOS2::OS2GetStyle(
   long                              lStyle
 , WXDWORD*                          pdwExflags
 WXDWORD wxTopLevelWindowOS2::OS2GetStyle(
   long                              lStyle
 , WXDWORD*                          pdwExflags
@@ -332,7 +398,7 @@ bool wxTopLevelWindowOS2::CreateDialog(
                       ,nY
                       ,nWidth
                       ,nHeight
                       ,nY
                       ,nWidth
                       ,nHeight
-                      ,SWP_MOVE | SWP_SIZE | SWP_ZORDER | SWP_SHOW
+                      ,SWP_MOVE | SWP_SIZE | SWP_ZORDER | SWP_SHOW | SWP_ACTIVATE
                      );
     ::WinQueryWindowPos(GetHwnd(), GetSwp());
     m_hFrame = m_hWnd;
                      );
     ::WinQueryWindowPos(GetHwnd(), GetSwp());
     m_hFrame = m_hWnd;
@@ -527,20 +593,50 @@ bool wxTopLevelWindowOS2::Create(
 
 wxTopLevelWindowOS2::~wxTopLevelWindowOS2()
 {
 
 wxTopLevelWindowOS2::~wxTopLevelWindowOS2()
 {
-    wxTopLevelWindows.DeleteObject(this);
+    if (this == m_spHiddenParent)
+    {
+        //
+        // Stop [infinite] recursion which would otherwise happen when we do
+        // "delete ms_hiddenParent" below -- and we're not interested in doing
+        // anything of the rest below for that window because the rest of
+        // wxWindows doesn't even know about it
+        //
+        return;
+    }
 
     if (wxModelessWindows.Find(this))
         wxModelessWindows.DeleteObject(this);
 
     //
 
     if (wxModelessWindows.Find(this))
         wxModelessWindows.DeleteObject(this);
 
     //
-    // If this is the last top-level window, exit.
+    // After destroying an owned window, Windows activates the next top level
+    // window in Z order but it may be different from our owner (to reproduce
+    // this simply Alt-TAB to another application and back before closing the
+    // owned frame) whereas we always want to yield activation to our parent
+    //
+    if (HasFlag(wxFRAME_FLOAT_ON_PARENT))
+    {
+        wxWindow*                   pParent = GetParent();
+
+        if (pParent)
+        {
+            ::WinSetWindowPos( GetHwndOf(pParent)
+                              ,HWND_TOP
+                              ,0, 0, 0, 0
+                              ,SWP_ZORDER
+                             );
+        }
+    }
+
+    //
+    // If this is the last top-level window, we're going to exit and we should
+    // delete ms_hiddenParent now to avoid leaking it
     //
     //
-    if (wxTheApp && (wxTopLevelWindows.Number() == 0))
+    if (IsLastBeforeExit())
     {
     {
-        wxTheApp->SetTopWindow(NULL);
-        if ( wxTheApp->GetExitOnFrameDelete() )
+        if (m_spHiddenParent)
         {
         {
-            ::WinPostMsg(NULL, WM_QUIT, 0, 0);
+            delete m_spHiddenParent;
+            m_spHiddenParent = NULL;
         }
     }
 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2
         }
     }
 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2
@@ -657,15 +753,6 @@ bool wxTopLevelWindowOS2::Show(
 
             ::WinQueryWindowPos(hWndParent, &vSwp);
             m_bIconized = vSwp.fl & SWP_MINIMIZE;
 
             ::WinQueryWindowPos(hWndParent, &vSwp);
             m_bIconized = vSwp.fl & SWP_MINIMIZE;
-            if (hWndParent)
-                ::WinSetWindowPos( hWndParent
-                                  ,HWND_TOP
-                                  ,vSwp.x
-                                  ,vSwp.y
-                                  ,vSwp.cx
-                                  ,vSwp.cy
-                                  ,SWP_ZORDER | SWP_ACTIVATE | SWP_SHOW | SWP_MOVE
-                                 );
             ::WinEnableWindow(hWndParent, TRUE);
         }
     }
             ::WinEnableWindow(hWndParent, TRUE);
         }
     }