Blind implementation of YieldFor() for OSX/Cocoa, copied from OSX/Carbon
[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 wxGUIEventLoop::wxGUIEventLoop()
42 {
43     m_sleepTime = 0.0;
44 }
45
46 void wxGUIEventLoop::WakeUp()
47 {
48     extern void wxMacWakeUp();
49
50     wxMacWakeUp();
51 }
52
53 bool wxGUIEventLoop::Pending() const
54 {
55     wxMacAutoreleasePool autoreleasepool;
56     // a pointer to the event is returned if there is one, or nil if not
57     return [[NSApplication sharedApplication]
58             nextEventMatchingMask: NSAnyEventMask
59             untilDate: nil
60             inMode: NSDefaultRunLoopMode
61             dequeue: NO];
62 }
63
64 bool wxGUIEventLoop::Dispatch()
65 {
66     if ( !wxTheApp )
67         return false;
68
69     wxMacAutoreleasePool autoreleasepool;
70
71     if(NSEvent *event = [NSApp
72                 nextEventMatchingMask:NSAnyEventMask
73                 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
74                 inMode:NSDefaultRunLoopMode
75                 dequeue: YES])
76     {
77         m_sleepTime = 0.0;
78         [NSApp sendEvent: event];
79     }
80     else
81     {
82         if ( wxTheApp->ProcessIdle() )
83             m_sleepTime = 0.0 ;
84         else
85         {
86             m_sleepTime = 1.0;
87 #if wxUSE_THREADS
88             wxMutexGuiLeave();
89             wxMilliSleep(20);
90             wxMutexGuiEnter();
91 #endif
92         }
93     }
94
95     return true;
96 }
97
98 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
99 {
100 #if wxUSE_THREADS
101     // Yielding from a non-gui thread needs to bail out, otherwise we end up
102     // possibly sending events in the thread too.
103     if ( !wxThread::IsMain() )
104     {
105         return true;
106     }
107 #endif // wxUSE_THREADS
108
109     m_isInsideYield = true;
110     m_eventsToProcessInsideYield = eventsToProcess;
111
112 #if wxUSE_LOG
113     // disable log flushing from here because a call to wxYield() shouldn't
114     // normally result in message boxes popping up &c
115     wxLog::Suspend();
116 #endif // wxUSE_LOG
117
118     // process all pending events:
119     while ( Pending() )
120         Dispatch();
121
122     // it's necessary to call ProcessIdle() to update the frames sizes which
123     // might have been changed (it also will update other things set from
124     // OnUpdateUI() which is a nice (and desired) side effect)
125     while ( ProcessIdle() ) {}
126
127 #if wxUSE_LOG
128     wxLog::Resume();
129 #endif // wxUSE_LOG
130     m_isInsideYield = false;
131
132     return true;
133 }
134
135 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
136 {
137     wxMacAutoreleasePool autoreleasepool;
138
139     NSEvent *event = [NSApp
140                 nextEventMatchingMask:NSAnyEventMask
141                 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
142                 inMode:NSDefaultRunLoopMode
143                 dequeue: YES];
144     if ( !event )
145         return -1;
146
147     [NSApp sendEvent: event];
148
149     return true;
150 }