]>
Commit | Line | Data |
---|---|---|
8000ae7f | 1 | /////////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/gtk1/evtloop.cpp |
8000ae7f VZ |
3 | // Purpose: implements wxEventLoop for GTK+ |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 10.07.01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // License: wxWindows licence |
8000ae7f VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
8000ae7f VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
8000ae7f | 27 | #include "wx/evtloop.h" |
670f9935 WS |
28 | |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
6918257d | 31 | #include "wx/log.h" |
670f9935 | 32 | #endif // WX_PRECOMP |
8000ae7f VZ |
33 | |
34 | #include <gtk/gtk.h> | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // wxEventLoopImpl | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | class WXDLLEXPORT wxEventLoopImpl | |
41 | { | |
42 | public: | |
43 | // ctor | |
44 | wxEventLoopImpl() { SetExitCode(0); } | |
45 | ||
46 | // set/get the exit code | |
47 | void SetExitCode(int exitcode) { m_exitcode = exitcode; } | |
48 | int GetExitCode() const { return m_exitcode; } | |
49 | ||
50 | private: | |
51 | // the exit code of the event loop | |
52 | int m_exitcode; | |
53 | }; | |
54 | ||
55 | // ============================================================================ | |
b46b1d59 | 56 | // wxGUIEventLoop implementation |
8000ae7f VZ |
57 | // ============================================================================ |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
b46b1d59 | 60 | // wxGUIEventLoop running and exiting |
8000ae7f VZ |
61 | // ---------------------------------------------------------------------------- |
62 | ||
b46b1d59 | 63 | wxGUIEventLoop::~wxGUIEventLoop() |
8000ae7f | 64 | { |
9a83f860 | 65 | wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") ); |
8000ae7f VZ |
66 | } |
67 | ||
b46b1d59 | 68 | int wxGUIEventLoop::Run() |
8000ae7f VZ |
69 | { |
70 | // event loops are not recursive, you need to create another loop! | |
9a83f860 | 71 | wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); |
8000ae7f | 72 | |
77fb1a02 | 73 | wxEventLoopActivator activate(this); |
b9f246f7 | 74 | |
8000ae7f VZ |
75 | m_impl = new wxEventLoopImpl; |
76 | ||
77 | gtk_main(); | |
78 | ||
16d17da6 VZ |
79 | OnExit(); |
80 | ||
8000ae7f VZ |
81 | int exitcode = m_impl->GetExitCode(); |
82 | delete m_impl; | |
83 | m_impl = NULL; | |
84 | ||
85 | return exitcode; | |
86 | } | |
87 | ||
b46b1d59 | 88 | void wxGUIEventLoop::Exit(int rc) |
8000ae7f | 89 | { |
9a83f860 | 90 | wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") ); |
8000ae7f VZ |
91 | |
92 | m_impl->SetExitCode(rc); | |
93 | ||
94 | gtk_main_quit(); | |
95 | } | |
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | // wxEventLoop message processing dispatching | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
b46b1d59 | 101 | bool wxGUIEventLoop::Pending() const |
8000ae7f | 102 | { |
c263eb03 | 103 | if (wxTheApp) |
9213ca5d RD |
104 | { |
105 | // We need to remove idle callbacks or gtk_events_pending will | |
106 | // never return false. | |
fe593cc5 | 107 | wxTheApp->RemoveIdleTag(); |
9213ca5d RD |
108 | } |
109 | ||
110 | return gtk_events_pending(); | |
8000ae7f VZ |
111 | } |
112 | ||
b46b1d59 | 113 | bool wxGUIEventLoop::Dispatch() |
8000ae7f | 114 | { |
9a83f860 | 115 | wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") ); |
8000ae7f VZ |
116 | |
117 | gtk_main_iteration(); | |
118 | ||
670f9935 | 119 | return true; |
8000ae7f | 120 | } |
dde19c21 FM |
121 | |
122 | //----------------------------------------------------------------------------- | |
123 | // wxYield | |
124 | //----------------------------------------------------------------------------- | |
125 | ||
126 | bool wxGUIEventLoop::YieldFor(long eventsToProcess) | |
127 | { | |
128 | #if wxUSE_THREADS | |
129 | if ( !wxThread::IsMain() ) | |
130 | { | |
131 | // can't call gtk_main_iteration() from other threads like this | |
132 | return true; | |
133 | } | |
134 | #endif // wxUSE_THREADS | |
135 | ||
136 | m_isInsideYield = true; | |
137 | m_eventsToProcessInsideYield = eventsToProcess; | |
138 | ||
139 | // We need to remove idle callbacks or the loop will | |
140 | // never finish. | |
141 | wxTheApp->RemoveIdleTag(); | |
142 | ||
143 | #if wxUSE_LOG | |
144 | // disable log flushing from here because a call to wxYield() shouldn't | |
145 | // normally result in message boxes popping up &c | |
146 | wxLog::Suspend(); | |
147 | #endif | |
148 | ||
149 | // TODO: implement event filtering using the eventsToProcess mask | |
150 | while (gtk_events_pending()) | |
151 | gtk_main_iteration(); | |
152 | ||
153 | // It's necessary to call ProcessIdle() to update the frames sizes which | |
154 | // might have been changed (it also will update other things set from | |
155 | // OnUpdateUI() which is a nice (and desired) side effect). But we | |
156 | // call ProcessIdle() only once since this is not meant for longish | |
157 | // background jobs (controlled by wxIdleEvent::RequestMore() and the | |
158 | // return value of Processidle(). | |
159 | ProcessIdle(); | |
160 | ||
161 | #if wxUSE_LOG | |
162 | // let the logs be flashed again | |
163 | wxLog::Resume(); | |
164 | #endif | |
165 | ||
166 | m_isInsideYield = false; | |
167 | ||
168 | return true; | |
169 | } |