]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mgl/app.cpp
Updated to new PyCrust
[wxWidgets.git] / src / mgl / app.cpp
... / ...
CommitLineData
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/univ/theme.h"
35#include "wx/univ/renderer.h"
36#include "wx/mgl/private.h"
37
38#define MGL_DEBUG
39
40#if defined(MGL_DEBUG) && !defined(__WXDEBUG__)
41#undef MGL_DEBUG
42#endif
43
44//-----------------------------------------------------------------------------
45// Global data
46//-----------------------------------------------------------------------------
47
48wxApp *wxTheApp = NULL;
49wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
50
51
52//-----------------------------------------------------------------------------
53// wxExit
54//-----------------------------------------------------------------------------
55
56void wxExit()
57{
58 MGL_exit();
59 exit(0);
60}
61
62//-----------------------------------------------------------------------------
63// wxYield
64//-----------------------------------------------------------------------------
65
66static bool gs_inYield = FALSE;
67
68bool wxYield()
69{
70#if wxUSE_THREADS
71 if ( !wxThread::IsMain() )
72 {
73 // can't process events from other threads, MGL is thread-unsafe
74 return TRUE;
75 }
76#endif // wxUSE_THREADS
77
78 gs_inYield = TRUE;
79
80 wxLog::Suspend();
81
82 if ( wxEventLoop::GetActive() )
83 {
84 while (wxEventLoop::GetActive()->Pending())
85 wxEventLoop::GetActive()->Dispatch();
86 }
87
88 /* it's necessary to call ProcessIdle() to update the frames sizes which
89 might have been changed (it also will update other things set from
90 OnUpdateUI() which is a nice (and desired) side effect) */
91 while (wxTheApp->ProcessIdle()) { }
92
93 wxLog::Resume();
94
95 gs_inYield = FALSE;
96
97 return TRUE;
98}
99
100bool wxYieldIfNeeded()
101{
102 if (gs_inYield)
103 return FALSE;
104
105 return wxYield();
106}
107
108
109//-----------------------------------------------------------------------------
110// wxWakeUpIdle
111//-----------------------------------------------------------------------------
112
113void wxWakeUpIdle()
114{
115#if wxUSE_THREADS
116 if (!wxThread::IsMain())
117 wxMutexGuiEnter();
118#endif
119
120 while (wxTheApp->ProcessIdle()) {}
121
122#if wxUSE_THREADS
123 if (!wxThread::IsMain())
124 wxMutexGuiLeave();
125#endif
126}
127
128//-----------------------------------------------------------------------------
129// wxApp
130//-----------------------------------------------------------------------------
131
132IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
133
134BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
135 EVT_IDLE(wxApp::OnIdle)
136END_EVENT_TABLE()
137
138
139wxApp::wxApp() : m_mainLoop(NULL)
140{
141}
142
143wxApp::~wxApp()
144{
145}
146
147bool wxApp::OnInitGui()
148{
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
166bool wxApp::ProcessIdle()
167{
168 wxIdleEvent event;
169 event.SetEventObject(this);
170 ProcessEvent(event);
171
172 return event.MoreRequested();
173}
174
175void 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
199bool 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
215bool 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
239int 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
251void wxApp::ExitMainLoop()
252{
253 if ( m_mainLoop )
254 m_mainLoop->Exit(0);
255}
256
257bool wxApp::Initialized()
258{
259 return (wxTopLevelWindows.GetCount() != 0);
260}
261
262bool wxApp::Pending()
263{
264 return wxEventLoop::GetActive()->Pending();
265}
266
267void wxApp::Dispatch()
268{
269 wxEventLoop::GetActive()->Dispatch();
270}
271
272void wxApp::DeletePendingObjects()
273{
274 wxNode *node = wxPendingDelete.First();
275 while (node)
276 {
277 wxObject *obj = (wxObject *)node->Data();
278
279 delete obj;
280
281 if ( wxPendingDelete.Find(obj) )
282 delete node;
283
284 node = wxPendingDelete.First();
285 }
286}
287
288bool wxApp::Initialize()
289{
290 if ( MGL_init(".", NULL) == 0 )
291 return FALSE;
292
293 wxBuffer = new wxChar[BUFSIZ + 512];
294
295 wxClassInfo::InitializeClasses();
296
297 wxSystemSettings::Init();
298
299#if wxUSE_INTL
300 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
301#endif
302
303 // GL: I'm annoyed ... I don't know where to put this and I don't want to
304 // create a module for that as it's part of the core.
305#if wxUSE_THREADS
306 wxPendingEvents = new wxList;
307 wxPendingEventsLocker = new wxCriticalSection;
308#endif
309
310 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
311 wxTheColourDatabase->Initialize();
312
313 // Can't do this in wxModule, because fonts are needed by stock lists
314 wxTheFontsManager = new wxFontsManager;
315
316 wxInitializeStockLists();
317 wxInitializeStockObjects();
318
319#if wxUSE_WX_RESOURCES
320 wxInitializeResourceSystem();
321#endif
322
323 wxModule::RegisterModules();
324 if (!wxModule::InitializeModules()) return FALSE;
325
326 return TRUE;
327}
328
329wxIcon wxApp::GetStdIcon(int which) const
330{
331 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
332}
333
334void wxApp::CleanUp()
335{
336#if wxUSE_LOG
337 // flush the logged messages if any
338 wxLog *log = wxLog::GetActiveTarget();
339 if (log != NULL && log->HasPendingMessages())
340 log->Flush();
341
342 // continuing to use user defined log target is unsafe from now on because
343 // some resources may be already unavailable, so replace it by something
344 // more safe
345 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
346 if ( oldlog )
347 delete oldlog;
348#endif // wxUSE_LOG
349
350 wxModule::CleanUpModules();
351
352#if wxUSE_WX_RESOURCES
353 wxCleanUpResourceSystem();
354#endif
355
356 if (wxTheColourDatabase)
357 delete wxTheColourDatabase;
358
359 wxTheColourDatabase = (wxColourDatabase*) NULL;
360
361 wxDeleteStockObjects();
362 wxDeleteStockLists();
363
364 // Can't do this in wxModule, because fonts are needed by stock lists
365 delete wxTheFontsManager;
366 wxTheFontsManager = (wxFontsManager*) NULL;
367
368 delete wxTheApp;
369 wxTheApp = (wxApp*) NULL;
370
371 // GL: I'm annoyed ... I don't know where to put this and I don't want to
372 // create a module for that as it's part of the core.
373#if wxUSE_THREADS
374 delete wxPendingEvents;
375 delete wxPendingEventsLocker;
376#endif
377
378 wxSystemSettings::Done();
379
380 delete[] wxBuffer;
381
382 wxClassInfo::CleanUpClasses();
383
384 // check for memory leaks
385#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
386 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
387 {
388 wxLogDebug(wxT("There were memory leaks.\n"));
389 wxDebugContext::Dump();
390 wxDebugContext::PrintStatistics();
391 }
392#endif // Debug
393
394#if wxUSE_LOG
395 // do this as the very last thing because everything else can log messages
396 wxLog::DontCreateOnDemand();
397
398 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
399 if (oldLog)
400 delete oldLog;
401#endif // wxUSE_LOG
402
403 wxDestroyMGL_WM();
404 MGL_exit();
405}
406
407
408int wxEntryStart(int argc, char *argv[])
409{
410 return wxApp::Initialize() ? 0 : -1;
411}
412
413
414int wxEntryInitGui()
415{
416 return wxTheApp->OnInitGui() ? 0 : -1;
417}
418
419
420void wxEntryCleanup()
421{
422 wxApp::CleanUp();
423}
424
425
426
427int wxEntry(int argc, char *argv[])
428{
429#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
430 // This seems to be necessary since there are 'rogue'
431 // objects present at this point (perhaps global objects?)
432 // Setting a checkpoint will ignore them as far as the
433 // memory checking facility is concerned.
434 // Of course you may argue that memory allocated in globals should be
435 // checked, but this is a reasonable compromise.
436 wxDebugContext::SetCheckpoint();
437#endif
438 int err = wxEntryStart(argc, argv);
439 if ( err )
440 return err;
441
442 if ( !wxTheApp )
443 {
444 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
445 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
446
447 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
448
449 wxObject *test_app = app_ini();
450
451 wxTheApp = (wxApp*) test_app;
452 }
453
454 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
455
456 wxTheApp->argc = argc;
457#if wxUSE_UNICODE
458 wxTheApp->argv = new wxChar*[argc+1];
459 int mb_argc = 0;
460 while (mb_argc < argc)
461 {
462 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
463 mb_argc++;
464 }
465 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
466#else
467 wxTheApp->argv = argv;
468#endif
469
470 wxString name(wxFileNameFromPath(argv[0]));
471 wxStripExtension(name);
472 wxTheApp->SetAppName(name);
473
474 int retValue;
475 retValue = wxEntryInitGui();
476
477 // Here frames insert themselves automatically into wxTopLevelWindows by
478 // getting created in OnInit().
479 if ( retValue == 0 )
480 {
481 if ( !wxTheApp->OnInit() )
482 retValue = -1;
483 }
484
485 if ( retValue == 0 )
486 {
487 /* delete pending toplevel windows (typically a single
488 dialog) so that, if there isn't any left, we don't
489 call OnRun() */
490 wxTheApp->DeletePendingObjects();
491
492 if ( wxTheApp->Initialized() )
493 {
494 wxTheApp->OnRun();
495
496 wxWindow *topWindow = wxTheApp->GetTopWindow();
497 if ( topWindow )
498 {
499 /* Forcibly delete the window. */
500 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
501 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
502 {
503 topWindow->Close(TRUE);
504 wxTheApp->DeletePendingObjects();
505 }
506 else
507 {
508 delete topWindow;
509 wxTheApp->SetTopWindow((wxWindow*) NULL);
510 }
511 }
512
513 retValue = wxTheApp->OnExit();
514 }
515 }
516
517 wxEntryCleanup();
518
519 return retValue;
520}