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