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