]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: emulator.cpp | |
3 | // Purpose: Emulator wxWindows sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma implementation "emulator.h" | |
18 | #endif | |
19 | ||
20 | // ---------------------------------------------------------------------------- | |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
24 | // For compilers that support precompilation, includes "wx/wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | // for all others, include the necessary headers (this file is usually all you | |
32 | // need because it includes almost all "standard" wxWindows headers) | |
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/wx.h" | |
35 | #endif | |
36 | ||
37 | #ifdef __WXX11__ | |
38 | #include "wx/x11/reparent.h" | |
39 | #endif | |
40 | ||
41 | #include "emulator.h" | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // resources | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | // the application icon (under Windows and OS/2 it is in resources) | |
48 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) | |
49 | #include "mondrian.xpm" | |
50 | #endif | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // event tables and other macros for wxWindows | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | // the event tables connect the wxWindows events with the functions (event | |
57 | // handlers) which process them. It can be also done at run-time, but for the | |
58 | // simple menu events like this the static method is much simpler. | |
59 | BEGIN_EVENT_TABLE(wxEmulatorFrame, wxFrame) | |
60 | EVT_MENU(Emulator_Quit, wxEmulatorFrame::OnQuit) | |
61 | EVT_MENU(Emulator_About, wxEmulatorFrame::OnAbout) | |
62 | END_EVENT_TABLE() | |
63 | ||
64 | // Create a new application object: this macro will allow wxWindows to create | |
65 | // the application object during program execution (it's better than using a | |
66 | // static object for many reasons) and also declares the accessor function | |
67 | // wxGetApp() which will return the reference of the right type (i.e. wxEmulatorApp and | |
68 | // not wxApp) | |
69 | IMPLEMENT_APP(wxEmulatorApp) | |
70 | ||
71 | // ============================================================================ | |
72 | // implementation | |
73 | // ============================================================================ | |
74 | ||
75 | // ---------------------------------------------------------------------------- | |
76 | // the application class | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
79 | wxEmulatorApp::wxEmulatorApp() | |
80 | { | |
81 | m_xnestWindow = NULL; | |
82 | m_containerWindow = NULL; | |
83 | } | |
84 | ||
85 | // 'Main program' equivalent: the program execution "starts" here | |
86 | bool wxEmulatorApp::OnInit() | |
87 | { | |
88 | wxInitAllImageHandlers(); | |
89 | ||
90 | // create the main application window | |
91 | wxEmulatorFrame *frame = new wxEmulatorFrame(_T("wxEmulator"), | |
92 | wxPoint(50, 50), wxSize(450, 340)); | |
93 | ||
94 | m_containerWindow = new wxEmulatorContainer(frame, -1); | |
95 | ||
96 | // Load the emulation info | |
97 | if (!LoadEmulator()) | |
98 | { | |
99 | frame->Destroy(); | |
100 | wxMessageBox(wxT("Sorry, could not load this emulator. Please check bitmaps are valid.")); | |
101 | return FALSE; | |
102 | } | |
103 | ||
104 | if (m_emulatorInfo.m_emulatorBackgroundBitmap.Ok()) | |
105 | frame->SetClientSize(m_emulatorInfo.m_emulatorBackgroundBitmap.GetWidth(), | |
106 | m_emulatorInfo.m_emulatorBackgroundBitmap.GetHeight()); | |
107 | ||
108 | // and show it (the frames, unlike simple controls, are not shown when | |
109 | // created initially) | |
110 | frame->Show(TRUE); | |
111 | ||
112 | #ifdef __WXX11__ | |
113 | m_xnestWindow = new wxAdoptedWindow; | |
114 | ||
115 | wxString cmd; | |
116 | // cmd.Printf(wxT("Xnest :100 -geometry %dx%d+50+50"), | |
117 | cmd.Printf(wxT("Xnest :100 -geometry %dx%d"), | |
118 | (int) m_emulatorInfo.m_emulatorScreenSize.x, (int) m_emulatorInfo.m_emulatorScreenSize.y); | |
119 | ||
120 | // Asynchronously executes Xnest | |
121 | if (0 == wxExecute(cmd)) | |
122 | { | |
123 | frame->Destroy(); | |
124 | wxMessageBox(wxT("Sorry, could not run Xnest. Please check your PATH.")); | |
125 | return FALSE; | |
126 | } | |
127 | ||
128 | wxReparenter reparenter; | |
129 | if (!reparenter.WaitAndReparent(m_containerWindow, m_xnestWindow, wxT("Xnest"))) | |
130 | { | |
131 | wxMessageBox(wxT("Sorry, could not reparent Xnest..")); | |
132 | frame->Destroy(); | |
133 | return FALSE; | |
134 | } | |
135 | ||
136 | m_containerWindow->DoResize(); | |
137 | #endif | |
138 | ||
139 | // success: wxApp::OnRun() will be called which will enter the main message | |
140 | // loop and the application will run. If we returned FALSE here, the | |
141 | // application would exit immediately. | |
142 | return TRUE; | |
143 | } | |
144 | ||
145 | // Load the specified emulator. | |
146 | // For now, hard-wired. TODO: make this configurable | |
147 | bool wxEmulatorApp::LoadEmulator() | |
148 | { | |
149 | m_emulatorInfo.m_emulatorTitle = wxT("iPAQ Emulator"); | |
150 | ||
151 | m_emulatorInfo.m_emulatorDescription = wxT("No description yet"); | |
152 | ||
153 | // The offset from the top-left of the main emulator | |
154 | // bitmap and the virtual screen (where Xnest is | |
155 | // positioned) | |
156 | m_emulatorInfo.m_emulatorScreenPosition = wxPoint(56, 69); | |
157 | ||
158 | // The emulated screen size | |
159 | m_emulatorInfo.m_emulatorScreenSize = wxSize(240, 320); | |
160 | ||
161 | m_emulatorInfo.m_emulatorBackgroundBitmapName = wxT("ipaq01.jpg"); | |
162 | ||
163 | m_emulatorInfo.m_emulatorBackgroundColour = * wxBLACK; | |
164 | ||
165 | return m_emulatorInfo.Load(); | |
166 | } | |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // main frame | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | // frame constructor | |
173 | wxEmulatorFrame::wxEmulatorFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
174 | : wxFrame(NULL, -1, title, pos, size) | |
175 | { | |
176 | // set the frame icon | |
177 | SetIcon(wxICON(mondrian)); | |
178 | ||
179 | #if wxUSE_MENUS | |
180 | // create a menu bar | |
181 | wxMenu *menuFile = new wxMenu; | |
182 | ||
183 | // the "About" item should be in the help menu | |
184 | wxMenu *helpMenu = new wxMenu; | |
185 | helpMenu->Append(Emulator_About, _T("&About...\tF1"), _T("Show about dialog")); | |
186 | ||
187 | menuFile->Append(Emulator_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); | |
188 | ||
189 | // now append the freshly created menu to the menu bar... | |
190 | wxMenuBar *menuBar = new wxMenuBar(); | |
191 | menuBar->Append(menuFile, _T("&File")); | |
192 | menuBar->Append(helpMenu, _T("&Help")); | |
193 | ||
194 | // ... and attach this menu bar to the frame | |
195 | SetMenuBar(menuBar); | |
196 | #endif // wxUSE_MENUS | |
197 | ||
198 | #if wxUSE_STATUSBAR | |
199 | // create a status bar just for fun (by default with 1 pane only) | |
200 | CreateStatusBar(2); | |
201 | SetStatusText(_T("Welcome to wxWindows!")); | |
202 | #endif // wxUSE_STATUSBAR | |
203 | } | |
204 | ||
205 | ||
206 | // event handlers | |
207 | ||
208 | void wxEmulatorFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
209 | { | |
210 | // TRUE is to force the frame to close | |
211 | Close(TRUE); | |
212 | } | |
213 | ||
214 | void wxEmulatorFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
215 | { | |
216 | wxString msg; | |
217 | msg.Printf( _T("wxEmulator is an environment for testing embedded X11 apps.\n")); | |
218 | ||
219 | wxMessageBox(msg, _T("About wxEmulator"), wxOK | wxICON_INFORMATION, this); | |
220 | } | |
221 | ||
222 | IMPLEMENT_CLASS(wxEmulatorContainer, wxWindow) | |
223 | ||
224 | BEGIN_EVENT_TABLE(wxEmulatorContainer, wxWindow) | |
225 | EVT_SIZE(wxEmulatorContainer::OnSize) | |
226 | EVT_PAINT(wxEmulatorContainer::OnPaint) | |
227 | EVT_ERASE_BACKGROUND(wxEmulatorContainer::OnEraseBackground) | |
228 | END_EVENT_TABLE() | |
229 | ||
230 | wxEmulatorContainer::wxEmulatorContainer(wxWindow* parent, wxWindowID id): | |
231 | wxWindow(parent, id, wxDefaultPosition, wxDefaultSize) | |
232 | { | |
233 | } | |
234 | ||
235 | void wxEmulatorContainer::OnSize(wxSizeEvent& event) | |
236 | { | |
237 | DoResize(); | |
238 | } | |
239 | ||
240 | void wxEmulatorContainer::DoResize() | |
241 | { | |
242 | wxSize sz = GetClientSize(); | |
243 | if (wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.Ok() && | |
244 | wxGetApp().m_xnestWindow) | |
245 | { | |
246 | int bitmapWidth = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetWidth(); | |
247 | int bitmapHeight = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetHeight(); | |
248 | ||
249 | int x = wxMax(0, (int) ((sz.x - bitmapWidth)/2.0)); | |
250 | int y = wxMax(0, (int) ((sz.y - bitmapHeight)/2.0)); | |
251 | ||
252 | x += wxGetApp().m_emulatorInfo.m_emulatorScreenPosition.x; | |
253 | y += wxGetApp().m_emulatorInfo.m_emulatorScreenPosition.y; | |
254 | ||
255 | wxGetApp().m_xnestWindow->Move(x, y); | |
256 | } | |
257 | Refresh(); | |
258 | } | |
259 | ||
260 | void wxEmulatorContainer::OnPaint(wxPaintEvent& event) | |
261 | { | |
262 | wxPaintDC dc(this); | |
263 | ||
264 | wxSize sz = GetClientSize(); | |
265 | if (wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.Ok()) | |
266 | { | |
267 | int bitmapWidth = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetWidth(); | |
268 | int bitmapHeight = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetHeight(); | |
269 | ||
270 | int x = wxMax(0, (int) ((sz.x - bitmapWidth)/2.0)); | |
271 | int y = wxMax(0, (int) ((sz.y - bitmapHeight)/2.0)); | |
272 | ||
273 | dc.DrawBitmap(wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap, x, y); | |
274 | } | |
275 | } | |
276 | ||
277 | void wxEmulatorContainer::OnEraseBackground(wxEraseEvent& event) | |
278 | { | |
279 | wxDC* dc = NULL; | |
280 | ||
281 | if (event.GetDC()) | |
282 | { | |
283 | dc = event.GetDC(); | |
284 | } | |
285 | else | |
286 | { | |
287 | dc = new wxClientDC(this); | |
288 | } | |
289 | ||
290 | dc->SetBackground(wxBrush(wxGetApp().m_emulatorInfo.m_emulatorBackgroundColour, wxSOLID)); | |
291 | dc->Clear(); | |
292 | ||
293 | if (!event.GetDC()) | |
294 | delete dc; | |
295 | } | |
296 | ||
297 | // Information about the emulator decorations | |
298 | ||
299 | void wxEmulatorInfo::Copy(const wxEmulatorInfo& info) | |
300 | { | |
301 | m_emulatorTitle = info.m_emulatorTitle; | |
302 | m_emulatorDescription = info.m_emulatorDescription; | |
303 | m_emulatorScreenPosition = info.m_emulatorScreenPosition; | |
304 | m_emulatorScreenSize = info.m_emulatorScreenSize; | |
305 | m_emulatorBackgroundBitmap = info.m_emulatorBackgroundBitmap; | |
306 | m_emulatorBackgroundBitmapName = info.m_emulatorBackgroundBitmapName; | |
307 | m_emulatorBackgroundColour = info.m_emulatorBackgroundColour; | |
308 | } | |
309 | ||
310 | // Initialisation | |
311 | void wxEmulatorInfo::Init() | |
312 | { | |
313 | } | |
314 | ||
315 | // Loads bitmaps | |
316 | bool wxEmulatorInfo::Load() | |
317 | { | |
318 | if (m_emulatorBackgroundBitmapName.IsEmpty()) | |
319 | return FALSE; | |
320 | ||
321 | // TODO: prepend current directory if relative name | |
322 | int type = wxDetermineImageType(m_emulatorBackgroundBitmapName); | |
323 | if (type == -1) | |
324 | return FALSE; | |
325 | ||
326 | return m_emulatorBackgroundBitmap.LoadFile(m_emulatorBackgroundBitmapName, type); | |
327 | } | |
328 | ||
329 | // Returns the image type, or -1, determined from the extension. | |
330 | int wxDetermineImageType(const wxString& filename) | |
331 | { | |
332 | wxString path, name, ext; | |
333 | ||
334 | wxSplitPath(filename, & path, & name, & ext); | |
335 | ||
336 | ext.MakeLower(); | |
337 | if (ext == "jpg" || ext == "jpeg") | |
338 | return wxBITMAP_TYPE_JPEG; | |
339 | else if (ext == "gif") | |
340 | return wxBITMAP_TYPE_GIF; | |
341 | else if (ext == "bmp") | |
342 | return wxBITMAP_TYPE_BMP; | |
343 | else if (ext == "png") | |
344 | return wxBITMAP_TYPE_PNG; | |
345 | else if (ext == "pcx") | |
346 | return wxBITMAP_TYPE_PCX; | |
347 | else if (ext == "tif" || ext == "tiff") | |
348 | return wxBITMAP_TYPE_TIF; | |
349 | else | |
350 | return -1; | |
351 | } | |
352 | ||
353 |