]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/txrc.tex
Largely successful attempts to get better spacing
[wxWidgets.git] / docs / latex / wx / txrc.tex
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
6 Classes: \helpref{wxXmlResource}{wxxmlresource}, \helpref{wxXmlResourceHandler}{wxxmlresourcehandler}
7
8 {\bf IMPORTANT NOTE:} XRC is not yet a part of the core wxWindows library, so
9 please see the next section for how to compile and link it. Otherwise if you
10 try to use it, you will get link errors.
11
12 The XML-based resource system, known as XRC, allows user interface elements such as
13 dialogs, menu bars and toolbars, to be stored in text files and loaded into
14 the application at run-time. XRC files can also be compiled into binary XRS files or C++
15 code (the former makes it possible to store all resources in since file and the latter
16 is useful when you want to embed the resources into the executable).
17
18 There 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
22 resources change.
23 \item If you use a dialog designers that generates C++ code, it can be hard
24 to reintegrate this into existing C++ code. Separation of resources and code
25 is 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
28 and highly portable.
29 \item The XRC format is a wxWindows standard,
30 and can be generated or postprocessed by any program that understands it. As it is based
31 on the XML standard, existing XML editors can be used for simple editing purposes.
32 \end{itemize}
33
34 XRC was written by Vaclav Slavik.
35
36 \subsection{Compiling and using XRC}\label{compilingxrc}
37
38 XRC 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
48 To compile XRC:
49
50 \begin{itemize}\itemsep=0pt
51 \item Under Windows using VC++, open the contrib/src/xrc/XrcVC.dsw project
52 and compile. Also compile contrib/utils/wxrc using wxBase if you wish to compile
53 resource files.
54 \item Under Unix, XRC should be configured when you configured
55 wxWindows. Make XRC by changing directory to contrib/src/xrc and
56 type 'make'. Similarly compile contrib/utils/wxrc using wxBase if you wish to compile
57 resource files. {\bf Note:} there is currently a
58 problem with the wxWindows build system that means that
59 only the static version of library can be built at present.
60 \end{itemize}
61
62 To 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
71 These 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;
75 \item If you are going to use \helpref{XRS files}{binaryresourcefiles}, install
76 wxFileSystem ZIP handler first with {\tt wxFileSystem::AddHandler(new wxZipFSHandler);}
77 \item call {\tt wxXmlResource::Get()->InitAllHandlers()} from your wxApp::OnInit function,
78 and then call {\tt wxXmlResource::Get()->Load("myfile.xrc")} to load the resource file;
79 \item to create a dialog from a resource, create it using the default constructor, and then
80 load using for example {\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");}
81 \item set up event tables as usual but use the {\tt XRCID(str)} macro to translate from XRC string names
82 to a suitable integer identifier, for example {\tt EVT\_MENU(XRCID("quit"), MyFrame::OnQuit)}.
83 \end{itemize}
84
85 To create an XRC file, you can use one of the following methods.
86
87 \begin{itemize}\itemsep=0pt
88 \item Create the file by hand;
89 \item use \urlref{wxDesigner}{http://www.roebling.de}, a commercial dialog designer/RAD tool;
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
92 dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWindows
93 CVS archive;
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;
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}.
97 \end{itemize}
98
99 A 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}.
100
101 It is highly recommended that you use a resource editing tool, since it's fiddly writing
102 XRC files by hand.
103
104 You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways.
105 You can pass an XRC file (XML-based text resource file)
106 or a \helpref{zip-compressed file}{binaryresourcefiles} (extension ZIP or XRS) containing other XRC.
107
108 You can also use \helpref{embedded C++ resources}{embeddedresource}
109
110 \subsection{Using binary resource files}\label{binaryresourcefiles}
111
112 To 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:
114 \begin{itemize}\itemsep=0pt
115 \item -h (--help): show a help message
116 \item -v (--verbose): show verbose logging information
117 \item -c (--cpp-code): write C++ source rather than a XRS file
118 \item -e (--extra-cpp-code): if used together with -c, generates C++ header file
119 containing class definitions for the windows defined by the XRC file (see special subsection)
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
127 For example:
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
134 \wxheading{Note}
135
136 XRS file is esentially a renamed ZIP archive which means that you can manipulate
137 it with standard ZIP tools. Note that if you are using XRS files, you have
138 to initialize \helpref{wxFileSystem}{wxfilesystem} ZIP handler first! It is a simple
139 thing 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
149 It is sometimes useful to embed resources in the executable itself instead
150 of loading external file (e.g. when your app is small and consists only of one
151 exe file). XRC provides means to convert resources into regular C++ file that
152 can be compiled and included in the executable.
153
154 Use the {\tt -c} switch to
155 {\tt wxrc} utility to produce C++ file with embedded resources. This file will
156 contain a function called {\it InitXmlResource} (unless you override this with
157 a command line switch). Use it to load the resource:
158 \begin{verbatim}
159 extern void InitXMLResource(); // defined in generated file
160 ...
161 wxXmlResource::Get()->InitAllHandlers();
162 InitXmlResource();
163 ...
164 \end{verbatim}
165
166 \subsection{XRC C++ sample}\label{xrccppsample}
167
168 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
169
170 \begin{verbatim}
171 #include "wx/wx.h"
172 #include "wx/image.h"
173 #include "wx/xrc/xmlres.h"
174
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
185 class MyApp : public wxApp
186 {
187 public:
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
198 class MyFrame : public wxFrame
199 {
200 public:
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
210 private:
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
219 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
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)
224 END_EVENT_TABLE()
225
226 IMPLEMENT_APP(MyApp)
227
228 // ----------------------------------------------------------------------------
229 // the application class
230 // ----------------------------------------------------------------------------
231
232 // 'Main program' equivalent: the program execution "starts" here
233 bool 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));
241 frame->Show(true);
242 return true;
243 }
244
245 // ----------------------------------------------------------------------------
246 // main frame
247 // ----------------------------------------------------------------------------
248
249 // frame constructor
250 MyFrame::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
259 // event handlers
260 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
261 {
262 // true is to force the frame to close
263 Close(true);
264 }
265
266 void 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
275 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
276 {
277 wxDialog dlg;
278 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
279 dlg.ShowModal();
280 }
281
282 void 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
292 This is the XML file (resource.xrc) for the XRC sample.
293
294 \begin{verbatim}
295 <?xml version="1.0"?>
296 <resource version="2.3.0.1">
297 <object class="wxMenuBar" name="mainmenu">
298 <style>wxMB_DOCKABLE</style>
299 <object class="wxMenu" name="menu_file">
300 <label>_File</label>
301 <style>wxMENU_TEAROFF</style>
302 <object class="wxMenuItem" name="menu_about">
303 <label>_About...</label>
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">
315 <label>E_xit\tAlt-X</label>
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
453 Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWindows
454 distribution.
455
456 \subsection{C++ header file generation}\label{xrccppheader}
457
458 Using the {\tt -e} switch together with {\tt -c}, a C++ header file is written
459 containing class definitions for the GUI windows defined in the XRC file.
460 This code generation can make it easier to use XRC and automate program
461 development.
462 The classes can be used as basis for development, freeing the
463 programmer from dealing with most of the XRC specifities (e.g. {\tt XRCCTRL}).
464
465 For each top level window defined in the XRC file a C++ class definition is
466 generated, containing as class members the named widgets of the window.
467 A default constructor for each class is also generated. Inside the constructor
468 all XRC loading is done and all class members representing widgets are initialized.
469
470 A simple example will help understand how the scheme works. Suppose you have
471 a XRC file defining a top level window {\tt TestWnd\_Base}, which subclasses {\tt wxFrame} (any
472 other class like {\tt wxDialog} will do also), and has subwidgets {\tt wxTextCtrl} A and {\tt wxButton} B.
473 The 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
498 class TestWnd_Base : public wxFrame {
499 protected:
500 wxTextCtrl* A;
501 wxButton* B;
502
503 private:
504 void InitWidgetsFromXRC(){
505 wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
506 A = XRCCTRL(*this,"A",wxTextCtrl);
507 B = XRCCTRL(*this,"B",wxButton);
508 }
509 public:
510 TestWnd::TestWnd(){
511 InitWidgetsFromXRC();
512 }
513 };
514 \end{verbatim}
515
516 The generated window class can be used as basis for the full window class. The
517 class 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),
519 though you must still use {\tt XRCID} to refer to widget ids in the event
520 table.
521
522 Example:
523 \begin{verbatim}
524 #include "resource.h"
525
526 class 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
539 BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
540 EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
541 END_EVENT_TABLE()
542
543 \end{verbatim}
544
545 \subsection{Adding new resource handlers}\label{newresourcehandlers}
546
547 Coming soon.
548