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