]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/evtloop_cf.cpp
Fix selection corner cases in wxOSX wxComboBox.
[wxWidgets.git] / src / osx / core / evtloop_cf.cpp
CommitLineData
5cd99866
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/core/evtloop_cf.cpp
3// Purpose: wxEventLoop implementation common to both Carbon and Cocoa
4// Author: Vadim Zeitlin
5// Created: 2009-10-18
0056673c 6// RCS-ID: $Id$
5cd99866
VZ
7// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#include "wx/evtloop.h"
27
28#if wxUSE_EVENTLOOP_SOURCE
29
fe5816f0
VZ
30#ifndef WX_PRECOMP
31 #include "wx/log.h"
0056673c 32 #include "wx/app.h"
fe5816f0
VZ
33#endif
34
5cd99866
VZ
35#include "wx/evtloopsrc.h"
36
37#include "wx/scopedptr.h"
38
39#include "wx/osx/private.h"
40#include "wx/osx/core/cfref.h"
41
976e63a7
VZ
42#if wxUSE_GUI
43 #include "wx/nonownedwnd.h"
44#endif
45
5cd99866
VZ
46// ============================================================================
47// wxCFEventLoopSource and wxCFEventLoop implementation
48// ============================================================================
49
50#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
51namespace
52{
53
54void EnableDescriptorCallBacks(CFFileDescriptorRef cffd, int flags)
55{
56 if ( flags & wxEVENT_SOURCE_INPUT )
57 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
58 if ( flags & wxEVENT_SOURCE_OUTPUT )
59 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorWriteCallBack);
60}
61
62void
63wx_cffiledescriptor_callback(CFFileDescriptorRef cffd,
64 CFOptionFlags flags,
65 void *ctxData)
66{
67 wxLogTrace(wxTRACE_EVT_SOURCE,
68 "CFFileDescriptor callback, flags=%d", flags);
69
70 wxCFEventLoopSource * const
71 source = static_cast<wxCFEventLoopSource *>(ctxData);
72
73 wxEventLoopSourceHandler * const
74 handler = source->GetHandler();
75 if ( flags & kCFFileDescriptorReadCallBack )
76 handler->OnReadWaiting();
77 if ( flags & kCFFileDescriptorWriteCallBack )
78 handler->OnWriteWaiting();
79
80 // we need to re-enable callbacks to be called again
81 EnableDescriptorCallBacks(cffd, source->GetFlags());
82}
83
84} // anonymous namespace
85
86wxEventLoopSource *
87wxCFEventLoop::AddSourceForFD(int fd,
88 wxEventLoopSourceHandler *handler,
89 int flags)
90{
91 wxCHECK_MSG( fd != -1, NULL, "can't monitor invalid fd" );
92
93 wxScopedPtr<wxCFEventLoopSource>
94 source(new wxCFEventLoopSource(handler, flags));
95
96 CFFileDescriptorContext ctx = { 0, source.get(), NULL, NULL, NULL };
97 wxCFRef<CFFileDescriptorRef>
98 cffd(CFFileDescriptorCreate
99 (
100 kCFAllocatorDefault,
101 fd,
102 true, // close on invalidate
103 wx_cffiledescriptor_callback,
104 &ctx
105 ));
106 if ( !cffd )
107 return NULL;
108
109 source->SetFileDescriptor(cffd.release());
110
111 wxCFRef<CFRunLoopSourceRef>
112 cfsrc(CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, cffd, 0));
113 if ( !cfsrc )
114 return NULL;
115
116 CFRunLoopRef cfloop = CFGetCurrentRunLoop();
117 CFRunLoopAddSource(cfloop, cfsrc, kCFRunLoopDefaultMode);
118
119 return source.release();
120}
121
122void wxCFEventLoopSource::SetFileDescriptor(CFFileDescriptorRef cffd)
123{
124 wxASSERT_MSG( !m_cffd, "shouldn't be called more than once" );
125
126 m_cffd = cffd;
127}
128
129wxCFEventLoopSource::~wxCFEventLoopSource()
130{
131 if ( m_cffd )
132 CFRelease(m_cffd);
133}
134
135#else // OS X < 10.5
136
137wxEventLoopSource *
138wxCFEventLoop::AddSourceForFD(int WXUNUSED(fd),
139 wxEventLoopSourceHandler * WXUNUSED(handler),
140 int WXUNUSED(flags))
141{
142 return NULL;
143}
144
145#endif // MAC_OS_X_VERSION_MAX_ALLOWED
146
147#endif // wxUSE_EVENTLOOP_SOURCE
0056673c 148
58e74b83 149extern "C" void wxObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
293a13ba
SC
150{
151 wxCFEventLoop * eventloop = static_cast<wxCFEventLoop *>(info);
152 if ( eventloop )
153 eventloop->ObserverCallBack(observer, activity);
976e63a7 154}
293a13ba 155
de5361fa 156void wxCFEventLoop::ObserverCallBack(CFRunLoopObserverRef WXUNUSED(observer), int activity)
293a13ba
SC
157{
158 if ( activity & kCFRunLoopBeforeTimers )
159 {
160 // process pending wx events first as they correspond to low-level events
161 // which happened before, i.e. typically pending events were queued by a
162 // previous call to Dispatch() and if we didn't process them now the next
163 // call to it might enqueue them again (as happens with e.g. socket events
164 // which would be generated as long as there is input available on socket
165 // and this input is only removed from it when pending event handlers are
166 // executed)
167
168 if ( wxTheApp )
169 wxTheApp->ProcessPendingEvents();
170 }
976e63a7 171
293a13ba
SC
172 if ( activity & kCFRunLoopBeforeWaiting )
173 {
174 if ( ProcessIdle() )
175 {
176 WakeUp();
177 }
178 else
179 {
180#if wxUSE_THREADS
181 wxMutexGuiLeave();
182 wxMilliSleep(20);
183 wxMutexGuiEnter();
184#endif
185 }
186 }
187}
188
0056673c
SC
189wxCFEventLoop::wxCFEventLoop()
190{
191 m_shouldExit = false;
976e63a7 192
9aee1212 193 m_runLoop = CFGetCurrentRunLoop();
976e63a7 194
293a13ba
SC
195 CFRunLoopObserverContext ctxt;
196 bzero( &ctxt, sizeof(ctxt) );
197 ctxt.info = this;
976e63a7 198 m_runLoopObserver = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopBeforeTimers | kCFRunLoopBeforeWaiting , true /* repeats */, 0,
58e74b83 199 wxObserverCallBack, &ctxt );
2be05d45 200 CFRunLoopAddObserver(m_runLoop, m_runLoopObserver, kCFRunLoopCommonModes);
58e74b83 201 CFRelease(m_runLoopObserver);
0056673c
SC
202}
203
204wxCFEventLoop::~wxCFEventLoop()
205{
2be05d45 206 CFRunLoopRemoveObserver(m_runLoop, m_runLoopObserver, kCFRunLoopCommonModes);
0056673c 207}
976e63a7 208
0056673c
SC
209
210CFRunLoopRef wxCFEventLoop::CFGetCurrentRunLoop() const
211{
293a13ba 212 return CFRunLoopGetCurrent();
0056673c
SC
213}
214
215void wxCFEventLoop::WakeUp()
216{
58e74b83 217 CFRunLoopWakeUp(m_runLoop);
0056673c
SC
218}
219
3bfba421
SC
220#if wxUSE_BASE
221
222void wxMacWakeUp()
223{
224 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
225
226 if ( loop )
227 loop->WakeUp();
228}
229
230#endif
231
0056673c
SC
232bool wxCFEventLoop::YieldFor(long eventsToProcess)
233{
234#if wxUSE_THREADS
235 // Yielding from a non-gui thread needs to bail out, otherwise we end up
236 // possibly sending events in the thread too.
237 if ( !wxThread::IsMain() )
238 {
239 return true;
240 }
241#endif // wxUSE_THREADS
976e63a7 242
0056673c
SC
243 m_isInsideYield = true;
244 m_eventsToProcessInsideYield = eventsToProcess;
976e63a7 245
0056673c
SC
246#if wxUSE_LOG
247 // disable log flushing from here because a call to wxYield() shouldn't
248 // normally result in message boxes popping up &c
249 wxLog::Suspend();
250#endif // wxUSE_LOG
976e63a7 251
0056673c
SC
252 // process all pending events:
253 while ( DoProcessEvents() == 1 )
254 ;
976e63a7 255
0056673c
SC
256 // it's necessary to call ProcessIdle() to update the frames sizes which
257 // might have been changed (it also will update other things set from
258 // OnUpdateUI() which is a nice (and desired) side effect)
259 while ( ProcessIdle() ) {}
976e63a7 260
0056673c
SC
261 // if there are pending events, we must process them.
262 if (wxTheApp)
263 wxTheApp->ProcessPendingEvents();
976e63a7 264
0056673c
SC
265#if wxUSE_LOG
266 wxLog::Resume();
267#endif // wxUSE_LOG
268 m_isInsideYield = false;
976e63a7 269
0056673c
SC
270 return true;
271}
272
273// implement/override base class pure virtual
274bool wxCFEventLoop::Pending() const
275{
276 return true;
277}
278
279int wxCFEventLoop::DoProcessEvents()
280{
76435717 281 return DispatchTimeout( 0 );
0056673c
SC
282}
283
284bool wxCFEventLoop::Dispatch()
285{
293a13ba 286 return DoProcessEvents() != 0;
0056673c
SC
287}
288
289int wxCFEventLoop::DispatchTimeout(unsigned long timeout)
290{
291 if ( !wxTheApp )
292 return 0;
293
0056673c 294 int status = DoDispatchTimeout(timeout);
976e63a7 295
0056673c
SC
296 switch( status )
297 {
298 case 0:
299 break;
300 case -1:
301 if ( m_shouldExit )
302 return 0;
976e63a7 303
0056673c
SC
304 break;
305 case 1:
0056673c
SC
306 break;
307 }
976e63a7 308
0056673c
SC
309 return status;
310}
311
312int wxCFEventLoop::DoDispatchTimeout(unsigned long timeout)
976e63a7 313{
0056673c
SC
314 SInt32 status = CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout / 1000.0 , true);
315 switch( status )
316 {
317 case kCFRunLoopRunFinished:
318 wxFAIL_MSG( "incorrect run loop state" );
319 break;
320 case kCFRunLoopRunStopped:
321 return 0;
322 break;
323 case kCFRunLoopRunTimedOut:
324 return -1;
325 break;
326 case kCFRunLoopRunHandledSource:
327 default:
328 break;
329 }
330 return 1;
331}
332
80eee837
SC
333void wxCFEventLoop::DoRun()
334{
335 for ( ;; )
336 {
337 // generate and process idle events for as long as we don't
338 // have anything else to do
339 DoProcessEvents();
976e63a7 340
80eee837
SC
341 // if the "should exit" flag is set, the loop should terminate
342 // but not before processing any remaining messages so while
343 // Pending() returns true, do process them
344 if ( m_shouldExit )
345 {
346 while ( DoProcessEvents() == 1 )
347 ;
976e63a7 348
80eee837
SC
349 break;
350 }
351 }
352}
353
354void wxCFEventLoop::DoStop()
355{
356 CFRunLoopStop(CFGetCurrentRunLoop());
357}
358
0056673c
SC
359// enters a loop calling OnNextIteration(), Pending() and Dispatch() and
360// terminating when Exit() is called
361int wxCFEventLoop::Run()
362{
363 // event loops are not recursive, you need to create another loop!
364 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
976e63a7 365
0056673c
SC
366 // ProcessIdle() and ProcessEvents() below may throw so the code here should
367 // be exception-safe, hence we must use local objects for all actions we
368 // should undo
369 wxEventLoopActivator activate(this);
976e63a7 370
0056673c
SC
371 // we must ensure that OnExit() is called even if an exception is thrown
372 // from inside ProcessEvents() but we must call it from Exit() in normal
373 // situations because it is supposed to be called synchronously,
374 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
375 // something similar here)
376#if wxUSE_EXCEPTIONS
377 for ( ;; )
378 {
379 try
380 {
381#endif // wxUSE_EXCEPTIONS
976e63a7 382
80eee837 383 DoRun();
976e63a7 384
0056673c
SC
385#if wxUSE_EXCEPTIONS
386 // exit the outer loop as well
387 break;
388 }
389 catch ( ... )
390 {
391 try
392 {
393 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
394 {
395 OnExit();
396 break;
397 }
398 //else: continue running the event loop
399 }
400 catch ( ... )
401 {
402 // OnException() throwed, possibly rethrowing the same
403 // exception again: very good, but we still need OnExit() to
404 // be called
405 OnExit();
406 throw;
407 }
408 }
409 }
410#endif // wxUSE_EXCEPTIONS
976e63a7 411
0056673c
SC
412 return m_exitcode;
413}
414
415// sets the "should exit" flag and wakes up the loop so that it terminates
416// soon
417void wxCFEventLoop::Exit(int rc)
418{
419 m_exitcode = rc;
420 m_shouldExit = true;
80eee837
SC
421 DoStop();
422}