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