implement DispatchTimeout() for wxOSX/Cocoa
[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/osx/private.h"
34
35 // ============================================================================
36 // wxEventLoop implementation
37 // ============================================================================
38
39 wxGUIEventLoop::wxGUIEventLoop()
40 {
41     m_sleepTime = 0.0;
42 }
43
44 void wxGUIEventLoop::WakeUp()
45 {
46     extern void wxMacWakeUp();
47
48     wxMacWakeUp();
49 }
50
51 bool wxGUIEventLoop::Pending() const
52 {
53     wxMacAutoreleasePool autoreleasepool;
54     // a pointer to the event is returned if there is one, or nil if not
55     return [[NSApplication sharedApplication]
56             nextEventMatchingMask: NSAnyEventMask
57             untilDate: nil
58             inMode: NSDefaultRunLoopMode
59             dequeue: NO];
60 }
61
62 bool wxGUIEventLoop::Dispatch()
63 {
64     if ( !wxTheApp )
65         return false;
66
67     wxMacAutoreleasePool autoreleasepool;
68
69     if(NSEvent *event = [NSApp
70                 nextEventMatchingMask:NSAnyEventMask
71                 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
72                 inMode:NSDefaultRunLoopMode
73                 dequeue: YES])
74     {
75         m_sleepTime = 0.0;
76         [NSApp sendEvent: event];
77     }
78     else
79     {
80         if ( wxTheApp->ProcessIdle() )
81             m_sleepTime = 0.0 ;
82         else
83         {
84             m_sleepTime = 1.0;
85 #if wxUSE_THREADS
86             wxMutexGuiLeave();
87             wxMilliSleep(20);
88             wxMutexGuiEnter();
89 #endif
90         }
91     }
92
93     return true;
94 }
95
96 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
97 {
98     wxMacAutoreleasePool autoreleasepool;
99
100     NSEvent *event = [NSApp
101                 nextEventMatchingMask:NSAnyEventMask
102                 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
103                 inMode:NSDefaultRunLoopMode
104                 dequeue: YES];
105     if ( !event )
106         return -1;
107
108     [NSApp sendEvent: event];
109
110     return true;
111 }