]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
Resource sample works now, apart from
[wxWidgets.git] / src / gtk / 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 USE_GDK_IMLIB
29 #include "../gdk_imlib/gdk_imlib.h"
30 #endif
31
32 //-----------------------------------------------------------------------------
33 // global data
34 //-----------------------------------------------------------------------------
35
36 wxApp *wxTheApp = 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 = NULL;
84 m_exitOnFrameDelete = TRUE;
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 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 USE_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 USE_POSTSCRIPT
265 wxInitializePrintSetupData();
266 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
267 wxThePrintPaperDatabase->CreateDatabase();
268 #endif
269
270
271 /*
272 wxBitmap::InitStandardHandlers();
273
274 g_globalCursor = new wxCursor;
275 */
276 };
277
278 void wxApp::CommonCleanUp(void)
279 {
280 wxDELETE(wxTheColourDatabase);
281 wxDELETE(wxThePrintPaperDatabase);
282 wxDELETE(wxThePrintSetupData);
283 wxDELETE(wxTheFontNameDirectory);
284 wxDeleteStockObjects();
285
286 wxFlushResources();
287
288 wxDELETE(wxTheResourceCache);
289
290 wxDeleteStockLists();
291
292 wxCleanUpResourceSystem();
293
294 wxSystemSettings::Done();
295 };
296
297 wxLog *wxApp::CreateLogTarget()
298 {
299 return new wxLogGui;
300 }
301
302 //-----------------------------------------------------------------------------
303 // wxEntry
304 //-----------------------------------------------------------------------------
305
306 int wxEntry( int argc, char *argv[] )
307 {
308 wxBuffer = new char[BUFSIZ + 512];
309
310 wxClassInfo::InitializeClasses();
311
312 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
313
314 #if !defined(_WINDLL)
315 streambuf* sBuf = new wxDebugStreamBuf;
316 #else
317 streambuf* sBuf = NULL;
318 #endif
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 gtk_init( &argc, &argv );
349
350 #ifdef USE_GDK_IMLIB
351
352 gdk_imlib_init();
353
354 gtk_widget_push_visual(gdk_imlib_get_visual());
355
356 gtk_widget_push_colormap(gdk_imlib_get_colormap());
357
358 #endif
359
360 wxApp::CommonInit();
361
362 wxTheApp->OnInitGui();
363
364 // Here frames insert themselves automatically
365 // into wxTopLevelWindows by getting created
366 // in OnInit().
367
368 if (!wxTheApp->OnInit()) return 0;
369
370 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
371
372 int retValue = 0;
373
374 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
375
376 wxTheApp->DeletePendingObjects();
377
378 wxTheApp->OnExit();
379
380 wxApp::CommonCleanUp();
381
382 wxDELETE(wxTheApp);
383
384 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
385 // At this point we want to check if there are any memory
386 // blocks that aren't part of the wxDebugContext itself,
387 // as a special case. Then when dumping we need to ignore
388 // wxDebugContext, too.
389 if (wxDebugContext::CountObjectsLeft() > 0)
390 {
391 wxTrace("There were memory leaks.\n");
392 wxDebugContext::Dump();
393 wxDebugContext::PrintStatistics();
394 }
395 wxDebugContext::SetStream(NULL, NULL);
396 #endif
397
398 return retValue;
399 };
400
401 //-----------------------------------------------------------------------------
402 // main()
403 //-----------------------------------------------------------------------------
404
405 #if defined(AIX) || defined(AIX4) || defined(____HPUX__)
406
407 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
408
409 #else
410
411 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
412
413 #endif
414
415