]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
808e82c972207a8a06979498d365d5a4181a3778
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/evtloop.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/stopwatch.h" // for wxMilliClock_t
32 #endif //WX_PRECOMP
33
34 // ----------------------------------------------------------------------------
35 // globals
36 // ----------------------------------------------------------------------------
37
38 wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL;
39
40 // wxEventLoopManual is unused in the other ports
41 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
42
43 // ============================================================================
44 // wxEventLoopManual implementation
45 // ============================================================================
46
47 wxEventLoopManual::wxEventLoopManual()
48 {
49 m_exitcode = 0;
50 m_shouldExit = false;
51 }
52
53 int wxEventLoopManual::Run()
54 {
55 // event loops are not recursive, you need to create another loop!
56 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
57
58 // ProcessIdle() and Dispatch() below may throw so the code here should
59 // be exception-safe, hence we must use local objects for all actions we
60 // should undo
61 wxEventLoopActivator activate(this);
62
63 // we must ensure that OnExit() is called even if an exception is thrown
64 // from inside Dispatch() but we must call it from Exit() in normal
65 // situations because it is supposed to be called synchronously,
66 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
67 // something similar here)
68 #if wxUSE_EXCEPTIONS
69 for ( ;; )
70 {
71 try
72 {
73 #endif // wxUSE_EXCEPTIONS
74
75 // this is the event loop itself
76 for ( ;; )
77 {
78 // give them the possibility to do whatever they want
79 OnNextIteration();
80
81 // generate and process idle events for as long as we don't
82 // have anything else to do
83 while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) )
84 ;
85
86 // if the "should exit" flag is set, the loop should terminate
87 // but not before processing any remaining messages so while
88 // Pending() returns true, do process them
89 if ( m_shouldExit )
90 {
91 while ( Pending() )
92 Dispatch();
93
94 break;
95 }
96
97 // a message came or no more idle processing to do, sit in
98 // Dispatch() waiting for the next message
99 if ( !Dispatch() )
100 {
101 // we got WM_QUIT
102 break;
103 }
104 }
105
106 #if wxUSE_EXCEPTIONS
107 // exit the outer loop as well
108 break;
109 }
110 catch ( ... )
111 {
112 try
113 {
114 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
115 {
116 OnExit();
117 break;
118 }
119 //else: continue running the event loop
120 }
121 catch ( ... )
122 {
123 // OnException() throwed, possibly rethrowing the same
124 // exception again: very good, but we still need OnExit() to
125 // be called
126 OnExit();
127 throw;
128 }
129 }
130 }
131 #endif // wxUSE_EXCEPTIONS
132
133 return m_exitcode;
134 }
135
136 void wxEventLoopManual::Exit(int rc)
137 {
138 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
139
140 m_exitcode = rc;
141 m_shouldExit = true;
142
143 OnExit();
144
145 // all we have to do to exit from the loop is to (maybe) wake it up so that
146 // it can notice that Exit() had been called
147 //
148 // in particular, do *not* use here calls such as PostQuitMessage() (under
149 // MSW) which terminate the current event loop here because we're not sure
150 // that it is going to be processed by the correct event loop: it would be
151 // possible that another one is started and terminated by mistake if we do
152 // this
153 WakeUp();
154 }
155
156 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__
157
158 #ifdef wxNEEDS_GENERIC_DISPATCH_TIMEOUT
159
160 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
161 {
162 // TODO: this is, of course, horribly inefficient and a proper wait with
163 // timeout should be implemented for all ports natively...
164 const wxMilliClock_t timeEnd = wxGetLocalTimeMillis() + timeout;
165 for ( ;; )
166 {
167 if ( Pending() )
168 return Dispatch();
169
170 if ( wxGetLocalTimeMillis() >= timeEnd )
171 return -1;
172 }
173 }
174
175 #endif // wxNEEDS_GENERIC_DISPATCH_TIMEOUT
176