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