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)
38 #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 cmd
.Printf(wxT("Xnest :100 -geometry %dx%d"),
118 (int) m_emulatorInfo
.m_emulatorScreenSize
.x
, (int) m_emulatorInfo
.m_emulatorScreenSize
.y
);
120 // Asynchronously executes Xnest
121 if (0 == wxExecute(cmd
))
124 wxMessageBox(wxT("Sorry, could not run Xnest. Please check your PATH."));
128 wxReparenter reparenter
;
129 if (!reparenter
.WaitAndReparent(m_containerWindow
, m_xnestWindow
, wxT("Xnest")))
131 wxMessageBox(wxT("Sorry, could not reparent Xnest.."));
136 m_containerWindow
->DoResize();
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.
145 // Load the specified emulator.
146 // For now, hard-wired. TODO: make this configurable
147 bool wxEmulatorApp::LoadEmulator()
149 m_emulatorInfo
.m_emulatorTitle
= wxT("iPAQ Emulator");
151 m_emulatorInfo
.m_emulatorDescription
= wxT("No description yet");
153 // The offset from the top-left of the main emulator
154 // bitmap and the virtual screen (where Xnest is
156 m_emulatorInfo
.m_emulatorScreenPosition
= wxPoint(56, 69);
158 // The emulated screen size
159 m_emulatorInfo
.m_emulatorScreenSize
= wxSize(240, 320);
161 m_emulatorInfo
.m_emulatorBackgroundBitmapName
= wxT("ipaq01.jpg");
163 m_emulatorInfo
.m_emulatorBackgroundColour
= * wxBLACK
;
165 return m_emulatorInfo
.Load();
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
173 wxEmulatorFrame::wxEmulatorFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
174 : wxFrame(NULL
, -1, title
, pos
, size
)
176 // set the frame icon
177 SetIcon(wxICON(mondrian
));
181 wxMenu
*menuFile
= new wxMenu
;
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"));
187 menuFile
->Append(Emulator_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
194 // ... and attach this menu bar to the frame
196 #endif // wxUSE_MENUS
199 // create a status bar just for fun (by default with 1 pane only)
201 SetStatusText(_T("Welcome to wxWindows!"));
202 #endif // wxUSE_STATUSBAR
208 void wxEmulatorFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
210 // TRUE is to force the frame to close
214 void wxEmulatorFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
217 msg
.Printf( _T("wxEmulator is an environment for testing embedded X11 apps.\n"));
219 wxMessageBox(msg
, _T("About wxEmulator"), wxOK
| wxICON_INFORMATION
, this);
222 IMPLEMENT_CLASS(wxEmulatorContainer
, wxWindow
)
224 BEGIN_EVENT_TABLE(wxEmulatorContainer
, wxWindow
)
225 EVT_SIZE(wxEmulatorContainer::OnSize
)
226 EVT_PAINT(wxEmulatorContainer::OnPaint
)
227 EVT_ERASE_BACKGROUND(wxEmulatorContainer::OnEraseBackground
)
230 wxEmulatorContainer::wxEmulatorContainer(wxWindow
* parent
, wxWindowID id
):
231 wxWindow(parent
, id
, wxDefaultPosition
, wxDefaultSize
)
235 void wxEmulatorContainer::OnSize(wxSizeEvent
& event
)
240 void wxEmulatorContainer::DoResize()
242 wxSize sz
= GetClientSize();
243 if (wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.Ok() &&
244 wxGetApp().m_xnestWindow
)
246 int bitmapWidth
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetWidth();
247 int bitmapHeight
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetHeight();
249 int x
= wxMax(0, (int) ((sz
.x
- bitmapWidth
)/2.0));
250 int y
= wxMax(0, (int) ((sz
.y
- bitmapHeight
)/2.0));
252 x
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.x
;
253 y
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.y
;
255 wxGetApp().m_xnestWindow
->Move(x
, y
);
260 void wxEmulatorContainer::OnPaint(wxPaintEvent
& event
)
264 wxSize sz
= GetClientSize();
265 if (wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.Ok())
267 int bitmapWidth
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetWidth();
268 int bitmapHeight
= wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.GetHeight();
270 int x
= wxMax(0, (int) ((sz
.x
- bitmapWidth
)/2.0));
271 int y
= wxMax(0, (int) ((sz
.y
- bitmapHeight
)/2.0));
273 dc
.DrawBitmap(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
, x
, y
);
277 void wxEmulatorContainer::OnEraseBackground(wxEraseEvent
& event
)
287 dc
= new wxClientDC(this);
290 dc
->SetBackground(wxBrush(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundColour
, wxSOLID
));
297 // Information about the emulator decorations
299 void wxEmulatorInfo::Copy(const wxEmulatorInfo
& info
)
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
;
311 void wxEmulatorInfo::Init()
316 bool wxEmulatorInfo::Load()
318 if (m_emulatorBackgroundBitmapName
.IsEmpty())
321 // TODO: prepend current directory if relative name
322 int type
= wxDetermineImageType(m_emulatorBackgroundBitmapName
);
326 return m_emulatorBackgroundBitmap
.LoadFile(m_emulatorBackgroundBitmapName
, type
);
329 // Returns the image type, or -1, determined from the extension.
330 int wxDetermineImageType(const wxString
& filename
)
332 wxString path
, name
, ext
;
334 wxSplitPath(filename
, & path
, & name
, & ext
);
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
;