]> git.saurik.com Git - wxWidgets.git/blame - src/common/evtloopcmn.cpp
wxVariant <-> wxAny conversion functionality. Includes implicit construction of wxAny...
[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
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
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
VZ
24
25// ----------------------------------------------------------------------------
8b93348e 26// wxEventLoopBase
c8026dea
VZ
27// ----------------------------------------------------------------------------
28
2ddff00c 29wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL;
c8026dea 30
dde19c21
FM
31wxEventLoopBase::wxEventLoopBase()
32{
33 m_isInsideYield = false;
34 m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL;
35}
36
ec38d07d
FM
37bool wxEventLoopBase::IsMain() const
38{
39 if (wxTheApp)
40 return wxTheApp->GetMainLoop() == this;
41 return false;
42}
43
44/* static */
45void wxEventLoopBase::SetActive(wxEventLoopBase* loop)
46{
47 ms_activeLoop = loop;
48
49 if (wxTheApp)
50 wxTheApp->OnEventLoopEnter(loop);
51}
52
53void wxEventLoopBase::OnExit()
54{
55 if (wxTheApp)
56 wxTheApp->OnEventLoopExit(this);
57}
58
dde19c21
FM
59void wxEventLoopBase::WakeUpIdle()
60{
61 WakeUp();
62}
63
64bool wxEventLoopBase::ProcessIdle()
65{
a758f601 66 return wxTheApp && wxTheApp->ProcessIdle();
dde19c21
FM
67}
68
69bool 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
52c9b349 84// wxEventLoopManual is unused in the other ports
80eee837 85#if defined(__WXMSW__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
52c9b349 86
c8026dea
VZ
87// ============================================================================
88// wxEventLoopManual implementation
89// ============================================================================
90
91wxEventLoopManual::wxEventLoopManual()
92{
93 m_exitcode = 0;
94 m_shouldExit = false;
95}
96
26bacb82
VZ
97bool 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
c8026dea
VZ
112int wxEventLoopManual::Run()
113{
114 // event loops are not recursive, you need to create another loop!
9a83f860 115 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
c8026dea 116
26bacb82 117 // ProcessIdle() and ProcessEvents() below may throw so the code here should
c8026dea
VZ
118 // be exception-safe, hence we must use local objects for all actions we
119 // should undo
b46b1d59 120 wxEventLoopActivator activate(this);
c8026dea
VZ
121
122 // we must ensure that OnExit() is called even if an exception is thrown
26bacb82 123 // from inside ProcessEvents() but we must call it from Exit() in normal
c8026dea
VZ
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
a758f601 142 while ( !Pending() && ProcessIdle() )
c8026dea
VZ
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() )
26bacb82 151 ProcessEvents();
c8026dea
VZ
152
153 break;
154 }
155
26bacb82
VZ
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() )
c8026dea
VZ
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
196void wxEventLoopManual::Exit(int rc)
197{
9a83f860 198 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
c8026dea
VZ
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
b3c86150 216#endif // __WXMSW__ || __WXMAC__ || __WXDFB__
9af42efd 217