]> git.saurik.com Git - wxWidgets.git/blame - src/x11/app.cpp
Integrated fixes from latest UNIX version.
[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"
83df96d6 29
ef8c973b
VS
30#include "wx/univ/theme.h"
31#include "wx/univ/renderer.h"
32
dc4025af
RR
33#define ABS(a) (((a) < 0) ? -(a) : (a))
34
83df96d6
JS
35#if wxUSE_THREADS
36 #include "wx/thread.h"
37#endif
38
39#if wxUSE_WX_RESOURCES
40 #include "wx/resource.h"
41#endif
42
7eaac9f5 43#include "wx/x11/private.h"
83df96d6
JS
44
45#include <string.h>
46
9d2cef1c
RR
47//------------------------------------------------------------------------
48// global data
49//------------------------------------------------------------------------
50
83df96d6
JS
51extern wxList wxPendingDelete;
52
9d2cef1c 53wxHashTable *wxWidgetHashTable = NULL;
ab6b6b15 54wxHashTable *wxClientWidgetHashTable = NULL;
9d2cef1c 55
83df96d6
JS
56wxApp *wxTheApp = NULL;
57
9d2cef1c
RR
58// This is set within wxEntryStart -- too early on
59// to put these in wxTheApp
60static int g_newArgc = 0;
61static wxChar** g_newArgv = NULL;
62static bool g_showIconic = FALSE;
63static wxSize g_initialSize = wxDefaultSize;
83df96d6 64
9d2cef1c
RR
65// This is required for wxFocusEvent::SetWindow(). It will only
66// work for focus events which we provoke ourselves (by calling
67// SetFocus()). It will not work for those events, which X11
68// generates itself.
69static wxWindow *g_nextFocus = NULL;
70static wxWindow *g_prevFocus = NULL;
83df96d6 71
9d2cef1c
RR
72//------------------------------------------------------------------------
73// X11 error handling
74//------------------------------------------------------------------------
83df96d6
JS
75
76#ifdef __WXDEBUG__
1b0fb34b 77typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
83df96d6 78
1b0fb34b 79XErrorHandlerFunc gs_pfnXErrorHandler = 0;
83df96d6 80
1b0fb34b
JS
81static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
82{
83 // just forward to the default handler for now
c79a329d
JS
84 if (gs_pfnXErrorHandler)
85 return gs_pfnXErrorHandler(dpy, xevent);
86 else
87 return 0;
1b0fb34b 88}
83df96d6
JS
89#endif // __WXDEBUG__
90
9d2cef1c
RR
91//------------------------------------------------------------------------
92// wxApp
93//------------------------------------------------------------------------
94
83df96d6 95long wxApp::sm_lastMessageTime = 0;
a11672a4 96WXDisplay *wxApp::ms_display = NULL;
83df96d6 97
9d2cef1c
RR
98IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
99
100BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
101 EVT_IDLE(wxApp::OnIdle)
102END_EVENT_TABLE()
256d631a 103
83df96d6
JS
104bool wxApp::Initialize()
105{
83df96d6
JS
106 wxClassInfo::InitializeClasses();
107
0598e263
JS
108#if wxUSE_INTL
109 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
110#endif
111
83df96d6
JS
112 // GL: I'm annoyed ... I don't know where to put this and I don't want to
113 // create a module for that as it's part of the core.
114#if wxUSE_THREADS
115 wxPendingEventsLocker = new wxCriticalSection();
116#endif
117
118 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
119 wxTheColourDatabase->Initialize();
120
121 wxInitializeStockLists();
122 wxInitializeStockObjects();
123
124#if wxUSE_WX_RESOURCES
125 wxInitializeResourceSystem();
126#endif
127
83df96d6 128 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
ab6b6b15 129 wxClientWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
83df96d6
JS
130
131 wxModule::RegisterModules();
132 if (!wxModule::InitializeModules()) return FALSE;
133
134 return TRUE;
135}
136
137void wxApp::CleanUp()
138{
256d631a
JS
139 if (g_newArgv)
140 delete[] g_newArgv;
141 g_newArgv = NULL;
142
83df96d6
JS
143 delete wxWidgetHashTable;
144 wxWidgetHashTable = NULL;
ab6b6b15
RR
145 delete wxClientWidgetHashTable;
146 wxClientWidgetHashTable = NULL;
83df96d6
JS
147
148 wxModule::CleanUpModules();
149
150#if wxUSE_WX_RESOURCES
151 wxCleanUpResourceSystem();
152#endif
153
83df96d6
JS
154 delete wxTheColourDatabase;
155 wxTheColourDatabase = NULL;
156
a11672a4 157 wxDeleteStockObjects();
e2386592 158
a11672a4
RR
159 wxDeleteStockLists();
160
161 delete wxTheApp;
162 wxTheApp = NULL;
83df96d6 163
83df96d6
JS
164 wxClassInfo::CleanUpClasses();
165
83df96d6
JS
166#if wxUSE_THREADS
167 delete wxPendingEvents;
168 delete wxPendingEventsLocker;
169#endif
170
171#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
172 // At this point we want to check if there are any memory
173 // blocks that aren't part of the wxDebugContext itself,
174 // as a special case. Then when dumping we need to ignore
175 // wxDebugContext, too.
176 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
177 {
ba696cfa 178 wxLogDebug("There were memory leaks.");
83df96d6
JS
179 wxDebugContext::Dump();
180 wxDebugContext::PrintStatistics();
181 }
182#endif
183
184 // do it as the very last thing because everything else can log messages
185 wxLog::DontCreateOnDemand();
186 // do it as the very last thing because everything else can log messages
187 delete wxLog::SetActiveTarget(NULL);
188}
189
a11672a4
RR
190// NB: argc and argv may be changed here, pass by reference!
191int wxEntryStart( int& argc, char *argv[] )
192{
193#ifdef __WXDEBUG__
ff6b424a 194#if !wxUSE_NANOX
a11672a4
RR
195 // install the X error handler
196 gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler );
ff6b424a 197#endif
a11672a4
RR
198#endif // __WXDEBUG__
199
256d631a
JS
200 wxString displayName;
201 bool syncDisplay = FALSE;
202
3a0b23eb 203 // Parse the arguments.
256d631a
JS
204 // We can't use wxCmdLineParser or OnInitCmdLine and friends because
205 // we have to create the Display earlier. If we can find a way to
206 // use the wxAppBase API then I'll be quite happy to change it.
207 g_newArgv = new wxChar*[argc];
208 g_newArgc = 0;
209 int i;
210 for (i = 0; i < argc; i++)
211 {
212 wxString arg(argv[i]);
213 if (arg == wxT("-display"))
214 {
215 if (i < (argc - 1))
216 {
217 i ++;
218 displayName = argv[i];
219 continue;
220 }
221 }
222 else if (arg == wxT("-geometry"))
223 {
224 if (i < (argc - 1))
225 {
226 i ++;
eb90fb3e 227 wxString windowGeometry = argv[i];
256d631a
JS
228 int w, h;
229 if (wxSscanf(windowGeometry.c_str(), _T("%dx%d"), &w, &h) != 2)
230 {
eb90fb3e 231 wxLogError(_("Invalid geometry specification '%s'"), windowGeometry.c_str());
256d631a
JS
232 }
233 else
234 {
235 g_initialSize = wxSize(w, h);
236 }
237 continue;
238 }
239 }
240 else if (arg == wxT("-sync"))
241 {
242 syncDisplay = TRUE;
243 continue;
244 }
245 else if (arg == wxT("-iconic"))
246 {
247 g_showIconic = TRUE;
45ff6421 248
256d631a
JS
249 continue;
250 }
251
252 // Not eaten by wxWindows, so pass through
253 g_newArgv[g_newArgc] = argv[i];
254 g_newArgc ++;
255 }
256
c79a329d 257 Display* xdisplay = NULL;
256d631a
JS
258 if (displayName.IsEmpty())
259 xdisplay = XOpenDisplay(NULL);
260 else
461e93f9 261 xdisplay = XOpenDisplay((char*) displayName.c_str());
a11672a4
RR
262
263 if (!xdisplay)
264 {
265 wxLogError( _("wxWindows could not open display. Exiting.") );
266 return -1;
267 }
256d631a
JS
268
269 if (syncDisplay)
270 {
271 XSynchronize(xdisplay, True);
272 }
e2386592 273
a11672a4 274 wxApp::ms_display = (WXDisplay*) xdisplay;
e2386592 275
a11672a4 276 XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask);
e2386592 277
7e4501ee 278 wxSetDetectableAutoRepeat( TRUE );
a11672a4
RR
279
280 if (!wxApp::Initialize())
281 return -1;
282
283 return 0;
284}
285
a11672a4
RR
286int wxEntryInitGui()
287{
288 int retValue = 0;
289
290 if ( !wxTheApp->OnInitGui() )
291 retValue = -1;
292
293 return retValue;
294}
295
296
83df96d6
JS
297int wxEntry( int argc, char *argv[] )
298{
299#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
300 // This seems to be necessary since there are 'rogue'
301 // objects present at this point (perhaps global objects?)
302 // Setting a checkpoint will ignore them as far as the
303 // memory checking facility is concerned.
304 // Of course you may argue that memory allocated in globals should be
305 // checked, but this is a reasonable compromise.
306 wxDebugContext::SetCheckpoint();
307#endif
a11672a4
RR
308 int err = wxEntryStart(argc, argv);
309 if (err)
310 return err;
83df96d6
JS
311
312 if (!wxTheApp)
313 {
314 if (!wxApp::GetInitializerFunction())
315 {
316 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
317 return 0;
318 };
319
320 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
321 };
322
323 if (!wxTheApp)
324 {
325 printf( "wxWindows error: wxTheApp == NULL\n" );
326 return 0;
327 };
328
329 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
330 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
331
45ff6421
JS
332 // The command line may have been changed
333 // by stripping out -display etc.
334 if (g_newArgc > 0)
335 {
336 wxTheApp->argc = g_newArgc;
337 wxTheApp->argv = g_newArgv;
338 }
339 else
340 {
341 wxTheApp->argc = argc;
342 wxTheApp->argv = argv;
343 }
256d631a
JS
344 wxTheApp->m_showIconic = g_showIconic;
345 wxTheApp->m_initialSize = g_initialSize;
83df96d6 346
a11672a4
RR
347 int retValue;
348 retValue = wxEntryInitGui();
83df96d6
JS
349
350 // Here frames insert themselves automatically into wxTopLevelWindows by
351 // getting created in OnInit().
a11672a4
RR
352 if ( retValue == 0 )
353 {
354 if ( !wxTheApp->OnInit() )
355 retValue = -1;
356 }
83df96d6 357
a11672a4 358 if ( retValue == 0 )
83df96d6
JS
359 {
360 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
361 }
362
363 // flush the logged messages if any
364 wxLog *pLog = wxLog::GetActiveTarget();
365 if ( pLog != NULL && pLog->HasPendingMessages() )
366 pLog->Flush();
367
368 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
369 // for further messages
370
371 if (wxTheApp->GetTopWindow())
372 {
373 delete wxTheApp->GetTopWindow();
374 wxTheApp->SetTopWindow(NULL);
375 }
376
377 wxTheApp->DeletePendingObjects();
378
379 wxTheApp->OnExit();
380
381 wxApp::CleanUp();
382
383 return retValue;
384};
385
386// Static member initialization
387wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
388
389wxApp::wxApp()
390{
00344dd0 391 // TODO: parse the command line
83df96d6
JS
392 argc = 0;
393 argv = NULL;
00344dd0 394
83df96d6 395 m_mainColormap = (WXColormap) NULL;
7eaac9f5 396 m_topLevelWidget = (WXWindow) NULL;
83df96d6 397 m_maxRequestSize = 0;
1b0fb34b 398 m_mainLoop = NULL;
256d631a
JS
399 m_showIconic = FALSE;
400 m_initialSize = wxDefaultSize;
366e8ae6 401
8601b2e1 402#if !wxUSE_NANOX
dc4025af
RR
403 m_visualColormap = NULL;
404 m_colorCube = NULL;
8601b2e1 405#endif
dc4025af
RR
406}
407
408wxApp::~wxApp()
409{
8601b2e1 410#if !wxUSE_NANOX
dc4025af
RR
411 if (m_colorCube)
412 free( m_colorCube );
366e8ae6 413
dc4025af
RR
414 if (m_visualColormap)
415 delete [] (XColor*)m_visualColormap;
8601b2e1 416#endif
83df96d6
JS
417}
418
419bool wxApp::Initialized()
420{
421 if (GetTopWindow())
422 return TRUE;
423 else
424 return FALSE;
425}
426
427int wxApp::MainLoop()
428{
c978d361 429 int rt;
1b0fb34b 430 m_mainLoop = new wxEventLoop;
83df96d6 431
1b0fb34b 432 rt = m_mainLoop->Run();
83df96d6 433
1b0fb34b
JS
434 delete m_mainLoop;
435 m_mainLoop = NULL;
436 return rt;
437}
83df96d6 438
70b8ab77 439#if !wxUSE_NANOX
f809133f
RR
440//-----------------------------------------------------------------------
441// X11 predicate function for exposure compression
442//-----------------------------------------------------------------------
443
444struct wxExposeInfo
445{
446 Window window;
447 Bool found_non_matching;
448};
449
450static Bool expose_predicate (Display *display, XEvent *xevent, XPointer arg)
451{
452 wxExposeInfo *info = (wxExposeInfo*) arg;
e2386592 453
f809133f
RR
454 if (info->found_non_matching)
455 return FALSE;
e2386592 456
f809133f
RR
457 if (xevent->xany.type != Expose)
458 {
459 info->found_non_matching = TRUE;
460 return FALSE;
461 }
e2386592 462
f809133f
RR
463 if (xevent->xexpose.window != info->window)
464 {
465 info->found_non_matching = TRUE;
466 return FALSE;
467 }
e2386592 468
f809133f
RR
469 return TRUE;
470}
70b8ab77
JS
471#endif
472 // wxUSE_NANOX
f809133f
RR
473
474//-----------------------------------------------------------------------
086fd560 475// Processes an X event, returning TRUE if the event was processed.
f809133f
RR
476//-----------------------------------------------------------------------
477
086fd560 478bool wxApp::ProcessXEvent(WXEvent* _event)
1b0fb34b
JS
479{
480 XEvent* event = (XEvent*) _event;
83df96d6 481
1b0fb34b 482 wxWindow* win = NULL;
c79a329d 483 Window window = XEventGetWindow(event);
e2386592 484#if 0
1b0fb34b 485 Window actualWindow = window;
e2386592 486#endif
0b5c0e1a 487
1b0fb34b 488 // Find the first wxWindow that corresponds to this event window
774b90fb
JS
489 // Because we're receiving events after a window
490 // has been destroyed, assume a 1:1 match between
491 // Window and wxWindow, so if it's not in the table,
492 // it must have been destroyed.
e2386592 493
774b90fb
JS
494 win = wxGetWindowFromTable(window);
495 if (!win)
ab6b6b15
RR
496 {
497#if wxUSE_TWO_WINDOWS
498 win = wxGetClientWindowFromTable(window);
499 if (!win)
500#endif
501 return FALSE;
502 }
83df96d6 503
0b5c0e1a
JS
504#ifdef __WXDEBUG__
505 wxString windowClass = win->GetClassInfo()->GetClassName();
506#endif
e2386592 507
1b0fb34b
JS
508 switch (event->type)
509 {
2f12683e
RR
510 case Expose:
511 {
8601b2e1 512#if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
f41bc3e3 513 if (event->xexpose.window != (Window)win->GetClientAreaWindow())
ab6b6b15
RR
514 {
515 XEvent tmp_event;
516 wxExposeInfo info;
517 info.window = event->xexpose.window;
518 info.found_non_matching = FALSE;
519 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
520 {
521 // Don't worry about optimizing redrawing the border etc.
522 }
523 win->NeedUpdateNcAreaInIdle();
524 }
525 else
526#endif
527 {
528 win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
529 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
ab6b6b15 530 win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
2f12683e
RR
531 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
532
533#if !wxUSE_NANOX
ab6b6b15
RR
534 XEvent tmp_event;
535 wxExposeInfo info;
536 info.window = event->xexpose.window;
537 info.found_non_matching = FALSE;
538 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
539 {
540 win->GetUpdateRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
541 tmp_event.xexpose.width, tmp_event.xexpose.height );
e2386592 542
ab6b6b15
RR
543 win->GetClearRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
544 tmp_event.xexpose.width, tmp_event.xexpose.height );
545 }
2f12683e 546#endif
c2c0dabf
RR
547
548 // This simplifies the expose and clear areas to simple
549 // rectangles.
550 win->GetUpdateRegion() = win->GetUpdateRegion().GetBox();
551 win->GetClearRegion() = win->GetClearRegion().GetBox();
e2386592 552
c2c0dabf 553 // If we only have one X11 window, always indicate
e2386592 554 // that borders might have to be redrawn.
f41bc3e3 555 if (win->GetMainWindow() == win->GetClientAreaWindow())
ab6b6b15 556 win->NeedUpdateNcAreaInIdle();
2f12683e 557
ab6b6b15
RR
558 // Only erase background, paint in idle time.
559 win->SendEraseEvents();
065722d7 560 // win->Update();
ab6b6b15 561 }
2f12683e
RR
562
563 return TRUE;
564 }
e2386592 565
2f12683e
RR
566#if !wxUSE_NANOX
567 case GraphicsExpose:
568 {
4175e952
RR
569 printf( "GraphicExpose event\n" );
570
89cd4125 571 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win->GetName().c_str());
e2386592 572
2f12683e
RR
573 win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
574 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 575
2f12683e
RR
576 win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
577 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 578
2f12683e
RR
579 if (event->xgraphicsexpose.count == 0)
580 {
581 // Only erase background, paint in idle time.
582 win->SendEraseEvents();
c2c0dabf 583 // win->Update();
2f12683e
RR
584 }
585
586 return TRUE;
587 }
588#endif
589
1b0fb34b 590 case KeyPress:
83df96d6 591 {
9d2cef1c 592 if (!win->IsEnabled())
086fd560 593 return FALSE;
b513212d 594
9d2cef1c
RR
595 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
596 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 597
9d2cef1c 598 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
e2386592 599
120b822d
RR
600 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
601 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
602 return TRUE;
e2386592 603
120b822d
RR
604 keyEvent.SetEventType(wxEVT_CHAR);
605 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
606 return TRUE;
e2386592 607
120b822d
RR
608 if ( (keyEvent.m_keyCode == WXK_TAB) &&
609 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
9d2cef1c 610 {
120b822d
RR
611 wxNavigationKeyEvent new_event;
612 new_event.SetEventObject( win->GetParent() );
613 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
614 new_event.SetDirection( (keyEvent.m_keyCode == WXK_TAB) );
615 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
616 new_event.SetWindowChange( keyEvent.ControlDown() );
617 new_event.SetCurrentFocus( win );
618 return win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1b0fb34b 619 }
120b822d
RR
620
621 return FALSE;
83df96d6 622 }
1b0fb34b 623 case KeyRelease:
7eaac9f5 624 {
9d2cef1c 625 if (!win->IsEnabled())
086fd560 626 return FALSE;
b513212d 627
9d2cef1c
RR
628 wxKeyEvent keyEvent(wxEVT_KEY_UP);
629 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 630
086fd560 631 return win->GetEventHandler()->ProcessEvent( keyEvent );
7eaac9f5 632 }
256d631a
JS
633 case ConfigureNotify:
634 {
c79a329d 635#if wxUSE_NANOX
9d2cef1c 636 if (event->update.utype == GR_UPDATE_SIZE)
c79a329d 637#endif
256d631a 638 {
c2c0dabf
RR
639 if (win->IsTopLevel())
640 {
641 wxTopLevelWindow *tlw = (wxTopLevelWindow*) win;
642 tlw->SetConfigureGeometry( XConfigureEventGetX(event), XConfigureEventGetY(event),
643 XConfigureEventGetWidth(event), XConfigureEventGetHeight(event) );
644 }
e2386592 645
2f12683e 646 if (win->IsTopLevel() && win->IsShown())
77df2fbc
RR
647 {
648 wxTopLevelWindowX11 *tlw = (wxTopLevelWindowX11 *) win;
649 tlw->SetNeedResizeInIdle();
650 }
651 else
652 {
653 wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() );
654 sizeEvent.SetEventObject( win );
e2386592 655
77df2fbc
RR
656 return win->GetEventHandler()->ProcessEvent( sizeEvent );
657 }
256d631a 658 }
086fd560 659 return FALSE;
c2ff68d3 660 break;
256d631a
JS
661 }
662#if !wxUSE_NANOX
1b0fb34b 663 case PropertyNotify:
7eaac9f5 664 {
0b5c0e1a 665 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
086fd560 666 return HandlePropertyChange(_event);
7eaac9f5 667 }
b513212d 668 case ClientMessage:
3a0b23eb 669 {
9d2cef1c 670 if (!win->IsEnabled())
086fd560 671 return FALSE;
3a0b23eb 672
7e4501ee
RR
673 Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True);
674 Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True);
b513212d
JS
675
676 if (event->xclient.message_type == wm_protocols)
677 {
6a44bffd 678 if ((Atom) (event->xclient.data.l[0]) == wm_delete_window)
b513212d 679 {
9d2cef1c 680 win->Close(FALSE);
086fd560 681 return TRUE;
b513212d
JS
682 }
683 }
086fd560 684 return FALSE;
b513212d 685 }
2f12683e
RR
686#if 0
687 case DestroyNotify:
688 {
689 printf( "destroy from %s\n", win->GetName().c_str() );
690 break;
691 }
692 case CreateNotify:
693 {
694 printf( "create from %s\n", win->GetName().c_str() );
695 break;
696 }
697 case MapRequest:
698 {
699 printf( "map request from %s\n", win->GetName().c_str() );
700 break;
701 }
1b0fb34b 702 case ResizeRequest:
7eaac9f5 703 {
2f12683e 704 printf( "resize request from %s\n", win->GetName().c_str() );
e2386592 705
7266b672 706 Display *disp = (Display*) wxGetDisplay();
1b0fb34b 707 XEvent report;
e2386592 708
1b0fb34b
JS
709 // to avoid flicker
710 report = * event;
711 while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report));
e2386592 712
b1633d20
RR
713 wxSize sz = win->GetSize();
714 wxSizeEvent sizeEvent(sz, win->GetId());
715 sizeEvent.SetEventObject(win);
1b0fb34b 716
b1633d20 717 return win->GetEventHandler()->ProcessEvent( sizeEvent );
7eaac9f5 718 }
256d631a 719#endif
b1633d20 720#endif
256d631a
JS
721#if wxUSE_NANOX
722 case GR_EVENT_TYPE_CLOSE_REQ:
723 {
724 if (win)
725 {
726 win->Close(FALSE);
086fd560 727 return TRUE;
256d631a 728 }
086fd560 729 return FALSE;
256d631a
JS
730 break;
731 }
c79a329d 732#endif
1b0fb34b
JS
733 case EnterNotify:
734 case LeaveNotify:
735 case ButtonPress:
736 case ButtonRelease:
737 case MotionNotify:
7eaac9f5 738 {
7e4501ee 739 if (!win->IsEnabled())
086fd560 740 return FALSE;
366e8ae6 741
9691c806
RR
742 // Here we check if the top level window is
743 // disabled, which is one aspect of modality.
744 wxWindow *tlw = win;
745 while (tlw && !tlw->IsTopLevel())
746 tlw = tlw->GetParent();
747 if (tlw && !tlw->IsEnabled())
086fd560 748 return FALSE;
e2386592 749
7e4501ee 750 if (event->type == ButtonPress)
1b0fb34b 751 {
7e4501ee 752 if ((win != wxWindow::FindFocus()) && win->AcceptsFocus())
9d2cef1c
RR
753 {
754 // This might actually be done in wxWindow::SetFocus()
ea1ad04b 755 // and not here. TODO.
9d2cef1c
RR
756 g_prevFocus = wxWindow::FindFocus();
757 g_nextFocus = win;
e2386592 758
58ec2255
JS
759 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
760
761 // Record the fact that this window is
762 // getting the focus, because we'll need to
763 // check if its parent is getting a bogus
764 // focus and duly ignore it.
765 // TODO: may need to have this code in SetFocus, too.
766 extern wxWindow* g_GettingFocus;
767 g_GettingFocus = win;
9d2cef1c
RR
768 win->SetFocus();
769 }
1b0fb34b 770 }
e2386592 771
ea1ad04b
RR
772#if !wxUSE_NANOX
773 if (event->type == LeaveNotify || event->type == EnterNotify)
774 {
775 // Throw out NotifyGrab and NotifyUngrab
776 if (event->xcrossing.mode != NotifyNormal)
086fd560 777 return FALSE;
ea1ad04b
RR
778 }
779#endif
7e4501ee
RR
780 wxMouseEvent wxevent;
781 wxTranslateMouseEvent(wxevent, win, window, event);
086fd560 782 return win->GetEventHandler()->ProcessEvent( wxevent );
7eaac9f5 783 }
1b0fb34b
JS
784 case FocusIn:
785 {
256d631a 786#if !wxUSE_NANOX
9d2cef1c
RR
787 if ((event->xfocus.detail != NotifyPointer) &&
788 (event->xfocus.mode == NotifyNormal))
256d631a 789#endif
1b0fb34b 790 {
58ec2255 791 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
366e8ae6 792
58ec2255
JS
793 extern wxWindow* g_GettingFocus;
794 if (g_GettingFocus && g_GettingFocus->GetParent() == win)
795 {
796 // Ignore this, this can be a spurious FocusIn
797 // caused by a child having its focus set.
798 g_GettingFocus = NULL;
799 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
800 return TRUE;
801 }
802 else
803 {
804 wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId());
805 focusEvent.SetEventObject(win);
806 focusEvent.SetWindow( g_prevFocus );
807 g_prevFocus = NULL;
808
809 return win->GetEventHandler()->ProcessEvent(focusEvent);
810 }
1b0fb34b 811 }
086fd560 812 return FALSE;
1b0fb34b
JS
813 break;
814 }
815 case FocusOut:
816 {
256d631a 817#if !wxUSE_NANOX
9d2cef1c
RR
818 if ((event->xfocus.detail != NotifyPointer) &&
819 (event->xfocus.mode == NotifyNormal))
256d631a 820#endif
1b0fb34b 821 {
58ec2255 822 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
e2386592 823
1b0fb34b
JS
824 wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId());
825 focusEvent.SetEventObject(win);
9d2cef1c
RR
826 focusEvent.SetWindow( g_nextFocus );
827 g_nextFocus = NULL;
086fd560 828 return win->GetEventHandler()->ProcessEvent(focusEvent);
1b0fb34b 829 }
086fd560 830 return FALSE;
1b0fb34b
JS
831 break;
832 }
833 default:
834 {
45ff6421
JS
835#ifdef __WXDEBUG__
836 //wxString eventName = wxGetXEventName(XEvent& event);
837 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
838#endif
086fd560 839 return FALSE;
1b0fb34b
JS
840 break;
841 }
83df96d6 842 }
086fd560 843 return FALSE;
83df96d6
JS
844}
845
846// Returns TRUE if more time is needed.
1b0fb34b
JS
847// Note that this duplicates wxEventLoopImpl::SendIdleEvent
848// but ProcessIdle may be needed by apps, so is kept.
83df96d6
JS
849bool wxApp::ProcessIdle()
850{
851 wxIdleEvent event;
852 event.SetEventObject(this);
853 ProcessEvent(event);
854
855 return event.MoreRequested();
856}
857
858void wxApp::ExitMainLoop()
859{
1b0fb34b
JS
860 if (m_mainLoop)
861 m_mainLoop->Exit(0);
83df96d6
JS
862}
863
864// Is a message/event pending?
865bool wxApp::Pending()
866{
1b0fb34b 867 return wxEventLoop::GetActive()->Pending();
83df96d6
JS
868}
869
870// Dispatch a message.
871void wxApp::Dispatch()
872{
1b0fb34b 873 wxEventLoop::GetActive()->Dispatch();
83df96d6
JS
874}
875
876// This should be redefined in a derived class for
877// handling property change events for XAtom IPC.
086fd560 878bool wxApp::HandlePropertyChange(WXEvent *event)
83df96d6
JS
879{
880 // by default do nothing special
7eaac9f5 881 // TODO: what to do for X11
256d631a 882 // XtDispatchEvent((XEvent*) event);
086fd560 883 return FALSE;
83df96d6
JS
884}
885
886void wxApp::OnIdle(wxIdleEvent& event)
887{
0d1dff01 888 static bool s_inOnIdle = FALSE;
83df96d6
JS
889
890 // Avoid recursion (via ProcessEvent default case)
0d1dff01 891 if (s_inOnIdle)
83df96d6
JS
892 return;
893
0d1dff01 894 s_inOnIdle = TRUE;
83df96d6 895
0d1dff01
RR
896 // Resend in the main thread events which have been prepared in other
897 // threads
83df96d6
JS
898 ProcessPendingEvents();
899
0d1dff01 900 // 'Garbage' collection of windows deleted with Close()
83df96d6
JS
901 DeletePendingObjects();
902
83df96d6
JS
903 // Send OnIdle events to all windows
904 bool needMore = SendIdleEvents();
905
906 if (needMore)
907 event.RequestMore(TRUE);
908
0d1dff01 909 s_inOnIdle = FALSE;
83df96d6
JS
910}
911
912void wxWakeUpIdle()
913{
914 // **** please implement me! ****
915 // Wake up the idle handler processor, even if it is in another thread...
916}
917
918
919// Send idle event to all top-level windows
920bool wxApp::SendIdleEvents()
921{
922 bool needMore = FALSE;
923
924 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
925 while (node)
926 {
927 wxWindow* win = node->GetData();
928 if (SendIdleEvents(win))
929 needMore = TRUE;
930 node = node->GetNext();
931 }
932
933 return needMore;
934}
935
936// Send idle event to window and all subwindows
937bool wxApp::SendIdleEvents(wxWindow* win)
938{
939 bool needMore = FALSE;
940
941 wxIdleEvent event;
942 event.SetEventObject(win);
0d1dff01
RR
943
944 win->GetEventHandler()->ProcessEvent(event);
945
83df96d6
JS
946 if (event.MoreRequested())
947 needMore = TRUE;
948
949 wxNode* node = win->GetChildren().First();
950 while (node)
951 {
952 wxWindow* win = (wxWindow*) node->Data();
953 if (SendIdleEvents(win))
954 needMore = TRUE;
955
956 node = node->Next();
957 }
e2386592 958
193e19cf
RR
959 win->OnInternalIdle();
960
0d1dff01 961 return needMore;
83df96d6
JS
962}
963
964void wxApp::DeletePendingObjects()
965{
966 wxNode *node = wxPendingDelete.First();
967 while (node)
968 {
969 wxObject *obj = (wxObject *)node->Data();
970
971 delete obj;
972
973 if (wxPendingDelete.Member(obj))
974 delete node;
975
976 // Deleting one object may have deleted other pending
977 // objects, so start from beginning of list again.
978 node = wxPendingDelete.First();
979 }
980}
981
dc4025af
RR
982static void wxCalcPrecAndShift( unsigned long mask, int *shift, int *prec )
983{
984 *shift = 0;
985 *prec = 0;
986
987 while (!(mask & 0x1))
988 {
989 (*shift)++;
990 mask >>= 1;
991 }
992
993 while (mask & 0x1)
994 {
995 (*prec)++;
996 mask >>= 1;
997 }
998}
999
256d631a 1000// Create display, and other initialization
83df96d6
JS
1001bool wxApp::OnInitGui()
1002{
ca7497c2
JS
1003 // Eventually this line will be removed, but for
1004 // now we don't want to try popping up a dialog
1005 // for error messages.
1006 delete wxLog::SetActiveTarget(new wxLogStderr);
e2386592 1007
ea596687 1008 if (!wxAppBase::OnInitGui())
dc4025af 1009 return FALSE;
e2386592 1010
a11672a4 1011 GetMainColormap( wxApp::GetDisplay() );
256d631a 1012
a11672a4 1013 m_maxRequestSize = XMaxRequestSize( (Display*) wxApp::GetDisplay() );
83df96d6 1014
8601b2e1 1015#if !wxUSE_NANOX
dc4025af
RR
1016 // Get info about the current visual. It is enough
1017 // to do this once here unless we support different
1018 // visuals, displays and screens. Given that wxX11
1019 // mostly for embedded things, that is no real
1020 // limitation.
1021 Display *xdisplay = (Display*) wxApp::GetDisplay();
1022 int xscreen = DefaultScreen(xdisplay);
1023 Visual* xvisual = DefaultVisual(xdisplay,xscreen);
1024 int xdepth = DefaultDepth(xdisplay, xscreen);
1025
1026 XVisualInfo vinfo_template;
1027 vinfo_template.visual = xvisual;
1028 vinfo_template.visualid = XVisualIDFromVisual( xvisual );
1029 vinfo_template.depth = xdepth;
366e8ae6 1030
dc4025af
RR
1031 int nitem = 0;
1032 XVisualInfo *vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1033 wxASSERT_MSG( vi, wxT("No visual info") );
366e8ae6 1034
dc4025af
RR
1035 m_visualType = vi->visual->c_class;
1036 m_visualScreen = vi->screen;
366e8ae6 1037
dc4025af
RR
1038 m_visualRedMask = vi->red_mask;
1039 m_visualGreenMask = vi->green_mask;
1040 m_visualBlueMask = vi->blue_mask;
366e8ae6 1041
dc4025af
RR
1042 if (m_visualType != GrayScale && m_visualType != PseudoColor)
1043 {
1044 wxCalcPrecAndShift( m_visualRedMask, &m_visualRedShift, &m_visualRedPrec );
1045 wxCalcPrecAndShift( m_visualGreenMask, &m_visualGreenShift, &m_visualGreenPrec );
1046 wxCalcPrecAndShift( m_visualBlueMask, &m_visualBlueShift, &m_visualBluePrec );
1047 }
366e8ae6 1048
dc4025af
RR
1049 m_visualDepth = xdepth;
1050 if (xdepth == 16)
1051 xdepth = m_visualRedPrec + m_visualGreenPrec + m_visualBluePrec;
366e8ae6 1052
dc4025af 1053 m_visualColormapSize = vi->colormap_size;
366e8ae6 1054
dc4025af 1055 XFree( vi );
366e8ae6 1056
dc4025af
RR
1057 if (m_visualDepth > 8)
1058 return TRUE;
366e8ae6 1059
dc4025af
RR
1060 m_visualColormap = new XColor[m_visualColormapSize];
1061 XColor* colors = (XColor*) m_visualColormap;
366e8ae6 1062
dc4025af
RR
1063 for (int i = 0; i < m_visualColormapSize; i++)
1064 colors[i].pixel = i;
1065
1066 XQueryColors( xdisplay, DefaultColormap(xdisplay,xscreen), colors, m_visualColormapSize );
366e8ae6 1067
dc4025af
RR
1068 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
1069
1070 for (int r = 0; r < 32; r++)
1071 {
1072 for (int g = 0; g < 32; g++)
1073 {
1074 for (int b = 0; b < 32; b++)
1075 {
1076 int rr = (r << 3) | (r >> 2);
1077 int gg = (g << 3) | (g >> 2);
1078 int bb = (b << 3) | (b >> 2);
1079
1080 int index = -1;
1081
1082 if (colors)
1083 {
1084 int max = 3 * 65536;
1085
1086 for (int i = 0; i < m_visualColormapSize; i++)
1087 {
1088 int rdiff = ((rr << 8) - colors[i].red);
1089 int gdiff = ((gg << 8) - colors[i].green);
1090 int bdiff = ((bb << 8) - colors[i].blue);
1091 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1092 if (sum < max)
1093 {
1094 index = i; max = sum;
1095 }
1096 }
1097 }
1098 else
1099 {
1100 // assume 8-bit true or static colors. this really exists
1101 index = (r >> (5 - m_visualRedPrec)) << m_visualRedShift;
1102 index |= (g >> (5 - m_visualGreenPrec)) << m_visualGreenShift;
1103 index |= (b >> (5 - m_visualBluePrec)) << m_visualBlueShift;
1104 }
1105 m_colorCube[ (r*1024) + (g*32) + b ] = index;
1106 }
1107 }
1108 }
8601b2e1 1109#endif
366e8ae6 1110
83df96d6
JS
1111 return TRUE;
1112}
1113
1114WXColormap wxApp::GetMainColormap(WXDisplay* display)
1115{
1116 if (!display) /* Must be called first with non-NULL display */
1117 return m_mainColormap;
1118
1119 int defaultScreen = DefaultScreen((Display*) display);
1120 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
1121
1122 Colormap c = DefaultColormapOfScreen(screen);
1123
1124 if (!m_mainColormap)
1125 m_mainColormap = (WXColormap) c;
1126
1127 return (WXColormap) c;
1128}
1129
8354aa92 1130Window wxGetWindowParent(Window window)
7eaac9f5 1131{
86fd8bda 1132 wxASSERT_MSG( window, "invalid window" );
e2386592 1133
86fd8bda
RR
1134 return (Window) 0;
1135
7eaac9f5 1136 Window parent, root = 0;
c79a329d
JS
1137#if wxUSE_NANOX
1138 int noChildren = 0;
1139#else
7eaac9f5 1140 unsigned int noChildren = 0;
c79a329d 1141#endif
ea596687 1142 Window* children = NULL;
c79a329d 1143
ee351013 1144 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
c79a329d
JS
1145 int res = 1;
1146#if !wxUSE_NANOX
1147 res =
1148#endif
1149 XQueryTree((Display*) wxGetDisplay(), window, & root, & parent,
ee351013 1150 & children, & noChildren);
ea596687
JS
1151 if (children)
1152 XFree(children);
1153 if (res)
7eaac9f5
JS
1154 return parent;
1155 else
1156 return (Window) 0;
1157}
1158
83df96d6
JS
1159void wxExit()
1160{
1161 int retValue = 0;
1162 if (wxTheApp)
1163 retValue = wxTheApp->OnExit();
1164
1165 wxApp::CleanUp();
1166 /*
1167 * Exit in some platform-specific way. Not recommended that the app calls this:
1168 * only for emergencies.
1169 */
1170 exit(retValue);
1171}
1172
1173// Yield to other processes
1174
1175bool wxApp::Yield(bool onlyIfNeeded)
1176{
bbcd408a
JS
1177 // Sometimes only 2 yields seem
1178 // to do the trick, e.g. in the
1179 // progress dialog
1180 int i;
1181 for (i = 0; i < 2; i++)
83df96d6 1182 {
bbcd408a
JS
1183 bool s_inYield = FALSE;
1184
1185 if ( s_inYield )
83df96d6 1186 {
bbcd408a
JS
1187 if ( !onlyIfNeeded )
1188 {
1189 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1190 }
83df96d6 1191
bbcd408a
JS
1192 return FALSE;
1193 }
83df96d6 1194
bbcd408a 1195 s_inYield = TRUE;
83df96d6 1196
bbcd408a
JS
1197 // Make sure we have an event loop object,
1198 // or Pending/Dispatch will fail
1199 wxEventLoop* eventLoop = wxEventLoop::GetActive();
1200 wxEventLoop* newEventLoop = NULL;
1201 if (!eventLoop)
1202 {
1203 newEventLoop = new wxEventLoop;
1204 wxEventLoop::SetActive(newEventLoop);
1205 }
df0e1b64 1206
bbcd408a
JS
1207 while (wxTheApp && wxTheApp->Pending())
1208 wxTheApp->Dispatch();
83df96d6 1209
868741e9 1210#if wxUSE_TIMER
bbcd408a 1211 wxTimer::NotifyTimers();
868741e9 1212#endif
bbcd408a 1213 ProcessIdle();
868741e9 1214
bbcd408a
JS
1215 if (newEventLoop)
1216 {
1217 wxEventLoop::SetActive(NULL);
1218 delete newEventLoop;
1219 }
df0e1b64 1220
bbcd408a
JS
1221 s_inYield = FALSE;
1222 }
83df96d6
JS
1223
1224 return TRUE;
1225}
1226
d715d419
VZ
1227#ifdef __WXDEBUG__
1228
5968a0dc 1229void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
7edcafa4
JS
1230{
1231 // While the GUI isn't working that well, just print out the
1232 // message.
193e19cf 1233#if 1
5968a0dc 1234 wxAppBase::OnAssert(file, line, cond, msg);
7edcafa4
JS
1235#else
1236 wxString msg2;
1237 msg2.Printf("At file %s:%d: %s", file, line, msg);
1238 wxLogDebug(msg2);
1239#endif
1240}
1241
d715d419
VZ
1242#endif // __WXDEBUG__
1243