]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp
Version 0.4 of wxPython for MSW.
[wxWidgets.git] / src / gtk1 / app.cpp
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"
21 #include "wx/memory.h"
22 #include "wx/font.h"
23 #include "wx/settings.h"
24 #include "wx/resource.h"
25
26 #include "unistd.h"
27
28 #ifdef wxUSE_GDK_IMLIB
29 #include "../gdk_imlib/gdk_imlib.h"
30 #endif
31
32 //-----------------------------------------------------------------------------
33 // global data
34 //-----------------------------------------------------------------------------
35
36 wxApp *wxTheApp = (wxApp *) NULL;
37 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
38
39 extern wxList wxPendingDelete;
40 extern wxResourceCache *wxTheResourceCache;
41
42 //-----------------------------------------------------------------------------
43 // local functions
44 //-----------------------------------------------------------------------------
45
46 extern void wxFlushResources(void);
47
48 //-----------------------------------------------------------------------------
49 // global functions
50 //-----------------------------------------------------------------------------
51
52 void wxExit(void)
53 {
54 gtk_main_quit();
55 }
56
57 bool wxYield(void)
58 {
59 while (gtk_events_pending() > 0) gtk_main_iteration();
60 return TRUE;
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxApp
65 //-----------------------------------------------------------------------------
66
67 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
68
69 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
70 EVT_IDLE(wxApp::OnIdle)
71 END_EVENT_TABLE()
72
73 gint wxapp_idle_callback( gpointer WXUNUSED(data) )
74 {
75 if (wxTheApp) while (wxTheApp->ProcessIdle()) {}
76 usleep( 10000 );
77 return TRUE;
78 }
79
80 wxApp::wxApp()
81 {
82 m_idleTag = 0;
83 m_topWindow = (wxWindow *) NULL;
84 m_exitOnFrameDelete = TRUE;
85 wxTheApp = this;
86 }
87
88 wxApp::~wxApp(void)
89 {
90 gtk_idle_remove( m_idleTag );
91 }
92
93 bool wxApp::OnInit(void)
94 {
95 return TRUE;
96 }
97
98 bool wxApp::OnInitGui(void)
99 {
100 m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL );
101 return TRUE;
102 }
103
104 int wxApp::OnRun(void)
105 {
106 return MainLoop();
107 }
108
109 bool wxApp::ProcessIdle(void)
110 {
111 wxIdleEvent event;
112 event.SetEventObject( this );
113 ProcessEvent( event );
114
115 return event.MoreRequested();
116 }
117
118 void wxApp::OnIdle( wxIdleEvent &event )
119 {
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().
129 DeletePendingObjects();
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;
143 }
144
145 bool 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;
158 }
159
160 bool 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 ;
181 }
182
183 int wxApp::OnExit(void)
184 {
185 return 0;
186 }
187
188 int wxApp::MainLoop(void)
189 {
190 gtk_main();
191 return 0;
192 }
193
194 void wxApp::ExitMainLoop(void)
195 {
196 gtk_main_quit();
197 }
198
199 bool wxApp::Initialized(void)
200 {
201 return m_initialized;
202 }
203
204 bool wxApp::Pending(void)
205 {
206 return FALSE;
207 }
208
209 void wxApp::Dispatch(void)
210 {
211 }
212
213 void wxApp::DeletePendingObjects(void)
214 {
215 wxNode *node = wxPendingDelete.First();
216 while (node)
217 {
218 wxObject *obj = (wxObject *)node->Data();
219
220 delete obj;
221
222 if (wxPendingDelete.Member(obj))
223 delete node;
224
225 node = wxPendingDelete.First();
226 }
227 }
228
229 wxWindow *wxApp::GetTopWindow(void)
230 {
231 if (m_topWindow) return m_topWindow;
232 wxNode *node = wxTopLevelWindows.First();
233 if (!node) return (wxWindow *) NULL;
234 return (wxWindow*)node->Data();
235 }
236
237 void wxApp::SetTopWindow( wxWindow *win )
238 {
239 m_topWindow = win;
240 }
241
242 void wxApp::CommonInit(void)
243 {
244
245 /*
246 #if wxUSE_RESOURCES
247 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
248 #endif
249 */
250 wxSystemSettings::Init();
251 wxTheResourceCache = new wxResourceCache(wxKEY_STRING);
252
253 wxTheFontNameDirectory = new wxFontNameDirectory;
254 wxTheFontNameDirectory->Initialize();
255
256 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
257 wxTheColourDatabase->Initialize();
258
259 wxInitializeStockLists();
260 wxInitializeStockObjects();
261
262 wxInitializeResourceSystem();
263
264 // For PostScript printing
265 #if wxUSE_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 */
277 }
278
279 void wxApp::CommonCleanUp(void)
280 {
281 wxDELETE(wxTheColourDatabase);
282 wxDELETE(wxThePrintPaperDatabase);
283 wxDELETE(wxThePrintSetupData);
284 wxDELETE(wxTheFontNameDirectory);
285 wxDeleteStockObjects();
286
287 wxFlushResources();
288
289 wxDELETE(wxTheResourceCache);
290
291 wxDeleteStockLists();
292
293 wxCleanUpResourceSystem();
294
295 wxSystemSettings::Done();
296 }
297
298 wxLog *wxApp::CreateLogTarget()
299 {
300 return new wxLogGui;
301 }
302
303 //-----------------------------------------------------------------------------
304 // wxEntry
305 //-----------------------------------------------------------------------------
306
307 int wxEntry( int argc, char *argv[] )
308 {
309 wxBuffer = new char[BUFSIZ + 512];
310
311 wxClassInfo::InitializeClasses();
312
313 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
314
315 streambuf* sBuf = new wxDebugStreamBuf;
316 ostream* oStr = new ostream(sBuf) ;
317 wxDebugContext::SetStream(oStr, sBuf);
318
319 #endif
320
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;
334 }
335
336 if (!wxTheApp)
337 {
338 printf( "wxWindows error: wxTheApp == NULL\n" );
339 return 0;
340 }
341
342 wxTheApp->argc = argc;
343 wxTheApp->argv = argv;
344
345 char name[200];
346 strcpy( name, argv[0] );
347 strcpy( name, wxFileNameFromPath(name) );
348 wxStripExtension( name );
349 wxTheApp->SetAppName( name );
350
351 gtk_set_locale();
352
353 gtk_init( &argc, &argv );
354
355 #ifdef wxUSE_GDK_IMLIB
356
357 gdk_imlib_init();
358
359 gtk_widget_push_visual(gdk_imlib_get_visual());
360
361 gtk_widget_push_colormap(gdk_imlib_get_colormap());
362
363 #endif
364
365 wxApp::CommonInit();
366
367 wxTheApp->OnInitGui();
368
369 // Here frames insert themselves automatically
370 // into wxTopLevelWindows by getting created
371 // in OnInit().
372
373 if (!wxTheApp->OnInit()) return 0;
374
375 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
376
377 int retValue = 0;
378
379 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
380
381 wxTheApp->DeletePendingObjects();
382
383 wxTheApp->OnExit();
384
385 wxApp::CommonCleanUp();
386
387 wxDELETE(wxTheApp);
388
389 wxLog *oldLog = wxLog::SetActiveTarget( NULL );
390 if (oldLog) delete oldLog;
391
392 wxClassInfo::CleanUpClasses();
393
394 delete[] wxBuffer;
395
396 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
397
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);
405
406 #endif
407
408 return retValue;
409 }
410
411 //-----------------------------------------------------------------------------
412 // main()
413 //-----------------------------------------------------------------------------
414
415 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
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