]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/txrc.tex
MF_BYCOMMAND is zero so don't try to test it
[wxWidgets.git] / docs / latex / wx / txrc.tex
CommitLineData
1dce6f09
VS
1% Note: -e/C++ header generation documentation added by
2% Eduardo Marques <edrdo@netcabo.pt>
3%
d958c9bd
JS
4\section{XML-based resource system overview}\label{xrcoverview}
5
6Classes: \helpref{wxXmlResource}{wxxmlresource}, \helpref{wxXmlResourceHandler}{wxxmlresourcehandler}
7
8The XML-based resource system, known as XRC, allows user interface elements such as
9dialogs, menu bars and toolbars, to be stored in text files and loaded into
10the application at run-time. XRC files can also be compiled into binary XRS files or C++
3980000c 11code (the former makes it possible to store all resources in a single file and the latter
f9363b96 12is useful when you want to embed the resources into the executable).
d958c9bd
JS
13
14There are several advantages to using XRC resources.
15
16\begin{itemize}\itemsep=0pt
17\item Recompiling and linking an application is not necessary if the
18resources change.
3980000c 19\item If you use a dialog designer that generates C++ code, it can be hard
d958c9bd
JS
20to reintegrate this into existing C++ code. Separation of resources and code
21is 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
24and highly portable.
fc2171bd 25\item The XRC format is a wxWidgets standard,
d958c9bd
JS
26and can be generated or postprocessed by any program that understands it. As it is based
27on the XML standard, existing XML editors can be used for simple editing purposes.
28\end{itemize}
29
30XRC was written by Vaclav Slavik.
31
d958c9bd
JS
32\subsection{XRC concepts}\label{xrcconcepts}
33
34These are the typical steps for using XRC files in your application.
35
36\begin{itemize}\itemsep=0pt
37\item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
f9363b96
VS
38\item If you are going to use \helpref{XRS files}{binaryresourcefiles}, install
39wxFileSystem ZIP handler first with {\tt wxFileSystem::AddHandler(new wxZipFSHandler);}
9ecce691
VS
40\item call {\tt wxXmlResource::Get()->InitAllHandlers()} from your wxApp::OnInit function,
41and then call {\tt wxXmlResource::Get()->Load("myfile.xrc")} to load the resource file;
d958c9bd 42\item to create a dialog from a resource, create it using the default constructor, and then
3980000c 43load it using for example {\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");}
9ecce691
VS
44\item set up event tables as usual but use the {\tt XRCID(str)} macro to translate from XRC string names
45to a suitable integer identifier, for example {\tt EVT\_MENU(XRCID("quit"), MyFrame::OnQuit)}.
d958c9bd
JS
46\end{itemize}
47
ff7047fd 48To create an XRC file, you can use one of the following methods.
d958c9bd 49
7af3ca16 50\begin{itemize}\itemsep=0pt
d958c9bd 51\item Create the file by hand;
d8908b52 52\item use \urlref{wxDesigner}{http://www.roebling.de}, a commercial dialog designer/RAD tool;
ff7047fd
JS
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
fc2171bd 55dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWidgets
d8908b52 56CVS archive;
dbfb85a7 57\item use \urlref{wxGlade}{http://wxglade.sf.net}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
d8908b52 58\item convert WIN32 RC files to XRC with the tool in {\tt contrib/utils/convertrc}.
d958c9bd
JS
59\end{itemize}
60
fc2171bd 61A 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}.
ff7047fd
JS
62
63It is highly recommended that you use a resource editing tool, since it's fiddly writing
d958c9bd
JS
64XRC files by hand.
65
66You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways.
f9363b96
VS
67You can pass an XRC file (XML-based text resource file)
68or a \helpref{zip-compressed file}{binaryresourcefiles} (extension ZIP or XRS) containing other XRC.
d958c9bd 69
f9363b96 70You can also use \helpref{embedded C++ resources}{embeddedresource}
d958c9bd
JS
71
72\subsection{Using binary resource files}\label{binaryresourcefiles}
73
f9363b96
VS
74To 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:
7af3ca16 76\begin{itemize}\itemsep=0pt
d958c9bd
JS
77\item -h (--help): show a help message
78\item -v (--verbose): show verbose logging information
f9363b96 79\item -c (--cpp-code): write C++ source rather than a XRS file
1dce6f09
VS
80\item -e (--extra-cpp-code): if used together with -c, generates C++ header file
81containing class definitions for the windows defined by the XRC file (see special subsection)
d958c9bd 82\item -u (--uncompressed): do not compress XML files (C++ only)
dbfb85a7 83\item -g (--gettext): output underscore-wrapped strings that poEdit or gettext can scan. Outputs to stdout, or a file if -o is used
d958c9bd
JS
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
87\end{itemize}
88
89For example:
d958c9bd 90\begin{verbatim}
088dedd3
VS
91 % wxrc resource.xrc
92 % wxrc resource.xrc -o resource.xrs
93 % wxrc resource.xrc -v -c -o resource.cpp
d958c9bd
JS
94\end{verbatim}
95
f9363b96
VS
96\wxheading{Note}
97
dbd94b75 98XRS file is essentially a renamed ZIP archive which means that you can manipulate
f9363b96 99it with standard ZIP tools. Note that if you are using XRS files, you have
3980000c 100to initialize the \helpref{wxFileSystem}{wxfilesystem} ZIP handler first! It is a simple
f9363b96 101thing to do:
d2c2afc9 102
f9363b96
VS
103\begin{verbatim}
104 #include <wx/filesys.h>
105 #include <wx/fs_zip.h>
106 ...
107 wxFileSystem::AddHandler(new wxZipFSHandler);
108\end{verbatim}
109
110\subsection{Using embedded resources}\label{embeddedresource}
111
112It is sometimes useful to embed resources in the executable itself instead
3980000c 113of loading an external file (e.g. when your app is small and consists only of one
f9363b96
VS
114exe file). XRC provides means to convert resources into regular C++ file that
115can be compiled and included in the executable.
116
117Use the {\tt -c} switch to
118{\tt wxrc} utility to produce C++ file with embedded resources. This file will
119contain a function called {\it InitXmlResource} (unless you override this with
120a command line switch). Use it to load the resource:
d2c2afc9 121
f9363b96 122\begin{verbatim}
f6bb64a6 123 extern void InitXmlResource(); // defined in generated file
96aba1e9 124 ...
f9363b96
VS
125 wxXmlResource::Get()->InitAllHandlers();
126 InitXmlResource();
127 ...
128\end{verbatim}
129
d958c9bd
JS
130\subsection{XRC C++ sample}\label{xrccppsample}
131
132This is the C++ source file (xrcdemo.cpp) for the XRC sample.
133
134\begin{verbatim}
88bed4b4 135#include "wx/wx.h"
d958c9bd
JS
136#include "wx/image.h"
137#include "wx/xrc/xmlres.h"
138
d958c9bd
JS
139// the application icon
140#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
141 #include "rc/appicon.xpm"
142#endif
143
144// ----------------------------------------------------------------------------
145// private classes
146// ----------------------------------------------------------------------------
147
148// Define a new application type, each program should derive a class from wxApp
149class MyApp : public wxApp
150{
151public:
152 // override base class virtuals
153 // ----------------------------
154
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();
159};
160
161// Define a new frame type: this is going to be our main frame
162class MyFrame : public wxFrame
163{
164public:
165 // ctor(s)
166 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
167
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);
173
174private:
fc2171bd 175 // any class wishing to process wxWidgets events must use this macro
d958c9bd
JS
176 DECLARE_EVENT_TABLE()
177};
178
179// ----------------------------------------------------------------------------
fc2171bd 180// event tables and other macros for wxWidgets
d958c9bd
JS
181// ----------------------------------------------------------------------------
182
d958c9bd 183BEGIN_EVENT_TABLE(MyFrame, wxFrame)
094eb71a
VS
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)
d958c9bd
JS
188END_EVENT_TABLE()
189
d958c9bd
JS
190IMPLEMENT_APP(MyApp)
191
d958c9bd
JS
192// ----------------------------------------------------------------------------
193// the application class
194// ----------------------------------------------------------------------------
195
196// 'Main program' equivalent: the program execution "starts" here
197bool MyApp::OnInit()
198{
199 wxImage::AddHandler(new wxGIFHandler);
200 wxXmlResource::Get()->InitAllHandlers();
201 wxXmlResource::Get()->Load("rc/resource.xrc");
202
203 MyFrame *frame = new MyFrame("XML resources demo",
204 wxPoint(50, 50), wxSize(450, 340));
cc81d32f
VS
205 frame->Show(true);
206 return true;
d958c9bd
JS
207}
208
209// ----------------------------------------------------------------------------
210// main frame
211// ----------------------------------------------------------------------------
212
213// frame constructor
214MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
215 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
216{
217 SetIcon(wxICON(appicon));
218
219 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
220 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
221}
222
d958c9bd 223// event handlers
d958c9bd
JS
224void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
225{
cc81d32f
VS
226 // true is to force the frame to close
227 Close(true);
d958c9bd
JS
228}
229
230void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
231{
232 wxString msg;
233 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
234 _T("Welcome to %s"), wxVERSION_STRING);
235
236 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
237}
238
239void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
240{
241 wxDialog dlg;
242 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
243 dlg.ShowModal();
244}
245
d958c9bd
JS
246void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
247{
248 wxDialog dlg;
249 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
250 dlg.ShowModal();
251}
252\end{verbatim}
253
254\subsection{XRC resource file sample}\label{xrcsample}
255
256This is the XML file (resource.xrc) for the XRC sample.
257
258\begin{verbatim}
259<?xml version="1.0"?>
9ecce691 260<resource version="2.3.0.1">
d958c9bd
JS
261 <object class="wxMenuBar" name="mainmenu">
262 <style>wxMB_DOCKABLE</style>
263 <object class="wxMenu" name="menu_file">
9ecce691 264 <label>_File</label>
d958c9bd
JS
265 <style>wxMENU_TEAROFF</style>
266 <object class="wxMenuItem" name="menu_about">
9ecce691 267 <label>_About...</label>
d958c9bd
JS
268 <bitmap>filesave.gif</bitmap>
269 </object>
270 <object class="separator"/>
271 <object class="wxMenuItem" name="menu_dlg1">
272 <label>Dialog 1</label>
273 </object>
274 <object class="wxMenuItem" name="menu_dlg2">
275 <label>Dialog 2</label>
276 </object>
277 <object class="separator"/>
278 <object class="wxMenuItem" name="menu_quit">
9ecce691 279 <label>E_xit\tAlt-X</label>
d958c9bd
JS
280 </object>
281 </object>
282 </object>
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>
289 </object>
290 <object class="tool" name="menu_save">
291 <bitmap>filesave.gif</bitmap>
292 <tooltip>Save catalog</tooltip>
293 </object>
294 <object class="tool" name="menu_update">
295 <bitmap>update.gif</bitmap>
296 <tooltip>Update catalog - synchronize it with sources</tooltip>
297 </object>
298 <separator/>
299 <object class="tool" name="menu_quotes">
300 <bitmap>quotes.gif</bitmap>
301 <toggle>1</toggle>
302 <tooltip>Display quotes around the string?</tooltip>
303 </object>
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>
308 <toggle>1</toggle>
309 </object>
310 </object>
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>
317 </object>
318 </object>
319 <object class="sizeritem">
320 <object class="wxPanel">
321 <object class="wxStaticText">
322 <label>fdgdfgdfgdfg</label>
323 </object>
324 <style>wxSUNKEN_BORDER</style>
325 </object>
326 <flag>wxALIGN_CENTER</flag>
327 </object>
328 <object class="sizeritem">
329 <object class="wxButton">
330 <label>Buttonek</label>
331 </object>
332 <border>10d</border>
333 <flag>wxALL</flag>
334 </object>
335 <object class="sizeritem">
336 <object class="wxHtmlWindow">
337 <htmlcode>&lt;h1&gt;Hi,&lt;/h1&gt;man</htmlcode>
338 <size>100,45d</size>
339 </object>
340 </object>
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 &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
349 <size>50,50d</size>
350 </object>
351 <option>1</option>
352 </object>
353 </object>
354 </object>
355 <label>Page</label>
356 </object>
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 &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
363 <size>50,50d</size>
364 </object>
365 </object>
366 </object>
367 </object>
368 <label>Page 2</label>
369 </object>
370 <usenotebooksizer>1</usenotebooksizer>
371 </object>
372 <flag>wxEXPAND</flag>
373 </object>
374 <orient>wxVERTICAL</orient>
375 </object>
376 </object>
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>
385 </object>
386 <option>1</option>
387 <flag>wxEXPAND|wxALL</flag>
388 <border>10</border>
389 </object>
390 <object class="sizeritem">
391 <object class="wxBoxSizer">
392 <object class="sizeritem">
393 <object class="wxButton" name="wxID_OK">
394 <label>Ok</label>
395 <default>1</default>
396 </object>
397 </object>
398 <object class="sizeritem">
399 <object class="wxButton" name="wxID_CANCEL">
400 <label>Cancel</label>
401 </object>
402 <border>10</border>
403 <flag>wxLEFT</flag>
404 </object>
405 </object>
406 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
407 <border>10</border>
408 </object>
409 </object>
410 <title>Second testing dialog</title>
411 </object>
412</resource>
413\end{verbatim}
414
415\subsection{XRC file format}\label{xrcfileformat}
416
fc2171bd 417Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWidgets
1c0c339c 418distribution.
d8908b52 419
1dce6f09
VS
420\subsection{C++ header file generation}\label{xrccppheader}
421
422Using the {\tt -e} switch together with {\tt -c}, a C++ header file is written
423containing class definitions for the GUI windows defined in the XRC file.
424This code generation can make it easier to use XRC and automate program
425development.
426The classes can be used as basis for development, freeing the
dbd94b75 427programmer from dealing with most of the XRC specifics (e.g. {\tt XRCCTRL}).
1dce6f09
VS
428
429For each top level window defined in the XRC file a C++ class definition is
430generated, containing as class members the named widgets of the window.
431A default constructor for each class is also generated. Inside the constructor
432all XRC loading is done and all class members representing widgets are initialized.
433
434A simple example will help understand how the scheme works. Suppose you have
435a XRC file defining a top level window {\tt TestWnd\_Base}, which subclasses {\tt wxFrame} (any
436other class like {\tt wxDialog} will do also), and has subwidgets {\tt wxTextCtrl} A and {\tt wxButton} B.
437The XRC file and corresponding class definition in the header file will be something like:
438
439\begin{verbatim}
440<?xml version="1.0"?>
441<resource version="2.3.0.1">
442 <object class="wxFrame" name="TestWnd_Base">
443 <size>-1,-1</size>
444 <title>Test</title>
445 <object class="wxBoxSizer">
446 <orient>wxHORIZONTAL</orient>
447 <object class="sizeritem">
448 <object class="wxTextCtrl" name="A">
449 <label>Test label</label>
450 </object>
451 </object>
452 <object class="sizeritem">
453 <object class="wxButton" name="B">
454 <label>Test button</label>
455 </object>
456 </object>
457 </object>
458 </object>
459</resource>
460
461
462class TestWnd_Base : public wxFrame {
463protected:
464 wxTextCtrl* A;
465 wxButton* B;
466
467private:
468 void InitWidgetsFromXRC(){
469 wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
470 A = XRCCTRL(*this,"A",wxTextCtrl);
471 B = XRCCTRL(*this,"B",wxButton);
472 }
473public:
474TestWnd::TestWnd(){
475 InitWidgetsFromXRC();
476 }
477};
478\end{verbatim}
479
480The generated window class can be used as basis for the full window class. The
481class 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),
3980000c 483though you must still use {\tt XRCID} to refer to widget IDs in the event
1dce6f09
VS
484table.
485
486Example:
d2c2afc9 487
1dce6f09
VS
488\begin{verbatim}
489#include "resource.h"
490
491class TestWnd : public TestWnd_Base {
492 public:
493 TestWnd(){
494 // A, B already initialised at this point
495 A->SetValue("Updated in TestWnd::TestWnd");
496 B->SetValue("Nice :)");
497 }
498 void OnBPressed(wxEvent& event){
499 Close();
500 }
501 DECLARE_EVENT_TABLE();
502};
503
504BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
505EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
506END_EVENT_TABLE()
507
508\end{verbatim}
509
d8908b52 510\subsection{Adding new resource handlers}\label{newresourcehandlers}
d958c9bd 511
d8908b52 512Coming soon.
d958c9bd 513