]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | #include "unistd.h" | |
26 | ||
27 | #ifdef USE_GDK_IMLIB | |
28 | #include "../gdk_imlib/gdk_imlib.h" | |
29 | #endif | |
30 | ||
31 | //----------------------------------------------------------------------------- | |
32 | // global data | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | wxApp *wxTheApp = 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 = NULL; | |
83 | m_exitOnFrameDelete = TRUE; | |
84 | }; | |
85 | ||
86 | wxApp::~wxApp(void) | |
87 | { | |
88 | gtk_idle_remove( m_idleTag ); | |
89 | }; | |
90 | ||
91 | bool wxApp::OnInit(void) | |
92 | { | |
93 | return TRUE; | |
94 | }; | |
95 | ||
96 | bool wxApp::OnInitGui(void) | |
97 | { | |
98 | m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL ); | |
99 | return TRUE; | |
100 | }; | |
101 | ||
102 | int wxApp::OnRun(void) | |
103 | { | |
104 | return MainLoop(); | |
105 | }; | |
106 | ||
107 | bool wxApp::ProcessIdle(void) | |
108 | { | |
109 | wxIdleEvent event; | |
110 | event.SetEventObject( this ); | |
111 | ProcessEvent( event ); | |
112 | ||
113 | return event.MoreRequested(); | |
114 | }; | |
115 | ||
116 | void wxApp::OnIdle( wxIdleEvent &event ) | |
117 | { | |
118 | static bool inOnIdle = FALSE; | |
119 | ||
120 | // Avoid recursion (via ProcessEvent default case) | |
121 | if (inOnIdle) | |
122 | return; | |
123 | ||
124 | inOnIdle = TRUE; | |
125 | ||
126 | // 'Garbage' collection of windows deleted with Close(). | |
127 | DeletePendingObjects(); | |
128 | ||
129 | // flush the logged messages if any | |
130 | wxLog *pLog = wxLog::GetActiveTarget(); | |
131 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
132 | pLog->Flush(); | |
133 | ||
134 | // Send OnIdle events to all windows | |
135 | bool needMore = SendIdleEvents(); | |
136 | ||
137 | if (needMore) | |
138 | event.RequestMore(TRUE); | |
139 | ||
140 | inOnIdle = FALSE; | |
141 | }; | |
142 | ||
143 | bool wxApp::SendIdleEvents(void) | |
144 | { | |
145 | bool needMore = FALSE; | |
146 | wxNode* node = wxTopLevelWindows.First(); | |
147 | while (node) | |
148 | { | |
149 | wxWindow* win = (wxWindow*) node->Data(); | |
150 | if (SendIdleEvents(win)) | |
151 | needMore = TRUE; | |
152 | ||
153 | node = node->Next(); | |
154 | } | |
155 | return needMore; | |
156 | }; | |
157 | ||
158 | bool wxApp::SendIdleEvents( wxWindow* win ) | |
159 | { | |
160 | bool needMore = FALSE; | |
161 | ||
162 | wxIdleEvent event; | |
163 | event.SetEventObject(win); | |
164 | win->ProcessEvent(event); | |
165 | ||
166 | if (event.MoreRequested()) | |
167 | needMore = TRUE; | |
168 | ||
169 | wxNode* node = win->GetChildren()->First(); | |
170 | while (node) | |
171 | { | |
172 | wxWindow* win = (wxWindow*) node->Data(); | |
173 | if (SendIdleEvents(win)) | |
174 | needMore = TRUE; | |
175 | ||
176 | node = node->Next(); | |
177 | } | |
178 | return needMore ; | |
179 | }; | |
180 | ||
181 | int wxApp::OnExit(void) | |
182 | { | |
183 | return 0; | |
184 | }; | |
185 | ||
186 | int wxApp::MainLoop(void) | |
187 | { | |
188 | gtk_main(); | |
189 | return 0; | |
190 | }; | |
191 | ||
192 | void wxApp::ExitMainLoop(void) | |
193 | { | |
194 | gtk_main_quit(); | |
195 | }; | |
196 | ||
197 | bool wxApp::Initialized(void) | |
198 | { | |
199 | return m_initialized; | |
200 | }; | |
201 | ||
202 | bool wxApp::Pending(void) | |
203 | { | |
204 | return FALSE; | |
205 | }; | |
206 | ||
207 | void wxApp::Dispatch(void) | |
208 | { | |
209 | }; | |
210 | ||
211 | void wxApp::DeletePendingObjects(void) | |
212 | { | |
213 | wxNode *node = wxPendingDelete.First(); | |
214 | while (node) | |
215 | { | |
216 | wxObject *obj = (wxObject *)node->Data(); | |
217 | ||
218 | delete obj; | |
219 | ||
220 | if (wxPendingDelete.Member(obj)) | |
221 | delete node; | |
222 | ||
223 | node = wxPendingDelete.First(); | |
224 | }; | |
225 | }; | |
226 | ||
227 | wxWindow *wxApp::GetTopWindow(void) | |
228 | { | |
229 | if (m_topWindow) return m_topWindow; | |
230 | wxNode *node = wxTopLevelWindows.First(); | |
231 | if (!node) return NULL; | |
232 | return (wxWindow*)node->Data(); | |
233 | }; | |
234 | ||
235 | void wxApp::SetTopWindow( wxWindow *win ) | |
236 | { | |
237 | m_topWindow = win; | |
238 | }; | |
239 | ||
240 | void wxApp::CommonInit(void) | |
241 | { | |
242 | ||
243 | /* | |
244 | #if USE_RESOURCES | |
245 | (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion); | |
246 | #endif | |
247 | */ | |
248 | wxSystemSettings::Init(); | |
249 | wxTheResourceCache = new wxResourceCache(wxKEY_STRING); | |
250 | ||
251 | wxTheFontNameDirectory = new wxFontNameDirectory; | |
252 | wxTheFontNameDirectory->Initialize(); | |
253 | ||
254 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
255 | wxTheColourDatabase->Initialize(); | |
256 | ||
257 | wxInitializeStockLists(); | |
258 | wxInitializeStockObjects(); | |
259 | ||
260 | // For PostScript printing | |
261 | #if USE_POSTSCRIPT | |
262 | wxInitializePrintSetupData(); | |
263 | wxThePrintPaperDatabase = new wxPrintPaperDatabase; | |
264 | wxThePrintPaperDatabase->CreateDatabase(); | |
265 | #endif | |
266 | ||
267 | ||
268 | /* | |
269 | wxBitmap::InitStandardHandlers(); | |
270 | ||
271 | g_globalCursor = new wxCursor; | |
272 | */ | |
273 | ||
274 | // wxInitializeStockObjects(); | |
275 | }; | |
276 | ||
277 | void wxApp::CommonCleanUp(void) | |
278 | { | |
279 | wxDELETE(wxTheColourDatabase); | |
280 | wxDELETE(wxThePrintPaperDatabase); | |
281 | wxDELETE(wxThePrintSetupData); | |
282 | wxDELETE(wxTheFontNameDirectory); | |
283 | wxDeleteStockObjects(); | |
284 | ||
285 | wxFlushResources(); | |
286 | ||
287 | wxDELETE(wxTheResourceCache); | |
288 | ||
289 | wxDeleteStockLists(); | |
290 | ||
291 | wxSystemSettings::Done(); | |
292 | }; | |
293 | ||
294 | wxLog *wxApp::CreateLogTarget() | |
295 | { | |
296 | return new wxLogGui; | |
297 | } | |
298 | ||
299 | //----------------------------------------------------------------------------- | |
300 | // wxEntry | |
301 | //----------------------------------------------------------------------------- | |
302 | ||
303 | int wxEntry( int argc, char *argv[] ) | |
304 | { | |
305 | wxBuffer = new char[BUFSIZ + 512]; | |
306 | ||
307 | wxClassInfo::InitializeClasses(); | |
308 | ||
309 | #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT | |
310 | ||
311 | #if !defined(_WINDLL) | |
312 | streambuf* sBuf = new wxDebugStreamBuf; | |
313 | #else | |
314 | streambuf* sBuf = NULL; | |
315 | #endif | |
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 | gtk_init( &argc, &argv ); | |
346 | ||
347 | #ifdef USE_GDK_IMLIB | |
348 | ||
349 | gdk_imlib_init(); | |
350 | ||
351 | gtk_widget_push_visual(gdk_imlib_get_visual()); | |
352 | ||
353 | gtk_widget_push_colormap(gdk_imlib_get_colormap()); | |
354 | ||
355 | #endif | |
356 | ||
357 | wxApp::CommonInit(); | |
358 | ||
359 | wxTheApp->OnInitGui(); | |
360 | ||
361 | // Here frames insert themselves automatically | |
362 | // into wxTopLevelWindows by getting created | |
363 | // in OnInit(). | |
364 | ||
365 | if (!wxTheApp->OnInit()) return 0; | |
366 | ||
367 | wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0); | |
368 | ||
369 | int retValue = 0; | |
370 | ||
371 | if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); | |
372 | ||
373 | wxTheApp->DeletePendingObjects(); | |
374 | ||
375 | wxTheApp->OnExit(); | |
376 | ||
377 | wxApp::CommonCleanUp(); | |
378 | ||
379 | wxDELETE(wxTheApp); | |
380 | ||
381 | #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT | |
382 | // At this point we want to check if there are any memory | |
383 | // blocks that aren't part of the wxDebugContext itself, | |
384 | // as a special case. Then when dumping we need to ignore | |
385 | // wxDebugContext, too. | |
386 | if (wxDebugContext::CountObjectsLeft() > 0) | |
387 | { | |
388 | wxTrace("There were memory leaks.\n"); | |
389 | wxDebugContext::Dump(); | |
390 | wxDebugContext::PrintStatistics(); | |
391 | } | |
392 | wxDebugContext::SetStream(NULL, NULL); | |
393 | #endif | |
394 | ||
395 | return retValue; | |
396 | }; | |
397 | ||
398 | //----------------------------------------------------------------------------- | |
399 | // main() | |
400 | //----------------------------------------------------------------------------- | |
401 | ||
402 | #if defined(AIX) || defined(AIX4) || defined(____HPUX__) | |
403 | ||
404 | // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h | |
405 | ||
406 | #else | |
407 | ||
408 | int main(int argc, char *argv[]) { return wxEntry(argc, argv); } | |
409 | ||
410 | #endif | |
411 | ||
412 |