]>
Commit | Line | Data |
---|---|---|
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 | |
91b8de8d | 31 | #if wxUSE_THREADS |
f3855ef0 | 32 | #include "wx/thread.h" |
91b8de8d | 33 | #endif |
afb74891 | 34 | |
c801d85f KB |
35 | #include "unistd.h" |
36 | ||
91b8de8d RR |
37 | #include "glib.h" |
38 | #include "gdk/gdk.h" | |
39 | #include "gtk/gtk.h" | |
24178e4a | 40 | |
83624f79 RR |
41 | #include "wx/gtk/win_gtk.h" |
42 | ||
c801d85f KB |
43 | //----------------------------------------------------------------------------- |
44 | // global data | |
45 | //----------------------------------------------------------------------------- | |
46 | ||
c67daf87 | 47 | wxApp *wxTheApp = (wxApp *) NULL; |
c801d85f KB |
48 | wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL; |
49 | ||
7214297d | 50 | #if wxUSE_THREADS |
4d3a259a GL |
51 | extern wxList *wxPendingEvents; |
52 | extern wxCriticalSection *wxPendingEventsLocker; | |
7214297d | 53 | #endif |
a3622daa | 54 | extern wxResourceCache *wxTheResourceCache; |
acfd422a | 55 | extern bool g_isIdle; |
c801d85f | 56 | |
01111366 RR |
57 | unsigned char g_palette[64*3] = |
58 | { | |
59 | 0x0, 0x0, 0x0, | |
60 | 0xff, 0xff, 0xff, | |
61 | 0xff, 0x0, 0x0, | |
62 | 0xff, 0xff, 0x0, | |
63 | 0x0, 0xff, 0x0, | |
64 | 0x0, 0x0, 0xff, | |
65 | 0x0, 0xff, 0xff, | |
66 | 0x99, 0x99, 0x99, | |
67 | 0xff, 0x88, 0x0, | |
68 | 0x88, 0x0, 0x0, | |
69 | 0x0, 0x88, 0x88, | |
70 | 0x88, 0x88, 0x0, | |
71 | 0xff, 0xcc, 0x97, | |
72 | 0xbb, 0xbb, 0xbb, | |
73 | 0x9f, 0x6b, 0x42, | |
74 | 0x55, 0x55, 0x55, | |
75 | 0xdd, 0xdd, 0xdd, | |
76 | 0x77, 0x77, 0x77, | |
77 | 0x33, 0x33, 0x33, | |
78 | 0xcc, 0x0, 0x0, | |
79 | 0xff, 0x44, 0x0, | |
80 | 0xff, 0xcc, 0x0, | |
81 | 0xcc, 0xcc, 0x0, | |
82 | 0x60, 0x60, 0x0, | |
83 | 0x0, 0x43, 0x0, | |
84 | 0x0, 0x7f, 0x0, | |
85 | 0x0, 0xcc, 0x0, | |
86 | 0x0, 0x44, 0x44, | |
87 | 0x0, 0x0, 0x44, | |
88 | 0x0, 0x0, 0x88, | |
89 | 0xef, 0xb1, 0x7b, | |
90 | 0xdf, 0x98, 0x5f, | |
91 | 0xbf, 0x87, 0x56, | |
92 | 0x7f, 0x57, 0x26, | |
93 | 0x5f, 0x39, 0xc, | |
94 | 0x3f, 0x1c, 0x0, | |
95 | 0x21, 0x0, 0x0, | |
96 | 0x0, 0x43, 0x87, | |
97 | 0x2d, 0x70, 0xaf, | |
98 | 0x5a, 0x9e, 0xd7, | |
99 | 0x87, 0xcc, 0xff, | |
100 | 0xff, 0xe0, 0xba, | |
101 | 0x21, 0x43, 0xf, | |
102 | 0x3d, 0x5d, 0x25, | |
103 | 0x59, 0x78, 0x3a, | |
104 | 0x75, 0x93, 0x4f, | |
105 | 0x91, 0xae, 0x64, | |
106 | 0xad, 0xc8, 0x7a, | |
e0253070 | 107 | 0xf0, 0xa8, 0xef, |
01111366 RR |
108 | 0xd0, 0x88, 0xd0, |
109 | 0xaf, 0x66, 0xaf, | |
110 | 0x8e, 0x44, 0x8e, | |
111 | 0x6d, 0x22, 0x6d, | |
e0253070 | 112 | 0x4b, 0x0, 0x4b, |
01111366 RR |
113 | 0xff, 0xc0, 0xbc, |
114 | 0xff, 0x93, 0x91, | |
115 | 0xff, 0x66, 0x67, | |
116 | 0xd8, 0xf2, 0xbf, | |
117 | 0xff, 0xc9, 0x68, | |
118 | 0xff, 0x96, 0x67, | |
119 | 0xa5, 0x60, 0xff, | |
120 | 0x51, 0xff, 0x99, | |
121 | 0x3f, 0xa5, 0x63, | |
122 | 0x98, 0x90, 0x67 | |
123 | }; | |
124 | ||
c801d85f KB |
125 | //----------------------------------------------------------------------------- |
126 | // local functions | |
127 | //----------------------------------------------------------------------------- | |
128 | ||
129 | extern void wxFlushResources(void); | |
130 | ||
131 | //----------------------------------------------------------------------------- | |
132 | // global functions | |
133 | //----------------------------------------------------------------------------- | |
134 | ||
60acb947 | 135 | void wxExit() |
c801d85f | 136 | { |
ec758a20 | 137 | gtk_main_quit(); |
ff7b1510 | 138 | } |
c801d85f | 139 | |
acfd422a | 140 | /* forward declaration */ |
53a8af59 KB |
141 | gint wxapp_idle_callback( gpointer WXUNUSED(data) ); |
142 | ||
60acb947 | 143 | bool wxYield() |
c801d85f | 144 | { |
7b90a8f2 RR |
145 | /* it's necessary to call ProcessIdle() to update the frames sizes which |
146 | might have been changed (it also will update other things set from | |
147 | OnUpdateUI() which is a nice (and desired) side effect) */ | |
1601ab7a RR |
148 | while (wxTheApp->ProcessIdle()) { } |
149 | ||
150 | #if 0 | |
e146b8c8 | 151 | for ( wxWindowList::Node *node = wxTopLevelWindows.GetFirst(); |
5f12ae5c VZ |
152 | node; |
153 | node = node->GetNext() ) | |
154 | { | |
e146b8c8 | 155 | wxWindow *win = node->GetData(); |
5f12ae5c VZ |
156 | win->OnInternalIdle(); |
157 | } | |
1601ab7a | 158 | #endif |
5f12ae5c | 159 | |
acfd422a RR |
160 | if (wxTheApp->m_idleTag) |
161 | { | |
162 | /* We need to temporarily remove idle callbacks or the loop will | |
163 | never finish. */ | |
164 | gtk_idle_remove( wxTheApp->m_idleTag ); | |
165 | wxTheApp->m_idleTag = 0; | |
166 | ||
167 | while (gtk_events_pending()) | |
168 | gtk_main_iteration(); | |
169 | ||
170 | /* re-add idle handler */ | |
171 | wxTheApp->m_idleTag = gtk_idle_add( wxapp_idle_callback, (gpointer) NULL ); | |
172 | } | |
173 | else | |
174 | { | |
175 | while (gtk_events_pending()) | |
176 | gtk_main_iteration(); | |
177 | } | |
178 | ||
179 | return TRUE; | |
180 | } | |
181 | ||
182 | gint wxapp_idle_callback( gpointer WXUNUSED(data) ) | |
183 | { | |
184 | if (!wxTheApp) return TRUE; | |
185 | ||
86fdd27b | 186 | #if (GTK_MINOR_VERSION > 0) |
7b90a8f2 RR |
187 | /* when getting called from GDK's idle handler we |
188 | are no longer within GDK's grab on the GUI | |
189 | thread so we must lock it here ourselves */ | |
190 | GDK_THREADS_ENTER (); | |
86fdd27b | 191 | #endif |
7b90a8f2 | 192 | |
acfd422a RR |
193 | /* sent idle event to all who request them */ |
194 | while (wxTheApp->ProcessIdle()) { } | |
195 | ||
196 | /* we don't want any more idle events until the next event is | |
197 | sent to wxGTK */ | |
53a8af59 | 198 | gtk_idle_remove( wxTheApp->m_idleTag ); |
acfd422a RR |
199 | wxTheApp->m_idleTag = 0; |
200 | ||
201 | /* indicate that we are now in idle mode - even so deeply | |
202 | in idle mode that we don't get any idle events anymore. | |
203 | this is like wxMSW where an idle event is sent only | |
204 | once each time after the event queue has been completely | |
205 | emptied */ | |
206 | g_isIdle = TRUE; | |
207 | ||
86fdd27b | 208 | #if (GTK_MINOR_VERSION > 0) |
7b90a8f2 RR |
209 | /* release lock again */ |
210 | GDK_THREADS_LEAVE (); | |
86fdd27b | 211 | #endif |
acfd422a RR |
212 | |
213 | return TRUE; | |
214 | } | |
8801832d | 215 | |
acfd422a RR |
216 | void wxapp_install_idle_handler() |
217 | { | |
861ccde4 | 218 | wxASSERT_MSG( wxTheApp->m_idleTag == 0, _T("attempt to install idle handler twice") ); |
7b90a8f2 | 219 | |
acfd422a RR |
220 | /* this routine gets called by all event handlers |
221 | indicating that the idle is over. */ | |
5f12ae5c | 222 | |
53a8af59 | 223 | wxTheApp->m_idleTag = gtk_idle_add( wxapp_idle_callback, (gpointer) NULL ); |
acfd422a RR |
224 | |
225 | g_isIdle = FALSE; | |
ff7b1510 | 226 | } |
c801d85f | 227 | |
6b3eb77a | 228 | #if wxUSE_THREADS |
7b90a8f2 RR |
229 | static gint wxapp_wakeup_timerout_callback( gpointer WXUNUSED(data) ) |
230 | { | |
231 | gtk_timeout_remove( wxTheApp->m_wakeUpTimerTag ); | |
232 | wxTheApp->m_wakeUpTimerTag = 0; | |
233 | ||
86fdd27b | 234 | #if (GTK_MINOR_VERSION > 0) |
7b90a8f2 RR |
235 | /* when getting called from GDK's time-out handler |
236 | we are no longer within GDK's grab on the GUI | |
237 | thread so we must lock it here ourselves */ | |
238 | GDK_THREADS_ENTER (); | |
86fdd27b | 239 | #endif |
7b90a8f2 RR |
240 | |
241 | /* unblock other threads wishing to do some GUI things */ | |
242 | wxMutexGuiLeave(); | |
243 | ||
244 | /* wake up other threads */ | |
245 | wxUsleep( 1 ); | |
246 | ||
247 | /* block other thread again */ | |
248 | wxMutexGuiEnter(); | |
249 | ||
86fdd27b | 250 | #if (GTK_MINOR_VERSION > 0) |
7b90a8f2 RR |
251 | /* release lock again */ |
252 | GDK_THREADS_LEAVE (); | |
86fdd27b | 253 | #endif |
7b90a8f2 | 254 | |
db2d879a | 255 | wxTheApp->m_wakeUpTimerTag = gtk_timeout_add( 20, wxapp_wakeup_timerout_callback, (gpointer) NULL ); |
7b90a8f2 RR |
256 | |
257 | return TRUE; | |
258 | } | |
6b3eb77a | 259 | #endif |
7b90a8f2 | 260 | |
c801d85f KB |
261 | //----------------------------------------------------------------------------- |
262 | // wxApp | |
263 | //----------------------------------------------------------------------------- | |
264 | ||
265 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
266 | ||
53010e52 RR |
267 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
268 | EVT_IDLE(wxApp::OnIdle) | |
269 | END_EVENT_TABLE() | |
270 | ||
c801d85f KB |
271 | wxApp::wxApp() |
272 | { | |
0d2a2b60 | 273 | wxTheApp = this; |
60acb947 | 274 | |
ec758a20 RR |
275 | m_topWindow = (wxWindow *) NULL; |
276 | m_exitOnFrameDelete = TRUE; | |
60acb947 | 277 | |
0d2a2b60 | 278 | m_idleTag = gtk_idle_add( wxapp_idle_callback, (gpointer) NULL ); |
7b90a8f2 | 279 | |
6b3eb77a | 280 | #if wxUSE_THREADS |
db2d879a | 281 | m_wakeUpTimerTag = gtk_timeout_add( 20, wxapp_wakeup_timerout_callback, (gpointer) NULL ); |
6b3eb77a | 282 | #endif |
60acb947 | 283 | |
f6fcbb63 | 284 | m_colorCube = (unsigned char*) NULL; |
ff7b1510 | 285 | } |
c801d85f | 286 | |
60acb947 | 287 | wxApp::~wxApp() |
c801d85f | 288 | { |
acfd422a | 289 | if (m_idleTag) gtk_idle_remove( m_idleTag ); |
60acb947 | 290 | |
6b3eb77a | 291 | #if wxUSE_THREADS |
7b90a8f2 | 292 | if (m_wakeUpTimerTag) gtk_timeout_remove( m_wakeUpTimerTag ); |
6b3eb77a | 293 | #endif |
7b90a8f2 | 294 | |
f6fcbb63 | 295 | if (m_colorCube) free(m_colorCube); |
ff7b1510 | 296 | } |
c801d85f | 297 | |
0d2a2b60 | 298 | bool wxApp::OnInitGui() |
c801d85f | 299 | { |
7b280524 RR |
300 | /* on some SGIs, the default visual is just 256 colours, so we |
301 | make sure we get the best. this can sometimes be wasteful, | |
302 | of course, but what do these guys pay $30.000 for? */ | |
303 | GdkVisual* visual = gdk_visual_get_best(); | |
304 | gtk_widget_set_default_visual( visual ); | |
f6fcbb63 | 305 | |
7b280524 | 306 | /* Nothing to do for 15, 16, 24, 32 bit displays */ |
f6fcbb63 | 307 | if (visual->depth > 8) return TRUE; |
60acb947 | 308 | |
0d2a2b60 RR |
309 | /* this initiates the standard palette as defined by GdkImlib |
310 | in the GNOME libraries. it ensures that all GNOME applications | |
311 | use the same 64 colormap entries on 8-bit displays so you | |
312 | can use several rather graphics-heavy applications at the | |
313 | same time. | |
8801832d | 314 | NOTE: this doesn't really seem to work this way... */ |
bbe0af5b | 315 | |
0d2a2b60 RR |
316 | /* |
317 | GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE ); | |
bbe0af5b | 318 | |
0d2a2b60 RR |
319 | for (int i = 0; i < 64; i++) |
320 | { | |
321 | GdkColor col; | |
322 | col.red = g_palette[i*3 + 0] << 8; | |
323 | col.green = g_palette[i*3 + 1] << 8; | |
324 | col.blue = g_palette[i*3 + 2] << 8; | |
325 | col.pixel = 0; | |
326 | ||
327 | gdk_color_alloc( cmap, &col ); | |
328 | } | |
60acb947 | 329 | |
0d2a2b60 RR |
330 | gtk_widget_set_default_colormap( cmap ); |
331 | */ | |
60acb947 | 332 | |
f6fcbb63 | 333 | /* initialize color cube for 8-bit color reduction dithering */ |
60acb947 | 334 | |
f6fcbb63 | 335 | GdkColormap *cmap = gtk_widget_get_default_colormap(); |
60acb947 | 336 | |
f6fcbb63 RR |
337 | m_colorCube = (unsigned char*)malloc(32 * 32 * 32); |
338 | ||
f03fc89f VZ |
339 | for (int r = 0; r < 32; r++) |
340 | { | |
8801832d VZ |
341 | for (int g = 0; g < 32; g++) |
342 | { | |
343 | for (int b = 0; b < 32; b++) | |
344 | { | |
345 | int rr = (r << 3) | (r >> 2); | |
346 | int gg = (g << 3) | (g >> 2); | |
347 | int bb = (b << 3) | (b >> 2); | |
60acb947 | 348 | |
f03fc89f VZ |
349 | int index = -1; |
350 | ||
f6fcbb63 | 351 | GdkColor *colors = cmap->colors; |
ca26177c | 352 | if (colors) |
f03fc89f VZ |
353 | { |
354 | int max = 3 * 65536; | |
355 | ||
356 | for (int i = 0; i < cmap->size; i++) | |
357 | { | |
358 | int rdiff = ((rr << 8) - colors[i].red); | |
359 | int gdiff = ((gg << 8) - colors[i].green); | |
360 | int bdiff = ((bb << 8) - colors[i].blue); | |
361 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
362 | if (sum < max) | |
363 | { | |
364 | index = i; max = sum; | |
365 | } | |
366 | } | |
f6fcbb63 | 367 | } |
ca26177c RR |
368 | else |
369 | { | |
370 | /* assume 8-bit true or static colors. this really | |
371 | exists. */ | |
372 | GdkVisual* vis = gdk_colormap_get_visual( cmap ); | |
373 | index = (r >> (5 - vis->red_prec)) << vis->red_shift; | |
374 | index |= (g >> (5 - vis->green_prec)) << vis->green_shift; | |
375 | index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift; | |
376 | } | |
377 | ||
8801832d VZ |
378 | m_colorCube[ (r*1024) + (g*32) + b ] = index; |
379 | } | |
380 | } | |
f6fcbb63 | 381 | } |
c801d85f | 382 | |
bbe0af5b RR |
383 | return TRUE; |
384 | } | |
385 | ||
60acb947 | 386 | bool wxApp::ProcessIdle() |
53010e52 | 387 | { |
ec758a20 RR |
388 | wxIdleEvent event; |
389 | event.SetEventObject( this ); | |
390 | ProcessEvent( event ); | |
0cf2cb36 | 391 | |
ec758a20 | 392 | return event.MoreRequested(); |
ff7b1510 | 393 | } |
53010e52 RR |
394 | |
395 | void wxApp::OnIdle( wxIdleEvent &event ) | |
c801d85f | 396 | { |
ec758a20 | 397 | static bool inOnIdle = FALSE; |
53010e52 | 398 | |
d524867f | 399 | /* Avoid recursion (via ProcessEvent default case) */ |
ec758a20 RR |
400 | if (inOnIdle) |
401 | return; | |
53010e52 | 402 | |
ec758a20 | 403 | inOnIdle = TRUE; |
53010e52 | 404 | |
d345e841 | 405 | #if wxUSE_THREADS |
7214297d GL |
406 | /* Resend in the main thread events which have been prepared in other |
407 | threads */ | |
408 | ProcessPendingEvents(); | |
d345e841 | 409 | #endif |
7214297d | 410 | |
d524867f | 411 | /* 'Garbage' collection of windows deleted with Close(). */ |
ec758a20 | 412 | DeletePendingObjects(); |
53010e52 | 413 | |
d524867f | 414 | /* flush the logged messages if any */ |
88ac883a | 415 | #if wxUSE_LOG |
d524867f RR |
416 | wxLog *log = wxLog::GetActiveTarget(); |
417 | if (log != NULL && log->HasPendingMessages()) | |
418 | log->Flush(); | |
88ac883a | 419 | #endif // wxUSE_LOG |
53010e52 | 420 | |
d524867f | 421 | /* Send OnIdle events to all windows */ |
ec758a20 | 422 | bool needMore = SendIdleEvents(); |
53010e52 | 423 | |
ec758a20 RR |
424 | if (needMore) |
425 | event.RequestMore(TRUE); | |
53010e52 | 426 | |
ec758a20 | 427 | inOnIdle = FALSE; |
ff7b1510 | 428 | } |
53010e52 | 429 | |
60acb947 | 430 | bool wxApp::SendIdleEvents() |
53010e52 RR |
431 | { |
432 | bool needMore = FALSE; | |
e0253070 | 433 | |
e146b8c8 | 434 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); |
ec758a20 RR |
435 | while (node) |
436 | { | |
e146b8c8 | 437 | wxWindow* win = node->GetData(); |
f3855ef0 | 438 | if (SendIdleEvents(win)) |
53010e52 | 439 | needMore = TRUE; |
e146b8c8 | 440 | node = node->GetNext(); |
ec758a20 | 441 | } |
e146b8c8 | 442 | |
53010e52 | 443 | return needMore; |
ff7b1510 | 444 | } |
53010e52 RR |
445 | |
446 | bool wxApp::SendIdleEvents( wxWindow* win ) | |
447 | { | |
448 | bool needMore = FALSE; | |
449 | ||
8bbe427f VZ |
450 | wxIdleEvent event; |
451 | event.SetEventObject(win); | |
60acb947 | 452 | |
9390a202 | 453 | win->OnInternalIdle(); |
60acb947 | 454 | |
8bbe427f | 455 | win->ProcessEvent(event); |
53010e52 RR |
456 | |
457 | if (event.MoreRequested()) | |
458 | needMore = TRUE; | |
459 | ||
8bbe427f VZ |
460 | wxNode* node = win->GetChildren().First(); |
461 | while (node) | |
462 | { | |
463 | wxWindow* win = (wxWindow*) node->Data(); | |
464 | if (SendIdleEvents(win)) | |
53010e52 RR |
465 | needMore = TRUE; |
466 | ||
8bbe427f VZ |
467 | node = node->Next(); |
468 | } | |
53010e52 | 469 | return needMore ; |
ff7b1510 | 470 | } |
c801d85f | 471 | |
60acb947 | 472 | int wxApp::MainLoop() |
c801d85f | 473 | { |
ec758a20 RR |
474 | gtk_main(); |
475 | return 0; | |
ff7b1510 | 476 | } |
c801d85f | 477 | |
60acb947 | 478 | void wxApp::ExitMainLoop() |
c801d85f | 479 | { |
ec758a20 | 480 | gtk_main_quit(); |
ff7b1510 | 481 | } |
c801d85f | 482 | |
60acb947 | 483 | bool wxApp::Initialized() |
c801d85f | 484 | { |
ec758a20 | 485 | return m_initialized; |
ff7b1510 | 486 | } |
c801d85f | 487 | |
60acb947 | 488 | bool wxApp::Pending() |
c801d85f | 489 | { |
acfd422a | 490 | return (gtk_events_pending() > 0); |
ff7b1510 | 491 | } |
c801d85f | 492 | |
60acb947 | 493 | void wxApp::Dispatch() |
c801d85f | 494 | { |
8801832d | 495 | gtk_main_iteration(); |
ff7b1510 | 496 | } |
c801d85f | 497 | |
7214297d GL |
498 | #if wxUSE_THREADS |
499 | void wxApp::ProcessPendingEvents() | |
500 | { | |
4d3a259a GL |
501 | wxNode *node = wxPendingEvents->First(); |
502 | wxCriticalSectionLocker locker(*wxPendingEventsLocker); | |
7214297d GL |
503 | |
504 | while (node) | |
505 | { | |
506 | wxEvtHandler *handler = (wxEvtHandler *)node->Data(); | |
507 | ||
508 | handler->ProcessPendingEvents(); | |
e146b8c8 | 509 | |
7214297d GL |
510 | delete node; |
511 | ||
4d3a259a | 512 | node = wxPendingEvents->First(); |
7214297d GL |
513 | } |
514 | } | |
8801832d | 515 | #endif // wxUSE_THREADS |
7214297d | 516 | |
60acb947 | 517 | void wxApp::DeletePendingObjects() |
c801d85f | 518 | { |
ec758a20 RR |
519 | wxNode *node = wxPendingDelete.First(); |
520 | while (node) | |
521 | { | |
522 | wxObject *obj = (wxObject *)node->Data(); | |
0cf2cb36 | 523 | |
ec758a20 | 524 | delete obj; |
c801d85f | 525 | |
f03fc89f VZ |
526 | if (wxPendingDelete.Find(obj)) |
527 | delete node; | |
c801d85f | 528 | |
ec758a20 RR |
529 | node = wxPendingDelete.First(); |
530 | } | |
ff7b1510 | 531 | } |
c801d85f | 532 | |
60acb947 | 533 | wxWindow *wxApp::GetTopWindow() |
c801d85f | 534 | { |
e146b8c8 VZ |
535 | if (m_topWindow) |
536 | return m_topWindow; | |
537 | else if (wxTopLevelWindows.GetCount() > 0) | |
538 | return wxTopLevelWindows.GetFirst()->GetData(); | |
539 | else | |
540 | return NULL; | |
ff7b1510 | 541 | } |
c801d85f KB |
542 | |
543 | void wxApp::SetTopWindow( wxWindow *win ) | |
544 | { | |
ec758a20 | 545 | m_topWindow = win; |
ff7b1510 | 546 | } |
c801d85f | 547 | |
60acb947 | 548 | bool wxApp::Initialize() |
c801d85f | 549 | { |
9cc7a35d | 550 | wxBuffer = new wxChar[BUFSIZ + 512]; |
0d2a2b60 RR |
551 | |
552 | wxClassInfo::InitializeClasses(); | |
60acb947 | 553 | |
0d2a2b60 | 554 | wxSystemSettings::Init(); |
60acb947 | 555 | |
4d3a259a GL |
556 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
557 | // create a module for that as it's part of the core. | |
558 | #if wxUSE_THREADS | |
559 | wxPendingEvents = new wxList(); | |
560 | wxPendingEventsLocker = new wxCriticalSection(); | |
561 | #endif | |
562 | ||
36b3b54a | 563 | /* |
0d2a2b60 RR |
564 | wxTheFontNameDirectory = new wxFontNameDirectory; |
565 | wxTheFontNameDirectory->Initialize(); | |
36b3b54a | 566 | */ |
c801d85f | 567 | |
0d2a2b60 RR |
568 | wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING ); |
569 | wxTheColourDatabase->Initialize(); | |
a3622daa | 570 | |
0d2a2b60 RR |
571 | wxInitializeStockLists(); |
572 | wxInitializeStockObjects(); | |
c801d85f | 573 | |
06cfab17 | 574 | #if wxUSE_WX_RESOURCES |
0d2a2b60 | 575 | wxTheResourceCache = new wxResourceCache( wxKEY_STRING ); |
60acb947 | 576 | |
0d2a2b60 | 577 | wxInitializeResourceSystem(); |
f5abe911 | 578 | #endif |
0cf2cb36 | 579 | |
0d2a2b60 | 580 | wxImage::InitStandardHandlers(); |
e0253070 | 581 | |
0d2a2b60 RR |
582 | wxModule::RegisterModules(); |
583 | if (!wxModule::InitializeModules()) return FALSE; | |
60acb947 | 584 | |
0d2a2b60 | 585 | return TRUE; |
ff7b1510 | 586 | } |
c801d85f | 587 | |
60acb947 | 588 | void wxApp::CleanUp() |
c801d85f | 589 | { |
0d2a2b60 | 590 | wxModule::CleanUpModules(); |
0cf2cb36 | 591 | |
06cfab17 | 592 | #if wxUSE_WX_RESOURCES |
ec758a20 | 593 | wxFlushResources(); |
a3622daa | 594 | |
60acb947 VZ |
595 | if (wxTheResourceCache) |
596 | delete wxTheResourceCache; | |
bbe0af5b | 597 | wxTheResourceCache = (wxResourceCache*) NULL; |
60acb947 | 598 | |
f5abe911 RR |
599 | wxCleanUpResourceSystem(); |
600 | #endif | |
a3622daa | 601 | |
60acb947 VZ |
602 | if (wxTheColourDatabase) |
603 | delete wxTheColourDatabase; | |
0d2a2b60 | 604 | wxTheColourDatabase = (wxColourDatabase*) NULL; |
60acb947 | 605 | |
36b3b54a | 606 | /* |
0d2a2b60 RR |
607 | if (wxTheFontNameDirectory) delete wxTheFontNameDirectory; |
608 | wxTheFontNameDirectory = (wxFontNameDirectory*) NULL; | |
36b3b54a | 609 | */ |
60acb947 | 610 | |
0d2a2b60 RR |
611 | wxDeleteStockObjects(); |
612 | ||
ec758a20 | 613 | wxDeleteStockLists(); |
a3622daa | 614 | |
ec758a20 | 615 | wxImage::CleanUpHandlers(); |
0cf2cb36 | 616 | |
0d2a2b60 RR |
617 | delete wxTheApp; |
618 | wxTheApp = (wxApp*) NULL; | |
619 | ||
4d3a259a GL |
620 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
621 | // create a module for that as it's part of the core. | |
622 | #if wxUSE_THREADS | |
623 | delete wxPendingEvents; | |
624 | delete wxPendingEventsLocker; | |
625 | #endif | |
626 | ||
3e61c765 | 627 | wxSystemSettings::Done(); |
60acb947 | 628 | |
3e61c765 RR |
629 | delete[] wxBuffer; |
630 | ||
631 | wxClassInfo::CleanUpClasses(); | |
60acb947 VZ |
632 | |
633 | // check for memory leaks | |
0d2a2b60 RR |
634 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
635 | if (wxDebugContext::CountObjectsLeft() > 0) | |
636 | { | |
9cc7a35d | 637 | wxLogDebug(_T("There were memory leaks.\n")); |
0d2a2b60 RR |
638 | wxDebugContext::Dump(); |
639 | wxDebugContext::PrintStatistics(); | |
640 | } | |
8801832d | 641 | #endif // Debug |
0d2a2b60 | 642 | |
88ac883a | 643 | #if wxUSE_LOG |
60acb947 | 644 | // do this as the very last thing because everything else can log messages |
0d2a2b60 | 645 | wxLog::DontCreateOnDemand(); |
60acb947 | 646 | |
0d2a2b60 | 647 | wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL ); |
60acb947 VZ |
648 | if (oldLog) |
649 | delete oldLog; | |
ff7b1510 | 650 | } |
0cf2cb36 | 651 | |
c801d85f KB |
652 | wxLog *wxApp::CreateLogTarget() |
653 | { | |
0d2a2b60 | 654 | return new wxLogGui; |
c801d85f | 655 | } |
88ac883a | 656 | #endif // wxUSE_LOG |
c801d85f KB |
657 | |
658 | //----------------------------------------------------------------------------- | |
659 | // wxEntry | |
660 | //----------------------------------------------------------------------------- | |
661 | ||
662 | int wxEntry( int argc, char *argv[] ) | |
663 | { | |
0d2a2b60 | 664 | gtk_set_locale(); |
c801d85f | 665 | |
dcf924a3 | 666 | if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; |
002f4218 | 667 | |
0d2a2b60 | 668 | gtk_init( &argc, &argv ); |
0cf2cb36 | 669 | |
f0492f7d RR |
670 | wxSetDetectableAutoRepeat( TRUE ); |
671 | ||
60acb947 VZ |
672 | if (!wxApp::Initialize()) |
673 | return -1; | |
0cf2cb36 | 674 | |
ec758a20 | 675 | if (!wxTheApp) |
c801d85f | 676 | { |
60acb947 | 677 | wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, |
9cc7a35d | 678 | _T("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); |
0cf2cb36 | 679 | |
ec758a20 | 680 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); |
0cf2cb36 | 681 | |
ec758a20 | 682 | wxObject *test_app = app_ini(); |
0cf2cb36 | 683 | |
ec758a20 RR |
684 | wxTheApp = (wxApp*) test_app; |
685 | } | |
0cf2cb36 | 686 | |
9cc7a35d | 687 | wxCHECK_MSG( wxTheApp, -1, _T("wxWindows error: no application object") ); |
c801d85f | 688 | |
ec758a20 RR |
689 | wxTheApp->argc = argc; |
690 | wxTheApp->argv = argv; | |
0cf2cb36 | 691 | |
8801832d | 692 | wxString name(wxFileNameFromPath(argv[0])); |
ec758a20 RR |
693 | wxStripExtension( name ); |
694 | wxTheApp->SetAppName( name ); | |
e0253070 | 695 | |
0151c3eb VZ |
696 | int retValue = 0; |
697 | ||
698 | if ( !wxTheApp->OnInitGui() ) | |
699 | retValue = -1; | |
c801d85f | 700 | |
8801832d VZ |
701 | // Here frames insert themselves automatically into wxTopLevelWindows by |
702 | // getting created in OnInit(). | |
0151c3eb VZ |
703 | if ( retValue == 0 ) |
704 | { | |
705 | if ( !wxTheApp->OnInit() ) | |
706 | retValue = -1; | |
707 | } | |
0cf2cb36 | 708 | |
0151c3eb VZ |
709 | if ( retValue == 0 ) |
710 | { | |
cfb50f14 RR |
711 | /* delete pending toplevel windows (typically a single |
712 | dialog) so that, if there isn't any left, we don't | |
713 | call OnRun() */ | |
714 | wxTheApp->DeletePendingObjects(); | |
715 | ||
0151c3eb | 716 | wxTheApp->m_initialized = wxTopLevelWindows.GetCount() != 0; |
0cf2cb36 | 717 | |
0151c3eb | 718 | if (wxTheApp->Initialized()) |
cfb50f14 | 719 | { |
0151c3eb | 720 | retValue = wxTheApp->OnRun(); |
0cf2cb36 | 721 | |
cfb50f14 RR |
722 | wxWindow *topWindow = wxTheApp->GetTopWindow(); |
723 | if (topWindow) | |
0151c3eb | 724 | { |
cfb50f14 RR |
725 | /* Forcibly delete the window. */ |
726 | if (topWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
727 | topWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
728 | { | |
729 | topWindow->Close( TRUE ); | |
730 | wxTheApp->DeletePendingObjects(); | |
731 | } | |
732 | else | |
733 | { | |
734 | delete topWindow; | |
735 | wxTheApp->SetTopWindow( (wxWindow*) NULL ); | |
736 | } | |
737 | } | |
738 | wxTheApp->OnExit(); | |
0d2a2b60 | 739 | } |
0151c3eb | 740 | } |
0cf2cb36 | 741 | |
88ac883a | 742 | #if wxUSE_LOG |
60acb947 | 743 | // flush the logged messages if any |
0d2a2b60 RR |
744 | wxLog *log = wxLog::GetActiveTarget(); |
745 | if (log != NULL && log->HasPendingMessages()) | |
746 | log->Flush(); | |
747 | ||
60acb947 VZ |
748 | // continuing to use user defined log target is unsafe from now on because |
749 | // some resources may be already unavailable, so replace it by something | |
750 | // more safe | |
751 | wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr); | |
752 | if ( oldlog ) | |
753 | delete oldlog; | |
88ac883a | 754 | #endif // wxUSE_LOG |
60acb947 | 755 | |
0d2a2b60 | 756 | wxApp::CleanUp(); |
184b5d99 | 757 | |
ec758a20 | 758 | return retValue; |
ff7b1510 | 759 | } |
1a56f55c | 760 |