]>
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 | ||
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" | |
670f9935 WS |
28 | |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
31 | #endif //WX_PRECOMP | |
c8026dea VZ |
32 | |
33 | // ---------------------------------------------------------------------------- | |
34 | // globals | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
37 | wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL; | |
38 | ||
52c9b349 | 39 | // wxEventLoopManual is unused in the other ports |
b46b1d59 | 40 | #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && !wxUSE_GUI) |
52c9b349 | 41 | |
c8026dea VZ |
42 | // ============================================================================ |
43 | // wxEventLoopManual implementation | |
44 | // ============================================================================ | |
45 | ||
46 | wxEventLoopManual::wxEventLoopManual() | |
47 | { | |
48 | m_exitcode = 0; | |
49 | m_shouldExit = false; | |
50 | } | |
51 | ||
52 | int wxEventLoopManual::Run() | |
53 | { | |
54 | // event loops are not recursive, you need to create another loop! | |
55 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
56 | ||
57 | // ProcessIdle() and Dispatch() below may throw so the code here should | |
58 | // be exception-safe, hence we must use local objects for all actions we | |
59 | // should undo | |
b46b1d59 | 60 | wxEventLoopActivator activate(this); |
c8026dea VZ |
61 | |
62 | // we must ensure that OnExit() is called even if an exception is thrown | |
63 | // from inside Dispatch() but we must call it from Exit() in normal | |
64 | // situations because it is supposed to be called synchronously, | |
65 | // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or | |
66 | // something similar here) | |
67 | #if wxUSE_EXCEPTIONS | |
68 | for ( ;; ) | |
69 | { | |
70 | try | |
71 | { | |
72 | #endif // wxUSE_EXCEPTIONS | |
73 | ||
74 | // this is the event loop itself | |
75 | for ( ;; ) | |
76 | { | |
77 | // give them the possibility to do whatever they want | |
78 | OnNextIteration(); | |
79 | ||
80 | // generate and process idle events for as long as we don't | |
81 | // have anything else to do | |
82 | while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) ) | |
83 | ; | |
84 | ||
85 | // if the "should exit" flag is set, the loop should terminate | |
86 | // but not before processing any remaining messages so while | |
87 | // Pending() returns true, do process them | |
88 | if ( m_shouldExit ) | |
89 | { | |
90 | while ( Pending() ) | |
91 | Dispatch(); | |
92 | ||
93 | break; | |
94 | } | |
95 | ||
96 | // a message came or no more idle processing to do, sit in | |
97 | // Dispatch() waiting for the next message | |
98 | if ( !Dispatch() ) | |
99 | { | |
100 | // we got WM_QUIT | |
101 | break; | |
102 | } | |
103 | } | |
104 | ||
105 | #if wxUSE_EXCEPTIONS | |
106 | // exit the outer loop as well | |
107 | break; | |
108 | } | |
109 | catch ( ... ) | |
110 | { | |
111 | try | |
112 | { | |
113 | if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() ) | |
114 | { | |
115 | OnExit(); | |
116 | break; | |
117 | } | |
118 | //else: continue running the event loop | |
119 | } | |
120 | catch ( ... ) | |
121 | { | |
122 | // OnException() throwed, possibly rethrowing the same | |
123 | // exception again: very good, but we still need OnExit() to | |
124 | // be called | |
125 | OnExit(); | |
126 | throw; | |
127 | } | |
128 | } | |
129 | } | |
130 | #endif // wxUSE_EXCEPTIONS | |
131 | ||
132 | return m_exitcode; | |
133 | } | |
134 | ||
135 | void wxEventLoopManual::Exit(int rc) | |
136 | { | |
137 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
138 | ||
139 | m_exitcode = rc; | |
140 | m_shouldExit = true; | |
141 | ||
142 | OnExit(); | |
143 | ||
144 | // all we have to do to exit from the loop is to (maybe) wake it up so that | |
145 | // it can notice that Exit() had been called | |
146 | // | |
147 | // in particular, do *not* use here calls such as PostQuitMessage() (under | |
148 | // MSW) which terminate the current event loop here because we're not sure | |
149 | // that it is going to be processed by the correct event loop: it would be | |
150 | // possible that another one is started and terminated by mistake if we do | |
151 | // this | |
152 | WakeUp(); | |
153 | } | |
154 | ||
b3c86150 | 155 | #endif // __WXMSW__ || __WXMAC__ || __WXDFB__ |