]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/app.cpp
couple of minor bug fixes/enhancements (interface unchanged)
[wxWidgets.git] / src / gtk1 / app.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "app.h"
13#endif
14
15#include "wx/app.h"
16#include "wx/gdicmn.h"
17#include "wx/utils.h"
18#include "wx/postscrp.h"
19#include "wx/intl.h"
20#include "wx/log.h"
46dc76ba 21#include "wx/memory.h"
a3622daa
VZ
22#include "wx/font.h"
23#include "wx/settings.h"
c801d85f
KB
24
25#include "unistd.h"
26
27#ifdef USE_GDK_IMLIB
1f0299c1 28#include "../gdk_imlib/gdk_imlib.h"
c801d85f
KB
29#endif
30
31//-----------------------------------------------------------------------------
32// global data
33//-----------------------------------------------------------------------------
34
35wxApp *wxTheApp = NULL;
36wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
37
38extern wxList wxPendingDelete;
a3622daa 39extern wxResourceCache *wxTheResourceCache;
c801d85f
KB
40
41//-----------------------------------------------------------------------------
42// local functions
43//-----------------------------------------------------------------------------
44
45extern void wxFlushResources(void);
46
47//-----------------------------------------------------------------------------
48// global functions
49//-----------------------------------------------------------------------------
50
51void wxExit(void)
52{
53 gtk_main_quit();
54};
55
56bool wxYield(void)
57{
58 while (gtk_events_pending() > 0) gtk_main_iteration();
59 return TRUE;
60};
61
62//-----------------------------------------------------------------------------
63// wxApp
64//-----------------------------------------------------------------------------
65
66IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
67
53010e52
RR
68BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
69 EVT_IDLE(wxApp::OnIdle)
70END_EVENT_TABLE()
71
c801d85f
KB
72gint wxapp_idle_callback( gpointer WXUNUSED(data) )
73{
53010e52 74 if (wxTheApp) while (wxTheApp->ProcessIdle()) {};
c801d85f
KB
75 usleep( 10000 );
76 return TRUE;
77};
78
79wxApp::wxApp()
80{
81 m_idleTag = 0;
82 m_topWindow = NULL;
83 m_exitOnFrameDelete = TRUE;
84};
85
86wxApp::~wxApp(void)
87{
88 gtk_idle_remove( m_idleTag );
89};
90
91bool wxApp::OnInit(void)
92{
93 return TRUE;
94};
95
96bool wxApp::OnInitGui(void)
97{
98 m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL );
99 return TRUE;
100};
101
102int wxApp::OnRun(void)
103{
104 return MainLoop();
105};
106
53010e52
RR
107bool wxApp::ProcessIdle(void)
108{
109 wxIdleEvent event;
110 event.SetEventObject( this );
111 ProcessEvent( event );
112
113 return event.MoreRequested();
114};
115
116void wxApp::OnIdle( wxIdleEvent &event )
c801d85f 117{
53010e52
RR
118 static bool inOnIdle = FALSE;
119
120 // Avoid recursion (via ProcessEvent default case)
121 if (inOnIdle)
122 return;
123
124 inOnIdle = TRUE;
125
126 // 'Garbage' collection of windows deleted with Close().
c801d85f 127 DeletePendingObjects();
53010e52
RR
128
129 // flush the logged messages if any
130 wxLog *pLog = wxLog::GetActiveTarget();
131 if ( pLog != NULL && pLog->HasPendingMessages() )
132 pLog->Flush();
133
134 // Send OnIdle events to all windows
135 bool needMore = SendIdleEvents();
136
137 if (needMore)
138 event.RequestMore(TRUE);
139
140 inOnIdle = FALSE;
141};
142
143bool wxApp::SendIdleEvents(void)
144{
145 bool needMore = FALSE;
146 wxNode* node = wxTopLevelWindows.First();
147 while (node)
148 {
149 wxWindow* win = (wxWindow*) node->Data();
150 if (SendIdleEvents(win))
151 needMore = TRUE;
152
153 node = node->Next();
154 }
155 return needMore;
156};
157
158bool wxApp::SendIdleEvents( wxWindow* win )
159{
160 bool needMore = FALSE;
161
162 wxIdleEvent event;
163 event.SetEventObject(win);
164 win->ProcessEvent(event);
165
166 if (event.MoreRequested())
167 needMore = TRUE;
168
169 wxNode* node = win->GetChildren()->First();
170 while (node)
171 {
172 wxWindow* win = (wxWindow*) node->Data();
173 if (SendIdleEvents(win))
174 needMore = TRUE;
175
176 node = node->Next();
177 }
178 return needMore ;
c801d85f
KB
179};
180
181int wxApp::OnExit(void)
182{
183 return 0;
184};
185
186int wxApp::MainLoop(void)
187{
188 gtk_main();
189 return 0;
190};
191
192void wxApp::ExitMainLoop(void)
193{
194 gtk_main_quit();
195};
196
197bool wxApp::Initialized(void)
198{
199 return m_initialized;
200};
201
202bool wxApp::Pending(void)
203{
204 return FALSE;
205};
206
207void wxApp::Dispatch(void)
208{
209};
210
211void wxApp::DeletePendingObjects(void)
212{
213 wxNode *node = wxPendingDelete.First();
214 while (node)
215 {
216 wxObject *obj = (wxObject *)node->Data();
217
218 delete obj;
219
220 if (wxPendingDelete.Member(obj))
221 delete node;
222
223 node = wxPendingDelete.First();
224 };
225};
226
227wxWindow *wxApp::GetTopWindow(void)
228{
229 if (m_topWindow) return m_topWindow;
230 wxNode *node = wxTopLevelWindows.First();
231 if (!node) return NULL;
232 return (wxWindow*)node->Data();
233};
234
235void wxApp::SetTopWindow( wxWindow *win )
236{
237 m_topWindow = win;
238};
239
240void wxApp::CommonInit(void)
241{
242
243/*
244#if USE_RESOURCES
245 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
246#endif
247*/
a3622daa
VZ
248 wxSystemSettings::Init();
249 wxTheResourceCache = new wxResourceCache(wxKEY_STRING);
250
251 wxTheFontNameDirectory = new wxFontNameDirectory;
252 wxTheFontNameDirectory->Initialize();
c801d85f
KB
253
254 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
255 wxTheColourDatabase->Initialize();
a3622daa
VZ
256
257 wxInitializeStockLists();
c801d85f
KB
258 wxInitializeStockObjects();
259
260 // For PostScript printing
261#if USE_POSTSCRIPT
262 wxInitializePrintSetupData();
263 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
264 wxThePrintPaperDatabase->CreateDatabase();
265#endif
266
267
268/*
269 wxBitmap::InitStandardHandlers();
270
271 g_globalCursor = new wxCursor;
272*/
273
a3622daa 274// wxInitializeStockObjects();
c801d85f
KB
275};
276
277void wxApp::CommonCleanUp(void)
278{
a3622daa
VZ
279 wxDELETE(wxTheColourDatabase);
280 wxDELETE(wxThePrintPaperDatabase);
281 wxDELETE(wxThePrintSetupData);
282 wxDELETE(wxTheFontNameDirectory);
c801d85f
KB
283 wxDeleteStockObjects();
284
285 wxFlushResources();
a3622daa
VZ
286
287 wxDELETE(wxTheResourceCache);
288
289 wxDeleteStockLists();
290
291 wxSystemSettings::Done();
c801d85f
KB
292};
293
294wxLog *wxApp::CreateLogTarget()
295{
296 return new wxLogGui;
297}
298
299//-----------------------------------------------------------------------------
300// wxEntry
301//-----------------------------------------------------------------------------
302
303int wxEntry( int argc, char *argv[] )
304{
305 wxBuffer = new char[BUFSIZ + 512];
306
307 wxClassInfo::InitializeClasses();
308
46dc76ba
RR
309#if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
310
311#if !defined(_WINDLL)
312 streambuf* sBuf = new wxDebugStreamBuf;
313#else
314 streambuf* sBuf = NULL;
315#endif
316 ostream* oStr = new ostream(sBuf) ;
317 wxDebugContext::SetStream(oStr, sBuf);
318
319#endif
320
c801d85f
KB
321 if (!wxTheApp)
322 {
323 if (!wxApp::GetInitializerFunction())
324 {
325 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
326 return 0;
327 };
328
329 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
330
331 wxObject *test_app = app_ini();
332
333 wxTheApp = (wxApp*) test_app;
c801d85f
KB
334 };
335
336 if (!wxTheApp)
337 {
338 printf( "wxWindows error: wxTheApp == NULL\n" );
339 return 0;
340 };
341
c801d85f
KB
342 wxTheApp->argc = argc;
343 wxTheApp->argv = argv;
344
345 gtk_init( &argc, &argv );
346
347#ifdef USE_GDK_IMLIB
348
349 gdk_imlib_init();
350
351 gtk_widget_push_visual(gdk_imlib_get_visual());
352
353 gtk_widget_push_colormap(gdk_imlib_get_colormap());
354
355#endif
356
357 wxApp::CommonInit();
358
359 wxTheApp->OnInitGui();
360
361 // Here frames insert themselves automatically
362 // into wxTopLevelWindows by getting created
363 // in OnInit().
364
365 if (!wxTheApp->OnInit()) return 0;
366
367 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
368
369 int retValue = 0;
370
371 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
372
373 wxTheApp->DeletePendingObjects();
374
375 wxTheApp->OnExit();
376
377 wxApp::CommonCleanUp();
a3622daa
VZ
378
379 wxDELETE(wxTheApp);
c801d85f 380
46dc76ba
RR
381#if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
382 // At this point we want to check if there are any memory
383 // blocks that aren't part of the wxDebugContext itself,
384 // as a special case. Then when dumping we need to ignore
385 // wxDebugContext, too.
386 if (wxDebugContext::CountObjectsLeft() > 0)
387 {
388 wxTrace("There were memory leaks.\n");
389 wxDebugContext::Dump();
390 wxDebugContext::PrintStatistics();
391 }
392 wxDebugContext::SetStream(NULL, NULL);
393#endif
394
c801d85f
KB
395 return retValue;
396};
1a56f55c
RR
397
398//-----------------------------------------------------------------------------
399// main()
400//-----------------------------------------------------------------------------
401
7f4dc78d 402#if defined(AIX) || defined(AIX4) || defined(____HPUX__)
1a56f55c
RR
403
404 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
405
406#else
407
408 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
409
410#endif
411
412