]> git.saurik.com Git - wxWidgets.git/blob - src/msw/evtloop.cpp
set TOOLKIT to WINCE for VC8 builds targeting pocketpc2003
[wxWidgets.git] / src / msw / evtloop.cpp
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>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
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
28 #if wxUSE_GUI
29 #include "wx/window.h"
30 #endif
31 #include "wx/app.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/evtloop.h"
35 #include "wx/thread.h"
36 #include "wx/except.h"
37 #include "wx/ptr_scpd.h"
38 #include "wx/msw/private.h"
39
40 #if wxUSE_GUI
41 #include "wx/tooltip.h"
42 #if wxUSE_THREADS
43 // define the list of MSG strutures
44 WX_DECLARE_LIST(MSG, wxMsgList);
45
46 #include "wx/listimpl.cpp"
47
48 WX_DEFINE_LIST(wxMsgList)
49 #endif // wxUSE_THREADS
50 #endif //wxUSE_GUI
51
52 #if wxUSE_BASE
53
54 // ============================================================================
55 // wxMSWEventLoopBase implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // ctor/dtor
60 // ----------------------------------------------------------------------------
61
62 wxMSWEventLoopBase::wxMSWEventLoopBase()
63 {
64 m_shouldExit = false;
65 m_exitcode = 0;
66 }
67
68 // ----------------------------------------------------------------------------
69 // wxEventLoop message processing dispatching
70 // ----------------------------------------------------------------------------
71
72 bool wxMSWEventLoopBase::Pending() const
73 {
74 MSG msg;
75 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
76 }
77
78 bool wxMSWEventLoopBase::GetNextMessage(WXMSG* msg)
79 {
80 wxCHECK_MSG( IsRunning(), false, _T("can't get messages if not running") );
81
82 const BOOL rc = ::GetMessage(msg, NULL, 0, 0);
83
84 if ( rc == 0 )
85 {
86 // got WM_QUIT
87 return false;
88 }
89
90 if ( rc == -1 )
91 {
92 // should never happen, but let's test for it nevertheless
93 wxLogLastError(wxT("GetMessage"));
94
95 // still break from the loop
96 return false;
97 }
98
99 return true;
100 }
101
102 #endif // wxUSE_BASE
103
104 #if wxUSE_GUI
105
106 // ============================================================================
107 // GUI wxEventLoop implementation
108 // ============================================================================
109
110 wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL;
111
112 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
113 {
114 while ( win )
115 {
116 if ( win == ms_winCritical )
117 return true;
118
119 win = win->GetParent();
120 }
121
122 return false;
123 }
124
125 bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg)
126 {
127 HWND hwnd = msg->hwnd;
128 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
129 wxWindow *wnd;
130
131 // this might happen if we're in a modeless dialog, or if a wx control has
132 // children which themselves were not created by wx (i.e. wxActiveX control children)
133 if ( !wndThis )
134 {
135 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
136 {
137 hwnd = ::GetParent(hwnd);
138
139 // If the control has a wx parent, break and give the parent a chance
140 // to process the window message
141 wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
142 if (wndThis != NULL)
143 break;
144 }
145
146 if ( !wndThis )
147 {
148 // this may happen if the event occurred in a standard modeless dialog (the
149 // only example of which I know of is the find/replace dialog) - then call
150 // IsDialogMessage() to make TAB navigation in it work
151
152 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
153 // them) if we call it for the control itself
154 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
155 }
156 }
157
158 if ( !AllowProcessing(wndThis) )
159 {
160 // not a child of critical window, so we eat the event but take care to
161 // stop an endless stream of WM_PAINTs which would have resulted if we
162 // didn't validate the invalidated part of the window
163 if ( msg->message == WM_PAINT )
164 ::ValidateRect(hwnd, NULL);
165
166 return true;
167 }
168
169 #if wxUSE_TOOLTIPS
170 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
171 // popup the tooltip bubbles
172 if ( msg->message == WM_MOUSEMOVE )
173 {
174 // we should do it if one of window children has an associated tooltip
175 // (and not just if the window has a tooltip itself)
176 if ( wndThis->HasToolTips() )
177 wxToolTip::RelayEvent((WXMSG *)msg);
178 }
179 #endif // wxUSE_TOOLTIPS
180
181 // allow the window to prevent certain messages from being
182 // translated/processed (this is currently used by wxTextCtrl to always
183 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
184 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
185 {
186 return false;
187 }
188
189 // try translations first: the accelerators override everything
190 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
191 {
192 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
193 return true;
194
195 // stop at first top level window, i.e. don't try to process the key
196 // strokes originating in a dialog using the accelerators of the parent
197 // frame - this doesn't make much sense
198 if ( wnd->IsTopLevel() )
199 break;
200 }
201
202 // now try the other hooks (kbd navigation is handled here)
203 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
204 {
205 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
206 return true;
207
208 // also stop at first top level window here, just as above because
209 // if we don't do this, pressing ESC on a modal dialog shown as child
210 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
211 // be closed, for example
212 if ( wnd->IsTopLevel() )
213 break;
214 }
215
216 // no special preprocessing for this message, dispatch it normally
217 return false;
218 }
219
220 void wxGUIEventLoop::ProcessMessage(WXMSG *msg)
221 {
222 // give us the chance to preprocess the message first
223 if ( !PreProcessMessage(msg) )
224 {
225 // if it wasn't done, dispatch it to the corresponding window
226 ::TranslateMessage(msg);
227 ::DispatchMessage(msg);
228 }
229 }
230
231 bool wxGUIEventLoop::Dispatch()
232 {
233 MSG msg;
234 if ( !GetNextMessage(&msg) )
235 return false;
236
237 #if wxUSE_THREADS
238 wxASSERT_MSG( wxThread::IsMain(),
239 wxT("only the main thread can process Windows messages") );
240
241 static bool s_hadGuiLock = true;
242 static wxMsgList s_aSavedMessages;
243
244 // if a secondary thread owning the mutex is doing GUI calls, save all
245 // messages for later processing - we can't process them right now because
246 // it will lead to recursive library calls (and we're not reentrant)
247 if ( !wxGuiOwnedByMainThread() )
248 {
249 s_hadGuiLock = false;
250
251 // leave out WM_COMMAND messages: too dangerous, sometimes
252 // the message will be processed twice
253 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
254 {
255 MSG* pMsg = new MSG(msg);
256 s_aSavedMessages.Append(pMsg);
257 }
258
259 return true;
260 }
261 else
262 {
263 // have we just regained the GUI lock? if so, post all of the saved
264 // messages
265 //
266 // FIXME of course, it's not _exactly_ the same as processing the
267 // messages normally - expect some things to break...
268 if ( !s_hadGuiLock )
269 {
270 s_hadGuiLock = true;
271
272 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
273 while (node)
274 {
275 MSG* pMsg = node->GetData();
276 s_aSavedMessages.Erase(node);
277
278 ProcessMessage(pMsg);
279 delete pMsg;
280
281 node = s_aSavedMessages.GetFirst();
282 }
283 }
284 }
285 #endif // wxUSE_THREADS
286
287 ProcessMessage(&msg);
288
289 return true;
290 }
291
292 void wxGUIEventLoop::OnNextIteration()
293 {
294 #if wxUSE_THREADS
295 wxMutexGuiLeaveOrEnter();
296 #endif // wxUSE_THREADS
297 }
298
299 void wxGUIEventLoop::WakeUp()
300 {
301 ::PostMessage(NULL, WM_NULL, 0, 0);
302 }
303
304 #else // !wxUSE_GUI
305
306 #if wxUSE_CONSOLE_EVENTLOOP
307
308 void wxConsoleEventLoop::OnNextIteration()
309 {
310 if ( wxTheApp )
311 wxTheApp->ProcessPendingEvents();
312 }
313
314 void wxConsoleEventLoop::WakeUp()
315 {
316 #if wxUSE_THREADS
317 wxWakeUpMainThread();
318 #endif
319 }
320
321 bool wxConsoleEventLoop::Dispatch()
322 {
323 MSG msg;
324 if ( !GetNextMessage(&msg) )
325 return false;
326
327 if ( msg.message == WM_TIMER )
328 {
329 TIMERPROC proc = (TIMERPROC)msg.lParam;
330 if ( proc )
331 (*proc)(NULL, 0, msg.wParam, 0);
332 }
333 else
334 {
335 ::DispatchMessage(&msg);
336 }
337
338 return !m_shouldExit;
339 }
340
341 #endif // wxUSE_CONSOLE_EVENTLOOP
342
343 #endif //wxUSE_GUI