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