| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/osx/carbon/evtloop.cpp |
| 3 | // Purpose: implementation of wxEventLoop for wxMac |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 2006-01-12 |
| 7 | // RCS-ID: $Id$ |
| 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 = kEventDurationNoWait; |
| 42 | } |
| 43 | |
| 44 | void wxGUIEventLoop::WakeUp() |
| 45 | { |
| 46 | extern void wxMacWakeUp(); |
| 47 | |
| 48 | wxMacWakeUp(); |
| 49 | } |
| 50 | |
| 51 | void wxGUIEventLoop::DispatchAndReleaseEvent(EventRef theEvent) |
| 52 | { |
| 53 | if ( wxTheApp ) |
| 54 | wxTheApp->MacSetCurrentEvent( theEvent, NULL ); |
| 55 | |
| 56 | OSStatus status = SendEventToEventTarget(theEvent, GetEventDispatcherTarget()); |
| 57 | if (status == eventNotHandledErr && wxTheApp) |
| 58 | wxTheApp->MacHandleUnhandledEvent(theEvent); |
| 59 | |
| 60 | ReleaseEvent( theEvent ); |
| 61 | } |
| 62 | |
| 63 | bool wxGUIEventLoop::Pending() const |
| 64 | { |
| 65 | EventRef theEvent; |
| 66 | |
| 67 | return ReceiveNextEvent |
| 68 | ( |
| 69 | 0, // we want any event at all so we don't specify neither |
| 70 | NULL, // the number of event types nor the types themselves |
| 71 | kEventDurationNoWait, |
| 72 | false, // don't remove the event from queue |
| 73 | &theEvent |
| 74 | ) == noErr; |
| 75 | } |
| 76 | |
| 77 | bool wxGUIEventLoop::Dispatch() |
| 78 | { |
| 79 | if ( !wxTheApp ) |
| 80 | return false; |
| 81 | |
| 82 | wxMacAutoreleasePool autoreleasepool; |
| 83 | |
| 84 | EventRef theEvent; |
| 85 | |
| 86 | OSStatus status = ReceiveNextEvent(0, NULL, m_sleepTime, true, &theEvent) ; |
| 87 | |
| 88 | switch (status) |
| 89 | { |
| 90 | case eventLoopTimedOutErr : |
| 91 | if ( wxTheApp->ProcessIdle() ) |
| 92 | m_sleepTime = kEventDurationNoWait ; |
| 93 | else |
| 94 | { |
| 95 | m_sleepTime = kEventDurationSecond; |
| 96 | #if wxUSE_THREADS |
| 97 | wxMutexGuiLeave(); |
| 98 | wxMilliSleep(20); |
| 99 | wxMutexGuiEnter(); |
| 100 | #endif |
| 101 | } |
| 102 | break; |
| 103 | |
| 104 | case eventLoopQuitErr : |
| 105 | // according to QA1061 this may also occur |
| 106 | // when a WakeUp Process is executed |
| 107 | break; |
| 108 | |
| 109 | default: |
| 110 | DispatchAndReleaseEvent(theEvent); |
| 111 | m_sleepTime = kEventDurationNoWait ; |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | int wxGUIEventLoop::DispatchTimeout(unsigned long timeout) |
| 119 | { |
| 120 | EventRef event; |
| 121 | OSStatus status = ReceiveNextEvent(0, NULL, timeout/1000, true, &event); |
| 122 | switch ( status ) |
| 123 | { |
| 124 | default: |
| 125 | wxFAIL_MSG( "unexpected ReceiveNextEvent() error" ); |
| 126 | // fall through |
| 127 | |
| 128 | case eventLoopTimedOutErr: |
| 129 | return -1; |
| 130 | |
| 131 | case eventLoopQuitErr: |
| 132 | return 0; |
| 133 | |
| 134 | case noErr: |
| 135 | DispatchAndReleaseEvent(event); |
| 136 | return 1; |
| 137 | } |
| 138 | } |
| 139 | |