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