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