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