1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Emulator wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 #include "wx/confbase.h"
34 #include "wx/fileconf.h"
35 #include "wx/cmdline.h"
38 #include "wx/filename.h"
41 #include "wx/x11/reparent.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // the application icon (under Windows and OS/2 it is in resources)
51 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
52 #include "emulator.xpm"
55 // ----------------------------------------------------------------------------
56 // event tables and other macros for wxWidgets
57 // ----------------------------------------------------------------------------
59 // the event tables connect the wxWidgets events with the functions (event
60 // handlers) which process them. It can be also done at run-time, but for the
61 // simple menu events like this the static method is much simpler.
62 BEGIN_EVENT_TABLE(wxEmulatorFrame
, wxFrame
)
63 EVT_MENU(Emulator_Quit
, wxEmulatorFrame::OnQuit
)
64 EVT_MENU(Emulator_About
, wxEmulatorFrame::OnAbout
)
65 EVT_CLOSE(wxEmulatorFrame::OnCloseWindow
)
68 // Create a new application object: this macro will allow wxWidgets to create
69 // the application object during program execution (it's better than using a
70 // static object for many reasons) and also declares the accessor function
71 // wxGetApp() which will return the reference of the right type (i.e. wxEmulatorApp and
73 IMPLEMENT_APP(wxEmulatorApp
)
75 static const wxCmdLineEntryDesc sg_cmdLineDesc
[] =
77 { wxCMD_LINE_OPTION
, "u", "use-display", "display number to use (default 100)" },
79 { wxCMD_LINE_SWITCH
, "h", "help", "displays help on the command line parameters" },
80 { wxCMD_LINE_SWITCH
, "v", "version", "print version" },
82 { wxCMD_LINE_PARAM
, NULL
, NULL
, "config file 1", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_OPTIONAL
},
88 // ============================================================================
90 // ============================================================================
92 // ----------------------------------------------------------------------------
93 // the application class
94 // ----------------------------------------------------------------------------
96 wxEmulatorApp::wxEmulatorApp()
99 m_containerWindow
= NULL
;
100 m_displayNumber
= wxT("100");
105 // 'Main program' equivalent: the program execution "starts" here
106 bool wxEmulatorApp::OnInit()
109 wxLog::DisableTimestamp();
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(wxT("DEBUG")) == 0) ||
121 (m_appDir
.Right(11).CmpNoCase(wxT("DEBUGSTABLE")) == 0) ||
122 (m_appDir
.Right(7).CmpNoCase(wxT("RELEASE")) == 0) ||
123 (m_appDir
.Right(13).CmpNoCase(wxT("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("wxWidgets 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(wxT("wxEmulator"),
180 wxPoint(50, 50), wxSize(450, 340));
183 frame
->SetStatusText(m_emulatorInfo
.m_emulatorTitle
, 0);
186 sizeStr
.Printf(wxT("Screen: %dx%d"), (int) m_emulatorInfo
.m_emulatorScreenSize
.x
,
187 (int) m_emulatorInfo
.m_emulatorScreenSize
.y
);
188 frame
->SetStatusText(sizeStr
, 1);
189 #endif // wxUSE_STATUSBAR
191 m_containerWindow
= new wxEmulatorContainer(frame
, wxID_ANY
);
193 frame
->SetClientSize(m_emulatorInfo
.m_emulatorDeviceSize
.x
,
194 m_emulatorInfo
.m_emulatorDeviceSize
.y
);
196 // and show it (the frames, unlike simple controls, are not shown when
197 // created initially)
201 m_xnestWindow
= new wxAdoptedWindow
;
204 cmd
.Printf(wxT("Xnest :%s -geometry %dx%d"),
205 m_displayNumber
.c_str(),
206 (int) m_emulatorInfo
.m_emulatorScreenSize
.x
,
207 (int) m_emulatorInfo
.m_emulatorScreenSize
.y
);
209 // Asynchronously executes Xnest
210 m_xnestPID
= wxExecute(cmd
);
214 wxMessageBox(wxT("Sorry, could not run Xnest. Please check your PATH."));
218 wxReparenter reparenter
;
219 if (!reparenter
.WaitAndReparent(m_containerWindow
, m_xnestWindow
, wxT("Xnest")))
221 wxMessageBox(wxT("Sorry, could not reparent Xnest.."));
227 m_containerWindow
->DoResize();
229 // success: wxApp::OnRun() will be called which will enter the main message
230 // loop and the application will run. If we returned false here, the
231 // application would exit immediately.
235 // Prepend the current program directory to the name
236 wxString
wxEmulatorApp::GetFullAppPath(const wxString
& filename
) const
238 wxString
path(m_appDir
);
239 if (path
.Last() != '\\' && path
.Last() != '/' && filename
[0] != '\\' && filename
[0] != '/')
251 // Load the specified emulator.
252 // For now, hard-wired. TODO: make this configurable
253 bool wxEmulatorApp::LoadEmulator(const wxString
& appDir
)
255 // Load config file and bitmaps
256 return m_emulatorInfo
.Load(appDir
);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
264 wxEmulatorFrame::wxEmulatorFrame(const wxString
& title
,
265 const wxPoint
& pos
, const wxSize
& size
)
266 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
)
268 // set the frame icon
269 SetIcon(wxICON(emulator
));
273 wxMenu
*menuFile
= new wxMenu
;
275 // the "About" item should be in the help menu
276 wxMenu
*helpMenu
= new wxMenu
;
277 helpMenu
->Append(Emulator_About
, wxT("&About...\tF1"), wxT("Show about dialog"));
279 menuFile
->Append(Emulator_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
281 // now append the freshly created menu to the menu bar...
282 wxMenuBar
*menuBar
= new wxMenuBar();
283 menuBar
->Append(menuFile
, wxT("&File"));
284 menuBar
->Append(helpMenu
, wxT("&Help"));
286 // ... and attach this menu bar to the frame
288 #endif // wxUSE_MENUS
291 // create a status bar just for fun (by default with 1 pane only)
293 #endif // wxUSE_STATUSBAR
299 void wxEmulatorFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
301 // true is to force the frame to close
305 void wxEmulatorFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
308 msg
.Printf( wxT("wxEmulator is an environment for testing embedded X11 apps.\n"));
310 wxMessageBox(msg
, wxT("About wxEmulator"), wxOK
| wxICON_INFORMATION
, this);
313 void wxEmulatorFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
316 if (wxGetApp().m_xnestWindow
)
318 wxGetApp().m_xnestWindow
->SetHandle((WXWindow
) NULL
);
322 if (wxGetApp().m_xnestPID
> 0)
324 wxKill(wxGetApp().m_xnestPID
);
325 wxGetApp().m_xnestPID
= 0;
329 IMPLEMENT_CLASS(wxEmulatorContainer
, wxWindow
)
331 BEGIN_EVENT_TABLE(wxEmulatorContainer
, wxWindow
)
332 EVT_SIZE(wxEmulatorContainer::OnSize
)
333 EVT_PAINT(wxEmulatorContainer::OnPaint
)
334 EVT_ERASE_BACKGROUND(wxEmulatorContainer::OnEraseBackground
)
337 wxEmulatorContainer::wxEmulatorContainer(wxWindow
* parent
, wxWindowID id
):
338 wxWindow(parent
, id
, wxDefaultPosition
, wxDefaultSize
)
342 void wxEmulatorContainer::OnSize(wxSizeEvent
& WXUNUSED(event
))
347 void wxEmulatorContainer::DoResize()
349 wxSize sz
= GetClientSize();
350 if (wxGetApp().m_xnestWindow
352 && wxGetApp().m_xnestWindow
->X11GetMainWindow()
356 int deviceWidth
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.x
;
357 int deviceHeight
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.y
;
359 int x
= wxMax(0, (int) ((sz
.x
- deviceWidth
)/2.0));
360 int y
= wxMax(0, (int) ((sz
.y
- deviceHeight
)/2.0));
362 x
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.x
;
363 y
+= wxGetApp().m_emulatorInfo
.m_emulatorScreenPosition
.y
;
365 wxGetApp().m_xnestWindow
->Move(x
, y
);
370 void wxEmulatorContainer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
374 wxSize sz
= GetClientSize();
375 if (wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
.IsOk())
377 int deviceWidth
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.x
;
378 int deviceHeight
= wxGetApp().m_emulatorInfo
.m_emulatorDeviceSize
.y
;
380 int x
= wxMax(0, (int) ((sz
.x
- deviceWidth
)/2.0));
381 int y
= wxMax(0, (int) ((sz
.y
- deviceHeight
)/2.0));
383 dc
.DrawBitmap(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundBitmap
, x
, y
);
387 void wxEmulatorContainer::OnEraseBackground(wxEraseEvent
& event
)
389 wxDC
* dc
wxDUMMY_INITIALIZE(NULL
);
397 dc
= new wxClientDC(this);
400 dc
->SetBackground(wxBrush(wxGetApp().m_emulatorInfo
.m_emulatorBackgroundColour
, wxSOLID
));
407 // Information about the emulator decorations
409 void wxEmulatorInfo::Copy(const wxEmulatorInfo
& info
)
411 m_emulatorFilename
= info
.m_emulatorFilename
;
412 m_emulatorTitle
= info
.m_emulatorTitle
;
413 m_emulatorDescription
= info
.m_emulatorDescription
;
414 m_emulatorScreenPosition
= info
.m_emulatorScreenPosition
;
415 m_emulatorScreenSize
= info
.m_emulatorScreenSize
;
416 m_emulatorBackgroundBitmap
= info
.m_emulatorBackgroundBitmap
;
417 m_emulatorBackgroundBitmapName
= info
.m_emulatorBackgroundBitmapName
;
418 m_emulatorBackgroundColour
= info
.m_emulatorBackgroundColour
;
419 m_emulatorDeviceSize
= info
.m_emulatorDeviceSize
;
423 void wxEmulatorInfo::Init()
425 m_emulatorDeviceSize
= wxSize(260, 340);
426 m_emulatorScreenSize
= wxSize(240, 320);
430 bool wxEmulatorInfo::Load(const wxString
& appDir
)
432 // Try to find absolute path
433 wxString absoluteConfigPath
= m_emulatorFilename
;
434 if ( !::wxIsAbsolutePath(absoluteConfigPath
) )
436 wxString currDir
= wxGetCwd();
437 absoluteConfigPath
= currDir
+ wxString(wxFILE_SEP_PATH
) + m_emulatorFilename
;
438 if ( !wxFile::Exists(absoluteConfigPath
) )
440 absoluteConfigPath
= appDir
+ wxString(wxFILE_SEP_PATH
)
441 + m_emulatorFilename
;
445 if ( !wxFile::Exists(absoluteConfigPath
) )
448 str
.Printf( wxT("Could not find config file %s"),
449 absoluteConfigPath
.c_str() );
455 wxString rootPath
= wxPathOnly(absoluteConfigPath
);
458 wxFileConfig
config(wxT("wxEmulator"), wxT("wxWidgets"),
459 absoluteConfigPath
, wxEmptyString
, wxCONFIG_USE_LOCAL_FILE
);
461 config
.Read(wxT("/General/title"), & m_emulatorTitle
);
462 config
.Read(wxT("/General/description"), & m_emulatorDescription
);
463 config
.Read(wxT("/General/backgroundBitmap"), & m_emulatorBackgroundBitmapName
);
466 if (config
.Read(wxT("/General/backgroundColour"), & colString
) ||
467 config
.Read(wxT("/General/backgroundColor"), & colString
)
470 m_emulatorBackgroundColour
= wxHexStringToColour(colString
);
473 int x
= 0, y
= 0, w
= 0, h
= 0, dw
= 0, dh
= 0;
474 config
.Read(wxT("/General/screenX"), & x
);
475 config
.Read(wxT("/General/screenY"), & y
);
476 config
.Read(wxT("/General/screenWidth"), & w
);
477 config
.Read(wxT("/General/screenHeight"), & h
);
478 if (config
.Read(wxT("/General/deviceWidth"), & dw
) && config
.Read(wxT("/General/deviceHeight"), & dh
))
480 m_emulatorDeviceSize
= wxSize(dw
, dh
);
483 m_emulatorScreenPosition
= wxPoint(x
, y
);
484 m_emulatorScreenSize
= wxSize(w
, h
);
487 if (!m_emulatorBackgroundBitmapName
.empty())
489 wxString absoluteBackgroundBitmapName
= rootPath
+ wxString(wxFILE_SEP_PATH
) + m_emulatorBackgroundBitmapName
;
490 if ( !wxFile::Exists(absoluteBackgroundBitmapName
) )
493 str
.Printf( wxT("Could not find bitmap %s"),
494 absoluteBackgroundBitmapName
.c_str() );
499 wxBitmapType type
= wxDetermineImageType(m_emulatorBackgroundBitmapName
);
500 if (type
== wxBITMAP_TYPE_INVALID
)
503 if (!m_emulatorBackgroundBitmap
.LoadFile(m_emulatorBackgroundBitmapName
, type
))
506 str
.Printf( wxT("Could not load bitmap file %s"),
507 m_emulatorBackgroundBitmapName
.c_str() );
512 m_emulatorDeviceSize
= wxSize(m_emulatorBackgroundBitmap
.GetWidth(),
513 m_emulatorBackgroundBitmap
.GetHeight());
518 // Returns the image type, or -1, determined from the extension.
519 wxBitmapType
wxDetermineImageType(const wxString
& filename
)
521 wxString path
, name
, ext
;
523 wxFileName::SplitPath(filename
, & path
, & name
, & ext
);
526 if (ext
== wxT("jpg") || ext
== wxT("jpeg"))
527 return wxBITMAP_TYPE_JPEG
;
528 if (ext
== wxT("gif"))
529 return wxBITMAP_TYPE_GIF
;
530 if (ext
== wxT("bmp"))
531 return wxBITMAP_TYPE_BMP
;
532 if (ext
== wxT("png"))
533 return wxBITMAP_TYPE_PNG
;
534 if (ext
== wxT("pcx"))
535 return wxBITMAP_TYPE_PCX
;
536 if (ext
== wxT("tif") || ext
== wxT("tiff"))
537 return wxBITMAP_TYPE_TIFF
;
539 return wxBITMAP_TYPE_INVALID
;
542 // Convert a colour to a 6-digit hex string
543 wxString
wxColourToHexString(const wxColour
& col
)
547 hex
+= wxDecToHex(col
.Red());
548 hex
+= wxDecToHex(col
.Green());
549 hex
+= wxDecToHex(col
.Blue());
554 // Convert 6-digit hex string to a colour
555 wxColour
wxHexStringToColour(const wxString
& hex
)
557 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
558 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
559 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
561 return wxColour(r
, g
, b
);
564 // Find the absolute path where this application has been run from.
565 // argv0 is wxTheApp->argv[0]
566 // cwd is the current working directory (at startup)
567 // appVariableName is the name of a variable containing the directory for this app, e.g.
568 // MYAPPDIR. This is checked first.
570 wxString
wxFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
574 // Try appVariableName
575 if (!appVariableName
.empty())
577 str
= wxGetenv(appVariableName
);
582 if (wxIsAbsolutePath(argv0
))
583 return wxPathOnly(argv0
);
586 // Is it a relative path?
587 wxString
currentDir(cwd
);
588 if (!wxEndsWithPathSeparator(currentDir
))
589 currentDir
+= wxFILE_SEP_PATH
;
591 str
= currentDir
+ argv0
;
592 if ( wxFile::Exists(str
) )
593 return wxPathOnly(str
);
596 // OK, it's neither an absolute path nor a relative path.
600 pathList
.AddEnvList(wxT("PATH"));
601 str
= pathList
.FindAbsoluteValidPath(argv0
);
603 return wxPathOnly(str
);
606 return wxEmptyString
;