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