]> git.saurik.com Git - wxWidgets.git/blame - src/x11/app.cpp
fix when not having precompiled headers
[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{
391 m_topWindow = NULL;
392 wxTheApp = this;
393 m_className = "";
394 m_wantDebugOutput = TRUE ;
395 m_appName = "";
396 argc = 0;
397 argv = NULL;
398 m_exitOnFrameDelete = TRUE;
83df96d6 399 m_mainColormap = (WXColormap) NULL;
7eaac9f5 400 m_topLevelWidget = (WXWindow) NULL;
83df96d6 401 m_maxRequestSize = 0;
1b0fb34b 402 m_mainLoop = NULL;
256d631a
JS
403 m_showIconic = FALSE;
404 m_initialSize = wxDefaultSize;
dc4025af 405
8601b2e1 406#if !wxUSE_NANOX
dc4025af
RR
407 m_visualColormap = NULL;
408 m_colorCube = NULL;
8601b2e1 409#endif
dc4025af
RR
410}
411
412wxApp::~wxApp()
413{
8601b2e1 414#if !wxUSE_NANOX
dc4025af
RR
415 if (m_colorCube)
416 free( m_colorCube );
417
418 if (m_visualColormap)
419 delete [] (XColor*)m_visualColormap;
8601b2e1 420#endif
83df96d6
JS
421}
422
423bool wxApp::Initialized()
424{
425 if (GetTopWindow())
426 return TRUE;
427 else
428 return FALSE;
429}
430
431int wxApp::MainLoop()
432{
c978d361 433 int rt;
1b0fb34b 434 m_mainLoop = new wxEventLoop;
83df96d6 435
1b0fb34b 436 rt = m_mainLoop->Run();
83df96d6 437
1b0fb34b
JS
438 delete m_mainLoop;
439 m_mainLoop = NULL;
440 return rt;
441}
83df96d6 442
70b8ab77 443#if !wxUSE_NANOX
f809133f
RR
444//-----------------------------------------------------------------------
445// X11 predicate function for exposure compression
446//-----------------------------------------------------------------------
447
448struct wxExposeInfo
449{
450 Window window;
451 Bool found_non_matching;
452};
453
454static Bool expose_predicate (Display *display, XEvent *xevent, XPointer arg)
455{
456 wxExposeInfo *info = (wxExposeInfo*) arg;
e2386592 457
f809133f
RR
458 if (info->found_non_matching)
459 return FALSE;
e2386592 460
f809133f
RR
461 if (xevent->xany.type != Expose)
462 {
463 info->found_non_matching = TRUE;
464 return FALSE;
465 }
e2386592 466
f809133f
RR
467 if (xevent->xexpose.window != info->window)
468 {
469 info->found_non_matching = TRUE;
470 return FALSE;
471 }
e2386592 472
f809133f
RR
473 return TRUE;
474}
70b8ab77
JS
475#endif
476 // wxUSE_NANOX
f809133f
RR
477
478//-----------------------------------------------------------------------
086fd560 479// Processes an X event, returning TRUE if the event was processed.
f809133f
RR
480//-----------------------------------------------------------------------
481
086fd560 482bool wxApp::ProcessXEvent(WXEvent* _event)
1b0fb34b
JS
483{
484 XEvent* event = (XEvent*) _event;
83df96d6 485
1b0fb34b 486 wxWindow* win = NULL;
c79a329d 487 Window window = XEventGetWindow(event);
e2386592 488#if 0
1b0fb34b 489 Window actualWindow = window;
e2386592 490#endif
0b5c0e1a 491
1b0fb34b 492 // Find the first wxWindow that corresponds to this event window
774b90fb
JS
493 // Because we're receiving events after a window
494 // has been destroyed, assume a 1:1 match between
495 // Window and wxWindow, so if it's not in the table,
496 // it must have been destroyed.
e2386592 497
774b90fb
JS
498 win = wxGetWindowFromTable(window);
499 if (!win)
ab6b6b15
RR
500 {
501#if wxUSE_TWO_WINDOWS
502 win = wxGetClientWindowFromTable(window);
503 if (!win)
504#endif
505 return FALSE;
506 }
83df96d6 507
0b5c0e1a
JS
508#ifdef __WXDEBUG__
509 wxString windowClass = win->GetClassInfo()->GetClassName();
510#endif
e2386592 511
1b0fb34b
JS
512 switch (event->type)
513 {
2f12683e
RR
514 case Expose:
515 {
8601b2e1 516#if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
f41bc3e3 517 if (event->xexpose.window != (Window)win->GetClientAreaWindow())
ab6b6b15
RR
518 {
519 XEvent tmp_event;
520 wxExposeInfo info;
521 info.window = event->xexpose.window;
522 info.found_non_matching = FALSE;
523 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
524 {
525 // Don't worry about optimizing redrawing the border etc.
526 }
527 win->NeedUpdateNcAreaInIdle();
528 }
529 else
530#endif
531 {
532 win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
533 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
ab6b6b15 534 win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
2f12683e
RR
535 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
536
537#if !wxUSE_NANOX
ab6b6b15
RR
538 XEvent tmp_event;
539 wxExposeInfo info;
540 info.window = event->xexpose.window;
541 info.found_non_matching = FALSE;
542 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
543 {
544 win->GetUpdateRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
545 tmp_event.xexpose.width, tmp_event.xexpose.height );
e2386592 546
ab6b6b15
RR
547 win->GetClearRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
548 tmp_event.xexpose.width, tmp_event.xexpose.height );
549 }
2f12683e 550#endif
c2c0dabf
RR
551
552 // This simplifies the expose and clear areas to simple
553 // rectangles.
554 win->GetUpdateRegion() = win->GetUpdateRegion().GetBox();
555 win->GetClearRegion() = win->GetClearRegion().GetBox();
e2386592 556
c2c0dabf 557 // If we only have one X11 window, always indicate
e2386592 558 // that borders might have to be redrawn.
f41bc3e3 559 if (win->GetMainWindow() == win->GetClientAreaWindow())
ab6b6b15 560 win->NeedUpdateNcAreaInIdle();
2f12683e 561
ab6b6b15
RR
562 // Only erase background, paint in idle time.
563 win->SendEraseEvents();
065722d7 564 // win->Update();
ab6b6b15 565 }
2f12683e
RR
566
567 return TRUE;
568 }
e2386592 569
2f12683e
RR
570#if !wxUSE_NANOX
571 case GraphicsExpose:
572 {
4175e952
RR
573 printf( "GraphicExpose event\n" );
574
58ec2255
JS
575 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win->GetName().c_str(),
576 event->xgraphicsexpose.x, event->xgraphicsexpose.y,
577 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 578
2f12683e
RR
579 win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
580 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 581
2f12683e
RR
582 win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
583 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
e2386592 584
2f12683e
RR
585 if (event->xgraphicsexpose.count == 0)
586 {
587 // Only erase background, paint in idle time.
588 win->SendEraseEvents();
c2c0dabf 589 // win->Update();
2f12683e
RR
590 }
591
592 return TRUE;
593 }
594#endif
595
1b0fb34b 596 case KeyPress:
83df96d6 597 {
9d2cef1c 598 if (!win->IsEnabled())
086fd560 599 return FALSE;
b513212d 600
9d2cef1c
RR
601 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
602 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 603
9d2cef1c 604 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
e2386592 605
120b822d
RR
606 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
607 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
608 return TRUE;
e2386592 609
120b822d
RR
610 keyEvent.SetEventType(wxEVT_CHAR);
611 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
612 return TRUE;
e2386592 613
120b822d
RR
614 if ( (keyEvent.m_keyCode == WXK_TAB) &&
615 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
9d2cef1c 616 {
120b822d
RR
617 wxNavigationKeyEvent new_event;
618 new_event.SetEventObject( win->GetParent() );
619 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
620 new_event.SetDirection( (keyEvent.m_keyCode == WXK_TAB) );
621 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
622 new_event.SetWindowChange( keyEvent.ControlDown() );
623 new_event.SetCurrentFocus( win );
624 return win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1b0fb34b 625 }
120b822d
RR
626
627 return FALSE;
83df96d6 628 }
1b0fb34b 629 case KeyRelease:
7eaac9f5 630 {
9d2cef1c 631 if (!win->IsEnabled())
086fd560 632 return FALSE;
b513212d 633
9d2cef1c
RR
634 wxKeyEvent keyEvent(wxEVT_KEY_UP);
635 wxTranslateKeyEvent(keyEvent, win, window, event);
e2386592 636
086fd560 637 return win->GetEventHandler()->ProcessEvent( keyEvent );
7eaac9f5 638 }
256d631a
JS
639 case ConfigureNotify:
640 {
c79a329d 641#if wxUSE_NANOX
9d2cef1c 642 if (event->update.utype == GR_UPDATE_SIZE)
c79a329d 643#endif
256d631a 644 {
c2c0dabf
RR
645 if (win->IsTopLevel())
646 {
647 wxTopLevelWindow *tlw = (wxTopLevelWindow*) win;
648 tlw->SetConfigureGeometry( XConfigureEventGetX(event), XConfigureEventGetY(event),
649 XConfigureEventGetWidth(event), XConfigureEventGetHeight(event) );
650 }
e2386592 651
2f12683e 652 if (win->IsTopLevel() && win->IsShown())
77df2fbc
RR
653 {
654 wxTopLevelWindowX11 *tlw = (wxTopLevelWindowX11 *) win;
655 tlw->SetNeedResizeInIdle();
656 }
657 else
658 {
659 wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() );
660 sizeEvent.SetEventObject( win );
e2386592 661
77df2fbc
RR
662 return win->GetEventHandler()->ProcessEvent( sizeEvent );
663 }
256d631a 664 }
086fd560 665 return FALSE;
c2ff68d3 666 break;
256d631a
JS
667 }
668#if !wxUSE_NANOX
1b0fb34b 669 case PropertyNotify:
7eaac9f5 670 {
0b5c0e1a 671 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
086fd560 672 return HandlePropertyChange(_event);
7eaac9f5 673 }
b513212d 674 case ClientMessage:
3a0b23eb 675 {
9d2cef1c 676 if (!win->IsEnabled())
086fd560 677 return FALSE;
3a0b23eb 678
7e4501ee
RR
679 Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True);
680 Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True);
b513212d
JS
681
682 if (event->xclient.message_type == wm_protocols)
683 {
6a44bffd 684 if ((Atom) (event->xclient.data.l[0]) == wm_delete_window)
b513212d 685 {
9d2cef1c 686 win->Close(FALSE);
086fd560 687 return TRUE;
b513212d
JS
688 }
689 }
086fd560 690 return FALSE;
b513212d 691 }
2f12683e
RR
692#if 0
693 case DestroyNotify:
694 {
695 printf( "destroy from %s\n", win->GetName().c_str() );
696 break;
697 }
698 case CreateNotify:
699 {
700 printf( "create from %s\n", win->GetName().c_str() );
701 break;
702 }
703 case MapRequest:
704 {
705 printf( "map request from %s\n", win->GetName().c_str() );
706 break;
707 }
1b0fb34b 708 case ResizeRequest:
7eaac9f5 709 {
2f12683e 710 printf( "resize request from %s\n", win->GetName().c_str() );
e2386592 711
7266b672 712 Display *disp = (Display*) wxGetDisplay();
1b0fb34b 713 XEvent report;
e2386592 714
1b0fb34b
JS
715 // to avoid flicker
716 report = * event;
717 while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report));
e2386592 718
b1633d20
RR
719 wxSize sz = win->GetSize();
720 wxSizeEvent sizeEvent(sz, win->GetId());
721 sizeEvent.SetEventObject(win);
1b0fb34b 722
b1633d20 723 return win->GetEventHandler()->ProcessEvent( sizeEvent );
7eaac9f5 724 }
256d631a 725#endif
b1633d20 726#endif
256d631a
JS
727#if wxUSE_NANOX
728 case GR_EVENT_TYPE_CLOSE_REQ:
729 {
730 if (win)
731 {
732 win->Close(FALSE);
086fd560 733 return TRUE;
256d631a 734 }
086fd560 735 return FALSE;
256d631a
JS
736 break;
737 }
c79a329d 738#endif
1b0fb34b
JS
739 case EnterNotify:
740 case LeaveNotify:
741 case ButtonPress:
742 case ButtonRelease:
743 case MotionNotify:
7eaac9f5 744 {
7e4501ee 745 if (!win->IsEnabled())
086fd560 746 return FALSE;
a17a79ba 747
9691c806
RR
748 // Here we check if the top level window is
749 // disabled, which is one aspect of modality.
750 wxWindow *tlw = win;
751 while (tlw && !tlw->IsTopLevel())
752 tlw = tlw->GetParent();
753 if (tlw && !tlw->IsEnabled())
086fd560 754 return FALSE;
e2386592 755
7e4501ee 756 if (event->type == ButtonPress)
1b0fb34b 757 {
7e4501ee 758 if ((win != wxWindow::FindFocus()) && win->AcceptsFocus())
9d2cef1c
RR
759 {
760 // This might actually be done in wxWindow::SetFocus()
ea1ad04b 761 // and not here. TODO.
9d2cef1c
RR
762 g_prevFocus = wxWindow::FindFocus();
763 g_nextFocus = win;
e2386592 764
58ec2255
JS
765 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
766
767 // Record the fact that this window is
768 // getting the focus, because we'll need to
769 // check if its parent is getting a bogus
770 // focus and duly ignore it.
771 // TODO: may need to have this code in SetFocus, too.
772 extern wxWindow* g_GettingFocus;
773 g_GettingFocus = win;
9d2cef1c
RR
774 win->SetFocus();
775 }
1b0fb34b 776 }
e2386592 777
ea1ad04b
RR
778#if !wxUSE_NANOX
779 if (event->type == LeaveNotify || event->type == EnterNotify)
780 {
781 // Throw out NotifyGrab and NotifyUngrab
782 if (event->xcrossing.mode != NotifyNormal)
086fd560 783 return FALSE;
ea1ad04b
RR
784 }
785#endif
7e4501ee
RR
786 wxMouseEvent wxevent;
787 wxTranslateMouseEvent(wxevent, win, window, event);
086fd560 788 return win->GetEventHandler()->ProcessEvent( wxevent );
7eaac9f5 789 }
1b0fb34b
JS
790 case FocusIn:
791 {
256d631a 792#if !wxUSE_NANOX
9d2cef1c
RR
793 if ((event->xfocus.detail != NotifyPointer) &&
794 (event->xfocus.mode == NotifyNormal))
256d631a 795#endif
1b0fb34b 796 {
58ec2255 797 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
4cae9a20 798
58ec2255
JS
799 extern wxWindow* g_GettingFocus;
800 if (g_GettingFocus && g_GettingFocus->GetParent() == win)
801 {
802 // Ignore this, this can be a spurious FocusIn
803 // caused by a child having its focus set.
804 g_GettingFocus = NULL;
805 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
806 return TRUE;
807 }
808 else
809 {
810 wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId());
811 focusEvent.SetEventObject(win);
812 focusEvent.SetWindow( g_prevFocus );
813 g_prevFocus = NULL;
814
815 return win->GetEventHandler()->ProcessEvent(focusEvent);
816 }
1b0fb34b 817 }
086fd560 818 return FALSE;
1b0fb34b
JS
819 break;
820 }
821 case FocusOut:
822 {
256d631a 823#if !wxUSE_NANOX
9d2cef1c
RR
824 if ((event->xfocus.detail != NotifyPointer) &&
825 (event->xfocus.mode == NotifyNormal))
256d631a 826#endif
1b0fb34b 827 {
58ec2255 828 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
e2386592 829
1b0fb34b
JS
830 wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId());
831 focusEvent.SetEventObject(win);
9d2cef1c
RR
832 focusEvent.SetWindow( g_nextFocus );
833 g_nextFocus = NULL;
086fd560 834 return win->GetEventHandler()->ProcessEvent(focusEvent);
1b0fb34b 835 }
086fd560 836 return FALSE;
1b0fb34b
JS
837 break;
838 }
839 default:
840 {
45ff6421
JS
841#ifdef __WXDEBUG__
842 //wxString eventName = wxGetXEventName(XEvent& event);
843 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
844#endif
086fd560 845 return FALSE;
1b0fb34b
JS
846 break;
847 }
83df96d6 848 }
086fd560 849 return FALSE;
83df96d6
JS
850}
851
852// Returns TRUE if more time is needed.
1b0fb34b
JS
853// Note that this duplicates wxEventLoopImpl::SendIdleEvent
854// but ProcessIdle may be needed by apps, so is kept.
83df96d6
JS
855bool wxApp::ProcessIdle()
856{
857 wxIdleEvent event;
858 event.SetEventObject(this);
859 ProcessEvent(event);
860
861 return event.MoreRequested();
862}
863
864void wxApp::ExitMainLoop()
865{
1b0fb34b
JS
866 if (m_mainLoop)
867 m_mainLoop->Exit(0);
83df96d6
JS
868}
869
870// Is a message/event pending?
871bool wxApp::Pending()
872{
1b0fb34b 873 return wxEventLoop::GetActive()->Pending();
83df96d6
JS
874}
875
876// Dispatch a message.
877void wxApp::Dispatch()
878{
1b0fb34b 879 wxEventLoop::GetActive()->Dispatch();
83df96d6
JS
880}
881
882// This should be redefined in a derived class for
883// handling property change events for XAtom IPC.
086fd560 884bool wxApp::HandlePropertyChange(WXEvent *event)
83df96d6
JS
885{
886 // by default do nothing special
7eaac9f5 887 // TODO: what to do for X11
256d631a 888 // XtDispatchEvent((XEvent*) event);
086fd560 889 return FALSE;
83df96d6
JS
890}
891
892void wxApp::OnIdle(wxIdleEvent& event)
893{
0d1dff01 894 static bool s_inOnIdle = FALSE;
83df96d6
JS
895
896 // Avoid recursion (via ProcessEvent default case)
0d1dff01 897 if (s_inOnIdle)
83df96d6
JS
898 return;
899
0d1dff01 900 s_inOnIdle = TRUE;
83df96d6 901
0d1dff01
RR
902 // Resend in the main thread events which have been prepared in other
903 // threads
83df96d6
JS
904 ProcessPendingEvents();
905
0d1dff01 906 // 'Garbage' collection of windows deleted with Close()
83df96d6
JS
907 DeletePendingObjects();
908
83df96d6
JS
909 // Send OnIdle events to all windows
910 bool needMore = SendIdleEvents();
911
912 if (needMore)
913 event.RequestMore(TRUE);
914
0d1dff01 915 s_inOnIdle = FALSE;
83df96d6
JS
916}
917
918void wxWakeUpIdle()
919{
920 // **** please implement me! ****
921 // Wake up the idle handler processor, even if it is in another thread...
922}
923
924
925// Send idle event to all top-level windows
926bool wxApp::SendIdleEvents()
927{
928 bool needMore = FALSE;
929
930 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
931 while (node)
932 {
933 wxWindow* win = node->GetData();
934 if (SendIdleEvents(win))
935 needMore = TRUE;
936 node = node->GetNext();
937 }
938
939 return needMore;
940}
941
942// Send idle event to window and all subwindows
943bool wxApp::SendIdleEvents(wxWindow* win)
944{
945 bool needMore = FALSE;
946
947 wxIdleEvent event;
948 event.SetEventObject(win);
0d1dff01
RR
949
950 win->GetEventHandler()->ProcessEvent(event);
951
83df96d6
JS
952 if (event.MoreRequested())
953 needMore = TRUE;
954
955 wxNode* node = win->GetChildren().First();
956 while (node)
957 {
958 wxWindow* win = (wxWindow*) node->Data();
959 if (SendIdleEvents(win))
960 needMore = TRUE;
961
962 node = node->Next();
963 }
e2386592 964
193e19cf
RR
965 win->OnInternalIdle();
966
0d1dff01 967 return needMore;
83df96d6
JS
968}
969
970void wxApp::DeletePendingObjects()
971{
972 wxNode *node = wxPendingDelete.First();
973 while (node)
974 {
975 wxObject *obj = (wxObject *)node->Data();
976
977 delete obj;
978
979 if (wxPendingDelete.Member(obj))
980 delete node;
981
982 // Deleting one object may have deleted other pending
983 // objects, so start from beginning of list again.
984 node = wxPendingDelete.First();
985 }
986}
987
dc4025af
RR
988static void wxCalcPrecAndShift( unsigned long mask, int *shift, int *prec )
989{
990 *shift = 0;
991 *prec = 0;
992
993 while (!(mask & 0x1))
994 {
995 (*shift)++;
996 mask >>= 1;
997 }
998
999 while (mask & 0x1)
1000 {
1001 (*prec)++;
1002 mask >>= 1;
1003 }
1004}
1005
256d631a 1006// Create display, and other initialization
83df96d6
JS
1007bool wxApp::OnInitGui()
1008{
ca7497c2
JS
1009 // Eventually this line will be removed, but for
1010 // now we don't want to try popping up a dialog
1011 // for error messages.
1012 delete wxLog::SetActiveTarget(new wxLogStderr);
e2386592 1013
ea596687 1014 if (!wxAppBase::OnInitGui())
dc4025af 1015 return FALSE;
e2386592 1016
a11672a4 1017 GetMainColormap( wxApp::GetDisplay() );
256d631a 1018
a11672a4 1019 m_maxRequestSize = XMaxRequestSize( (Display*) wxApp::GetDisplay() );
83df96d6 1020
8601b2e1 1021#if !wxUSE_NANOX
dc4025af
RR
1022 // Get info about the current visual. It is enough
1023 // to do this once here unless we support different
1024 // visuals, displays and screens. Given that wxX11
1025 // mostly for embedded things, that is no real
1026 // limitation.
1027 Display *xdisplay = (Display*) wxApp::GetDisplay();
1028 int xscreen = DefaultScreen(xdisplay);
1029 Visual* xvisual = DefaultVisual(xdisplay,xscreen);
1030 int xdepth = DefaultDepth(xdisplay, xscreen);
1031
1032 XVisualInfo vinfo_template;
1033 vinfo_template.visual = xvisual;
1034 vinfo_template.visualid = XVisualIDFromVisual( xvisual );
1035 vinfo_template.depth = xdepth;
1036
1037 int nitem = 0;
1038 XVisualInfo *vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1039 wxASSERT_MSG( vi, wxT("No visual info") );
1040
1041 m_visualType = vi->visual->c_class;
1042 m_visualScreen = vi->screen;
1043
1044 m_visualRedMask = vi->red_mask;
1045 m_visualGreenMask = vi->green_mask;
1046 m_visualBlueMask = vi->blue_mask;
1047
1048 if (m_visualType != GrayScale && m_visualType != PseudoColor)
1049 {
1050 wxCalcPrecAndShift( m_visualRedMask, &m_visualRedShift, &m_visualRedPrec );
1051 wxCalcPrecAndShift( m_visualGreenMask, &m_visualGreenShift, &m_visualGreenPrec );
1052 wxCalcPrecAndShift( m_visualBlueMask, &m_visualBlueShift, &m_visualBluePrec );
1053 }
1054
1055 m_visualDepth = xdepth;
1056 if (xdepth == 16)
1057 xdepth = m_visualRedPrec + m_visualGreenPrec + m_visualBluePrec;
1058
1059 m_visualColormapSize = vi->colormap_size;
1060
1061 XFree( vi );
1062
1063 if (m_visualDepth > 8)
1064 return TRUE;
1065
1066 m_visualColormap = new XColor[m_visualColormapSize];
1067 XColor* colors = (XColor*) m_visualColormap;
1068
1069 for (int i = 0; i < m_visualColormapSize; i++)
1070 colors[i].pixel = i;
1071
1072 XQueryColors( xdisplay, DefaultColormap(xdisplay,xscreen), colors, m_visualColormapSize );
1073
1074 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
1075
1076 for (int r = 0; r < 32; r++)
1077 {
1078 for (int g = 0; g < 32; g++)
1079 {
1080 for (int b = 0; b < 32; b++)
1081 {
1082 int rr = (r << 3) | (r >> 2);
1083 int gg = (g << 3) | (g >> 2);
1084 int bb = (b << 3) | (b >> 2);
1085
1086 int index = -1;
1087
1088 if (colors)
1089 {
1090 int max = 3 * 65536;
1091
1092 for (int i = 0; i < m_visualColormapSize; i++)
1093 {
1094 int rdiff = ((rr << 8) - colors[i].red);
1095 int gdiff = ((gg << 8) - colors[i].green);
1096 int bdiff = ((bb << 8) - colors[i].blue);
1097 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1098 if (sum < max)
1099 {
1100 index = i; max = sum;
1101 }
1102 }
1103 }
1104 else
1105 {
1106 // assume 8-bit true or static colors. this really exists
1107 index = (r >> (5 - m_visualRedPrec)) << m_visualRedShift;
1108 index |= (g >> (5 - m_visualGreenPrec)) << m_visualGreenShift;
1109 index |= (b >> (5 - m_visualBluePrec)) << m_visualBlueShift;
1110 }
1111 m_colorCube[ (r*1024) + (g*32) + b ] = index;
1112 }
1113 }
1114 }
8601b2e1 1115#endif
dc4025af 1116
83df96d6
JS
1117 return TRUE;
1118}
1119
1120WXColormap wxApp::GetMainColormap(WXDisplay* display)
1121{
1122 if (!display) /* Must be called first with non-NULL display */
1123 return m_mainColormap;
1124
1125 int defaultScreen = DefaultScreen((Display*) display);
1126 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
1127
1128 Colormap c = DefaultColormapOfScreen(screen);
1129
1130 if (!m_mainColormap)
1131 m_mainColormap = (WXColormap) c;
1132
1133 return (WXColormap) c;
1134}
1135
8354aa92 1136Window wxGetWindowParent(Window window)
7eaac9f5 1137{
86fd8bda 1138 wxASSERT_MSG( window, "invalid window" );
e2386592 1139
86fd8bda
RR
1140 return (Window) 0;
1141
7eaac9f5 1142 Window parent, root = 0;
c79a329d
JS
1143#if wxUSE_NANOX
1144 int noChildren = 0;
1145#else
7eaac9f5 1146 unsigned int noChildren = 0;
c79a329d 1147#endif
ea596687 1148 Window* children = NULL;
c79a329d 1149
ee351013 1150 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
c79a329d
JS
1151 int res = 1;
1152#if !wxUSE_NANOX
1153 res =
1154#endif
1155 XQueryTree((Display*) wxGetDisplay(), window, & root, & parent,
ee351013 1156 & children, & noChildren);
ea596687
JS
1157 if (children)
1158 XFree(children);
1159 if (res)
7eaac9f5
JS
1160 return parent;
1161 else
1162 return (Window) 0;
1163}
1164
83df96d6
JS
1165void wxExit()
1166{
1167 int retValue = 0;
1168 if (wxTheApp)
1169 retValue = wxTheApp->OnExit();
1170
1171 wxApp::CleanUp();
1172 /*
1173 * Exit in some platform-specific way. Not recommended that the app calls this:
1174 * only for emergencies.
1175 */
1176 exit(retValue);
1177}
1178
1179// Yield to other processes
1180
1181bool wxApp::Yield(bool onlyIfNeeded)
1182{
1183 bool s_inYield = FALSE;
1184
1185 if ( s_inYield )
1186 {
1187 if ( !onlyIfNeeded )
1188 {
1189 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1190 }
1191
1192 return FALSE;
1193 }
1194
1195 s_inYield = TRUE;
1196
df0e1b64
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 }
1206
83df96d6
JS
1207 while (wxTheApp && wxTheApp->Pending())
1208 wxTheApp->Dispatch();
1209
868741e9
JS
1210#if wxUSE_TIMER
1211 wxTimer::NotifyTimers();
1212#endif
1213 ProcessIdle();
1214
df0e1b64
JS
1215 if (newEventLoop)
1216 {
1217 wxEventLoop::SetActive(NULL);
1218 delete newEventLoop;
1219 }
1220
83df96d6
JS
1221 s_inYield = FALSE;
1222
1223 return TRUE;
1224}
1225
d715d419
VZ
1226#ifdef __WXDEBUG__
1227
5968a0dc 1228void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
7edcafa4
JS
1229{
1230 // While the GUI isn't working that well, just print out the
1231 // message.
193e19cf 1232#if 1
5968a0dc 1233 wxAppBase::OnAssert(file, line, cond, msg);
7edcafa4
JS
1234#else
1235 wxString msg2;
1236 msg2.Printf("At file %s:%d: %s", file, line, msg);
1237 wxLogDebug(msg2);
1238#endif
1239}
1240
d715d419
VZ
1241#endif // __WXDEBUG__
1242