]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/evtloop.cpp
Reapplied some previous patches that got lost while merging the Cocoa implementation
[wxWidgets.git] / src / osx / carbon / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/evtloop.cpp
3 // Purpose: implementation of wxEventLoop for wxMac
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 2006-01-12
7 // RCS-ID: $Id$
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 #include "wx/log.h"
32 #endif // WX_PRECOMP
33
34 #include "wx/osx/private.h"
35
36 // ============================================================================
37 // wxEventLoop implementation
38 // ============================================================================
39
40 wxGUIEventLoop::wxGUIEventLoop()
41 {
42 m_sleepTime = kEventDurationNoWait;
43 }
44
45 void wxGUIEventLoop::WakeUp()
46 {
47 extern void wxMacWakeUp();
48
49 wxMacWakeUp();
50 }
51
52 void wxGUIEventLoop::DispatchAndReleaseEvent(EventRef theEvent)
53 {
54 if ( wxTheApp )
55 wxTheApp->MacSetCurrentEvent( theEvent, NULL );
56
57 OSStatus status = SendEventToEventTarget(theEvent, GetEventDispatcherTarget());
58 if (status == eventNotHandledErr && wxTheApp)
59 wxTheApp->MacHandleUnhandledEvent(theEvent);
60
61 ReleaseEvent( theEvent );
62 }
63
64 bool wxGUIEventLoop::Pending() const
65 {
66 EventRef theEvent;
67
68 return ReceiveNextEvent
69 (
70 0, // we want any event at all so we don't specify neither
71 NULL, // the number of event types nor the types themselves
72 kEventDurationNoWait,
73 false, // don't remove the event from queue
74 &theEvent
75 ) == noErr;
76 }
77
78 bool wxGUIEventLoop::Dispatch()
79 {
80 if ( !wxTheApp )
81 return false;
82
83 wxMacAutoreleasePool autoreleasepool;
84
85 EventRef theEvent;
86
87 OSStatus status = ReceiveNextEvent(0, NULL, m_sleepTime, true, &theEvent) ;
88
89 switch (status)
90 {
91 case eventLoopTimedOutErr :
92 if ( wxTheApp->ProcessIdle() )
93 m_sleepTime = kEventDurationNoWait ;
94 else
95 {
96 m_sleepTime = kEventDurationSecond;
97 #if wxUSE_THREADS
98 wxMutexGuiLeave();
99 wxMilliSleep(20);
100 wxMutexGuiEnter();
101 #endif
102 }
103 break;
104
105 case eventLoopQuitErr :
106 // according to QA1061 this may also occur
107 // when a WakeUp Process is executed
108 break;
109
110 default:
111 DispatchAndReleaseEvent(theEvent);
112 m_sleepTime = kEventDurationNoWait ;
113 break;
114 }
115
116 return true;
117 }
118
119 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
120 {
121 EventRef event;
122 OSStatus status = ReceiveNextEvent(0, NULL, timeout/1000, true, &event);
123 switch ( status )
124 {
125 default:
126 wxFAIL_MSG( "unexpected ReceiveNextEvent() error" );
127 // fall through
128
129 case eventLoopTimedOutErr:
130 return -1;
131
132 case eventLoopQuitErr:
133 return 0;
134
135 case noErr:
136 DispatchAndReleaseEvent(event);
137 return 1;
138 }
139 }
140
141 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
142 {
143 #if wxUSE_THREADS
144 // Yielding from a non-gui thread needs to bail out, otherwise we end up
145 // possibly sending events in the thread too.
146 if ( !wxThread::IsMain() )
147 {
148 return true;
149 }
150 #endif // wxUSE_THREADS
151
152 m_isInsideYield = true;
153 m_eventsToProcessInsideYield = eventsToProcess;
154
155 #if wxUSE_LOG
156 // disable log flushing from here because a call to wxYield() shouldn't
157 // normally result in message boxes popping up &c
158 wxLog::Suspend();
159 #endif // wxUSE_LOG
160
161 // process all pending events:
162 while ( Pending() )
163 Dispatch();
164
165 // it's necessary to call ProcessIdle() to update the frames sizes which
166 // might have been changed (it also will update other things set from
167 // OnUpdateUI() which is a nice (and desired) side effect)
168 while ( ProcessIdle() ) {}
169
170 #if wxUSE_LOG
171 wxLog::Resume();
172 #endif // wxUSE_LOG
173 m_isInsideYield = false;
174
175 return true;
176 }