]> git.saurik.com Git - wxWidgets.git/blob - utils/emulator/src/emulator.cpp
Added emulator utility.
[wxWidgets.git] / utils / emulator / src / emulator.cpp
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 #include "emulator.h"
38
39 #ifdef __WXX11__
40 #include "wx/x11/reparent.h"
41 #endif
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 (int) m_emulatorScreenSize.x, (int) m_emulatorScreenSize.y);
118
119 // Asynchronously executes Xnest
120 if (0 == wxExecute(cmd))
121 {
122 frame->Destroy();
123 wxMessageBox(wxT("Sorry, could not run Xnest. Please check your PATH."));
124 return FALSE;
125 }
126
127 wxReparenter reparenter;
128 if (!reparenter.WaitAndReparent(m_containerWindow, m_xnestWindow, wxT("Xnest")))
129 {
130 wxMessageBox(wxT("Sorry, could not reparent Xnest.."));
131 frame->Destroy();
132 return FALSE;
133 }
134 #endif
135
136 // success: wxApp::OnRun() will be called which will enter the main message
137 // loop and the application will run. If we returned FALSE here, the
138 // application would exit immediately.
139 return TRUE;
140 }
141
142 // Load the specified emulator.
143 // For now, hard-wired. TODO: make this configurable
144 bool wxEmulatorApp::LoadEmulator()
145 {
146 m_emulatorInfo.m_emulatorTitle = wxT("iPAQ Emulator");
147
148 m_emulatorInfo.m_emulatorDescription = wxT("No description yet");
149
150 // The offset from the top-left of the main emulator
151 // bitmap and the virtual screen (where Xnest is
152 // positioned)
153 m_emulatorInfo.m_emulatorScreenPosition = wxPoint(45, 57);
154
155 // The emulated screen size
156 m_emulatorInfo.m_emulatorScreenSize = wxSize(240, 320);
157
158 m_emulatorInfo.m_emulatorBackgroundBitmapName = wxT("ipaq01.jpg");
159
160 m_emulatorInfo.m_emulatorBackgroundColour = * wxBLACK;
161
162 return m_emulatorInfo.Load();
163 }
164
165 // ----------------------------------------------------------------------------
166 // main frame
167 // ----------------------------------------------------------------------------
168
169 // frame constructor
170 wxEmulatorFrame::wxEmulatorFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
171 : wxFrame(NULL, -1, title, pos, size)
172 {
173 // set the frame icon
174 SetIcon(wxICON(mondrian));
175
176 #if wxUSE_MENUS
177 // create a menu bar
178 wxMenu *menuFile = new wxMenu;
179
180 // the "About" item should be in the help menu
181 wxMenu *helpMenu = new wxMenu;
182 helpMenu->Append(Emulator_About, _T("&About...\tF1"), _T("Show about dialog"));
183
184 menuFile->Append(Emulator_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
185
186 // now append the freshly created menu to the menu bar...
187 wxMenuBar *menuBar = new wxMenuBar();
188 menuBar->Append(menuFile, _T("&File"));
189 menuBar->Append(helpMenu, _T("&Help"));
190
191 // ... and attach this menu bar to the frame
192 SetMenuBar(menuBar);
193 #endif // wxUSE_MENUS
194
195 #if wxUSE_STATUSBAR
196 // create a status bar just for fun (by default with 1 pane only)
197 CreateStatusBar(2);
198 SetStatusText(_T("Welcome to wxWindows!"));
199 #endif // wxUSE_STATUSBAR
200 }
201
202
203 // event handlers
204
205 void wxEmulatorFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
206 {
207 // TRUE is to force the frame to close
208 Close(TRUE);
209 }
210
211 void wxEmulatorFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
212 {
213 wxString msg;
214 msg.Printf( _T("wxEmulator is an environment for testing embedded X11 apps.\n"));
215
216 wxMessageBox(msg, _T("About wxEmulator"), wxOK | wxICON_INFORMATION, this);
217 }
218
219 IMPLEMENT_CLASS(wxEmulatorContainer, wxWindow)
220
221 BEGIN_EVENT_TABLE(wxEmulatorContainer, wxWindow)
222 EVT_SIZE(wxEmulatorContainer::OnSize)
223 EVT_PAINT(wxEmulatorContainer::OnPaint)
224 EVT_ERASE_BACKGROUND(wxEmulatorContainer::OnEraseBackground)
225 END_EVENT_TABLE()
226
227 wxEmulatorContainer::wxEmulatorContainer(wxWindow* parent, wxWindowID id):
228 wxWindow(parent, id, wxDefaultPosition, wxDefaultSize)
229 {
230 }
231
232 void wxEmulatorContainer::OnSize(wxSizeEvent& event)
233 {
234 wxSize sz = GetClientSize();
235 if (wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.Ok() &&
236 wxGetApp().m_xnestWindow)
237 {
238 int bitmapWidth = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetWidth();
239 int bitmapHeight = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetHeight();
240
241 int x = wxMax(0, (sz.x - bitmapWidth)/2.0);
242 int y = wxMax(0, (sz.y - bitmapHeight)/2.0);
243
244 x += wxGetApp().m_emulatorInfo.m_emulatorScreenPosition.x;
245 y += wxGetApp().m_emulatorInfo.m_emulatorScreenPosition.y;
246
247 wxGetApp().m_xnestWindow->Move(x, y);
248 }
249 Refresh();
250 }
251
252 void wxEmulatorContainer::OnPaint(wxPaintEvent& event)
253 {
254 wxPaintDC dc(this);
255
256 wxSize sz = GetClientSize();
257 if (wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.Ok())
258 {
259 int bitmapWidth = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetWidth();
260 int bitmapHeight = wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap.GetHeight();
261
262 int x = wxMax(0, (sz.x - bitmapWidth)/2.0);
263 int y = wxMax(0, (sz.y - bitmapHeight)/2.0);
264
265 dc.DrawBitmap(wxGetApp().m_emulatorInfo.m_emulatorBackgroundBitmap, x, y);
266 }
267 }
268
269 void wxEmulatorContainer::OnEraseBackground(wxEraseEvent& event)
270 {
271 wxDC* dc = NULL;
272
273 if (event.GetDC())
274 {
275 dc = event.GetDC();
276 }
277 else
278 {
279 dc = new wxClientDC(this);
280 }
281
282 dc->SetBackground(wxBrush(wxGetApp().m_emulatorInfo.m_emulatorBackgroundColour, wxSOLID));
283 dc->Clear();
284
285 if (!event.GetDC())
286 delete dc;
287 }
288
289 // Information about the emulator decorations
290
291 void wxEmulatorInfo::Copy(const wxEmulatorInfo& info)
292 {
293 m_emulatorTitle = info.m_emulatorTitle;
294 m_emulatorDescription = info.m_emulatorDescription;
295 m_emulatorScreenPosition = info.m_emulatorScreenPosition;
296 m_emulatorScreenSize = info.m_emulatorScreenSize;
297 m_emulatorBackgroundBitmap = info.m_emulatorBackgroundBitmap;
298 m_emulatorBackgroundBitmapName = info.m_emulatorBackgroundBitmapName;
299 m_emulatorBackgroundColour = info.m_emulatorBackgroundColour;
300 }
301
302 // Initialisation
303 void wxEmulatorInfo::Init()
304 {
305 }
306
307 // Loads bitmaps
308 bool wxEmulatorInfo::Load()
309 {
310 if (m_emulatorBackgroundBitmapName.IsEmpty())
311 return FALSE;
312
313 // TODO: prepend current directory if relative name
314 int type = wxDetermineImageType(m_emulatorBackgroundBitmapName);
315 if (type == -1)
316 return FALSE;
317
318 return m_emulatorBackgroundBitmap.LoadFile(m_emulatorBackgroundBitmapName, type);
319 }
320
321 // Returns the image type, or -1, determined from the extension.
322 int wxDetermineImageType(const wxString& filename)
323 {
324 wxString path, name, ext;
325
326 wxSplitPath(filename, & path, & name, & ext);
327
328 ext.MakeLower();
329 if (ext == "jpg" || ext == "jpeg")
330 return wxBITMAP_TYPE_JPEG;
331 else if (ext == "gif")
332 return wxBITMAP_TYPE_GIF;
333 else if (ext == "bmp")
334 return wxBITMAP_TYPE_BMP;
335 else if (ext == "png")
336 return wxBITMAP_TYPE_PNG;
337 else if (ext == "pcx")
338 return wxBITMAP_TYPE_PCX;
339 else if (ext == "tif" || ext == "tiff")
340 return wxBITMAP_TYPE_TIF;
341 else
342 return -1;
343 }
344
345