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