1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Emulator wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
43 #include "wx/x11/reparent.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the application icon (under Windows and OS/2 it is in resources)
53 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
54 #include "emulator.xpm"
57 // ----------------------------------------------------------------------------
58 // event tables and other macros for wxWindows
59 // ----------------------------------------------------------------------------
61 // the event tables connect the wxWindows events with the functions (event
62 // handlers) which process them. It can be also done at run-time, but for the
63 // simple menu events like this the static method is much simpler.
64 BEGIN_EVENT_TABLE(wxEmulatorFrame
, wxFrame
)
65 EVT_MENU(Emulator_Quit
, wxEmulatorFrame::OnQuit
)
66 EVT_MENU(Emulator_About
, wxEmulatorFrame::OnAbout
)
67 EVT_CLOSE(wxEmulatorFrame::OnCloseWindow
)
70 // Create a new application object: this macro will allow wxWindows to create
71 // the application object during program execution (it's better than using a
72 // static object for many reasons) and also declares the accessor function
73 // wxGetApp() which will return the reference of the right type (i.e. wxEmulatorApp and
75 IMPLEMENT_APP(wxEmulatorApp
)
77 static const wxCmdLineEntryDesc sg_cmdLineDesc
[] =
79 { wxCMD_LINE_OPTION
, _T("u"), _T("use-display"), _T("display number to use (default 100)") },
81 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("displays help on the command line parameters") },
82 { wxCMD_LINE_SWITCH
, _T("v"), _T("version"), _T("print version") },
84 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("config file 1"), wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_OPTIONAL
},
90 // ============================================================================
92 // ============================================================================
94 // ----------------------------------------------------------------------------
95 // the application class
96 // ----------------------------------------------------------------------------
98 wxEmulatorApp::wxEmulatorApp()
100 m_xnestWindow
= NULL
;
101 m_containerWindow
= NULL
;
102 m_displayNumber
= wxT("100");
107 // 'Main program' equivalent: the program execution "starts" here
108 bool wxEmulatorApp::OnInit()
110 wxLog::SetTimestamp(NULL
);
111 wxInitAllImageHandlers();
113 wxString currentDir
= wxGetCwd();
115 // Use argv to get current app directory
116 m_appDir
= wxFindAppPath(argv
[0], currentDir
, wxT("WXEMUDIR"));
118 // If the development version, go up a directory.
120 if ((m_appDir
.Right(5).CmpNoCase(_T("DEBUG")) == 0) ||
121 (m_appDir
.Right(11).CmpNoCase(_T("DEBUGSTABLE")) == 0) ||
122 (m_appDir
.Right(7).CmpNoCase(_T("RELEASE")) == 0) ||
123 (m_appDir
.Right(13).CmpNoCase(_T("RELEASESTABLE")) == 0)
125 m_appDir
= wxPathOnly(m_appDir
);
128 // Parse the command-line parameters and options
129 wxCmdLineParser
parser(sg_cmdLineDesc
, argc
, argv
);
133 res
= parser
.Parse();
135 if (res
== -1 || res
> 0 || parser
.Found(wxT("h")))
138 wxLog::SetActiveTarget(new wxLogStderr
);
143 if (parser
.Found(wxT("v")))
146 wxLog::SetActiveTarget(new wxLogStderr
);
149 msg
.Printf(wxT("wxWindows PDA Emulator (c) Julian Smart, 2002 Version %.2f, %s"), wxEMULATOR_VERSION
, __DATE__
);
153 if (parser
.Found(wxT("u"), & m_displayNumber
))
155 // Should only be number, so strip out anything before
156 // and including a : character
157 if (m_displayNumber
.Find(wxT(':')) != -1)
159 m_displayNumber
= m_displayNumber
.AfterFirst(wxT(':'));
162 if (parser
.GetParamCount() == 0)
164 m_emulatorInfo
.m_emulatorFilename
= wxT("default.wxe");
166 else if (parser
.GetParamCount() > 0)
168 m_emulatorInfo
.m_emulatorFilename
= parser
.GetParam(0);
171 // Load the emulation info
172 if (!LoadEmulator(m_appDir
))
174 //wxMessageBox(wxT("Sorry, could not load this emulator. Please check bitmaps are valid."));
178 // create the main application window
179 wxEmulatorFrame
*frame
= new wxEmulatorFrame(_T("wxEmulator"),
180 wxPoint(50, 50), wxSize(450, 340));
182 frame
->SetStatusText(m_emulatorInfo
.m_emulatorTitle
, 0);
185 sizeStr
.Printf(wxT("Screen: %dx%d"), (int) m_emulatorInfo
.m_emulatorScreenSize
.x
,
186 (int) m_emulatorInfo
.m_emulatorScreenSize
.y
);
187 frame
->SetStatusText(sizeStr
, 1);
189 m_containerWindow
= new wxEmulatorContainer(frame
, wxID_ANY
);
191 frame
->SetClientSize(m_emulatorInfo
.m_emulatorDeviceSize
.x
,
192 m_emulatorInfo
.m_emulatorDeviceSize
.y
);
194 // and show it (the frames, unlike simple controls, are not shown when
195 // created initially)
199 m_xnestWindow
= new wxAdoptedWindow
;
202 cmd
.Printf(wxT("Xnest :%s -geometry %dx%d"),
203 m_displayNumber
.c_str(),
204 (int) m_emulatorInfo
.m_emulatorScreenSize
.x
,
205 (int) m_emulatorInfo
.m_emulatorScreenSize
.y
);
207 // Asynchronously executes Xnest
208 m_xnestPID
= wxExecute(cmd
);
212 wxMessageBox(wxT("Sorry, could not run Xnest. Please check your PATH."));
216 wxReparenter reparenter
;
217 if (!reparenter
.WaitAndReparent(m_containerWindow
, m_xnestWindow
, wxT("Xnest")))
219 wxMessageBox(wxT("Sorry, could not reparent Xnest.."));
225 m_containerWindow
->DoResize();
227 // success: wxApp::OnRun() will be called which will enter the main message
228 // loop and the application will run. If we returned false here, the
229 // application would exit immediately.
233 // Prepend the current program directory to the name
234 wxString
wxEmulatorApp::GetFullAppPath(const wxString
& filename
) const
236 wxString
path(m_appDir
);
237 if (path
.Last() != '\\' && path
.Last() != '/' && filename
[0] != '\\' && filename
[0] != '/')
249 // Load the specified emulator.
250 // For now, hard-wired. TODO: make this configurable
251 bool wxEmulatorApp::LoadEmulator(const wxString
& appDir
)
253 // Load config file and bitmaps
254 return m_emulatorInfo
.Load(appDir
);
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
262 wxEmulatorFrame::wxEmulatorFrame(const wxString
& title
,
263 const wxPoint
& pos
, const wxSize
& size
)
264 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
)
266 // set the frame icon
267 SetIcon(wxICON(emulator
));
271 wxMenu
*menuFile
= new wxMenu
;
273 // the "About" item should be in the help menu
274 wxMenu
*helpMenu
= new wxMenu
;
275 helpMenu
->Append(Emulator_About
, _T("&About...\tF1"), _T("Show about dialog"));
277 menuFile
->Append(Emulator_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
279 // now append the freshly created menu to the menu bar...
280 wxMenuBar
*menuBar
= new wxMenuBar();
281 menuBar
->Append(menuFile
, _T("&File"));
282 menuBar
->Append(helpMenu
, _T("&Help"));
284 // ... and attach this menu bar to the frame
286 #endif // wxUSE_MENUS
289 // create a status bar just for fun (by default with 1 pane only)
291 #endif // wxUSE_STATUSBAR
297 void wxEmulatorFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
299 // true is to force the frame to close
303 void wxEmulatorFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
306 msg
.Printf( _T("wxEmulator is an environment for testing embedded X11 apps.\n"));
308 wxMessageBox(msg
, _T("About wxEmulator"), wxOK
| wxICON_INFORMATION
, this);
311 void wxEmulatorFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
314 if (wxGetApp().m_xnestWindow
)
316 wxGetApp().m_xnestWindow
->SetHandle((WXWindow
) NULL
);
320 if (wxGetApp().m_xnestPID
> 0)
322 wxKill(wxGetApp().m_xnestPID
);
323 wxGetApp().m_xnestPID
= 0;
327 IMPLEMENT_CLASS(wxEmulatorContainer
, wxWindow
)
329 BEGIN_EVENT_TABLE(wxEmulatorContainer
, wxWindow
)
330 EVT_SIZE(wxEmulatorContainer::OnSize
)
331 EVT_PAINT(wxEmulatorContainer::OnPaint
)
332 EVT_ERASE_BACKGROUND(wxEmulatorContainer::OnEraseBackground
)
335 wxEmulatorContainer::wxEmulatorContainer(wxWindow
* parent
, wxWindowID id
):
336 wxWindow(parent
, id
, wxDefaultPosition
, wxDefaultSize
)
340 void wxEmulatorContainer::OnSize(wxSizeEvent
& WXUNUSED(event
))
345 void wxEmulatorContainer::DoResize()
347 wxSize sz
= GetClientSize();
348 if (wxGetApp().m_xnestWindow
350 && wxGetApp().m_xnestWindow
->GetMainWindow()
354 int deviceWidth
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.x
;
355 int deviceHeight
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.y
;
357 int x
= wxMax(0, (int) ((sz
.x
- deviceWidth
)/2.0));
358 int y
= wxMax(0, (int) ((sz
.y
- deviceHeight
)/2.0));
360 x
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.x
;
361 y
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.y
;
363 wxGetApp().m_xnestWindow
->Move(x
, y
);
368 void wxEmulatorContainer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
372 wxSize sz
= GetClientSize();
373 if (wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.Ok())
375 int deviceWidth
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.x
;
376 int deviceHeight
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.y
;
378 int x
= wxMax(0, (int) ((sz
.x
- deviceWidth
)/2.0));
379 int y
= wxMax(0, (int) ((sz
.y
- deviceHeight
)/2.0));
381 dc
.DrawBitmap(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
, x
, y
);
385 void wxEmulatorContainer::OnEraseBackground(wxEraseEvent
& event
)
387 wxDC
* dc
wxDUMMY_INITIALIZE(NULL
);
395 dc
= new wxClientDC(this);
398 dc
->SetBackground(wxBrush(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundColour
, wxSOLID
));
405 // Information about the emulator decorations
407 void wxEmulatorInfo::Copy(const wxEmulatorInfo
& info
)
409 m_emulatorFilename
= info
.m_emulatorFilename
;
410 m_emulatorTitle
= info
.m_emulatorTitle
;
411 m_emulatorDescription
= info
.m_emulatorDescription
;
412 m_emulatorScreenPosition
= info
.m_emulatorScreenPosition
;
413 m_emulatorScreenSize
= info
.m_emulatorScreenSize
;
414 m_emulatorBackgroundBitmap
= info
.m_emulatorBackgroundBitmap
;
415 m_emulatorBackgroundBitmapName
= info
.m_emulatorBackgroundBitmapName
;
416 m_emulatorBackgroundColour
= info
.m_emulatorBackgroundColour
;
417 m_emulatorDeviceSize
= info
.m_emulatorDeviceSize
;
421 void wxEmulatorInfo::Init()
423 m_emulatorDeviceSize
= wxSize(260, 340);
424 m_emulatorScreenSize
= wxSize(240, 320);
428 bool wxEmulatorInfo::Load(const wxString
& appDir
)
430 // Try to find absolute path
431 wxString absoluteConfigPath
= m_emulatorFilename
;
432 if ( !::wxIsAbsolutePath(absoluteConfigPath
) )
434 wxString currDir
= wxGetCwd();
435 absoluteConfigPath
= currDir
+ wxString(wxFILE_SEP_PATH
) + m_emulatorFilename
;
436 if ( !wxFile::Exists(absoluteConfigPath
) )
438 absoluteConfigPath
= appDir
+ wxString(wxFILE_SEP_PATH
)
439 + m_emulatorFilename
;
443 if ( !wxFile::Exists(absoluteConfigPath
) )
446 str
.Printf( wxT("Could not find config file %s"),
447 absoluteConfigPath
.c_str() );
453 wxString rootPath
= wxPathOnly(absoluteConfigPath
);
456 wxFileConfig
config(wxT("wxEmulator"), wxT("wxWindows"),
457 absoluteConfigPath
, wxEmptyString
, wxCONFIG_USE_LOCAL_FILE
);
459 config
.Read(wxT("/General/title"), & m_emulatorTitle
);
460 config
.Read(wxT("/General/description"), & m_emulatorDescription
);
461 config
.Read(wxT("/General/backgroundBitmap"), & m_emulatorBackgroundBitmapName
);
464 if (config
.Read(wxT("/General/backgroundColour"), & colString
) ||
465 config
.Read(wxT("/General/backgroundColor"), & colString
)
468 m_emulatorBackgroundColour
= wxHexStringToColour(colString
);
471 int x
= 0, y
= 0, w
= 0, h
= 0, dw
= 0, dh
= 0;
472 config
.Read(wxT("/General/screenX"), & x
);
473 config
.Read(wxT("/General/screenY"), & y
);
474 config
.Read(wxT("/General/screenWidth"), & w
);
475 config
.Read(wxT("/General/screenHeight"), & h
);
476 if (config
.Read(wxT("/General/deviceWidth"), & dw
) && config
.Read(wxT("/General/deviceHeight"), & dh
))
478 m_emulatorDeviceSize
= wxSize(dw
, dh
);
481 m_emulatorScreenPosition
= wxPoint(x
, y
);
482 m_emulatorScreenSize
= wxSize(w
, h
);
485 if (!m_emulatorBackgroundBitmapName
.IsEmpty())
487 wxString absoluteBackgroundBitmapName
= rootPath
+ wxString(wxFILE_SEP_PATH
) + m_emulatorBackgroundBitmapName
;
488 if ( !wxFile::Exists(absoluteBackgroundBitmapName
) )
491 str
.Printf( wxT("Could not find bitmap %s"),
492 absoluteBackgroundBitmapName
.c_str() );
497 wxBitmapType type
= wxDetermineImageType(m_emulatorBackgroundBitmapName
);
498 if (type
== wxBITMAP_TYPE_INVALID
)
501 if (!m_emulatorBackgroundBitmap
.LoadFile(m_emulatorBackgroundBitmapName
, type
))
504 str
.Printf( wxT("Could not load bitmap file %s"),
505 m_emulatorBackgroundBitmapName
.c_str() );
510 m_emulatorDeviceSize
= wxSize(m_emulatorBackgroundBitmap
.GetWidth(),
511 m_emulatorBackgroundBitmap
.GetHeight());
516 // Returns the image type, or -1, determined from the extension.
517 wxBitmapType
wxDetermineImageType(const wxString
& filename
)
519 wxString path
, name
, ext
;
521 wxSplitPath(filename
, & path
, & name
, & ext
);
524 if (ext
== _T("jpg") || ext
== _T("jpeg"))
525 return wxBITMAP_TYPE_JPEG
;
526 if (ext
== _T("gif"))
527 return wxBITMAP_TYPE_GIF
;
528 if (ext
== _T("bmp"))
529 return wxBITMAP_TYPE_BMP
;
530 if (ext
== _T("png"))
531 return wxBITMAP_TYPE_PNG
;
532 if (ext
== _T("pcx"))
533 return wxBITMAP_TYPE_PCX
;
534 if (ext
== _T("tif") || ext
== _T("tiff"))
535 return wxBITMAP_TYPE_TIF
;
537 return wxBITMAP_TYPE_INVALID
;
540 // Convert a colour to a 6-digit hex string
541 wxString
wxColourToHexString(const wxColour
& col
)
545 hex
+= wxDecToHex(col
.Red());
546 hex
+= wxDecToHex(col
.Green());
547 hex
+= wxDecToHex(col
.Blue());
552 // Convert 6-digit hex string to a colour
553 wxColour
wxHexStringToColour(const wxString
& hex
)
555 unsigned int r
= wxHexToDec(hex
.Mid(0, 2));
556 unsigned int g
= wxHexToDec(hex
.Mid(2, 2));
557 unsigned int b
= wxHexToDec(hex
.Mid(4, 2));
559 return wxColour(r
, g
, b
);
562 // Find the absolute path where this application has been run from.
563 // argv0 is wxTheApp->argv[0]
564 // cwd is the current working directory (at startup)
565 // appVariableName is the name of a variable containing the directory for this app, e.g.
566 // MYAPPDIR. This is checked first.
568 wxString
wxFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
572 // Try appVariableName
573 if (!appVariableName
.IsEmpty())
575 str
= wxGetenv(appVariableName
);
580 if (wxIsAbsolutePath(argv0
))
581 return wxPathOnly(argv0
);
584 // Is it a relative path?
585 wxString
currentDir(cwd
);
586 if (!wxEndsWithPathSeparator(currentDir
))
587 currentDir
+= wxFILE_SEP_PATH
;
589 str
= currentDir
+ argv0
;
590 if ( wxFile::Exists(str
) )
591 return wxPathOnly(str
);
594 // OK, it's neither an absolute path nor a relative path.
598 pathList
.AddEnvList(wxT("PATH"));
599 str
= pathList
.FindAbsoluteValidPath(argv0
);
601 return wxPathOnly(str
);
604 return wxEmptyString
;