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