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