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