]>
Commit | Line | Data |
---|---|---|
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 | 29 | wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL; |
c8026dea | 30 | |
dde19c21 FM |
31 | wxEventLoopBase::wxEventLoopBase() |
32 | { | |
33 | m_isInsideYield = false; | |
34 | m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL; | |
35 | } | |
36 | ||
ec38d07d FM |
37 | bool wxEventLoopBase::IsMain() const |
38 | { | |
39 | if (wxTheApp) | |
40 | return wxTheApp->GetMainLoop() == this; | |
41 | return false; | |
42 | } | |
43 | ||
44 | /* static */ | |
45 | void wxEventLoopBase::SetActive(wxEventLoopBase* loop) | |
46 | { | |
47 | ms_activeLoop = loop; | |
48 | ||
49 | if (wxTheApp) | |
50 | wxTheApp->OnEventLoopEnter(loop); | |
51 | } | |
52 | ||
53 | void wxEventLoopBase::OnExit() | |
54 | { | |
55 | if (wxTheApp) | |
56 | wxTheApp->OnEventLoopExit(this); | |
57 | } | |
58 | ||
dde19c21 FM |
59 | void wxEventLoopBase::WakeUpIdle() |
60 | { | |
61 | WakeUp(); | |
62 | } | |
63 | ||
64 | bool wxEventLoopBase::ProcessIdle() | |
65 | { | |
a758f601 | 66 | return wxTheApp && wxTheApp->ProcessIdle(); |
dde19c21 FM |
67 | } |
68 | ||
69 | bool 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 |
2e38bcd2 | 85 | #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE) |
52c9b349 | 86 | |
c8026dea VZ |
87 | // ============================================================================ |
88 | // wxEventLoopManual implementation | |
89 | // ============================================================================ | |
90 | ||
91 | wxEventLoopManual::wxEventLoopManual() | |
92 | { | |
93 | m_exitcode = 0; | |
94 | m_shouldExit = false; | |
95 | } | |
96 | ||
97 | int wxEventLoopManual::Run() | |
98 | { | |
99 | // event loops are not recursive, you need to create another loop! | |
100 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
101 | ||
102 | // ProcessIdle() and Dispatch() below may throw so the code here should | |
103 | // be exception-safe, hence we must use local objects for all actions we | |
104 | // should undo | |
b46b1d59 | 105 | wxEventLoopActivator activate(this); |
c8026dea VZ |
106 | |
107 | // we must ensure that OnExit() is called even if an exception is thrown | |
108 | // from inside Dispatch() but we must call it from Exit() in normal | |
109 | // situations because it is supposed to be called synchronously, | |
110 | // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or | |
111 | // something similar here) | |
112 | #if wxUSE_EXCEPTIONS | |
113 | for ( ;; ) | |
114 | { | |
115 | try | |
116 | { | |
117 | #endif // wxUSE_EXCEPTIONS | |
118 | ||
119 | // this is the event loop itself | |
120 | for ( ;; ) | |
121 | { | |
122 | // give them the possibility to do whatever they want | |
123 | OnNextIteration(); | |
124 | ||
125 | // generate and process idle events for as long as we don't | |
126 | // have anything else to do | |
a758f601 | 127 | while ( !Pending() && ProcessIdle() ) |
c8026dea VZ |
128 | ; |
129 | ||
130 | // if the "should exit" flag is set, the loop should terminate | |
131 | // but not before processing any remaining messages so while | |
132 | // Pending() returns true, do process them | |
133 | if ( m_shouldExit ) | |
134 | { | |
135 | while ( Pending() ) | |
136 | Dispatch(); | |
137 | ||
138 | break; | |
139 | } | |
140 | ||
141 | // a message came or no more idle processing to do, sit in | |
142 | // Dispatch() waiting for the next message | |
143 | if ( !Dispatch() ) | |
144 | { | |
145 | // we got WM_QUIT | |
146 | break; | |
147 | } | |
148 | } | |
149 | ||
150 | #if wxUSE_EXCEPTIONS | |
151 | // exit the outer loop as well | |
152 | break; | |
153 | } | |
154 | catch ( ... ) | |
155 | { | |
156 | try | |
157 | { | |
158 | if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() ) | |
159 | { | |
160 | OnExit(); | |
161 | break; | |
162 | } | |
163 | //else: continue running the event loop | |
164 | } | |
165 | catch ( ... ) | |
166 | { | |
167 | // OnException() throwed, possibly rethrowing the same | |
168 | // exception again: very good, but we still need OnExit() to | |
169 | // be called | |
170 | OnExit(); | |
171 | throw; | |
172 | } | |
173 | } | |
174 | } | |
175 | #endif // wxUSE_EXCEPTIONS | |
176 | ||
177 | return m_exitcode; | |
178 | } | |
179 | ||
180 | void wxEventLoopManual::Exit(int rc) | |
181 | { | |
182 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
183 | ||
184 | m_exitcode = rc; | |
185 | m_shouldExit = true; | |
186 | ||
187 | OnExit(); | |
188 | ||
189 | // all we have to do to exit from the loop is to (maybe) wake it up so that | |
190 | // it can notice that Exit() had been called | |
191 | // | |
192 | // in particular, do *not* use here calls such as PostQuitMessage() (under | |
193 | // MSW) which terminate the current event loop here because we're not sure | |
194 | // that it is going to be processed by the correct event loop: it would be | |
195 | // possible that another one is started and terminated by mistake if we do | |
196 | // this | |
197 | WakeUp(); | |
198 | } | |
199 | ||
b3c86150 | 200 | #endif // __WXMSW__ || __WXMAC__ || __WXDFB__ |
9af42efd | 201 |