]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/txrc.tex
suppress VC7.1 64 bit correctness warnings
[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++
f9363b96
VS
11code (the former makes it possible to store all resources in since file and the latter
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.
19\item If you use a dialog designers that generates C++ code, it can be hard
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
32\subsection{Compiling and using XRC}\label{compilingxrc}
33
34XRC can be found under the 'contrib' hierarchy, in the following directories:
35
36\begin{verbatim}
37 contrib/src/xrc ; XRC source
38 contrib/include/wx/xrc ; XRC headers
39 contrib/samples/xrc ; XRC sample
40 contrib/utils/wxrc ; XRC resource compiler
41 contrib/utils/wxrcedit ; XRC editor (in progress)
42\end{verbatim}
43
44To compile XRC:
45
46\begin{itemize}\itemsep=0pt
47\item Under Windows using VC++, open the contrib/src/xrc/XrcVC.dsw project
48and compile. Also compile contrib/utils/wxrc using wxBase if you wish to compile
49resource files.
50\item Under Unix, XRC should be configured when you configured
fc2171bd 51wxWidgets. Make XRC by changing directory to contrib/src/xrc and
d958c9bd
JS
52type 'make'. Similarly compile contrib/utils/wxrc using wxBase if you wish to compile
53resource files. {\bf Note:} there is currently a
fc2171bd 54problem with the wxWidgets build system that means that
d958c9bd
JS
55only the static version of library can be built at present.
56\end{itemize}
57
58To use XRC:
59
60\begin{itemize}\itemsep=0pt
61\item Under Windows using VC++, link with wxxrc[d].lib.
62\item Under Unix, link with libwxxrc[d].a.
63\end{itemize}
64
65\subsection{XRC concepts}\label{xrcconcepts}
66
67These are the typical steps for using XRC files in your application.
68
69\begin{itemize}\itemsep=0pt
70\item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
f9363b96
VS
71\item If you are going to use \helpref{XRS files}{binaryresourcefiles}, install
72wxFileSystem ZIP handler first with {\tt wxFileSystem::AddHandler(new wxZipFSHandler);}
9ecce691
VS
73\item call {\tt wxXmlResource::Get()->InitAllHandlers()} from your wxApp::OnInit function,
74and then call {\tt wxXmlResource::Get()->Load("myfile.xrc")} to load the resource file;
d958c9bd 75\item to create a dialog from a resource, create it using the default constructor, and then
7af3ca16 76load using for example {\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");}
9ecce691
VS
77\item set up event tables as usual but use the {\tt XRCID(str)} macro to translate from XRC string names
78to a suitable integer identifier, for example {\tt EVT\_MENU(XRCID("quit"), MyFrame::OnQuit)}.
d958c9bd
JS
79\end{itemize}
80
ff7047fd 81To create an XRC file, you can use one of the following methods.
d958c9bd 82
7af3ca16 83\begin{itemize}\itemsep=0pt
d958c9bd 84\item Create the file by hand;
d8908b52 85\item use \urlref{wxDesigner}{http://www.roebling.de}, a commercial dialog designer/RAD tool;
ff7047fd
JS
86\item use \urlref{DialogBlocks}{http://www.anthemion.co.uk/dialogblocks}, a commercial dialog editor;
87\item use \urlref{XRCed}{http://xrced.sf.net}, a wxPython-based
fc2171bd 88dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWidgets
d8908b52 89CVS archive;
ff7047fd 90\item use \urlref{Glade}{http://wxglade.sf.net}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
d8908b52
JS
91\item use wxrcedit ({\tt utils/contrib/wxrcedit}) (under development);
92\item convert WIN32 RC files to XRC with the tool in {\tt contrib/utils/convertrc}.
d958c9bd
JS
93\end{itemize}
94
fc2171bd 95A 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
96
97It is highly recommended that you use a resource editing tool, since it's fiddly writing
d958c9bd
JS
98XRC files by hand.
99
100You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways.
f9363b96
VS
101You can pass an XRC file (XML-based text resource file)
102or a \helpref{zip-compressed file}{binaryresourcefiles} (extension ZIP or XRS) containing other XRC.
d958c9bd 103
f9363b96 104You can also use \helpref{embedded C++ resources}{embeddedresource}
d958c9bd
JS
105
106\subsection{Using binary resource files}\label{binaryresourcefiles}
107
f9363b96
VS
108To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters
109(the input XRC files) and the following switches and options:
7af3ca16 110\begin{itemize}\itemsep=0pt
d958c9bd
JS
111\item -h (--help): show a help message
112\item -v (--verbose): show verbose logging information
f9363b96 113\item -c (--cpp-code): write C++ source rather than a XRS file
1dce6f09
VS
114\item -e (--extra-cpp-code): if used together with -c, generates C++ header file
115containing class definitions for the windows defined by the XRC file (see special subsection)
d958c9bd
JS
116\item -u (--uncompressed): do not compress XML files (C++ only)
117\item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
118\item -n (--function) <name>: specify C++ function name (use with -c)
119\item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
120\item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
121\end{itemize}
122
123For example:
d958c9bd
JS
124\begin{verbatim}
125 % wxrc resource.wrc
126 % wxrc resource.wrc -o resource.wrs
127 % wxrc resource.wrc -v -c -o resource.cpp
128\end{verbatim}
129
f9363b96
VS
130\wxheading{Note}
131
132XRS file is esentially a renamed ZIP archive which means that you can manipulate
133it with standard ZIP tools. Note that if you are using XRS files, you have
134to initialize \helpref{wxFileSystem}{wxfilesystem} ZIP handler first! It is a simple
135thing to do:
d2c2afc9 136
f9363b96
VS
137\begin{verbatim}
138 #include <wx/filesys.h>
139 #include <wx/fs_zip.h>
140 ...
141 wxFileSystem::AddHandler(new wxZipFSHandler);
142\end{verbatim}
143
144\subsection{Using embedded resources}\label{embeddedresource}
145
146It is sometimes useful to embed resources in the executable itself instead
147of loading external file (e.g. when your app is small and consists only of one
148exe file). XRC provides means to convert resources into regular C++ file that
149can be compiled and included in the executable.
150
151Use the {\tt -c} switch to
152{\tt wxrc} utility to produce C++ file with embedded resources. This file will
153contain a function called {\it InitXmlResource} (unless you override this with
154a command line switch). Use it to load the resource:
d2c2afc9 155
f9363b96 156\begin{verbatim}
96aba1e9
VS
157 extern void InitXMLResource(); // defined in generated file
158 ...
f9363b96
VS
159 wxXmlResource::Get()->InitAllHandlers();
160 InitXmlResource();
161 ...
162\end{verbatim}
163
d958c9bd
JS
164\subsection{XRC C++ sample}\label{xrccppsample}
165
166This is the C++ source file (xrcdemo.cpp) for the XRC sample.
167
168\begin{verbatim}
88bed4b4 169#include "wx/wx.h"
d958c9bd
JS
170#include "wx/image.h"
171#include "wx/xrc/xmlres.h"
172
d958c9bd
JS
173// the application icon
174#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
175 #include "rc/appicon.xpm"
176#endif
177
178// ----------------------------------------------------------------------------
179// private classes
180// ----------------------------------------------------------------------------
181
182// Define a new application type, each program should derive a class from wxApp
183class MyApp : public wxApp
184{
185public:
186 // override base class virtuals
187 // ----------------------------
188
189 // this one is called on application startup and is a good place for the app
190 // initialization (doing it here and not in the ctor allows to have an error
191 // return: if OnInit() returns false, the application terminates)
192 virtual bool OnInit();
193};
194
195// Define a new frame type: this is going to be our main frame
196class MyFrame : public wxFrame
197{
198public:
199 // ctor(s)
200 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
201
202 // event handlers (these functions should _not_ be virtual)
203 void OnQuit(wxCommandEvent& event);
204 void OnAbout(wxCommandEvent& event);
205 void OnDlg1(wxCommandEvent& event);
206 void OnDlg2(wxCommandEvent& event);
207
208private:
fc2171bd 209 // any class wishing to process wxWidgets events must use this macro
d958c9bd
JS
210 DECLARE_EVENT_TABLE()
211};
212
213// ----------------------------------------------------------------------------
fc2171bd 214// event tables and other macros for wxWidgets
d958c9bd
JS
215// ----------------------------------------------------------------------------
216
d958c9bd 217BEGIN_EVENT_TABLE(MyFrame, wxFrame)
094eb71a
VS
218 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
219 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
220 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
221 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
d958c9bd
JS
222END_EVENT_TABLE()
223
d958c9bd
JS
224IMPLEMENT_APP(MyApp)
225
d958c9bd
JS
226// ----------------------------------------------------------------------------
227// the application class
228// ----------------------------------------------------------------------------
229
230// 'Main program' equivalent: the program execution "starts" here
231bool MyApp::OnInit()
232{
233 wxImage::AddHandler(new wxGIFHandler);
234 wxXmlResource::Get()->InitAllHandlers();
235 wxXmlResource::Get()->Load("rc/resource.xrc");
236
237 MyFrame *frame = new MyFrame("XML resources demo",
238 wxPoint(50, 50), wxSize(450, 340));
cc81d32f
VS
239 frame->Show(true);
240 return true;
d958c9bd
JS
241}
242
243// ----------------------------------------------------------------------------
244// main frame
245// ----------------------------------------------------------------------------
246
247// frame constructor
248MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
249 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
250{
251 SetIcon(wxICON(appicon));
252
253 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
254 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
255}
256
d958c9bd 257// event handlers
d958c9bd
JS
258void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
259{
cc81d32f
VS
260 // true is to force the frame to close
261 Close(true);
d958c9bd
JS
262}
263
264void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
265{
266 wxString msg;
267 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
268 _T("Welcome to %s"), wxVERSION_STRING);
269
270 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
271}
272
273void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
274{
275 wxDialog dlg;
276 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
277 dlg.ShowModal();
278}
279
d958c9bd
JS
280void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
281{
282 wxDialog dlg;
283 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
284 dlg.ShowModal();
285}
286\end{verbatim}
287
288\subsection{XRC resource file sample}\label{xrcsample}
289
290This is the XML file (resource.xrc) for the XRC sample.
291
292\begin{verbatim}
293<?xml version="1.0"?>
9ecce691 294<resource version="2.3.0.1">
d958c9bd
JS
295 <object class="wxMenuBar" name="mainmenu">
296 <style>wxMB_DOCKABLE</style>
297 <object class="wxMenu" name="menu_file">
9ecce691 298 <label>_File</label>
d958c9bd
JS
299 <style>wxMENU_TEAROFF</style>
300 <object class="wxMenuItem" name="menu_about">
9ecce691 301 <label>_About...</label>
d958c9bd
JS
302 <bitmap>filesave.gif</bitmap>
303 </object>
304 <object class="separator"/>
305 <object class="wxMenuItem" name="menu_dlg1">
306 <label>Dialog 1</label>
307 </object>
308 <object class="wxMenuItem" name="menu_dlg2">
309 <label>Dialog 2</label>
310 </object>
311 <object class="separator"/>
312 <object class="wxMenuItem" name="menu_quit">
9ecce691 313 <label>E_xit\tAlt-X</label>
d958c9bd
JS
314 </object>
315 </object>
316 </object>
317 <object class="wxToolBar" name="toolbar">
318 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
319 <margins>2,2</margins>
320 <object class="tool" name="menu_open">
321 <bitmap>fileopen.gif</bitmap>
322 <tooltip>Open catalog</tooltip>
323 </object>
324 <object class="tool" name="menu_save">
325 <bitmap>filesave.gif</bitmap>
326 <tooltip>Save catalog</tooltip>
327 </object>
328 <object class="tool" name="menu_update">
329 <bitmap>update.gif</bitmap>
330 <tooltip>Update catalog - synchronize it with sources</tooltip>
331 </object>
332 <separator/>
333 <object class="tool" name="menu_quotes">
334 <bitmap>quotes.gif</bitmap>
335 <toggle>1</toggle>
336 <tooltip>Display quotes around the string?</tooltip>
337 </object>
338 <object class="separator"/>
339 <object class="tool" name="menu_fuzzy">
340 <bitmap>fuzzy.gif</bitmap>
341 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
342 <toggle>1</toggle>
343 </object>
344 </object>
345 <object class="wxDialog" name="dlg1">
346 <object class="wxBoxSizer">
347 <object class="sizeritem">
348 <object class="wxBitmapButton">
349 <bitmap>fuzzy.gif</bitmap>
350 <focus>fileopen.gif</focus>
351 </object>
352 </object>
353 <object class="sizeritem">
354 <object class="wxPanel">
355 <object class="wxStaticText">
356 <label>fdgdfgdfgdfg</label>
357 </object>
358 <style>wxSUNKEN_BORDER</style>
359 </object>
360 <flag>wxALIGN_CENTER</flag>
361 </object>
362 <object class="sizeritem">
363 <object class="wxButton">
364 <label>Buttonek</label>
365 </object>
366 <border>10d</border>
367 <flag>wxALL</flag>
368 </object>
369 <object class="sizeritem">
370 <object class="wxHtmlWindow">
371 <htmlcode>&lt;h1&gt;Hi,&lt;/h1&gt;man</htmlcode>
372 <size>100,45d</size>
373 </object>
374 </object>
375 <object class="sizeritem">
376 <object class="wxNotebook">
377 <object class="notebookpage">
378 <object class="wxPanel">
379 <object class="wxBoxSizer">
380 <object class="sizeritem">
381 <object class="wxHtmlWindow">
382 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
383 <size>50,50d</size>
384 </object>
385 <option>1</option>
386 </object>
387 </object>
388 </object>
389 <label>Page</label>
390 </object>
391 <object class="notebookpage">
392 <object class="wxPanel">
393 <object class="wxBoxSizer">
394 <object class="sizeritem">
395 <object class="wxHtmlWindow">
396 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
397 <size>50,50d</size>
398 </object>
399 </object>
400 </object>
401 </object>
402 <label>Page 2</label>
403 </object>
404 <usenotebooksizer>1</usenotebooksizer>
405 </object>
406 <flag>wxEXPAND</flag>
407 </object>
408 <orient>wxVERTICAL</orient>
409 </object>
410 </object>
411 <object class="wxDialog" name="dlg2">
412 <object class="wxBoxSizer">
413 <orient>wxVERTICAL</orient>
414 <object class="sizeritem" name="dfgdfg">
415 <object class="wxTextCtrl">
416 <size>200,200d</size>
417 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
418 <value>Hello, this is an ordinary multiline\n textctrl....</value>
419 </object>
420 <option>1</option>
421 <flag>wxEXPAND|wxALL</flag>
422 <border>10</border>
423 </object>
424 <object class="sizeritem">
425 <object class="wxBoxSizer">
426 <object class="sizeritem">
427 <object class="wxButton" name="wxID_OK">
428 <label>Ok</label>
429 <default>1</default>
430 </object>
431 </object>
432 <object class="sizeritem">
433 <object class="wxButton" name="wxID_CANCEL">
434 <label>Cancel</label>
435 </object>
436 <border>10</border>
437 <flag>wxLEFT</flag>
438 </object>
439 </object>
440 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
441 <border>10</border>
442 </object>
443 </object>
444 <title>Second testing dialog</title>
445 </object>
446</resource>
447\end{verbatim}
448
449\subsection{XRC file format}\label{xrcfileformat}
450
fc2171bd 451Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWidgets
1c0c339c 452distribution.
d8908b52 453
1dce6f09
VS
454\subsection{C++ header file generation}\label{xrccppheader}
455
456Using the {\tt -e} switch together with {\tt -c}, a C++ header file is written
457containing class definitions for the GUI windows defined in the XRC file.
458This code generation can make it easier to use XRC and automate program
459development.
460The classes can be used as basis for development, freeing the
461programmer from dealing with most of the XRC specifities (e.g. {\tt XRCCTRL}).
462
463For each top level window defined in the XRC file a C++ class definition is
464generated, containing as class members the named widgets of the window.
465A default constructor for each class is also generated. Inside the constructor
466all XRC loading is done and all class members representing widgets are initialized.
467
468A simple example will help understand how the scheme works. Suppose you have
469a XRC file defining a top level window {\tt TestWnd\_Base}, which subclasses {\tt wxFrame} (any
470other class like {\tt wxDialog} will do also), and has subwidgets {\tt wxTextCtrl} A and {\tt wxButton} B.
471The XRC file and corresponding class definition in the header file will be something like:
472
473\begin{verbatim}
474<?xml version="1.0"?>
475<resource version="2.3.0.1">
476 <object class="wxFrame" name="TestWnd_Base">
477 <size>-1,-1</size>
478 <title>Test</title>
479 <object class="wxBoxSizer">
480 <orient>wxHORIZONTAL</orient>
481 <object class="sizeritem">
482 <object class="wxTextCtrl" name="A">
483 <label>Test label</label>
484 </object>
485 </object>
486 <object class="sizeritem">
487 <object class="wxButton" name="B">
488 <label>Test button</label>
489 </object>
490 </object>
491 </object>
492 </object>
493</resource>
494
495
496class TestWnd_Base : public wxFrame {
497protected:
498 wxTextCtrl* A;
499 wxButton* B;
500
501private:
502 void InitWidgetsFromXRC(){
503 wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
504 A = XRCCTRL(*this,"A",wxTextCtrl);
505 B = XRCCTRL(*this,"B",wxButton);
506 }
507public:
508TestWnd::TestWnd(){
509 InitWidgetsFromXRC();
510 }
511};
512\end{verbatim}
513
514The generated window class can be used as basis for the full window class. The
515class members which represent widgets may be accessed by name instead of using
516{\tt XRCCTRL} every time you wish to reference them (note that they are {\tt protected} class members),
517though you must still use {\tt XRCID} to refer to widget ids in the event
518table.
519
520Example:
d2c2afc9 521
1dce6f09
VS
522\begin{verbatim}
523#include "resource.h"
524
525class TestWnd : public TestWnd_Base {
526 public:
527 TestWnd(){
528 // A, B already initialised at this point
529 A->SetValue("Updated in TestWnd::TestWnd");
530 B->SetValue("Nice :)");
531 }
532 void OnBPressed(wxEvent& event){
533 Close();
534 }
535 DECLARE_EVENT_TABLE();
536};
537
538BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
539EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
540END_EVENT_TABLE()
541
542\end{verbatim}
543
d8908b52 544\subsection{Adding new resource handlers}\label{newresourcehandlers}
d958c9bd 545
d8908b52 546Coming soon.
d958c9bd 547