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