added assert to detect attempt to install idle handler from widget callback
[wxWidgets.git] / src / gtk / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "app.h"
12 #endif
13
14 #ifdef __VMS
15 #include <vms_jackets.h>
16 #undef ConnectionNumber
17 #endif
18
19 #include "wx/app.h"
20 #include "wx/gdicmn.h"
21 #include "wx/utils.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/memory.h"
25 #include "wx/font.h"
26 #include "wx/settings.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
29 #include "wx/file.h"
30 #include "wx/filename.h"
31 #include "wx/module.h"
32 #include "wx/image.h"
33
34 #ifdef __WXUNIVERSAL__
35 #include "wx/univ/theme.h"
36 #include "wx/univ/renderer.h"
37 #endif
38
39 #if wxUSE_THREADS
40 #include "wx/thread.h"
41 #endif
42
43 #include <unistd.h>
44
45 #ifdef HAVE_POLL
46 #if defined(__VMS)
47 #include <poll.h>
48 #else
49 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
50 // in neither poll.h nor sys/poll.h which results in link errors later
51 #ifdef __OPENBSD__
52 extern "C"
53 {
54 #endif
55
56 #include <sys/poll.h>
57
58 #ifdef __OPENBSD__
59 };
60 #endif
61 #endif // platform
62 #else // !HAVE_POLL
63 // we implement poll() ourselves using select() which is supposed exist in
64 // all modern Unices
65 #include <sys/types.h>
66 #include <sys/time.h>
67 #include <unistd.h>
68 #endif // HAVE_POLL/!HAVE_POLL
69
70 #include "wx/gtk/win_gtk.h"
71
72 #include <gtk/gtk.h>
73
74
75 //-----------------------------------------------------------------------------
76 // global data
77 //-----------------------------------------------------------------------------
78
79 wxApp *wxTheApp = (wxApp *) NULL;
80 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
81
82 bool g_mainThreadLocked = FALSE;
83 gint g_pendingTag = 0;
84
85 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
86
87 //-----------------------------------------------------------------------------
88 // idle system
89 //-----------------------------------------------------------------------------
90
91 extern bool g_isIdle;
92
93 void wxapp_install_idle_handler();
94
95 //-----------------------------------------------------------------------------
96 // wxExit
97 //-----------------------------------------------------------------------------
98
99 void wxExit()
100 {
101 gtk_main_quit();
102 }
103
104 //-----------------------------------------------------------------------------
105 // wxYield
106 //-----------------------------------------------------------------------------
107
108 // not static because used by textctrl.cpp
109 //
110 // MT-FIXME
111 bool wxIsInsideYield = FALSE;
112
113 bool wxApp::Yield(bool onlyIfNeeded)
114 {
115 if ( wxIsInsideYield )
116 {
117 if ( !onlyIfNeeded )
118 {
119 wxFAIL_MSG( wxT("wxYield called recursively" ) );
120 }
121
122 return FALSE;
123 }
124
125 #if wxUSE_THREADS
126 if ( !wxThread::IsMain() )
127 {
128 // can't call gtk_main_iteration() from other threads like this
129 return TRUE;
130 }
131 #endif // wxUSE_THREADS
132
133 wxIsInsideYield = TRUE;
134
135 if (!g_isIdle)
136 {
137 // We need to remove idle callbacks or the loop will
138 // never finish.
139 gtk_idle_remove( m_idleTag );
140 m_idleTag = 0;
141 g_isIdle = TRUE;
142 }
143
144 // disable log flushing from here because a call to wxYield() shouldn't
145 // normally result in message boxes popping up &c
146 wxLog::Suspend();
147
148 while (gtk_events_pending())
149 gtk_main_iteration();
150
151 // It's necessary to call ProcessIdle() to update the frames sizes which
152 // might have been changed (it also will update other things set from
153 // OnUpdateUI() which is a nice (and desired) side effect). But we
154 // call ProcessIdle() only once since this is not meant for longish
155 // background jobs (controlled by wxIdleEvent::RequestMore() and the
156 // return value of Processidle().
157 ProcessIdle();
158
159 // let the logs be flashed again
160 wxLog::Resume();
161
162 wxIsInsideYield = FALSE;
163
164 return TRUE;
165 }
166
167 //-----------------------------------------------------------------------------
168 // wxWakeUpIdle
169 //-----------------------------------------------------------------------------
170
171 static bool gs_WakeUpIdle = false;
172
173 void wxWakeUpIdle()
174 {
175 #if wxUSE_THREADS
176 if (!wxThread::IsMain())
177 wxMutexGuiEnter();
178 #endif
179
180 if (g_isIdle) {
181 gs_WakeUpIdle = true;
182 wxapp_install_idle_handler();
183 gs_WakeUpIdle = false;
184 }
185
186 #if wxUSE_THREADS
187 if (!wxThread::IsMain())
188 wxMutexGuiLeave();
189 #endif
190 }
191
192 //-----------------------------------------------------------------------------
193 // local functions
194 //-----------------------------------------------------------------------------
195
196 // the callback functions must be extern "C" to comply with GTK+ declarations
197 extern "C"
198 {
199
200 static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
201 {
202 if (!wxTheApp) return TRUE;
203
204 // When getting called from GDK's time-out handler
205 // we are no longer within GDK's grab on the GUI
206 // thread so we must lock it here ourselves.
207 gdk_threads_enter();
208
209 // Sent idle event to all who request them.
210 wxTheApp->ProcessPendingEvents();
211
212 g_pendingTag = 0;
213
214 // Flush the logged messages if any.
215 #if wxUSE_LOG
216 wxLog::FlushActive();
217 #endif // wxUSE_LOG
218
219 // Release lock again
220 gdk_threads_leave();
221
222 // Return FALSE to indicate that no more idle events are
223 // to be sent (single shot instead of continuous stream)
224 return FALSE;
225 }
226
227 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
228 {
229 if (!wxTheApp)
230 return TRUE;
231
232 #ifdef __WXDEBUG__
233 // don't generate the idle events while the assert modal dialog is shown,
234 // this completely confuses the apps which don't expect to be reentered
235 // from some safely-looking functions
236 if ( wxTheApp->IsInAssert() )
237 {
238 // But repaint the assertion message if necessary
239 if (wxTopLevelWindows.GetCount() > 0)
240 {
241 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
242 #ifdef __WXGTK20__
243 if (win->IsKindOf(CLASSINFO(wxMessageDialog)))
244 #else
245 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
246 #endif
247 win->OnInternalIdle();
248 }
249 return TRUE;
250 }
251 #endif // __WXDEBUG__
252
253 // When getting called from GDK's time-out handler
254 // we are no longer within GDK's grab on the GUI
255 // thread so we must lock it here ourselves.
256 gdk_threads_enter();
257
258 // Indicate that we are now in idle mode and event handlers
259 // will have to reinstall the idle handler again.
260 g_isIdle = TRUE;
261 wxTheApp->m_idleTag = 0;
262
263 // Send idle event to all who request them as long as
264 // no events have popped up in the event queue.
265 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
266 ;
267
268 // Release lock again
269 gdk_threads_leave();
270
271 // Return FALSE to indicate that no more idle events are
272 // to be sent (single shot instead of continuous stream).
273 return FALSE;
274 }
275
276 #if wxUSE_THREADS
277
278 #ifdef HAVE_POLL
279 #define wxPoll poll
280 #define wxPollFd pollfd
281 #else // !HAVE_POLL
282
283 typedef GPollFD wxPollFd;
284
285 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
286 {
287 // convert timeout from ms to struct timeval (s/us)
288 timeval tv_timeout;
289 tv_timeout.tv_sec = timeout/1000;
290 tv_timeout.tv_usec = (timeout%1000)*1000;
291
292 // remember the highest fd used here
293 int fdMax = -1;
294
295 // and fill the sets for select()
296 fd_set readfds;
297 fd_set writefds;
298 fd_set exceptfds;
299 FD_ZERO(&readfds);
300 FD_ZERO(&writefds);
301 FD_ZERO(&exceptfds);
302
303 unsigned int i;
304 for ( i = 0; i < nfds; i++ )
305 {
306 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") );
307
308 if ( ufds[i].events & G_IO_IN )
309 FD_SET(ufds[i].fd, &readfds);
310
311 if ( ufds[i].events & G_IO_PRI )
312 FD_SET(ufds[i].fd, &exceptfds);
313
314 if ( ufds[i].events & G_IO_OUT )
315 FD_SET(ufds[i].fd, &writefds);
316
317 if ( ufds[i].fd > fdMax )
318 fdMax = ufds[i].fd;
319 }
320
321 fdMax++;
322 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
323
324 // translate the results back
325 for ( i = 0; i < nfds; i++ )
326 {
327 ufds[i].revents = 0;
328
329 if ( FD_ISSET(ufds[i].fd, &readfds ) )
330 ufds[i].revents |= G_IO_IN;
331
332 if ( FD_ISSET(ufds[i].fd, &exceptfds ) )
333 ufds[i].revents |= G_IO_PRI;
334
335 if ( FD_ISSET(ufds[i].fd, &writefds ) )
336 ufds[i].revents |= G_IO_OUT;
337 }
338
339 return res;
340 }
341
342 #endif // HAVE_POLL/!HAVE_POLL
343
344 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
345 {
346 gdk_threads_enter();
347
348 wxMutexGuiLeave();
349 g_mainThreadLocked = TRUE;
350
351 // we rely on the fact that glib GPollFD struct is really just pollfd but
352 // I wonder how wise is this in the long term (VZ)
353 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
354
355 wxMutexGuiEnter();
356 g_mainThreadLocked = FALSE;
357
358 gdk_threads_leave();
359
360 return res;
361 }
362
363 #endif // wxUSE_THREADS
364
365 } // extern "C"
366
367 void wxapp_install_idle_handler()
368 {
369 wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
370
371 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
372
373 g_isIdle = FALSE;
374
375 if (g_pendingTag == 0)
376 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
377
378 // This routine gets called by all event handlers
379 // indicating that the idle is over. It may also
380 // get called from other thread for sending events
381 // to the main thread (and processing these in
382 // idle time). Very low priority.
383 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
384 }
385
386 //-----------------------------------------------------------------------------
387 // Access to the root window global
388 //-----------------------------------------------------------------------------
389
390 GtkWidget* wxGetRootWindow()
391 {
392 if (gs_RootWindow == NULL)
393 {
394 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
395 gtk_widget_realize( gs_RootWindow );
396 }
397 return gs_RootWindow;
398 }
399
400 //-----------------------------------------------------------------------------
401 // wxApp
402 //-----------------------------------------------------------------------------
403
404 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
405
406 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
407 EVT_IDLE(wxApp::OnIdle)
408 END_EVENT_TABLE()
409
410 wxApp::wxApp()
411 {
412 m_initialized = FALSE;
413 #ifdef __WXDEBUG__
414 m_isInAssert = FALSE;
415 #endif // __WXDEBUG__
416
417 m_idleTag = 0;
418 wxapp_install_idle_handler();
419
420 #if wxUSE_THREADS
421 g_main_set_poll_func( wxapp_poll_func );
422 #endif
423
424 m_colorCube = (unsigned char*) NULL;
425
426 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
427 m_glVisualInfo = (void *) NULL;
428 }
429
430 wxApp::~wxApp()
431 {
432 if (m_idleTag) gtk_idle_remove( m_idleTag );
433
434 if (m_colorCube) free(m_colorCube);
435 }
436
437 bool wxApp::OnInitGui()
438 {
439 if ( !wxAppBase::OnInitGui() )
440 return FALSE;
441
442 GdkVisual *visual = gdk_visual_get_system();
443
444 // if this is a wxGLApp (derived from wxApp), and we've already
445 // chosen a specific visual, then derive the GdkVisual from that
446 if (m_glVisualInfo != NULL)
447 {
448 #ifdef __WXGTK20__
449 // seems gtk_widget_set_default_visual no longer exists?
450 GdkVisual* vis = gtk_widget_get_default_visual();
451 #else
452 GdkVisual* vis = gdkx_visual_get(
453 ((XVisualInfo *) m_glVisualInfo) ->visualid );
454 gtk_widget_set_default_visual( vis );
455 #endif
456
457 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
458 gtk_widget_set_default_colormap( colormap );
459
460 visual = vis;
461 }
462
463 // On some machines, the default visual is just 256 colours, so
464 // we make sure we get the best. This can sometimes be wasteful.
465
466 else
467 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
468 {
469 #ifdef __WXGTK20__
470 /* seems gtk_widget_set_default_visual no longer exists? */
471 GdkVisual* vis = gtk_widget_get_default_visual();
472 #else
473 GdkVisual* vis = gdk_visual_get_best();
474 gtk_widget_set_default_visual( vis );
475 #endif
476
477 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
478 gtk_widget_set_default_colormap( colormap );
479
480 visual = vis;
481 }
482
483 // Nothing to do for 15, 16, 24, 32 bit displays
484 if (visual->depth > 8) return TRUE;
485
486 // initialize color cube for 8-bit color reduction dithering
487
488 GdkColormap *cmap = gtk_widget_get_default_colormap();
489
490 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
491
492 for (int r = 0; r < 32; r++)
493 {
494 for (int g = 0; g < 32; g++)
495 {
496 for (int b = 0; b < 32; b++)
497 {
498 int rr = (r << 3) | (r >> 2);
499 int gg = (g << 3) | (g >> 2);
500 int bb = (b << 3) | (b >> 2);
501
502 int index = -1;
503
504 GdkColor *colors = cmap->colors;
505 if (colors)
506 {
507 int max = 3 * 65536;
508
509 for (int i = 0; i < cmap->size; i++)
510 {
511 int rdiff = ((rr << 8) - colors[i].red);
512 int gdiff = ((gg << 8) - colors[i].green);
513 int bdiff = ((bb << 8) - colors[i].blue);
514 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
515 if (sum < max)
516 {
517 index = i; max = sum;
518 }
519 }
520 }
521 else
522 {
523 // assume 8-bit true or static colors. this really exists
524 GdkVisual* vis = gdk_colormap_get_visual( cmap );
525 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
526 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
527 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
528 }
529 m_colorCube[ (r*1024) + (g*32) + b ] = index;
530 }
531 }
532 }
533
534 return TRUE;
535 }
536
537 GdkVisual *wxApp::GetGdkVisual()
538 {
539 GdkVisual *visual = NULL;
540
541 if (m_glVisualInfo)
542 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
543 else
544 visual = gdk_window_get_visual( wxGetRootWindow()->window );
545
546 wxASSERT( visual );
547
548 return visual;
549 }
550
551 bool wxApp::ProcessIdle()
552 {
553 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
554 node = wxTopLevelWindows.GetFirst();
555 while (node)
556 {
557 wxWindow* win = node->GetData();
558 CallInternalIdle( win );
559
560 node = node->GetNext();
561 }
562
563 wxIdleEvent event;
564 event.SetEventObject( this );
565 ProcessEvent( event );
566
567 return event.MoreRequested();
568 }
569
570 void wxApp::OnIdle( wxIdleEvent &event )
571 {
572 static bool s_inOnIdle = FALSE;
573
574 // Avoid recursion (via ProcessEvent default case)
575 if (s_inOnIdle)
576 return;
577
578 s_inOnIdle = TRUE;
579
580 // Resend in the main thread events which have been prepared in other
581 // threads
582 ProcessPendingEvents();
583
584 // 'Garbage' collection of windows deleted with Close()
585 DeletePendingObjects();
586
587 // Send OnIdle events to all windows
588 bool needMore = SendIdleEvents();
589
590 if (needMore)
591 event.RequestMore(TRUE);
592
593 s_inOnIdle = FALSE;
594 }
595
596 bool wxApp::SendIdleEvents()
597 {
598 bool needMore = FALSE;
599
600 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
601 while (node)
602 {
603 wxWindow* win = node->GetData();
604 if (SendIdleEvents(win))
605 needMore = TRUE;
606
607 node = node->GetNext();
608 }
609
610 return needMore;
611 }
612
613 bool wxApp::CallInternalIdle( wxWindow* win )
614 {
615 win->OnInternalIdle();
616
617 wxWindowList::Node *node = win->GetChildren().GetFirst();
618 while (node)
619 {
620 wxWindow *win = node->GetData();
621
622 CallInternalIdle( win );
623 node = node->GetNext();
624 }
625
626 return TRUE;
627 }
628
629 bool wxApp::SendIdleEvents( wxWindow* win )
630 {
631 bool needMore = FALSE;
632
633 wxIdleEvent event;
634 event.SetEventObject(win);
635
636 win->GetEventHandler()->ProcessEvent(event);
637
638 if (event.MoreRequested())
639 needMore = TRUE;
640
641 wxWindowList::Node *node = win->GetChildren().GetFirst();
642 while (node)
643 {
644 wxWindow *win = node->GetData();
645
646 if (SendIdleEvents(win))
647 needMore = TRUE;
648 node = node->GetNext();
649 }
650
651 return needMore;
652 }
653
654 int wxApp::MainLoop()
655 {
656 gtk_main();
657 return 0;
658 }
659
660 void wxApp::ExitMainLoop()
661 {
662 if (gtk_main_level() > 0)
663 gtk_main_quit();
664 }
665
666 bool wxApp::Initialized()
667 {
668 return m_initialized;
669 }
670
671 bool wxApp::Pending()
672 {
673 return (gtk_events_pending() > 0);
674 }
675
676 void wxApp::Dispatch()
677 {
678 gtk_main_iteration();
679 }
680
681 void wxApp::DeletePendingObjects()
682 {
683 wxNode *node = wxPendingDelete.GetFirst();
684 while (node)
685 {
686 wxObject *obj = (wxObject *)node->GetData();
687
688 delete obj;
689
690 if (wxPendingDelete.Find(obj))
691 delete node;
692
693 node = wxPendingDelete.GetFirst();
694 }
695 }
696
697 bool wxApp::Initialize()
698 {
699 wxClassInfo::InitializeClasses();
700
701 // GL: I'm annoyed ... I don't know where to put this and I don't want to
702 // create a module for that as it's part of the core.
703 #if wxUSE_THREADS
704 wxPendingEvents = new wxList();
705 wxPendingEventsLocker = new wxCriticalSection();
706 #endif
707
708 wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING );
709 wxTheColourDatabase->Initialize();
710
711 wxInitializeStockLists();
712 wxInitializeStockObjects();
713
714 wxModule::RegisterModules();
715 if (!wxModule::InitializeModules())
716 return FALSE;
717
718 #if wxUSE_INTL
719 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
720 #endif
721
722 return TRUE;
723 }
724
725 void wxApp::CleanUp()
726 {
727 wxModule::CleanUpModules();
728
729 delete wxTheColourDatabase;
730 wxTheColourDatabase = (wxColourDatabase*) NULL;
731
732 wxDeleteStockObjects();
733
734 wxDeleteStockLists();
735
736 delete wxTheApp;
737 wxTheApp = (wxApp*) NULL;
738
739 wxClassInfo::CleanUpClasses();
740
741 #if wxUSE_THREADS
742 delete wxPendingEvents;
743 wxPendingEvents = NULL;
744 delete wxPendingEventsLocker;
745 wxPendingEventsLocker = NULL;
746 #endif
747
748 // check for memory leaks
749 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
750 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
751 {
752 wxLogDebug(wxT("There were memory leaks.\n"));
753 wxDebugContext::Dump();
754 wxDebugContext::PrintStatistics();
755 }
756 #endif // Debug
757
758 #if wxUSE_LOG
759 // do this as the very last thing because everything else can log messages
760 wxLog::DontCreateOnDemand();
761
762 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
763 if (oldLog)
764 delete oldLog;
765 #endif // wxUSE_LOG
766 }
767
768 //-----------------------------------------------------------------------------
769 // wxEntry
770 //-----------------------------------------------------------------------------
771
772 // NB: argc and argv may be changed here, pass by reference!
773 int wxEntryStart( int& argc, char *argv[] )
774 {
775 #if wxUSE_THREADS
776 // GTK 1.2 up to version 1.2.3 has broken threads
777 if ((gtk_major_version == 1) &&
778 (gtk_minor_version == 2) &&
779 (gtk_micro_version < 4))
780 {
781 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
782 }
783 else
784 {
785 g_thread_init(NULL);
786 }
787 #endif
788
789 gtk_set_locale();
790
791 // We should have the wxUSE_WCHAR_T test on the _outside_
792 #if wxUSE_WCHAR_T
793 #if defined(__WXGTK20__)
794 // gtk+ 2.0 supports Unicode through UTF-8 strings
795 wxConvCurrent = &wxConvUTF8;
796 #else
797 if (!wxOKlibc()) wxConvCurrent = &wxConvLocal;
798 #endif
799 #else
800 if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL;
801 #endif
802
803 gdk_threads_enter();
804
805 gtk_init( &argc, &argv );
806
807 wxSetDetectableAutoRepeat( TRUE );
808
809 if (!wxApp::Initialize())
810 {
811 gdk_threads_leave();
812 return -1;
813 }
814
815 return 0;
816 }
817
818
819 int wxEntryInitGui()
820 {
821 int retValue = 0;
822
823 if ( !wxTheApp->OnInitGui() )
824 retValue = -1;
825
826 wxGetRootWindow();
827
828 return retValue;
829 }
830
831
832 void wxEntryCleanup()
833 {
834 #if wxUSE_LOG
835 // flush the logged messages if any
836 wxLog *log = wxLog::GetActiveTarget();
837 if (log != NULL && log->HasPendingMessages())
838 log->Flush();
839
840 // continuing to use user defined log target is unsafe from now on because
841 // some resources may be already unavailable, so replace it by something
842 // more safe
843 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
844 if ( oldlog )
845 delete oldlog;
846 #endif // wxUSE_LOG
847
848 wxApp::CleanUp();
849
850 gdk_threads_leave();
851 }
852
853
854 int wxEntry( int argc, char *argv[] )
855 {
856 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
857 // This seems to be necessary since there are 'rogue'
858 // objects present at this point (perhaps global objects?)
859 // Setting a checkpoint will ignore them as far as the
860 // memory checking facility is concerned.
861 // Of course you may argue that memory allocated in globals should be
862 // checked, but this is a reasonable compromise.
863 wxDebugContext::SetCheckpoint();
864 #endif
865 int err = wxEntryStart(argc, argv);
866 if (err)
867 return err;
868
869 if (!wxTheApp)
870 {
871 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
872 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
873
874 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
875
876 wxObject *test_app = app_ini();
877
878 wxTheApp = (wxApp*) test_app;
879 }
880
881 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
882
883 wxTheApp->argc = argc;
884 #if wxUSE_UNICODE
885 wxTheApp->argv = new wxChar*[argc+1];
886 int mb_argc = 0;
887 while (mb_argc < argc)
888 {
889 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
890 mb_argc++;
891 }
892 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
893 #else
894 wxTheApp->argv = argv;
895 #endif
896
897 if (wxTheApp->argc > 0)
898 {
899 wxFileName fname( wxTheApp->argv[0] );
900 wxTheApp->SetAppName( fname.GetName() );
901 }
902
903 int retValue;
904 retValue = wxEntryInitGui();
905
906 // Here frames insert themselves automatically into wxTopLevelWindows by
907 // getting created in OnInit().
908 if ( retValue == 0 )
909 {
910 if ( !wxTheApp->OnInit() )
911 retValue = -1;
912 }
913
914 if ( retValue == 0 )
915 {
916 // Delete pending toplevel windows
917 wxTheApp->DeletePendingObjects();
918
919 // When is the app not initialized ?
920 wxTheApp->m_initialized = TRUE;
921
922 if (wxTheApp->Initialized())
923 {
924 wxTheApp->OnRun();
925
926 wxWindow *topWindow = wxTheApp->GetTopWindow();
927
928 // Delete all pending windows if any
929 wxTheApp->DeletePendingObjects();
930
931 // Reset top window
932 if (topWindow)
933 wxTheApp->SetTopWindow( (wxWindow*) NULL );
934
935 retValue = wxTheApp->OnExit();
936 }
937 }
938
939 wxEntryCleanup();
940
941 return retValue;
942 }
943
944 #ifdef __WXDEBUG__
945
946 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
947 {
948 m_isInAssert = TRUE;
949
950 wxAppBase::OnAssert(file, line, cond, msg);
951
952 m_isInAssert = FALSE;
953 }
954
955 #endif // __WXDEBUG__
956