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