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