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