]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
Add wxUSE_PREFERENCES_EDITOR and the corresponding configure option.
[wxWidgets.git] / src / common / evtloopcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/evtloopcmn.cpp
3 // Purpose: common wxEventLoop-related stuff
4 // Author: Vadim Zeitlin
5 // Created: 2006-01-12
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006, 2013 Vadim Zeitlin <vadim@wxwindows.org>
8 // (c) 2013 Rob Bresalier
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 #include "wx/scopeguard.h"
26 #include "wx/apptrait.h"
27 #include "wx/private/eventloopsourcesmanager.h"
28
29 // ----------------------------------------------------------------------------
30 // wxEventLoopBase
31 // ----------------------------------------------------------------------------
32
33 wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL;
34
35 wxEventLoopBase::wxEventLoopBase()
36 {
37 m_isInsideRun = false;
38 m_shouldExit = false;
39
40 m_isInsideYield = false;
41 m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL;
42 }
43
44 bool wxEventLoopBase::IsMain() const
45 {
46 if (wxTheApp)
47 return wxTheApp->GetMainLoop() == this;
48 return false;
49 }
50
51 /* static */
52 void wxEventLoopBase::SetActive(wxEventLoopBase* loop)
53 {
54 ms_activeLoop = loop;
55
56 if (wxTheApp)
57 wxTheApp->OnEventLoopEnter(loop);
58 }
59
60 int wxEventLoopBase::Run()
61 {
62 // event loops are not recursive, you need to create another loop!
63 wxCHECK_MSG( !IsInsideRun(), -1, wxT("can't reenter a message loop") );
64
65 // ProcessIdle() and ProcessEvents() below may throw so the code here should
66 // be exception-safe, hence we must use local objects for all actions we
67 // should undo
68 wxEventLoopActivator activate(this);
69
70 // We might be called again, after a previous call to ScheduleExit(), so
71 // reset this flag.
72 m_shouldExit = false;
73
74 // Set this variable to true for the duration of this method.
75 m_isInsideRun = true;
76 wxON_BLOCK_EXIT_SET(m_isInsideRun, false);
77
78 // Finally really run the loop.
79 return DoRun();
80 }
81
82 void wxEventLoopBase::Exit(int rc)
83 {
84 wxCHECK_RET( IsRunning(), wxS("Use ScheduleExit() on not running loop") );
85
86 ScheduleExit(rc);
87 }
88
89 void wxEventLoopBase::OnExit()
90 {
91 if (wxTheApp)
92 wxTheApp->OnEventLoopExit(this);
93 }
94
95 void wxEventLoopBase::WakeUpIdle()
96 {
97 WakeUp();
98 }
99
100 bool wxEventLoopBase::ProcessIdle()
101 {
102 return wxTheApp && wxTheApp->ProcessIdle();
103 }
104
105 bool wxEventLoopBase::Yield(bool onlyIfNeeded)
106 {
107 if ( m_isInsideYield )
108 {
109 if ( !onlyIfNeeded )
110 {
111 wxFAIL_MSG( wxT("wxYield called recursively" ) );
112 }
113
114 return false;
115 }
116
117 return YieldFor(wxEVT_CATEGORY_ALL);
118 }
119
120 #if wxUSE_EVENTLOOP_SOURCE
121
122 wxEventLoopSource*
123 wxEventLoopBase::AddSourceForFD(int fd,
124 wxEventLoopSourceHandler *handler,
125 int flags)
126 {
127 #if wxUSE_CONSOLE_EVENTLOOP
128 // Delegate to the event loop sources manager defined by it.
129 wxEventLoopSourcesManagerBase* const
130 manager = wxApp::GetValidTraits().GetEventLoopSourcesManager();
131 wxCHECK_MSG( manager, NULL, wxS("Must have wxEventLoopSourcesManager") );
132
133 return manager->AddSourceForFD(fd, handler, flags);
134 #else // !wxUSE_CONSOLE_EVENTLOOP
135 return NULL;
136 #endif // wxUSE_CONSOLE_EVENTLOOP/!wxUSE_CONSOLE_EVENTLOOP
137 }
138
139 #endif // wxUSE_EVENTLOOP_SOURCE
140
141 // wxEventLoopManual is unused in the other ports
142 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
143
144 // ============================================================================
145 // wxEventLoopManual implementation
146 // ============================================================================
147
148 wxEventLoopManual::wxEventLoopManual()
149 {
150 m_exitcode = 0;
151 }
152
153 bool wxEventLoopManual::ProcessEvents()
154 {
155 // process pending wx events first as they correspond to low-level events
156 // which happened before, i.e. typically pending events were queued by a
157 // previous call to Dispatch() and if we didn't process them now the next
158 // call to it might enqueue them again (as happens with e.g. socket events
159 // which would be generated as long as there is input available on socket
160 // and this input is only removed from it when pending event handlers are
161 // executed)
162 if ( wxTheApp )
163 {
164 wxTheApp->ProcessPendingEvents();
165
166 // One of the pending event handlers could have decided to exit the
167 // loop so check for the flag before trying to dispatch more events
168 // (which could block indefinitely if no more are coming).
169 if ( m_shouldExit )
170 return false;
171 }
172
173 return Dispatch();
174 }
175
176 int wxEventLoopManual::DoRun()
177 {
178 // we must ensure that OnExit() is called even if an exception is thrown
179 // from inside ProcessEvents() but we must call it from Exit() in normal
180 // situations because it is supposed to be called synchronously,
181 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
182 // something similar here)
183 #if wxUSE_EXCEPTIONS
184 for ( ;; )
185 {
186 try
187 {
188 #endif // wxUSE_EXCEPTIONS
189
190 // this is the event loop itself
191 for ( ;; )
192 {
193 // give them the possibility to do whatever they want
194 OnNextIteration();
195
196 // generate and process idle events for as long as we don't
197 // have anything else to do
198 while ( !m_shouldExit && !Pending() && ProcessIdle() )
199 ;
200
201 if ( m_shouldExit )
202 break;
203
204 // a message came or no more idle processing to do, dispatch
205 // all the pending events and call Dispatch() to wait for the
206 // next message
207 if ( !ProcessEvents() )
208 {
209 // we got WM_QUIT
210 break;
211 }
212 }
213
214 // Process the remaining queued messages, both at the level of the
215 // underlying toolkit level (Pending/Dispatch()) and wx level
216 // (Has/ProcessPendingEvents()).
217 //
218 // We do run the risk of never exiting this loop if pending event
219 // handlers endlessly generate new events but they shouldn't do
220 // this in a well-behaved program and we shouldn't just discard the
221 // events we already have, they might be important.
222 for ( ;; )
223 {
224 bool hasMoreEvents = false;
225 if ( wxTheApp && wxTheApp->HasPendingEvents() )
226 {
227 wxTheApp->ProcessPendingEvents();
228 hasMoreEvents = true;
229 }
230
231 if ( Pending() )
232 {
233 Dispatch();
234 hasMoreEvents = true;
235 }
236
237 if ( !hasMoreEvents )
238 break;
239 }
240
241 #if wxUSE_EXCEPTIONS
242 // exit the outer loop as well
243 break;
244 }
245 catch ( ... )
246 {
247 try
248 {
249 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
250 {
251 OnExit();
252 break;
253 }
254 //else: continue running the event loop
255 }
256 catch ( ... )
257 {
258 // OnException() throwed, possibly rethrowing the same
259 // exception again: very good, but we still need OnExit() to
260 // be called
261 OnExit();
262 throw;
263 }
264 }
265 }
266 #endif // wxUSE_EXCEPTIONS
267
268 return m_exitcode;
269 }
270
271 void wxEventLoopManual::ScheduleExit(int rc)
272 {
273 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not running") );
274
275 m_exitcode = rc;
276 m_shouldExit = true;
277
278 OnExit();
279
280 // all we have to do to exit from the loop is to (maybe) wake it up so that
281 // it can notice that Exit() had been called
282 //
283 // in particular, do *not* use here calls such as PostQuitMessage() (under
284 // MSW) which terminate the current event loop here because we're not sure
285 // that it is going to be processed by the correct event loop: it would be
286 // possible that another one is started and terminated by mistake if we do
287 // this
288 WakeUp();
289 }
290
291 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__
292