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