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