]>
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 | #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 = (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 USE_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 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 | */ | |
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 && 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 | |
324 | ||
325 | if (!wxTheApp) | |
326 | { | |
327 | if (!wxApp::GetInitializerFunction()) | |
328 | { | |
329 | printf( _("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); | |
330 | return 0; | |
331 | } | |
332 | ||
333 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); | |
334 | ||
335 | wxObject *test_app = app_ini(); | |
336 | ||
337 | wxTheApp = (wxApp*) test_app; | |
338 | } | |
339 | ||
340 | if (!wxTheApp) | |
341 | { | |
342 | printf( _("wxWindows error: wxTheApp == NULL\n") ); | |
343 | return 0; | |
344 | } | |
345 | ||
346 | wxTheApp->argc = argc; | |
347 | wxTheApp->argv = argv; | |
348 | ||
349 | gtk_init( &argc, &argv ); | |
350 | ||
351 | #ifdef USE_GDK_IMLIB | |
352 | ||
353 | gdk_imlib_init(); | |
354 | ||
355 | gtk_widget_push_visual(gdk_imlib_get_visual()); | |
356 | ||
357 | gtk_widget_push_colormap(gdk_imlib_get_colormap()); | |
358 | ||
359 | #endif | |
360 | ||
361 | wxApp::CommonInit(); | |
362 | ||
363 | wxTheApp->OnInitGui(); | |
364 | ||
365 | // Here frames insert themselves automatically | |
366 | // into wxTopLevelWindows by getting created | |
367 | // in OnInit(). | |
368 | ||
369 | if (!wxTheApp->OnInit()) return 0; | |
370 | ||
371 | wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0); | |
372 | ||
373 | int retValue = 0; | |
374 | ||
375 | if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); | |
376 | ||
377 | wxTheApp->DeletePendingObjects(); | |
378 | ||
379 | wxTheApp->OnExit(); | |
380 | ||
381 | wxApp::CommonCleanUp(); | |
382 | ||
383 | wxDELETE(wxTheApp); | |
384 | ||
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 | |
398 | ||
399 | return retValue; | |
400 | } | |
401 | ||
402 | //----------------------------------------------------------------------------- | |
403 | // main() | |
404 | //----------------------------------------------------------------------------- | |
405 | ||
406 | #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN) | |
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 |