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