]>
Commit | Line | Data |
---|---|---|
3808e191 JS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/evtloop.cpp | |
3 | // Purpose: implements wxEventLoop for MSW | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 01.06.01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // License: wxWindows licence |
3808e191 JS |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
3808e191 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
6c0e8b4e VZ |
28 | #include "wx/window.h" |
29 | #include "wx/app.h" | |
3808e191 JS |
30 | #endif //WX_PRECOMP |
31 | ||
32 | #include "wx/evtloop.h" | |
4300caa7 | 33 | |
0cd7d9b2 | 34 | #include "wx/tooltip.h" |
4300caa7 VZ |
35 | #include "wx/except.h" |
36 | #include "wx/ptr_scpd.h" | |
3808e191 JS |
37 | |
38 | #include "wx/msw/private.h" | |
39 | ||
0cd7d9b2 | 40 | #if wxUSE_THREADS |
4300caa7 VZ |
41 | #include "wx/thread.h" |
42 | ||
9af08eb8 VZ |
43 | // define the list of MSG strutures |
44 | WX_DECLARE_LIST(MSG, wxMsgList); | |
4300caa7 | 45 | |
9af08eb8 | 46 | #include "wx/listimpl.cpp" |
4300caa7 | 47 | |
259c43f6 | 48 | WX_DEFINE_LIST(wxMsgList) |
4300caa7 | 49 | #endif // wxUSE_THREADS |
0cd7d9b2 | 50 | |
3808e191 | 51 | // ============================================================================ |
3754265e | 52 | // wxEventLoop implementation |
3808e191 JS |
53 | // ============================================================================ |
54 | ||
b4626104 | 55 | wxWindowMSW *wxEventLoop::ms_winCritical = NULL; |
3754265e | 56 | |
3808e191 | 57 | // ---------------------------------------------------------------------------- |
3754265e | 58 | // ctor/dtor |
3808e191 JS |
59 | // ---------------------------------------------------------------------------- |
60 | ||
3754265e VZ |
61 | wxEventLoop::wxEventLoop() |
62 | { | |
63 | m_shouldExit = false; | |
64 | m_exitcode = 0; | |
65 | } | |
66 | ||
67 | // ---------------------------------------------------------------------------- | |
68 | // wxEventLoop message processing | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
71 | void wxEventLoop::ProcessMessage(WXMSG *msg) | |
3808e191 JS |
72 | { |
73 | // give us the chance to preprocess the message first | |
74 | if ( !PreProcessMessage(msg) ) | |
75 | { | |
76 | // if it wasn't done, dispatch it to the corresponding window | |
77 | ::TranslateMessage(msg); | |
78 | ::DispatchMessage(msg); | |
79 | } | |
80 | } | |
81 | ||
b4626104 | 82 | bool wxEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win) |
a7b7500c VZ |
83 | { |
84 | while ( win ) | |
85 | { | |
86 | if ( win == ms_winCritical ) | |
87 | return true; | |
88 | ||
89 | win = win->GetParent(); | |
90 | } | |
91 | ||
92 | return false; | |
93 | } | |
94 | ||
3754265e | 95 | bool wxEventLoop::PreProcessMessage(WXMSG *msg) |
3808e191 | 96 | { |
ac8d0c11 | 97 | HWND hwnd = msg->hwnd; |
89c83180 | 98 | wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd); |
a7b7500c | 99 | wxWindow *wnd; |
ac8d0c11 | 100 | |
18f5234f DS |
101 | // this might happen if we're in a modeless dialog, or if a wx control has |
102 | // children which themselves were not created by wx (i.e. wxActiveX control children) | |
ac8d0c11 VZ |
103 | if ( !wndThis ) |
104 | { | |
18f5234f | 105 | while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )) |
ac8d0c11 VZ |
106 | { |
107 | hwnd = ::GetParent(hwnd); | |
89c83180 | 108 | |
18f5234f DS |
109 | // If the control has a wx parent, break and give the parent a chance |
110 | // to process the window message | |
111 | wndThis = wxGetWindowFromHWND((WXHWND)hwnd); | |
112 | if (wndThis != NULL) | |
113 | break; | |
ac8d0c11 VZ |
114 | } |
115 | ||
18f5234f DS |
116 | if ( !wndThis ) |
117 | { | |
118 | // this may happen if the event occurred in a standard modeless dialog (the | |
119 | // only example of which I know of is the find/replace dialog) - then call | |
120 | // IsDialogMessage() to make TAB navigation in it work | |
121 | ||
122 | // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for | |
123 | // them) if we call it for the control itself | |
124 | return hwnd && ::IsDialogMessage(hwnd, msg) != 0; | |
125 | } | |
ac8d0c11 | 126 | } |
3808e191 | 127 | |
a7b7500c VZ |
128 | if ( !AllowProcessing(wndThis) ) |
129 | { | |
130 | // not a child of critical window, so we eat the event but take care to | |
131 | // stop an endless stream of WM_PAINTs which would have resulted if we | |
132 | // didn't validate the invalidated part of the window | |
133 | if ( msg->message == WM_PAINT ) | |
134 | ::ValidateRect(hwnd, NULL); | |
135 | ||
136 | return true; | |
137 | } | |
138 | ||
3808e191 JS |
139 | #if wxUSE_TOOLTIPS |
140 | // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to | |
141 | // popup the tooltip bubbles | |
ac8d0c11 | 142 | if ( msg->message == WM_MOUSEMOVE ) |
3808e191 | 143 | { |
c009bf3e VZ |
144 | // we should do it if one of window children has an associated tooltip |
145 | // (and not just if the window has a tooltip itself) | |
146 | if ( wndThis->HasToolTips() ) | |
147 | wxToolTip::RelayEvent((WXMSG *)msg); | |
3808e191 JS |
148 | } |
149 | #endif // wxUSE_TOOLTIPS | |
150 | ||
ac8d0c11 VZ |
151 | // allow the window to prevent certain messages from being |
152 | // translated/processed (this is currently used by wxTextCtrl to always | |
153 | // grab Ctrl-C/V/X, even if they are also accelerators in some parent) | |
154 | if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) ) | |
155 | { | |
3754265e | 156 | return false; |
ac8d0c11 VZ |
157 | } |
158 | ||
159 | // try translations first: the accelerators override everything | |
3808e191 JS |
160 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) |
161 | { | |
ac8d0c11 | 162 | if ( wnd->MSWTranslateMessage((WXMSG *)msg)) |
3754265e | 163 | return true; |
ac8d0c11 VZ |
164 | |
165 | // stop at first top level window, i.e. don't try to process the key | |
166 | // strokes originating in a dialog using the accelerators of the parent | |
167 | // frame - this doesn't make much sense | |
168 | if ( wnd->IsTopLevel() ) | |
169 | break; | |
3808e191 JS |
170 | } |
171 | ||
22cfea03 JS |
172 | // now try the other hooks (kbd navigation is handled here) |
173 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
3808e191 | 174 | { |
22cfea03 JS |
175 | if (wnd != wndThis) // Skip the first since wndThis->MSWProcessMessage() was called above |
176 | { | |
177 | if ( wnd->MSWProcessMessage((WXMSG *)msg) ) | |
178 | return true; | |
179 | } | |
c8026dea | 180 | |
22cfea03 JS |
181 | // Stop at first top level window (as per comment above). |
182 | // If we don't do this, pressing ESC on a modal dialog shown as child of a modal | |
183 | // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example | |
184 | if (wnd->IsTopLevel()) | |
185 | break; | |
3808e191 JS |
186 | } |
187 | ||
ac8d0c11 | 188 | // no special preprocessing for this message, dispatch it normally |
3754265e | 189 | return false; |
cea62f9c JS |
190 | } |
191 | ||
3808e191 JS |
192 | // ---------------------------------------------------------------------------- |
193 | // wxEventLoop running and exiting | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
c8026dea VZ |
196 | // ---------------------------------------------------------------------------- |
197 | // wxEventLoopManual customization | |
198 | // ---------------------------------------------------------------------------- | |
3808e191 | 199 | |
c8026dea VZ |
200 | void wxEventLoop::OnNextIteration() |
201 | { | |
202 | #if wxUSE_THREADS | |
203 | wxMutexGuiLeaveOrEnter(); | |
204 | #endif // wxUSE_THREADS | |
3808e191 JS |
205 | } |
206 | ||
c8026dea | 207 | void wxEventLoop::WakeUp() |
3808e191 | 208 | { |
f7550d73 | 209 | ::PostMessage(NULL, WM_NULL, 0, 0); |
3808e191 JS |
210 | } |
211 | ||
212 | // ---------------------------------------------------------------------------- | |
213 | // wxEventLoop message processing dispatching | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
216 | bool wxEventLoop::Pending() const | |
217 | { | |
218 | MSG msg; | |
219 | return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0; | |
220 | } | |
221 | ||
222 | bool wxEventLoop::Dispatch() | |
223 | { | |
3754265e | 224 | wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") ); |
3808e191 JS |
225 | |
226 | MSG msg; | |
227 | BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0); | |
228 | ||
229 | if ( rc == 0 ) | |
230 | { | |
231 | // got WM_QUIT | |
3754265e | 232 | return false; |
3808e191 JS |
233 | } |
234 | ||
235 | if ( rc == -1 ) | |
236 | { | |
237 | // should never happen, but let's test for it nevertheless | |
238 | wxLogLastError(wxT("GetMessage")); | |
239 | ||
240 | // still break from the loop | |
3754265e | 241 | return false; |
3808e191 JS |
242 | } |
243 | ||
244 | #if wxUSE_THREADS | |
245 | wxASSERT_MSG( wxThread::IsMain(), | |
246 | wxT("only the main thread can process Windows messages") ); | |
247 | ||
3754265e | 248 | static bool s_hadGuiLock = true; |
9af08eb8 | 249 | static wxMsgList s_aSavedMessages; |
3808e191 JS |
250 | |
251 | // if a secondary thread owning the mutex is doing GUI calls, save all | |
252 | // messages for later processing - we can't process them right now because | |
253 | // it will lead to recursive library calls (and we're not reentrant) | |
254 | if ( !wxGuiOwnedByMainThread() ) | |
255 | { | |
3754265e | 256 | s_hadGuiLock = false; |
3808e191 JS |
257 | |
258 | // leave out WM_COMMAND messages: too dangerous, sometimes | |
259 | // the message will be processed twice | |
6c0e8b4e | 260 | if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND ) |
3808e191 | 261 | { |
9af08eb8 VZ |
262 | MSG* pMsg = new MSG(msg); |
263 | s_aSavedMessages.Append(pMsg); | |
3808e191 JS |
264 | } |
265 | ||
3754265e | 266 | return true; |
3808e191 JS |
267 | } |
268 | else | |
269 | { | |
270 | // have we just regained the GUI lock? if so, post all of the saved | |
271 | // messages | |
272 | // | |
273 | // FIXME of course, it's not _exactly_ the same as processing the | |
274 | // messages normally - expect some things to break... | |
275 | if ( !s_hadGuiLock ) | |
276 | { | |
3754265e | 277 | s_hadGuiLock = true; |
3808e191 | 278 | |
9af08eb8 VZ |
279 | wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst(); |
280 | while (node) | |
3808e191 | 281 | { |
9af08eb8 VZ |
282 | MSG* pMsg = node->GetData(); |
283 | s_aSavedMessages.Erase(node); | |
284 | ||
285 | ProcessMessage(pMsg); | |
286 | delete pMsg; | |
3808e191 | 287 | |
9af08eb8 VZ |
288 | node = s_aSavedMessages.GetFirst(); |
289 | } | |
3808e191 JS |
290 | } |
291 | } | |
292 | #endif // wxUSE_THREADS | |
293 | ||
3754265e | 294 | ProcessMessage(&msg); |
3808e191 | 295 | |
3754265e | 296 | return true; |
3808e191 JS |
297 | } |
298 |