]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
Forgot to commit the header change too.
[wxWidgets.git] / src / common / evtloopcmn.cpp
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
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/evtloop.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #endif //WX_PRECOMP
24
25 // ----------------------------------------------------------------------------
26 // wxEventLoopBase
27 // ----------------------------------------------------------------------------
28
29 wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL;
30
31 wxEventLoopBase::wxEventLoopBase()
32 {
33 m_isInsideYield = false;
34 m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL;
35 }
36
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
59 void wxEventLoopBase::WakeUpIdle()
60 {
61 WakeUp();
62 }
63
64 bool wxEventLoopBase::ProcessIdle()
65 {
66 if (!wxTheApp)
67 return false;
68
69 // process pending wx events before sending idle events
70 wxTheApp->ProcessPendingEvents();
71
72 // synthetize an idle event and send it to wxApp
73 wxIdleEvent event;
74 event.SetEventObject(wxTheApp);
75 wxTheApp->ProcessEvent(event);
76
77 return event.MoreRequested();
78 }
79
80 bool wxEventLoopBase::Yield(bool onlyIfNeeded)
81 {
82 if ( m_isInsideYield )
83 {
84 if ( !onlyIfNeeded )
85 {
86 wxFAIL_MSG( wxT("wxYield called recursively" ) );
87 }
88
89 return false;
90 }
91
92 return YieldFor(wxEVT_CATEGORY_ALL);
93 }
94
95 // wxEventLoopManual is unused in the other ports
96 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
97
98 // ============================================================================
99 // wxEventLoopManual implementation
100 // ============================================================================
101
102 wxEventLoopManual::wxEventLoopManual()
103 {
104 m_exitcode = 0;
105 m_shouldExit = false;
106 }
107
108 int wxEventLoopManual::Run()
109 {
110 // event loops are not recursive, you need to create another loop!
111 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
112
113 // ProcessIdle() and Dispatch() below may throw so the code here should
114 // be exception-safe, hence we must use local objects for all actions we
115 // should undo
116 wxEventLoopActivator activate(this);
117
118 // we must ensure that OnExit() is called even if an exception is thrown
119 // from inside Dispatch() but we must call it from Exit() in normal
120 // situations because it is supposed to be called synchronously,
121 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
122 // something similar here)
123 #if wxUSE_EXCEPTIONS
124 for ( ;; )
125 {
126 try
127 {
128 #endif // wxUSE_EXCEPTIONS
129
130 // this is the event loop itself
131 for ( ;; )
132 {
133 // give them the possibility to do whatever they want
134 OnNextIteration();
135
136 // generate and process idle events for as long as we don't
137 // have anything else to do
138 while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) )
139 ;
140
141 // if the "should exit" flag is set, the loop should terminate
142 // but not before processing any remaining messages so while
143 // Pending() returns true, do process them
144 if ( m_shouldExit )
145 {
146 while ( Pending() )
147 Dispatch();
148
149 break;
150 }
151
152 // a message came or no more idle processing to do, sit in
153 // Dispatch() waiting for the next message
154 if ( !Dispatch() )
155 {
156 // we got WM_QUIT
157 break;
158 }
159 }
160
161 #if wxUSE_EXCEPTIONS
162 // exit the outer loop as well
163 break;
164 }
165 catch ( ... )
166 {
167 try
168 {
169 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
170 {
171 OnExit();
172 break;
173 }
174 //else: continue running the event loop
175 }
176 catch ( ... )
177 {
178 // OnException() throwed, possibly rethrowing the same
179 // exception again: very good, but we still need OnExit() to
180 // be called
181 OnExit();
182 throw;
183 }
184 }
185 }
186 #endif // wxUSE_EXCEPTIONS
187
188 return m_exitcode;
189 }
190
191 void wxEventLoopManual::Exit(int rc)
192 {
193 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
194
195 m_exitcode = rc;
196 m_shouldExit = true;
197
198 OnExit();
199
200 // all we have to do to exit from the loop is to (maybe) wake it up so that
201 // it can notice that Exit() had been called
202 //
203 // in particular, do *not* use here calls such as PostQuitMessage() (under
204 // MSW) which terminate the current event loop here because we're not sure
205 // that it is going to be processed by the correct event loop: it would be
206 // possible that another one is started and terminated by mistake if we do
207 // this
208 WakeUp();
209 }
210
211 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__
212