1 % Note: -e/C++ header generation documentation added by
2 % Eduardo Marques <edrdo@netcabo.pt>
4 \section{XML-based resource system overview
}\label{xrcoverview
}
6 Classes:
\helpref{wxXmlResource
}{wxxmlresource
},
\helpref{wxXmlResourceHandler
}{wxxmlresourcehandler
}
8 The XML-based resource system, known as XRC, allows user interface elements such as
9 dialogs, menu bars and toolbars, to be stored in text files and loaded into
10 the application at run-time. XRC files can also be compiled into binary XRS files or C++
11 code (the former makes it possible to store all resources in since file and the latter
12 is useful when you want to embed the resources into the executable).
14 There are several advantages to using XRC resources.
16 \begin{itemize
}\itemsep=
0pt
17 \item Recompiling and linking an application is not necessary if the
19 \item If you use a dialog designers that generates C++ code, it can be hard
20 to reintegrate this into existing C++ code. Separation of resources and code
21 is a more elegant solution.
22 \item You can choose between different alternative resource files at run time, if necessary.
23 \item The XRC format uses sizers for flexibility, allowing dialogs to be resizable
25 \item The XRC format is a wxWidgets standard,
26 and can be generated or postprocessed by any program that understands it. As it is based
27 on the XML standard, existing XML editors can be used for simple editing purposes.
30 XRC was written by Vaclav Slavik.
32 \subsection{XRC concepts
}\label{xrcconcepts
}
34 These are the typical steps for using XRC files in your application.
36 \begin{itemize
}\itemsep=
0pt
37 \item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
38 \item If you are going to use
\helpref{XRS files
}{binaryresourcefiles
}, install
39 wxFileSystem ZIP handler first with
{\tt wxFileSystem::AddHandler(new wxZipFSHandler);
}
40 \item call
{\tt wxXmlResource::Get()->InitAllHandlers()
} from your wxApp::OnInit function,
41 and then call
{\tt wxXmlResource::Get()->Load("myfile.xrc")
} to load the resource file;
42 \item to create a dialog from a resource, create it using the default constructor, and then
43 load using for example
{\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");
}
44 \item set up event tables as usual but use the
{\tt XRCID(str)
} macro to translate from XRC string names
45 to a suitable integer identifier, for example
{\tt EVT
\_MENU(XRCID("quit"), MyFrame::OnQuit)
}.
48 To create an XRC file, you can use one of the following methods.
50 \begin{itemize
}\itemsep=
0pt
51 \item Create the file by hand;
52 \item use
\urlref{wxDesigner
}{http://www.roebling.de
}, a commercial dialog designer/RAD tool;
53 \item use
\urlref{DialogBlocks
}{http://www.anthemion.co.uk/dialogblocks
}, a commercial dialog editor;
54 \item use
\urlref{XRCed
}{http://xrced.sf.net
}, a wxPython-based
55 dialog editor that you can find in the
{\tt wxPython/tools
} subdirectory of the wxWidgets
57 \item use
\urlref{Glade
}{http://wxglade.sf.net
}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
58 \item convert WIN32 RC files to XRC with the tool in
{\tt contrib/utils/convertrc
}.
61 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
}.
63 It is highly recommended that you use a resource editing tool, since it's fiddly writing
66 You can use
\helpref{wxXmlResource::Load
}{wxxmlresourceload
} in a number of ways.
67 You can pass an XRC file (XML-based text resource file)
68 or a
\helpref{zip-compressed file
}{binaryresourcefiles
} (extension ZIP or XRS) containing other XRC.
70 You can also use
\helpref{embedded C++ resources
}{embeddedresource
}
72 \subsection{Using binary resource files
}\label{binaryresourcefiles
}
74 To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
75 (the input XRC files) and the following switches and options:
76 \begin{itemize
}\itemsep=
0pt
77 \item -h (--help): show a help message
78 \item -v (--verbose): show verbose logging information
79 \item -c (--cpp-code): write C++ source rather than a XRS file
80 \item -e (--extra-cpp-code): if used together with -c, generates C++ header file
81 containing class definitions for the windows defined by the XRC file (see special subsection)
82 \item -u (--uncompressed): do not compress XML files (C++ only)
83 \item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
84 \item -n (--function) <name>: specify C++ function name (use with -c)
85 \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
86 \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
92 % wxrc resource.xrc -o resource.xrs
93 % wxrc resource.xrc -v -c -o resource.cpp
98 XRS file is essentially a renamed ZIP archive which means that you can manipulate
99 it with standard ZIP tools. Note that if you are using XRS files, you have
100 to initialize
\helpref{wxFileSystem
}{wxfilesystem
} ZIP handler first! It is a simple
104 #include <wx/filesys.h>
105 #include <wx/fs_zip.h>
107 wxFileSystem::AddHandler(new wxZipFSHandler);
110 \subsection{Using embedded resources
}\label{embeddedresource
}
112 It is sometimes useful to embed resources in the executable itself instead
113 of loading external file (e.g. when your app is small and consists only of one
114 exe file). XRC provides means to convert resources into regular C++ file that
115 can be compiled and included in the executable.
117 Use the
{\tt -c
} switch to
118 {\tt wxrc
} utility to produce C++ file with embedded resources. This file will
119 contain a function called
{\it InitXmlResource
} (unless you override this with
120 a command line switch). Use it to load the resource:
123 extern void InitXmlResource(); // defined in generated file
125 wxXmlResource::Get()->InitAllHandlers();
130 \subsection{XRC C++ sample
}\label{xrccppsample
}
132 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
136 #include "wx/image.h"
137 #include "wx/xrc/xmlres.h"
139 // the application icon
140 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
141 #include "rc/appicon.xpm"
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 // Define a new application type, each program should derive a class from wxApp
149 class MyApp : public wxApp
152 // override base class virtuals
153 // ----------------------------
155 // this one is called on application startup and is a good place for the app
156 // initialization (doing it here and not in the ctor allows to have an error
157 // return: if OnInit() returns false, the application terminates)
158 virtual bool OnInit();
161 // Define a new frame type: this is going to be our main frame
162 class MyFrame : public wxFrame
166 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
168 // event handlers (these functions should _not_ be virtual)
169 void OnQuit(wxCommandEvent& event);
170 void OnAbout(wxCommandEvent& event);
171 void OnDlg1(wxCommandEvent& event);
172 void OnDlg2(wxCommandEvent& event);
175 // any class wishing to process wxWidgets events must use this macro
176 DECLARE_EVENT_TABLE()
179 // ----------------------------------------------------------------------------
180 // event tables and other macros for wxWidgets
181 // ----------------------------------------------------------------------------
183 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
184 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
185 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
186 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
187 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
192 // ----------------------------------------------------------------------------
193 // the application class
194 // ----------------------------------------------------------------------------
196 // 'Main program' equivalent: the program execution "starts" here
199 wxImage::AddHandler(new wxGIFHandler);
200 wxXmlResource::Get()->InitAllHandlers();
201 wxXmlResource::Get()->Load("rc/resource.xrc");
203 MyFrame *frame = new MyFrame("XML resources demo",
204 wxPoint(
50,
50), wxSize(
450,
340));
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
215 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
217 SetIcon(wxICON(appicon));
219 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
220 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
224 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
226 // true is to force the frame to close
230 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
233 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
234 _T("Welcome to
%s"), wxVERSION_STRING);
236 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
239 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
242 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
246 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
249 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
254 \subsection{XRC resource file sample
}\label{xrcsample
}
256 This is the XML file (resource.xrc) for the XRC sample.
259 <?xml version="
1.0"?>
260 <resource version="
2.3.0.1">
261 <object class="wxMenuBar" name="mainmenu">
262 <style>wxMB_DOCKABLE</style>
263 <object class="wxMenu" name="menu_file">
265 <style>wxMENU_TEAROFF</style>
266 <object class="wxMenuItem" name="menu_about">
267 <label>_About...</label>
268 <bitmap>filesave.gif</bitmap>
270 <object class="separator"/>
271 <object class="wxMenuItem" name="menu_dlg1">
272 <label>Dialog
1</label>
274 <object class="wxMenuItem" name="menu_dlg2">
275 <label>Dialog
2</label>
277 <object class="separator"/>
278 <object class="wxMenuItem" name="menu_quit">
279 <label>E_xit
\tAlt-X</label>
283 <object class="wxToolBar" name="toolbar">
284 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
285 <margins>
2,
2</margins>
286 <object class="tool" name="menu_open">
287 <bitmap>fileopen.gif</bitmap>
288 <tooltip>Open catalog</tooltip>
290 <object class="tool" name="menu_save">
291 <bitmap>filesave.gif</bitmap>
292 <tooltip>Save catalog</tooltip>
294 <object class="tool" name="menu_update">
295 <bitmap>update.gif</bitmap>
296 <tooltip>Update catalog - synchronize it with sources</tooltip>
299 <object class="tool" name="menu_quotes">
300 <bitmap>quotes.gif</bitmap>
302 <tooltip>Display quotes around the string?</tooltip>
304 <object class="separator"/>
305 <object class="tool" name="menu_fuzzy">
306 <bitmap>fuzzy.gif</bitmap>
307 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
311 <object class="wxDialog" name="dlg1">
312 <object class="wxBoxSizer">
313 <object class="sizeritem">
314 <object class="wxBitmapButton">
315 <bitmap>fuzzy.gif</bitmap>
316 <focus>fileopen.gif</focus>
319 <object class="sizeritem">
320 <object class="wxPanel">
321 <object class="wxStaticText">
322 <label>fdgdfgdfgdfg</label>
324 <style>wxSUNKEN_BORDER</style>
326 <flag>wxALIGN_CENTER</flag>
328 <object class="sizeritem">
329 <object class="wxButton">
330 <label>Buttonek</label>
335 <object class="sizeritem">
336 <object class="wxHtmlWindow">
337 <htmlcode><h1>Hi,</h1>man</htmlcode>
341 <object class="sizeritem">
342 <object class="wxNotebook">
343 <object class="notebookpage">
344 <object class="wxPanel">
345 <object class="wxBoxSizer">
346 <object class="sizeritem">
347 <object class="wxHtmlWindow">
348 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
357 <object class="notebookpage">
358 <object class="wxPanel">
359 <object class="wxBoxSizer">
360 <object class="sizeritem">
361 <object class="wxHtmlWindow">
362 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
368 <label>Page
2</label>
370 <usenotebooksizer>
1</usenotebooksizer>
372 <flag>wxEXPAND</flag>
374 <orient>wxVERTICAL</orient>
377 <object class="wxDialog" name="dlg2">
378 <object class="wxBoxSizer">
379 <orient>wxVERTICAL</orient>
380 <object class="sizeritem" name="dfgdfg">
381 <object class="wxTextCtrl">
382 <size>
200,
200d</size>
383 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
384 <value>Hello, this is an ordinary multiline
\n textctrl....</value>
387 <flag>wxEXPAND|wxALL</flag>
390 <object class="sizeritem">
391 <object class="wxBoxSizer">
392 <object class="sizeritem">
393 <object class="wxButton" name="wxID_OK">
398 <object class="sizeritem">
399 <object class="wxButton" name="wxID_CANCEL">
400 <label>Cancel</label>
406 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
410 <title>Second testing dialog</title>
415 \subsection{XRC file format
}\label{xrcfileformat
}
417 Please see Technical Note
14 (docs/tech/tn0014.txt) in your wxWidgets
420 \subsection{C++ header file generation
}\label{xrccppheader
}
422 Using the
{\tt -e
} switch together with
{\tt -c
}, a C++ header file is written
423 containing class definitions for the GUI windows defined in the XRC file.
424 This code generation can make it easier to use XRC and automate program
426 The classes can be used as basis for development, freeing the
427 programmer from dealing with most of the XRC specifics (e.g.
{\tt XRCCTRL
}).
429 For each top level window defined in the XRC file a C++ class definition is
430 generated, containing as class members the named widgets of the window.
431 A default constructor for each class is also generated. Inside the constructor
432 all XRC loading is done and all class members representing widgets are initialized.
434 A simple example will help understand how the scheme works. Suppose you have
435 a XRC file defining a top level window
{\tt TestWnd
\_Base}, which subclasses
{\tt wxFrame
} (any
436 other class like
{\tt wxDialog
} will do also), and has subwidgets
{\tt wxTextCtrl
} A and
{\tt wxButton
} B.
437 The XRC file and corresponding class definition in the header file will be something like:
440 <?xml version="
1.0"?>
441 <resource version="
2.3.0.1">
442 <object class="wxFrame" name="TestWnd_Base">
445 <object class="wxBoxSizer">
446 <orient>wxHORIZONTAL</orient>
447 <object class="sizeritem">
448 <object class="wxTextCtrl" name="A">
449 <label>Test label</label>
452 <object class="sizeritem">
453 <object class="wxButton" name="B">
454 <label>Test button</label>
462 class TestWnd_Base : public wxFrame
{
468 void InitWidgetsFromXRC()
{
469 wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
470 A = XRCCTRL
(*this,"A",wxTextCtrl);
471 B = XRCCTRL(*this,"B",wxButton);
475 InitWidgetsFromXRC();
480 The generated window class can be used as basis for the full window class. The
481 class members which represent widgets may be accessed by name instead of using
482 {\tt XRCCTRL} every time you wish to reference them (note that they are {\tt protected} class members),
483 though you must still use {\tt XRCID} to refer to widget ids in the event
489 #include "resource.h"
491 class TestWnd : public TestWnd_Base {
494 // A, B already initialised at this point
495 A->SetValue("Updated in TestWnd::TestWnd");
496 B->SetValue("Nice :)");
498 void OnBPressed(wxEvent& event){
501 DECLARE_EVENT_TABLE();
504 BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
505 EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
510 \subsection{Adding new resource handlers}\label{newresourcehandlers}