]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
partially working wxTaskBarIcon for X11
[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 delete wxPendingEventsLocker;
737 #endif
738
739 // check for memory leaks
740 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
741 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
742 {
743 wxLogDebug(wxT("There were memory leaks.\n"));
744 wxDebugContext::Dump();
745 wxDebugContext::PrintStatistics();
746 }
747 #endif // Debug
748
749 #if wxUSE_LOG
750 // do this as the very last thing because everything else can log messages
751 wxLog::DontCreateOnDemand();
752
753 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
754 if (oldLog)
755 delete oldLog;
756 #endif // wxUSE_LOG
757 }
758
759 //-----------------------------------------------------------------------------
760 // wxEntry
761 //-----------------------------------------------------------------------------
762
763 // NB: argc and argv may be changed here, pass by reference!
764 int wxEntryStart( int& argc, char *argv[] )
765 {
766 #if wxUSE_THREADS
767 // GTK 1.2 up to version 1.2.3 has broken threads
768 if ((gtk_major_version == 1) &&
769 (gtk_minor_version == 2) &&
770 (gtk_micro_version < 4))
771 {
772 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
773 }
774 else
775 {
776 g_thread_init(NULL);
777 }
778 #endif
779
780 gtk_set_locale();
781
782 // We should have the wxUSE_WCHAR_T test on the _outside_
783 #if wxUSE_WCHAR_T
784 #if defined(__WXGTK20__)
785 // gtk+ 2.0 supports Unicode through UTF-8 strings
786 wxConvCurrent = &wxConvUTF8;
787 #else
788 if (!wxOKlibc()) wxConvCurrent = &wxConvLocal;
789 #endif
790 #else
791 if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL;
792 #endif
793
794 gdk_threads_enter();
795
796 gtk_init( &argc, &argv );
797
798 wxSetDetectableAutoRepeat( TRUE );
799
800 if (!wxApp::Initialize())
801 {
802 gdk_threads_leave();
803 return -1;
804 }
805
806 return 0;
807 }
808
809
810 int wxEntryInitGui()
811 {
812 int retValue = 0;
813
814 if ( !wxTheApp->OnInitGui() )
815 retValue = -1;
816
817 wxGetRootWindow();
818
819 return retValue;
820 }
821
822
823 void wxEntryCleanup()
824 {
825 #if wxUSE_LOG
826 // flush the logged messages if any
827 wxLog *log = wxLog::GetActiveTarget();
828 if (log != NULL && log->HasPendingMessages())
829 log->Flush();
830
831 // continuing to use user defined log target is unsafe from now on because
832 // some resources may be already unavailable, so replace it by something
833 // more safe
834 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
835 if ( oldlog )
836 delete oldlog;
837 #endif // wxUSE_LOG
838
839 wxApp::CleanUp();
840
841 gdk_threads_leave();
842 }
843
844
845 int wxEntry( int argc, char *argv[] )
846 {
847 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
848 // This seems to be necessary since there are 'rogue'
849 // objects present at this point (perhaps global objects?)
850 // Setting a checkpoint will ignore them as far as the
851 // memory checking facility is concerned.
852 // Of course you may argue that memory allocated in globals should be
853 // checked, but this is a reasonable compromise.
854 wxDebugContext::SetCheckpoint();
855 #endif
856 int err = wxEntryStart(argc, argv);
857 if (err)
858 return err;
859
860 if (!wxTheApp)
861 {
862 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
863 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
864
865 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
866
867 wxObject *test_app = app_ini();
868
869 wxTheApp = (wxApp*) test_app;
870 }
871
872 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
873
874 wxTheApp->argc = argc;
875 #if wxUSE_UNICODE
876 wxTheApp->argv = new wxChar*[argc+1];
877 int mb_argc = 0;
878 while (mb_argc < argc)
879 {
880 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
881 mb_argc++;
882 }
883 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
884 #else
885 wxTheApp->argv = argv;
886 #endif
887
888 if (wxTheApp->argc > 0)
889 {
890 wxFileName fname( wxTheApp->argv[0] );
891 wxTheApp->SetAppName( fname.GetName() );
892 }
893
894 int retValue;
895 retValue = wxEntryInitGui();
896
897 // Here frames insert themselves automatically into wxTopLevelWindows by
898 // getting created in OnInit().
899 if ( retValue == 0 )
900 {
901 if ( !wxTheApp->OnInit() )
902 retValue = -1;
903 }
904
905 if ( retValue == 0 )
906 {
907 // Delete pending toplevel windows
908 wxTheApp->DeletePendingObjects();
909
910 // When is the app not initialized ?
911 wxTheApp->m_initialized = TRUE;
912
913 if (wxTheApp->Initialized())
914 {
915 wxTheApp->OnRun();
916
917 wxWindow *topWindow = wxTheApp->GetTopWindow();
918
919 // Delete all pending windows if any
920 wxTheApp->DeletePendingObjects();
921
922 // Reset top window
923 if (topWindow)
924 wxTheApp->SetTopWindow( (wxWindow*) NULL );
925
926 retValue = wxTheApp->OnExit();
927 }
928 }
929
930 wxEntryCleanup();
931
932 return retValue;
933 }
934
935 #ifdef __WXDEBUG__
936
937 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
938 {
939 m_isInAssert = TRUE;
940
941 wxAppBase::OnAssert(file, line, cond, msg);
942
943 m_isInAssert = FALSE;
944 }
945
946 #endif // __WXDEBUG__
947