]>
Commit | Line | Data |
---|---|---|
b503b036 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/cocoa/evtloop.mm | |
3 | // Purpose: implementation of wxEventLoop for OS X | |
4 | // Author: Vadim Zeitlin, Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 2006-01-12 | |
a9a4f229 | 7 | // RCS-ID: $Id$ |
b503b036 SC |
8 | // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org> |
9 | // Licence: 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 | ||
1e04d2bf | 27 | #include "wx/evtloop.h" |
b503b036 SC |
28 | |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
bf06fbce | 31 | #include "wx/nonownedwnd.h" |
b503b036 SC |
32 | #endif // WX_PRECOMP |
33 | ||
f965a844 RR |
34 | #include "wx/log.h" |
35 | ||
b503b036 SC |
36 | #include "wx/osx/private.h" |
37 | ||
38 | // ============================================================================ | |
39 | // wxEventLoop implementation | |
40 | // ============================================================================ | |
41 | ||
e9e8b381 SC |
42 | #if 0 |
43 | ||
44 | // in case we want to integrate this | |
11fed901 | 45 | |
902ddbfd SC |
46 | static NSUInteger CalculateNSEventMaskFromEventCategory(wxEventCategory cat) |
47 | { | |
48 | // the masking system doesn't really help, as only the lowlevel UI events | |
49 | // are split in a useful way, all others are way to broad | |
50 | ||
51 | if ( (cat | wxEVT_CATEGORY_USER_INPUT) && (cat | (~wxEVT_CATEGORY_USER_INPUT) ) ) | |
52 | return NSAnyEventMask; | |
53 | ||
54 | NSUInteger mask = 0; | |
11fed901 | 55 | |
902ddbfd SC |
56 | if ( cat | wxEVT_CATEGORY_USER_INPUT ) |
57 | { | |
58 | mask |= | |
59 | NSLeftMouseDownMask | | |
60 | NSLeftMouseUpMask | | |
61 | NSRightMouseDownMask | | |
62 | NSRightMouseUpMask | | |
63 | NSMouseMovedMask | | |
64 | NSLeftMouseDraggedMask | | |
65 | NSRightMouseDraggedMask | | |
66 | NSMouseEnteredMask | | |
67 | NSMouseExitedMask | | |
68 | NSScrollWheelMask | | |
69 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 | |
70 | NSTabletPointMask | | |
71 | NSTabletProximityMask | | |
72 | #endif | |
73 | NSOtherMouseDownMask | | |
74 | NSOtherMouseUpMask | | |
75 | NSOtherMouseDraggedMask | | |
76 | ||
77 | NSKeyDownMask | | |
78 | NSKeyUpMask | | |
79 | NSFlagsChangedMask | | |
80 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 | |
81 | NSEventMaskGesture | | |
82 | NSEventMaskMagnify | | |
83 | NSEventMaskSwipe | | |
84 | NSEventMaskRotate | | |
85 | NSEventMaskBeginGesture | | |
86 | NSEventMaskEndGesture | | |
87 | #endif | |
88 | 0; | |
89 | } | |
90 | ||
91 | if ( cat | (~wxEVT_CATEGORY_USER_INPUT) ) | |
92 | { | |
93 | mask |= | |
94 | NSAppKitDefinedMask | | |
95 | NSSystemDefinedMask | | |
96 | NSApplicationDefinedMask | | |
97 | NSPeriodicMask | | |
98 | NSCursorUpdateMask; | |
99 | } | |
100 | ||
101 | return mask; | |
102 | } | |
11fed901 | 103 | |
e9e8b381 | 104 | #endif |
11fed901 | 105 | |
902ddbfd SC |
106 | wxGUIEventLoop::wxGUIEventLoop() |
107 | { | |
108 | m_modalSession = nil; | |
109 | m_dummyWindow = nil; | |
0aff141c SC |
110 | m_modalNestedLevel = 0; |
111 | m_modalWindow = NULL; | |
11fed901 | 112 | } |
11fed901 | 113 | |
902ddbfd | 114 | wxGUIEventLoop::~wxGUIEventLoop() |
b503b036 | 115 | { |
902ddbfd SC |
116 | wxASSERT( m_modalSession == nil ); |
117 | wxASSERT( m_dummyWindow == nil ); | |
0aff141c | 118 | wxASSERT( m_modalNestedLevel == 0 ); |
6b8ef0b3 VZ |
119 | } |
120 | ||
121 | //----------------------------------------------------------------------------- | |
122 | // events dispatch and loop handling | |
123 | //----------------------------------------------------------------------------- | |
124 | ||
0056673c SC |
125 | #if 0 |
126 | ||
b503b036 SC |
127 | bool wxGUIEventLoop::Pending() const |
128 | { | |
a765eef3 SC |
129 | #if 0 |
130 | // this code doesn't reliably detect pending events | |
131 | // so better return true and have the dispatch deal with it | |
132 | // as otherwise we end up in a tight loop when idle events are responded | |
133 | // to by RequestMore(true) | |
dbeddfb9 | 134 | wxMacAutoreleasePool autoreleasepool; |
a765eef3 | 135 | |
b503b036 SC |
136 | return [[NSApplication sharedApplication] |
137 | nextEventMatchingMask: NSAnyEventMask | |
138 | untilDate: nil | |
139 | inMode: NSDefaultRunLoopMode | |
a765eef3 SC |
140 | dequeue: NO] != nil; |
141 | #else | |
142 | return true; | |
143 | #endif | |
b503b036 SC |
144 | } |
145 | ||
146 | bool wxGUIEventLoop::Dispatch() | |
147 | { | |
148 | if ( !wxTheApp ) | |
149 | return false; | |
150 | ||
151 | wxMacAutoreleasePool autoreleasepool; | |
152 | ||
153 | if(NSEvent *event = [NSApp | |
154 | nextEventMatchingMask:NSAnyEventMask | |
155 | untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime] | |
156 | inMode:NSDefaultRunLoopMode | |
157 | dequeue: YES]) | |
158 | { | |
0b6f851f SC |
159 | WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent(); |
160 | WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef(); | |
161 | ||
7dab9892 KO |
162 | if (wxTheApp) |
163 | wxTheApp->MacSetCurrentEvent(event, NULL); | |
b503b036 SC |
164 | m_sleepTime = 0.0; |
165 | [NSApp sendEvent: event]; | |
0b6f851f SC |
166 | |
167 | if (wxTheApp) | |
168 | wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler); | |
b503b036 SC |
169 | } |
170 | else | |
171 | { | |
b0a9bfc8 SC |
172 | if (wxTheApp) |
173 | wxTheApp->ProcessPendingEvents(); | |
174 | ||
b503b036 SC |
175 | if ( wxTheApp->ProcessIdle() ) |
176 | m_sleepTime = 0.0 ; | |
177 | else | |
178 | { | |
179 | m_sleepTime = 1.0; | |
180 | #if wxUSE_THREADS | |
181 | wxMutexGuiLeave(); | |
182 | wxMilliSleep(20); | |
183 | wxMutexGuiEnter(); | |
184 | #endif | |
185 | } | |
186 | } | |
187 | ||
188 | return true; | |
189 | } | |
91407318 | 190 | |
0056673c | 191 | #endif |
f965a844 | 192 | |
0056673c | 193 | int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout) |
91407318 VZ |
194 | { |
195 | wxMacAutoreleasePool autoreleasepool; | |
196 | ||
902ddbfd SC |
197 | if ( m_modalSession ) |
198 | { | |
199 | NSInteger response = [NSApp runModalSession:(NSModalSession)m_modalSession]; | |
200 | ||
201 | switch (response) | |
202 | { | |
203 | case NSRunContinuesResponse: | |
204 | { | |
205 | if ( [[NSApplication sharedApplication] | |
206 | nextEventMatchingMask: NSAnyEventMask | |
207 | untilDate: nil | |
208 | inMode: NSDefaultRunLoopMode | |
209 | dequeue: NO] != nil ) | |
210 | return 1; | |
211 | ||
212 | return -1; | |
213 | } | |
214 | ||
215 | case NSRunStoppedResponse: | |
216 | case NSRunAbortedResponse: | |
217 | return -1; | |
218 | default: | |
219 | wxFAIL_MSG("unknown response code"); | |
902ddbfd SC |
220 | break; |
221 | } | |
a624c97f | 222 | return -1; |
902ddbfd SC |
223 | } |
224 | else | |
225 | { | |
226 | NSEvent *event = [NSApp | |
227 | nextEventMatchingMask:NSAnyEventMask | |
228 | untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000] | |
229 | inMode:NSDefaultRunLoopMode | |
230 | dequeue: YES]; | |
231 | ||
232 | if ( event == nil ) | |
233 | return -1; | |
91407318 | 234 | |
902ddbfd | 235 | [NSApp sendEvent: event]; |
91407318 | 236 | |
902ddbfd SC |
237 | return 1; |
238 | } | |
91407318 | 239 | } |
80eee837 SC |
240 | |
241 | void wxGUIEventLoop::DoRun() | |
242 | { | |
243 | wxMacAutoreleasePool autoreleasepool; | |
244 | [NSApp run]; | |
245 | } | |
246 | ||
247 | void wxGUIEventLoop::DoStop() | |
248 | { | |
2e5f9929 SC |
249 | // only calling stop: is not enough when called from a runloop-observer, |
250 | // therefore add a dummy event, to make sure the runloop gets another round | |
85a74f93 SC |
251 | [NSApp stop:0]; |
252 | WakeUp(); | |
253 | } | |
254 | ||
255 | void wxGUIEventLoop::WakeUp() | |
256 | { | |
829c8572 | 257 | wxMacAutoreleasePool autoreleasepool; |
2e5f9929 SC |
258 | NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined |
259 | location:NSMakePoint(0.0, 0.0) | |
260 | modifierFlags:0 | |
261 | timestamp:0 | |
262 | windowNumber:0 | |
263 | context:nil | |
264 | subtype:0 data1:0 data2:0]; | |
265 | [NSApp postEvent:event atStart:FALSE]; | |
80eee837 SC |
266 | } |
267 | ||
7934e447 SC |
268 | CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const |
269 | { | |
270 | NSRunLoop* nsloop = [NSRunLoop currentRunLoop]; | |
271 | return [nsloop getCFRunLoop]; | |
272 | } | |
273 | ||
274 | ||
cfb0ef70 SC |
275 | // TODO move into a evtloop_osx.cpp |
276 | ||
277 | wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow) | |
2439f1d9 | 278 | { |
cfb0ef70 | 279 | m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow); |
2439f1d9 | 280 | wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" ); |
cfb0ef70 | 281 | m_modalNativeWindow = m_modalWindow->GetWXWindow(); |
2439f1d9 SC |
282 | } |
283 | ||
cfb0ef70 SC |
284 | wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow) |
285 | { | |
286 | m_modalWindow = NULL; | |
287 | wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" ); | |
288 | m_modalNativeWindow = modalNativeWindow; | |
289 | } | |
290 | ||
291 | // END move into a evtloop_osx.cpp | |
292 | ||
80eee837 SC |
293 | void wxModalEventLoop::DoRun() |
294 | { | |
295 | wxMacAutoreleasePool pool; | |
296 | ||
297 | // If the app hasn't started, flush the event queue | |
298 | // If we don't do this, the Dock doesn't get the message that | |
299 | // the app has started so will refuse to activate it. | |
300 | [NSApplication sharedApplication]; | |
301 | if (![NSApp isRunning]) | |
302 | { | |
303 | while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES]) | |
304 | { | |
305 | [NSApp sendEvent:event]; | |
306 | } | |
307 | } | |
308 | ||
cfb0ef70 | 309 | [NSApp runModalForWindow:m_modalNativeWindow]; |
80eee837 SC |
310 | } |
311 | ||
312 | void wxModalEventLoop::DoStop() | |
313 | { | |
314 | [NSApp stopModal]; | |
315 | } | |
316 | ||
902ddbfd SC |
317 | void wxGUIEventLoop::BeginModalSession( wxWindow* modalWindow ) |
318 | { | |
319 | WXWindow nsnow = nil; | |
0aff141c SC |
320 | |
321 | if ( m_modalNestedLevel > 0 ) | |
322 | { | |
323 | wxASSERT_MSG( m_modalWindow == modalWindow, "Nested Modal Sessions must be based on same window"); | |
324 | m_modalNestedLevel++; | |
325 | return; | |
326 | } | |
327 | ||
328 | m_modalWindow = modalWindow; | |
329 | m_modalNestedLevel = 1; | |
902ddbfd SC |
330 | |
331 | if ( modalWindow ) | |
332 | { | |
b64af07b SC |
333 | // we must show now, otherwise beginModalSessionForWindow does it but it |
334 | // also would do a centering of the window before overriding all our position | |
335 | if ( !modalWindow->IsShownOnScreen() ) | |
336 | modalWindow->Show(); | |
337 | ||
902ddbfd SC |
338 | wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (modalWindow); |
339 | wxASSERT_MSG( now != NULL, "must pass in a toplevel window for modal event loop" ); | |
340 | nsnow = now ? now->GetWXWindow() : nil; | |
341 | } | |
342 | else | |
343 | { | |
344 | NSRect r = NSMakeRect(10, 10, 0, 0); | |
345 | nsnow = [NSPanel alloc]; | |
346 | [nsnow initWithContentRect:r | |
347 | styleMask:NSBorderlessWindowMask | |
348 | backing:NSBackingStoreBuffered | |
349 | defer:YES | |
350 | ]; | |
351 | [nsnow orderOut:nil]; | |
352 | m_dummyWindow = nsnow; | |
353 | } | |
354 | m_modalSession = [NSApp beginModalSessionForWindow:nsnow]; | |
203ec424 | 355 | wxASSERT_MSG(m_modalSession != NULL, "modal session couldn't be started"); |
902ddbfd SC |
356 | } |
357 | ||
358 | void wxGUIEventLoop::EndModalSession() | |
359 | { | |
360 | wxASSERT_MSG(m_modalSession != NULL, "no modal session active"); | |
0aff141c SC |
361 | |
362 | wxASSERT_MSG(m_modalNestedLevel > 0, "incorrect modal nesting level"); | |
363 | ||
364 | if ( --m_modalNestedLevel == 0 ) | |
902ddbfd | 365 | { |
0aff141c SC |
366 | [NSApp endModalSession:(NSModalSession)m_modalSession]; |
367 | m_modalSession = nil; | |
368 | if ( m_dummyWindow ) | |
369 | { | |
370 | [m_dummyWindow release]; | |
371 | m_dummyWindow = nil; | |
372 | } | |
902ddbfd SC |
373 | } |
374 | } | |
375 | ||
376 | // | |
377 | // | |
378 | // | |
379 | ||
380 | wxWindowDisabler::wxWindowDisabler(bool disable) | |
381 | { | |
382 | m_modalEventLoop = NULL; | |
383 | m_disabled = disable; | |
384 | if ( disable ) | |
385 | DoDisable(); | |
386 | } | |
387 | ||
388 | wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip) | |
389 | { | |
390 | m_disabled = true; | |
391 | DoDisable(winToSkip); | |
392 | } | |
393 | ||
394 | void wxWindowDisabler::DoDisable(wxWindow *winToSkip) | |
395 | { | |
396 | // remember the top level windows which were already disabled, so that we | |
397 | // don't reenable them later | |
398 | m_winDisabled = NULL; | |
399 | ||
400 | wxWindowList::compatibility_iterator node; | |
401 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) | |
402 | { | |
403 | wxWindow *winTop = node->GetData(); | |
404 | if ( winTop == winToSkip ) | |
405 | continue; | |
406 | ||
407 | // we don't need to disable the hidden or already disabled windows | |
408 | if ( winTop->IsEnabled() && winTop->IsShown() ) | |
409 | { | |
410 | winTop->Disable(); | |
411 | } | |
412 | else | |
413 | { | |
414 | if ( !m_winDisabled ) | |
415 | { | |
416 | m_winDisabled = new wxWindowList; | |
417 | } | |
418 | ||
419 | m_winDisabled->Append(winTop); | |
420 | } | |
421 | } | |
422 | ||
423 | m_modalEventLoop = (wxEventLoop*)wxEventLoopBase::GetActive(); | |
424 | m_modalEventLoop->BeginModalSession(winToSkip); | |
425 | } | |
426 | ||
427 | wxWindowDisabler::~wxWindowDisabler() | |
428 | { | |
429 | if ( !m_disabled ) | |
430 | return; | |
431 | ||
432 | m_modalEventLoop->EndModalSession(); | |
433 | ||
434 | wxWindowList::compatibility_iterator node; | |
435 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) | |
436 | { | |
437 | wxWindow *winTop = node->GetData(); | |
438 | if ( !m_winDisabled || !m_winDisabled->Find(winTop) ) | |
439 | { | |
440 | winTop->Enable(); | |
441 | } | |
442 | //else: had been already disabled, don't reenable | |
443 | } | |
444 | ||
445 | delete m_winDisabled; | |
446 | } | |
447 |