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