]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
Compilation fixes
[wxWidgets.git] / src / gtk / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "app.h"
12 #endif
13
14 #include "wx/app.h"
15 #include "wx/gdicmn.h"
16 #include "wx/utils.h"
17 #include "wx/intl.h"
18 #include "wx/log.h"
19 #include "wx/memory.h"
20 #include "wx/font.h"
21 #include "wx/settings.h"
22 #if wxUSE_WX_RESOURCES
23 #include "wx/resource.h"
24 #endif
25 #include "wx/module.h"
26 #include "wx/image.h"
27 #if wxUSE_THREADS
28 #include "wx/thread.h"
29 #endif
30 #include "unistd.h"
31
32 #include "glib.h"
33 #include "gdk/gdk.h"
34 #include "gtk/gtk.h"
35 #include "wx/gtk/win_gtk.h"
36
37 //-----------------------------------------------------------------------------
38 // global data
39 //-----------------------------------------------------------------------------
40
41 wxApp *wxTheApp = (wxApp *) NULL;
42 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
43
44 extern wxList wxPendingDelete;
45 extern wxResourceCache *wxTheResourceCache;
46
47 unsigned char g_palette[64*3] =
48 {
49 0x0, 0x0, 0x0,
50 0xff, 0xff, 0xff,
51 0xff, 0x0, 0x0,
52 0xff, 0xff, 0x0,
53 0x0, 0xff, 0x0,
54 0x0, 0x0, 0xff,
55 0x0, 0xff, 0xff,
56 0x99, 0x99, 0x99,
57 0xff, 0x88, 0x0,
58 0x88, 0x0, 0x0,
59 0x0, 0x88, 0x88,
60 0x88, 0x88, 0x0,
61 0xff, 0xcc, 0x97,
62 0xbb, 0xbb, 0xbb,
63 0x9f, 0x6b, 0x42,
64 0x55, 0x55, 0x55,
65 0xdd, 0xdd, 0xdd,
66 0x77, 0x77, 0x77,
67 0x33, 0x33, 0x33,
68 0xcc, 0x0, 0x0,
69 0xff, 0x44, 0x0,
70 0xff, 0xcc, 0x0,
71 0xcc, 0xcc, 0x0,
72 0x60, 0x60, 0x0,
73 0x0, 0x43, 0x0,
74 0x0, 0x7f, 0x0,
75 0x0, 0xcc, 0x0,
76 0x0, 0x44, 0x44,
77 0x0, 0x0, 0x44,
78 0x0, 0x0, 0x88,
79 0xef, 0xb1, 0x7b,
80 0xdf, 0x98, 0x5f,
81 0xbf, 0x87, 0x56,
82 0x7f, 0x57, 0x26,
83 0x5f, 0x39, 0xc,
84 0x3f, 0x1c, 0x0,
85 0x21, 0x0, 0x0,
86 0x0, 0x43, 0x87,
87 0x2d, 0x70, 0xaf,
88 0x5a, 0x9e, 0xd7,
89 0x87, 0xcc, 0xff,
90 0xff, 0xe0, 0xba,
91 0x21, 0x43, 0xf,
92 0x3d, 0x5d, 0x25,
93 0x59, 0x78, 0x3a,
94 0x75, 0x93, 0x4f,
95 0x91, 0xae, 0x64,
96 0xad, 0xc8, 0x7a,
97 0xf0, 0xa8, 0xef,
98 0xd0, 0x88, 0xd0,
99 0xaf, 0x66, 0xaf,
100 0x8e, 0x44, 0x8e,
101 0x6d, 0x22, 0x6d,
102 0x4b, 0x0, 0x4b,
103 0xff, 0xc0, 0xbc,
104 0xff, 0x93, 0x91,
105 0xff, 0x66, 0x67,
106 0xd8, 0xf2, 0xbf,
107 0xff, 0xc9, 0x68,
108 0xff, 0x96, 0x67,
109 0xa5, 0x60, 0xff,
110 0x51, 0xff, 0x99,
111 0x3f, 0xa5, 0x63,
112 0x98, 0x90, 0x67
113 };
114
115 //-----------------------------------------------------------------------------
116 // local functions
117 //-----------------------------------------------------------------------------
118
119 extern void wxFlushResources(void);
120
121 //-----------------------------------------------------------------------------
122 // global functions
123 //-----------------------------------------------------------------------------
124
125 void wxExit(void)
126 {
127 gtk_main_quit();
128 }
129
130 bool wxYield(void)
131 {
132 while (gtk_events_pending() > 0) gtk_main_iteration();
133 return TRUE;
134 }
135
136 //-----------------------------------------------------------------------------
137 // wxApp
138 //-----------------------------------------------------------------------------
139
140 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
141
142 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
143 EVT_IDLE(wxApp::OnIdle)
144 END_EVENT_TABLE()
145
146 gint wxapp_idle_callback( gpointer WXUNUSED(data) )
147 {
148 if (wxTheApp) while (wxTheApp->ProcessIdle()) {}
149 #if wxUSE_THREADS
150 wxMutexGuiLeave();
151 #endif
152 usleep(10000);
153 #if wxUSE_THREADS
154 wxMutexGuiEnter();
155 #endif
156 return TRUE;
157 }
158
159 wxApp::wxApp()
160 {
161 m_idleTag = 0;
162 m_topWindow = (wxWindow *) NULL;
163 m_exitOnFrameDelete = TRUE;
164 wxTheApp = this;
165 }
166
167 wxApp::~wxApp(void)
168 {
169 gtk_idle_remove( m_idleTag );
170 }
171
172 bool wxApp::OnInit(void)
173 {
174 return TRUE;
175 }
176
177 bool wxApp::OnInitGui(void)
178 {
179 m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL );
180 return TRUE;
181 }
182
183 int wxApp::OnRun(void)
184 {
185 return MainLoop();
186 }
187
188 bool wxApp::ProcessIdle(void)
189 {
190 wxIdleEvent event;
191 event.SetEventObject( this );
192 ProcessEvent( event );
193
194 return event.MoreRequested();
195 }
196
197 void wxApp::OnIdle( wxIdleEvent &event )
198 {
199 static bool inOnIdle = FALSE;
200
201 /* Avoid recursion (via ProcessEvent default case) */
202 if (inOnIdle)
203 return;
204
205 inOnIdle = TRUE;
206
207 /* 'Garbage' collection of windows deleted with Close(). */
208 DeletePendingObjects();
209
210 /* flush the logged messages if any */
211 wxLog *log = wxLog::GetActiveTarget();
212 if (log != NULL && log->HasPendingMessages())
213 log->Flush();
214
215 /* Send OnIdle events to all windows */
216 bool needMore = SendIdleEvents();
217
218 if (needMore)
219 event.RequestMore(TRUE);
220
221 inOnIdle = FALSE;
222 }
223
224 bool wxApp::SendIdleEvents(void)
225 {
226 bool needMore = FALSE;
227
228 wxNode* node = wxTopLevelWindows.First();
229 while (node)
230 {
231 wxWindow* win = (wxWindow*) node->Data();
232 if (SendIdleEvents(win))
233 needMore = TRUE;
234 node = node->Next();
235 }
236 return needMore;
237 }
238
239 bool wxApp::SendIdleEvents( wxWindow* win )
240 {
241 bool needMore = FALSE;
242
243 wxIdleEvent event;
244 event.SetEventObject(win);
245 win->ProcessEvent(event);
246
247 if (event.MoreRequested())
248 needMore = TRUE;
249
250 wxNode* node = win->GetChildren().First();
251 while (node)
252 {
253 wxWindow* win = (wxWindow*) node->Data();
254 if (SendIdleEvents(win))
255 needMore = TRUE;
256
257 node = node->Next();
258 }
259 return needMore ;
260 }
261
262 int wxApp::OnExit(void)
263 {
264 return 0;
265 }
266
267 int wxApp::MainLoop(void)
268 {
269 gtk_main();
270 return 0;
271 }
272
273 void wxApp::ExitMainLoop(void)
274 {
275 gtk_main_quit();
276 }
277
278 bool wxApp::Initialized(void)
279 {
280 return m_initialized;
281 }
282
283 bool wxApp::Pending(void)
284 {
285 return FALSE;
286 }
287
288 void wxApp::Dispatch(void)
289 {
290 }
291
292 void wxApp::DeletePendingObjects(void)
293 {
294 wxNode *node = wxPendingDelete.First();
295 while (node)
296 {
297 wxObject *obj = (wxObject *)node->Data();
298
299 delete obj;
300
301 if (wxPendingDelete.Member(obj))
302 delete node;
303
304 node = wxPendingDelete.First();
305 }
306 }
307
308 wxWindow *wxApp::GetTopWindow(void)
309 {
310 if (m_topWindow) return m_topWindow;
311 wxNode *node = wxTopLevelWindows.First();
312 if (!node) return (wxWindow *) NULL;
313 return (wxWindow*)node->Data();
314 }
315
316 void wxApp::SetTopWindow( wxWindow *win )
317 {
318 m_topWindow = win;
319 }
320
321 void wxApp::CommonInit(void)
322 {
323
324 /*
325 #if wxUSE_RESOURCES
326 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
327 #endif
328 */
329 wxSystemSettings::Init();
330
331 wxTheFontNameDirectory = new wxFontNameDirectory;
332 wxTheFontNameDirectory->Initialize();
333
334 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
335 wxTheColourDatabase->Initialize();
336
337 wxInitializeStockLists();
338 wxInitializeStockObjects();
339
340 #if wxUSE_WX_RESOURCES
341 wxTheResourceCache = new wxResourceCache(wxKEY_STRING);
342
343 wxInitializeResourceSystem();
344 #endif
345
346 wxImage::InitStandardHandlers();
347
348 // g_globalCursor = new wxCursor;
349 }
350
351 void wxApp::CommonCleanUp(void)
352 {
353 wxDELETE(wxTheColourDatabase);
354 wxDELETE(wxTheFontNameDirectory);
355 wxDeleteStockObjects();
356
357 #if wxUSE_WX_RESOURCES
358 wxFlushResources();
359
360 wxDELETE(wxTheResourceCache);
361
362 wxCleanUpResourceSystem();
363 #endif
364
365 wxDeleteStockLists();
366
367 wxImage::CleanUpHandlers();
368
369 wxSystemSettings::Done();
370 }
371
372 wxLog *wxApp::CreateLogTarget()
373 {
374 return new wxLogGui;
375 }
376
377 //-----------------------------------------------------------------------------
378 // wxEntry
379 //-----------------------------------------------------------------------------
380
381 int wxEntry( int argc, char *argv[] )
382 {
383 wxBuffer = new char[BUFSIZ + 512];
384
385 wxClassInfo::InitializeClasses();
386
387
388 if (!wxTheApp)
389 {
390 if (!wxApp::GetInitializerFunction())
391 {
392 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
393 return 0;
394 }
395
396 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
397
398 wxObject *test_app = app_ini();
399
400 wxTheApp = (wxApp*) test_app;
401 }
402
403 if (!wxTheApp)
404 {
405 printf( "wxWindows error: wxTheApp == NULL\n" );
406 return 0;
407 }
408
409 wxTheApp->argc = argc;
410 wxTheApp->argv = argv;
411
412 char name[200];
413 strcpy( name, argv[0] );
414 strcpy( name, wxFileNameFromPath(name) );
415 wxStripExtension( name );
416 wxTheApp->SetAppName( name );
417
418 gtk_set_locale();
419
420 gtk_init( &argc, &argv );
421
422 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
423
424 for (int i = 0; i < 64; i++)
425 {
426 GdkColor col;
427 col.red = g_palette[i*3 + 0] << 8;
428 col.green = g_palette[i*3 + 1] << 8;
429 col.blue = g_palette[i*3 + 2] << 8;
430 col.pixel = 0;
431
432 gdk_color_alloc( cmap, &col );
433 }
434
435 gtk_widget_push_colormap( cmap );
436
437 gtk_widget_set_default_colormap( cmap );
438
439 wxApp::CommonInit();
440
441 wxModule::RegisterModules();
442 if (!wxModule::InitializeModules()) return FALSE;
443
444 wxTheApp->OnInitGui();
445
446 // Here frames insert themselves automatically
447 // into wxTopLevelWindows by getting created
448 // in OnInit().
449
450 if (!wxTheApp->OnInit()) return 0;
451
452 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
453
454 int retValue = 0;
455
456 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
457
458 wxTheApp->DeletePendingObjects();
459
460 wxTheApp->OnExit();
461
462 wxModule::CleanUpModules();
463
464 wxApp::CommonCleanUp();
465
466 delete wxTheApp;
467 wxTheApp = (wxApp*) NULL;
468
469 wxClassInfo::CleanUpClasses();
470
471 delete[] wxBuffer;
472
473 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
474
475 if (wxDebugContext::CountObjectsLeft() > 0)
476 {
477 wxLogDebug("There were memory leaks.\n");
478 wxDebugContext::Dump();
479 wxDebugContext::PrintStatistics();
480 }
481
482 #endif
483
484 wxLog *oldLog = wxLog::SetActiveTarget( NULL );
485 if (oldLog) delete oldLog;
486
487 return retValue;
488 }
489
490 //-----------------------------------------------------------------------------
491