]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/app.cpp
extracted 4 copies of identical code into a function
[wxWidgets.git] / src / mgl / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Author: Vaclav Slavik
4 // based on GTK and MSW implementations
5 // Id: $Id$
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "app.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/settings.h"
23 #include "wx/module.h"
24 #include "wx/evtloop.h"
25 #include "wx/frame.h"
26 #include "wx/dialog.h"
27 #include "wx/intl.h"
28 #endif
29
30 #include "wx/app.h"
31 #include "wx/fontutil.h"
32 #include "wx/mgl/private.h"
33
34 //-----------------------------------------------------------------------------
35 // Global data
36 //-----------------------------------------------------------------------------
37
38 wxApp *wxTheApp = NULL;
39 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
40
41
42 //-----------------------------------------------------------------------------
43 // wxExit
44 //-----------------------------------------------------------------------------
45
46 void wxExit()
47 {
48 MGL_exit();
49 exit(0);
50 }
51
52 //-----------------------------------------------------------------------------
53 // wxYield
54 //-----------------------------------------------------------------------------
55
56 static bool gs_inYield = FALSE;
57
58 bool wxYield()
59 {
60 #if wxUSE_THREADS
61 if ( !wxThread::IsMain() )
62 {
63 // can't process events from other threads, MGL is thread-unsafe
64 return TRUE;
65 }
66 #endif // wxUSE_THREADS
67
68 gs_inYield = TRUE;
69
70 wxLog::Suspend();
71
72 if ( wxEventLoop::GetActive() )
73 {
74 while (wxEventLoop::GetActive()->Pending())
75 wxEventLoop::GetActive()->Dispatch();
76 }
77 else
78 MGL_wmUpdateDC(g_winMng); // FIXME_MGL -- temporary hack, please remove
79
80 /* it's necessary to call ProcessIdle() to update the frames sizes which
81 might have been changed (it also will update other things set from
82 OnUpdateUI() which is a nice (and desired) side effect) */
83 while (wxTheApp->ProcessIdle()) { }
84
85 wxLog::Resume();
86
87 gs_inYield = FALSE;
88
89 return TRUE;
90 }
91
92 bool wxYieldIfNeeded()
93 {
94 if (gs_inYield)
95 return FALSE;
96
97 return wxYield();
98 }
99
100
101 //-----------------------------------------------------------------------------
102 // wxWakeUpIdle
103 //-----------------------------------------------------------------------------
104
105 void wxWakeUpIdle()
106 {
107 #if wxUSE_THREADS
108 if (!wxThread::IsMain())
109 wxMutexGuiEnter();
110 #endif
111
112 while (wxTheApp->ProcessIdle()) {}
113
114 #if wxUSE_THREADS
115 if (!wxThread::IsMain())
116 wxMutexGuiLeave();
117 #endif
118 }
119
120 //-----------------------------------------------------------------------------
121 // wxApp
122 //-----------------------------------------------------------------------------
123
124 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
125
126 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
127 EVT_IDLE(wxApp::OnIdle)
128 END_EVENT_TABLE()
129
130
131 wxApp::wxApp() : m_mainLoop(NULL)
132 {
133 }
134
135 wxApp::~wxApp()
136 {
137 }
138
139 bool wxApp::OnInitGui()
140 {
141 if ( MGL_init(".", NULL) == 0 )
142 return FALSE;
143 if ( !wxCreateMGL_WM() )
144 return FALSE;
145
146 // This has to be done *after* wxCreateMGL_WM() because it initializes
147 // wxUniv's themes
148 if ( !wxAppBase::OnInitGui() )
149 return FALSE;
150
151 return TRUE;
152 }
153
154 bool wxApp::ProcessIdle()
155 {
156 wxIdleEvent event;
157 event.SetEventObject(this);
158 ProcessEvent(event);
159
160 return event.MoreRequested();
161 }
162
163 void wxApp::OnIdle(wxIdleEvent &event)
164 {
165 static bool s_inOnIdle = FALSE;
166
167 /* Avoid recursion (via ProcessEvent default case) */
168 if (s_inOnIdle)
169 return;
170
171 s_inOnIdle = TRUE;
172
173 /* Resend in the main thread events which have been prepared in other
174 threads */
175 ProcessPendingEvents();
176
177 // 'Garbage' collection of windows deleted with Close().
178 DeletePendingObjects();
179
180 // Send OnIdle events to all windows
181 if ( SendIdleEvents() )
182 event.RequestMore(TRUE);
183
184 s_inOnIdle = FALSE;
185 }
186
187 bool wxApp::SendIdleEvents()
188 {
189 bool needMore = FALSE;
190
191 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
192 while (node)
193 {
194 wxWindow* win = node->GetData();
195 if ( SendIdleEvents(win) )
196 needMore = TRUE;
197 node = node->GetNext();
198 }
199
200 return needMore;
201 }
202
203 bool wxApp::SendIdleEvents(wxWindow* win)
204 {
205 bool needMore = FALSE;
206
207 wxIdleEvent event;
208 event.SetEventObject(win);
209
210 win->GetEventHandler()->ProcessEvent(event);
211
212 #if 0 // FIXME_MGL - what the hell it is?
213 win->OnInternalIdle();
214
215 if ( event.MoreRequested() )
216 needMore = TRUE;
217 #endif
218
219 wxNode* node = win->GetChildren().First();
220 while (node)
221 {
222 wxWindow* win = (wxWindow*) node->Data();
223 if ( SendIdleEvents(win) )
224 needMore = TRUE;
225
226 node = node->Next();
227 }
228 return needMore;
229 }
230
231 int wxApp::MainLoop()
232 {
233 int rt;
234 m_mainLoop = new wxEventLoop;
235
236 rt = m_mainLoop->Run();
237
238 delete m_mainLoop;
239 m_mainLoop = NULL;
240 return rt;
241 }
242
243 void wxApp::ExitMainLoop()
244 {
245 if ( m_mainLoop )
246 m_mainLoop->Exit(0);
247 }
248
249 bool wxApp::Initialized()
250 {
251 // FIXME_MGL -- only for now because we don't have wxFrame/wxDialog yet
252 return TRUE;
253 //return (wxTopLevelWindows.GetCount() != 0);
254 }
255
256 bool wxApp::Pending()
257 {
258 return wxEventLoop::GetActive()->Pending();
259 }
260
261 void wxApp::Dispatch()
262 {
263 wxEventLoop::GetActive()->Dispatch();
264 }
265
266 void wxApp::DeletePendingObjects()
267 {
268 wxNode *node = wxPendingDelete.First();
269 while (node)
270 {
271 wxObject *obj = (wxObject *)node->Data();
272
273 delete obj;
274
275 if ( wxPendingDelete.Find(obj) )
276 delete node;
277
278 node = wxPendingDelete.First();
279 }
280 }
281
282 bool wxApp::Initialize()
283 {
284 wxBuffer = new wxChar[BUFSIZ + 512];
285
286 wxClassInfo::InitializeClasses();
287
288 wxSystemSettings::Init();
289
290 #if wxUSE_INTL
291 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
292 #endif
293
294 // GL: I'm annoyed ... I don't know where to put this and I don't want to
295 // create a module for that as it's part of the core.
296 #if wxUSE_THREADS
297 wxPendingEvents = new wxList;
298 wxPendingEventsLocker = new wxCriticalSection;
299 #endif
300
301 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
302 wxTheColourDatabase->Initialize();
303
304 // Can't do this in wxModule, because fonts are needed by stock lists
305 wxTheFontsManager = new wxFontsManager;
306
307 wxInitializeStockLists();
308 wxInitializeStockObjects();
309
310 #if wxUSE_WX_RESOURCES
311 wxInitializeResourceSystem();
312 #endif
313
314 wxModule::RegisterModules();
315 if (!wxModule::InitializeModules()) return FALSE;
316
317 return TRUE;
318 }
319
320 #include "info.xpm"
321 #include "error.xpm"
322 #include "question.xpm"
323 #include "warning.xpm"
324
325 wxIcon wxApp::GetStdIcon(int which) const
326 {
327 switch(which)
328 {
329 case wxICON_INFORMATION:
330 return wxIcon(info_xpm);
331 case wxICON_QUESTION:
332 return wxIcon(question_xpm);
333 case wxICON_EXCLAMATION:
334 return wxIcon(warning_xpm);
335 default:
336 wxFAIL_MSG(wxT("requested non existent standard icon"));
337 // still fall through
338 case wxICON_HAND:
339 return wxIcon(error_xpm);
340 }
341 }
342
343 void wxApp::CleanUp()
344 {
345 #if wxUSE_LOG
346 // flush the logged messages if any
347 wxLog *log = wxLog::GetActiveTarget();
348 if (log != NULL && log->HasPendingMessages())
349 log->Flush();
350
351 // continuing to use user defined log target is unsafe from now on because
352 // some resources may be already unavailable, so replace it by something
353 // more safe
354 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
355 if ( oldlog )
356 delete oldlog;
357 #endif // wxUSE_LOG
358
359 wxModule::CleanUpModules();
360
361 #if wxUSE_WX_RESOURCES
362 wxCleanUpResourceSystem();
363 #endif
364
365 if (wxTheColourDatabase)
366 delete wxTheColourDatabase;
367
368 wxTheColourDatabase = (wxColourDatabase*) NULL;
369
370 wxDeleteStockObjects();
371 wxDeleteStockLists();
372
373 // Can't do this in wxModule, because fonts are needed by stock lists
374 delete wxTheFontsManager;
375 wxTheFontsManager = (wxFontsManager*) NULL;
376
377 delete wxTheApp;
378 wxTheApp = (wxApp*) NULL;
379
380 // GL: I'm annoyed ... I don't know where to put this and I don't want to
381 // create a module for that as it's part of the core.
382 #if wxUSE_THREADS
383 delete wxPendingEvents;
384 delete wxPendingEventsLocker;
385 #endif
386
387 wxSystemSettings::Done();
388
389 delete[] wxBuffer;
390
391 wxClassInfo::CleanUpClasses();
392
393 // check for memory leaks
394 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
395 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
396 {
397 wxLogDebug(wxT("There were memory leaks.\n"));
398 wxDebugContext::Dump();
399 wxDebugContext::PrintStatistics();
400 }
401 #endif // Debug
402
403 #if wxUSE_LOG
404 // do this as the very last thing because everything else can log messages
405 wxLog::DontCreateOnDemand();
406
407 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
408 if (oldLog)
409 delete oldLog;
410 #endif // wxUSE_LOG
411
412 wxDestroyMGL_WM();
413 MGL_exit();
414 }
415
416
417 int wxEntryStart(int argc, char *argv[])
418 {
419 return wxApp::Initialize() ? 0 : -1;
420 }
421
422
423 int wxEntryInitGui()
424 {
425 return wxTheApp->OnInitGui() ? 0 : -1;
426 }
427
428
429 void wxEntryCleanup()
430 {
431 wxApp::CleanUp();
432 }
433
434
435
436 int wxEntry(int argc, char *argv[])
437 {
438 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
439 // This seems to be necessary since there are 'rogue'
440 // objects present at this point (perhaps global objects?)
441 // Setting a checkpoint will ignore them as far as the
442 // memory checking facility is concerned.
443 // Of course you may argue that memory allocated in globals should be
444 // checked, but this is a reasonable compromise.
445 wxDebugContext::SetCheckpoint();
446 #endif
447 int err = wxEntryStart(argc, argv);
448 if ( err )
449 return err;
450
451 if ( !wxTheApp )
452 {
453 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
454 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
455
456 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
457
458 wxObject *test_app = app_ini();
459
460 wxTheApp = (wxApp*) test_app;
461 }
462
463 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
464
465 wxTheApp->argc = argc;
466 #if wxUSE_UNICODE
467 wxTheApp->argv = new wxChar*[argc+1];
468 int mb_argc = 0;
469 while (mb_argc < argc)
470 {
471 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
472 mb_argc++;
473 }
474 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
475 #else
476 wxTheApp->argv = argv;
477 #endif
478
479 wxString name(wxFileNameFromPath(argv[0]));
480 wxStripExtension(name);
481 wxTheApp->SetAppName(name);
482
483 int retValue;
484 retValue = wxEntryInitGui();
485
486 // Here frames insert themselves automatically into wxTopLevelWindows by
487 // getting created in OnInit().
488 if ( retValue == 0 )
489 {
490 if ( !wxTheApp->OnInit() )
491 retValue = -1;
492 }
493
494 if ( retValue == 0 )
495 {
496 /* delete pending toplevel windows (typically a single
497 dialog) so that, if there isn't any left, we don't
498 call OnRun() */
499 wxTheApp->DeletePendingObjects();
500
501 if ( wxTheApp->Initialized() )
502 {
503 wxTheApp->OnRun();
504
505 wxWindow *topWindow = wxTheApp->GetTopWindow();
506 if ( topWindow )
507 {
508 /* Forcibly delete the window. */
509 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
510 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
511 {
512 topWindow->Close(TRUE);
513 wxTheApp->DeletePendingObjects();
514 }
515 else
516 {
517 delete topWindow;
518 wxTheApp->SetTopWindow((wxWindow*) NULL);
519 }
520 }
521
522 retValue = wxTheApp->OnExit();
523 }
524 }
525
526 wxEntryCleanup();
527
528 return retValue;
529 }