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