]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/app.cpp
Build fix in wxLog
[wxWidgets.git] / src / gtk1 / app.cpp
... / ...
CommitLineData
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
30wxApp *wxTheApp = (wxApp *) NULL;
31wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
32
33extern wxList wxPendingDelete;
34extern wxResourceCache *wxTheResourceCache;
35
36unsigned 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
108extern void wxFlushResources(void);
109
110//-----------------------------------------------------------------------------
111// global functions
112//-----------------------------------------------------------------------------
113
114void wxExit(void)
115{
116 gtk_main_quit();
117}
118
119bool wxYield(void)
120{
121 while (gtk_events_pending() > 0) gtk_main_iteration();
122 return TRUE;
123}
124
125//-----------------------------------------------------------------------------
126// wxApp
127//-----------------------------------------------------------------------------
128
129IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
130
131BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
132 EVT_IDLE(wxApp::OnIdle)
133END_EVENT_TABLE()
134
135gint wxapp_idle_callback( gpointer WXUNUSED(data) )
136{
137 if (wxTheApp) while (wxTheApp->ProcessIdle()) {}
138 usleep( 10000 );
139 return TRUE;
140}
141
142wxApp::wxApp()
143{
144 m_idleTag = 0;
145 m_topWindow = (wxWindow *) NULL;
146 m_exitOnFrameDelete = TRUE;
147 wxTheApp = this;
148}
149
150wxApp::~wxApp(void)
151{
152 gtk_idle_remove( m_idleTag );
153}
154
155bool wxApp::OnInit(void)
156{
157 return TRUE;
158}
159
160bool wxApp::OnInitGui(void)
161{
162 m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL );
163 return TRUE;
164}
165
166int wxApp::OnRun(void)
167{
168 return MainLoop();
169}
170
171bool wxApp::ProcessIdle(void)
172{
173 wxIdleEvent event;
174 event.SetEventObject( this );
175 ProcessEvent( event );
176
177 return event.MoreRequested();
178}
179
180void 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
207bool 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
222bool 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
245int wxApp::OnExit(void)
246{
247 return 0;
248}
249
250int wxApp::MainLoop(void)
251{
252 gtk_main();
253 return 0;
254}
255
256void wxApp::ExitMainLoop(void)
257{
258 gtk_main_quit();
259}
260
261bool wxApp::Initialized(void)
262{
263 return m_initialized;
264}
265
266bool wxApp::Pending(void)
267{
268 return FALSE;
269}
270
271void wxApp::Dispatch(void)
272{
273}
274
275void 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
291wxWindow *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
299void wxApp::SetTopWindow( wxWindow *win )
300{
301 m_topWindow = win;
302}
303
304void 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
331void 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
350wxLog *wxApp::CreateLogTarget()
351{
352 return new wxLogGui;
353}
354
355//-----------------------------------------------------------------------------
356// wxEntry
357//-----------------------------------------------------------------------------
358
359int wxEntry( int argc, char *argv[] )
360{
361 wxBuffer = new char[BUFSIZ + 512];
362
363 wxClassInfo::InitializeClasses();
364
365 /* Debug stream no longer used
366#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
367
368 streambuf* sBuf = new wxDebugStreamBuf;
369 ostream* oStr = new ostream(sBuf) ;
370 wxDebugContext::SetStream(oStr, sBuf);
371#endif
372*/
373
374 if (!wxTheApp)
375 {
376 if (!wxApp::GetInitializerFunction())
377 {
378 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
379 return 0;
380 }
381
382 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
383
384 wxObject *test_app = app_ini();
385
386 wxTheApp = (wxApp*) test_app;
387 }
388
389 if (!wxTheApp)
390 {
391 printf( "wxWindows error: wxTheApp == NULL\n" );
392 return 0;
393 }
394
395 wxTheApp->argc = argc;
396 wxTheApp->argv = argv;
397
398 char name[200];
399 strcpy( name, argv[0] );
400 strcpy( name, wxFileNameFromPath(name) );
401 wxStripExtension( name );
402 wxTheApp->SetAppName( name );
403
404 gtk_set_locale();
405
406 gtk_init( &argc, &argv );
407
408 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
409
410 for (int i = 0; i < 64; i++)
411 {
412 GdkColor col;
413 col.red = g_palette[i*3 + 0] << 8;
414 col.green = g_palette[i*3 + 1] << 8;
415 col.blue = g_palette[i*3 + 2] << 8;
416 col.pixel = 0;
417
418 gdk_color_alloc( cmap, &col );
419 }
420
421 gtk_widget_push_colormap( cmap );
422
423 gtk_widget_set_default_colormap( cmap );
424
425 wxApp::CommonInit();
426
427 wxTheApp->OnInitGui();
428
429 // Here frames insert themselves automatically
430 // into wxTopLevelWindows by getting created
431 // in OnInit().
432
433 if (!wxTheApp->OnInit()) return 0;
434
435 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
436
437 int retValue = 0;
438
439 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
440
441 wxTheApp->DeletePendingObjects();
442
443 wxTheApp->OnExit();
444
445 wxApp::CommonCleanUp();
446
447 wxDELETE(wxTheApp);
448
449 wxClassInfo::CleanUpClasses();
450
451 delete[] wxBuffer;
452
453#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
454
455 if (wxDebugContext::CountObjectsLeft() > 0)
456 {
457 wxLogDebug("There were memory leaks.\n");
458 wxDebugContext::Dump();
459 wxDebugContext::PrintStatistics();
460 }
461// wxDebugContext::SetStream(NULL, NULL);
462
463#endif
464
465 wxLog *oldLog = wxLog::SetActiveTarget( NULL );
466 if (oldLog) delete oldLog;
467
468
469 return retValue;
470}
471
472//-----------------------------------------------------------------------------
473// main()
474//-----------------------------------------------------------------------------
475
476#if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
477
478 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
479
480#else
481
482 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
483
484#endif
485
486