pending events detected didn't work properly, go the safe route now, fixes #11797
[wxWidgets.git] / src / osx / cocoa / evtloop.mm
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
7 // RCS-ID:      $Id: evtloop.cpp 54845 2008-07-30 14:52:41Z 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
27 #include "wx/evtloop.h"
28
29 #ifndef WX_PRECOMP
30     #include "wx/app.h"
31 #endif // WX_PRECOMP
32
33 #include "wx/log.h"
34
35 #include "wx/osx/private.h"
36
37 // ============================================================================
38 // wxEventLoop implementation
39 // ============================================================================
40
41 /*
42 static int CalculateNSEventMaskFromEventCategory(wxEventCategory cat)
43 {
44         NSLeftMouseDownMask     |
45         NSLeftMouseUpMask |
46         NSRightMouseDownMask |
47         NSRightMouseUpMask              = 1 << NSRightMouseUp,
48         NSMouseMovedMask                = 1 << NSMouseMoved,
49         NSLeftMouseDraggedMask          = 1 << NSLeftMouseDragged,
50         NSRightMouseDraggedMask         = 1 << NSRightMouseDragged,
51         NSMouseEnteredMask              = 1 << NSMouseEntered,
52         NSMouseExitedMask               = 1 << NSMouseExited,
53         NSScrollWheelMask               = 1 << NSScrollWheel,
54 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
55         NSTabletPointMask               = 1 << NSTabletPoint,
56         NSTabletProximityMask           = 1 << NSTabletProximity,
57 #endif
58         NSOtherMouseDownMask            = 1 << NSOtherMouseDown,
59         NSOtherMouseUpMask              = 1 << NSOtherMouseUp,
60         NSOtherMouseDraggedMask         = 1 << NSOtherMouseDragged,
61
62
63
64         NSKeyDownMask                   = 1 << NSKeyDown,
65         NSKeyUpMask                     = 1 << NSKeyUp,
66         NSFlagsChangedMask              = 1 << NSFlagsChanged,
67
68         NSAppKitDefinedMask             = 1 << NSAppKitDefined,
69         NSSystemDefinedMask             = 1 << NSSystemDefined,
70         NSApplicationDefinedMask        = 1 << NSApplicationDefined,
71         NSPeriodicMask                  = 1 << NSPeriodic,
72         NSCursorUpdateMask              = 1 << NSCursorUpdate,
73
74         NSAnyEventMask                  = 0xffffffffU
75 }
76 */
77
78 wxGUIEventLoop::wxGUIEventLoop()
79 {
80     m_sleepTime = 0.0;
81 }
82
83 void wxGUIEventLoop::WakeUp()
84 {
85     extern void wxMacWakeUp();
86
87     wxMacWakeUp();
88 }
89
90 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
91 {
92     NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
93     return [nsloop getCFRunLoop];
94 }
95
96 //-----------------------------------------------------------------------------
97 // events dispatch and loop handling
98 //-----------------------------------------------------------------------------
99
100 bool wxGUIEventLoop::Pending() const
101 {
102 #if 0
103     // this code doesn't reliably detect pending events
104     // so better return true and have the dispatch deal with it
105     // as otherwise we end up in a tight loop when idle events are responded
106     // to by RequestMore(true)
107     wxMacAutoreleasePool autoreleasepool;
108   
109     return [[NSApplication sharedApplication]
110             nextEventMatchingMask: NSAnyEventMask
111             untilDate: nil
112             inMode: NSDefaultRunLoopMode
113             dequeue: NO] != nil;
114 #else
115     return true;
116 #endif
117 }
118
119 bool wxGUIEventLoop::Dispatch()
120 {
121     if ( !wxTheApp )
122         return false;
123
124     wxMacAutoreleasePool autoreleasepool;
125
126     if(NSEvent *event = [NSApp
127                 nextEventMatchingMask:NSAnyEventMask
128                 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
129                 inMode:NSDefaultRunLoopMode
130                 dequeue: YES])
131     {
132         if (wxTheApp)
133             wxTheApp->MacSetCurrentEvent(event, NULL);
134         m_sleepTime = 0.0;
135         [NSApp sendEvent: event];
136     }
137     else
138     {
139         if (wxTheApp)
140             wxTheApp->ProcessPendingEvents();
141         
142         if ( wxTheApp->ProcessIdle() )
143             m_sleepTime = 0.0 ;
144         else
145         {
146             m_sleepTime = 1.0;
147 #if wxUSE_THREADS
148             wxMutexGuiLeave();
149             wxMilliSleep(20);
150             wxMutexGuiEnter();
151 #endif
152         }
153     }
154
155     return true;
156 }
157
158 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
159 {
160 #if wxUSE_THREADS
161     // Yielding from a non-gui thread needs to bail out, otherwise we end up
162     // possibly sending events in the thread too.
163     if ( !wxThread::IsMain() )
164     {
165         return true;
166     }
167 #endif // wxUSE_THREADS
168
169     m_isInsideYield = true;
170     m_eventsToProcessInsideYield = eventsToProcess;
171
172 #if wxUSE_LOG
173     // disable log flushing from here because a call to wxYield() shouldn't
174     // normally result in message boxes popping up &c
175     wxLog::Suspend();
176 #endif // wxUSE_LOG
177
178     // process all pending events:
179     while ( Pending() )
180         Dispatch();
181
182     // it's necessary to call ProcessIdle() to update the frames sizes which
183     // might have been changed (it also will update other things set from
184     // OnUpdateUI() which is a nice (and desired) side effect)
185     while ( ProcessIdle() ) {}
186
187     // if there are pending events, we must process them.
188     if (wxTheApp)
189         wxTheApp->ProcessPendingEvents();
190
191 #if wxUSE_LOG
192     wxLog::Resume();
193 #endif // wxUSE_LOG
194     m_isInsideYield = false;
195
196     return true;
197 }
198
199 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
200 {
201     wxMacAutoreleasePool autoreleasepool;
202
203     NSEvent *event = [NSApp
204                 nextEventMatchingMask:NSAnyEventMask
205                 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
206                 inMode:NSDefaultRunLoopMode
207                 dequeue: YES];
208     if ( !event )
209         return -1;
210
211     [NSApp sendEvent: event];
212
213     return true;
214 }