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