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