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