2004-02-05 16:58:29 -05:00
% Note: -e/C++ header generation documentation added by
% Eduardo Marques <edrdo@netcabo.pt>
%
2002-01-23 10:17:44 -05:00
\section { XML-based resource system overview} \label { xrcoverview}
Classes: \helpref { wxXmlResource} { wxxmlresource} , \helpref { wxXmlResourceHandler} { wxxmlresourcehandler}
The XML-based resource system, known as XRC, allows user interface elements such as
dialogs, menu bars and toolbars, to be stored in text files and loaded into
the application at run-time. XRC files can also be compiled into binary XRS files or C++
2004-12-11 16:33:17 -05:00
code (the former makes it possible to store all resources in a single file and the latter
2002-07-27 14:14:36 -04:00
is useful when you want to embed the resources into the executable).
2002-01-23 10:17:44 -05:00
There are several advantages to using XRC resources.
\begin { itemize} \itemsep =0pt
\item Recompiling and linking an application is not necessary if the
resources change.
2004-12-11 16:33:17 -05:00
\item If you use a dialog designer that generates C++ code, it can be hard
2002-01-23 10:17:44 -05:00
to reintegrate this into existing C++ code. Separation of resources and code
is a more elegant solution.
\item You can choose between different alternative resource files at run time, if necessary.
\item The XRC format uses sizers for flexibility, allowing dialogs to be resizable
and highly portable.
2004-05-04 04:27:20 -04:00
\item The XRC format is a wxWidgets standard,
2002-01-23 10:17:44 -05:00
and can be generated or postprocessed by any program that understands it. As it is based
on the XML standard, existing XML editors can be used for simple editing purposes.
\end { itemize}
XRC was written by Vaclav Slavik.
\subsection { XRC concepts} \label { xrcconcepts}
These are the typical steps for using XRC files in your application.
\begin { itemize} \itemsep =0pt
\item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
2002-07-27 14:14:36 -04:00
\item If you are going to use \helpref { XRS files} { binaryresourcefiles} , install
wxFileSystem ZIP handler first with { \tt wxFileSystem::AddHandler(new wxZipFSHandler);}
2002-03-22 14:13:29 -05:00
\item call { \tt wxXmlResource::Get()->InitAllHandlers()} from your wxApp::OnInit function,
and then call { \tt wxXmlResource::Get()->Load("myfile.xrc")} to load the resource file;
2002-01-23 10:17:44 -05:00
\item to create a dialog from a resource, create it using the default constructor, and then
2004-12-11 16:33:17 -05:00
load it using for example { \tt wxXmlResource::Get()->LoadDialog(\& dlg, this, "dlg1");}
2002-03-22 14:13:29 -05:00
\item set up event tables as usual but use the { \tt XRCID(str)} macro to translate from XRC string names
to a suitable integer identifier, for example { \tt EVT\_ MENU(XRCID("quit"), MyFrame::OnQuit)} .
2002-01-23 10:17:44 -05:00
\end { itemize}
2003-08-21 04:05:22 -04:00
To create an XRC file, you can use one of the following methods.
2002-01-23 10:17:44 -05:00
2002-05-05 10:24:07 -04:00
\begin { itemize} \itemsep =0pt
2002-01-23 10:17:44 -05:00
\item Create the file by hand;
2002-01-26 16:52:55 -05:00
\item use \urlref { wxDesigner} { http://www.roebling.de} , a commercial dialog designer/RAD tool;
2003-08-21 04:05:22 -04:00
\item use \urlref { DialogBlocks} { http://www.anthemion.co.uk/dialogblocks} , a commercial dialog editor;
\item use \urlref { XRCed} { http://xrced.sf.net} , a wxPython-based
2004-05-04 04:27:20 -04:00
dialog editor that you can find in the { \tt wxPython/tools} subdirectory of the wxWidgets
2002-01-26 16:52:55 -05:00
CVS archive;
2003-08-21 04:05:22 -04:00
\item use \urlref { Glade} { http://wxglade.sf.net} , a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
2002-01-26 16:52:55 -05:00
\item convert WIN32 RC files to XRC with the tool in { \tt contrib/utils/convertrc} .
2002-01-23 10:17:44 -05:00
\end { itemize}
2004-05-04 04:27:20 -04:00
A complete list of third-party tools that write to XRC can be found at \urlref { www.wxwidgets.org/lnk\_ tool.htm} { http://www.wxwidgets.org/lnk\_ tool.in} .
2003-08-21 04:05:22 -04:00
It is highly recommended that you use a resource editing tool, since it's fiddly writing
2002-01-23 10:17:44 -05:00
XRC files by hand.
You can use \helpref { wxXmlResource::Load} { wxxmlresourceload} in a number of ways.
2002-07-27 14:14:36 -04:00
You can pass an XRC file (XML-based text resource file)
or a \helpref { zip-compressed file} { binaryresourcefiles} (extension ZIP or XRS) containing other XRC.
2002-01-23 10:17:44 -05:00
2002-07-27 14:14:36 -04:00
You can also use \helpref { embedded C++ resources} { embeddedresource}
2002-01-23 10:17:44 -05:00
\subsection { Using binary resource files} \label { binaryresourcefiles}
2002-07-27 14:14:36 -04:00
To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
(the input XRC files) and the following switches and options:
2002-05-05 10:24:07 -04:00
\begin { itemize} \itemsep =0pt
2002-01-23 10:17:44 -05:00
\item -h (--help): show a help message
\item -v (--verbose): show verbose logging information
2002-07-27 14:14:36 -04:00
\item -c (--cpp-code): write C++ source rather than a XRS file
2004-02-05 16:58:29 -05:00
\item -e (--extra-cpp-code): if used together with -c, generates C++ header file
containing class definitions for the windows defined by the XRC file (see special subsection)
2002-01-23 10:17:44 -05:00
\item -u (--uncompressed): do not compress XML files (C++ only)
\item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
\item -n (--function) <name>: specify C++ function name (use with -c)
\item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
\item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
\end { itemize}
For example:
\begin { verbatim}
2004-10-12 14:28:43 -04:00
% wxrc resource.xrc
% wxrc resource.xrc -o resource.xrs
% wxrc resource.xrc -v -c -o resource.cpp
2002-01-23 10:17:44 -05:00
\end { verbatim}
2002-07-27 14:14:36 -04:00
\wxheading { Note}
2004-10-22 15:15:35 -04:00
XRS file is essentially a renamed ZIP archive which means that you can manipulate
2002-07-27 14:14:36 -04:00
it with standard ZIP tools. Note that if you are using XRS files, you have
2004-12-11 16:33:17 -05:00
to initialize the \helpref { wxFileSystem} { wxfilesystem} ZIP handler first! It is a simple
2002-07-27 14:14:36 -04:00
thing to do:
2004-02-21 20:16:32 -05:00
2002-07-27 14:14:36 -04:00
\begin { verbatim}
#include <wx/filesys.h>
#include <wx/fs_ zip.h>
...
wxFileSystem::AddHandler(new wxZipFSHandler);
\end { verbatim}
\subsection { Using embedded resources} \label { embeddedresource}
It is sometimes useful to embed resources in the executable itself instead
2004-12-11 16:33:17 -05:00
of loading an external file (e.g. when your app is small and consists only of one
2002-07-27 14:14:36 -04:00
exe file). XRC provides means to convert resources into regular C++ file that
can be compiled and included in the executable.
Use the { \tt -c} switch to
{ \tt wxrc} utility to produce C++ file with embedded resources. This file will
contain a function called { \it InitXmlResource} (unless you override this with
a command line switch). Use it to load the resource:
2004-02-21 20:16:32 -05:00
2002-07-27 14:14:36 -04:00
\begin { verbatim}
2004-11-16 15:48:11 -05:00
extern void InitXmlResource(); // defined in generated file
2003-05-26 12:20:54 -04:00
...
2002-07-27 14:14:36 -04:00
wxXmlResource::Get()->InitAllHandlers();
InitXmlResource();
...
\end { verbatim}
2002-01-23 10:17:44 -05:00
\subsection { XRC C++ sample} \label { xrccppsample}
This is the C++ source file (xrcdemo.cpp) for the XRC sample.
\begin { verbatim}
2002-01-29 18:08:49 -05:00
#include "wx/wx.h"
2002-01-23 10:17:44 -05:00
#include "wx/image.h"
#include "wx/xrc/xmlres.h"
// the application icon
#if defined(_ _ WXGTK_ _ ) || defined(_ _ WXMOTIF_ _ ) || defined(_ _ WXMAC_ _ )
#include "rc/appicon.xpm"
#endif
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
// override base class virtuals
// ----------------------------
// this one is called on application startup and is a good place for the app
// initialization (doing it here and not in the ctor allows to have an error
// return: if OnInit() returns false, the application terminates)
virtual bool OnInit();
} ;
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
// event handlers (these functions should _ not_ be virtual)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnDlg1(wxCommandEvent& event);
void OnDlg2(wxCommandEvent& event);
private:
2004-05-04 04:27:20 -04:00
// any class wishing to process wxWidgets events must use this macro
2002-01-23 10:17:44 -05:00
DECLARE_ EVENT_ TABLE()
} ;
// ----------------------------------------------------------------------------
2004-05-04 04:27:20 -04:00
// event tables and other macros for wxWidgets
2002-01-23 10:17:44 -05:00
// ----------------------------------------------------------------------------
BEGIN_ EVENT_ TABLE(MyFrame, wxFrame)
2002-01-27 18:51:05 -05:00
EVT_ MENU(XRCID("menu_ quit"), MyFrame::OnQuit)
EVT_ MENU(XRCID("menu_ about"), MyFrame::OnAbout)
EVT_ MENU(XRCID("menu_ dlg1"), MyFrame::OnDlg1)
EVT_ MENU(XRCID("menu_ dlg2"), MyFrame::OnDlg2)
2002-01-23 10:17:44 -05:00
END_ EVENT_ TABLE()
IMPLEMENT_ APP(MyApp)
// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
wxImage::AddHandler(new wxGIFHandler);
wxXmlResource::Get()->InitAllHandlers();
wxXmlResource::Get()->Load("rc/resource.xrc");
MyFrame *frame = new MyFrame("XML resources demo",
wxPoint(50, 50), wxSize(450, 340));
2003-01-17 19:16:34 -05:00
frame->Show(true);
return true;
2002-01-23 10:17:44 -05:00
}
// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------
// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
SetIcon(wxICON(appicon));
SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
}
// event handlers
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
2003-01-17 19:16:34 -05:00
// true is to force the frame to close
Close(true);
2002-01-23 10:17:44 -05:00
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxString msg;
msg.Printf( _ T("This is the about dialog of XML resources demo.\n ")
_ T("Welcome to %s"), wxVERSION_STRING);
wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_ INFORMATION, this);
}
void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
{
wxDialog dlg;
wxXmlResource::Get()->LoadDialog(& dlg, this, "dlg1");
dlg.ShowModal();
}
void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
{
wxDialog dlg;
wxXmlResource::Get()->LoadDialog(& dlg, this, "dlg2");
dlg.ShowModal();
}
\end { verbatim}
\subsection { XRC resource file sample} \label { xrcsample}
This is the XML file (resource.xrc) for the XRC sample.
\begin { verbatim}
<?xml version="1.0"?>
2002-03-22 14:13:29 -05:00
<resource version="2.3.0.1">
2002-01-23 10:17:44 -05:00
<object class="wxMenuBar" name="mainmenu">
<style>wxMB_ DOCKABLE</style>
<object class="wxMenu" name="menu_ file">
2002-03-22 14:13:29 -05:00
<label>_ File</label>
2002-01-23 10:17:44 -05:00
<style>wxMENU_ TEAROFF</style>
<object class="wxMenuItem" name="menu_ about">
2002-03-22 14:13:29 -05:00
<label>_ About...</label>
2002-01-23 10:17:44 -05:00
<bitmap>filesave.gif</bitmap>
</object>
<object class="separator"/>
<object class="wxMenuItem" name="menu_ dlg1">
<label>Dialog 1</label>
</object>
<object class="wxMenuItem" name="menu_ dlg2">
<label>Dialog 2</label>
</object>
<object class="separator"/>
<object class="wxMenuItem" name="menu_ quit">
2002-03-22 14:13:29 -05:00
<label>E_ xit\tAlt -X</label>
2002-01-23 10:17:44 -05:00
</object>
</object>
</object>
<object class="wxToolBar" name="toolbar">
<style>wxTB_ FLAT|wxTB_ DOCKABLE</style>
<margins>2,2</margins>
<object class="tool" name="menu_ open">
<bitmap>fileopen.gif</bitmap>
<tooltip>Open catalog</tooltip>
</object>
<object class="tool" name="menu_ save">
<bitmap>filesave.gif</bitmap>
<tooltip>Save catalog</tooltip>
</object>
<object class="tool" name="menu_ update">
<bitmap>update.gif</bitmap>
<tooltip>Update catalog - synchronize it with sources</tooltip>
</object>
<separator/>
<object class="tool" name="menu_ quotes">
<bitmap>quotes.gif</bitmap>
<toggle>1</toggle>
<tooltip>Display quotes around the string?</tooltip>
</object>
<object class="separator"/>
<object class="tool" name="menu_ fuzzy">
<bitmap>fuzzy.gif</bitmap>
<tooltip>Toggled if selected string is fuzzy translation</tooltip>
<toggle>1</toggle>
</object>
</object>
<object class="wxDialog" name="dlg1">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxBitmapButton">
<bitmap>fuzzy.gif</bitmap>
<focus>fileopen.gif</focus>
</object>
</object>
<object class="sizeritem">
<object class="wxPanel">
<object class="wxStaticText">
<label>fdgdfgdfgdfg</label>
</object>
<style>wxSUNKEN_ BORDER</style>
</object>
<flag>wxALIGN_ CENTER</flag>
</object>
<object class="sizeritem">
<object class="wxButton">
<label>Buttonek</label>
</object>
<border>10d</border>
<flag>wxALL</flag>
</object>
<object class="sizeritem">
<object class="wxHtmlWindow">
<htmlcode>& lt;h1& gt;Hi,& lt;/h1& gt;man</htmlcode>
<size>100,45d</size>
</object>
</object>
<object class="sizeritem">
<object class="wxNotebook">
<object class="notebookpage">
<object class="wxPanel">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxHtmlWindow">
<htmlcode>Hello, we are inside a & lt;u& gt;NOTEBOOK& lt;/u& gt;...</htmlcode>
<size>50,50d</size>
</object>
<option>1</option>
</object>
</object>
</object>
<label>Page</label>
</object>
<object class="notebookpage">
<object class="wxPanel">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxHtmlWindow">
<htmlcode>Hello, we are inside a & lt;u& gt;NOTEBOOK& lt;/u& gt;...</htmlcode>
<size>50,50d</size>
</object>
</object>
</object>
</object>
<label>Page 2</label>
</object>
<usenotebooksizer>1</usenotebooksizer>
</object>
<flag>wxEXPAND</flag>
</object>
<orient>wxVERTICAL</orient>
</object>
</object>
<object class="wxDialog" name="dlg2">
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem" name="dfgdfg">
<object class="wxTextCtrl">
<size>200,200d</size>
<style>wxTE_ MULTILINE|wxSUNKEN_ BORDER</style>
<value>Hello, this is an ordinary multiline\n textctrl....</value>
</object>
<option>1</option>
<flag>wxEXPAND|wxALL</flag>
<border>10</border>
</object>
<object class="sizeritem">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxButton" name="wxID_ OK">
<label>Ok</label>
<default>1</default>
</object>
</object>
<object class="sizeritem">
<object class="wxButton" name="wxID_ CANCEL">
<label>Cancel</label>
</object>
<border>10</border>
<flag>wxLEFT</flag>
</object>
</object>
<flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_ RIGHT</flag>
<border>10</border>
</object>
</object>
<title>Second testing dialog</title>
</object>
</resource>
\end { verbatim}
\subsection { XRC file format} \label { xrcfileformat}
2004-05-04 04:27:20 -04:00
Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWidgets
2002-01-27 14:58:13 -05:00
distribution.
2002-01-26 16:52:55 -05:00
2004-02-05 16:58:29 -05:00
\subsection { C++ header file generation} \label { xrccppheader}
Using the { \tt -e} switch together with { \tt -c} , a C++ header file is written
containing class definitions for the GUI windows defined in the XRC file.
This code generation can make it easier to use XRC and automate program
development.
The classes can be used as basis for development, freeing the
2004-10-22 15:15:35 -04:00
programmer from dealing with most of the XRC specifics (e.g. { \tt XRCCTRL} ).
2004-02-05 16:58:29 -05:00
For each top level window defined in the XRC file a C++ class definition is
generated, containing as class members the named widgets of the window.
A default constructor for each class is also generated. Inside the constructor
all XRC loading is done and all class members representing widgets are initialized.
A simple example will help understand how the scheme works. Suppose you have
a XRC file defining a top level window { \tt TestWnd\_ Base} , which subclasses { \tt wxFrame} (any
other class like { \tt wxDialog} will do also), and has subwidgets { \tt wxTextCtrl} A and { \tt wxButton} B.
The XRC file and corresponding class definition in the header file will be something like:
\begin { verbatim}
<?xml version="1.0"?>
<resource version="2.3.0.1">
<object class="wxFrame" name="TestWnd_ Base">
<size>-1,-1</size>
<title>Test</title>
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxTextCtrl" name="A">
<label>Test label</label>
</object>
</object>
<object class="sizeritem">
<object class="wxButton" name="B">
<label>Test button</label>
</object>
</object>
</object>
</object>
</resource>
class TestWnd_ Base : public wxFrame {
protected:
wxTextCtrl* A;
wxButton* B;
private:
void InitWidgetsFromXRC(){
wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
A = XRCCTRL(*this,"A",wxTextCtrl);
B = XRCCTRL(*this,"B",wxButton);
}
public:
TestWnd::TestWnd(){
InitWidgetsFromXRC();
}
} ;
\end { verbatim}
The generated window class can be used as basis for the full window class. The
class members which represent widgets may be accessed by name instead of using
{ \tt XRCCTRL} every time you wish to reference them (note that they are { \tt protected} class members),
2004-12-11 16:33:17 -05:00
though you must still use { \tt XRCID} to refer to widget IDs in the event
2004-02-05 16:58:29 -05:00
table.
Example:
2004-02-21 20:16:32 -05:00
2004-02-05 16:58:29 -05:00
\begin { verbatim}
#include "resource.h"
class TestWnd : public TestWnd_ Base {
public:
TestWnd(){
// A, B already initialised at this point
A->SetValue("Updated in TestWnd::TestWnd");
B->SetValue("Nice :)");
}
void OnBPressed(wxEvent& event){
Close();
}
DECLARE_ EVENT_ TABLE();
} ;
BEGIN_ EVENT_ TABLE(TestWnd,TestWnd_ Base)
EVT_ BUTTON(XRCID("B"),TestWnd::OnBPressed)
END_ EVENT_ TABLE()
\end { verbatim}
2002-01-26 16:52:55 -05:00
\subsection { Adding new resource handlers} \label { newresourcehandlers}
2002-01-23 10:17:44 -05:00
2002-01-26 16:52:55 -05:00
Coming soon.
2002-01-23 10:17:44 -05:00