prevent the parent window from losing activation when a popup is shown
[wxWidgets.git] / src / msw / popupwin.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/popupwin.cpp
3 // Purpose: implements wxPopupWindow for MSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 08.05.02
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "popup.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #endif //WX_PRECOMP
33
34 #include "wx/popupwin.h"
35
36 #include "wx/msw/private.h" // for WS_CHILD and WS_POPUP
37
38 wxWindowList wxPopupWindow::ms_shownPopups;
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 bool wxPopupWindow::Create(wxWindow *parent, int flags)
45 {
46 return wxPopupWindowBase::Create(parent) &&
47 wxWindow::Create(parent, -1,
48 wxDefaultPosition, wxDefaultSize,
49 flags | wxPOPUP_WINDOW);
50 }
51
52 void wxPopupWindow::DoGetPosition(int *x, int *y) const
53 {
54 // the position of a "top level" window such as this should be in
55 // screen coordinates, not in the client ones which MSW gives us
56 // (because we are a child window)
57 wxPopupWindowBase::DoGetPosition(x, y);
58
59 GetParent()->ClientToScreen(x, y);
60 }
61
62 WXDWORD wxPopupWindow::MSWGetStyle(long flags, WXDWORD *exstyle) const
63 {
64 // we only hnour the border flags
65 WXDWORD style = wxWindow::MSWGetStyle(flags & wxBORDER_MASK, exstyle);
66
67 // and we mustn't have WS_CHILD style or we would be limited to the parents
68 // client area
69 style &= ~WS_CHILD;
70 style |= WS_POPUP;
71
72 if ( exstyle )
73 {
74 // a popup window floats on top of everything
75 *exstyle |= WS_EX_TOPMOST | WS_EX_TOOLWINDOW;
76 }
77
78 return style;
79 }
80
81 bool wxPopupWindow::Show(bool show)
82 {
83 // skip wxWindow::Show() which calls wxBringWindowToTop(): this results in
84 // activating the popup window and stealing the atcivation from our parent
85 // which means that the parent frame becomes deactivated when opening a
86 // combobox, for example -- definitely not what we want
87 if ( !wxWindowBase::Show(show) )
88 return FALSE;
89
90 if ( show )
91 {
92 ms_shownPopups.Append(this);
93 }
94 else // remove from the shown list
95 {
96 ms_shownPopups.DeleteObject(this);
97 }
98
99 ::ShowWindow(GetHwnd(), show ? SW_SHOWNOACTIVATE : SW_HIDE);
100
101 return TRUE;
102 }
103
104 /* static */
105 wxPopupWindow *wxPopupWindow::FindPopupFor(wxWindow *winParent)
106 {
107 // find a popup with the given parent in the linked list of all shown
108 // popups
109 for ( wxWindowList::Node *node = ms_shownPopups.GetFirst();
110 node;
111 node = node->GetNext() )
112 {
113 wxWindow *win = node->GetData();
114 if ( win->GetParent() == winParent )
115 return (wxPopupWindow *)win;
116 }
117
118 return NULL;
119 }
120