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