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