]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/txrc.tex
Reverted renderer changes and just use window variant within renderer functions
[wxWidgets.git] / docs / latex / wx / txrc.tex
... / ...
CommitLineData
1% Note: -e/C++ header generation documentation added by
2% Eduardo Marques <edrdo@netcabo.pt>
3%
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++
11code (the former makes it possible to store all resources in a single file and the latter
12is useful when you want to embed the resources into the executable).
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 designer 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.
25\item The XRC format is a wxWidgets standard,
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{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;
38\item If you are going to use \helpref{XRS files}{binaryresourcefiles}, install
39wxFileSystem archive handler first with {\tt wxFileSystem::AddHandler(new wxArchiveFSHandler);}
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;
42\item to create a dialog from a resource, create it using the default constructor, and then
43load it 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
45to a suitable integer identifier, for example {\tt EVT\_MENU(XRCID("quit"), MyFrame::OnQuit)}.
46\end{itemize}
47
48To create an XRC file, you can use one of the following methods.
49
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
55dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWidgets
56CVS archive;
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;
58\item convert WIN32 RC files to XRC with the tool in {\tt contrib/utils/convertrc}.
59\end{itemize}
60
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.htm}.
62
63It is highly recommended that you use a resource editing tool, since it's fiddly writing
64XRC files by hand.
65
66You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways.
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.
69
70You can also use \helpref{embedded C++ resources}{embeddedresource}
71
72\subsection{Using binary resource files}\label{binaryresourcefiles}
73
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:
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
81containing 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 underscore-wrapped strings that poEdit or gettext can scan. Outputs 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
87\end{itemize}
88
89For example:
90\begin{verbatim}
91 % wxrc resource.xrc
92 % wxrc resource.xrc -o resource.xrs
93 % wxrc resource.xrc -v -c -o resource.cpp
94\end{verbatim}
95
96\wxheading{Note}
97
98XRS file is essentially a renamed ZIP archive which means that you can manipulate
99it with standard ZIP tools. Note that if you are using XRS files, you have
100to initialize the \helpref{wxFileSystem}{wxfilesystem} archive handler first! It is a simple
101thing to do:
102
103\begin{verbatim}
104 #include <wx/filesys.h>
105 #include <wx/fs_arc.h>
106 ...
107 wxFileSystem::AddHandler(new wxArchiveFSHandler);
108\end{verbatim}
109
110\subsection{Using embedded resources}\label{embeddedresource}
111
112It is sometimes useful to embed resources in the executable itself instead
113of loading an external file (e.g. when your app is small and consists only of one
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:
121
122\begin{verbatim}
123 extern void InitXmlResource(); // defined in generated file
124 ...
125 wxXmlResource::Get()->InitAllHandlers();
126 InitXmlResource();
127 ...
128\end{verbatim}
129
130\subsection{XRC C++ sample}\label{xrccppsample}
131
132This is the C++ source file (xrcdemo.cpp) for the XRC sample.
133
134\begin{verbatim}
135#include "wx/wx.h"
136#include "wx/image.h"
137#include "wx/xrc/xmlres.h"
138
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:
175 // any class wishing to process wxWidgets events must use this macro
176 DECLARE_EVENT_TABLE()
177};
178
179// ----------------------------------------------------------------------------
180// event tables and other macros for wxWidgets
181// ----------------------------------------------------------------------------
182
183BEGIN_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)
188END_EVENT_TABLE()
189
190IMPLEMENT_APP(MyApp)
191
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));
205 frame->Show(true);
206 return true;
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
223// event handlers
224void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
225{
226 // true is to force the frame to close
227 Close(true);
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
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"?>
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">
264 <label>_File</label>
265 <style>wxMENU_TEAROFF</style>
266 <object class="wxMenuItem" name="menu_about">
267 <label>_About...</label>
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">
279 <label>E_xit\tAlt-X</label>
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
417Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWidgets
418distribution.
419
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
427programmer from dealing with most of the XRC specifics (e.g. {\tt XRCCTRL}).
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),
483though you must still use {\tt XRCID} to refer to widget IDs in the event
484table.
485
486Example:
487
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
510\subsection{Adding new resource handlers}\label{newresourcehandlers}
511
512Coming soon.
513