]> git.saurik.com Git - wxWidgets.git/blame - src/common/evtloopcmn.cpp
Generate text events in generic wxSpinCtrl itself.
[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
d98a58c5 85#if defined(__WINDOWS__) || 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 )
586c4551 107 {
26bacb82
VZ
108 wxTheApp->ProcessPendingEvents();
109
586c4551
VZ
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).
34c5aaa7 113 if ( m_shouldExit )
586c4551 114 return false;
586c4551
VZ
115 }
116
26bacb82
VZ
117 return Dispatch();
118}
119
c8026dea
VZ
120int wxEventLoopManual::Run()
121{
122 // event loops are not recursive, you need to create another loop!
9a83f860 123 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
c8026dea 124
26bacb82 125 // ProcessIdle() and ProcessEvents() below may throw so the code here should
c8026dea
VZ
126 // be exception-safe, hence we must use local objects for all actions we
127 // should undo
b46b1d59 128 wxEventLoopActivator activate(this);
c8026dea
VZ
129
130 // we must ensure that OnExit() is called even if an exception is thrown
26bacb82 131 // from inside ProcessEvents() but we must call it from Exit() in normal
c8026dea
VZ
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
34c5aaa7 150 while ( !m_shouldExit && !Pending() && ProcessIdle() )
c8026dea
VZ
151 ;
152
c8026dea 153 if ( m_shouldExit )
c8026dea 154 break;
c8026dea 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
34c5aaa7
VZ
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
c8026dea
VZ
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
223void wxEventLoopManual::Exit(int rc)
224{
9a83f860 225 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
c8026dea
VZ
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
d98a58c5 243#endif // __WINDOWS__ || __WXMAC__ || __WXDFB__
9af42efd 244