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