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