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