1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Emulator wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "emulator.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers)
40 #include "wx/x11/reparent.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
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"
52 // ----------------------------------------------------------------------------
53 // event tables and other macros for wxWindows
54 // ----------------------------------------------------------------------------
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
)
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
69 IMPLEMENT_APP(wxEmulatorApp
)
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
76 // the application class
77 // ----------------------------------------------------------------------------
79 wxEmulatorApp::wxEmulatorApp()
82 m_containerWindow
= NULL
;
85 // 'Main program' equivalent: the program execution "starts" here
86 bool wxEmulatorApp::OnInit()
88 wxInitAllImageHandlers();
90 // create the main application window
91 wxEmulatorFrame
*frame
= new wxEmulatorFrame(_T("wxEmulator"),
92 wxPoint(50, 50), wxSize(450, 340));
94 m_containerWindow
= new wxEmulatorContainer(frame
, -1);
96 // Load the emulation info
100 wxMessageBox(wxT("Sorry, could not load this emulator. Please check bitmaps are valid."));
104 if (m_emulatorInfo
.m_emulatorBackgroundBitmap
.Ok())
105 frame
->SetClientSize(m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetWidth(),
106 m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetHeight());
108 // and show it (the frames, unlike simple controls, are not shown when
109 // created initially)
113 m_xnestWindow
= new wxAdoptedWindow
;
116 cmd
.Printf(wxT("Xnest :100 -geometry %dx%d+50+50"),
117 (int) m_emulatorScreenSize
.x
, (int) m_emulatorScreenSize
.y
);
119 // Asynchronously executes Xnest
120 if (0 == wxExecute(cmd
))
123 wxMessageBox(wxT("Sorry, could not run Xnest. Please check your PATH."));
127 wxReparenter reparenter
;
128 if (!reparenter
.WaitAndReparent(m_containerWindow
, m_xnestWindow
, wxT("Xnest")))
130 wxMessageBox(wxT("Sorry, could not reparent Xnest.."));
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.
142 // Load the specified emulator.
143 // For now, hard-wired. TODO: make this configurable
144 bool wxEmulatorApp::LoadEmulator()
146 m_emulatorInfo
.m_emulatorTitle
= wxT("iPAQ Emulator");
148 m_emulatorInfo
.m_emulatorDescription
= wxT("No description yet");
150 // The offset from the top-left of the main emulator
151 // bitmap and the virtual screen (where Xnest is
153 m_emulatorInfo
.m_emulatorScreenPosition
= wxPoint(45, 57);
155 // The emulated screen size
156 m_emulatorInfo
.m_emulatorScreenSize
= wxSize(240, 320);
158 m_emulatorInfo
.m_emulatorBackgroundBitmapName
= wxT("ipaq01.jpg");
160 m_emulatorInfo
.m_emulatorBackgroundColour
= * wxBLACK
;
162 return m_emulatorInfo
.Load();
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
170 wxEmulatorFrame::wxEmulatorFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
171 : wxFrame(NULL
, -1, title
, pos
, size
)
173 // set the frame icon
174 SetIcon(wxICON(mondrian
));
178 wxMenu
*menuFile
= new wxMenu
;
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"));
184 menuFile
->Append(Emulator_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
191 // ... and attach this menu bar to the frame
193 #endif // wxUSE_MENUS
196 // create a status bar just for fun (by default with 1 pane only)
198 SetStatusText(_T("Welcome to wxWindows!"));
199 #endif // wxUSE_STATUSBAR
205 void wxEmulatorFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
207 // TRUE is to force the frame to close
211 void wxEmulatorFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
214 msg
.Printf( _T("wxEmulator is an environment for testing embedded X11 apps.\n"));
216 wxMessageBox(msg
, _T("About wxEmulator"), wxOK
| wxICON_INFORMATION
, this);
219 IMPLEMENT_CLASS(wxEmulatorContainer
, wxWindow
)
221 BEGIN_EVENT_TABLE(wxEmulatorContainer
, wxWindow
)
222 EVT_SIZE(wxEmulatorContainer::OnSize
)
223 EVT_PAINT(wxEmulatorContainer::OnPaint
)
224 EVT_ERASE_BACKGROUND(wxEmulatorContainer::OnEraseBackground
)
227 wxEmulatorContainer::wxEmulatorContainer(wxWindow
* parent
, wxWindowID id
):
228 wxWindow(parent
, id
, wxDefaultPosition
, wxDefaultSize
)
232 void wxEmulatorContainer::OnSize(wxSizeEvent
& event
)
234 wxSize sz
= GetClientSize();
235 if (wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.Ok() &&
236 wxGetApp().m_xnestWindow
)
238 int bitmapWidth
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetWidth();
239 int bitmapHeight
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetHeight();
241 int x
= wxMax(0, (sz
.x
- bitmapWidth
)/2.0);
242 int y
= wxMax(0, (sz
.y
- bitmapHeight
)/2.0);
244 x
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.x
;
245 y
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.y
;
247 wxGetApp().m_xnestWindow
->Move(x
, y
);
252 void wxEmulatorContainer::OnPaint(wxPaintEvent
& event
)
256 wxSize sz
= GetClientSize();
257 if (wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.Ok())
259 int bitmapWidth
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetWidth();
260 int bitmapHeight
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetHeight();
262 int x
= wxMax(0, (sz
.x
- bitmapWidth
)/2.0);
263 int y
= wxMax(0, (sz
.y
- bitmapHeight
)/2.0);
265 dc
.DrawBitmap(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
, x
, y
);
269 void wxEmulatorContainer::OnEraseBackground(wxEraseEvent
& event
)
279 dc
= new wxClientDC(this);
282 dc
->SetBackground(wxBrush(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundColour
, wxSOLID
));
289 // Information about the emulator decorations
291 void wxEmulatorInfo::Copy(const wxEmulatorInfo
& info
)
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
;
303 void wxEmulatorInfo::Init()
308 bool wxEmulatorInfo::Load()
310 if (m_emulatorBackgroundBitmapName
.IsEmpty())
313 // TODO: prepend current directory if relative name
314 int type
= wxDetermineImageType(m_emulatorBackgroundBitmapName
);
318 return m_emulatorBackgroundBitmap
.LoadFile(m_emulatorBackgroundBitmapName
, type
);
321 // Returns the image type, or -1, determined from the extension.
322 int wxDetermineImageType(const wxString
& filename
)
324 wxString path
, name
, ext
;
326 wxSplitPath(filename
, & path
, & name
, & ext
);
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
;