]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/app.cpp
corrected warnings when compiling with -Wall -W
[wxWidgets.git] / src / gtk1 / app.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose:
4// Author: Robert Roebling
32e9da8b 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Julian Smart
8bbe427f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
afb74891 11 #pragma implementation "app.h"
c801d85f
KB
12#endif
13
df744f4d
JJ
14#ifdef __VMS
15#include <vms_jackets.h>
16#endif
17
c801d85f
KB
18#include "wx/app.h"
19#include "wx/gdicmn.h"
20#include "wx/utils.h"
c801d85f
KB
21#include "wx/intl.h"
22#include "wx/log.h"
46dc76ba 23#include "wx/memory.h"
a3622daa
VZ
24#include "wx/font.h"
25#include "wx/settings.h"
0d2a2b60 26#include "wx/dialog.h"
afb74891 27
06cfab17 28#if wxUSE_WX_RESOURCES
afb74891 29 #include "wx/resource.h"
f5abe911 30#endif
afb74891 31
031b2a7b 32#include "wx/module.h"
4bc67cc5 33#include "wx/image.h"
afb74891 34
df028524
VS
35#ifdef __WXUNIVERSAL__
36 #include "wx/univ/theme.h"
37 #include "wx/univ/renderer.h"
38#endif
39
91b8de8d 40#if wxUSE_THREADS
a37a5a73 41 #include "wx/thread.h"
91b8de8d 42#endif
afb74891 43
20e05ffb 44#include <unistd.h>
c690ae86
GD
45#if defined(__DARWIN__)
46// FIXME: select must be used instead of poll (GD)
47#elif defined(__VMS)
df744f4d
JJ
48# include <poll.h>
49#else
50# include <sys/poll.h>
51#endif
e8106239 52#include "wx/gtk/win_gtk.h"
c801d85f 53
20e05ffb 54#include <gtk/gtk.h>
24178e4a 55
83624f79 56
c801d85f
KB
57//-----------------------------------------------------------------------------
58// global data
59//-----------------------------------------------------------------------------
60
c67daf87 61wxApp *wxTheApp = (wxApp *) NULL;
32d4bfd1 62wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
c801d85f 63
96997d65
RR
64bool g_mainThreadLocked = FALSE;
65gint g_pendingTag = 0;
3ac8d3bc 66
c2fa61e8 67static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
d76fe38b 68
c801d85f 69//-----------------------------------------------------------------------------
3133cb9f 70// idle system
c801d85f
KB
71//-----------------------------------------------------------------------------
72
3133cb9f 73extern bool g_isIdle;
c4fda16b 74
90350682 75void wxapp_install_idle_handler();
bf9e3e73 76
c801d85f 77//-----------------------------------------------------------------------------
bf9e3e73 78// wxExit
c801d85f
KB
79//-----------------------------------------------------------------------------
80
60acb947 81void wxExit()
c801d85f 82{
ec758a20 83 gtk_main_quit();
ff7b1510 84}
c801d85f 85
bf9e3e73
RR
86//-----------------------------------------------------------------------------
87// wxYield
88//-----------------------------------------------------------------------------
53a8af59 89
8461e4c2 90bool wxApp::Yield(bool onlyIfNeeded)
c801d85f 91{
8461e4c2
VZ
92 // MT-FIXME
93 static bool s_inYield = FALSE;
94
95 if ( s_inYield )
96 {
97 if ( !onlyIfNeeded )
98 {
99 wxFAIL_MSG( wxT("wxYield called recursively" ) );
100 }
101
102 return FALSE;
103 }
104
6dc5fd71
VZ
105#if wxUSE_THREADS
106 if ( !wxThread::IsMain() )
107 {
108 // can't call gtk_main_iteration() from other threads like this
109 return TRUE;
110 }
111#endif // wxUSE_THREADS
112
8461e4c2 113 s_inYield = TRUE;
e90c1d2a 114
99ba739f 115 if (!g_isIdle)
acfd422a 116 {
99ba739f
RR
117 // We need to remove idle callbacks or the loop will
118 // never finish.
8461e4c2
VZ
119 gtk_idle_remove( m_idleTag );
120 m_idleTag = 0;
99ba739f 121 g_isIdle = TRUE;
956dbab1 122 }
acfd422a 123
2ed3265e
VZ
124 // disable log flushing from here because a call to wxYield() shouldn't
125 // normally result in message boxes popping up &c
126 wxLog::Suspend();
127
406a6f6b
VZ
128 while (gtk_events_pending())
129 gtk_main_iteration();
130
5375a1f5
RR
131 // It's necessary to call ProcessIdle() to update the frames sizes which
132 // might have been changed (it also will update other things set from
133 // OnUpdateUI() which is a nice (and desired) side effect). But we
134 // call ProcessIdle() only once since this is not meant for longish
135 // background jobs (controlled by wxIdleEvent::RequestMore() and the
136 // return value of Processidle().
137 ProcessIdle();
2ed3265e
VZ
138
139 // let the logs be flashed again
140 wxLog::Resume();
7741c4e1 141
8461e4c2 142 s_inYield = FALSE;
99ba739f 143
acfd422a
RR
144 return TRUE;
145}
146
bf9e3e73
RR
147//-----------------------------------------------------------------------------
148// wxWakeUpIdle
149//-----------------------------------------------------------------------------
150
151void wxWakeUpIdle()
152{
924ef850
RR
153#if wxUSE_THREADS
154 if (!wxThread::IsMain())
ce6d2511 155 wxMutexGuiEnter();
924ef850
RR
156#endif
157
2286341c 158 if (g_isIdle)
bf9e3e73 159 wxapp_install_idle_handler();
2286341c 160
924ef850
RR
161#if wxUSE_THREADS
162 if (!wxThread::IsMain())
ce6d2511 163 wxMutexGuiLeave();
924ef850 164#endif
bf9e3e73
RR
165}
166
167//-----------------------------------------------------------------------------
168// local functions
169//-----------------------------------------------------------------------------
170
90350682
VZ
171// the callback functions must be extern "C" to comply with GTK+ declarations
172extern "C"
173{
174
3133cb9f 175static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
acfd422a
RR
176{
177 if (!wxTheApp) return TRUE;
c2fa61e8 178
5375a1f5 179 // When getting called from GDK's time-out handler
96997d65 180 // we are no longer within GDK's grab on the GUI
5375a1f5 181 // thread so we must lock it here ourselves.
96997d65
RR
182 gdk_threads_enter();
183
5375a1f5 184 // Sent idle event to all who request them.
96997d65 185 wxTheApp->ProcessPendingEvents();
e90c1d2a 186
96997d65
RR
187 g_pendingTag = 0;
188
5375a1f5 189 // Flush the logged messages if any.
1b2dab34
RR
190#if wxUSE_LOG
191 wxLog::FlushActive();
192#endif // wxUSE_LOG
193
96997d65
RR
194 // Release lock again
195 gdk_threads_leave();
196
197 // Return FALSE to indicate that no more idle events are
198 // to be sent (single shot instead of continuous stream)
199 return FALSE;
200}
201
3133cb9f 202static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
96997d65 203{
a5f1fd3e
VZ
204 if (!wxTheApp)
205 return TRUE;
206
207#ifdef __WXDEBUG__
6d477bb4
VZ
208 // don't generate the idle events while the assert modal dialog is shown,
209 // this completely confuses the apps which don't expect to be reentered
210 // from some safely-looking functions
a5f1fd3e
VZ
211 if ( wxTheApp->IsInAssert() )
212 {
6d477bb4 213 return TRUE;
a5f1fd3e
VZ
214 }
215#endif // __WXDEBUG__
c2fa61e8 216
5375a1f5 217 // When getting called from GDK's time-out handler
924ef850 218 // we are no longer within GDK's grab on the GUI
5375a1f5 219 // thread so we must lock it here ourselves.
924ef850 220 gdk_threads_enter();
094637f6 221
5375a1f5
RR
222 // Indicate that we are now in idle mode and event handlers
223 // will have to reinstall the idle handler again.
acfd422a 224 g_isIdle = TRUE;
96997d65 225 wxTheApp->m_idleTag = 0;
094637f6 226
3133cb9f 227 // Send idle event to all who request them as long as
5375a1f5
RR
228 // no events have popped up in the event queue.
229 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
6d477bb4 230 ;
c9e35272 231
96997d65 232 // Release lock again
924ef850 233 gdk_threads_leave();
acfd422a 234
96997d65 235 // Return FALSE to indicate that no more idle events are
5375a1f5 236 // to be sent (single shot instead of continuous stream).
96997d65 237 return FALSE;
acfd422a 238}
8801832d 239
90350682
VZ
240#if wxUSE_THREADS
241
3133cb9f 242static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
acfd422a 243{
3133cb9f 244 gint res;
90350682 245 gdk_threads_enter();
7b90a8f2 246
90350682 247 wxMutexGuiLeave();
90350682 248 g_mainThreadLocked = TRUE;
7941ba11 249
c690ae86
GD
250#ifdef __DARWIN__
251 // FIXME: poll is not available under Darwin/Mac OS X and this needs
252 // to be implemented using select instead (GD)
253 // what about other BSD derived systems?
254 res = -1;
255#else
3133cb9f 256 res = poll( (struct pollfd*) ufds, nfds, timeout );
c690ae86 257#endif
90350682 258
90350682 259 wxMutexGuiEnter();
90350682
VZ
260 g_mainThreadLocked = FALSE;
261
90350682
VZ
262 gdk_threads_leave();
263
3133cb9f 264 return res;
ff7b1510 265}
c801d85f 266
90350682
VZ
267#endif // wxUSE_THREADS
268
269} // extern "C"
270
3133cb9f 271void wxapp_install_idle_handler()
b453e1b2 272{
3133cb9f 273 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
c2fa61e8 274
3133cb9f 275 g_isIdle = FALSE;
96997d65 276
3133cb9f
RR
277 if (g_pendingTag == 0)
278 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
e90c1d2a 279
3133cb9f
RR
280 // This routine gets called by all event handlers
281 // indicating that the idle is over. It may also
282 // get called from other thread for sending events
283 // to the main thread (and processing these in
284 // idle time). Very low priority.
285 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
b453e1b2
RR
286}
287
005f5d18
RR
288//-----------------------------------------------------------------------------
289// Access to the root window global
290//-----------------------------------------------------------------------------
291
292GtkWidget* wxGetRootWindow()
293{
294 if (gs_RootWindow == NULL)
295 {
296 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
297 gtk_widget_realize( gs_RootWindow );
298 }
299 return gs_RootWindow;
300}
301
c801d85f
KB
302//-----------------------------------------------------------------------------
303// wxApp
304//-----------------------------------------------------------------------------
305
306IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
307
53010e52
RR
308BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
309 EVT_IDLE(wxApp::OnIdle)
310END_EVENT_TABLE()
311
c801d85f
KB
312wxApp::wxApp()
313{
a5f1fd3e
VZ
314 m_initialized = FALSE;
315#ifdef __WXDEBUG__
316 m_isInAssert = FALSE;
317#endif // __WXDEBUG__
318
df5ddbca
RR
319 m_idleTag = 0;
320 wxapp_install_idle_handler();
094637f6 321
6b3eb77a 322#if wxUSE_THREADS
3133cb9f 323 g_main_set_poll_func( wxapp_poll_func );
6b3eb77a 324#endif
60acb947 325
f6fcbb63 326 m_colorCube = (unsigned char*) NULL;
a6f5aa49
VZ
327
328 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
329 m_glVisualInfo = (void *) NULL;
ff7b1510 330}
c801d85f 331
60acb947 332wxApp::~wxApp()
c801d85f 333{
acfd422a 334 if (m_idleTag) gtk_idle_remove( m_idleTag );
60acb947 335
f6fcbb63 336 if (m_colorCube) free(m_colorCube);
ff7b1510 337}
c801d85f 338
0d2a2b60 339bool wxApp::OnInitGui()
c801d85f 340{
1e6feb95
VZ
341 if ( !wxAppBase::OnInitGui() )
342 return FALSE;
343
b134516c
RR
344 GdkVisual *visual = gdk_visual_get_system();
345
a6f5aa49
VZ
346 // if this is a wxGLApp (derived from wxApp), and we've already
347 // chosen a specific visual, then derive the GdkVisual from that
005f5d18
RR
348 if (m_glVisualInfo != NULL)
349 {
a6f5aa49 350#ifdef __WXGTK20__
005f5d18 351 // seems gtk_widget_set_default_visual no longer exists?
a6f5aa49
VZ
352 GdkVisual* vis = gtk_widget_get_default_visual();
353#else
354 GdkVisual* vis = gdkx_visual_get(
355 ((XVisualInfo *) m_glVisualInfo) ->visualid );
356 gtk_widget_set_default_visual( vis );
357#endif
358
359 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
360 gtk_widget_set_default_colormap( colormap );
361
362 visual = vis;
363 }
364
005f5d18
RR
365 // On some machines, the default visual is just 256 colours, so
366 // we make sure we get the best. This can sometimes be wasteful.
2286341c 367
005f5d18
RR
368 else
369 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
b134516c 370 {
2d4dc3a4
OK
371#ifdef __WXGTK20__
372 /* seems gtk_widget_set_default_visual no longer exists? */
373 GdkVisual* vis = gtk_widget_get_default_visual();
374#else
b134516c
RR
375 GdkVisual* vis = gdk_visual_get_best();
376 gtk_widget_set_default_visual( vis );
2d4dc3a4 377#endif
f6fcbb63 378
b134516c
RR
379 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
380 gtk_widget_set_default_colormap( colormap );
094637f6
VZ
381
382 visual = vis;
b134516c 383 }
d76fe38b 384
005f5d18 385 // Nothing to do for 15, 16, 24, 32 bit displays
f6fcbb63 386 if (visual->depth > 8) return TRUE;
60acb947 387
005f5d18 388 // initialize color cube for 8-bit color reduction dithering
60acb947 389
f6fcbb63 390 GdkColormap *cmap = gtk_widget_get_default_colormap();
60acb947 391
f6fcbb63
RR
392 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
393
f03fc89f
VZ
394 for (int r = 0; r < 32; r++)
395 {
8801832d
VZ
396 for (int g = 0; g < 32; g++)
397 {
398 for (int b = 0; b < 32; b++)
399 {
400 int rr = (r << 3) | (r >> 2);
401 int gg = (g << 3) | (g >> 2);
402 int bb = (b << 3) | (b >> 2);
60acb947 403
f03fc89f
VZ
404 int index = -1;
405
f6fcbb63 406 GdkColor *colors = cmap->colors;
ca26177c 407 if (colors)
f03fc89f
VZ
408 {
409 int max = 3 * 65536;
410
411 for (int i = 0; i < cmap->size; i++)
412 {
413 int rdiff = ((rr << 8) - colors[i].red);
414 int gdiff = ((gg << 8) - colors[i].green);
415 int bdiff = ((bb << 8) - colors[i].blue);
416 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
417 if (sum < max)
094637f6 418 {
f03fc89f
VZ
419 index = i; max = sum;
420 }
421 }
f6fcbb63 422 }
094637f6
VZ
423 else
424 {
005f5d18 425 // assume 8-bit true or static colors. this really exists
094637f6
VZ
426 GdkVisual* vis = gdk_colormap_get_visual( cmap );
427 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
428 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
429 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
094637f6 430 }
8801832d
VZ
431 m_colorCube[ (r*1024) + (g*32) + b ] = index;
432 }
433 }
f6fcbb63 434 }
c801d85f 435
bbe0af5b
RR
436 return TRUE;
437}
438
005f5d18
RR
439GdkVisual *wxApp::GetGdkVisual()
440{
441 GdkVisual *visual = NULL;
442
443 if (m_glVisualInfo)
7b775074 444 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
005f5d18
RR
445 else
446 visual = gdk_window_get_visual( wxGetRootWindow()->window );
447
448 wxASSERT( visual );
449
450 return visual;
451}
452
60acb947 453bool wxApp::ProcessIdle()
53010e52 454{
ec758a20
RR
455 wxIdleEvent event;
456 event.SetEventObject( this );
457 ProcessEvent( event );
0cf2cb36 458
ec758a20 459 return event.MoreRequested();
ff7b1510 460}
53010e52
RR
461
462void wxApp::OnIdle( wxIdleEvent &event )
c801d85f 463{
956dbab1 464 static bool s_inOnIdle = FALSE;
2286341c 465
5375a1f5 466 // Avoid recursion (via ProcessEvent default case)
956dbab1 467 if (s_inOnIdle)
ec758a20 468 return;
2286341c 469
956dbab1 470 s_inOnIdle = TRUE;
53010e52 471
5375a1f5
RR
472 // Resend in the main thread events which have been prepared in other
473 // threads
7214297d
GL
474 ProcessPendingEvents();
475
5375a1f5 476 // 'Garbage' collection of windows deleted with Close()
ec758a20 477 DeletePendingObjects();
53010e52 478
5375a1f5 479 // Send OnIdle events to all windows
ec758a20 480 bool needMore = SendIdleEvents();
53010e52 481
ec758a20
RR
482 if (needMore)
483 event.RequestMore(TRUE);
53010e52 484
956dbab1 485 s_inOnIdle = FALSE;
ff7b1510 486}
53010e52 487
60acb947 488bool wxApp::SendIdleEvents()
53010e52
RR
489{
490 bool needMore = FALSE;
e0253070 491
e146b8c8 492 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
ec758a20
RR
493 while (node)
494 {
e146b8c8 495 wxWindow* win = node->GetData();
f3855ef0 496 if (SendIdleEvents(win))
53010e52 497 needMore = TRUE;
010afced 498
e146b8c8 499 node = node->GetNext();
ec758a20 500 }
e146b8c8 501
010afced
RR
502 node = wxTopLevelWindows.GetFirst();
503 while (node)
504 {
505 wxWindow* win = node->GetData();
506 CallInternalIdle( win );
507
508 node = node->GetNext();
509 }
53010e52 510 return needMore;
ff7b1510 511}
53010e52 512
010afced
RR
513bool wxApp::CallInternalIdle( wxWindow* win )
514{
515 win->OnInternalIdle();
516
517 wxNode* node = win->GetChildren().First();
518 while (node)
519 {
520 wxWindow* win = (wxWindow*) node->Data();
521 CallInternalIdle( win );
522
523 node = node->Next();
524 }
525
526 return TRUE;
527}
528
53010e52
RR
529bool wxApp::SendIdleEvents( wxWindow* win )
530{
531 bool needMore = FALSE;
532
8bbe427f
VZ
533 wxIdleEvent event;
534 event.SetEventObject(win);
60acb947 535
f6bcfd97 536 win->GetEventHandler()->ProcessEvent(event);
53010e52
RR
537
538 if (event.MoreRequested())
539 needMore = TRUE;
540
8bbe427f
VZ
541 wxNode* node = win->GetChildren().First();
542 while (node)
543 {
544 wxWindow* win = (wxWindow*) node->Data();
545 if (SendIdleEvents(win))
53010e52
RR
546 needMore = TRUE;
547
8bbe427f
VZ
548 node = node->Next();
549 }
0d1dff01
RR
550
551 return needMore;
ff7b1510 552}
c801d85f 553
60acb947 554int wxApp::MainLoop()
c801d85f 555{
ec758a20
RR
556 gtk_main();
557 return 0;
ff7b1510 558}
c801d85f 559
60acb947 560void wxApp::ExitMainLoop()
c801d85f 561{
7ec2881a
RR
562 if (gtk_main_level() > 0)
563 gtk_main_quit();
ff7b1510 564}
c801d85f 565
60acb947 566bool wxApp::Initialized()
c801d85f 567{
ec758a20 568 return m_initialized;
ff7b1510 569}
c801d85f 570
60acb947 571bool wxApp::Pending()
c801d85f 572{
acfd422a 573 return (gtk_events_pending() > 0);
ff7b1510 574}
c801d85f 575
60acb947 576void wxApp::Dispatch()
c801d85f 577{
8801832d 578 gtk_main_iteration();
ff7b1510 579}
c801d85f 580
60acb947 581void wxApp::DeletePendingObjects()
c801d85f 582{
ec758a20
RR
583 wxNode *node = wxPendingDelete.First();
584 while (node)
585 {
586 wxObject *obj = (wxObject *)node->Data();
0cf2cb36 587
ec758a20 588 delete obj;
c801d85f 589
f03fc89f
VZ
590 if (wxPendingDelete.Find(obj))
591 delete node;
c801d85f 592
ec758a20
RR
593 node = wxPendingDelete.First();
594 }
ff7b1510 595}
c801d85f 596
60acb947 597bool wxApp::Initialize()
c801d85f 598{
0d2a2b60 599 wxClassInfo::InitializeClasses();
60acb947 600
5c3e299e 601#if wxUSE_INTL
a92b8709 602 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
5c3e299e 603#endif
60acb947 604
4d3a259a
GL
605 // GL: I'm annoyed ... I don't know where to put this and I don't want to
606 // create a module for that as it's part of the core.
607#if wxUSE_THREADS
608 wxPendingEvents = new wxList();
609 wxPendingEventsLocker = new wxCriticalSection();
610#endif
611
0d2a2b60
RR
612 wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING );
613 wxTheColourDatabase->Initialize();
a3622daa 614
0d2a2b60
RR
615 wxInitializeStockLists();
616 wxInitializeStockObjects();
c801d85f 617
4a0c68a7
RR
618#if wxUSE_WX_RESOURCES
619 wxInitializeResourceSystem();
620#endif
621
0d2a2b60
RR
622 wxModule::RegisterModules();
623 if (!wxModule::InitializeModules()) return FALSE;
60acb947 624
0d2a2b60 625 return TRUE;
ff7b1510 626}
c801d85f 627
60acb947 628void wxApp::CleanUp()
c801d85f 629{
0d2a2b60 630 wxModule::CleanUpModules();
0cf2cb36 631
4a0c68a7
RR
632#if wxUSE_WX_RESOURCES
633 wxCleanUpResourceSystem();
634#endif
635
a11672a4 636 delete wxTheColourDatabase;
0d2a2b60 637 wxTheColourDatabase = (wxColourDatabase*) NULL;
60acb947 638
0d2a2b60
RR
639 wxDeleteStockObjects();
640
ec758a20 641 wxDeleteStockLists();
a3622daa 642
0d2a2b60
RR
643 delete wxTheApp;
644 wxTheApp = (wxApp*) NULL;
645
a11672a4
RR
646 wxClassInfo::CleanUpClasses();
647
4d3a259a
GL
648#if wxUSE_THREADS
649 delete wxPendingEvents;
650 delete wxPendingEventsLocker;
651#endif
652
60acb947 653 // check for memory leaks
0d2a2b60 654#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
6981d3ec 655 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
0d2a2b60 656 {
223d09f6 657 wxLogDebug(wxT("There were memory leaks.\n"));
0d2a2b60
RR
658 wxDebugContext::Dump();
659 wxDebugContext::PrintStatistics();
660 }
8801832d 661#endif // Debug
0d2a2b60 662
88ac883a 663#if wxUSE_LOG
60acb947 664 // do this as the very last thing because everything else can log messages
0d2a2b60 665 wxLog::DontCreateOnDemand();
60acb947 666
0d2a2b60 667 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
60acb947
VZ
668 if (oldLog)
669 delete oldLog;
88ac883a 670#endif // wxUSE_LOG
c19c1ca9 671}
c801d85f
KB
672
673//-----------------------------------------------------------------------------
674// wxEntry
675//-----------------------------------------------------------------------------
676
17154fc8
VZ
677// NB: argc and argv may be changed here, pass by reference!
678int wxEntryStart( int& argc, char *argv[] )
c801d85f 679{
924ef850 680#if wxUSE_THREADS
005f5d18
RR
681 // GTK 1.2 up to version 1.2.3 has broken threads
682 if ((gtk_major_version == 1) &&
8f75cb6c 683 (gtk_minor_version == 2) &&
2286341c 684 (gtk_micro_version < 4))
96997d65 685 {
fb65642c 686 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
8f75cb6c
RR
687 }
688 else
689 {
690 g_thread_init(NULL);
691 }
924ef850 692#endif
2286341c 693
0d2a2b60 694 gtk_set_locale();
c801d85f 695
f9862abd
JS
696 // We should have the wxUSE_WCHAR_T test on the _outside_
697#if wxUSE_WCHAR_T
2d4dc3a4
OK
698#if defined(__WXGTK20__)
699 // gtk+ 2.0 supports Unicode through UTF-8 strings
700 wxConvCurrent = &wxConvUTF8;
f9862abd 701#else
dcf924a3 702 if (!wxOKlibc()) wxConvCurrent = &wxConvLocal;
f9862abd 703#endif
fd9811b1
RR
704#else
705 if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL;
706#endif
002f4218 707
924ef850
RR
708 gdk_threads_enter();
709
0d2a2b60 710 gtk_init( &argc, &argv );
0cf2cb36 711
f0492f7d 712 wxSetDetectableAutoRepeat( TRUE );
094637f6 713
60acb947 714 if (!wxApp::Initialize())
924ef850
RR
715 {
716 gdk_threads_leave();
60acb947 717 return -1;
924ef850 718 }
0cf2cb36 719
954de0f1
RD
720 return 0;
721}
722
c2fa61e8 723
954de0f1
RD
724int wxEntryInitGui()
725{
726 int retValue = 0;
727
728 if ( !wxTheApp->OnInitGui() )
729 retValue = -1;
730
c2fa61e8 731 wxGetRootWindow();
954de0f1
RD
732
733 return retValue;
734}
735
736
737void wxEntryCleanup()
738{
739#if wxUSE_LOG
740 // flush the logged messages if any
741 wxLog *log = wxLog::GetActiveTarget();
742 if (log != NULL && log->HasPendingMessages())
743 log->Flush();
744
745 // continuing to use user defined log target is unsafe from now on because
746 // some resources may be already unavailable, so replace it by something
747 // more safe
748 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
749 if ( oldlog )
750 delete oldlog;
751#endif // wxUSE_LOG
752
753 wxApp::CleanUp();
754
755 gdk_threads_leave();
756}
757
758
954de0f1
RD
759int wxEntry( int argc, char *argv[] )
760{
2db0bbde
JS
761#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
762 // This seems to be necessary since there are 'rogue'
763 // objects present at this point (perhaps global objects?)
764 // Setting a checkpoint will ignore them as far as the
765 // memory checking facility is concerned.
766 // Of course you may argue that memory allocated in globals should be
767 // checked, but this is a reasonable compromise.
768 wxDebugContext::SetCheckpoint();
769#endif
a37a5a73 770 int err = wxEntryStart(argc, argv);
954de0f1
RD
771 if (err)
772 return err;
773
ec758a20 774 if (!wxTheApp)
c801d85f 775 {
60acb947 776 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
223d09f6 777 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
0cf2cb36 778
ec758a20 779 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
0cf2cb36 780
ec758a20 781 wxObject *test_app = app_ini();
0cf2cb36 782
ec758a20
RR
783 wxTheApp = (wxApp*) test_app;
784 }
0cf2cb36 785
223d09f6 786 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
c801d85f 787
ec758a20 788 wxTheApp->argc = argc;
6de92826
OK
789#if wxUSE_UNICODE
790 wxTheApp->argv = new wxChar*[argc+1];
791 int mb_argc = 0;
2286341c 792 while (mb_argc < argc)
924ef850
RR
793 {
794 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
795 mb_argc++;
6de92826
OK
796 }
797 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
798#else
ec758a20 799 wxTheApp->argv = argv;
6de92826 800#endif
0cf2cb36 801
8801832d 802 wxString name(wxFileNameFromPath(argv[0]));
ec758a20
RR
803 wxStripExtension( name );
804 wxTheApp->SetAppName( name );
e0253070 805
954de0f1
RD
806 int retValue;
807 retValue = wxEntryInitGui();
68df5777 808
8801832d
VZ
809 // Here frames insert themselves automatically into wxTopLevelWindows by
810 // getting created in OnInit().
0151c3eb
VZ
811 if ( retValue == 0 )
812 {
813 if ( !wxTheApp->OnInit() )
814 retValue = -1;
815 }
0cf2cb36 816
0151c3eb
VZ
817 if ( retValue == 0 )
818 {
047ac72b 819 // Delete pending toplevel windows
cfb50f14 820 wxTheApp->DeletePendingObjects();
094637f6 821
047ac72b
RR
822 // When is the app not initialized ?
823 wxTheApp->m_initialized = TRUE;
0cf2cb36 824
0151c3eb 825 if (wxTheApp->Initialized())
094637f6 826 {
2286341c 827 wxTheApp->OnRun();
0cf2cb36 828
cfb50f14 829 wxWindow *topWindow = wxTheApp->GetTopWindow();
047ac72b
RR
830
831 // Delete all pending windows if any
832 wxTheApp->DeletePendingObjects();
833
834 // Reset top window
cfb50f14 835 if (topWindow)
047ac72b 836 wxTheApp->SetTopWindow( (wxWindow*) NULL );
2286341c
VZ
837
838 retValue = wxTheApp->OnExit();
0d2a2b60 839 }
0151c3eb 840 }
0cf2cb36 841
954de0f1 842 wxEntryCleanup();
924ef850 843
ec758a20 844 return retValue;
ff7b1510 845}
1a56f55c 846
a5f1fd3e
VZ
847#ifdef __WXDEBUG__
848
849void wxApp::OnAssert(const wxChar *file, int line, const wxChar *msg)
850{
851 m_isInAssert = TRUE;
852
853 wxAppBase::OnAssert(file, line, msg);
854
855 m_isInAssert = FALSE;
856}
857
858#endif // __WXDEBUG__
859