]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/txrc.tex
tried to make wxCloseEvent docs more clear too
[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 wxXmlResource::Get()->InitAllHandlers();
152 InitXmlResource();
153 ...
154 \end{verbatim}
155
156 \subsection{XRC C++ sample}\label{xrccppsample}
157
158 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
159
160 \begin{verbatim}
161 #include "wx/wx.h"
162 #include "wx/image.h"
163 #include "wx/xrc/xmlres.h"
164
165 // the application icon
166 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
167 #include "rc/appicon.xpm"
168 #endif
169
170 // ----------------------------------------------------------------------------
171 // private classes
172 // ----------------------------------------------------------------------------
173
174 // Define a new application type, each program should derive a class from wxApp
175 class MyApp : public wxApp
176 {
177 public:
178 // override base class virtuals
179 // ----------------------------
180
181 // this one is called on application startup and is a good place for the app
182 // initialization (doing it here and not in the ctor allows to have an error
183 // return: if OnInit() returns false, the application terminates)
184 virtual bool OnInit();
185 };
186
187 // Define a new frame type: this is going to be our main frame
188 class MyFrame : public wxFrame
189 {
190 public:
191 // ctor(s)
192 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
193
194 // event handlers (these functions should _not_ be virtual)
195 void OnQuit(wxCommandEvent& event);
196 void OnAbout(wxCommandEvent& event);
197 void OnDlg1(wxCommandEvent& event);
198 void OnDlg2(wxCommandEvent& event);
199
200 private:
201 // any class wishing to process wxWindows events must use this macro
202 DECLARE_EVENT_TABLE()
203 };
204
205 // ----------------------------------------------------------------------------
206 // event tables and other macros for wxWindows
207 // ----------------------------------------------------------------------------
208
209 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
210 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
211 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
212 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
213 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
214 END_EVENT_TABLE()
215
216 IMPLEMENT_APP(MyApp)
217
218 // ----------------------------------------------------------------------------
219 // the application class
220 // ----------------------------------------------------------------------------
221
222 // 'Main program' equivalent: the program execution "starts" here
223 bool MyApp::OnInit()
224 {
225 wxImage::AddHandler(new wxGIFHandler);
226 wxXmlResource::Get()->InitAllHandlers();
227 wxXmlResource::Get()->Load("rc/resource.xrc");
228
229 MyFrame *frame = new MyFrame("XML resources demo",
230 wxPoint(50, 50), wxSize(450, 340));
231 frame->Show(TRUE);
232 return TRUE;
233 }
234
235 // ----------------------------------------------------------------------------
236 // main frame
237 // ----------------------------------------------------------------------------
238
239 // frame constructor
240 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
241 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
242 {
243 SetIcon(wxICON(appicon));
244
245 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
246 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
247 }
248
249 // event handlers
250 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
251 {
252 // TRUE is to force the frame to close
253 Close(TRUE);
254 }
255
256 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
257 {
258 wxString msg;
259 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
260 _T("Welcome to %s"), wxVERSION_STRING);
261
262 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
263 }
264
265 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
266 {
267 wxDialog dlg;
268 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
269 dlg.ShowModal();
270 }
271
272 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
273 {
274 wxDialog dlg;
275 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
276 dlg.ShowModal();
277 }
278 \end{verbatim}
279
280 \subsection{XRC resource file sample}\label{xrcsample}
281
282 This is the XML file (resource.xrc) for the XRC sample.
283
284 \begin{verbatim}
285 <?xml version="1.0"?>
286 <resource version="2.3.0.1">
287 <object class="wxMenuBar" name="mainmenu">
288 <style>wxMB_DOCKABLE</style>
289 <object class="wxMenu" name="menu_file">
290 <label>_File</label>
291 <style>wxMENU_TEAROFF</style>
292 <object class="wxMenuItem" name="menu_about">
293 <label>_About...</label>
294 <bitmap>filesave.gif</bitmap>
295 </object>
296 <object class="separator"/>
297 <object class="wxMenuItem" name="menu_dlg1">
298 <label>Dialog 1</label>
299 </object>
300 <object class="wxMenuItem" name="menu_dlg2">
301 <label>Dialog 2</label>
302 </object>
303 <object class="separator"/>
304 <object class="wxMenuItem" name="menu_quit">
305 <label>E_xit\tAlt-X</label>
306 </object>
307 </object>
308 </object>
309 <object class="wxToolBar" name="toolbar">
310 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
311 <margins>2,2</margins>
312 <object class="tool" name="menu_open">
313 <bitmap>fileopen.gif</bitmap>
314 <tooltip>Open catalog</tooltip>
315 </object>
316 <object class="tool" name="menu_save">
317 <bitmap>filesave.gif</bitmap>
318 <tooltip>Save catalog</tooltip>
319 </object>
320 <object class="tool" name="menu_update">
321 <bitmap>update.gif</bitmap>
322 <tooltip>Update catalog - synchronize it with sources</tooltip>
323 </object>
324 <separator/>
325 <object class="tool" name="menu_quotes">
326 <bitmap>quotes.gif</bitmap>
327 <toggle>1</toggle>
328 <tooltip>Display quotes around the string?</tooltip>
329 </object>
330 <object class="separator"/>
331 <object class="tool" name="menu_fuzzy">
332 <bitmap>fuzzy.gif</bitmap>
333 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
334 <toggle>1</toggle>
335 </object>
336 </object>
337 <object class="wxDialog" name="dlg1">
338 <object class="wxBoxSizer">
339 <object class="sizeritem">
340 <object class="wxBitmapButton">
341 <bitmap>fuzzy.gif</bitmap>
342 <focus>fileopen.gif</focus>
343 </object>
344 </object>
345 <object class="sizeritem">
346 <object class="wxPanel">
347 <object class="wxStaticText">
348 <label>fdgdfgdfgdfg</label>
349 </object>
350 <style>wxSUNKEN_BORDER</style>
351 </object>
352 <flag>wxALIGN_CENTER</flag>
353 </object>
354 <object class="sizeritem">
355 <object class="wxButton">
356 <label>Buttonek</label>
357 </object>
358 <border>10d</border>
359 <flag>wxALL</flag>
360 </object>
361 <object class="sizeritem">
362 <object class="wxHtmlWindow">
363 <htmlcode>&lt;h1&gt;Hi,&lt;/h1&gt;man</htmlcode>
364 <size>100,45d</size>
365 </object>
366 </object>
367 <object class="sizeritem">
368 <object class="wxNotebook">
369 <object class="notebookpage">
370 <object class="wxPanel">
371 <object class="wxBoxSizer">
372 <object class="sizeritem">
373 <object class="wxHtmlWindow">
374 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
375 <size>50,50d</size>
376 </object>
377 <option>1</option>
378 </object>
379 </object>
380 </object>
381 <label>Page</label>
382 </object>
383 <object class="notebookpage">
384 <object class="wxPanel">
385 <object class="wxBoxSizer">
386 <object class="sizeritem">
387 <object class="wxHtmlWindow">
388 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
389 <size>50,50d</size>
390 </object>
391 </object>
392 </object>
393 </object>
394 <label>Page 2</label>
395 </object>
396 <usenotebooksizer>1</usenotebooksizer>
397 </object>
398 <flag>wxEXPAND</flag>
399 </object>
400 <orient>wxVERTICAL</orient>
401 </object>
402 </object>
403 <object class="wxDialog" name="dlg2">
404 <object class="wxBoxSizer">
405 <orient>wxVERTICAL</orient>
406 <object class="sizeritem" name="dfgdfg">
407 <object class="wxTextCtrl">
408 <size>200,200d</size>
409 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
410 <value>Hello, this is an ordinary multiline\n textctrl....</value>
411 </object>
412 <option>1</option>
413 <flag>wxEXPAND|wxALL</flag>
414 <border>10</border>
415 </object>
416 <object class="sizeritem">
417 <object class="wxBoxSizer">
418 <object class="sizeritem">
419 <object class="wxButton" name="wxID_OK">
420 <label>Ok</label>
421 <default>1</default>
422 </object>
423 </object>
424 <object class="sizeritem">
425 <object class="wxButton" name="wxID_CANCEL">
426 <label>Cancel</label>
427 </object>
428 <border>10</border>
429 <flag>wxLEFT</flag>
430 </object>
431 </object>
432 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
433 <border>10</border>
434 </object>
435 </object>
436 <title>Second testing dialog</title>
437 </object>
438 </resource>
439 \end{verbatim}
440
441 \subsection{XRC file format}\label{xrcfileformat}
442
443 Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWindows
444 distribution.
445
446 \subsection{Adding new resource handlers}\label{newresourcehandlers}
447
448 Coming soon.
449