1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/evtloop.cpp
3 // Purpose: implementation of wxEventLoop for wxMac
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/evtloop.h"
35 #include "wx/nonownedwnd.h"
38 #include "wx/osx/private.h"
40 // ============================================================================
41 // wxEventLoop implementation
42 // ============================================================================
44 wxGUIEventLoop::wxGUIEventLoop()
48 static void DispatchAndReleaseEvent(EventRef theEvent
)
51 wxTheApp
->MacSetCurrentEvent( theEvent
, NULL
);
53 OSStatus status
= SendEventToEventTarget(theEvent
, GetEventDispatcherTarget());
54 if (status
== eventNotHandledErr
&& wxTheApp
)
55 wxTheApp
->MacHandleUnhandledEvent(theEvent
);
57 ReleaseEvent( theEvent
);
60 int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout
)
62 wxMacAutoreleasePool autoreleasepool
;
65 OSStatus status
= ReceiveNextEvent(0, NULL
, timeout
/1000, true, &event
);
69 wxFAIL_MSG( "unexpected ReceiveNextEvent() error" );
72 case eventLoopTimedOutErr
:
75 case eventLoopQuitErr
:
76 // according to QA1061 this may also occur
77 // when a WakeUp Process is executed
81 DispatchAndReleaseEvent(event
);
86 void wxGUIEventLoop::WakeUp()
89 wxMacCarbonEvent wakeupEvent
;
90 wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
91 kEventAttributeNone
);
92 err
= PostEventToQueue(GetMainEventQueue(), wakeupEvent
,
96 void wxGUIEventLoop::OSXDoRun()
98 wxMacAutoreleasePool autoreleasepool
;
100 while (!m_shouldExit
)
102 RunApplicationEventLoop();
105 // Force enclosing event loop to temporarily exit and check
106 // if it should be stopped.
107 QuitApplicationEventLoop();
110 void wxGUIEventLoop::OSXDoStop()
112 QuitApplicationEventLoop();
115 CFRunLoopRef
wxGUIEventLoop::CFGetCurrentRunLoop() const
117 return wxCFEventLoop::CFGetCurrentRunLoop();
120 // TODO move into a evtloop_osx.cpp
122 wxModalEventLoop::wxModalEventLoop(wxWindow
*modalWindow
)
124 m_modalWindow
= wxDynamicCast(modalWindow
, wxNonOwnedWindow
);
125 wxASSERT_MSG( m_modalWindow
!= NULL
, "must pass in a toplevel window for modal event loop" );
126 m_modalNativeWindow
= m_modalWindow
->GetWXWindow();
129 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow
)
131 m_modalWindow
= NULL
;
132 wxASSERT_MSG( modalNativeWindow
!= NULL
, "must pass in a toplevel window for modal event loop" );
133 m_modalNativeWindow
= modalNativeWindow
;
136 // END move into a evtloop_osx.cpp
138 void wxModalEventLoop::OSXDoRun()
140 wxWindowDisabler
disabler(m_modalWindow
);
141 wxMacAutoreleasePool autoreleasepool
;
143 bool resetGroupParent
= false;
145 WindowGroupRef windowGroup
= NULL
;
146 WindowGroupRef formerParentGroup
= NULL
;
148 // make sure modal dialogs are in the right layer so that they are not covered
149 if ( m_modalWindow
!= NULL
)
151 if ( m_modalWindow
->GetParent() == NULL
)
153 windowGroup
= GetWindowGroup(m_modalNativeWindow
) ;
154 if ( windowGroup
!= GetWindowGroupOfClass( kMovableModalWindowClass
) )
156 formerParentGroup
= GetWindowGroupParent( windowGroup
);
157 SetWindowGroupParent( windowGroup
, GetWindowGroupOfClass( kMovableModalWindowClass
) );
158 resetGroupParent
= true;
163 m_modalWindow
->SetFocus();
165 RunAppModalLoopForWindow(m_modalNativeWindow
);
167 if ( resetGroupParent
)
169 SetWindowGroupParent( windowGroup
, formerParentGroup
);
174 void wxModalEventLoop::OSXDoStop()
176 wxMacAutoreleasePool autoreleasepool
;
177 QuitAppModalLoopForWindow(m_modalNativeWindow
);