]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/evtloop.cpp
Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / gtk1 / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/evtloop.cpp
3 // Purpose: implements wxEventLoop for GTK+
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 10.07.01
7 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/evtloop.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
31 #endif // WX_PRECOMP
32
33 #include <gtk/gtk.h>
34
35 // ----------------------------------------------------------------------------
36 // wxEventLoopImpl
37 // ----------------------------------------------------------------------------
38
39 class WXDLLEXPORT wxEventLoopImpl
40 {
41 public:
42 // ctor
43 wxEventLoopImpl() { SetExitCode(0); }
44
45 // set/get the exit code
46 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
47 int GetExitCode() const { return m_exitcode; }
48
49 private:
50 // the exit code of the event loop
51 int m_exitcode;
52 };
53
54 // ============================================================================
55 // wxGUIEventLoop implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxGUIEventLoop running and exiting
60 // ----------------------------------------------------------------------------
61
62 wxGUIEventLoop::~wxGUIEventLoop()
63 {
64 wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") );
65 }
66
67 int wxGUIEventLoop::DoRun()
68 {
69 m_impl = new wxEventLoopImpl;
70
71 guint loopLevel = gtk_main_level();
72
73 // This is placed inside of a loop to take into account nested
74 // event loops. For example, inside this event loop, we may recieve
75 // Exit() for a different event loop (which we are currently inside of)
76 // That Exit() will cause this gtk_main() to exit so we need to re-enter it.
77 while ( !m_shouldExit )
78 {
79 gtk_main();
80 }
81
82 // Force the enclosing event loop to also exit to see if it is done
83 // in case that event loop ended inside of this one. If it is not time
84 // yet for that event loop to exit, it will be executed again due to
85 // the while() loop on m_shouldExit().
86 //
87 // This is unnecessary if we are the top level loop, i.e. loop of level 0.
88 if ( loopLevel )
89 {
90 gtk_main_quit();
91 }
92
93 OnExit();
94
95 int exitcode = m_impl->GetExitCode();
96 wxDELETE(m_impl);
97
98 return exitcode;
99 }
100
101 void wxGUIEventLoop::ScheduleExit(int rc)
102 {
103 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not started") );
104
105 m_impl->SetExitCode(rc);
106
107 m_shouldExit = true;
108
109 gtk_main_quit();
110 }
111
112 // ----------------------------------------------------------------------------
113 // wxEventLoop message processing dispatching
114 // ----------------------------------------------------------------------------
115
116 bool wxGUIEventLoop::Pending() const
117 {
118 if (wxTheApp)
119 {
120 // We need to remove idle callbacks or gtk_events_pending will
121 // never return false.
122 wxTheApp->RemoveIdleTag();
123 }
124
125 return gtk_events_pending();
126 }
127
128 bool wxGUIEventLoop::Dispatch()
129 {
130 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
131
132 gtk_main_iteration();
133
134 return true;
135 }
136
137 //-----------------------------------------------------------------------------
138 // wxYield
139 //-----------------------------------------------------------------------------
140
141 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
142 {
143 #if wxUSE_THREADS
144 if ( !wxThread::IsMain() )
145 {
146 // can't call gtk_main_iteration() from other threads like this
147 return true;
148 }
149 #endif // wxUSE_THREADS
150
151 m_isInsideYield = true;
152 m_eventsToProcessInsideYield = eventsToProcess;
153
154 // We need to remove idle callbacks or the loop will
155 // never finish.
156 wxTheApp->RemoveIdleTag();
157
158 #if wxUSE_LOG
159 // disable log flushing from here because a call to wxYield() shouldn't
160 // normally result in message boxes popping up &c
161 wxLog::Suspend();
162 #endif
163
164 // TODO: implement event filtering using the eventsToProcess mask
165 while (gtk_events_pending())
166 gtk_main_iteration();
167
168 // It's necessary to call ProcessIdle() to update the frames sizes which
169 // might have been changed (it also will update other things set from
170 // OnUpdateUI() which is a nice (and desired) side effect). But we
171 // call ProcessIdle() only once since this is not meant for longish
172 // background jobs (controlled by wxIdleEvent::RequestMore() and the
173 // return value of Processidle().
174 ProcessIdle();
175
176 #if wxUSE_LOG
177 // let the logs be flashed again
178 wxLog::Resume();
179 #endif
180
181 m_isInsideYield = false;
182
183 return true;
184 }