]> git.saurik.com Git - wxWidgets.git/blob - src/msw/evtloop.cpp
wx{V,H}SCROLL code is needed on all platforms
[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 #include "wx/window.h"
29 #include "wx/app.h"
30 #endif //WX_PRECOMP
31
32 #include "wx/evtloop.h"
33
34 #include "wx/tooltip.h"
35 #include "wx/except.h"
36 #include "wx/ptr_scpd.h"
37
38 #include "wx/msw/private.h"
39
40 #if wxUSE_THREADS
41 #include "wx/thread.h"
42
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
51 // ============================================================================
52 // wxEventLoop implementation
53 // ============================================================================
54
55 wxWindowMSW *wxEventLoop::ms_winCritical = NULL;
56
57 // ----------------------------------------------------------------------------
58 // ctor/dtor
59 // ----------------------------------------------------------------------------
60
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)
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
82 bool wxEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
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
95 bool wxEventLoop::PreProcessMessage(WXMSG *msg)
96 {
97 HWND hwnd = msg->hwnd;
98 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
99 wxWindow *wnd;
100
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)
103 if ( !wndThis )
104 {
105 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
106 {
107 hwnd = ::GetParent(hwnd);
108
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;
114 }
115
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 }
126 }
127
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
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
142 if ( msg->message == WM_MOUSEMOVE )
143 {
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);
148 }
149 #endif // wxUSE_TOOLTIPS
150
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 {
156 return false;
157 }
158
159 // try translations first: the accelerators override everything
160 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
161 {
162 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
163 return true;
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;
170 }
171
172 // now try the other hooks (kbd navigation is handled here)
173 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
174 {
175 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
176 return true;
177
178 // also stop at first top level window here, just as above because
179 // if we don't do this, pressing ESC on a modal dialog shown as child
180 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
181 // be closed, for example
182 if ( wnd->IsTopLevel() )
183 break;
184 }
185
186 // no special preprocessing for this message, dispatch it normally
187 return false;
188 }
189
190 // ----------------------------------------------------------------------------
191 // wxEventLoop running and exiting
192 // ----------------------------------------------------------------------------
193
194 // ----------------------------------------------------------------------------
195 // wxEventLoopManual customization
196 // ----------------------------------------------------------------------------
197
198 void wxEventLoop::OnNextIteration()
199 {
200 #if wxUSE_THREADS
201 wxMutexGuiLeaveOrEnter();
202 #endif // wxUSE_THREADS
203 }
204
205 void wxEventLoop::WakeUp()
206 {
207 ::PostMessage(NULL, WM_NULL, 0, 0);
208 }
209
210 // ----------------------------------------------------------------------------
211 // wxEventLoop message processing dispatching
212 // ----------------------------------------------------------------------------
213
214 bool wxEventLoop::Pending() const
215 {
216 MSG msg;
217 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
218 }
219
220 bool wxEventLoop::Dispatch()
221 {
222 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
223
224 MSG msg;
225 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
226
227 if ( rc == 0 )
228 {
229 // got WM_QUIT
230 return false;
231 }
232
233 if ( rc == -1 )
234 {
235 // should never happen, but let's test for it nevertheless
236 wxLogLastError(wxT("GetMessage"));
237
238 // still break from the loop
239 return false;
240 }
241
242 #if wxUSE_THREADS
243 wxASSERT_MSG( wxThread::IsMain(),
244 wxT("only the main thread can process Windows messages") );
245
246 static bool s_hadGuiLock = true;
247 static wxMsgList s_aSavedMessages;
248
249 // if a secondary thread owning the mutex is doing GUI calls, save all
250 // messages for later processing - we can't process them right now because
251 // it will lead to recursive library calls (and we're not reentrant)
252 if ( !wxGuiOwnedByMainThread() )
253 {
254 s_hadGuiLock = false;
255
256 // leave out WM_COMMAND messages: too dangerous, sometimes
257 // the message will be processed twice
258 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
259 {
260 MSG* pMsg = new MSG(msg);
261 s_aSavedMessages.Append(pMsg);
262 }
263
264 return true;
265 }
266 else
267 {
268 // have we just regained the GUI lock? if so, post all of the saved
269 // messages
270 //
271 // FIXME of course, it's not _exactly_ the same as processing the
272 // messages normally - expect some things to break...
273 if ( !s_hadGuiLock )
274 {
275 s_hadGuiLock = true;
276
277 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
278 while (node)
279 {
280 MSG* pMsg = node->GetData();
281 s_aSavedMessages.Erase(node);
282
283 ProcessMessage(pMsg);
284 delete pMsg;
285
286 node = s_aSavedMessages.GetFirst();
287 }
288 }
289 }
290 #endif // wxUSE_THREADS
291
292 ProcessMessage(&msg);
293
294 return true;
295 }
296