]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/app.cpp
Applied patch [ 709618 ] fixes drawing position in wxPrinterDC::DoBlit()
[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>
ff7d1dcb 16#undef ConnectionNumber
df744f4d
JJ
17#endif
18
c801d85f
KB
19#include "wx/app.h"
20#include "wx/gdicmn.h"
21#include "wx/utils.h"
c801d85f
KB
22#include "wx/intl.h"
23#include "wx/log.h"
46dc76ba 24#include "wx/memory.h"
a3622daa
VZ
25#include "wx/font.h"
26#include "wx/settings.h"
0d2a2b60 27#include "wx/dialog.h"
94a6f0f8 28#include "wx/msgdlg.h"
b1ac3b56 29#include "wx/file.h"
2b5f62a0 30#include "wx/filename.h"
031b2a7b 31#include "wx/module.h"
4bc67cc5 32#include "wx/image.h"
afb74891 33
df028524
VS
34#ifdef __WXUNIVERSAL__
35 #include "wx/univ/theme.h"
36 #include "wx/univ/renderer.h"
37#endif
38
91b8de8d 39#if wxUSE_THREADS
a37a5a73 40 #include "wx/thread.h"
91b8de8d 41#endif
afb74891 42
20e05ffb 43#include <unistd.h>
2b5f62a0
VZ
44
45#ifdef HAVE_POLL
46 #if defined(__VMS)
47 #include <poll.h>
48 #else
49 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
50 // in neither poll.h nor sys/poll.h which results in link errors later
51 #ifdef __OPENBSD__
52 extern "C"
53 {
54 #endif
55
56 #include <sys/poll.h>
57
58 #ifdef __OPENBSD__
59 };
60 #endif
61 #endif // platform
62#else // !HAVE_POLL
63 // we implement poll() ourselves using select() which is supposed exist in
64 // all modern Unices
65 #include <sys/types.h>
66 #include <sys/time.h>
67 #include <unistd.h>
68#endif // HAVE_POLL/!HAVE_POLL
69
e8106239 70#include "wx/gtk/win_gtk.h"
c801d85f 71
20e05ffb 72#include <gtk/gtk.h>
24178e4a 73
83624f79 74
c801d85f
KB
75//-----------------------------------------------------------------------------
76// global data
77//-----------------------------------------------------------------------------
78
c67daf87 79wxApp *wxTheApp = (wxApp *) NULL;
32d4bfd1 80wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
c801d85f 81
96997d65
RR
82bool g_mainThreadLocked = FALSE;
83gint g_pendingTag = 0;
3ac8d3bc 84
c2fa61e8 85static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
d76fe38b 86
c801d85f 87//-----------------------------------------------------------------------------
3133cb9f 88// idle system
c801d85f
KB
89//-----------------------------------------------------------------------------
90
3133cb9f 91extern bool g_isIdle;
c4fda16b 92
90350682 93void wxapp_install_idle_handler();
bf9e3e73 94
c801d85f 95//-----------------------------------------------------------------------------
bf9e3e73 96// wxExit
c801d85f
KB
97//-----------------------------------------------------------------------------
98
60acb947 99void wxExit()
c801d85f 100{
ec758a20 101 gtk_main_quit();
ff7b1510 102}
c801d85f 103
bf9e3e73
RR
104//-----------------------------------------------------------------------------
105// wxYield
106//-----------------------------------------------------------------------------
53a8af59 107
1ee339ee
VZ
108// not static because used by textctrl.cpp
109//
110// MT-FIXME
111bool wxIsInsideYield = FALSE;
112
8461e4c2 113bool wxApp::Yield(bool onlyIfNeeded)
c801d85f 114{
1ee339ee 115 if ( wxIsInsideYield )
8461e4c2
VZ
116 {
117 if ( !onlyIfNeeded )
118 {
119 wxFAIL_MSG( wxT("wxYield called recursively" ) );
120 }
121
122 return FALSE;
123 }
124
6dc5fd71
VZ
125#if wxUSE_THREADS
126 if ( !wxThread::IsMain() )
127 {
128 // can't call gtk_main_iteration() from other threads like this
129 return TRUE;
130 }
131#endif // wxUSE_THREADS
132
1ee339ee 133 wxIsInsideYield = TRUE;
e90c1d2a 134
99ba739f 135 if (!g_isIdle)
acfd422a 136 {
99ba739f
RR
137 // We need to remove idle callbacks or the loop will
138 // never finish.
8461e4c2
VZ
139 gtk_idle_remove( m_idleTag );
140 m_idleTag = 0;
99ba739f 141 g_isIdle = TRUE;
956dbab1 142 }
acfd422a 143
2ed3265e
VZ
144 // disable log flushing from here because a call to wxYield() shouldn't
145 // normally result in message boxes popping up &c
146 wxLog::Suspend();
147
406a6f6b
VZ
148 while (gtk_events_pending())
149 gtk_main_iteration();
150
5375a1f5
RR
151 // It's necessary to call ProcessIdle() to update the frames sizes which
152 // might have been changed (it also will update other things set from
be88a6ad 153 // OnUpdateUI() which is a nice (and desired) side effect). But we
5375a1f5
RR
154 // call ProcessIdle() only once since this is not meant for longish
155 // background jobs (controlled by wxIdleEvent::RequestMore() and the
156 // return value of Processidle().
157 ProcessIdle();
2ed3265e
VZ
158
159 // let the logs be flashed again
160 wxLog::Resume();
7741c4e1 161
1ee339ee 162 wxIsInsideYield = FALSE;
99ba739f 163
acfd422a
RR
164 return TRUE;
165}
166
bf9e3e73
RR
167//-----------------------------------------------------------------------------
168// wxWakeUpIdle
169//-----------------------------------------------------------------------------
170
171void wxWakeUpIdle()
172{
924ef850
RR
173#if wxUSE_THREADS
174 if (!wxThread::IsMain())
ce6d2511 175 wxMutexGuiEnter();
924ef850
RR
176#endif
177
2286341c 178 if (g_isIdle)
bf9e3e73 179 wxapp_install_idle_handler();
2286341c 180
924ef850
RR
181#if wxUSE_THREADS
182 if (!wxThread::IsMain())
ce6d2511 183 wxMutexGuiLeave();
924ef850 184#endif
bf9e3e73
RR
185}
186
187//-----------------------------------------------------------------------------
188// local functions
189//-----------------------------------------------------------------------------
190
90350682
VZ
191// the callback functions must be extern "C" to comply with GTK+ declarations
192extern "C"
193{
194
3133cb9f 195static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
acfd422a
RR
196{
197 if (!wxTheApp) return TRUE;
c2fa61e8 198
5375a1f5 199 // When getting called from GDK's time-out handler
96997d65 200 // we are no longer within GDK's grab on the GUI
5375a1f5 201 // thread so we must lock it here ourselves.
96997d65
RR
202 gdk_threads_enter();
203
5375a1f5 204 // Sent idle event to all who request them.
96997d65 205 wxTheApp->ProcessPendingEvents();
e90c1d2a 206
96997d65
RR
207 g_pendingTag = 0;
208
5375a1f5 209 // Flush the logged messages if any.
1b2dab34
RR
210#if wxUSE_LOG
211 wxLog::FlushActive();
212#endif // wxUSE_LOG
213
96997d65
RR
214 // Release lock again
215 gdk_threads_leave();
216
217 // Return FALSE to indicate that no more idle events are
218 // to be sent (single shot instead of continuous stream)
219 return FALSE;
220}
221
3133cb9f 222static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
96997d65 223{
a5f1fd3e
VZ
224 if (!wxTheApp)
225 return TRUE;
226
227#ifdef __WXDEBUG__
6d477bb4
VZ
228 // don't generate the idle events while the assert modal dialog is shown,
229 // this completely confuses the apps which don't expect to be reentered
230 // from some safely-looking functions
a5f1fd3e
VZ
231 if ( wxTheApp->IsInAssert() )
232 {
94a6f0f8
JS
233 // But repaint the assertion message if necessary
234 if (wxTopLevelWindows.GetCount() > 0)
235 {
b1d4dd7a 236 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
13a7abf9
VS
237#ifdef __WXGTK20__
238 if (win->IsKindOf(CLASSINFO(wxMessageDialog)))
239#else
94a6f0f8 240 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
13a7abf9 241#endif
94a6f0f8
JS
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
b1d4dd7a 610 wxWindowList::Node *node = win->GetChildren().GetFirst();
010afced
RR
611 while (node)
612 {
b1d4dd7a 613 wxWindow *win = node->GetData();
010afced 614
b1d4dd7a
RL
615 CallInternalIdle( win );
616 node = node->GetNext();
010afced 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
b1d4dd7a 634 wxWindowList::Node *node = win->GetChildren().GetFirst();
8bbe427f
VZ
635 while (node)
636 {
b1d4dd7a
RL
637 wxWindow *win = node->GetData();
638
8bbe427f 639 if (SendIdleEvents(win))
53010e52 640 needMore = TRUE;
b1d4dd7a 641 node = node->GetNext();
8bbe427f 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{
b1d4dd7a 676 wxNode *node = wxPendingDelete.GetFirst();
ec758a20
RR
677 while (node)
678 {
b1d4dd7a 679 wxObject *obj = (wxObject *)node->GetData();
0cf2cb36 680
ec758a20 681 delete obj;
c801d85f 682
f03fc89f
VZ
683 if (wxPendingDelete.Find(obj))
684 delete node;
c801d85f 685
b1d4dd7a 686 node = wxPendingDelete.GetFirst();
ec758a20 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
0d2a2b60 707 wxModule::RegisterModules();
2b5f62a0
VZ
708 if (!wxModule::InitializeModules())
709 return FALSE;
710
711#if wxUSE_INTL
712 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
713#endif
60acb947 714
0d2a2b60 715 return TRUE;
ff7b1510 716}
c801d85f 717
60acb947 718void wxApp::CleanUp()
c801d85f 719{
0d2a2b60 720 wxModule::CleanUpModules();
0cf2cb36 721
a11672a4 722 delete wxTheColourDatabase;
0d2a2b60 723 wxTheColourDatabase = (wxColourDatabase*) NULL;
60acb947 724
0d2a2b60
RR
725 wxDeleteStockObjects();
726
ec758a20 727 wxDeleteStockLists();
a3622daa 728
0d2a2b60
RR
729 delete wxTheApp;
730 wxTheApp = (wxApp*) NULL;
731
a11672a4
RR
732 wxClassInfo::CleanUpClasses();
733
4d3a259a
GL
734#if wxUSE_THREADS
735 delete wxPendingEvents;
736 delete wxPendingEventsLocker;
737#endif
738
60acb947 739 // check for memory leaks
0d2a2b60 740#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
6981d3ec 741 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
0d2a2b60 742 {
223d09f6 743 wxLogDebug(wxT("There were memory leaks.\n"));
0d2a2b60
RR
744 wxDebugContext::Dump();
745 wxDebugContext::PrintStatistics();
746 }
8801832d 747#endif // Debug
0d2a2b60 748
88ac883a 749#if wxUSE_LOG
60acb947 750 // do this as the very last thing because everything else can log messages
0d2a2b60 751 wxLog::DontCreateOnDemand();
60acb947 752
0d2a2b60 753 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
60acb947
VZ
754 if (oldLog)
755 delete oldLog;
88ac883a 756#endif // wxUSE_LOG
c19c1ca9 757}
c801d85f
KB
758
759//-----------------------------------------------------------------------------
760// wxEntry
761//-----------------------------------------------------------------------------
762
17154fc8
VZ
763// NB: argc and argv may be changed here, pass by reference!
764int wxEntryStart( int& argc, char *argv[] )
c801d85f 765{
924ef850 766#if wxUSE_THREADS
005f5d18
RR
767 // GTK 1.2 up to version 1.2.3 has broken threads
768 if ((gtk_major_version == 1) &&
8f75cb6c 769 (gtk_minor_version == 2) &&
2286341c 770 (gtk_micro_version < 4))
96997d65 771 {
fb65642c 772 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
8f75cb6c
RR
773 }
774 else
775 {
776 g_thread_init(NULL);
777 }
924ef850 778#endif
2286341c 779
0d2a2b60 780 gtk_set_locale();
c801d85f 781
f9862abd
JS
782 // We should have the wxUSE_WCHAR_T test on the _outside_
783#if wxUSE_WCHAR_T
2d4dc3a4
OK
784#if defined(__WXGTK20__)
785 // gtk+ 2.0 supports Unicode through UTF-8 strings
786 wxConvCurrent = &wxConvUTF8;
f9862abd 787#else
dcf924a3 788 if (!wxOKlibc()) wxConvCurrent = &wxConvLocal;
f9862abd 789#endif
fd9811b1
RR
790#else
791 if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL;
792#endif
002f4218 793
924ef850
RR
794 gdk_threads_enter();
795
0d2a2b60 796 gtk_init( &argc, &argv );
0cf2cb36 797
f0492f7d 798 wxSetDetectableAutoRepeat( TRUE );
094637f6 799
60acb947 800 if (!wxApp::Initialize())
924ef850
RR
801 {
802 gdk_threads_leave();
60acb947 803 return -1;
924ef850 804 }
0cf2cb36 805
954de0f1
RD
806 return 0;
807}
808
c2fa61e8 809
954de0f1
RD
810int wxEntryInitGui()
811{
812 int retValue = 0;
813
814 if ( !wxTheApp->OnInitGui() )
815 retValue = -1;
816
c2fa61e8 817 wxGetRootWindow();
954de0f1
RD
818
819 return retValue;
820}
821
822
823void wxEntryCleanup()
824{
825#if wxUSE_LOG
826 // flush the logged messages if any
827 wxLog *log = wxLog::GetActiveTarget();
828 if (log != NULL && log->HasPendingMessages())
829 log->Flush();
830
831 // continuing to use user defined log target is unsafe from now on because
832 // some resources may be already unavailable, so replace it by something
833 // more safe
834 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
835 if ( oldlog )
836 delete oldlog;
837#endif // wxUSE_LOG
838
839 wxApp::CleanUp();
840
841 gdk_threads_leave();
842}
843
844
954de0f1
RD
845int wxEntry( int argc, char *argv[] )
846{
2db0bbde
JS
847#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
848 // This seems to be necessary since there are 'rogue'
849 // objects present at this point (perhaps global objects?)
850 // Setting a checkpoint will ignore them as far as the
851 // memory checking facility is concerned.
852 // Of course you may argue that memory allocated in globals should be
853 // checked, but this is a reasonable compromise.
854 wxDebugContext::SetCheckpoint();
855#endif
a37a5a73 856 int err = wxEntryStart(argc, argv);
954de0f1
RD
857 if (err)
858 return err;
859
ec758a20 860 if (!wxTheApp)
c801d85f 861 {
60acb947 862 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
223d09f6 863 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
0cf2cb36 864
ec758a20 865 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
0cf2cb36 866
ec758a20 867 wxObject *test_app = app_ini();
0cf2cb36 868
ec758a20
RR
869 wxTheApp = (wxApp*) test_app;
870 }
0cf2cb36 871
223d09f6 872 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
c801d85f 873
ec758a20 874 wxTheApp->argc = argc;
6de92826
OK
875#if wxUSE_UNICODE
876 wxTheApp->argv = new wxChar*[argc+1];
877 int mb_argc = 0;
2286341c 878 while (mb_argc < argc)
924ef850
RR
879 {
880 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
881 mb_argc++;
6de92826
OK
882 }
883 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
884#else
ec758a20 885 wxTheApp->argv = argv;
6de92826 886#endif
0cf2cb36 887
2b5f62a0
VZ
888 if (wxTheApp->argc > 0)
889 {
890 wxFileName fname( wxTheApp->argv[0] );
891 wxTheApp->SetAppName( fname.GetName() );
892 }
e0253070 893
954de0f1
RD
894 int retValue;
895 retValue = wxEntryInitGui();
68df5777 896
8801832d
VZ
897 // Here frames insert themselves automatically into wxTopLevelWindows by
898 // getting created in OnInit().
0151c3eb
VZ
899 if ( retValue == 0 )
900 {
901 if ( !wxTheApp->OnInit() )
902 retValue = -1;
903 }
0cf2cb36 904
0151c3eb
VZ
905 if ( retValue == 0 )
906 {
047ac72b 907 // Delete pending toplevel windows
cfb50f14 908 wxTheApp->DeletePendingObjects();
094637f6 909
047ac72b
RR
910 // When is the app not initialized ?
911 wxTheApp->m_initialized = TRUE;
0cf2cb36 912
0151c3eb 913 if (wxTheApp->Initialized())
094637f6 914 {
2286341c 915 wxTheApp->OnRun();
0cf2cb36 916
cfb50f14 917 wxWindow *topWindow = wxTheApp->GetTopWindow();
be88a6ad 918
047ac72b
RR
919 // Delete all pending windows if any
920 wxTheApp->DeletePendingObjects();
be88a6ad
RD
921
922 // Reset top window
cfb50f14 923 if (topWindow)
047ac72b 924 wxTheApp->SetTopWindow( (wxWindow*) NULL );
2286341c
VZ
925
926 retValue = wxTheApp->OnExit();
0d2a2b60 927 }
0151c3eb 928 }
0cf2cb36 929
954de0f1 930 wxEntryCleanup();
924ef850 931
ec758a20 932 return retValue;
ff7b1510 933}
1a56f55c 934
a5f1fd3e
VZ
935#ifdef __WXDEBUG__
936
be88a6ad 937void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
a5f1fd3e
VZ
938{
939 m_isInAssert = TRUE;
940
be88a6ad 941 wxAppBase::OnAssert(file, line, cond, msg);
a5f1fd3e
VZ
942
943 m_isInAssert = FALSE;
944}
945
946#endif // __WXDEBUG__
947