]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
Merge in from trunk r64802 - r68625
[wxWidgets.git] / src / common / evtloopcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/evtloopcmn.cpp
3 // Purpose: common wxEventLoop-related stuff
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 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/evtloop.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #endif //WX_PRECOMP
24
25 // ----------------------------------------------------------------------------
26 // wxEventLoopBase
27 // ----------------------------------------------------------------------------
28
29 wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL;
30
31 wxEventLoopBase::wxEventLoopBase()
32 {
33 m_isInsideYield = false;
34 m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL;
35 }
36
37 bool wxEventLoopBase::IsMain() const
38 {
39 if (wxTheApp)
40 return wxTheApp->GetMainLoop() == this;
41 return false;
42 }
43
44 /* static */
45 void wxEventLoopBase::SetActive(wxEventLoopBase* loop)
46 {
47 ms_activeLoop = loop;
48
49 if (wxTheApp)
50 wxTheApp->OnEventLoopEnter(loop);
51 }
52
53 void wxEventLoopBase::OnExit()
54 {
55 if (wxTheApp)
56 wxTheApp->OnEventLoopExit(this);
57 }
58
59 void wxEventLoopBase::WakeUpIdle()
60 {
61 WakeUp();
62 }
63
64 bool wxEventLoopBase::ProcessIdle()
65 {
66 return wxTheApp && wxTheApp->ProcessIdle();
67 }
68
69 bool wxEventLoopBase::Yield(bool onlyIfNeeded)
70 {
71 if ( m_isInsideYield )
72 {
73 if ( !onlyIfNeeded )
74 {
75 wxFAIL_MSG( wxT("wxYield called recursively" ) );
76 }
77
78 return false;
79 }
80
81 return YieldFor(wxEVT_CATEGORY_ALL);
82 }
83
84 // wxEventLoopManual is unused in the other ports
85 #if defined(__WXMSW__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
86
87 // ============================================================================
88 // wxEventLoopManual implementation
89 // ============================================================================
90
91 wxEventLoopManual::wxEventLoopManual()
92 {
93 m_exitcode = 0;
94 m_shouldExit = false;
95 }
96
97 bool wxEventLoopManual::ProcessEvents()
98 {
99 // process pending wx events first as they correspond to low-level events
100 // which happened before, i.e. typically pending events were queued by a
101 // previous call to Dispatch() and if we didn't process them now the next
102 // call to it might enqueue them again (as happens with e.g. socket events
103 // which would be generated as long as there is input available on socket
104 // and this input is only removed from it when pending event handlers are
105 // executed)
106 if ( wxTheApp )
107 wxTheApp->ProcessPendingEvents();
108
109 return Dispatch();
110 }
111
112 int wxEventLoopManual::Run()
113 {
114 // event loops are not recursive, you need to create another loop!
115 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
116
117 // ProcessIdle() and ProcessEvents() below may throw so the code here should
118 // be exception-safe, hence we must use local objects for all actions we
119 // should undo
120 wxEventLoopActivator activate(this);
121
122 // we must ensure that OnExit() is called even if an exception is thrown
123 // from inside ProcessEvents() but we must call it from Exit() in normal
124 // situations because it is supposed to be called synchronously,
125 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
126 // something similar here)
127 #if wxUSE_EXCEPTIONS
128 for ( ;; )
129 {
130 try
131 {
132 #endif // wxUSE_EXCEPTIONS
133
134 // this is the event loop itself
135 for ( ;; )
136 {
137 // give them the possibility to do whatever they want
138 OnNextIteration();
139
140 // generate and process idle events for as long as we don't
141 // have anything else to do
142 while ( !Pending() && ProcessIdle() && !m_shouldExit )
143 ;
144
145 // if the "should exit" flag is set, the loop should terminate
146 // but not before processing any remaining messages so while
147 // Pending() returns true, do process them
148 if ( m_shouldExit )
149 {
150 while ( Pending() )
151 ProcessEvents();
152
153 break;
154 }
155
156 // a message came or no more idle processing to do, dispatch
157 // all the pending events and call Dispatch() to wait for the
158 // next message
159 if ( !ProcessEvents() )
160 {
161 // we got WM_QUIT
162 break;
163 }
164 }
165
166 #if wxUSE_EXCEPTIONS
167 // exit the outer loop as well
168 break;
169 }
170 catch ( ... )
171 {
172 try
173 {
174 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
175 {
176 OnExit();
177 break;
178 }
179 //else: continue running the event loop
180 }
181 catch ( ... )
182 {
183 // OnException() throwed, possibly rethrowing the same
184 // exception again: very good, but we still need OnExit() to
185 // be called
186 OnExit();
187 throw;
188 }
189 }
190 }
191 #endif // wxUSE_EXCEPTIONS
192
193 return m_exitcode;
194 }
195
196 void wxEventLoopManual::Exit(int rc)
197 {
198 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
199
200 m_exitcode = rc;
201 m_shouldExit = true;
202
203 OnExit();
204
205 // all we have to do to exit from the loop is to (maybe) wake it up so that
206 // it can notice that Exit() had been called
207 //
208 // in particular, do *not* use here calls such as PostQuitMessage() (under
209 // MSW) which terminate the current event loop here because we're not sure
210 // that it is going to be processed by the correct event loop: it would be
211 // possible that another one is started and terminated by mistake if we do
212 // this
213 WakeUp();
214 }
215
216 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__
217