]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
moved wxFloat/DoubleToStringStr from src/*/data.cpp to src/common/utils.cpp; took...
[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$
c41c20a5 6// Copyright: (c) 2001-2002 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
2ec3892d 21
7bdc1879
VS
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"
2ec3892d 28 #include "wx/log.h"
7bdc1879
VS
29 #include "wx/intl.h"
30#endif
32b8ec41 31
7bdc1879 32#include "wx/app.h"
ef344ff8 33#include "wx/fontutil.h"
df028524
VS
34#include "wx/univ/theme.h"
35#include "wx/univ/renderer.h"
58061670 36#include "wx/univ/colschem.h"
05c9ccbe 37#include "wx/sysopt.h"
7bdc1879 38#include "wx/mgl/private.h"
32b8ec41
VZ
39
40//-----------------------------------------------------------------------------
41// Global data
42//-----------------------------------------------------------------------------
43
7bdc1879 44wxApp *wxTheApp = NULL;
32b8ec41
VZ
45wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
46
32b8ec41 47
7bdc1879
VS
48//-----------------------------------------------------------------------------
49// wxExit
50//-----------------------------------------------------------------------------
32b8ec41
VZ
51
52void wxExit()
53{
7bdc1879 54 MGL_exit();
32b8ec41
VZ
55 exit(0);
56}
57
58//-----------------------------------------------------------------------------
59// wxYield
60//-----------------------------------------------------------------------------
61
7bdc1879
VS
62static bool gs_inYield = FALSE;
63
e1218bd6 64bool wxApp::Yield(bool onlyIfNeeded)
32b8ec41 65{
e1218bd6
VS
66 if ( gs_inYield )
67 {
68 if ( !onlyIfNeeded )
69 {
70 wxFAIL_MSG( wxT("wxYield called recursively" ) );
71 }
72
73 return FALSE;
74 }
75
7bdc1879
VS
76#if wxUSE_THREADS
77 if ( !wxThread::IsMain() )
78 {
79 // can't process events from other threads, MGL is thread-unsafe
80 return TRUE;
81 }
82#endif // wxUSE_THREADS
83
84 gs_inYield = TRUE;
85
86 wxLog::Suspend();
87
ef344ff8
VS
88 if ( wxEventLoop::GetActive() )
89 {
90 while (wxEventLoop::GetActive()->Pending())
91 wxEventLoop::GetActive()->Dispatch();
92 }
7c9955d1 93
7bdc1879
VS
94 /* it's necessary to call ProcessIdle() to update the frames sizes which
95 might have been changed (it also will update other things set from
96 OnUpdateUI() which is a nice (and desired) side effect) */
97 while (wxTheApp->ProcessIdle()) { }
98
99 wxLog::Resume();
100
101 gs_inYield = FALSE;
102
32b8ec41
VZ
103 return TRUE;
104}
105
7bdc1879 106
32b8ec41
VZ
107//-----------------------------------------------------------------------------
108// wxWakeUpIdle
109//-----------------------------------------------------------------------------
110
111void wxWakeUpIdle()
112{
7bdc1879
VS
113#if wxUSE_THREADS
114 if (!wxThread::IsMain())
115 wxMutexGuiEnter();
116#endif
117
7c9955d1 118 while (wxTheApp->ProcessIdle()) {}
7bdc1879
VS
119
120#if wxUSE_THREADS
121 if (!wxThread::IsMain())
122 wxMutexGuiLeave();
123#endif
32b8ec41
VZ
124}
125
58061670
VS
126//-----------------------------------------------------------------------------
127// Root window
128//-----------------------------------------------------------------------------
129
130class wxRootWindow : public wxWindow
131{
132 public:
133 wxRootWindow() : wxWindow(NULL, -1)
134 {
135 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng));
136 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP));
137 }
138 ~wxRootWindow()
139 {
140 // we don't want to delete MGL_WM's rootWnd
7c9955d1 141 m_wnd = NULL;
58061670
VS
142 }
143
b7d5acd0 144 virtual bool AcceptsFocus() const { return FALSE; }
7c9955d1 145
b7d5acd0 146 DECLARE_DYNAMIC_CLASS(wxRootWindow)
58061670
VS
147};
148
b7d5acd0
VS
149IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow)
150
58061670
VS
151static wxRootWindow *gs_rootWindow = NULL;
152
153//-----------------------------------------------------------------------------
154// MGL initialization
155//-----------------------------------------------------------------------------
156
634f6a1f 157static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode)
58061670
VS
158{
159 int mode;
58061670 160 int refresh = MGL_DEFAULT_REFRESH;
7c9955d1 161
58061670 162#if wxUSE_SYSTEM_OPTIONS
05c9ccbe 163 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
58061670
VS
164 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
165#endif
7c9955d1
JS
166
167 mode = MGL_findMode(displayMode.GetWidth(),
168 displayMode.GetHeight(),
634f6a1f 169 displayMode.GetDepth());
58061670
VS
170 if ( mode == -1 )
171 {
7c9955d1 172 wxLogError(_("Mode %ix%i-%i not available."),
d76048f5
VS
173 displayMode.GetWidth(),
174 displayMode.GetHeight(),
634f6a1f 175 displayMode.GetDepth());
1f43b5c9 176 return FALSE;
58061670
VS
177 }
178 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
179 if ( !g_displayDC->isValid() )
180 {
181 delete g_displayDC;
182 g_displayDC = NULL;
183 return FALSE;
184 }
7c9955d1 185
58061670
VS
186 g_winMng = MGL_wmCreate(g_displayDC->getDC());
187 if (!g_winMng)
188 return FALSE;
1f43b5c9 189
58061670
VS
190 return TRUE;
191}
192
193static void wxDestroyMGL_WM()
194{
195 if ( g_winMng )
196 {
197 MGL_wmDestroy(g_winMng);
198 g_winMng = NULL;
199 }
200 if ( g_displayDC )
201 {
202 delete g_displayDC;
203 g_displayDC = NULL;
204 }
205}
206
32b8ec41
VZ
207//-----------------------------------------------------------------------------
208// wxApp
209//-----------------------------------------------------------------------------
210
211IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
212
213BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
214 EVT_IDLE(wxApp::OnIdle)
215END_EVENT_TABLE()
216
217
ef344ff8 218wxApp::wxApp() : m_mainLoop(NULL)
7bdc1879
VS
219{
220}
221
222wxApp::~wxApp()
223{
224}
225
a3e76614
VS
226wxDisplayModeInfo wxGetDefaultDisplayMode()
227{
228 wxString mode;
229 unsigned w, h, bpp;
230
7c9955d1 231 if ( !wxGetEnv(wxT("WXMODE"), &mode) ||
a3e76614
VS
232 (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) )
233 {
234 w = 640, h = 480, bpp = 16;
235 }
236
237 return wxDisplayModeInfo(w, h, bpp);
238}
239
634f6a1f
VS
240bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode)
241{
242 if ( !mode.IsOk() )
243 {
244 return FALSE;
245 }
246 if ( g_displayDC != NULL )
247 {
248 // FIXME_MGL -- we currently don't allow to switch video mode
1f43b5c9 249 // more than once. This can hopefully be changed...
634f6a1f
VS
250 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
251 return FALSE;
252 }
1f43b5c9
VS
253
254 if ( !wxCreateMGL_WM(mode) )
255 return FALSE;
256 gs_rootWindow = new wxRootWindow;
257
634f6a1f 258 m_displayMode = mode;
1f43b5c9 259
634f6a1f
VS
260 return TRUE;
261}
262
7bdc1879
VS
263bool wxApp::OnInitGui()
264{
7bdc1879
VS
265 if ( !wxAppBase::OnInitGui() )
266 return FALSE;
267
d76048f5
VS
268#ifdef __WXDEBUG__
269 // MGL redirects stdout and stderr to physical console, so lets redirect
270 // it to file. Do it only when WXDEBUG environment variable is set
343e418c
VS
271 wxString redirect;
272 if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
273 freopen(redirect.mb_str(), "wt", stderr);
2ec3892d
VS
274#endif
275
d76048f5
VS
276 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
277 if ( oldLog ) delete oldLog;
278
7bdc1879
VS
279 return TRUE;
280}
281
282bool wxApp::ProcessIdle()
283{
284 wxIdleEvent event;
285 event.SetEventObject(this);
286 ProcessEvent(event);
287
288 return event.MoreRequested();
289}
290
291void wxApp::OnIdle(wxIdleEvent &event)
292{
293 static bool s_inOnIdle = FALSE;
294
295 /* Avoid recursion (via ProcessEvent default case) */
296 if (s_inOnIdle)
297 return;
298
299 s_inOnIdle = TRUE;
300
301 /* Resend in the main thread events which have been prepared in other
302 threads */
303 ProcessPendingEvents();
304
305 // 'Garbage' collection of windows deleted with Close().
306 DeletePendingObjects();
307
d76048f5
VS
308#if wxUSE_LOG
309 // flush the logged messages if any
310 wxLog::FlushActive();
311#endif // wxUSE_LOG
312
7bdc1879
VS
313 // Send OnIdle events to all windows
314 if ( SendIdleEvents() )
315 event.RequestMore(TRUE);
316
317 s_inOnIdle = FALSE;
318}
319
320bool wxApp::SendIdleEvents()
321{
322 bool needMore = FALSE;
323
324 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
325 while (node)
326 {
327 wxWindow* win = node->GetData();
328 if ( SendIdleEvents(win) )
329 needMore = TRUE;
330 node = node->GetNext();
331 }
332
333 return needMore;
334}
335
336bool wxApp::SendIdleEvents(wxWindow* win)
337{
338 bool needMore = FALSE;
339
340 wxIdleEvent event;
341 event.SetEventObject(win);
342
343 win->GetEventHandler()->ProcessEvent(event);
344
7bdc1879
VS
345 if ( event.MoreRequested() )
346 needMore = TRUE;
7bdc1879
VS
347
348 wxNode* node = win->GetChildren().First();
349 while (node)
350 {
351 wxWindow* win = (wxWindow*) node->Data();
352 if ( SendIdleEvents(win) )
353 needMore = TRUE;
354
355 node = node->Next();
356 }
357 return needMore;
358}
359
360int wxApp::MainLoop()
361{
fd495ab3 362 int rt;
ef344ff8
VS
363 m_mainLoop = new wxEventLoop;
364
365 rt = m_mainLoop->Run();
366
367 delete m_mainLoop;
368 m_mainLoop = NULL;
fd495ab3 369 return rt;
7bdc1879
VS
370}
371
372void wxApp::ExitMainLoop()
373{
ef344ff8
VS
374 if ( m_mainLoop )
375 m_mainLoop->Exit(0);
7bdc1879
VS
376}
377
378bool wxApp::Initialized()
379{
bd73ba41 380 return (wxTopLevelWindows.GetCount() != 0);
7bdc1879
VS
381}
382
383bool wxApp::Pending()
384{
ef344ff8 385 return wxEventLoop::GetActive()->Pending();
7bdc1879
VS
386}
387
388void wxApp::Dispatch()
32b8ec41 389{
ef344ff8 390 wxEventLoop::GetActive()->Dispatch();
32b8ec41
VZ
391}
392
7bdc1879
VS
393void wxApp::DeletePendingObjects()
394{
395 wxNode *node = wxPendingDelete.First();
396 while (node)
397 {
398 wxObject *obj = (wxObject *)node->Data();
399
400 delete obj;
401
402 if ( wxPendingDelete.Find(obj) )
403 delete node;
32b8ec41 404
7bdc1879
VS
405 node = wxPendingDelete.First();
406 }
407}
408
409bool wxApp::Initialize()
32b8ec41 410{
bd73ba41 411 if ( MGL_init(".", NULL) == 0 )
ddfca47f
VS
412 {
413 wxLogError(_("Cannot initialize SciTech MGL!"));
bd73ba41 414 return FALSE;
ddfca47f 415 }
bd73ba41 416
32b8ec41 417 wxClassInfo::InitializeClasses();
7bdc1879 418
7bdc1879
VS
419#if wxUSE_INTL
420 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
421#endif
422
423 // GL: I'm annoyed ... I don't know where to put this and I don't want to
424 // create a module for that as it's part of the core.
425#if wxUSE_THREADS
426 wxPendingEvents = new wxList;
427 wxPendingEventsLocker = new wxCriticalSection;
428#endif
429
430 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
32b8ec41 431 wxTheColourDatabase->Initialize();
7c9955d1 432
ef344ff8
VS
433 // Can't do this in wxModule, because fonts are needed by stock lists
434 wxTheFontsManager = new wxFontsManager;
7bdc1879 435
32b8ec41
VZ
436 wxInitializeStockLists();
437 wxInitializeStockObjects();
7bdc1879 438
32b8ec41
VZ
439 wxModule::RegisterModules();
440 if (!wxModule::InitializeModules()) return FALSE;
7bdc1879 441
32b8ec41
VZ
442 return TRUE;
443}
444
7bdc1879
VS
445void wxApp::CleanUp()
446{
447#if wxUSE_LOG
7bdc1879
VS
448 // continuing to use user defined log target is unsafe from now on because
449 // some resources may be already unavailable, so replace it by something
450 // more safe
451 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
452 if ( oldlog )
453 delete oldlog;
454#endif // wxUSE_LOG
455
d76048f5
VS
456 delete gs_rootWindow;
457
7bdc1879
VS
458 wxModule::CleanUpModules();
459
7bdc1879
VS
460 if (wxTheColourDatabase)
461 delete wxTheColourDatabase;
462
463 wxTheColourDatabase = (wxColourDatabase*) NULL;
464
465 wxDeleteStockObjects();
7bdc1879
VS
466 wxDeleteStockLists();
467
468 delete wxTheApp;
469 wxTheApp = (wxApp*) NULL;
470
df16a53e 471
7bdc1879
VS
472 // GL: I'm annoyed ... I don't know where to put this and I don't want to
473 // create a module for that as it's part of the core.
474#if wxUSE_THREADS
475 delete wxPendingEvents;
476 delete wxPendingEventsLocker;
477#endif
478
7bdc1879
VS
479 wxClassInfo::CleanUpClasses();
480
df16a53e
VS
481 // Can't do this in wxModule, because fonts are needed by stock lists
482 // (do it after deleting wxTheApp and cleaning modules up, since somebody
483 // may be deleting fonts that lately)
484 delete wxTheFontsManager;
485 wxTheFontsManager = (wxFontsManager*) NULL;
486
7bdc1879
VS
487 // check for memory leaks
488#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
489 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
490 {
491 wxLogDebug(wxT("There were memory leaks.\n"));
492 wxDebugContext::Dump();
493 wxDebugContext::PrintStatistics();
494 }
495#endif // Debug
496
497#if wxUSE_LOG
498 // do this as the very last thing because everything else can log messages
499 wxLog::DontCreateOnDemand();
500
501 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
502 if (oldLog)
503 delete oldLog;
504#endif // wxUSE_LOG
505
506 wxDestroyMGL_WM();
507 MGL_exit();
508}
509
510
511int wxEntryStart(int argc, char *argv[])
512{
513 return wxApp::Initialize() ? 0 : -1;
514}
515
516
517int wxEntryInitGui()
518{
519 return wxTheApp->OnInitGui() ? 0 : -1;
520}
521
522
523void wxEntryCleanup()
524{
525 wxApp::CleanUp();
526}
527
528
529
530int wxEntry(int argc, char *argv[])
531{
6ed89758
VS
532#ifdef __DJGPP__
533 // VS: disable long filenames under DJGPP as the very first thing,
534 // since SciTech MGL doesn't like them much...
535 wxSetEnv(wxT("LFN"), wxT("N"));
536#endif
537
7bdc1879
VS
538#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
539 // This seems to be necessary since there are 'rogue'
540 // objects present at this point (perhaps global objects?)
541 // Setting a checkpoint will ignore them as far as the
542 // memory checking facility is concerned.
543 // Of course you may argue that memory allocated in globals should be
544 // checked, but this is a reasonable compromise.
545 wxDebugContext::SetCheckpoint();
546#endif
547 int err = wxEntryStart(argc, argv);
548 if ( err )
549 return err;
550
551 if ( !wxTheApp )
552 {
553 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
554 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
555
556 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
557
558 wxObject *test_app = app_ini();
559
560 wxTheApp = (wxApp*) test_app;
561 }
562
563 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
564
565 wxTheApp->argc = argc;
566#if wxUSE_UNICODE
567 wxTheApp->argv = new wxChar*[argc+1];
568 int mb_argc = 0;
569 while (mb_argc < argc)
570 {
571 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
572 mb_argc++;
573 }
574 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
575#else
576 wxTheApp->argv = argv;
577#endif
578
579 wxString name(wxFileNameFromPath(argv[0]));
580 wxStripExtension(name);
581 wxTheApp->SetAppName(name);
582
583 int retValue;
584 retValue = wxEntryInitGui();
585
586 // Here frames insert themselves automatically into wxTopLevelWindows by
587 // getting created in OnInit().
588 if ( retValue == 0 )
589 {
590 if ( !wxTheApp->OnInit() )
591 retValue = -1;
592 }
593
594 if ( retValue == 0 )
595 {
596 /* delete pending toplevel windows (typically a single
597 dialog) so that, if there isn't any left, we don't
598 call OnRun() */
599 wxTheApp->DeletePendingObjects();
600
fd495ab3 601 if ( wxTheApp->Initialized() )
7bdc1879
VS
602 {
603 wxTheApp->OnRun();
604
605 wxWindow *topWindow = wxTheApp->GetTopWindow();
606 if ( topWindow )
607 {
608 /* Forcibly delete the window. */
609 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
610 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
611 {
612 topWindow->Close(TRUE);
613 wxTheApp->DeletePendingObjects();
614 }
615 else
616 {
617 delete topWindow;
618 wxTheApp->SetTopWindow((wxWindow*) NULL);
619 }
620 }
621
d76048f5
VS
622#if wxUSE_LOG
623 // flush the logged messages if any
624 wxLog *log = wxLog::GetActiveTarget();
625 if (log != NULL && log->HasPendingMessages())
626 log->Flush();
627#endif // wxUSE_LOG
7bdc1879
VS
628 retValue = wxTheApp->OnExit();
629 }
630 }
631
632 wxEntryCleanup();
633
634 return retValue;
635}