]> git.saurik.com Git - wxWidgets.git/blame - src/x11/app.cpp
removed unused method declaration
[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
12#ifdef __GNUG__
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)
91 EVT_IDLE(wxApp::OnIdle)
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
2b5f62a0 101 char *displayName = NULL;
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
VZ
128 wxLogError( _("Invalid geometry specification '%s'"),
129 wxString::FromAscii(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
05e2b077 169 Display *xdisplay = XOpenDisplay( displayName );
a11672a4
RR
170 if (!xdisplay)
171 {
172 wxLogError( _("wxWindows could not open display. Exiting.") );
05e2b077
VZ
173 return false;
174 }
175
176 if ( !wxAppBase::Initialize(argc, argv) )
177 {
178 XCloseDisplay(xdisplay);
179
180 return false;
a11672a4 181 }
256d631a
JS
182
183 if (syncDisplay)
256d631a 184 XSynchronize(xdisplay, True);
e2386592 185
05e2b077 186 ms_display = (WXDisplay*) xdisplay;
7c9955d1 187
a11672a4 188 XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask);
e2386592 189
2b5f62a0 190 // Misc.
7e4501ee 191 wxSetDetectableAutoRepeat( TRUE );
a11672a4 192
2b5f62a0
VZ
193#if wxUSE_UNICODE
194 // Glib's type system required by Pango
195 g_type_init();
7c9955d1 196#endif
2b5f62a0 197
05e2b077
VZ
198#if wxUSE_INTL
199 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
200#endif
a11672a4 201
05e2b077
VZ
202 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
203 wxClientWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
a11672a4 204
05e2b077 205 return true;
a11672a4
RR
206}
207
05e2b077 208void wxApp::CleanUp()
83df96d6 209{
05e2b077
VZ
210 delete wxWidgetHashTable;
211 wxWidgetHashTable = NULL;
212 delete wxClientWidgetHashTable;
213 wxClientWidgetHashTable = NULL;
83df96d6 214
05e2b077
VZ
215 wxAppBase::CleanUp();
216}
83df96d6 217
83df96d6
JS
218wxApp::wxApp()
219{
00344dd0 220 // TODO: parse the command line
83df96d6
JS
221 argc = 0;
222 argv = NULL;
00344dd0 223
83df96d6 224 m_mainColormap = (WXColormap) NULL;
7eaac9f5 225 m_topLevelWidget = (WXWindow) NULL;
83df96d6 226 m_maxRequestSize = 0;
1b0fb34b 227 m_mainLoop = NULL;
256d631a
JS
228 m_showIconic = FALSE;
229 m_initialSize = wxDefaultSize;
366e8ae6 230
8601b2e1 231#if !wxUSE_NANOX
9ce8d6a2 232 m_visualInfo = NULL;
8601b2e1 233#endif
dc4025af
RR
234}
235
236wxApp::~wxApp()
237{
8601b2e1 238#if !wxUSE_NANOX
9ce8d6a2 239 delete m_visualInfo;
8601b2e1 240#endif
83df96d6
JS
241}
242
243bool wxApp::Initialized()
244{
245 if (GetTopWindow())
246 return TRUE;
247 else
248 return FALSE;
249}
250
251int wxApp::MainLoop()
252{
c978d361 253 int rt;
1b0fb34b 254 m_mainLoop = new wxEventLoop;
83df96d6 255
1b0fb34b 256 rt = m_mainLoop->Run();
83df96d6 257
1b0fb34b
JS
258 delete m_mainLoop;
259 m_mainLoop = NULL;
260 return rt;
261}
83df96d6 262
70b8ab77 263#if !wxUSE_NANOX
f809133f
RR
264//-----------------------------------------------------------------------
265// X11 predicate function for exposure compression
266//-----------------------------------------------------------------------
267
268struct wxExposeInfo
269{
270 Window window;
271 Bool found_non_matching;
272};
273
274static Bool expose_predicate (Display *display, XEvent *xevent, XPointer arg)
275{
276 wxExposeInfo *info = (wxExposeInfo*) arg;
e2386592 277
f809133f
RR
278 if (info->found_non_matching)
279 return FALSE;
e2386592 280
f809133f
RR
281 if (xevent->xany.type != Expose)
282 {
283 info->found_non_matching = TRUE;
284 return FALSE;
285 }
e2386592 286
f809133f
RR
287 if (xevent->xexpose.window != info->window)
288 {
289 info->found_non_matching = TRUE;
290 return FALSE;
291 }
e2386592 292
f809133f
RR
293 return TRUE;
294}
70b8ab77
JS
295#endif
296 // wxUSE_NANOX
f809133f
RR
297
298//-----------------------------------------------------------------------
086fd560 299// Processes an X event, returning TRUE if the event was processed.
f809133f
RR
300//-----------------------------------------------------------------------
301
086fd560 302bool wxApp::ProcessXEvent(WXEvent* _event)
1b0fb34b
JS
303{
304 XEvent* event = (XEvent*) _event;
83df96d6 305
1b0fb34b 306 wxWindow* win = NULL;
c79a329d 307 Window window = XEventGetWindow(event);
e2386592 308#if 0
1b0fb34b 309 Window actualWindow = window;
e2386592 310#endif
0b5c0e1a 311
1b0fb34b 312 // Find the first wxWindow that corresponds to this event window
774b90fb
JS
313 // Because we're receiving events after a window
314 // has been destroyed, assume a 1:1 match between
315 // Window and wxWindow, so if it's not in the table,
316 // it must have been destroyed.
e2386592 317
774b90fb
JS
318 win = wxGetWindowFromTable(window);
319 if (!win)
ab6b6b15
RR
320 {
321#if wxUSE_TWO_WINDOWS
322 win = wxGetClientWindowFromTable(window);
323 if (!win)
324#endif
325 return FALSE;
326 }
83df96d6 327
0b5c0e1a
JS
328#ifdef __WXDEBUG__
329 wxString windowClass = win->GetClassInfo()->GetClassName();
330#endif
e2386592 331
1b0fb34b
JS
332 switch (event->type)
333 {
2f12683e
RR
334 case Expose:
335 {
8601b2e1 336#if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
f41bc3e3 337 if (event->xexpose.window != (Window)win->GetClientAreaWindow())
ab6b6b15
RR
338 {
339 XEvent tmp_event;
340 wxExposeInfo info;
341 info.window = event->xexpose.window;
342 info.found_non_matching = FALSE;
343 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
344 {
345 // Don't worry about optimizing redrawing the border etc.
346 }
347 win->NeedUpdateNcAreaInIdle();
348 }
349 else
350#endif
351 {
352 win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
353 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
ab6b6b15 354 win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
2f12683e
RR
355 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
356
357#if !wxUSE_NANOX
ab6b6b15
RR
358 XEvent tmp_event;
359 wxExposeInfo info;
360 info.window = event->xexpose.window;
361 info.found_non_matching = FALSE;
362 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
363 {
364 win->GetUpdateRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
365 tmp_event.xexpose.width, tmp_event.xexpose.height );
e2386592 366
ab6b6b15
RR
367 win->GetClearRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
368 tmp_event.xexpose.width, tmp_event.xexpose.height );
369 }
2f12683e 370#endif
c2c0dabf
RR
371
372 // This simplifies the expose and clear areas to simple
373 // rectangles.
374 win->GetUpdateRegion() = win->GetUpdateRegion().GetBox();
375 win->GetClearRegion() = win->GetClearRegion().GetBox();
e2386592 376
c2c0dabf 377 // If we only have one X11 window, always indicate
e2386592 378 // that borders might have to be redrawn.
f41bc3e3 379 if (win->GetMainWindow() == win->GetClientAreaWindow())
ab6b6b15 380 win->NeedUpdateNcAreaInIdle();
2f12683e 381
ab6b6b15
RR
382 // Only erase background, paint in idle time.
383 win->SendEraseEvents();
09a1dffa
JS
384
385 // EXPERIMENT
386 //win->Update();
ab6b6b15 387 }
2f12683e
RR
388
389 return TRUE;
390 }
e2386592 391
2f12683e
RR
392#if !wxUSE_NANOX
393 case GraphicsExpose:
394 {
4175e952
RR
395 printf( "GraphicExpose event\n" );
396
89cd4125 397 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win->GetName().c_str());
e2386592 398
2f12683e
RR
399 win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
400 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 401
2f12683e
RR
402 win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
403 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 404
2f12683e
RR
405 if (event->xgraphicsexpose.count == 0)
406 {
407 // Only erase background, paint in idle time.
408 win->SendEraseEvents();
c2c0dabf 409 // win->Update();
2f12683e
RR
410 }
411
412 return TRUE;
413 }
414#endif
415
1b0fb34b 416 case KeyPress:
83df96d6 417 {
9d2cef1c 418 if (!win->IsEnabled())
086fd560 419 return FALSE;
b513212d 420
9d2cef1c
RR
421 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
422 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 423
9d2cef1c 424 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
e2386592 425
120b822d
RR
426 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
427 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
428 return TRUE;
e2386592 429
120b822d 430 keyEvent.SetEventType(wxEVT_CHAR);
2b5f62a0
VZ
431 // Do the translation again, retaining the ASCII
432 // code.
433 wxTranslateKeyEvent(keyEvent, win, window, event, TRUE);
120b822d
RR
434 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
435 return TRUE;
e2386592 436
120b822d
RR
437 if ( (keyEvent.m_keyCode == WXK_TAB) &&
438 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
9d2cef1c 439 {
120b822d
RR
440 wxNavigationKeyEvent new_event;
441 new_event.SetEventObject( win->GetParent() );
442 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
443 new_event.SetDirection( (keyEvent.m_keyCode == WXK_TAB) );
444 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
445 new_event.SetWindowChange( keyEvent.ControlDown() );
446 new_event.SetCurrentFocus( win );
447 return win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1b0fb34b 448 }
120b822d
RR
449
450 return FALSE;
83df96d6 451 }
1b0fb34b 452 case KeyRelease:
7eaac9f5 453 {
9d2cef1c 454 if (!win->IsEnabled())
086fd560 455 return FALSE;
b513212d 456
9d2cef1c
RR
457 wxKeyEvent keyEvent(wxEVT_KEY_UP);
458 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 459
086fd560 460 return win->GetEventHandler()->ProcessEvent( keyEvent );
7eaac9f5 461 }
256d631a
JS
462 case ConfigureNotify:
463 {
c79a329d 464#if wxUSE_NANOX
9d2cef1c 465 if (event->update.utype == GR_UPDATE_SIZE)
c79a329d 466#endif
256d631a 467 {
c2c0dabf
RR
468 if (win->IsTopLevel())
469 {
470 wxTopLevelWindow *tlw = (wxTopLevelWindow*) win;
471 tlw->SetConfigureGeometry( XConfigureEventGetX(event), XConfigureEventGetY(event),
472 XConfigureEventGetWidth(event), XConfigureEventGetHeight(event) );
473 }
e2386592 474
2f12683e 475 if (win->IsTopLevel() && win->IsShown())
77df2fbc
RR
476 {
477 wxTopLevelWindowX11 *tlw = (wxTopLevelWindowX11 *) win;
478 tlw->SetNeedResizeInIdle();
479 }
480 else
481 {
482 wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() );
483 sizeEvent.SetEventObject( win );
e2386592 484
77df2fbc
RR
485 return win->GetEventHandler()->ProcessEvent( sizeEvent );
486 }
256d631a 487 }
086fd560 488 return FALSE;
c2ff68d3 489 break;
256d631a
JS
490 }
491#if !wxUSE_NANOX
1b0fb34b 492 case PropertyNotify:
7eaac9f5 493 {
0b5c0e1a 494 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
086fd560 495 return HandlePropertyChange(_event);
7eaac9f5 496 }
b513212d 497 case ClientMessage:
3a0b23eb 498 {
9d2cef1c 499 if (!win->IsEnabled())
086fd560 500 return FALSE;
3a0b23eb 501
7e4501ee
RR
502 Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True);
503 Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True);
b513212d
JS
504
505 if (event->xclient.message_type == wm_protocols)
506 {
6a44bffd 507 if ((Atom) (event->xclient.data.l[0]) == wm_delete_window)
b513212d 508 {
9d2cef1c 509 win->Close(FALSE);
086fd560 510 return TRUE;
b513212d
JS
511 }
512 }
086fd560 513 return FALSE;
b513212d 514 }
2f12683e
RR
515#if 0
516 case DestroyNotify:
517 {
518 printf( "destroy from %s\n", win->GetName().c_str() );
519 break;
520 }
521 case CreateNotify:
522 {
523 printf( "create from %s\n", win->GetName().c_str() );
524 break;
525 }
526 case MapRequest:
527 {
528 printf( "map request from %s\n", win->GetName().c_str() );
529 break;
530 }
1b0fb34b 531 case ResizeRequest:
7eaac9f5 532 {
2f12683e 533 printf( "resize request from %s\n", win->GetName().c_str() );
e2386592 534
7266b672 535 Display *disp = (Display*) wxGetDisplay();
1b0fb34b 536 XEvent report;
e2386592 537
1b0fb34b
JS
538 // to avoid flicker
539 report = * event;
540 while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report));
e2386592 541
b1633d20
RR
542 wxSize sz = win->GetSize();
543 wxSizeEvent sizeEvent(sz, win->GetId());
544 sizeEvent.SetEventObject(win);
1b0fb34b 545
b1633d20 546 return win->GetEventHandler()->ProcessEvent( sizeEvent );
7eaac9f5 547 }
256d631a 548#endif
b1633d20 549#endif
256d631a
JS
550#if wxUSE_NANOX
551 case GR_EVENT_TYPE_CLOSE_REQ:
552 {
553 if (win)
554 {
555 win->Close(FALSE);
086fd560 556 return TRUE;
256d631a 557 }
086fd560 558 return FALSE;
256d631a
JS
559 break;
560 }
c79a329d 561#endif
1b0fb34b
JS
562 case EnterNotify:
563 case LeaveNotify:
564 case ButtonPress:
565 case ButtonRelease:
566 case MotionNotify:
7eaac9f5 567 {
7e4501ee 568 if (!win->IsEnabled())
086fd560 569 return FALSE;
366e8ae6 570
9691c806
RR
571 // Here we check if the top level window is
572 // disabled, which is one aspect of modality.
573 wxWindow *tlw = win;
574 while (tlw && !tlw->IsTopLevel())
575 tlw = tlw->GetParent();
576 if (tlw && !tlw->IsEnabled())
086fd560 577 return FALSE;
e2386592 578
7e4501ee 579 if (event->type == ButtonPress)
1b0fb34b 580 {
7e4501ee 581 if ((win != wxWindow::FindFocus()) && win->AcceptsFocus())
9d2cef1c
RR
582 {
583 // This might actually be done in wxWindow::SetFocus()
ea1ad04b 584 // and not here. TODO.
9d2cef1c
RR
585 g_prevFocus = wxWindow::FindFocus();
586 g_nextFocus = win;
e2386592 587
58ec2255
JS
588 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
589
590 // Record the fact that this window is
591 // getting the focus, because we'll need to
592 // check if its parent is getting a bogus
593 // focus and duly ignore it.
594 // TODO: may need to have this code in SetFocus, too.
595 extern wxWindow* g_GettingFocus;
596 g_GettingFocus = win;
9d2cef1c
RR
597 win->SetFocus();
598 }
1b0fb34b 599 }
e2386592 600
ea1ad04b
RR
601#if !wxUSE_NANOX
602 if (event->type == LeaveNotify || event->type == EnterNotify)
603 {
604 // Throw out NotifyGrab and NotifyUngrab
605 if (event->xcrossing.mode != NotifyNormal)
086fd560 606 return FALSE;
ea1ad04b
RR
607 }
608#endif
7e4501ee
RR
609 wxMouseEvent wxevent;
610 wxTranslateMouseEvent(wxevent, win, window, event);
086fd560 611 return win->GetEventHandler()->ProcessEvent( wxevent );
7eaac9f5 612 }
1b0fb34b
JS
613 case FocusIn:
614 {
256d631a 615#if !wxUSE_NANOX
9d2cef1c
RR
616 if ((event->xfocus.detail != NotifyPointer) &&
617 (event->xfocus.mode == NotifyNormal))
256d631a 618#endif
1b0fb34b 619 {
58ec2255 620 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
366e8ae6 621
58ec2255
JS
622 extern wxWindow* g_GettingFocus;
623 if (g_GettingFocus && g_GettingFocus->GetParent() == win)
624 {
625 // Ignore this, this can be a spurious FocusIn
626 // caused by a child having its focus set.
627 g_GettingFocus = NULL;
628 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
629 return TRUE;
630 }
631 else
632 {
633 wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId());
634 focusEvent.SetEventObject(win);
635 focusEvent.SetWindow( g_prevFocus );
636 g_prevFocus = NULL;
637
638 return win->GetEventHandler()->ProcessEvent(focusEvent);
639 }
1b0fb34b 640 }
086fd560 641 return FALSE;
1b0fb34b
JS
642 break;
643 }
644 case FocusOut:
645 {
256d631a 646#if !wxUSE_NANOX
9d2cef1c
RR
647 if ((event->xfocus.detail != NotifyPointer) &&
648 (event->xfocus.mode == NotifyNormal))
256d631a 649#endif
1b0fb34b 650 {
58ec2255 651 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
e2386592 652
1b0fb34b
JS
653 wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId());
654 focusEvent.SetEventObject(win);
9d2cef1c
RR
655 focusEvent.SetWindow( g_nextFocus );
656 g_nextFocus = NULL;
086fd560 657 return win->GetEventHandler()->ProcessEvent(focusEvent);
1b0fb34b 658 }
086fd560 659 return FALSE;
1b0fb34b
JS
660 break;
661 }
662 default:
663 {
45ff6421
JS
664#ifdef __WXDEBUG__
665 //wxString eventName = wxGetXEventName(XEvent& event);
666 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
667#endif
086fd560 668 return FALSE;
1b0fb34b
JS
669 break;
670 }
83df96d6 671 }
086fd560 672 return FALSE;
83df96d6
JS
673}
674
675// Returns TRUE if more time is needed.
1b0fb34b
JS
676// Note that this duplicates wxEventLoopImpl::SendIdleEvent
677// but ProcessIdle may be needed by apps, so is kept.
83df96d6
JS
678bool wxApp::ProcessIdle()
679{
680 wxIdleEvent event;
681 event.SetEventObject(this);
682 ProcessEvent(event);
683
684 return event.MoreRequested();
685}
686
687void wxApp::ExitMainLoop()
688{
1b0fb34b
JS
689 if (m_mainLoop)
690 m_mainLoop->Exit(0);
83df96d6
JS
691}
692
693// Is a message/event pending?
694bool wxApp::Pending()
695{
1b0fb34b 696 return wxEventLoop::GetActive()->Pending();
83df96d6
JS
697}
698
699// Dispatch a message.
700void wxApp::Dispatch()
701{
1b0fb34b 702 wxEventLoop::GetActive()->Dispatch();
83df96d6
JS
703}
704
705// This should be redefined in a derived class for
706// handling property change events for XAtom IPC.
086fd560 707bool wxApp::HandlePropertyChange(WXEvent *event)
83df96d6
JS
708{
709 // by default do nothing special
7eaac9f5 710 // TODO: what to do for X11
256d631a 711 // XtDispatchEvent((XEvent*) event);
086fd560 712 return FALSE;
83df96d6
JS
713}
714
715void wxApp::OnIdle(wxIdleEvent& event)
716{
0d1dff01 717 static bool s_inOnIdle = FALSE;
83df96d6
JS
718
719 // Avoid recursion (via ProcessEvent default case)
0d1dff01 720 if (s_inOnIdle)
83df96d6
JS
721 return;
722
0d1dff01 723 s_inOnIdle = TRUE;
83df96d6 724
0d1dff01
RR
725 // Resend in the main thread events which have been prepared in other
726 // threads
83df96d6
JS
727 ProcessPendingEvents();
728
0d1dff01 729 // 'Garbage' collection of windows deleted with Close()
83df96d6
JS
730 DeletePendingObjects();
731
83df96d6
JS
732 // Send OnIdle events to all windows
733 bool needMore = SendIdleEvents();
734
735 if (needMore)
736 event.RequestMore(TRUE);
737
0d1dff01 738 s_inOnIdle = FALSE;
83df96d6
JS
739}
740
e2478fde 741void wxApp::WakeUpIdle()
83df96d6 742{
e2478fde
VZ
743 // TODO: use wxMotif implementation?
744
83df96d6
JS
745 // Wake up the idle handler processor, even if it is in another thread...
746}
747
748
749// Send idle event to all top-level windows
750bool wxApp::SendIdleEvents()
751{
752 bool needMore = FALSE;
753
754 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
755 while (node)
756 {
757 wxWindow* win = node->GetData();
758 if (SendIdleEvents(win))
759 needMore = TRUE;
760 node = node->GetNext();
761 }
762
763 return needMore;
764}
765
766// Send idle event to window and all subwindows
767bool wxApp::SendIdleEvents(wxWindow* win)
768{
769 bool needMore = FALSE;
770
771 wxIdleEvent event;
772 event.SetEventObject(win);
0d1dff01
RR
773
774 win->GetEventHandler()->ProcessEvent(event);
775
83df96d6
JS
776 if (event.MoreRequested())
777 needMore = TRUE;
778
09a1dffa 779 wxWindowListNode* node = win->GetChildren().GetFirst();
83df96d6
JS
780 while (node)
781 {
09a1dffa 782 wxWindow* win = (wxWindow*) node->GetData();
83df96d6
JS
783 if (SendIdleEvents(win))
784 needMore = TRUE;
785
09a1dffa 786 node = node->GetNext();
83df96d6 787 }
e2386592 788
193e19cf
RR
789 win->OnInternalIdle();
790
0d1dff01 791 return needMore;
83df96d6
JS
792}
793
256d631a 794// Create display, and other initialization
83df96d6
JS
795bool wxApp::OnInitGui()
796{
ca7497c2
JS
797 // Eventually this line will be removed, but for
798 // now we don't want to try popping up a dialog
799 // for error messages.
800 delete wxLog::SetActiveTarget(new wxLogStderr);
e2386592 801
ea596687 802 if (!wxAppBase::OnInitGui())
dc4025af 803 return FALSE;
e2386592 804
a11672a4 805 GetMainColormap( wxApp::GetDisplay() );
256d631a 806
a11672a4 807 m_maxRequestSize = XMaxRequestSize( (Display*) wxApp::GetDisplay() );
83df96d6 808
8601b2e1 809#if !wxUSE_NANOX
9ce8d6a2
MB
810 m_visualInfo = new wxXVisualInfo;
811 wxFillXVisualInfo( m_visualInfo, (Display*) wxApp::GetDisplay() );
8601b2e1 812#endif
366e8ae6 813
83df96d6
JS
814 return TRUE;
815}
816
2b5f62a0
VZ
817#if wxUSE_UNICODE
818
819#include <pango/pango.h>
820#include <pango/pangox.h>
821#include <pango/pangoxft.h>
822
823PangoContext* wxApp::GetPangoContext()
824{
825 static PangoContext *ret = NULL;
826 if (ret)
827 return ret;
7c9955d1 828
2b5f62a0 829 Display *xdisplay = (Display*) wxApp::GetDisplay();
7c9955d1 830
2b5f62a0
VZ
831#if 1
832 int xscreen = DefaultScreen(xdisplay);
833 static int use_xft = -1;
834 if (use_xft == -1)
835 {
836 wxString val = wxGetenv( L"GDK_USE_XFT" );
837 use_xft = (val == L"1");
838 }
7c9955d1 839
2b5f62a0
VZ
840 if (use_xft)
841 ret = pango_xft_get_context( xdisplay, xscreen );
842 else
843#endif
844 ret = pango_x_get_context( xdisplay );
7c9955d1 845
2b5f62a0
VZ
846 if (!PANGO_IS_CONTEXT(ret))
847 wxLogError( wxT("No pango context.") );
7c9955d1 848
2b5f62a0
VZ
849 return ret;
850}
851#endif
852
83df96d6
JS
853WXColormap wxApp::GetMainColormap(WXDisplay* display)
854{
855 if (!display) /* Must be called first with non-NULL display */
856 return m_mainColormap;
857
858 int defaultScreen = DefaultScreen((Display*) display);
859 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
860
861 Colormap c = DefaultColormapOfScreen(screen);
862
863 if (!m_mainColormap)
864 m_mainColormap = (WXColormap) c;
865
866 return (WXColormap) c;
867}
868
8354aa92 869Window wxGetWindowParent(Window window)
7eaac9f5 870{
86fd8bda 871 wxASSERT_MSG( window, "invalid window" );
e2386592 872
86fd8bda
RR
873 return (Window) 0;
874
7eaac9f5 875 Window parent, root = 0;
c79a329d
JS
876#if wxUSE_NANOX
877 int noChildren = 0;
878#else
7eaac9f5 879 unsigned int noChildren = 0;
c79a329d 880#endif
ea596687 881 Window* children = NULL;
c79a329d 882
ee351013 883 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
c79a329d
JS
884 int res = 1;
885#if !wxUSE_NANOX
886 res =
887#endif
888 XQueryTree((Display*) wxGetDisplay(), window, & root, & parent,
ee351013 889 & children, & noChildren);
ea596687
JS
890 if (children)
891 XFree(children);
892 if (res)
7eaac9f5
JS
893 return parent;
894 else
895 return (Window) 0;
896}
897
e2478fde 898void wxApp::Exit()
83df96d6 899{
83df96d6 900 wxApp::CleanUp();
e2478fde
VZ
901
902 wxAppConsole::Exit();
83df96d6
JS
903}
904
905// Yield to other processes
906
907bool wxApp::Yield(bool onlyIfNeeded)
908{
bbcd408a
JS
909 // Sometimes only 2 yields seem
910 // to do the trick, e.g. in the
911 // progress dialog
912 int i;
913 for (i = 0; i < 2; i++)
83df96d6 914 {
bbcd408a
JS
915 bool s_inYield = FALSE;
916
917 if ( s_inYield )
83df96d6 918 {
bbcd408a
JS
919 if ( !onlyIfNeeded )
920 {
921 wxFAIL_MSG( wxT("wxYield called recursively" ) );
922 }
83df96d6 923
bbcd408a
JS
924 return FALSE;
925 }
83df96d6 926
bbcd408a 927 s_inYield = TRUE;
83df96d6 928
bbcd408a
JS
929 // Make sure we have an event loop object,
930 // or Pending/Dispatch will fail
931 wxEventLoop* eventLoop = wxEventLoop::GetActive();
932 wxEventLoop* newEventLoop = NULL;
933 if (!eventLoop)
934 {
935 newEventLoop = new wxEventLoop;
936 wxEventLoop::SetActive(newEventLoop);
937 }
df0e1b64 938
2b5f62a0
VZ
939 // Call dispatch at least once so that sockets
940 // can be tested
941 wxTheApp->Dispatch();
7c9955d1 942
bbcd408a
JS
943 while (wxTheApp && wxTheApp->Pending())
944 wxTheApp->Dispatch();
83df96d6 945
868741e9 946#if wxUSE_TIMER
bbcd408a 947 wxTimer::NotifyTimers();
868741e9 948#endif
bbcd408a 949 ProcessIdle();
868741e9 950
bbcd408a
JS
951 if (newEventLoop)
952 {
953 wxEventLoop::SetActive(NULL);
954 delete newEventLoop;
955 }
df0e1b64 956
bbcd408a
JS
957 s_inYield = FALSE;
958 }
83df96d6
JS
959
960 return TRUE;
961}
962
d715d419
VZ
963#ifdef __WXDEBUG__
964
5968a0dc 965void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
7edcafa4
JS
966{
967 // While the GUI isn't working that well, just print out the
968 // message.
193e19cf 969#if 1
5968a0dc 970 wxAppBase::OnAssert(file, line, cond, msg);
7edcafa4
JS
971#else
972 wxString msg2;
973 msg2.Printf("At file %s:%d: %s", file, line, msg);
974 wxLogDebug(msg2);
975#endif
976}
977
d715d419
VZ
978#endif // __WXDEBUG__
979