]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/app.cpp
More cleanup of OS X Cocoa wxDataViewCtrl code.
[wxWidgets.git] / src / mgl / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/app.cpp
3 // Author: Vaclav Slavik
4 // based on GTK and MSW implementations
5 // Id: $Id$
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #include "wx/app.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/settings.h"
21 #include "wx/frame.h"
22 #include "wx/dialog.h"
23 #include "wx/log.h"
24 #include "wx/intl.h"
25 #include "wx/module.h"
26 #endif
27
28 #include "wx/evtloop.h"
29 #include "wx/fontutil.h"
30 #include "wx/univ/theme.h"
31 #include "wx/univ/renderer.h"
32 #include "wx/univ/colschem.h"
33 #include "wx/sysopt.h"
34 #include "wx/mgl/private.h"
35 #include "wx/private/fontmgr.h"
36
37 //-----------------------------------------------------------------------------
38 // wxApp::Exit()
39 //-----------------------------------------------------------------------------
40
41 void wxApp::Exit()
42 {
43 MGL_exit();
44 exit(0);
45 }
46
47
48 //-----------------------------------------------------------------------------
49 // wxWakeUpIdle
50 //-----------------------------------------------------------------------------
51
52 void wxApp::WakeUpIdle()
53 {
54 #if wxUSE_THREADS
55 if (!wxThread::IsMain())
56 wxMutexGuiEnter();
57 #endif
58
59 while (wxTheApp->ProcessIdle())
60 ;
61
62 #if wxUSE_THREADS
63 if (!wxThread::IsMain())
64 wxMutexGuiLeave();
65 #endif
66 }
67
68 //-----------------------------------------------------------------------------
69 // Root window
70 //-----------------------------------------------------------------------------
71
72 class wxRootWindow : public wxWindow
73 {
74 public:
75 wxRootWindow() : wxWindow(NULL, wxID_ANY)
76 {
77 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng));
78 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP));
79 }
80 virtual ~wxRootWindow()
81 {
82 // we don't want to delete MGL_WM's rootWnd
83 m_wnd = NULL;
84 }
85
86 virtual bool AcceptsFocus() const { return false; }
87
88 DECLARE_DYNAMIC_CLASS(wxRootWindow)
89 };
90
91 IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow)
92
93 static wxRootWindow *gs_rootWindow = NULL;
94
95 //-----------------------------------------------------------------------------
96 // MGL initialization
97 //-----------------------------------------------------------------------------
98
99 static bool wxCreateMGL_WM(const wxVideoMode& displayMode)
100 {
101 int mode;
102 int refresh = MGL_DEFAULT_REFRESH;
103
104 #if wxUSE_SYSTEM_OPTIONS
105 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
106 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
107 #endif
108
109 mode = MGL_findMode(displayMode.GetWidth(),
110 displayMode.GetHeight(),
111 displayMode.GetDepth());
112 if ( mode == -1 )
113 {
114 wxLogError(_("Mode %ix%i-%i not available."),
115 displayMode.GetWidth(),
116 displayMode.GetHeight(),
117 displayMode.GetDepth());
118 return false;
119 }
120 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
121 if ( !g_displayDC->isValid() )
122 {
123 delete g_displayDC;
124 g_displayDC = NULL;
125 return false;
126 }
127
128 g_winMng = MGL_wmCreate(g_displayDC->getDC());
129 if (!g_winMng)
130 return false;
131
132 return true;
133 }
134
135 static void wxDestroyMGL_WM()
136 {
137 if ( g_winMng )
138 {
139 MGL_wmDestroy(g_winMng);
140 g_winMng = NULL;
141 }
142 if ( g_displayDC )
143 {
144 delete g_displayDC;
145 g_displayDC = NULL;
146 }
147 }
148
149 //-----------------------------------------------------------------------------
150 // wxApp
151 //-----------------------------------------------------------------------------
152
153 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
154
155 wxApp::wxApp()
156 {
157 }
158
159 wxApp::~wxApp()
160 {
161 }
162
163 wxVideoMode wxGetDefaultDisplayMode()
164 {
165 wxString mode;
166 unsigned w, h, bpp;
167
168 if ( !wxGetEnv(wxT("WXMODE"), &mode) ||
169 (wxSscanf(mode.c_str(), wxT("%ux%u-%u"), &w, &h, &bpp) != 3) )
170 {
171 w = 640, h = 480, bpp = 16;
172 }
173
174 return wxVideoMode(w, h, bpp);
175 }
176
177 bool wxApp::SetDisplayMode(const wxVideoMode& mode)
178 {
179 if ( !mode.IsOk() )
180 {
181 return false;
182 }
183 if ( g_displayDC != NULL )
184 {
185 // FIXME_MGL -- we currently don't allow to switch video mode
186 // more than once. This can hopefully be changed...
187 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
188 return false;
189 }
190
191 if ( !wxCreateMGL_WM(mode) )
192 return false;
193 gs_rootWindow = new wxRootWindow;
194
195 m_displayMode = mode;
196
197 return true;
198 }
199
200 bool wxApp::OnInitGui()
201 {
202 if ( !wxAppBase::OnInitGui() )
203 return false;
204
205 // MGL redirects stdout and stderr to physical console, so lets redirect
206 // it to file if WXSTDERR environment variable is set to be able to see
207 // wxLogDebug() output
208 wxString redirect;
209 if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
210 freopen(redirect.mb_str(), "wt", stderr);
211
212 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
213 if ( oldLog ) delete oldLog;
214
215 return true;
216 }
217
218 bool wxApp::Initialize(int& argc, wxChar **argv)
219 {
220 #ifdef __DJGPP__
221 // VS: disable long filenames under DJGPP as the very first thing,
222 // since SciTech MGL doesn't like them much...
223 wxSetEnv(wxT("LFN"), wxT("N"));
224 #endif
225
226 // intialize MGL before creating wxFontsManager since it uses MGL funcs
227 if ( MGL_init(".", NULL) == 0 )
228 {
229 wxLogError(_("Cannot initialize SciTech MGL!"));
230 return false;
231 }
232
233 if ( !wxAppBase::Initialize(argc, argv) )
234 {
235 MGL_exit();
236 return false;
237 }
238
239 #if wxUSE_INTL
240 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
241 #endif
242
243 return true;
244 }
245
246 // Modules are cleaned up after wxApp::CleanUp(), and some modules may
247 // require MGL to still be alive, e.g. the stock fonts need the fonts
248 // manager. So append this module last minute in wxApp::CleanUp() to close
249 // down MGL after all the other modules have been cleaned up.
250 //
251 struct wxMGLFinalCleanup: public wxModule
252 {
253 bool OnInit() { return true; }
254
255 void OnExit()
256 {
257 wxFontsManager::CleanUp();
258
259 wxDestroyMGL_WM();
260 MGL_exit();
261 }
262 };
263
264 void wxApp::CleanUp()
265 {
266 delete gs_rootWindow;
267
268 wxAppBase::CleanUp();
269
270 wxModule::RegisterModule(new wxMGLFinalCleanup);
271 }