]>
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> | |
9 | // License: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "evtloop.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/evtloop.h" | |
8cb172b4 JS |
35 | #include "wx/window.h" |
36 | #include "wx/app.h" | |
3808e191 JS |
37 | |
38 | #include "wx/msw/private.h" | |
39 | ||
40 | // ---------------------------------------------------------------------------- | |
41 | // wxEventLoopImpl | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | class WXDLLEXPORT wxEventLoopImpl | |
45 | { | |
46 | public: | |
47 | // ctor | |
48 | wxEventLoopImpl() { SetExitCode(0); } | |
49 | ||
50 | // process a message | |
51 | void ProcessMessage(MSG *msg); | |
52 | ||
53 | // generate an idle message, return TRUE if more idle time requested | |
54 | bool SendIdleMessage(); | |
55 | ||
56 | // set/get the exit code | |
57 | void SetExitCode(int exitcode) { m_exitcode = exitcode; } | |
58 | int GetExitCode() const { return m_exitcode; } | |
59 | ||
60 | private: | |
61 | // preprocess a message, return TRUE if processed (i.e. no further | |
62 | // dispatching required) | |
63 | bool PreProcessMessage(MSG *msg); | |
64 | ||
65 | // the exit code of the event loop | |
66 | int m_exitcode; | |
67 | }; | |
68 | ||
69 | // ============================================================================ | |
70 | // wxEventLoopImpl implementation | |
71 | // ============================================================================ | |
72 | ||
73 | // ---------------------------------------------------------------------------- | |
74 | // wxEventLoopImpl message processing | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
77 | void wxEventLoopImpl::ProcessMessage(MSG *msg) | |
78 | { | |
79 | // give us the chance to preprocess the message first | |
80 | if ( !PreProcessMessage(msg) ) | |
81 | { | |
82 | // if it wasn't done, dispatch it to the corresponding window | |
83 | ::TranslateMessage(msg); | |
84 | ::DispatchMessage(msg); | |
85 | } | |
86 | } | |
87 | ||
88 | bool wxEventLoopImpl::PreProcessMessage(MSG *msg) | |
89 | { | |
90 | HWND hWnd = msg->hwnd; | |
91 | wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd); | |
92 | ||
93 | #if wxUSE_TOOLTIPS | |
94 | // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to | |
95 | // popup the tooltip bubbles | |
96 | if ( wndThis && (msg->message == WM_MOUSEMOVE) ) | |
97 | { | |
98 | wxToolTip *tt = wndThis->GetToolTip(); | |
99 | if ( tt ) | |
100 | { | |
101 | tt->RelayEvent((WXMSG *)msg); | |
102 | } | |
103 | } | |
104 | #endif // wxUSE_TOOLTIPS | |
105 | ||
106 | // try translations first; find the youngest window with a translation | |
107 | // table. | |
108 | wxWindow *wnd; | |
109 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
110 | { | |
111 | if ( wnd->MSWTranslateMessage((WXMSG *)msg) ) | |
112 | return TRUE; | |
113 | } | |
114 | ||
115 | // Anyone for a non-translation message? Try youngest descendants first. | |
116 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
117 | { | |
118 | if ( wnd->MSWProcessMessage((WXMSG *)msg) ) | |
119 | return TRUE; | |
120 | } | |
121 | ||
122 | return FALSE; | |
123 | } | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // wxEventLoopImpl idle event processing | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
129 | bool wxEventLoopImpl::SendIdleMessage() | |
130 | { | |
131 | wxIdleEvent event; | |
132 | ||
133 | return wxTheApp->ProcessEvent(event) && event.MoreRequested(); | |
134 | } | |
135 | ||
136 | // ============================================================================ | |
137 | // wxEventLoop implementation | |
138 | // ============================================================================ | |
139 | ||
140 | // ---------------------------------------------------------------------------- | |
141 | // wxEventLoop running and exiting | |
142 | // ---------------------------------------------------------------------------- | |
143 | ||
144 | wxEventLoop::~wxEventLoop() | |
145 | { | |
146 | wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") ); | |
147 | } | |
148 | ||
149 | bool wxEventLoop::IsRunning() const | |
150 | { | |
151 | return m_impl != NULL; | |
152 | } | |
153 | ||
154 | int wxEventLoop::Run() | |
155 | { | |
156 | // event loops are not recursive, you need to create another loop! | |
157 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
158 | ||
159 | m_impl = new wxEventLoopImpl; | |
160 | ||
161 | for ( ;; ) | |
162 | { | |
163 | #if wxUSE_THREADS | |
164 | wxMutexGuiLeaveOrEnter(); | |
165 | #endif // wxUSE_THREADS | |
166 | ||
167 | // generate and process idle events for as long as we don't have | |
168 | // anything else to do | |
169 | while ( !Pending() && m_impl->SendIdleMessage() ) | |
170 | ; | |
171 | ||
172 | // a message came or no more idle processing to do, sit in Dispatch() | |
173 | // waiting for the next message | |
174 | if ( !Dispatch() ) | |
175 | { | |
176 | // we got WM_QUIT | |
177 | break; | |
178 | } | |
179 | } | |
180 | ||
181 | int exitcode = m_impl->GetExitCode(); | |
182 | delete m_impl; | |
183 | m_impl = NULL; | |
184 | ||
185 | return exitcode; | |
186 | } | |
187 | ||
188 | void wxEventLoop::Exit(int rc) | |
189 | { | |
190 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
191 | ||
192 | m_impl->SetExitCode(rc); | |
193 | ||
194 | ::PostQuitMessage(rc); | |
195 | } | |
196 | ||
197 | // ---------------------------------------------------------------------------- | |
198 | // wxEventLoop message processing dispatching | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
201 | bool wxEventLoop::Pending() const | |
202 | { | |
203 | MSG msg; | |
204 | return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0; | |
205 | } | |
206 | ||
207 | bool wxEventLoop::Dispatch() | |
208 | { | |
209 | wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") ); | |
210 | ||
211 | MSG msg; | |
212 | BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0); | |
213 | ||
214 | if ( rc == 0 ) | |
215 | { | |
216 | // got WM_QUIT | |
217 | return FALSE; | |
218 | } | |
219 | ||
220 | if ( rc == -1 ) | |
221 | { | |
222 | // should never happen, but let's test for it nevertheless | |
223 | wxLogLastError(wxT("GetMessage")); | |
224 | ||
225 | // still break from the loop | |
226 | return FALSE; | |
227 | } | |
228 | ||
229 | #if wxUSE_THREADS | |
230 | wxASSERT_MSG( wxThread::IsMain(), | |
231 | wxT("only the main thread can process Windows messages") ); | |
232 | ||
233 | static bool s_hadGuiLock = TRUE; | |
234 | static wxMsgArray s_aSavedMessages; | |
235 | ||
236 | // if a secondary thread owning the mutex is doing GUI calls, save all | |
237 | // messages for later processing - we can't process them right now because | |
238 | // it will lead to recursive library calls (and we're not reentrant) | |
239 | if ( !wxGuiOwnedByMainThread() ) | |
240 | { | |
241 | s_hadGuiLock = FALSE; | |
242 | ||
243 | // leave out WM_COMMAND messages: too dangerous, sometimes | |
244 | // the message will be processed twice | |
245 | if ( !wxIsWaitingForThread() || | |
246 | s_currentMsg.message != WM_COMMAND ) | |
247 | { | |
248 | s_aSavedMessages.Add(s_currentMsg); | |
249 | } | |
250 | ||
251 | return TRUE; | |
252 | } | |
253 | else | |
254 | { | |
255 | // have we just regained the GUI lock? if so, post all of the saved | |
256 | // messages | |
257 | // | |
258 | // FIXME of course, it's not _exactly_ the same as processing the | |
259 | // messages normally - expect some things to break... | |
260 | if ( !s_hadGuiLock ) | |
261 | { | |
262 | s_hadGuiLock = TRUE; | |
263 | ||
264 | size_t count = s_aSavedMessages.Count(); | |
265 | for ( size_t n = 0; n < count; n++ ) | |
266 | { | |
267 | MSG& msg = s_aSavedMessages[n]; | |
268 | m_impl->ProcessMessage(&msg); | |
269 | } | |
270 | ||
271 | s_aSavedMessages.Empty(); | |
272 | } | |
273 | } | |
274 | #endif // wxUSE_THREADS | |
275 | ||
276 | m_impl->ProcessMessage(&msg); | |
277 | ||
278 | return TRUE; | |
279 | } | |
280 |