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)
37 #include "wx/confbase.h"
38 #include "wx/fileconf.h"
39 #include "wx/cmdline.h"
42 #include "wx/x11/reparent.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // the application icon (under Windows and OS/2 it is in resources)
52 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
53 #include "emulator.xpm"
56 // ----------------------------------------------------------------------------
57 // event tables and other macros for wxWindows
58 // ----------------------------------------------------------------------------
60 // the event tables connect the wxWindows events with the functions (event
61 // handlers) which process them. It can be also done at run-time, but for the
62 // simple menu events like this the static method is much simpler.
63 BEGIN_EVENT_TABLE(wxEmulatorFrame
, wxFrame
)
64 EVT_MENU(Emulator_Quit
, wxEmulatorFrame::OnQuit
)
65 EVT_MENU(Emulator_About
, wxEmulatorFrame::OnAbout
)
66 EVT_CLOSE(wxEmulatorFrame::OnCloseWindow
)
69 // Create a new application object: this macro will allow wxWindows to create
70 // the application object during program execution (it's better than using a
71 // static object for many reasons) and also declares the accessor function
72 // wxGetApp() which will return the reference of the right type (i.e. wxEmulatorApp and
74 IMPLEMENT_APP(wxEmulatorApp
)
76 static const wxCmdLineEntryDesc sg_cmdLineDesc
[] =
78 { wxCMD_LINE_OPTION
, "u", "use-display", "display number to use (default 100)" },
80 { wxCMD_LINE_SWITCH
, "h", "help", "displays help on the command line parameters" },
81 { wxCMD_LINE_SWITCH
, "v", "version", "print version" },
83 { wxCMD_LINE_PARAM
, NULL
, NULL
, "config file 1", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_OPTIONAL
},
89 // ============================================================================
91 // ============================================================================
93 // ----------------------------------------------------------------------------
94 // the application class
95 // ----------------------------------------------------------------------------
97 wxEmulatorApp::wxEmulatorApp()
100 m_containerWindow
= NULL
;
101 m_displayNumber
= wxT("100");
106 // 'Main program' equivalent: the program execution "starts" here
107 bool wxEmulatorApp::OnInit()
109 wxLog::SetTimestamp(NULL
);
110 wxInitAllImageHandlers();
112 wxString currentDir
= wxGetCwd();
114 // Use argv to get current app directory
115 m_appDir
= wxFindAppPath(argv
[0], currentDir
, wxT("WXEMUDIR"));
117 // If the development version, go up a directory.
119 if ((m_appDir
.Right(5).CmpNoCase("DEBUG") == 0) ||
120 (m_appDir
.Right(11).CmpNoCase("DEBUGSTABLE") == 0) ||
121 (m_appDir
.Right(7).CmpNoCase("RELEASE") == 0) ||
122 (m_appDir
.Right(13).CmpNoCase("RELEASESTABLE") == 0)
124 m_appDir
= wxPathOnly(m_appDir
);
127 // Parse the command-line parameters and options
128 wxCmdLineParser
parser(sg_cmdLineDesc
, argc
, argv
);
132 res
= parser
.Parse();
134 if (res
== -1 || res
> 0 || parser
.Found(wxT("h")))
137 wxLog::SetActiveTarget(new wxLogStderr
);
142 if (parser
.Found(wxT("v")))
145 wxLog::SetActiveTarget(new wxLogStderr
);
148 msg
.Printf(wxT("wxWindows PDA Emulator (c) Julian Smart, 2002 Version %.2f, %s"), wxEMULATOR_VERSION
, __DATE__
);
152 if (parser
.Found(wxT("u"), & m_displayNumber
))
154 // Should only be number, so strip out anything before
155 // and including a : character
156 if (m_displayNumber
.Find(wxT(':')) != -1)
158 m_displayNumber
= m_displayNumber
.AfterFirst(wxT(':'));
161 if (parser
.GetParamCount() == 0)
163 m_emulatorInfo
.m_emulatorFilename
= wxT("default.wxe");
165 else if (parser
.GetParamCount() > 0)
167 m_emulatorInfo
.m_emulatorFilename
= parser
.GetParam(0);
170 // Load the emulation info
171 if (!LoadEmulator(m_appDir
))
173 //wxMessageBox(wxT("Sorry, could not load this emulator. Please check bitmaps are valid."));
177 // create the main application window
178 wxEmulatorFrame
*frame
= new wxEmulatorFrame(_T("wxEmulator"),
179 wxPoint(50, 50), wxSize(450, 340));
181 frame
->SetStatusText(m_emulatorInfo
.m_emulatorTitle
, 0);
184 sizeStr
.Printf(wxT("Screen: %dx%d"), (int) m_emulatorInfo
.m_emulatorScreenSize
.x
,
185 (int) m_emulatorInfo
.m_emulatorScreenSize
.y
);
186 frame
->SetStatusText(sizeStr
, 1);
188 m_containerWindow
= new wxEmulatorContainer(frame
, -1);
190 frame
->SetClientSize(m_emulatorInfo
.m_emulatorDeviceSize
.x
,
191 m_emulatorInfo
.m_emulatorDeviceSize
.y
);
193 // and show it (the frames, unlike simple controls, are not shown when
194 // created initially)
198 m_xnestWindow
= new wxAdoptedWindow
;
201 cmd
.Printf(wxT("Xnest :%s -geometry %dx%d"),
202 m_displayNumber
.c_str(),
203 (int) m_emulatorInfo
.m_emulatorScreenSize
.x
,
204 (int) m_emulatorInfo
.m_emulatorScreenSize
.y
);
206 // Asynchronously executes Xnest
207 m_xnestPID
= wxExecute(cmd
);
211 wxMessageBox(wxT("Sorry, could not run Xnest. Please check your PATH."));
215 wxReparenter reparenter
;
216 if (!reparenter
.WaitAndReparent(m_containerWindow
, m_xnestWindow
, wxT("Xnest")))
218 wxMessageBox(wxT("Sorry, could not reparent Xnest.."));
224 m_containerWindow
->DoResize();
226 // success: wxApp::OnRun() will be called which will enter the main message
227 // loop and the application will run. If we returned FALSE here, the
228 // application would exit immediately.
232 // Prepend the current program directory to the name
233 wxString
wxEmulatorApp::GetFullAppPath(const wxString
& filename
) const
235 wxString
path(m_appDir
);
236 if (path
.Last() != '\\' && path
.Last() != '/' && filename
[0] != '\\' && filename
[0] != '/')
248 // Load the specified emulator.
249 // For now, hard-wired. TODO: make this configurable
250 bool wxEmulatorApp::LoadEmulator(const wxString
& appDir
)
252 // Load config file and bitmaps
253 return m_emulatorInfo
.Load(appDir
);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
261 wxEmulatorFrame::wxEmulatorFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
262 : wxFrame(NULL
, -1, title
, pos
, size
)
264 // set the frame icon
265 SetIcon(wxICON(emulator
));
269 wxMenu
*menuFile
= new wxMenu
;
271 // the "About" item should be in the help menu
272 wxMenu
*helpMenu
= new wxMenu
;
273 helpMenu
->Append(Emulator_About
, _T("&About...\tF1"), _T("Show about dialog"));
275 menuFile
->Append(Emulator_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
277 // now append the freshly created menu to the menu bar...
278 wxMenuBar
*menuBar
= new wxMenuBar();
279 menuBar
->Append(menuFile
, _T("&File"));
280 menuBar
->Append(helpMenu
, _T("&Help"));
282 // ... and attach this menu bar to the frame
284 #endif // wxUSE_MENUS
287 // create a status bar just for fun (by default with 1 pane only)
289 #endif // wxUSE_STATUSBAR
295 void wxEmulatorFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
297 // TRUE is to force the frame to close
301 void wxEmulatorFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
304 msg
.Printf( _T("wxEmulator is an environment for testing embedded X11 apps.\n"));
306 wxMessageBox(msg
, _T("About wxEmulator"), wxOK
| wxICON_INFORMATION
, this);
309 void wxEmulatorFrame::OnCloseWindow(wxCloseEvent
& event
)
312 if (wxGetApp().m_xnestWindow
)
314 wxGetApp().m_xnestWindow
->SetHandle((WXWindow
) NULL
);
318 if (wxGetApp().m_xnestPID
> 0)
320 wxKill(wxGetApp().m_xnestPID
);
321 wxGetApp().m_xnestPID
= 0;
325 IMPLEMENT_CLASS(wxEmulatorContainer
, wxWindow
)
327 BEGIN_EVENT_TABLE(wxEmulatorContainer
, wxWindow
)
328 EVT_SIZE(wxEmulatorContainer::OnSize
)
329 EVT_PAINT(wxEmulatorContainer::OnPaint
)
330 EVT_ERASE_BACKGROUND(wxEmulatorContainer::OnEraseBackground
)
333 wxEmulatorContainer::wxEmulatorContainer(wxWindow
* parent
, wxWindowID id
):
334 wxWindow(parent
, id
, wxDefaultPosition
, wxDefaultSize
)
338 void wxEmulatorContainer::OnSize(wxSizeEvent
& event
)
343 void wxEmulatorContainer::DoResize()
345 wxSize sz
= GetClientSize();
346 if (wxGetApp().m_xnestWindow
348 && wxGetApp().m_xnestWindow
->GetMainWindow()
352 int deviceWidth
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.x
;
353 int deviceHeight
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.y
;
355 int x
= wxMax(0, (int) ((sz
.x
- deviceWidth
)/2.0));
356 int y
= wxMax(0, (int) ((sz
.y
- deviceHeight
)/2.0));
358 x
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.x
;
359 y
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.y
;
361 wxGetApp().m_xnestWindow
->Move(x
, y
);
366 void wxEmulatorContainer::OnPaint(wxPaintEvent
& event
)
370 wxSize sz
= GetClientSize();
371 if (wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.Ok())
373 int deviceWidth
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.x
;
374 int deviceHeight
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.y
;
376 int x
= wxMax(0, (int) ((sz
.x
- deviceWidth
)/2.0));
377 int y
= wxMax(0, (int) ((sz
.y
- deviceHeight
)/2.0));
379 dc
.DrawBitmap(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
, x
, y
);
383 void wxEmulatorContainer::OnEraseBackground(wxEraseEvent
& event
)
393 dc
= new wxClientDC(this);
396 dc
->SetBackground(wxBrush(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundColour
, wxSOLID
));
403 // Information about the emulator decorations
405 void wxEmulatorInfo::Copy(const wxEmulatorInfo
& info
)
407 m_emulatorFilename
= info
.m_emulatorFilename
;
408 m_emulatorTitle
= info
.m_emulatorTitle
;
409 m_emulatorDescription
= info
.m_emulatorDescription
;
410 m_emulatorScreenPosition
= info
.m_emulatorScreenPosition
;
411 m_emulatorScreenSize
= info
.m_emulatorScreenSize
;
412 m_emulatorBackgroundBitmap
= info
.m_emulatorBackgroundBitmap
;
413 m_emulatorBackgroundBitmapName
= info
.m_emulatorBackgroundBitmapName
;
414 m_emulatorBackgroundColour
= info
.m_emulatorBackgroundColour
;
415 m_emulatorDeviceSize
= info
.m_emulatorDeviceSize
;
419 void wxEmulatorInfo::Init()
421 m_emulatorDeviceSize
= wxSize(260, 340);
422 m_emulatorScreenSize
= wxSize(240, 320);
426 bool wxEmulatorInfo::Load(const wxString
& appDir
)
428 // Try to find absolute path
429 wxString absoluteConfigPath
= m_emulatorFilename
;
430 if (!wxIsAbsolutePath(absoluteConfigPath
))
432 wxString currDir
= wxGetCwd();
433 absoluteConfigPath
= currDir
+ wxString(wxFILE_SEP_PATH
) + m_emulatorFilename
;
434 if (!wxFileExists(absoluteConfigPath
))
436 absoluteConfigPath
= appDir
+ wxString(wxFILE_SEP_PATH
) + m_emulatorFilename
;
439 if (!wxFileExists(absoluteConfigPath
))
442 str
.Printf(wxT("Could not find config file %s"), absoluteConfigPath
.c_str()),
447 wxString rootPath
= wxPathOnly(absoluteConfigPath
);
450 wxFileConfig
config(wxT("wxEmulator"), wxT("wxWindows"),
451 absoluteConfigPath
, wxEmptyString
, wxCONFIG_USE_LOCAL_FILE
);
453 config
.Read(wxT("/General/title"), & m_emulatorTitle
);
454 config
.Read(wxT("/General/description"), & m_emulatorDescription
);
455 config
.Read(wxT("/General/backgroundBitmap"), & m_emulatorBackgroundBitmapName
);
458 if (config
.Read(wxT("/General/backgroundColour"), & colString
) ||
459 config
.Read(wxT("/General/backgroundColor"), & colString
)
462 m_emulatorBackgroundColour
= wxHexStringToColour(colString
);
465 int x
= 0, y
= 0, w
= 0, h
= 0, dw
= 0, dh
= 0;
466 config
.Read(wxT("/General/screenX"), & x
);
467 config
.Read(wxT("/General/screenY"), & y
);
468 config
.Read(wxT("/General/screenWidth"), & w
);
469 config
.Read(wxT("/General/screenHeight"), & h
);
470 if (config
.Read(wxT("/General/deviceWidth"), & dw
) && config
.Read(wxT("/General/deviceHeight"), & dh
))
472 m_emulatorDeviceSize
= wxSize(dw
, dh
);
475 m_emulatorScreenPosition
= wxPoint(x
, y
);
476 m_emulatorScreenSize
= wxSize(w
, h
);
479 if (!m_emulatorBackgroundBitmapName
.IsEmpty())
481 wxString absoluteBackgroundBitmapName
= rootPath
+ wxString(wxFILE_SEP_PATH
) + m_emulatorBackgroundBitmapName
;
482 if (!wxFileExists(absoluteBackgroundBitmapName
))
485 str
.Printf(wxT("Could not find bitmap %s"), absoluteBackgroundBitmapName
.c_str()),
490 int type
= wxDetermineImageType(m_emulatorBackgroundBitmapName
);
494 if (!m_emulatorBackgroundBitmap
.LoadFile(m_emulatorBackgroundBitmapName
, type
))
497 str
.Printf(wxT("Could not load bitmap file %s"), m_emulatorBackgroundBitmapName
.c_str()),
501 m_emulatorDeviceSize
= wxSize(m_emulatorBackgroundBitmap
.GetWidth(),
502 m_emulatorBackgroundBitmap
.GetHeight());
507 // Returns the image type, or -1, determined from the extension.
508 int wxDetermineImageType(const wxString
& filename
)
510 wxString path
, name
, ext
;
512 wxSplitPath(filename
, & path
, & name
, & ext
);
515 if (ext
== "jpg" || ext
== "jpeg")
516 return wxBITMAP_TYPE_JPEG
;
517 else if (ext
== "gif")
518 return wxBITMAP_TYPE_GIF
;
519 else if (ext
== "bmp")
520 return wxBITMAP_TYPE_BMP
;
521 else if (ext
== "png")
522 return wxBITMAP_TYPE_PNG
;
523 else if (ext
== "pcx")
524 return wxBITMAP_TYPE_PCX
;
525 else if (ext
== "tif" || ext
== "tiff")
526 return wxBITMAP_TYPE_TIF
;
531 // Convert a colour to a 6-digit hex string
532 wxString
wxColourToHexString(const wxColour
& col
)
536 hex
+= wxDecToHex(col
.Red());
537 hex
+= wxDecToHex(col
.Green());
538 hex
+= wxDecToHex(col
.Blue());
543 // Convert 6-digit hex string to a colour
544 wxColour
wxHexStringToColour(const wxString
& hex
)
549 r
= wxHexToDec(hex
.Mid(0, 2));
550 g
= wxHexToDec(hex
.Mid(2, 2));
551 b
= wxHexToDec(hex
.Mid(4, 2));
553 return wxColour(r
, g
, b
);
556 // Find the absolute path where this application has been run from.
557 // argv0 is wxTheApp->argv[0]
558 // cwd is the current working directory (at startup)
559 // appVariableName is the name of a variable containing the directory for this app, e.g.
560 // MYAPPDIR. This is checked first.
562 wxString
wxFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
566 // Try appVariableName
567 if (!appVariableName
.IsEmpty())
569 str
= wxGetenv(appVariableName
);
574 if (wxIsAbsolutePath(argv0
))
575 return wxPathOnly(argv0
);
578 // Is it a relative path?
579 wxString
currentDir(cwd
);
580 if (currentDir
.Last() != wxFILE_SEP_PATH
)
581 currentDir
+= wxFILE_SEP_PATH
;
583 str
= currentDir
+ argv0
;
584 if (wxFileExists(str
))
585 return wxPathOnly(str
);
588 // OK, it's neither an absolute path nor a relative path.
592 pathList
.AddEnvList(wxT("PATH"));
593 str
= pathList
.FindAbsoluteValidPath(argv0
);
595 return wxPathOnly(str
);
598 return wxEmptyString
;