]>
Commit | Line | Data |
---|---|---|
83df96d6 | 1 | ///////////////////////////////////////////////////////////////////////////// |
4bcc3647 | 2 | // Name: src/x11/app.cpp |
83df96d6 JS |
3 | // Purpose: wxApp |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
83df96d6 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
4bcc3647 WS |
12 | // for compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
88a7a4e1 WS |
15 | #include "wx/app.h" |
16 | ||
32d4c30a WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/hash.h" | |
88a7a4e1 | 19 | #include "wx/intl.h" |
e4db172a | 20 | #include "wx/log.h" |
de6185e2 | 21 | #include "wx/utils.h" |
76b49cf4 | 22 | #include "wx/frame.h" |
923d28da | 23 | #include "wx/icon.h" |
fdf565fe | 24 | #include "wx/dialog.h" |
c0badb70 | 25 | #include "wx/timer.h" |
5b56bffb | 26 | #include "wx/memory.h" |
dd05139a | 27 | #include "wx/gdicmn.h" |
02761f6c | 28 | #include "wx/module.h" |
32d4c30a WS |
29 | #endif |
30 | ||
1b0fb34b | 31 | #include "wx/evtloop.h" |
2b5f62a0 | 32 | #include "wx/filename.h" |
83df96d6 | 33 | |
ef8c973b VS |
34 | #include "wx/univ/theme.h" |
35 | #include "wx/univ/renderer.h" | |
36 | ||
83df96d6 JS |
37 | #if wxUSE_THREADS |
38 | #include "wx/thread.h" | |
39 | #endif | |
40 | ||
7eaac9f5 | 41 | #include "wx/x11/private.h" |
83df96d6 JS |
42 | |
43 | #include <string.h> | |
44 | ||
9d2cef1c RR |
45 | //------------------------------------------------------------------------ |
46 | // global data | |
47 | //------------------------------------------------------------------------ | |
48 | ||
b8033a5d VZ |
49 | wxWindowHash *wxWidgetHashTable = NULL; |
50 | wxWindowHash *wxClientWidgetHashTable = NULL; | |
9d2cef1c | 51 | |
4bcc3647 | 52 | static bool g_showIconic = false; |
9d2cef1c | 53 | static wxSize g_initialSize = wxDefaultSize; |
83df96d6 | 54 | |
9d2cef1c RR |
55 | // This is required for wxFocusEvent::SetWindow(). It will only |
56 | // work for focus events which we provoke ourselves (by calling | |
57 | // SetFocus()). It will not work for those events, which X11 | |
58 | // generates itself. | |
59 | static wxWindow *g_nextFocus = NULL; | |
60 | static wxWindow *g_prevFocus = NULL; | |
83df96d6 | 61 | |
9d2cef1c RR |
62 | //------------------------------------------------------------------------ |
63 | // X11 error handling | |
64 | //------------------------------------------------------------------------ | |
83df96d6 JS |
65 | |
66 | #ifdef __WXDEBUG__ | |
1b0fb34b | 67 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); |
83df96d6 | 68 | |
1b0fb34b | 69 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; |
83df96d6 | 70 | |
1b0fb34b JS |
71 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) |
72 | { | |
73 | // just forward to the default handler for now | |
c79a329d JS |
74 | if (gs_pfnXErrorHandler) |
75 | return gs_pfnXErrorHandler(dpy, xevent); | |
76 | else | |
77 | return 0; | |
1b0fb34b | 78 | } |
83df96d6 JS |
79 | #endif // __WXDEBUG__ |
80 | ||
9d2cef1c RR |
81 | //------------------------------------------------------------------------ |
82 | // wxApp | |
83 | //------------------------------------------------------------------------ | |
84 | ||
83df96d6 | 85 | long wxApp::sm_lastMessageTime = 0; |
a11672a4 | 86 | WXDisplay *wxApp::ms_display = NULL; |
83df96d6 | 87 | |
9d2cef1c RR |
88 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
89 | ||
90 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
955a9197 | 91 | EVT_IDLE(wxAppBase::OnIdle) |
9d2cef1c | 92 | END_EVENT_TABLE() |
256d631a | 93 | |
3ee937aa | 94 | bool wxApp::Initialize(int& argC, wxChar **argV) |
83df96d6 | 95 | { |
05e2b077 | 96 | #if defined(__WXDEBUG__) && !wxUSE_NANOX |
a11672a4 RR |
97 | // install the X error handler |
98 | gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler ); | |
99 | #endif // __WXDEBUG__ | |
100 | ||
c6441a0d | 101 | wxString displayName; |
4bcc3647 | 102 | bool syncDisplay = false; |
256d631a | 103 | |
3ee937aa JS |
104 | int argCOrig = argC; |
105 | for ( int i = 0; i < argCOrig; i++ ) | |
256d631a | 106 | { |
3ee937aa | 107 | if (wxStrcmp( argV[i], _T("-display") ) == 0) |
256d631a | 108 | { |
3ee937aa | 109 | if (i < (argC - 1)) |
256d631a | 110 | { |
3ee937aa | 111 | argV[i++] = NULL; |
05e2b077 | 112 | |
3ee937aa | 113 | displayName = argV[i]; |
05e2b077 | 114 | |
3ee937aa JS |
115 | argV[i] = NULL; |
116 | argC -= 2; | |
256d631a JS |
117 | } |
118 | } | |
3ee937aa | 119 | else if (wxStrcmp( argV[i], _T("-geometry") ) == 0) |
256d631a | 120 | { |
3ee937aa | 121 | if (i < (argC - 1)) |
256d631a | 122 | { |
3ee937aa | 123 | argV[i++] = NULL; |
05e2b077 | 124 | |
256d631a | 125 | int w, h; |
3ee937aa | 126 | if (wxSscanf(argV[i], _T("%dx%d"), &w, &h) != 2) |
256d631a | 127 | { |
05e2b077 | 128 | wxLogError( _("Invalid geometry specification '%s'"), |
3ee937aa | 129 | wxString(argV[i]).c_str() ); |
256d631a JS |
130 | } |
131 | else | |
132 | { | |
133 | g_initialSize = wxSize(w, h); | |
134 | } | |
05e2b077 | 135 | |
3ee937aa JS |
136 | argV[i] = NULL; |
137 | argC -= 2; | |
256d631a JS |
138 | } |
139 | } | |
3ee937aa | 140 | else if (wxStrcmp( argV[i], _T("-sync") ) == 0) |
256d631a | 141 | { |
4bcc3647 | 142 | syncDisplay = true; |
05e2b077 | 143 | |
3ee937aa JS |
144 | argV[i] = NULL; |
145 | argC--; | |
256d631a | 146 | } |
3ee937aa | 147 | else if (wxStrcmp( argV[i], _T("-iconic") ) == 0) |
256d631a | 148 | { |
4bcc3647 | 149 | g_showIconic = true; |
45ff6421 | 150 | |
3ee937aa JS |
151 | argV[i] = NULL; |
152 | argC--; | |
256d631a | 153 | } |
05e2b077 | 154 | } |
256d631a | 155 | |
3ee937aa | 156 | if ( argC != argCOrig ) |
05e2b077 VZ |
157 | { |
158 | // remove the argumens we consumed | |
3ee937aa | 159 | for ( int i = 0; i < argC; i++ ) |
05e2b077 | 160 | { |
3ee937aa | 161 | while ( !argV[i] ) |
05e2b077 | 162 | { |
3ee937aa | 163 | memmove(argV + i, argV + i + 1, argCOrig - i); |
05e2b077 VZ |
164 | } |
165 | } | |
256d631a | 166 | } |
a11672a4 | 167 | |
7c9955d1 | 168 | // X11 display stuff |
c6441a0d VS |
169 | Display *xdisplay; |
170 | if ( displayName.empty() ) | |
171 | xdisplay = XOpenDisplay( NULL ); | |
172 | else | |
173 | xdisplay = XOpenDisplay( displayName.ToAscii() ); | |
a11672a4 RR |
174 | if (!xdisplay) |
175 | { | |
77ffb593 | 176 | wxLogError( _("wxWidgets could not open display. Exiting.") ); |
05e2b077 VZ |
177 | return false; |
178 | } | |
179 | ||
256d631a | 180 | if (syncDisplay) |
256d631a | 181 | XSynchronize(xdisplay, True); |
e2386592 | 182 | |
05e2b077 | 183 | ms_display = (WXDisplay*) xdisplay; |
7c9955d1 | 184 | |
a11672a4 | 185 | XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask); |
e2386592 | 186 | |
2b5f62a0 | 187 | // Misc. |
4bcc3647 | 188 | wxSetDetectableAutoRepeat( true ); |
a11672a4 | 189 | |
3ee937aa | 190 | if ( !wxAppBase::Initialize(argC, argV) ) |
5b004007 VZ |
191 | { |
192 | XCloseDisplay(xdisplay); | |
193 | ||
194 | return false; | |
195 | } | |
196 | ||
2b5f62a0 VZ |
197 | #if wxUSE_UNICODE |
198 | // Glib's type system required by Pango | |
199 | g_type_init(); | |
7c9955d1 | 200 | #endif |
2b5f62a0 | 201 | |
05e2b077 VZ |
202 | #if wxUSE_INTL |
203 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
204 | #endif | |
a11672a4 | 205 | |
b8033a5d VZ |
206 | wxWidgetHashTable = new wxWindowHash; |
207 | wxClientWidgetHashTable = new wxWindowHash; | |
a11672a4 | 208 | |
05e2b077 | 209 | return true; |
a11672a4 RR |
210 | } |
211 | ||
05e2b077 | 212 | void wxApp::CleanUp() |
83df96d6 | 213 | { |
05e2b077 VZ |
214 | delete wxWidgetHashTable; |
215 | wxWidgetHashTable = NULL; | |
216 | delete wxClientWidgetHashTable; | |
217 | wxClientWidgetHashTable = NULL; | |
83df96d6 | 218 | |
05e2b077 VZ |
219 | wxAppBase::CleanUp(); |
220 | } | |
83df96d6 | 221 | |
83df96d6 JS |
222 | wxApp::wxApp() |
223 | { | |
00344dd0 | 224 | // TODO: parse the command line |
83df96d6 JS |
225 | argc = 0; |
226 | argv = NULL; | |
00344dd0 | 227 | |
83df96d6 | 228 | m_mainColormap = (WXColormap) NULL; |
7eaac9f5 | 229 | m_topLevelWidget = (WXWindow) NULL; |
83df96d6 | 230 | m_maxRequestSize = 0; |
4bcc3647 | 231 | m_showIconic = false; |
256d631a | 232 | m_initialSize = wxDefaultSize; |
366e8ae6 | 233 | |
8601b2e1 | 234 | #if !wxUSE_NANOX |
9ce8d6a2 | 235 | m_visualInfo = NULL; |
8601b2e1 | 236 | #endif |
dc4025af RR |
237 | } |
238 | ||
239 | wxApp::~wxApp() | |
240 | { | |
8601b2e1 | 241 | #if !wxUSE_NANOX |
9ce8d6a2 | 242 | delete m_visualInfo; |
8601b2e1 | 243 | #endif |
83df96d6 JS |
244 | } |
245 | ||
70b8ab77 | 246 | #if !wxUSE_NANOX |
aedeab23 | 247 | |
f809133f RR |
248 | //----------------------------------------------------------------------- |
249 | // X11 predicate function for exposure compression | |
250 | //----------------------------------------------------------------------- | |
251 | ||
252 | struct wxExposeInfo | |
253 | { | |
254 | Window window; | |
255 | Bool found_non_matching; | |
256 | }; | |
257 | ||
aedeab23 VZ |
258 | extern "C" |
259 | Bool wxX11ExposePredicate (Display *display, XEvent *xevent, XPointer arg) | |
f809133f RR |
260 | { |
261 | wxExposeInfo *info = (wxExposeInfo*) arg; | |
e2386592 | 262 | |
f809133f RR |
263 | if (info->found_non_matching) |
264 | return FALSE; | |
e2386592 | 265 | |
f809133f RR |
266 | if (xevent->xany.type != Expose) |
267 | { | |
4bcc3647 | 268 | info->found_non_matching = true; |
f809133f RR |
269 | return FALSE; |
270 | } | |
e2386592 | 271 | |
f809133f RR |
272 | if (xevent->xexpose.window != info->window) |
273 | { | |
4bcc3647 | 274 | info->found_non_matching = true; |
f809133f RR |
275 | return FALSE; |
276 | } | |
e2386592 | 277 | |
f809133f RR |
278 | return TRUE; |
279 | } | |
aedeab23 VZ |
280 | |
281 | #endif // wxUSE_NANOX | |
f809133f RR |
282 | |
283 | //----------------------------------------------------------------------- | |
4bcc3647 | 284 | // Processes an X event, returning true if the event was processed. |
f809133f RR |
285 | //----------------------------------------------------------------------- |
286 | ||
086fd560 | 287 | bool wxApp::ProcessXEvent(WXEvent* _event) |
1b0fb34b JS |
288 | { |
289 | XEvent* event = (XEvent*) _event; | |
83df96d6 | 290 | |
1b0fb34b | 291 | wxWindow* win = NULL; |
c79a329d | 292 | Window window = XEventGetWindow(event); |
e2386592 | 293 | #if 0 |
1b0fb34b | 294 | Window actualWindow = window; |
e2386592 | 295 | #endif |
0b5c0e1a | 296 | |
1b0fb34b | 297 | // Find the first wxWindow that corresponds to this event window |
774b90fb JS |
298 | // Because we're receiving events after a window |
299 | // has been destroyed, assume a 1:1 match between | |
300 | // Window and wxWindow, so if it's not in the table, | |
301 | // it must have been destroyed. | |
e2386592 | 302 | |
774b90fb JS |
303 | win = wxGetWindowFromTable(window); |
304 | if (!win) | |
ab6b6b15 RR |
305 | { |
306 | #if wxUSE_TWO_WINDOWS | |
307 | win = wxGetClientWindowFromTable(window); | |
308 | if (!win) | |
309 | #endif | |
4bcc3647 | 310 | return false; |
ab6b6b15 | 311 | } |
83df96d6 | 312 | |
0b5c0e1a JS |
313 | #ifdef __WXDEBUG__ |
314 | wxString windowClass = win->GetClassInfo()->GetClassName(); | |
315 | #endif | |
e2386592 | 316 | |
1b0fb34b JS |
317 | switch (event->type) |
318 | { | |
2f12683e RR |
319 | case Expose: |
320 | { | |
8601b2e1 | 321 | #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX |
f41bc3e3 | 322 | if (event->xexpose.window != (Window)win->GetClientAreaWindow()) |
ab6b6b15 RR |
323 | { |
324 | XEvent tmp_event; | |
325 | wxExposeInfo info; | |
326 | info.window = event->xexpose.window; | |
4bcc3647 | 327 | info.found_non_matching = false; |
aedeab23 | 328 | while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, wxX11ExposePredicate, (XPointer) &info )) |
ab6b6b15 RR |
329 | { |
330 | // Don't worry about optimizing redrawing the border etc. | |
331 | } | |
332 | win->NeedUpdateNcAreaInIdle(); | |
333 | } | |
334 | else | |
335 | #endif | |
336 | { | |
337 | win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event), | |
338 | XExposeEventGetWidth(event), XExposeEventGetHeight(event)); | |
ab6b6b15 | 339 | win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event), |
2f12683e RR |
340 | XExposeEventGetWidth(event), XExposeEventGetHeight(event)); |
341 | ||
342 | #if !wxUSE_NANOX | |
ab6b6b15 RR |
343 | XEvent tmp_event; |
344 | wxExposeInfo info; | |
345 | info.window = event->xexpose.window; | |
4bcc3647 | 346 | info.found_non_matching = false; |
aedeab23 | 347 | while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, wxX11ExposePredicate, (XPointer) &info )) |
ab6b6b15 RR |
348 | { |
349 | win->GetUpdateRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y, | |
350 | tmp_event.xexpose.width, tmp_event.xexpose.height ); | |
e2386592 | 351 | |
ab6b6b15 RR |
352 | win->GetClearRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y, |
353 | tmp_event.xexpose.width, tmp_event.xexpose.height ); | |
354 | } | |
2f12683e | 355 | #endif |
c2c0dabf RR |
356 | |
357 | // This simplifies the expose and clear areas to simple | |
358 | // rectangles. | |
359 | win->GetUpdateRegion() = win->GetUpdateRegion().GetBox(); | |
360 | win->GetClearRegion() = win->GetClearRegion().GetBox(); | |
e2386592 | 361 | |
c2c0dabf | 362 | // If we only have one X11 window, always indicate |
e2386592 | 363 | // that borders might have to be redrawn. |
f41bc3e3 | 364 | if (win->GetMainWindow() == win->GetClientAreaWindow()) |
ab6b6b15 | 365 | win->NeedUpdateNcAreaInIdle(); |
2f12683e | 366 | |
ab6b6b15 RR |
367 | // Only erase background, paint in idle time. |
368 | win->SendEraseEvents(); | |
09a1dffa JS |
369 | |
370 | // EXPERIMENT | |
371 | //win->Update(); | |
ab6b6b15 | 372 | } |
2f12683e | 373 | |
4bcc3647 | 374 | return true; |
2f12683e | 375 | } |
e2386592 | 376 | |
2f12683e RR |
377 | #if !wxUSE_NANOX |
378 | case GraphicsExpose: | |
379 | { | |
89cd4125 | 380 | wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win->GetName().c_str()); |
e2386592 | 381 | |
2f12683e RR |
382 | win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y, |
383 | event->xgraphicsexpose.width, event->xgraphicsexpose.height); | |
e2386592 | 384 | |
2f12683e RR |
385 | win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y, |
386 | event->xgraphicsexpose.width, event->xgraphicsexpose.height); | |
e2386592 | 387 | |
2f12683e RR |
388 | if (event->xgraphicsexpose.count == 0) |
389 | { | |
390 | // Only erase background, paint in idle time. | |
391 | win->SendEraseEvents(); | |
c2c0dabf | 392 | // win->Update(); |
2f12683e RR |
393 | } |
394 | ||
4bcc3647 | 395 | return true; |
2f12683e RR |
396 | } |
397 | #endif | |
398 | ||
1b0fb34b | 399 | case KeyPress: |
83df96d6 | 400 | { |
9d2cef1c | 401 | if (!win->IsEnabled()) |
4bcc3647 | 402 | return false; |
b513212d | 403 | |
9d2cef1c RR |
404 | wxKeyEvent keyEvent(wxEVT_KEY_DOWN); |
405 | wxTranslateKeyEvent(keyEvent, win, window, event); | |
e2386592 | 406 | |
9d2cef1c | 407 | // wxLogDebug( "OnKey from %s", win->GetName().c_str() ); |
e2386592 | 408 | |
120b822d RR |
409 | // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR |
410 | if (win->GetEventHandler()->ProcessEvent( keyEvent )) | |
4bcc3647 | 411 | return true; |
e2386592 | 412 | |
120b822d | 413 | keyEvent.SetEventType(wxEVT_CHAR); |
2b5f62a0 VZ |
414 | // Do the translation again, retaining the ASCII |
415 | // code. | |
4bcc3647 | 416 | wxTranslateKeyEvent(keyEvent, win, window, event, true); |
120b822d | 417 | if (win->GetEventHandler()->ProcessEvent( keyEvent )) |
4bcc3647 | 418 | return true; |
e2386592 | 419 | |
120b822d RR |
420 | if ( (keyEvent.m_keyCode == WXK_TAB) && |
421 | win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) ) | |
9d2cef1c | 422 | { |
120b822d RR |
423 | wxNavigationKeyEvent new_event; |
424 | new_event.SetEventObject( win->GetParent() ); | |
425 | /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */ | |
426 | new_event.SetDirection( (keyEvent.m_keyCode == WXK_TAB) ); | |
427 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
428 | new_event.SetWindowChange( keyEvent.ControlDown() ); | |
429 | new_event.SetCurrentFocus( win ); | |
430 | return win->GetParent()->GetEventHandler()->ProcessEvent( new_event ); | |
1b0fb34b | 431 | } |
120b822d | 432 | |
4bcc3647 | 433 | return false; |
83df96d6 | 434 | } |
1b0fb34b | 435 | case KeyRelease: |
7eaac9f5 | 436 | { |
9d2cef1c | 437 | if (!win->IsEnabled()) |
4bcc3647 | 438 | return false; |
b513212d | 439 | |
9d2cef1c RR |
440 | wxKeyEvent keyEvent(wxEVT_KEY_UP); |
441 | wxTranslateKeyEvent(keyEvent, win, window, event); | |
e2386592 | 442 | |
086fd560 | 443 | return win->GetEventHandler()->ProcessEvent( keyEvent ); |
7eaac9f5 | 444 | } |
256d631a JS |
445 | case ConfigureNotify: |
446 | { | |
c79a329d | 447 | #if wxUSE_NANOX |
9d2cef1c | 448 | if (event->update.utype == GR_UPDATE_SIZE) |
c79a329d | 449 | #endif |
256d631a | 450 | { |
3cd2f0bd VZ |
451 | wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow); |
452 | if ( tlw ) | |
c2c0dabf | 453 | { |
c2c0dabf RR |
454 | tlw->SetConfigureGeometry( XConfigureEventGetX(event), XConfigureEventGetY(event), |
455 | XConfigureEventGetWidth(event), XConfigureEventGetHeight(event) ); | |
456 | } | |
e2386592 | 457 | |
3cd2f0bd | 458 | if ( tlw && tlw->IsShown() ) |
77df2fbc | 459 | { |
77df2fbc RR |
460 | tlw->SetNeedResizeInIdle(); |
461 | } | |
462 | else | |
463 | { | |
464 | wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() ); | |
465 | sizeEvent.SetEventObject( win ); | |
e2386592 | 466 | |
77df2fbc RR |
467 | return win->GetEventHandler()->ProcessEvent( sizeEvent ); |
468 | } | |
256d631a | 469 | } |
4bcc3647 | 470 | return false; |
256d631a JS |
471 | } |
472 | #if !wxUSE_NANOX | |
1b0fb34b | 473 | case PropertyNotify: |
7eaac9f5 | 474 | { |
0b5c0e1a | 475 | //wxLogDebug("PropertyNotify: %s", windowClass.c_str()); |
086fd560 | 476 | return HandlePropertyChange(_event); |
7eaac9f5 | 477 | } |
b513212d | 478 | case ClientMessage: |
3a0b23eb | 479 | { |
9d2cef1c | 480 | if (!win->IsEnabled()) |
4bcc3647 | 481 | return false; |
3a0b23eb | 482 | |
7e4501ee RR |
483 | Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True); |
484 | Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True); | |
b513212d JS |
485 | |
486 | if (event->xclient.message_type == wm_protocols) | |
487 | { | |
6a44bffd | 488 | if ((Atom) (event->xclient.data.l[0]) == wm_delete_window) |
b513212d | 489 | { |
4bcc3647 WS |
490 | win->Close(false); |
491 | return true; | |
b513212d JS |
492 | } |
493 | } | |
4bcc3647 | 494 | return false; |
b513212d | 495 | } |
2f12683e RR |
496 | #if 0 |
497 | case DestroyNotify: | |
498 | { | |
499 | printf( "destroy from %s\n", win->GetName().c_str() ); | |
500 | break; | |
501 | } | |
502 | case CreateNotify: | |
503 | { | |
504 | printf( "create from %s\n", win->GetName().c_str() ); | |
505 | break; | |
506 | } | |
507 | case MapRequest: | |
508 | { | |
509 | printf( "map request from %s\n", win->GetName().c_str() ); | |
510 | break; | |
511 | } | |
1b0fb34b | 512 | case ResizeRequest: |
7eaac9f5 | 513 | { |
2f12683e | 514 | printf( "resize request from %s\n", win->GetName().c_str() ); |
e2386592 | 515 | |
7266b672 | 516 | Display *disp = (Display*) wxGetDisplay(); |
1b0fb34b | 517 | XEvent report; |
e2386592 | 518 | |
1b0fb34b JS |
519 | // to avoid flicker |
520 | report = * event; | |
521 | while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report)); | |
e2386592 | 522 | |
b1633d20 RR |
523 | wxSize sz = win->GetSize(); |
524 | wxSizeEvent sizeEvent(sz, win->GetId()); | |
525 | sizeEvent.SetEventObject(win); | |
1b0fb34b | 526 | |
b1633d20 | 527 | return win->GetEventHandler()->ProcessEvent( sizeEvent ); |
7eaac9f5 | 528 | } |
256d631a | 529 | #endif |
b1633d20 | 530 | #endif |
256d631a JS |
531 | #if wxUSE_NANOX |
532 | case GR_EVENT_TYPE_CLOSE_REQ: | |
533 | { | |
534 | if (win) | |
535 | { | |
4bcc3647 WS |
536 | win->Close(false); |
537 | return true; | |
256d631a | 538 | } |
4bcc3647 | 539 | return false; |
256d631a JS |
540 | break; |
541 | } | |
c79a329d | 542 | #endif |
1b0fb34b JS |
543 | case EnterNotify: |
544 | case LeaveNotify: | |
545 | case ButtonPress: | |
546 | case ButtonRelease: | |
547 | case MotionNotify: | |
7eaac9f5 | 548 | { |
7e4501ee | 549 | if (!win->IsEnabled()) |
4bcc3647 | 550 | return false; |
366e8ae6 | 551 | |
9691c806 RR |
552 | // Here we check if the top level window is |
553 | // disabled, which is one aspect of modality. | |
554 | wxWindow *tlw = win; | |
555 | while (tlw && !tlw->IsTopLevel()) | |
556 | tlw = tlw->GetParent(); | |
557 | if (tlw && !tlw->IsEnabled()) | |
4bcc3647 | 558 | return false; |
e2386592 | 559 | |
7e4501ee | 560 | if (event->type == ButtonPress) |
1b0fb34b | 561 | { |
7e4501ee | 562 | if ((win != wxWindow::FindFocus()) && win->AcceptsFocus()) |
9d2cef1c RR |
563 | { |
564 | // This might actually be done in wxWindow::SetFocus() | |
ea1ad04b | 565 | // and not here. TODO. |
9d2cef1c RR |
566 | g_prevFocus = wxWindow::FindFocus(); |
567 | g_nextFocus = win; | |
e2386592 | 568 | |
58ec2255 JS |
569 | wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() ); |
570 | ||
571 | // Record the fact that this window is | |
572 | // getting the focus, because we'll need to | |
573 | // check if its parent is getting a bogus | |
574 | // focus and duly ignore it. | |
575 | // TODO: may need to have this code in SetFocus, too. | |
576 | extern wxWindow* g_GettingFocus; | |
577 | g_GettingFocus = win; | |
9d2cef1c RR |
578 | win->SetFocus(); |
579 | } | |
1b0fb34b | 580 | } |
e2386592 | 581 | |
ea1ad04b RR |
582 | #if !wxUSE_NANOX |
583 | if (event->type == LeaveNotify || event->type == EnterNotify) | |
584 | { | |
585 | // Throw out NotifyGrab and NotifyUngrab | |
586 | if (event->xcrossing.mode != NotifyNormal) | |
4bcc3647 | 587 | return false; |
ea1ad04b RR |
588 | } |
589 | #endif | |
7e4501ee RR |
590 | wxMouseEvent wxevent; |
591 | wxTranslateMouseEvent(wxevent, win, window, event); | |
086fd560 | 592 | return win->GetEventHandler()->ProcessEvent( wxevent ); |
7eaac9f5 | 593 | } |
1b0fb34b | 594 | case FocusIn: |
256d631a | 595 | #if !wxUSE_NANOX |
bcd3832a VZ |
596 | if ((event->xfocus.detail != NotifyPointer) && |
597 | (event->xfocus.mode == NotifyNormal)) | |
256d631a | 598 | #endif |
bcd3832a VZ |
599 | { |
600 | wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() ); | |
601 | ||
602 | extern wxWindow* g_GettingFocus; | |
603 | if (g_GettingFocus && g_GettingFocus->GetParent() == win) | |
604 | { | |
605 | // Ignore this, this can be a spurious FocusIn | |
606 | // caused by a child having its focus set. | |
607 | g_GettingFocus = NULL; | |
608 | wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() ); | |
4bcc3647 | 609 | return true; |
bcd3832a VZ |
610 | } |
611 | else | |
1b0fb34b | 612 | { |
bcd3832a VZ |
613 | wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId()); |
614 | focusEvent.SetEventObject(win); | |
615 | focusEvent.SetWindow( g_prevFocus ); | |
616 | g_prevFocus = NULL; | |
366e8ae6 | 617 | |
bcd3832a | 618 | return win->GetEventHandler()->ProcessEvent(focusEvent); |
1b0fb34b | 619 | } |
1b0fb34b | 620 | } |
4bcc3647 | 621 | return false; |
bcd3832a | 622 | |
1b0fb34b | 623 | case FocusOut: |
256d631a | 624 | #if !wxUSE_NANOX |
bcd3832a VZ |
625 | if ((event->xfocus.detail != NotifyPointer) && |
626 | (event->xfocus.mode == NotifyNormal)) | |
256d631a | 627 | #endif |
bcd3832a VZ |
628 | { |
629 | wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() ); | |
e2386592 | 630 | |
bcd3832a VZ |
631 | wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId()); |
632 | focusEvent.SetEventObject(win); | |
633 | focusEvent.SetWindow( g_nextFocus ); | |
634 | g_nextFocus = NULL; | |
635 | return win->GetEventHandler()->ProcessEvent(focusEvent); | |
1b0fb34b | 636 | } |
4bcc3647 | 637 | return false; |
bcd3832a | 638 | |
45ff6421 | 639 | #ifdef __WXDEBUG__ |
bcd3832a | 640 | default: |
45ff6421 JS |
641 | //wxString eventName = wxGetXEventName(XEvent& event); |
642 | //wxLogDebug(wxT("Event %s not handled"), eventName.c_str()); | |
5b3b52ef | 643 | break; |
bcd3832a | 644 | #endif // __WXDEBUG__ |
83df96d6 | 645 | } |
83df96d6 | 646 | |
4bcc3647 | 647 | return false; |
83df96d6 JS |
648 | } |
649 | ||
650 | // This should be redefined in a derived class for | |
651 | // handling property change events for XAtom IPC. | |
086fd560 | 652 | bool wxApp::HandlePropertyChange(WXEvent *event) |
83df96d6 JS |
653 | { |
654 | // by default do nothing special | |
7eaac9f5 | 655 | // TODO: what to do for X11 |
256d631a | 656 | // XtDispatchEvent((XEvent*) event); |
4bcc3647 | 657 | return false; |
83df96d6 JS |
658 | } |
659 | ||
e2478fde | 660 | void wxApp::WakeUpIdle() |
83df96d6 | 661 | { |
e2478fde VZ |
662 | // TODO: use wxMotif implementation? |
663 | ||
83df96d6 JS |
664 | // Wake up the idle handler processor, even if it is in another thread... |
665 | } | |
666 | ||
667 | ||
256d631a | 668 | // Create display, and other initialization |
83df96d6 JS |
669 | bool wxApp::OnInitGui() |
670 | { | |
d8cebdca | 671 | #if wxUSE_LOG |
ca7497c2 JS |
672 | // Eventually this line will be removed, but for |
673 | // now we don't want to try popping up a dialog | |
674 | // for error messages. | |
675 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
d8cebdca | 676 | #endif |
e2386592 | 677 | |
ea596687 | 678 | if (!wxAppBase::OnInitGui()) |
4bcc3647 | 679 | return false; |
e2386592 | 680 | |
a11672a4 | 681 | GetMainColormap( wxApp::GetDisplay() ); |
256d631a | 682 | |
a11672a4 | 683 | m_maxRequestSize = XMaxRequestSize( (Display*) wxApp::GetDisplay() ); |
83df96d6 | 684 | |
8601b2e1 | 685 | #if !wxUSE_NANOX |
9ce8d6a2 MB |
686 | m_visualInfo = new wxXVisualInfo; |
687 | wxFillXVisualInfo( m_visualInfo, (Display*) wxApp::GetDisplay() ); | |
8601b2e1 | 688 | #endif |
366e8ae6 | 689 | |
4bcc3647 | 690 | return true; |
83df96d6 JS |
691 | } |
692 | ||
2b5f62a0 VZ |
693 | #if wxUSE_UNICODE |
694 | ||
695 | #include <pango/pango.h> | |
696 | #include <pango/pangox.h> | |
4ae1af05 MB |
697 | #ifdef HAVE_PANGO_XFT |
698 | #include <pango/pangoxft.h> | |
699 | #endif | |
2b5f62a0 VZ |
700 | |
701 | PangoContext* wxApp::GetPangoContext() | |
702 | { | |
703 | static PangoContext *ret = NULL; | |
704 | if (ret) | |
705 | return ret; | |
7c9955d1 | 706 | |
2b5f62a0 | 707 | Display *xdisplay = (Display*) wxApp::GetDisplay(); |
7c9955d1 | 708 | |
4ae1af05 | 709 | #ifdef HAVE_PANGO_XFT |
2b5f62a0 VZ |
710 | int xscreen = DefaultScreen(xdisplay); |
711 | static int use_xft = -1; | |
712 | if (use_xft == -1) | |
713 | { | |
714 | wxString val = wxGetenv( L"GDK_USE_XFT" ); | |
715 | use_xft = (val == L"1"); | |
716 | } | |
7c9955d1 | 717 | |
2b5f62a0 VZ |
718 | if (use_xft) |
719 | ret = pango_xft_get_context( xdisplay, xscreen ); | |
720 | else | |
721 | #endif | |
722 | ret = pango_x_get_context( xdisplay ); | |
7c9955d1 | 723 | |
2b5f62a0 VZ |
724 | if (!PANGO_IS_CONTEXT(ret)) |
725 | wxLogError( wxT("No pango context.") ); | |
7c9955d1 | 726 | |
2b5f62a0 VZ |
727 | return ret; |
728 | } | |
729 | #endif | |
730 | ||
83df96d6 JS |
731 | WXColormap wxApp::GetMainColormap(WXDisplay* display) |
732 | { | |
733 | if (!display) /* Must be called first with non-NULL display */ | |
734 | return m_mainColormap; | |
735 | ||
736 | int defaultScreen = DefaultScreen((Display*) display); | |
737 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
738 | ||
739 | Colormap c = DefaultColormapOfScreen(screen); | |
740 | ||
741 | if (!m_mainColormap) | |
742 | m_mainColormap = (WXColormap) c; | |
743 | ||
744 | return (WXColormap) c; | |
745 | } | |
746 | ||
8354aa92 | 747 | Window wxGetWindowParent(Window window) |
7eaac9f5 | 748 | { |
54385bdb | 749 | wxASSERT_MSG( window, _T("invalid window") ); |
e2386592 | 750 | |
86fd8bda RR |
751 | return (Window) 0; |
752 | ||
a371f703 JJ |
753 | #ifndef __VMS |
754 | // VMS chokes on unreacheable code | |
755 | Window parent, root = 0; | |
c79a329d JS |
756 | #if wxUSE_NANOX |
757 | int noChildren = 0; | |
758 | #else | |
7eaac9f5 | 759 | unsigned int noChildren = 0; |
c79a329d | 760 | #endif |
ea596687 | 761 | Window* children = NULL; |
c79a329d | 762 | |
ee351013 | 763 | // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc) |
c79a329d JS |
764 | int res = 1; |
765 | #if !wxUSE_NANOX | |
766 | res = | |
767 | #endif | |
768 | XQueryTree((Display*) wxGetDisplay(), window, & root, & parent, | |
ee351013 | 769 | & children, & noChildren); |
ea596687 JS |
770 | if (children) |
771 | XFree(children); | |
772 | if (res) | |
7eaac9f5 JS |
773 | return parent; |
774 | else | |
775 | return (Window) 0; | |
a371f703 | 776 | #endif |
7eaac9f5 JS |
777 | } |
778 | ||
e2478fde | 779 | void wxApp::Exit() |
83df96d6 | 780 | { |
83df96d6 | 781 | wxApp::CleanUp(); |
e2478fde VZ |
782 | |
783 | wxAppConsole::Exit(); | |
83df96d6 JS |
784 | } |
785 | ||
786 | // Yield to other processes | |
787 | ||
788 | bool wxApp::Yield(bool onlyIfNeeded) | |
789 | { | |
bbcd408a JS |
790 | // Sometimes only 2 yields seem |
791 | // to do the trick, e.g. in the | |
792 | // progress dialog | |
793 | int i; | |
794 | for (i = 0; i < 2; i++) | |
83df96d6 | 795 | { |
4bcc3647 | 796 | static bool s_inYield = false; |
bbcd408a JS |
797 | |
798 | if ( s_inYield ) | |
83df96d6 | 799 | { |
bbcd408a JS |
800 | if ( !onlyIfNeeded ) |
801 | { | |
802 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
803 | } | |
83df96d6 | 804 | |
4bcc3647 | 805 | return false; |
bbcd408a | 806 | } |
83df96d6 | 807 | |
4bcc3647 | 808 | s_inYield = true; |
83df96d6 | 809 | |
bbcd408a JS |
810 | // Make sure we have an event loop object, |
811 | // or Pending/Dispatch will fail | |
812 | wxEventLoop* eventLoop = wxEventLoop::GetActive(); | |
813 | wxEventLoop* newEventLoop = NULL; | |
814 | if (!eventLoop) | |
815 | { | |
816 | newEventLoop = new wxEventLoop; | |
817 | wxEventLoop::SetActive(newEventLoop); | |
818 | } | |
df0e1b64 | 819 | |
2b5f62a0 VZ |
820 | // Call dispatch at least once so that sockets |
821 | // can be tested | |
822 | wxTheApp->Dispatch(); | |
7c9955d1 | 823 | |
bbcd408a JS |
824 | while (wxTheApp && wxTheApp->Pending()) |
825 | wxTheApp->Dispatch(); | |
83df96d6 | 826 | |
868741e9 | 827 | #if wxUSE_TIMER |
bbcd408a | 828 | wxTimer::NotifyTimers(); |
868741e9 | 829 | #endif |
bbcd408a | 830 | ProcessIdle(); |
868741e9 | 831 | |
bbcd408a JS |
832 | if (newEventLoop) |
833 | { | |
834 | wxEventLoop::SetActive(NULL); | |
835 | delete newEventLoop; | |
836 | } | |
df0e1b64 | 837 | |
4bcc3647 | 838 | s_inYield = false; |
bbcd408a | 839 | } |
83df96d6 | 840 | |
4bcc3647 | 841 | return true; |
83df96d6 JS |
842 | } |
843 | ||
d715d419 VZ |
844 | #ifdef __WXDEBUG__ |
845 | ||
5968a0dc | 846 | void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg) |
7edcafa4 JS |
847 | { |
848 | // While the GUI isn't working that well, just print out the | |
849 | // message. | |
193e19cf | 850 | #if 1 |
5968a0dc | 851 | wxAppBase::OnAssert(file, line, cond, msg); |
7edcafa4 JS |
852 | #else |
853 | wxString msg2; | |
854 | msg2.Printf("At file %s:%d: %s", file, line, msg); | |
855 | wxLogDebug(msg2); | |
856 | #endif | |
857 | } | |
858 | ||
d715d419 | 859 | #endif // __WXDEBUG__ |