Refactor DND code into Carbon and Cocoa parts, and provide a basic OS X Cocoa impleme...
[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/log.h"
34
35 #include "wx/osx/private.h"
36
37 // ============================================================================
38 // wxEventLoop implementation
39 // ============================================================================
40
41 /*
42 static int CalculateNSEventMaskFromEventCategory(wxEventCategory cat)
43 {
44         NSLeftMouseDownMask     |
45         NSLeftMouseUpMask |
46         NSRightMouseDownMask |
47         NSRightMouseUpMask              = 1 << NSRightMouseUp,
48         NSMouseMovedMask                = 1 << NSMouseMoved,
49         NSLeftMouseDraggedMask          = 1 << NSLeftMouseDragged,
50         NSRightMouseDraggedMask         = 1 << NSRightMouseDragged,
51         NSMouseEnteredMask              = 1 << NSMouseEntered,
52         NSMouseExitedMask               = 1 << NSMouseExited,
53         NSScrollWheelMask               = 1 << NSScrollWheel,
54 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
55         NSTabletPointMask               = 1 << NSTabletPoint,
56         NSTabletProximityMask           = 1 << NSTabletProximity,
57 #endif
58         NSOtherMouseDownMask            = 1 << NSOtherMouseDown,
59         NSOtherMouseUpMask              = 1 << NSOtherMouseUp,
60         NSOtherMouseDraggedMask         = 1 << NSOtherMouseDragged,
61
62
63
64         NSKeyDownMask                   = 1 << NSKeyDown,
65         NSKeyUpMask                     = 1 << NSKeyUp,
66         NSFlagsChangedMask              = 1 << NSFlagsChanged,
67
68         NSAppKitDefinedMask             = 1 << NSAppKitDefined,
69         NSSystemDefinedMask             = 1 << NSSystemDefined,
70         NSApplicationDefinedMask        = 1 << NSApplicationDefined,
71         NSPeriodicMask                  = 1 << NSPeriodic,
72         NSCursorUpdateMask              = 1 << NSCursorUpdate,
73
74         NSAnyEventMask                  = 0xffffffffU
75 }
76 */
77
78 wxGUIEventLoop::wxGUIEventLoop()
79 {
80     m_sleepTime = 0.0;
81 }
82
83 void wxGUIEventLoop::WakeUp()
84 {
85     extern void wxMacWakeUp();
86
87     wxMacWakeUp();
88 }
89
90 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
91 {
92     NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
93     return [nsloop getCFRunLoop];
94 }
95
96 //-----------------------------------------------------------------------------
97 // events dispatch and loop handling
98 //-----------------------------------------------------------------------------
99
100 bool wxGUIEventLoop::Pending() const
101 {
102     wxMacAutoreleasePool autoreleasepool;
103     // a pointer to the event is returned if there is one, or nil if not
104     return [[NSApplication sharedApplication]
105             nextEventMatchingMask: NSAnyEventMask
106             untilDate: nil
107             inMode: NSDefaultRunLoopMode
108             dequeue: NO];
109 }
110
111 bool wxGUIEventLoop::Dispatch()
112 {
113     if ( !wxTheApp )
114         return false;
115
116     wxMacAutoreleasePool autoreleasepool;
117
118     if(NSEvent *event = [NSApp
119                 nextEventMatchingMask:NSAnyEventMask
120                 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
121                 inMode:NSDefaultRunLoopMode
122                 dequeue: YES])
123     {
124         if (wxTheApp)
125             wxTheApp->MacSetCurrentEvent(event, NULL);
126         m_sleepTime = 0.0;
127         [NSApp sendEvent: event];
128     }
129     else
130     {
131         if (wxTheApp)
132             wxTheApp->ProcessPendingEvents();
133         
134         if ( wxTheApp->ProcessIdle() )
135             m_sleepTime = 0.0 ;
136         else
137         {
138             m_sleepTime = 1.0;
139 #if wxUSE_THREADS
140             wxMutexGuiLeave();
141             wxMilliSleep(20);
142             wxMutexGuiEnter();
143 #endif
144         }
145     }
146
147     return true;
148 }
149
150 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
151 {
152 #if wxUSE_THREADS
153     // Yielding from a non-gui thread needs to bail out, otherwise we end up
154     // possibly sending events in the thread too.
155     if ( !wxThread::IsMain() )
156     {
157         return true;
158     }
159 #endif // wxUSE_THREADS
160
161     m_isInsideYield = true;
162     m_eventsToProcessInsideYield = eventsToProcess;
163
164 #if wxUSE_LOG
165     // disable log flushing from here because a call to wxYield() shouldn't
166     // normally result in message boxes popping up &c
167     wxLog::Suspend();
168 #endif // wxUSE_LOG
169
170     // process all pending events:
171     while ( Pending() )
172         Dispatch();
173
174     // it's necessary to call ProcessIdle() to update the frames sizes which
175     // might have been changed (it also will update other things set from
176     // OnUpdateUI() which is a nice (and desired) side effect)
177     while ( ProcessIdle() ) {}
178
179     // if there are pending events, we must process them.
180     if (wxTheApp)
181         wxTheApp->ProcessPendingEvents();
182
183 #if wxUSE_LOG
184     wxLog::Resume();
185 #endif // wxUSE_LOG
186     m_isInsideYield = false;
187
188     return true;
189 }
190
191 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
192 {
193     wxMacAutoreleasePool autoreleasepool;
194
195     NSEvent *event = [NSApp
196                 nextEventMatchingMask:NSAnyEventMask
197                 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
198                 inMode:NSDefaultRunLoopMode
199                 dequeue: YES];
200     if ( !event )
201         return -1;
202
203     [NSApp sendEvent: event];
204
205     return true;
206 }