]> git.saurik.com Git - wxWidgets.git/blame - src/x11/evtloop.cpp
added missing headers to fix compilation without PCH
[wxWidgets.git] / src / x11 / evtloop.cpp
CommitLineData
1b0fb34b
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: x11/evtloop.cpp
3// Purpose: implements wxEventLoop for X11
4// Author: Julian Smart
5// Modified by:
6// Created: 01.06.01
7// RCS-ID: $Id$
8// Copyright: (c) 2002 Julian Smart
9// License: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "evtloop.h"
22#endif
23
24#include "wx/window.h"
25#include "wx/app.h"
26#include "wx/evtloop.h"
27#include "wx/tooltip.h"
7266b672
JS
28#if wxUSE_THREADS
29#include "wx/thread.h"
30#endif
b513212d 31#include "wx/timer.h"
1b0fb34b
JS
32#include "wx/x11/private.h"
33#include "X11/Xlib.h"
34
35// ----------------------------------------------------------------------------
36// wxEventLoopImpl
37// ----------------------------------------------------------------------------
38
39class WXDLLEXPORT wxEventLoopImpl
40{
41public:
42 // ctor
43 wxEventLoopImpl() { SetExitCode(0); m_keepGoing = FALSE; }
44
086fd560
JS
45 // process an XEvent, return TRUE if it was processed
46 bool ProcessEvent(XEvent* event);
1b0fb34b
JS
47
48 // generate an idle message, return TRUE if more idle time requested
49 bool SendIdleEvent();
50
51 // set/get the exit code
52 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
53 int GetExitCode() const { return m_exitcode; }
54
55public:
56 // preprocess an event, return TRUE if processed (i.e. no further
57 // dispatching required)
7266b672 58 bool PreProcessEvent(XEvent* event);
1b0fb34b
JS
59
60 // the exit code of the event loop
61 int m_exitcode;
62
63 bool m_keepGoing;
64};
65
66// ============================================================================
67// wxEventLoopImpl implementation
68// ============================================================================
69
70// ----------------------------------------------------------------------------
71// wxEventLoopImpl message processing
72// ----------------------------------------------------------------------------
73
086fd560 74bool wxEventLoopImpl::ProcessEvent(XEvent *event)
1b0fb34b
JS
75{
76 // give us the chance to preprocess the message first
086fd560
JS
77 if ( PreProcessEvent(event) )
78 return TRUE;
79
80 // if it wasn't done, dispatch it to the corresponding window
81 if (wxTheApp)
82 return wxTheApp->ProcessXEvent((WXEvent*) event);
83
84 return FALSE;
1b0fb34b
JS
85}
86
87bool wxEventLoopImpl::PreProcessEvent(XEvent *event)
88{
89 // TODO
90#if 0
91 HWND hWnd = msg->hwnd;
92 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd);
93
94
95 // try translations first; find the youngest window with a translation
96 // table.
97 wxWindow *wnd;
98 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
99 {
100 if ( wnd->MSWTranslateMessage((WXMSG *)msg) )
101 return TRUE;
102 }
103
104 // Anyone for a non-translation message? Try youngest descendants first.
105 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
106 {
107 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
108 return TRUE;
109 }
110#endif
111
112 return FALSE;
113}
114
115// ----------------------------------------------------------------------------
116// wxEventLoopImpl idle event processing
117// ----------------------------------------------------------------------------
118
119bool wxEventLoopImpl::SendIdleEvent()
120{
121 wxIdleEvent event;
122 event.SetEventObject(wxTheApp);
123
124 return wxTheApp->ProcessEvent(event) && event.MoreRequested();
125}
126
127// ============================================================================
128// wxEventLoop implementation
129// ============================================================================
130
131wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
132
133// ----------------------------------------------------------------------------
134// wxEventLoop running and exiting
135// ----------------------------------------------------------------------------
136
137wxEventLoop::~wxEventLoop()
138{
139 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
140}
141
142bool wxEventLoop::IsRunning() const
143{
144 return m_impl != NULL;
145}
146
147int wxEventLoop::Run()
148{
149 // event loops are not recursive, you need to create another loop!
150 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
151
152 m_impl = new wxEventLoopImpl;
153
154 wxEventLoop *oldLoop = ms_activeLoop;
155 ms_activeLoop = this;
156
157 m_impl->m_keepGoing = TRUE;
158 while ( m_impl->m_keepGoing )
159 {
7266b672
JS
160#if 0 // wxUSE_THREADS
161 wxMutexGuiLeaveOrEnter();
1b0fb34b
JS
162#endif // wxUSE_THREADS
163
164 // generate and process idle events for as long as we don't have
165 // anything else to do
166 while ( ! Pending() )
167 {
b555c37c 168#if wxUSE_TIMER
b513212d 169 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
b555c37c 170#endif
1b0fb34b
JS
171 if (!m_impl->SendIdleEvent())
172 {
7266b672 173#if 0 // wxUSE_THREADS
1b0fb34b
JS
174 // leave the main loop to give other threads a chance to
175 // perform their GUI work
176 wxMutexGuiLeave();
177 wxUsleep(20);
178 wxMutexGuiEnter();
179#endif
180 // Break out of while loop
181 break;
182 }
183 }
184
185 // a message came or no more idle processing to do, sit in Dispatch()
186 // waiting for the next message
187 if ( !Dispatch() )
188 {
189 break;
190 }
191 }
192
193 int exitcode = m_impl->GetExitCode();
194 delete m_impl;
195 m_impl = NULL;
196
197 ms_activeLoop = oldLoop;
198
199 return exitcode;
200}
201
202void wxEventLoop::Exit(int rc)
203{
204 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
205
206 m_impl->SetExitCode(rc);
207 m_impl->m_keepGoing = FALSE;
208}
209
210// ----------------------------------------------------------------------------
211// wxEventLoop message processing dispatching
212// ----------------------------------------------------------------------------
213
214bool wxEventLoop::Pending() const
215{
7266b672
JS
216 XFlush((Display*) wxGetDisplay());
217 return (XPending((Display*) wxGetDisplay()) > 0);
1b0fb34b
JS
218}
219
220bool wxEventLoop::Dispatch()
221{
222 XEvent event;
223
224 // TODO allowing for threads, as per e.g. wxMSW
225
7266b672 226 XNextEvent((Display*) wxGetDisplay(), & event);
086fd560 227 (void) m_impl->ProcessEvent(& event);
1b0fb34b
JS
228 return TRUE;
229}
230