osx-cocoa event loop
[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     // a pointer to the event is returned if there is one, or nil if not
54     return [[NSApplication sharedApplication]
55             nextEventMatchingMask: NSAnyEventMask
56             untilDate: nil
57             inMode: NSDefaultRunLoopMode
58             dequeue: NO];
59 }
60
61 bool wxGUIEventLoop::Dispatch()
62 {
63     if ( !wxTheApp )
64         return false;
65
66     wxMacAutoreleasePool autoreleasepool;
67
68     if(NSEvent *event = [NSApp
69                 nextEventMatchingMask:NSAnyEventMask
70                 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
71                 inMode:NSDefaultRunLoopMode
72                 dequeue: YES])
73     {
74         m_sleepTime = 0.0;
75         [NSApp sendEvent: event];
76     }
77     else
78     {
79         if ( wxTheApp->ProcessIdle() )
80             m_sleepTime = 0.0 ;
81         else
82         {
83             m_sleepTime = 1.0;
84 #if wxUSE_THREADS
85             wxMutexGuiLeave();
86             wxMilliSleep(20);
87             wxMutexGuiEnter();
88 #endif
89         }
90     }
91
92     return true;
93 }