]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
Use "<Application> Preferences" as generic wxPreferencesEditor dialog title.
[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(__WINDOWS__) || 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 {
108 wxTheApp->ProcessPendingEvents();
109
110 // One of the pending event handlers could have decided to exit the
111 // loop so check for the flag before trying to dispatch more events
112 // (which could block indefinitely if no more are coming).
113 if ( m_shouldExit )
114 return false;
115 }
116
117 return Dispatch();
118 }
119
120 int wxEventLoopManual::Run()
121 {
122 // event loops are not recursive, you need to create another loop!
123 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
124
125 // ProcessIdle() and ProcessEvents() below may throw so the code here should
126 // be exception-safe, hence we must use local objects for all actions we
127 // should undo
128 wxEventLoopActivator activate(this);
129
130 // we must ensure that OnExit() is called even if an exception is thrown
131 // from inside ProcessEvents() but we must call it from Exit() in normal
132 // situations because it is supposed to be called synchronously,
133 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
134 // something similar here)
135 #if wxUSE_EXCEPTIONS
136 for ( ;; )
137 {
138 try
139 {
140 #endif // wxUSE_EXCEPTIONS
141
142 // this is the event loop itself
143 for ( ;; )
144 {
145 // give them the possibility to do whatever they want
146 OnNextIteration();
147
148 // generate and process idle events for as long as we don't
149 // have anything else to do
150 while ( !m_shouldExit && !Pending() && ProcessIdle() )
151 ;
152
153 if ( m_shouldExit )
154 break;
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 // Process the remaining queued messages, both at the level of the
167 // underlying toolkit level (Pending/Dispatch()) and wx level
168 // (Has/ProcessPendingEvents()).
169 //
170 // We do run the risk of never exiting this loop if pending event
171 // handlers endlessly generate new events but they shouldn't do
172 // this in a well-behaved program and we shouldn't just discard the
173 // events we already have, they might be important.
174 for ( ;; )
175 {
176 bool hasMoreEvents = false;
177 if ( wxTheApp && wxTheApp->HasPendingEvents() )
178 {
179 wxTheApp->ProcessPendingEvents();
180 hasMoreEvents = true;
181 }
182
183 if ( Pending() )
184 {
185 Dispatch();
186 hasMoreEvents = true;
187 }
188
189 if ( !hasMoreEvents )
190 break;
191 }
192
193 #if wxUSE_EXCEPTIONS
194 // exit the outer loop as well
195 break;
196 }
197 catch ( ... )
198 {
199 try
200 {
201 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
202 {
203 OnExit();
204 break;
205 }
206 //else: continue running the event loop
207 }
208 catch ( ... )
209 {
210 // OnException() throwed, possibly rethrowing the same
211 // exception again: very good, but we still need OnExit() to
212 // be called
213 OnExit();
214 throw;
215 }
216 }
217 }
218 #endif // wxUSE_EXCEPTIONS
219
220 return m_exitcode;
221 }
222
223 void wxEventLoopManual::Exit(int rc)
224 {
225 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
226
227 m_exitcode = rc;
228 m_shouldExit = true;
229
230 OnExit();
231
232 // all we have to do to exit from the loop is to (maybe) wake it up so that
233 // it can notice that Exit() had been called
234 //
235 // in particular, do *not* use here calls such as PostQuitMessage() (under
236 // MSW) which terminate the current event loop here because we're not sure
237 // that it is going to be processed by the correct event loop: it would be
238 // possible that another one is started and terminated by mistake if we do
239 // this
240 WakeUp();
241 }
242
243 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__
244