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