]> git.saurik.com Git - wxWidgets.git/blame - src/common/evtloopcmn.cpp
Fixed inconsistent calculation of line height in paragraph layout
[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 {
1b29603d
VZ
108 const bool hadExitedBefore = m_shouldExit;
109
26bacb82
VZ
110 wxTheApp->ProcessPendingEvents();
111
586c4551
VZ
112 // One of the pending event handlers could have decided to exit the
113 // loop so check for the flag before trying to dispatch more events
114 // (which could block indefinitely if no more are coming).
1b29603d 115 if ( !hadExitedBefore && m_shouldExit )
586c4551
VZ
116 {
117 // We still need to dispatch any remaining pending events, just as
118 // we do in the event loop in Run() if the loop is exited from a
119 // normal event handler.
120 while ( wxTheApp->HasPendingEvents() )
121 wxTheApp->ProcessPendingEvents();
122
123 return false;
124 }
125 }
126
26bacb82
VZ
127 return Dispatch();
128}
129
c8026dea
VZ
130int wxEventLoopManual::Run()
131{
132 // event loops are not recursive, you need to create another loop!
9a83f860 133 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
c8026dea 134
26bacb82 135 // ProcessIdle() and ProcessEvents() below may throw so the code here should
c8026dea
VZ
136 // be exception-safe, hence we must use local objects for all actions we
137 // should undo
b46b1d59 138 wxEventLoopActivator activate(this);
c8026dea
VZ
139
140 // we must ensure that OnExit() is called even if an exception is thrown
26bacb82 141 // from inside ProcessEvents() but we must call it from Exit() in normal
c8026dea
VZ
142 // situations because it is supposed to be called synchronously,
143 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
144 // something similar here)
145#if wxUSE_EXCEPTIONS
146 for ( ;; )
147 {
148 try
149 {
150#endif // wxUSE_EXCEPTIONS
151
152 // this is the event loop itself
153 for ( ;; )
154 {
155 // give them the possibility to do whatever they want
156 OnNextIteration();
157
158 // generate and process idle events for as long as we don't
159 // have anything else to do
e819ca3a 160 while ( !Pending() && ProcessIdle() && !m_shouldExit )
c8026dea
VZ
161 ;
162
163 // if the "should exit" flag is set, the loop should terminate
164 // but not before processing any remaining messages so while
165 // Pending() returns true, do process them
166 if ( m_shouldExit )
167 {
168 while ( Pending() )
26bacb82 169 ProcessEvents();
c8026dea
VZ
170
171 break;
172 }
173
26bacb82
VZ
174 // a message came or no more idle processing to do, dispatch
175 // all the pending events and call Dispatch() to wait for the
176 // next message
177 if ( !ProcessEvents() )
c8026dea
VZ
178 {
179 // we got WM_QUIT
180 break;
181 }
182 }
183
184#if wxUSE_EXCEPTIONS
185 // exit the outer loop as well
186 break;
187 }
188 catch ( ... )
189 {
190 try
191 {
192 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
193 {
194 OnExit();
195 break;
196 }
197 //else: continue running the event loop
198 }
199 catch ( ... )
200 {
201 // OnException() throwed, possibly rethrowing the same
202 // exception again: very good, but we still need OnExit() to
203 // be called
204 OnExit();
205 throw;
206 }
207 }
208 }
209#endif // wxUSE_EXCEPTIONS
210
211 return m_exitcode;
212}
213
214void wxEventLoopManual::Exit(int rc)
215{
9a83f860 216 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
c8026dea
VZ
217
218 m_exitcode = rc;
219 m_shouldExit = true;
220
221 OnExit();
222
223 // all we have to do to exit from the loop is to (maybe) wake it up so that
224 // it can notice that Exit() had been called
225 //
226 // in particular, do *not* use here calls such as PostQuitMessage() (under
227 // MSW) which terminate the current event loop here because we're not sure
228 // that it is going to be processed by the correct event loop: it would be
229 // possible that another one is started and terminated by mistake if we do
230 // this
231 WakeUp();
232}
233
d98a58c5 234#endif // __WINDOWS__ || __WXMAC__ || __WXDFB__
9af42efd 235