]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/app.cpp
Fixed a few compile things
[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
28#ifdef USE_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/*
246#if USE_RESOURCES
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
KB
264 // For PostScript printing
265#if USE_POSTSCRIPT
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
46dc76ba
RR
313#if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
314
315#if !defined(_WINDLL)
316 streambuf* sBuf = new wxDebugStreamBuf;
317#else
318 streambuf* sBuf = NULL;
319#endif
320 ostream* oStr = new ostream(sBuf) ;
321 wxDebugContext::SetStream(oStr, sBuf);
322
323#endif
0cf2cb36 324
c801d85f
KB
325 if (!wxTheApp)
326 {
327 if (!wxApp::GetInitializerFunction())
328 {
1a5a8367 329 printf( _("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
c801d85f 330 return 0;
ff7b1510 331 }
0cf2cb36 332
c801d85f 333 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
0cf2cb36 334
c801d85f 335 wxObject *test_app = app_ini();
0cf2cb36 336
c801d85f 337 wxTheApp = (wxApp*) test_app;
ff7b1510 338 }
0cf2cb36
RD
339
340 if (!wxTheApp)
c801d85f 341 {
1a5a8367 342 printf( _("wxWindows error: wxTheApp == NULL\n") );
c801d85f 343 return 0;
ff7b1510 344 }
c801d85f 345
c801d85f
KB
346 wxTheApp->argc = argc;
347 wxTheApp->argv = argv;
0cf2cb36 348
c801d85f
KB
349 gtk_init( &argc, &argv );
350
351#ifdef USE_GDK_IMLIB
352
353 gdk_imlib_init();
0cf2cb36 354
c801d85f 355 gtk_widget_push_visual(gdk_imlib_get_visual());
0cf2cb36 356
c801d85f 357 gtk_widget_push_colormap(gdk_imlib_get_colormap());
0cf2cb36
RD
358
359#endif
360
c801d85f
KB
361 wxApp::CommonInit();
362
363 wxTheApp->OnInitGui();
364
365 // Here frames insert themselves automatically
366 // into wxTopLevelWindows by getting created
367 // in OnInit().
0cf2cb36 368
c801d85f
KB
369 if (!wxTheApp->OnInit()) return 0;
370
371 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
0cf2cb36 372
c801d85f 373 int retValue = 0;
0cf2cb36 374
c801d85f 375 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
0cf2cb36
RD
376
377 wxTheApp->DeletePendingObjects();
378
c801d85f 379 wxTheApp->OnExit();
0cf2cb36 380
c801d85f 381 wxApp::CommonCleanUp();
a3622daa
VZ
382
383 wxDELETE(wxTheApp);
0cf2cb36 384
46dc76ba
RR
385#if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
386 // At this point we want to check if there are any memory
387 // blocks that aren't part of the wxDebugContext itself,
388 // as a special case. Then when dumping we need to ignore
389 // wxDebugContext, too.
390 if (wxDebugContext::CountObjectsLeft() > 0)
391 {
392 wxTrace("There were memory leaks.\n");
393 wxDebugContext::Dump();
394 wxDebugContext::PrintStatistics();
395 }
396 wxDebugContext::SetStream(NULL, NULL);
397#endif
0cf2cb36 398
c801d85f 399 return retValue;
ff7b1510 400}
1a56f55c
RR
401
402//-----------------------------------------------------------------------------
403// main()
404//-----------------------------------------------------------------------------
405
0cf2cb36 406#if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
1a56f55c
RR
407
408 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
409
410#else
411
412 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
413
414#endif
415
416