Committing in .
[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
32 #if wxUSE_WX_RESOURCES
33 #include "wx/resource.h"
34 #endif
35
36 #include "wx/module.h"
37 #include "wx/image.h"
38
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/theme.h"
41 #include "wx/univ/renderer.h"
42 #endif
43
44 #if wxUSE_THREADS
45 #include "wx/thread.h"
46 #endif
47
48 #include <unistd.h>
49
50 #ifdef HAVE_POLL
51 #if defined(__VMS)
52 #include <poll.h>
53 #else
54 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
55 // in neither poll.h nor sys/poll.h which results in link errors later
56 #ifdef __OPENBSD__
57 extern "C"
58 {
59 #endif
60
61 #include <sys/poll.h>
62
63 #ifdef __OPENBSD__
64 };
65 #endif
66 #endif // platform
67 #else // !HAVE_POLL
68 // we implement poll() ourselves using select() which is supposed exist in
69 // all modern Unices
70 #include <sys/types.h>
71 #include <sys/time.h>
72 #include <unistd.h>
73 #endif // HAVE_POLL/!HAVE_POLL
74
75 #include "wx/gtk/win_gtk.h"
76
77 #include <gtk/gtk.h>
78
79
80 //-----------------------------------------------------------------------------
81 // global data
82 //-----------------------------------------------------------------------------
83
84 wxApp *wxTheApp = (wxApp *) NULL;
85 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
86
87 bool g_mainThreadLocked = FALSE;
88 gint g_pendingTag = 0;
89
90 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
91
92 //-----------------------------------------------------------------------------
93 // idle system
94 //-----------------------------------------------------------------------------
95
96 extern bool g_isIdle;
97
98 void wxapp_install_idle_handler();
99
100 //-----------------------------------------------------------------------------
101 // wxExit
102 //-----------------------------------------------------------------------------
103
104 void wxExit()
105 {
106 gtk_main_quit();
107 }
108
109 //-----------------------------------------------------------------------------
110 // wxYield
111 //-----------------------------------------------------------------------------
112
113 // not static because used by textctrl.cpp
114 //
115 // MT-FIXME
116 bool wxIsInsideYield = FALSE;
117
118 bool wxApp::Yield(bool onlyIfNeeded)
119 {
120 if ( wxIsInsideYield )
121 {
122 if ( !onlyIfNeeded )
123 {
124 wxFAIL_MSG( wxT("wxYield called recursively" ) );
125 }
126
127 return FALSE;
128 }
129
130 #if wxUSE_THREADS
131 if ( !wxThread::IsMain() )
132 {
133 // can't call gtk_main_iteration() from other threads like this
134 return TRUE;
135 }
136 #endif // wxUSE_THREADS
137
138 wxIsInsideYield = TRUE;
139
140 if (!g_isIdle)
141 {
142 // We need to remove idle callbacks or the loop will
143 // never finish.
144 gtk_idle_remove( m_idleTag );
145 m_idleTag = 0;
146 g_isIdle = TRUE;
147 }
148
149 // disable log flushing from here because a call to wxYield() shouldn't
150 // normally result in message boxes popping up &c
151 wxLog::Suspend();
152
153 while (gtk_events_pending())
154 gtk_main_iteration();
155
156 // It's necessary to call ProcessIdle() to update the frames sizes which
157 // might have been changed (it also will update other things set from
158 // OnUpdateUI() which is a nice (and desired) side effect). But we
159 // call ProcessIdle() only once since this is not meant for longish
160 // background jobs (controlled by wxIdleEvent::RequestMore() and the
161 // return value of Processidle().
162 ProcessIdle();
163
164 // let the logs be flashed again
165 wxLog::Resume();
166
167 wxIsInsideYield = FALSE;
168
169 return TRUE;
170 }
171
172 //-----------------------------------------------------------------------------
173 // wxWakeUpIdle
174 //-----------------------------------------------------------------------------
175
176 void wxWakeUpIdle()
177 {
178 #if wxUSE_THREADS
179 if (!wxThread::IsMain())
180 wxMutexGuiEnter();
181 #endif
182
183 if (g_isIdle)
184 wxapp_install_idle_handler();
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 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
243 win->OnInternalIdle();
244 }
245 return TRUE;
246 }
247 #endif // __WXDEBUG__
248
249 // When getting called from GDK's time-out handler
250 // we are no longer within GDK's grab on the GUI
251 // thread so we must lock it here ourselves.
252 gdk_threads_enter();
253
254 // Indicate that we are now in idle mode and event handlers
255 // will have to reinstall the idle handler again.
256 g_isIdle = TRUE;
257 wxTheApp->m_idleTag = 0;
258
259 // Send idle event to all who request them as long as
260 // no events have popped up in the event queue.
261 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
262 ;
263
264 // Release lock again
265 gdk_threads_leave();
266
267 // Return FALSE to indicate that no more idle events are
268 // to be sent (single shot instead of continuous stream).
269 return FALSE;
270 }
271
272 #if wxUSE_THREADS
273
274 #ifdef HAVE_POLL
275 #define wxPoll poll
276 #define wxPollFd pollfd
277 #else // !HAVE_POLL
278
279 typedef GPollFD wxPollFd;
280
281 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
282 {
283 // convert timeout from ms to struct timeval (s/us)
284 timeval tv_timeout;
285 tv_timeout.tv_sec = timeout/1000;
286 tv_timeout.tv_usec = (timeout%1000)*1000;
287
288 // remember the highest fd used here
289 int fdMax = -1;
290
291 // and fill the sets for select()
292 fd_set readfds;
293 fd_set writefds;
294 fd_set exceptfds;
295 FD_ZERO(&readfds);
296 FD_ZERO(&writefds);
297 FD_ZERO(&exceptfds);
298
299 unsigned int i;
300 for ( i = 0; i < nfds; i++ )
301 {
302 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") );
303
304 if ( ufds[i].events & G_IO_IN )
305 FD_SET(ufds[i].fd, &readfds);
306
307 if ( ufds[i].events & G_IO_PRI )
308 FD_SET(ufds[i].fd, &exceptfds);
309
310 if ( ufds[i].events & G_IO_OUT )
311 FD_SET(ufds[i].fd, &writefds);
312
313 if ( ufds[i].fd > fdMax )
314 fdMax = ufds[i].fd;
315 }
316
317 fdMax++;
318 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
319
320 // translate the results back
321 for ( i = 0; i < nfds; i++ )
322 {
323 ufds[i].revents = 0;
324
325 if ( FD_ISSET(ufds[i].fd, &readfds ) )
326 ufds[i].revents |= G_IO_IN;
327
328 if ( FD_ISSET(ufds[i].fd, &exceptfds ) )
329 ufds[i].revents |= G_IO_PRI;
330
331 if ( FD_ISSET(ufds[i].fd, &writefds ) )
332 ufds[i].revents |= G_IO_OUT;
333 }
334
335 return res;
336 }
337
338 #endif // HAVE_POLL/!HAVE_POLL
339
340 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
341 {
342 gdk_threads_enter();
343
344 wxMutexGuiLeave();
345 g_mainThreadLocked = TRUE;
346
347 // we rely on the fact that glib GPollFD struct is really just pollfd but
348 // I wonder how wise is this in the long term (VZ)
349 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
350
351 wxMutexGuiEnter();
352 g_mainThreadLocked = FALSE;
353
354 gdk_threads_leave();
355
356 return res;
357 }
358
359 #endif // wxUSE_THREADS
360
361 } // extern "C"
362
363 void wxapp_install_idle_handler()
364 {
365 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
366
367 g_isIdle = FALSE;
368
369 if (g_pendingTag == 0)
370 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
371
372 // This routine gets called by all event handlers
373 // indicating that the idle is over. It may also
374 // get called from other thread for sending events
375 // to the main thread (and processing these in
376 // idle time). Very low priority.
377 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
378 }
379
380 //-----------------------------------------------------------------------------
381 // Access to the root window global
382 //-----------------------------------------------------------------------------
383
384 GtkWidget* wxGetRootWindow()
385 {
386 if (gs_RootWindow == NULL)
387 {
388 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
389 gtk_widget_realize( gs_RootWindow );
390 }
391 return gs_RootWindow;
392 }
393
394 //-----------------------------------------------------------------------------
395 // wxApp
396 //-----------------------------------------------------------------------------
397
398 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
399
400 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
401 EVT_IDLE(wxApp::OnIdle)
402 END_EVENT_TABLE()
403
404 wxApp::wxApp()
405 {
406 m_initialized = FALSE;
407 #ifdef __WXDEBUG__
408 m_isInAssert = FALSE;
409 #endif // __WXDEBUG__
410
411 m_idleTag = 0;
412 wxapp_install_idle_handler();
413
414 #if wxUSE_THREADS
415 g_main_set_poll_func( wxapp_poll_func );
416 #endif
417
418 m_colorCube = (unsigned char*) NULL;
419
420 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
421 m_glVisualInfo = (void *) NULL;
422 }
423
424 wxApp::~wxApp()
425 {
426 if (m_idleTag) gtk_idle_remove( m_idleTag );
427
428 if (m_colorCube) free(m_colorCube);
429 }
430
431 bool wxApp::OnInitGui()
432 {
433 if ( !wxAppBase::OnInitGui() )
434 return FALSE;
435
436 GdkVisual *visual = gdk_visual_get_system();
437
438 // if this is a wxGLApp (derived from wxApp), and we've already
439 // chosen a specific visual, then derive the GdkVisual from that
440 if (m_glVisualInfo != NULL)
441 {
442 #ifdef __WXGTK20__
443 // seems gtk_widget_set_default_visual no longer exists?
444 GdkVisual* vis = gtk_widget_get_default_visual();
445 #else
446 GdkVisual* vis = gdkx_visual_get(
447 ((XVisualInfo *) m_glVisualInfo) ->visualid );
448 gtk_widget_set_default_visual( vis );
449 #endif
450
451 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
452 gtk_widget_set_default_colormap( colormap );
453
454 visual = vis;
455 }
456
457 // On some machines, the default visual is just 256 colours, so
458 // we make sure we get the best. This can sometimes be wasteful.
459
460 else
461 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
462 {
463 #ifdef __WXGTK20__
464 /* seems gtk_widget_set_default_visual no longer exists? */
465 GdkVisual* vis = gtk_widget_get_default_visual();
466 #else
467 GdkVisual* vis = gdk_visual_get_best();
468 gtk_widget_set_default_visual( vis );
469 #endif
470
471 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
472 gtk_widget_set_default_colormap( colormap );
473
474 visual = vis;
475 }
476
477 // Nothing to do for 15, 16, 24, 32 bit displays
478 if (visual->depth > 8) return TRUE;
479
480 // initialize color cube for 8-bit color reduction dithering
481
482 GdkColormap *cmap = gtk_widget_get_default_colormap();
483
484 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
485
486 for (int r = 0; r < 32; r++)
487 {
488 for (int g = 0; g < 32; g++)
489 {
490 for (int b = 0; b < 32; b++)
491 {
492 int rr = (r << 3) | (r >> 2);
493 int gg = (g << 3) | (g >> 2);
494 int bb = (b << 3) | (b >> 2);
495
496 int index = -1;
497
498 GdkColor *colors = cmap->colors;
499 if (colors)
500 {
501 int max = 3 * 65536;
502
503 for (int i = 0; i < cmap->size; i++)
504 {
505 int rdiff = ((rr << 8) - colors[i].red);
506 int gdiff = ((gg << 8) - colors[i].green);
507 int bdiff = ((bb << 8) - colors[i].blue);
508 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
509 if (sum < max)
510 {
511 index = i; max = sum;
512 }
513 }
514 }
515 else
516 {
517 // assume 8-bit true or static colors. this really exists
518 GdkVisual* vis = gdk_colormap_get_visual( cmap );
519 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
520 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
521 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
522 }
523 m_colorCube[ (r*1024) + (g*32) + b ] = index;
524 }
525 }
526 }
527
528 return TRUE;
529 }
530
531 GdkVisual *wxApp::GetGdkVisual()
532 {
533 GdkVisual *visual = NULL;
534
535 if (m_glVisualInfo)
536 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
537 else
538 visual = gdk_window_get_visual( wxGetRootWindow()->window );
539
540 wxASSERT( visual );
541
542 return visual;
543 }
544
545 bool wxApp::ProcessIdle()
546 {
547 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
548 node = wxTopLevelWindows.GetFirst();
549 while (node)
550 {
551 wxWindow* win = node->GetData();
552 CallInternalIdle( win );
553
554 node = node->GetNext();
555 }
556
557 wxIdleEvent event;
558 event.SetEventObject( this );
559 ProcessEvent( event );
560
561 return event.MoreRequested();
562 }
563
564 void wxApp::OnIdle( wxIdleEvent &event )
565 {
566 static bool s_inOnIdle = FALSE;
567
568 // Avoid recursion (via ProcessEvent default case)
569 if (s_inOnIdle)
570 return;
571
572 s_inOnIdle = TRUE;
573
574 // Resend in the main thread events which have been prepared in other
575 // threads
576 ProcessPendingEvents();
577
578 // 'Garbage' collection of windows deleted with Close()
579 DeletePendingObjects();
580
581 // Send OnIdle events to all windows
582 bool needMore = SendIdleEvents();
583
584 if (needMore)
585 event.RequestMore(TRUE);
586
587 s_inOnIdle = FALSE;
588 }
589
590 bool wxApp::SendIdleEvents()
591 {
592 bool needMore = FALSE;
593
594 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
595 while (node)
596 {
597 wxWindow* win = node->GetData();
598 if (SendIdleEvents(win))
599 needMore = TRUE;
600
601 node = node->GetNext();
602 }
603
604 return needMore;
605 }
606
607 bool wxApp::CallInternalIdle( wxWindow* win )
608 {
609 win->OnInternalIdle();
610
611 wxWindowList::Node *node = win->GetChildren().GetFirst();
612 while (node)
613 {
614 wxWindow *win = node->GetData();
615
616 CallInternalIdle( win );
617 node = node->GetNext();
618 }
619
620 return TRUE;
621 }
622
623 bool wxApp::SendIdleEvents( wxWindow* win )
624 {
625 bool needMore = FALSE;
626
627 wxIdleEvent event;
628 event.SetEventObject(win);
629
630 win->GetEventHandler()->ProcessEvent(event);
631
632 if (event.MoreRequested())
633 needMore = TRUE;
634
635 wxWindowList::Node *node = win->GetChildren().GetFirst();
636 while (node)
637 {
638 wxWindow *win = node->GetData();
639
640 if (SendIdleEvents(win))
641 needMore = TRUE;
642 node = node->GetNext();
643 }
644
645 return needMore;
646 }
647
648 int wxApp::MainLoop()
649 {
650 gtk_main();
651 return 0;
652 }
653
654 void wxApp::ExitMainLoop()
655 {
656 if (gtk_main_level() > 0)
657 gtk_main_quit();
658 }
659
660 bool wxApp::Initialized()
661 {
662 return m_initialized;
663 }
664
665 bool wxApp::Pending()
666 {
667 return (gtk_events_pending() > 0);
668 }
669
670 void wxApp::Dispatch()
671 {
672 gtk_main_iteration();
673 }
674
675 void wxApp::DeletePendingObjects()
676 {
677 wxNode *node = wxPendingDelete.GetFirst();
678 while (node)
679 {
680 wxObject *obj = (wxObject *)node->GetData();
681
682 delete obj;
683
684 if (wxPendingDelete.Find(obj))
685 delete node;
686
687 node = wxPendingDelete.GetFirst();
688 }
689 }
690
691 bool wxApp::Initialize()
692 {
693 wxClassInfo::InitializeClasses();
694
695 // GL: I'm annoyed ... I don't know where to put this and I don't want to
696 // create a module for that as it's part of the core.
697 #if wxUSE_THREADS
698 wxPendingEvents = new wxList();
699 wxPendingEventsLocker = new wxCriticalSection();
700 #endif
701
702 wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING );
703 wxTheColourDatabase->Initialize();
704
705 wxInitializeStockLists();
706 wxInitializeStockObjects();
707
708 #if wxUSE_WX_RESOURCES
709 wxInitializeResourceSystem();
710 #endif
711
712 wxModule::RegisterModules();
713 if (!wxModule::InitializeModules())
714 return FALSE;
715
716 #if wxUSE_INTL
717 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
718 #endif
719
720 return TRUE;
721 }
722
723 void wxApp::CleanUp()
724 {
725 wxModule::CleanUpModules();
726
727 #if wxUSE_WX_RESOURCES
728 wxCleanUpResourceSystem();
729 #endif
730
731 delete wxTheColourDatabase;
732 wxTheColourDatabase = (wxColourDatabase*) NULL;
733
734 wxDeleteStockObjects();
735
736 wxDeleteStockLists();
737
738 delete wxTheApp;
739 wxTheApp = (wxApp*) NULL;
740
741 wxClassInfo::CleanUpClasses();
742
743 #if wxUSE_THREADS
744 delete wxPendingEvents;
745 delete wxPendingEventsLocker;
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