]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
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" | |
46dc76ba | 21 | #include "wx/memory.h" |
a3622daa VZ |
22 | #include "wx/font.h" |
23 | #include "wx/settings.h" | |
8d71b555 | 24 | #include "wx/resource.h" |
c801d85f KB |
25 | |
26 | #include "unistd.h" | |
27 | ||
28 | #ifdef USE_GDK_IMLIB | |
1f0299c1 | 29 | #include "../gdk_imlib/gdk_imlib.h" |
c801d85f KB |
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; | |
a3622daa | 40 | extern wxResourceCache *wxTheResourceCache; |
c801d85f KB |
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(); | |
ff7b1510 | 55 | } |
c801d85f KB |
56 | |
57 | bool wxYield(void) | |
58 | { | |
59 | while (gtk_events_pending() > 0) gtk_main_iteration(); | |
60 | return TRUE; | |
ff7b1510 | 61 | } |
c801d85f KB |
62 | |
63 | //----------------------------------------------------------------------------- | |
64 | // wxApp | |
65 | //----------------------------------------------------------------------------- | |
66 | ||
67 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
68 | ||
53010e52 RR |
69 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
70 | EVT_IDLE(wxApp::OnIdle) | |
71 | END_EVENT_TABLE() | |
72 | ||
c801d85f KB |
73 | gint wxapp_idle_callback( gpointer WXUNUSED(data) ) |
74 | { | |
ff7b1510 | 75 | if (wxTheApp) while (wxTheApp->ProcessIdle()) {} |
c801d85f KB |
76 | usleep( 10000 ); |
77 | return TRUE; | |
ff7b1510 | 78 | } |
c801d85f KB |
79 | |
80 | wxApp::wxApp() | |
81 | { | |
82 | m_idleTag = 0; | |
83 | m_topWindow = NULL; | |
84 | m_exitOnFrameDelete = TRUE; | |
ff7b1510 | 85 | } |
c801d85f KB |
86 | |
87 | wxApp::~wxApp(void) | |
88 | { | |
89 | gtk_idle_remove( m_idleTag ); | |
ff7b1510 | 90 | } |
c801d85f KB |
91 | |
92 | bool wxApp::OnInit(void) | |
93 | { | |
94 | return TRUE; | |
ff7b1510 | 95 | } |
c801d85f KB |
96 | |
97 | bool wxApp::OnInitGui(void) | |
98 | { | |
99 | m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL ); | |
100 | return TRUE; | |
ff7b1510 | 101 | } |
c801d85f KB |
102 | |
103 | int wxApp::OnRun(void) | |
104 | { | |
105 | return MainLoop(); | |
ff7b1510 | 106 | } |
c801d85f | 107 | |
53010e52 RR |
108 | bool wxApp::ProcessIdle(void) |
109 | { | |
110 | wxIdleEvent event; | |
111 | event.SetEventObject( this ); | |
112 | ProcessEvent( event ); | |
113 | ||
114 | return event.MoreRequested(); | |
ff7b1510 | 115 | } |
53010e52 RR |
116 | |
117 | void wxApp::OnIdle( wxIdleEvent &event ) | |
c801d85f | 118 | { |
53010e52 RR |
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(). | |
c801d85f | 128 | DeletePendingObjects(); |
53010e52 RR |
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; | |
ff7b1510 | 142 | } |
53010e52 RR |
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; | |
ff7b1510 | 157 | } |
53010e52 RR |
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 ; | |
ff7b1510 | 180 | } |
c801d85f KB |
181 | |
182 | int wxApp::OnExit(void) | |
183 | { | |
184 | return 0; | |
ff7b1510 | 185 | } |
c801d85f KB |
186 | |
187 | int wxApp::MainLoop(void) | |
188 | { | |
189 | gtk_main(); | |
190 | return 0; | |
ff7b1510 | 191 | } |
c801d85f KB |
192 | |
193 | void wxApp::ExitMainLoop(void) | |
194 | { | |
195 | gtk_main_quit(); | |
ff7b1510 | 196 | } |
c801d85f KB |
197 | |
198 | bool wxApp::Initialized(void) | |
199 | { | |
200 | return m_initialized; | |
ff7b1510 | 201 | } |
c801d85f KB |
202 | |
203 | bool wxApp::Pending(void) | |
204 | { | |
205 | return FALSE; | |
ff7b1510 | 206 | } |
c801d85f KB |
207 | |
208 | void wxApp::Dispatch(void) | |
209 | { | |
ff7b1510 | 210 | } |
c801d85f KB |
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(); | |
ff7b1510 RR |
225 | } |
226 | } | |
c801d85f KB |
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(); | |
ff7b1510 | 234 | } |
c801d85f KB |
235 | |
236 | void wxApp::SetTopWindow( wxWindow *win ) | |
237 | { | |
238 | m_topWindow = win; | |
ff7b1510 | 239 | } |
c801d85f KB |
240 | |
241 | void wxApp::CommonInit(void) | |
242 | { | |
243 | ||
244 | /* | |
245 | #if USE_RESOURCES | |
246 | (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion); | |
247 | #endif | |
248 | */ | |
a3622daa VZ |
249 | wxSystemSettings::Init(); |
250 | wxTheResourceCache = new wxResourceCache(wxKEY_STRING); | |
251 | ||
252 | wxTheFontNameDirectory = new wxFontNameDirectory; | |
253 | wxTheFontNameDirectory->Initialize(); | |
c801d85f KB |
254 | |
255 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
256 | wxTheColourDatabase->Initialize(); | |
a3622daa VZ |
257 | |
258 | wxInitializeStockLists(); | |
c801d85f KB |
259 | wxInitializeStockObjects(); |
260 | ||
8d71b555 RR |
261 | wxInitializeResourceSystem(); |
262 | ||
c801d85f KB |
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 | */ | |
ff7b1510 | 276 | } |
c801d85f KB |
277 | |
278 | void wxApp::CommonCleanUp(void) | |
279 | { | |
a3622daa VZ |
280 | wxDELETE(wxTheColourDatabase); |
281 | wxDELETE(wxThePrintPaperDatabase); | |
282 | wxDELETE(wxThePrintSetupData); | |
283 | wxDELETE(wxTheFontNameDirectory); | |
c801d85f KB |
284 | wxDeleteStockObjects(); |
285 | ||
286 | wxFlushResources(); | |
a3622daa VZ |
287 | |
288 | wxDELETE(wxTheResourceCache); | |
289 | ||
290 | wxDeleteStockLists(); | |
291 | ||
8d71b555 RR |
292 | wxCleanUpResourceSystem(); |
293 | ||
a3622daa | 294 | wxSystemSettings::Done(); |
ff7b1510 | 295 | } |
c801d85f KB |
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 | ||
46dc76ba RR |
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 | ||
c801d85f KB |
324 | if (!wxTheApp) |
325 | { | |
326 | if (!wxApp::GetInitializerFunction()) | |
327 | { | |
1a5a8367 | 328 | printf( _("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); |
c801d85f | 329 | return 0; |
ff7b1510 | 330 | } |
c801d85f KB |
331 | |
332 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); | |
333 | ||
334 | wxObject *test_app = app_ini(); | |
335 | ||
336 | wxTheApp = (wxApp*) test_app; | |
ff7b1510 | 337 | } |
c801d85f KB |
338 | |
339 | if (!wxTheApp) | |
340 | { | |
1a5a8367 | 341 | printf( _("wxWindows error: wxTheApp == NULL\n") ); |
c801d85f | 342 | return 0; |
ff7b1510 | 343 | } |
c801d85f | 344 | |
c801d85f KB |
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(); | |
a3622daa VZ |
381 | |
382 | wxDELETE(wxTheApp); | |
c801d85f | 383 | |
46dc76ba RR |
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 | ||
c801d85f | 398 | return retValue; |
ff7b1510 | 399 | } |
1a56f55c RR |
400 | |
401 | //----------------------------------------------------------------------------- | |
402 | // main() | |
403 | //----------------------------------------------------------------------------- | |
404 | ||
7f4dc78d | 405 | #if defined(AIX) || defined(AIX4) || defined(____HPUX__) |
1a56f55c RR |
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 |