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