]> git.saurik.com Git - wxWidgets.git/blame - src/x11/app.cpp
Minor fixes to buffered stream in connection
[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"
83df96d6 28
ef8c973b
VS
29#include "wx/univ/theme.h"
30#include "wx/univ/renderer.h"
31
83df96d6
JS
32#if wxUSE_THREADS
33 #include "wx/thread.h"
34#endif
35
36#if wxUSE_WX_RESOURCES
37 #include "wx/resource.h"
38#endif
39
40#ifdef __VMS__
41#pragma message disable nosimpint
42#endif
83df96d6
JS
43#include <X11/Xlib.h>
44#include <X11/Xutil.h>
83df96d6 45#include <X11/Xatom.h>
256d631a 46
83df96d6
JS
47#ifdef __VMS__
48#pragma message enable nosimpint
49#endif
50
7eaac9f5 51#include "wx/x11/private.h"
83df96d6
JS
52
53#include <string.h>
54
83df96d6
JS
55extern wxList wxPendingDelete;
56
57wxApp *wxTheApp = NULL;
58
59wxHashTable *wxWidgetHashTable = NULL;
60
61IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
62
63BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
64 EVT_IDLE(wxApp::OnIdle)
65END_EVENT_TABLE()
66
67#ifdef __WXDEBUG__
1b0fb34b 68typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
83df96d6 69
1b0fb34b 70XErrorHandlerFunc gs_pfnXErrorHandler = 0;
83df96d6 71
1b0fb34b
JS
72static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
73{
74 // just forward to the default handler for now
c79a329d
JS
75 if (gs_pfnXErrorHandler)
76 return gs_pfnXErrorHandler(dpy, xevent);
77 else
78 return 0;
1b0fb34b 79}
83df96d6
JS
80#endif // __WXDEBUG__
81
82long wxApp::sm_lastMessageTime = 0;
a11672a4 83WXDisplay *wxApp::ms_display = NULL;
83df96d6 84
256d631a
JS
85// This is set within wxEntryStart -- too early on
86// to put these in wxTheApp
87static int g_newArgc = 0;
88static wxChar** g_newArgv = NULL;
89static bool g_showIconic = FALSE;
90static wxSize g_initialSize = wxDefaultSize;
91
83df96d6
JS
92bool wxApp::Initialize()
93{
83df96d6
JS
94 wxClassInfo::InitializeClasses();
95
96 // GL: I'm annoyed ... I don't know where to put this and I don't want to
97 // create a module for that as it's part of the core.
98#if wxUSE_THREADS
99 wxPendingEventsLocker = new wxCriticalSection();
100#endif
101
102 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
103 wxTheColourDatabase->Initialize();
104
105 wxInitializeStockLists();
106 wxInitializeStockObjects();
107
108#if wxUSE_WX_RESOURCES
109 wxInitializeResourceSystem();
110#endif
111
83df96d6
JS
112 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
113
114 wxModule::RegisterModules();
115 if (!wxModule::InitializeModules()) return FALSE;
116
117 return TRUE;
118}
119
120void wxApp::CleanUp()
121{
256d631a
JS
122 if (g_newArgv)
123 delete[] g_newArgv;
124 g_newArgv = NULL;
125
83df96d6
JS
126 delete wxWidgetHashTable;
127 wxWidgetHashTable = NULL;
128
129 wxModule::CleanUpModules();
130
131#if wxUSE_WX_RESOURCES
132 wxCleanUpResourceSystem();
133#endif
134
83df96d6
JS
135 delete wxTheColourDatabase;
136 wxTheColourDatabase = NULL;
137
a11672a4
RR
138 wxDeleteStockObjects();
139
140 wxDeleteStockLists();
141
142 delete wxTheApp;
143 wxTheApp = NULL;
83df96d6 144
83df96d6
JS
145 wxClassInfo::CleanUpClasses();
146
83df96d6
JS
147#if wxUSE_THREADS
148 delete wxPendingEvents;
149 delete wxPendingEventsLocker;
150#endif
151
152#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
153 // At this point we want to check if there are any memory
154 // blocks that aren't part of the wxDebugContext itself,
155 // as a special case. Then when dumping we need to ignore
156 // wxDebugContext, too.
157 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
158 {
ba696cfa 159 wxLogDebug("There were memory leaks.");
83df96d6
JS
160 wxDebugContext::Dump();
161 wxDebugContext::PrintStatistics();
162 }
163#endif
164
165 // do it as the very last thing because everything else can log messages
166 wxLog::DontCreateOnDemand();
167 // do it as the very last thing because everything else can log messages
168 delete wxLog::SetActiveTarget(NULL);
169}
170
a11672a4
RR
171// NB: argc and argv may be changed here, pass by reference!
172int wxEntryStart( int& argc, char *argv[] )
173{
174#ifdef __WXDEBUG__
175 // install the X error handler
176 gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler );
177#endif // __WXDEBUG__
178
256d631a
JS
179 wxString displayName;
180 bool syncDisplay = FALSE;
181
3a0b23eb 182 // Parse the arguments.
256d631a
JS
183 // We can't use wxCmdLineParser or OnInitCmdLine and friends because
184 // we have to create the Display earlier. If we can find a way to
185 // use the wxAppBase API then I'll be quite happy to change it.
186 g_newArgv = new wxChar*[argc];
187 g_newArgc = 0;
188 int i;
189 for (i = 0; i < argc; i++)
190 {
191 wxString arg(argv[i]);
192 if (arg == wxT("-display"))
193 {
194 if (i < (argc - 1))
195 {
196 i ++;
197 displayName = argv[i];
198 continue;
199 }
200 }
201 else if (arg == wxT("-geometry"))
202 {
203 if (i < (argc - 1))
204 {
205 i ++;
eb90fb3e 206 wxString windowGeometry = argv[i];
256d631a
JS
207 int w, h;
208 if (wxSscanf(windowGeometry.c_str(), _T("%dx%d"), &w, &h) != 2)
209 {
eb90fb3e 210 wxLogError(_("Invalid geometry specification '%s'"), windowGeometry.c_str());
256d631a
JS
211 }
212 else
213 {
214 g_initialSize = wxSize(w, h);
215 }
216 continue;
217 }
218 }
219 else if (arg == wxT("-sync"))
220 {
221 syncDisplay = TRUE;
222 continue;
223 }
224 else if (arg == wxT("-iconic"))
225 {
226 g_showIconic = TRUE;
45ff6421 227
256d631a
JS
228 continue;
229 }
230
231 // Not eaten by wxWindows, so pass through
232 g_newArgv[g_newArgc] = argv[i];
233 g_newArgc ++;
234 }
235
c79a329d 236 Display* xdisplay = NULL;
256d631a
JS
237 if (displayName.IsEmpty())
238 xdisplay = XOpenDisplay(NULL);
239 else
c79a329d 240 xdisplay = XOpenDisplay(displayName.c_str());
a11672a4
RR
241
242 if (!xdisplay)
243 {
244 wxLogError( _("wxWindows could not open display. Exiting.") );
245 return -1;
246 }
256d631a
JS
247
248 if (syncDisplay)
249 {
250 XSynchronize(xdisplay, True);
251 }
a11672a4
RR
252
253 wxApp::ms_display = (WXDisplay*) xdisplay;
254
255 XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask);
256
7e4501ee 257 wxSetDetectableAutoRepeat( TRUE );
a11672a4
RR
258
259 if (!wxApp::Initialize())
260 return -1;
261
262 return 0;
263}
264
a11672a4
RR
265int wxEntryInitGui()
266{
267 int retValue = 0;
268
269 if ( !wxTheApp->OnInitGui() )
270 retValue = -1;
271
272 return retValue;
273}
274
275
83df96d6
JS
276int wxEntry( int argc, char *argv[] )
277{
278#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
279 // This seems to be necessary since there are 'rogue'
280 // objects present at this point (perhaps global objects?)
281 // Setting a checkpoint will ignore them as far as the
282 // memory checking facility is concerned.
283 // Of course you may argue that memory allocated in globals should be
284 // checked, but this is a reasonable compromise.
285 wxDebugContext::SetCheckpoint();
286#endif
a11672a4
RR
287 int err = wxEntryStart(argc, argv);
288 if (err)
289 return err;
83df96d6
JS
290
291 if (!wxTheApp)
292 {
293 if (!wxApp::GetInitializerFunction())
294 {
295 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
296 return 0;
297 };
298
299 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
300 };
301
302 if (!wxTheApp)
303 {
304 printf( "wxWindows error: wxTheApp == NULL\n" );
305 return 0;
306 };
307
308 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
309 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
310
45ff6421
JS
311 // The command line may have been changed
312 // by stripping out -display etc.
313 if (g_newArgc > 0)
314 {
315 wxTheApp->argc = g_newArgc;
316 wxTheApp->argv = g_newArgv;
317 }
318 else
319 {
320 wxTheApp->argc = argc;
321 wxTheApp->argv = argv;
322 }
256d631a
JS
323 wxTheApp->m_showIconic = g_showIconic;
324 wxTheApp->m_initialSize = g_initialSize;
83df96d6 325
a11672a4
RR
326 int retValue;
327 retValue = wxEntryInitGui();
83df96d6
JS
328
329 // Here frames insert themselves automatically into wxTopLevelWindows by
330 // getting created in OnInit().
a11672a4
RR
331 if ( retValue == 0 )
332 {
333 if ( !wxTheApp->OnInit() )
334 retValue = -1;
335 }
83df96d6 336
a11672a4 337 if ( retValue == 0 )
83df96d6
JS
338 {
339 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
340 }
341
342 // flush the logged messages if any
343 wxLog *pLog = wxLog::GetActiveTarget();
344 if ( pLog != NULL && pLog->HasPendingMessages() )
345 pLog->Flush();
346
347 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
348 // for further messages
349
350 if (wxTheApp->GetTopWindow())
351 {
352 delete wxTheApp->GetTopWindow();
353 wxTheApp->SetTopWindow(NULL);
354 }
355
356 wxTheApp->DeletePendingObjects();
357
358 wxTheApp->OnExit();
359
360 wxApp::CleanUp();
361
362 return retValue;
363};
364
365// Static member initialization
366wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
367
368wxApp::wxApp()
369{
370 m_topWindow = NULL;
371 wxTheApp = this;
372 m_className = "";
373 m_wantDebugOutput = TRUE ;
374 m_appName = "";
375 argc = 0;
376 argv = NULL;
377 m_exitOnFrameDelete = TRUE;
83df96d6 378 m_mainColormap = (WXColormap) NULL;
7eaac9f5 379 m_topLevelWidget = (WXWindow) NULL;
83df96d6 380 m_maxRequestSize = 0;
1b0fb34b 381 m_mainLoop = NULL;
256d631a
JS
382 m_showIconic = FALSE;
383 m_initialSize = wxDefaultSize;
83df96d6
JS
384}
385
386bool wxApp::Initialized()
387{
388 if (GetTopWindow())
389 return TRUE;
390 else
391 return FALSE;
392}
393
394int wxApp::MainLoop()
395{
1b0fb34b
JS
396 int rt;
397 m_mainLoop = new wxEventLoop;
83df96d6 398
1b0fb34b 399 rt = m_mainLoop->Run();
83df96d6 400
1b0fb34b
JS
401 delete m_mainLoop;
402 m_mainLoop = NULL;
403 return rt;
404}
83df96d6 405
1b0fb34b
JS
406// Processes an X event.
407void wxApp::ProcessXEvent(WXEvent* _event)
408{
409 XEvent* event = (XEvent*) _event;
83df96d6 410
1b0fb34b 411 wxWindow* win = NULL;
c79a329d 412 Window window = XEventGetWindow(event);
1b0fb34b 413 Window actualWindow = window;
4125131b 414
1b0fb34b 415 // Find the first wxWindow that corresponds to this event window
774b90fb
JS
416 // Because we're receiving events after a window
417 // has been destroyed, assume a 1:1 match between
418 // Window and wxWindow, so if it's not in the table,
419 // it must have been destroyed.
420
421 win = wxGetWindowFromTable(window);
422 if (!win)
45ff6421 423 return;
83df96d6 424
1b0fb34b
JS
425 switch (event->type)
426 {
427 case KeyPress:
83df96d6 428 {
b513212d
JS
429 if (win && !win->IsEnabled())
430 return;
431
1b0fb34b
JS
432 {
433 if (win)
434 {
435 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
7266b672 436 wxTranslateKeyEvent(keyEvent, win, window, event);
ba696cfa
RR
437
438 wxLogDebug( "OnKey from %s", win->GetName().c_str() );
1b0fb34b
JS
439
440 // We didn't process wxEVT_KEY_DOWN, so send
7266b672 441 // wxEVT_CHAR
b513212d 442 if (!win->GetEventHandler()->ProcessEvent( keyEvent ))
1b0fb34b 443 {
7266b672 444 keyEvent.SetEventType(wxEVT_CHAR);
b513212d 445 win->GetEventHandler()->ProcessEvent( keyEvent );
1b0fb34b
JS
446 }
447
448 // We intercepted and processed the key down event
449 return;
450 }
451 }
452 return;
83df96d6 453 }
1b0fb34b 454 case KeyRelease:
7eaac9f5 455 {
b513212d
JS
456 if (win && !win->IsEnabled())
457 return;
458
1b0fb34b
JS
459 if (win)
460 {
461 wxKeyEvent keyEvent(wxEVT_KEY_UP);
462 wxTranslateKeyEvent(keyEvent, win, window, event);
463
b513212d 464 win->GetEventHandler()->ProcessEvent( keyEvent );
1b0fb34b 465 }
83df96d6 466 return;
7eaac9f5 467 }
256d631a
JS
468 case ConfigureNotify:
469 {
c79a329d
JS
470 if (win
471#if wxUSE_NANOX
472 && (event->update.utype == GR_UPDATE_SIZE)
473#endif
474 )
256d631a 475 {
c79a329d 476 wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() );
256d631a
JS
477 sizeEvent.SetEventObject( win );
478
479 win->GetEventHandler()->ProcessEvent( sizeEvent );
480 }
481 }
482#if !wxUSE_NANOX
1b0fb34b 483 case PropertyNotify:
7eaac9f5 484 {
1b0fb34b 485 HandlePropertyChange(_event);
83df96d6 486 return;
7eaac9f5 487 }
b513212d 488 case ClientMessage:
3a0b23eb
JS
489 {
490 if (win && !win->IsEnabled())
491 return;
492
7e4501ee
RR
493 Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True);
494 Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True);
b513212d
JS
495
496 if (event->xclient.message_type == wm_protocols)
497 {
6a44bffd 498 if ((Atom) (event->xclient.data.l[0]) == wm_delete_window)
b513212d
JS
499 {
500 if (win)
501 {
502 win->Close(FALSE);
503 }
504 }
505 }
506 return;
507 }
1b0fb34b 508 case ResizeRequest:
7eaac9f5 509 {
256d631a 510 /*
1b0fb34b
JS
511 * If resize event, don't resize until the last resize event for this
512 * window is recieved. Prevents flicker as windows are resized.
513 */
514
7266b672 515 Display *disp = (Display*) wxGetDisplay();
1b0fb34b
JS
516 XEvent report;
517
518 // to avoid flicker
519 report = * event;
520 while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report));
521
1b0fb34b
JS
522 if (win)
523 {
524 wxSize sz = win->GetSize();
525 wxSizeEvent sizeEvent(sz, win->GetId());
526 sizeEvent.SetEventObject(win);
527
b513212d 528 win->GetEventHandler()->ProcessEvent( sizeEvent );
1b0fb34b
JS
529 }
530
7eaac9f5
JS
531 return;
532 }
256d631a
JS
533#endif
534#if wxUSE_NANOX
535 case GR_EVENT_TYPE_CLOSE_REQ:
536 {
537 if (win)
538 {
539 win->Close(FALSE);
540 }
541 break;
542 }
543#endif
1b0fb34b 544 case Expose:
7eaac9f5 545 {
1b0fb34b
JS
546 if (win)
547 {
c79a329d
JS
548 win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
549 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
550
551 win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
552 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
ba696cfa 553
788519c6 554#if !wxUSE_NANOX
887dd52f 555 if (event->xexpose.count == 0)
788519c6 556#endif
887dd52f
RR
557 {
558 // Only erase background, paint in idle time.
559 win->SendEraseEvents();
560 }
1b0fb34b
JS
561 }
562
7eaac9f5
JS
563 return;
564 }
c79a329d 565#if !wxUSE_NANOX
4125131b
RR
566 case GraphicsExpose:
567 {
568 if (win)
569 {
7e4501ee
RR
570 // wxLogDebug( "GraphicsExpose from %s", win->GetName().c_str(),
571 // event->xgraphicsexpose.x, event->xgraphicsexpose.y,
572 // event->xgraphicsexpose.width, event->xgraphicsexpose.height);
4125131b
RR
573
574 win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
575 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
576
577 win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
578 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
579
887dd52f
RR
580 if (event->xgraphicsexpose.count == 0)
581 {
582 // Only erase background, paint in idle time.
583 win->SendEraseEvents();
584 }
4125131b
RR
585 }
586
587 return;
588 }
c79a329d 589#endif
1b0fb34b
JS
590 case EnterNotify:
591 case LeaveNotify:
592 case ButtonPress:
593 case ButtonRelease:
594 case MotionNotify:
7eaac9f5 595 {
7e4501ee 596 if (!win)
b513212d 597 return;
7e4501ee
RR
598
599 if (!win->IsEnabled())
600 return;
601
602#if 1
603 if (event->type == ButtonPress)
1b0fb34b 604 {
7e4501ee
RR
605 if ((win != wxWindow::FindFocus()) && win->AcceptsFocus())
606 win->SetFocus();
1b0fb34b 607 }
7e4501ee
RR
608#endif
609
610 wxMouseEvent wxevent;
611 wxTranslateMouseEvent(wxevent, win, window, event);
612 win->GetEventHandler()->ProcessEvent( wxevent );
7eaac9f5
JS
613 return;
614 }
1b0fb34b
JS
615 case FocusIn:
616 {
256d631a 617#if !wxUSE_NANOX
1b0fb34b 618 if (win && event->xfocus.detail != NotifyPointer)
256d631a 619#endif
1b0fb34b 620 {
ba696cfa
RR
621 wxLogDebug( "FocusIn from %s", win->GetName().c_str() );
622
1b0fb34b
JS
623 wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId());
624 focusEvent.SetEventObject(win);
b513212d 625 win->GetEventHandler()->ProcessEvent(focusEvent);
1b0fb34b
JS
626 }
627 break;
628 }
629 case FocusOut:
630 {
256d631a 631#if !wxUSE_NANOX
1b0fb34b 632 if (win && event->xfocus.detail != NotifyPointer)
256d631a 633#endif
1b0fb34b 634 {
4125131b 635 wxLogDebug( "FocusOut from %s", win->GetName().c_str() );
ba696cfa 636
1b0fb34b
JS
637 wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId());
638 focusEvent.SetEventObject(win);
b513212d 639 win->GetEventHandler()->ProcessEvent(focusEvent);
1b0fb34b
JS
640 }
641 break;
642 }
c79a329d
JS
643#ifndef wxUSE_NANOX
644 case DestroyNotify:
b513212d
JS
645 {
646 // Do we want to process this (for top-level windows)?
647 // But we want to be able to veto closes, anyway
648 break;
649 }
c79a329d 650#endif
1b0fb34b
JS
651 default:
652 {
45ff6421
JS
653#ifdef __WXDEBUG__
654 //wxString eventName = wxGetXEventName(XEvent& event);
655 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
656#endif
1b0fb34b
JS
657 break;
658 }
83df96d6
JS
659 }
660}
661
662// Returns TRUE if more time is needed.
1b0fb34b
JS
663// Note that this duplicates wxEventLoopImpl::SendIdleEvent
664// but ProcessIdle may be needed by apps, so is kept.
83df96d6
JS
665bool wxApp::ProcessIdle()
666{
667 wxIdleEvent event;
668 event.SetEventObject(this);
669 ProcessEvent(event);
670
671 return event.MoreRequested();
672}
673
674void wxApp::ExitMainLoop()
675{
1b0fb34b
JS
676 if (m_mainLoop)
677 m_mainLoop->Exit(0);
83df96d6
JS
678}
679
680// Is a message/event pending?
681bool wxApp::Pending()
682{
1b0fb34b 683 return wxEventLoop::GetActive()->Pending();
83df96d6
JS
684}
685
686// Dispatch a message.
687void wxApp::Dispatch()
688{
1b0fb34b 689 wxEventLoop::GetActive()->Dispatch();
83df96d6
JS
690}
691
692// This should be redefined in a derived class for
693// handling property change events for XAtom IPC.
694void wxApp::HandlePropertyChange(WXEvent *event)
695{
696 // by default do nothing special
7eaac9f5 697 // TODO: what to do for X11
256d631a 698 // XtDispatchEvent((XEvent*) event);
83df96d6
JS
699}
700
701void wxApp::OnIdle(wxIdleEvent& event)
702{
0d1dff01 703 static bool s_inOnIdle = FALSE;
83df96d6
JS
704
705 // Avoid recursion (via ProcessEvent default case)
0d1dff01 706 if (s_inOnIdle)
83df96d6
JS
707 return;
708
0d1dff01 709 s_inOnIdle = TRUE;
83df96d6 710
0d1dff01
RR
711 // Resend in the main thread events which have been prepared in other
712 // threads
83df96d6
JS
713 ProcessPendingEvents();
714
0d1dff01 715 // 'Garbage' collection of windows deleted with Close()
83df96d6
JS
716 DeletePendingObjects();
717
83df96d6
JS
718 // Send OnIdle events to all windows
719 bool needMore = SendIdleEvents();
720
721 if (needMore)
722 event.RequestMore(TRUE);
723
0d1dff01 724 s_inOnIdle = FALSE;
83df96d6
JS
725}
726
727void wxWakeUpIdle()
728{
729 // **** please implement me! ****
730 // Wake up the idle handler processor, even if it is in another thread...
731}
732
733
734// Send idle event to all top-level windows
735bool wxApp::SendIdleEvents()
736{
737 bool needMore = FALSE;
738
739 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
740 while (node)
741 {
742 wxWindow* win = node->GetData();
743 if (SendIdleEvents(win))
744 needMore = TRUE;
745 node = node->GetNext();
746 }
747
748 return needMore;
749}
750
751// Send idle event to window and all subwindows
752bool wxApp::SendIdleEvents(wxWindow* win)
753{
754 bool needMore = FALSE;
755
756 wxIdleEvent event;
757 event.SetEventObject(win);
0d1dff01
RR
758
759 win->GetEventHandler()->ProcessEvent(event);
760
761 win->OnInternalIdle();
83df96d6
JS
762
763 if (event.MoreRequested())
764 needMore = TRUE;
765
766 wxNode* node = win->GetChildren().First();
767 while (node)
768 {
769 wxWindow* win = (wxWindow*) node->Data();
770 if (SendIdleEvents(win))
771 needMore = TRUE;
772
773 node = node->Next();
774 }
0d1dff01
RR
775
776 return needMore;
83df96d6
JS
777}
778
779void wxApp::DeletePendingObjects()
780{
781 wxNode *node = wxPendingDelete.First();
782 while (node)
783 {
784 wxObject *obj = (wxObject *)node->Data();
785
786 delete obj;
787
788 if (wxPendingDelete.Member(obj))
789 delete node;
790
791 // Deleting one object may have deleted other pending
792 // objects, so start from beginning of list again.
793 node = wxPendingDelete.First();
794 }
795}
796
256d631a 797// Create display, and other initialization
83df96d6
JS
798bool wxApp::OnInitGui()
799{
ca7497c2
JS
800 // Eventually this line will be removed, but for
801 // now we don't want to try popping up a dialog
802 // for error messages.
803 delete wxLog::SetActiveTarget(new wxLogStderr);
a11672a4 804
ea596687
JS
805 if (!wxAppBase::OnInitGui())
806 return FALSE;
807
a11672a4 808 GetMainColormap( wxApp::GetDisplay() );
256d631a 809
a11672a4 810 m_maxRequestSize = XMaxRequestSize( (Display*) wxApp::GetDisplay() );
83df96d6
JS
811
812 return TRUE;
813}
814
815WXColormap wxApp::GetMainColormap(WXDisplay* display)
816{
817 if (!display) /* Must be called first with non-NULL display */
818 return m_mainColormap;
819
820 int defaultScreen = DefaultScreen((Display*) display);
821 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
822
823 Colormap c = DefaultColormapOfScreen(screen);
824
825 if (!m_mainColormap)
826 m_mainColormap = (WXColormap) c;
827
828 return (WXColormap) c;
829}
830
8354aa92 831Window wxGetWindowParent(Window window)
7eaac9f5 832{
86fd8bda
RR
833 wxASSERT_MSG( window, "invalid window" );
834
835 return (Window) 0;
836
7eaac9f5 837 Window parent, root = 0;
c79a329d
JS
838#if wxUSE_NANOX
839 int noChildren = 0;
840#else
7eaac9f5 841 unsigned int noChildren = 0;
c79a329d 842#endif
ea596687 843 Window* children = NULL;
c79a329d
JS
844
845 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
846 int res = 1;
847#if !wxUSE_NANOX
848 res =
849#endif
850 XQueryTree((Display*) wxGetDisplay(), window, & root, & parent,
ea596687
JS
851 & children, & noChildren);
852 if (children)
853 XFree(children);
854 if (res)
7eaac9f5
JS
855 return parent;
856 else
857 return (Window) 0;
858}
859
83df96d6
JS
860void wxExit()
861{
862 int retValue = 0;
863 if (wxTheApp)
864 retValue = wxTheApp->OnExit();
865
866 wxApp::CleanUp();
867 /*
868 * Exit in some platform-specific way. Not recommended that the app calls this:
869 * only for emergencies.
870 */
871 exit(retValue);
872}
873
874// Yield to other processes
875
876bool wxApp::Yield(bool onlyIfNeeded)
877{
878 bool s_inYield = FALSE;
879
880 if ( s_inYield )
881 {
882 if ( !onlyIfNeeded )
883 {
884 wxFAIL_MSG( wxT("wxYield called recursively" ) );
885 }
886
887 return FALSE;
888 }
889
890 s_inYield = TRUE;
891
892 while (wxTheApp && wxTheApp->Pending())
893 wxTheApp->Dispatch();
894
895 s_inYield = FALSE;
896
897 return TRUE;
898}
899
ef8c973b 900wxIcon wxApp::GetStdIcon(int which) const
83df96d6 901{
ef8c973b 902 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
83df96d6
JS
903}
904
7edcafa4
JS
905void wxApp::OnAssert(const wxChar *file, int line, const wxChar *msg)
906{
907 // While the GUI isn't working that well, just print out the
908 // message.
909#if 0
910 wxAppBase::OnAssert(file, line, msg);
911#else
912 wxString msg2;
913 msg2.Printf("At file %s:%d: %s", file, line, msg);
914 wxLogDebug(msg2);
915#endif
916}
917