]> git.saurik.com Git - wxWidgets.git/blame - src/x11/app.cpp
misc fixes for wxDirDialog; new wxDD_CHANGE_DIR flag (patch 1478051)
[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"
32d4c30a
WS
23#endif
24
83df96d6 25#include "wx/gdicmn.h"
83df96d6 26#include "wx/icon.h"
83df96d6 27#include "wx/dialog.h"
83df96d6
JS
28#include "wx/module.h"
29#include "wx/memory.h"
1b0fb34b 30#include "wx/evtloop.h"
868741e9 31#include "wx/timer.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
83df96d6
JS
49extern wxList wxPendingDelete;
50
b8033a5d
VZ
51wxWindowHash *wxWidgetHashTable = NULL;
52wxWindowHash *wxClientWidgetHashTable = NULL;
9d2cef1c 53
4bcc3647 54static bool g_showIconic = false;
9d2cef1c 55static wxSize g_initialSize = wxDefaultSize;
83df96d6 56
9d2cef1c
RR
57// This is required for wxFocusEvent::SetWindow(). It will only
58// work for focus events which we provoke ourselves (by calling
59// SetFocus()). It will not work for those events, which X11
60// generates itself.
61static wxWindow *g_nextFocus = NULL;
62static wxWindow *g_prevFocus = NULL;
83df96d6 63
9d2cef1c
RR
64//------------------------------------------------------------------------
65// X11 error handling
66//------------------------------------------------------------------------
83df96d6
JS
67
68#ifdef __WXDEBUG__
1b0fb34b 69typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
83df96d6 70
1b0fb34b 71XErrorHandlerFunc gs_pfnXErrorHandler = 0;
83df96d6 72
1b0fb34b
JS
73static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
74{
75 // just forward to the default handler for now
c79a329d
JS
76 if (gs_pfnXErrorHandler)
77 return gs_pfnXErrorHandler(dpy, xevent);
78 else
79 return 0;
1b0fb34b 80}
83df96d6
JS
81#endif // __WXDEBUG__
82
9d2cef1c
RR
83//------------------------------------------------------------------------
84// wxApp
85//------------------------------------------------------------------------
86
83df96d6 87long wxApp::sm_lastMessageTime = 0;
a11672a4 88WXDisplay *wxApp::ms_display = NULL;
83df96d6 89
9d2cef1c
RR
90IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
91
92BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 93 EVT_IDLE(wxAppBase::OnIdle)
9d2cef1c 94END_EVENT_TABLE()
256d631a 95
3ee937aa 96bool wxApp::Initialize(int& argC, wxChar **argV)
83df96d6 97{
05e2b077 98#if defined(__WXDEBUG__) && !wxUSE_NANOX
a11672a4
RR
99 // install the X error handler
100 gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler );
101#endif // __WXDEBUG__
102
c6441a0d 103 wxString displayName;
4bcc3647 104 bool syncDisplay = false;
256d631a 105
3ee937aa
JS
106 int argCOrig = argC;
107 for ( int i = 0; i < argCOrig; i++ )
256d631a 108 {
3ee937aa 109 if (wxStrcmp( argV[i], _T("-display") ) == 0)
256d631a 110 {
3ee937aa 111 if (i < (argC - 1))
256d631a 112 {
3ee937aa 113 argV[i++] = NULL;
05e2b077 114
3ee937aa 115 displayName = argV[i];
05e2b077 116
3ee937aa
JS
117 argV[i] = NULL;
118 argC -= 2;
256d631a
JS
119 }
120 }
3ee937aa 121 else if (wxStrcmp( argV[i], _T("-geometry") ) == 0)
256d631a 122 {
3ee937aa 123 if (i < (argC - 1))
256d631a 124 {
3ee937aa 125 argV[i++] = NULL;
05e2b077 126
256d631a 127 int w, h;
3ee937aa 128 if (wxSscanf(argV[i], _T("%dx%d"), &w, &h) != 2)
256d631a 129 {
05e2b077 130 wxLogError( _("Invalid geometry specification '%s'"),
3ee937aa 131 wxString(argV[i]).c_str() );
256d631a
JS
132 }
133 else
134 {
135 g_initialSize = wxSize(w, h);
136 }
05e2b077 137
3ee937aa
JS
138 argV[i] = NULL;
139 argC -= 2;
256d631a
JS
140 }
141 }
3ee937aa 142 else if (wxStrcmp( argV[i], _T("-sync") ) == 0)
256d631a 143 {
4bcc3647 144 syncDisplay = true;
05e2b077 145
3ee937aa
JS
146 argV[i] = NULL;
147 argC--;
256d631a 148 }
3ee937aa 149 else if (wxStrcmp( argV[i], _T("-iconic") ) == 0)
256d631a 150 {
4bcc3647 151 g_showIconic = true;
45ff6421 152
3ee937aa
JS
153 argV[i] = NULL;
154 argC--;
256d631a 155 }
05e2b077 156 }
256d631a 157
3ee937aa 158 if ( argC != argCOrig )
05e2b077
VZ
159 {
160 // remove the argumens we consumed
3ee937aa 161 for ( int i = 0; i < argC; i++ )
05e2b077 162 {
3ee937aa 163 while ( !argV[i] )
05e2b077 164 {
3ee937aa 165 memmove(argV + i, argV + i + 1, argCOrig - i);
05e2b077
VZ
166 }
167 }
256d631a 168 }
a11672a4 169
7c9955d1 170 // X11 display stuff
c6441a0d
VS
171 Display *xdisplay;
172 if ( displayName.empty() )
173 xdisplay = XOpenDisplay( NULL );
174 else
175 xdisplay = XOpenDisplay( displayName.ToAscii() );
a11672a4
RR
176 if (!xdisplay)
177 {
77ffb593 178 wxLogError( _("wxWidgets could not open display. Exiting.") );
05e2b077
VZ
179 return false;
180 }
181
256d631a 182 if (syncDisplay)
256d631a 183 XSynchronize(xdisplay, True);
e2386592 184
05e2b077 185 ms_display = (WXDisplay*) xdisplay;
7c9955d1 186
a11672a4 187 XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask);
e2386592 188
2b5f62a0 189 // Misc.
4bcc3647 190 wxSetDetectableAutoRepeat( true );
a11672a4 191
3ee937aa 192 if ( !wxAppBase::Initialize(argC, argV) )
5b004007
VZ
193 {
194 XCloseDisplay(xdisplay);
195
196 return false;
197 }
198
2b5f62a0
VZ
199#if wxUSE_UNICODE
200 // Glib's type system required by Pango
201 g_type_init();
7c9955d1 202#endif
2b5f62a0 203
05e2b077
VZ
204#if wxUSE_INTL
205 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
206#endif
a11672a4 207
b8033a5d
VZ
208 wxWidgetHashTable = new wxWindowHash;
209 wxClientWidgetHashTable = new wxWindowHash;
a11672a4 210
05e2b077 211 return true;
a11672a4
RR
212}
213
05e2b077 214void wxApp::CleanUp()
83df96d6 215{
05e2b077
VZ
216 delete wxWidgetHashTable;
217 wxWidgetHashTable = NULL;
218 delete wxClientWidgetHashTable;
219 wxClientWidgetHashTable = NULL;
83df96d6 220
05e2b077
VZ
221 wxAppBase::CleanUp();
222}
83df96d6 223
83df96d6
JS
224wxApp::wxApp()
225{
00344dd0 226 // TODO: parse the command line
83df96d6
JS
227 argc = 0;
228 argv = NULL;
00344dd0 229
83df96d6 230 m_mainColormap = (WXColormap) NULL;
7eaac9f5 231 m_topLevelWidget = (WXWindow) NULL;
83df96d6 232 m_maxRequestSize = 0;
4bcc3647 233 m_showIconic = false;
256d631a 234 m_initialSize = wxDefaultSize;
366e8ae6 235
8601b2e1 236#if !wxUSE_NANOX
9ce8d6a2 237 m_visualInfo = NULL;
8601b2e1 238#endif
dc4025af
RR
239}
240
241wxApp::~wxApp()
242{
8601b2e1 243#if !wxUSE_NANOX
9ce8d6a2 244 delete m_visualInfo;
8601b2e1 245#endif
83df96d6
JS
246}
247
70b8ab77 248#if !wxUSE_NANOX
aedeab23 249
f809133f
RR
250//-----------------------------------------------------------------------
251// X11 predicate function for exposure compression
252//-----------------------------------------------------------------------
253
254struct wxExposeInfo
255{
256 Window window;
257 Bool found_non_matching;
258};
259
aedeab23
VZ
260extern "C"
261Bool wxX11ExposePredicate (Display *display, XEvent *xevent, XPointer arg)
f809133f
RR
262{
263 wxExposeInfo *info = (wxExposeInfo*) arg;
e2386592 264
f809133f
RR
265 if (info->found_non_matching)
266 return FALSE;
e2386592 267
f809133f
RR
268 if (xevent->xany.type != Expose)
269 {
4bcc3647 270 info->found_non_matching = true;
f809133f
RR
271 return FALSE;
272 }
e2386592 273
f809133f
RR
274 if (xevent->xexpose.window != info->window)
275 {
4bcc3647 276 info->found_non_matching = true;
f809133f
RR
277 return FALSE;
278 }
e2386592 279
f809133f
RR
280 return TRUE;
281}
aedeab23
VZ
282
283#endif // wxUSE_NANOX
f809133f
RR
284
285//-----------------------------------------------------------------------
4bcc3647 286// Processes an X event, returning true if the event was processed.
f809133f
RR
287//-----------------------------------------------------------------------
288
086fd560 289bool wxApp::ProcessXEvent(WXEvent* _event)
1b0fb34b
JS
290{
291 XEvent* event = (XEvent*) _event;
83df96d6 292
1b0fb34b 293 wxWindow* win = NULL;
c79a329d 294 Window window = XEventGetWindow(event);
e2386592 295#if 0
1b0fb34b 296 Window actualWindow = window;
e2386592 297#endif
0b5c0e1a 298
1b0fb34b 299 // Find the first wxWindow that corresponds to this event window
774b90fb
JS
300 // Because we're receiving events after a window
301 // has been destroyed, assume a 1:1 match between
302 // Window and wxWindow, so if it's not in the table,
303 // it must have been destroyed.
e2386592 304
774b90fb
JS
305 win = wxGetWindowFromTable(window);
306 if (!win)
ab6b6b15
RR
307 {
308#if wxUSE_TWO_WINDOWS
309 win = wxGetClientWindowFromTable(window);
310 if (!win)
311#endif
4bcc3647 312 return false;
ab6b6b15 313 }
83df96d6 314
0b5c0e1a
JS
315#ifdef __WXDEBUG__
316 wxString windowClass = win->GetClassInfo()->GetClassName();
317#endif
e2386592 318
1b0fb34b
JS
319 switch (event->type)
320 {
2f12683e
RR
321 case Expose:
322 {
8601b2e1 323#if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
f41bc3e3 324 if (event->xexpose.window != (Window)win->GetClientAreaWindow())
ab6b6b15
RR
325 {
326 XEvent tmp_event;
327 wxExposeInfo info;
328 info.window = event->xexpose.window;
4bcc3647 329 info.found_non_matching = false;
aedeab23 330 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, wxX11ExposePredicate, (XPointer) &info ))
ab6b6b15
RR
331 {
332 // Don't worry about optimizing redrawing the border etc.
333 }
334 win->NeedUpdateNcAreaInIdle();
335 }
336 else
337#endif
338 {
339 win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
340 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
ab6b6b15 341 win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
2f12683e
RR
342 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
343
344#if !wxUSE_NANOX
ab6b6b15
RR
345 XEvent tmp_event;
346 wxExposeInfo info;
347 info.window = event->xexpose.window;
4bcc3647 348 info.found_non_matching = false;
aedeab23 349 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, wxX11ExposePredicate, (XPointer) &info ))
ab6b6b15
RR
350 {
351 win->GetUpdateRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
352 tmp_event.xexpose.width, tmp_event.xexpose.height );
e2386592 353
ab6b6b15
RR
354 win->GetClearRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
355 tmp_event.xexpose.width, tmp_event.xexpose.height );
356 }
2f12683e 357#endif
c2c0dabf
RR
358
359 // This simplifies the expose and clear areas to simple
360 // rectangles.
361 win->GetUpdateRegion() = win->GetUpdateRegion().GetBox();
362 win->GetClearRegion() = win->GetClearRegion().GetBox();
e2386592 363
c2c0dabf 364 // If we only have one X11 window, always indicate
e2386592 365 // that borders might have to be redrawn.
f41bc3e3 366 if (win->GetMainWindow() == win->GetClientAreaWindow())
ab6b6b15 367 win->NeedUpdateNcAreaInIdle();
2f12683e 368
ab6b6b15
RR
369 // Only erase background, paint in idle time.
370 win->SendEraseEvents();
09a1dffa
JS
371
372 // EXPERIMENT
373 //win->Update();
ab6b6b15 374 }
2f12683e 375
4bcc3647 376 return true;
2f12683e 377 }
e2386592 378
2f12683e
RR
379#if !wxUSE_NANOX
380 case GraphicsExpose:
381 {
89cd4125 382 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win->GetName().c_str());
e2386592 383
2f12683e
RR
384 win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
385 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 386
2f12683e
RR
387 win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
388 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 389
2f12683e
RR
390 if (event->xgraphicsexpose.count == 0)
391 {
392 // Only erase background, paint in idle time.
393 win->SendEraseEvents();
c2c0dabf 394 // win->Update();
2f12683e
RR
395 }
396
4bcc3647 397 return true;
2f12683e
RR
398 }
399#endif
400
1b0fb34b 401 case KeyPress:
83df96d6 402 {
9d2cef1c 403 if (!win->IsEnabled())
4bcc3647 404 return false;
b513212d 405
9d2cef1c
RR
406 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
407 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 408
9d2cef1c 409 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
e2386592 410
120b822d
RR
411 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
412 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
4bcc3647 413 return true;
e2386592 414
120b822d 415 keyEvent.SetEventType(wxEVT_CHAR);
2b5f62a0
VZ
416 // Do the translation again, retaining the ASCII
417 // code.
4bcc3647 418 wxTranslateKeyEvent(keyEvent, win, window, event, true);
120b822d 419 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
4bcc3647 420 return true;
e2386592 421
120b822d
RR
422 if ( (keyEvent.m_keyCode == WXK_TAB) &&
423 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
9d2cef1c 424 {
120b822d
RR
425 wxNavigationKeyEvent new_event;
426 new_event.SetEventObject( win->GetParent() );
427 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
428 new_event.SetDirection( (keyEvent.m_keyCode == WXK_TAB) );
429 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
430 new_event.SetWindowChange( keyEvent.ControlDown() );
431 new_event.SetCurrentFocus( win );
432 return win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1b0fb34b 433 }
120b822d 434
4bcc3647 435 return false;
83df96d6 436 }
1b0fb34b 437 case KeyRelease:
7eaac9f5 438 {
9d2cef1c 439 if (!win->IsEnabled())
4bcc3647 440 return false;
b513212d 441
9d2cef1c
RR
442 wxKeyEvent keyEvent(wxEVT_KEY_UP);
443 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 444
086fd560 445 return win->GetEventHandler()->ProcessEvent( keyEvent );
7eaac9f5 446 }
256d631a
JS
447 case ConfigureNotify:
448 {
c79a329d 449#if wxUSE_NANOX
9d2cef1c 450 if (event->update.utype == GR_UPDATE_SIZE)
c79a329d 451#endif
256d631a 452 {
3cd2f0bd
VZ
453 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
454 if ( tlw )
c2c0dabf 455 {
c2c0dabf
RR
456 tlw->SetConfigureGeometry( XConfigureEventGetX(event), XConfigureEventGetY(event),
457 XConfigureEventGetWidth(event), XConfigureEventGetHeight(event) );
458 }
e2386592 459
3cd2f0bd 460 if ( tlw && tlw->IsShown() )
77df2fbc 461 {
77df2fbc
RR
462 tlw->SetNeedResizeInIdle();
463 }
464 else
465 {
466 wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() );
467 sizeEvent.SetEventObject( win );
e2386592 468
77df2fbc
RR
469 return win->GetEventHandler()->ProcessEvent( sizeEvent );
470 }
256d631a 471 }
4bcc3647 472 return false;
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())
4bcc3647 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 {
4bcc3647
WS
492 win->Close(false);
493 return true;
b513212d
JS
494 }
495 }
4bcc3647 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 {
4bcc3647
WS
538 win->Close(false);
539 return true;
256d631a 540 }
4bcc3647 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())
4bcc3647 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())
4bcc3647 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)
4bcc3647 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() );
4bcc3647 611 return true;
bcd3832a
VZ
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 }
4bcc3647 623 return false;
bcd3832a 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 }
4bcc3647 639 return false;
bcd3832a 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
4bcc3647 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);
4bcc3647 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())
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
701PangoContext* 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
731WXColormap 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 747Window 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 779void wxApp::Exit()
83df96d6 780{
83df96d6 781 wxApp::CleanUp();
e2478fde
VZ
782
783 wxAppConsole::Exit();
83df96d6
JS
784}
785
786// Yield to other processes
787
788bool 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 846void 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__