1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/evtloop.cpp
3 // Purpose: implementation of wxEventLoop for wxMac
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/evtloop.h"
34 #include "wx/nonownedwnd.h"
37 #include "wx/osx/private.h"
39 // ============================================================================
40 // wxEventLoop implementation
41 // ============================================================================
43 wxGUIEventLoop::wxGUIEventLoop()
47 static void DispatchAndReleaseEvent(EventRef theEvent
)
50 wxTheApp
->MacSetCurrentEvent( theEvent
, NULL
);
52 OSStatus status
= SendEventToEventTarget(theEvent
, GetEventDispatcherTarget());
53 if (status
== eventNotHandledErr
&& wxTheApp
)
54 wxTheApp
->MacHandleUnhandledEvent(theEvent
);
56 ReleaseEvent( theEvent
);
59 int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout
)
61 wxMacAutoreleasePool autoreleasepool
;
64 OSStatus status
= ReceiveNextEvent(0, NULL
, timeout
/1000, true, &event
);
68 wxFAIL_MSG( "unexpected ReceiveNextEvent() error" );
71 case eventLoopTimedOutErr
:
74 case eventLoopQuitErr
:
75 // according to QA1061 this may also occur
76 // when a WakeUp Process is executed
80 DispatchAndReleaseEvent(event
);
85 void wxGUIEventLoop::WakeUp()
88 wxMacCarbonEvent wakeupEvent
;
89 wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
90 kEventAttributeNone
);
91 err
= PostEventToQueue(GetMainEventQueue(), wakeupEvent
,
95 void wxGUIEventLoop::OSXDoRun()
97 wxMacAutoreleasePool autoreleasepool
;
101 RunApplicationEventLoop();
104 // Force enclosing event loop to temporarily exit and check
105 // if it should be stopped.
106 QuitApplicationEventLoop();
109 void wxGUIEventLoop::OSXDoStop()
111 QuitApplicationEventLoop();
114 CFRunLoopRef
wxGUIEventLoop::CFGetCurrentRunLoop() const
116 return wxCFEventLoop::CFGetCurrentRunLoop();
119 // TODO move into a evtloop_osx.cpp
121 wxModalEventLoop::wxModalEventLoop(wxWindow
*modalWindow
)
123 m_modalWindow
= wxDynamicCast(modalWindow
, wxNonOwnedWindow
);
124 wxASSERT_MSG( m_modalWindow
!= NULL
, "must pass in a toplevel window for modal event loop" );
125 m_modalNativeWindow
= m_modalWindow
->GetWXWindow();
128 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow
)
130 m_modalWindow
= NULL
;
131 wxASSERT_MSG( modalNativeWindow
!= NULL
, "must pass in a toplevel window for modal event loop" );
132 m_modalNativeWindow
= modalNativeWindow
;
135 // END move into a evtloop_osx.cpp
137 void wxModalEventLoop::OSXDoRun()
139 wxWindowDisabler
disabler(m_modalWindow
);
140 wxMacAutoreleasePool autoreleasepool
;
142 bool resetGroupParent
= false;
144 WindowGroupRef windowGroup
= NULL
;
145 WindowGroupRef formerParentGroup
= NULL
;
147 // make sure modal dialogs are in the right layer so that they are not covered
148 if ( m_modalWindow
!= NULL
)
150 if ( m_modalWindow
->GetParent() == NULL
)
152 windowGroup
= GetWindowGroup(m_modalNativeWindow
) ;
153 if ( windowGroup
!= GetWindowGroupOfClass( kMovableModalWindowClass
) )
155 formerParentGroup
= GetWindowGroupParent( windowGroup
);
156 SetWindowGroupParent( windowGroup
, GetWindowGroupOfClass( kMovableModalWindowClass
) );
157 resetGroupParent
= true;
162 m_modalWindow
->SetFocus();
164 RunAppModalLoopForWindow(m_modalNativeWindow
);
166 if ( resetGroupParent
)
168 SetWindowGroupParent( windowGroup
, formerParentGroup
);
173 void wxModalEventLoop::OSXDoStop()
175 wxMacAutoreleasePool autoreleasepool
;
176 QuitAppModalLoopForWindow(m_modalNativeWindow
);