]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/txrc.tex
added precisions for GetSize and GetClientSize
[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, so an XML parser does not need to be linked with the application and load times
13 are faster.
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 call \verb$wxXmlResource::Get()->InitAllHandlers()$ from your wxApp::OnInit function,
73 and then call \verb$wxXmlResource::Get()->Load("myfile.xrc")$ to load the resource file;
74 \item to create a dialog from a resource, create it using the default constructor, and then
75 load using for example \verb$wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1")$;
76 \item set up event tables as usual but use the \verb$XRCID(str)$ macro to translate from XRC string names
77 to a suitable integer identifier, for example \verb$EVT_MENU(XRCID("quit"), MyFrame::OnQuit)$.
78 \end{itemize}
79
80 To create an XRC file, use one of the following methods.
81
82 \begin{itemize}\itemsep=0
83 \item Create the file by hand;
84 \item use \urlref{wxDesigner}{http://www.roebling.de}, a commercial dialog designer/RAD tool;
85 \item use \urlref{XRCed}{http://www.mema.ucl.ac.be/~rolinsky/xrced/}, a wxPython-based
86 dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWindows
87 CVS archive;
88 \item use \urlref{wxWorkshop}{http://wxworkshop.sourceforge.net} (under development);
89 \item use wxrcedit ({\tt utils/contrib/wxrcedit}) (under development);
90 \item convert WIN32 RC files to XRC with the tool in {\tt contrib/utils/convertrc}.
91 \end{itemize}
92
93 It is highly recommended that you use a tool such as wxDesigner, since it's fiddly writing
94 XRC files by hand.
95
96 You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways.
97 You can pass an XRC file (XML-based text resource file), an XMB file (compiled binary file)
98 or a zip-compressed file (extension ZIP or RSC) containing other XRC or XMB files.
99
100 TODO: is the compiled binary format XMB or XRS? How do you handle a C++ resource file?
101
102 \subsection{Using binary resource files}\label{binaryresourcefiles}
103
104 To compile binary resource files, use the command-line wxrc utility. It takes a single file parameter (the
105 input XRC file) and the following switches and options.
106
107 \begin{itemize}\itemsep=0
108 \item -h (--help): show a help message
109 \item -v (--verbose): show verbose logging information
110 \item -c (--cpp-code): write C++ source rather than a RSC file
111 \item -u (--uncompressed): do not compress XML files (C++ only)
112 \item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
113 \item -n (--function) <name>: specify C++ function name (use with -c)
114 \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
115 \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
116 \end{itemize}
117
118 For example:
119
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 \subsection{XRC C++ sample}\label{xrccppsample}
127
128 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
129
130 \begin{verbatim}
131 /////////////////////////////////////////////////////////////////////////////
132 // Name: xmldemo.cpp
133 // Purpose: XML resources sample
134 // Author: Vaclav Slavik
135 // RCS-ID: $Id$
136 // Copyright: (c) Vaclav Slavik
137 // Licence: wxWindows licence
138 /////////////////////////////////////////////////////////////////////////////
139
140 // ============================================================================
141 // declarations
142 // ============================================================================
143
144 // ----------------------------------------------------------------------------
145 // headers
146 // ----------------------------------------------------------------------------
147 #ifdef __GNUG__
148 #pragma implementation "xrcdemo.cpp"
149 #pragma interface "xrcdemo.cpp"
150 #endif
151
152 // For compilers that support precompilation, includes "wx/wx.h".
153 #include "wx/wxprec.h"
154
155 #ifdef __BORLANDC__
156 #pragma hdrstop
157 #endif
158
159 // for all others, include the necessary headers (this file is usually all you
160 // need because it includes almost all "standard" wxWindows headers)
161 #ifndef WX_PRECOMP
162 #include "wx/wx.h"
163 #endif
164
165 #include "wx/image.h"
166 #include "wx/xrc/xmlres.h"
167
168 // ----------------------------------------------------------------------------
169 // resources
170 // ----------------------------------------------------------------------------
171 // the application icon
172 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
173 #include "rc/appicon.xpm"
174 #endif
175
176 // ----------------------------------------------------------------------------
177 // private classes
178 // ----------------------------------------------------------------------------
179
180 // Define a new application type, each program should derive a class from wxApp
181 class MyApp : public wxApp
182 {
183 public:
184 // override base class virtuals
185 // ----------------------------
186
187 // this one is called on application startup and is a good place for the app
188 // initialization (doing it here and not in the ctor allows to have an error
189 // return: if OnInit() returns false, the application terminates)
190 virtual bool OnInit();
191 };
192
193 // Define a new frame type: this is going to be our main frame
194 class MyFrame : public wxFrame
195 {
196 public:
197 // ctor(s)
198 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
199
200 // event handlers (these functions should _not_ be virtual)
201 void OnQuit(wxCommandEvent& event);
202 void OnAbout(wxCommandEvent& event);
203 void OnDlg1(wxCommandEvent& event);
204 void OnDlg2(wxCommandEvent& event);
205
206 private:
207 // any class wishing to process wxWindows events must use this macro
208 DECLARE_EVENT_TABLE()
209 };
210
211 // ----------------------------------------------------------------------------
212 // event tables and other macros for wxWindows
213 // ----------------------------------------------------------------------------
214
215 // the event tables connect the wxWindows events with the functions (event
216 // handlers) which process them. It can be also done at run-time, but for the
217 // simple menu events like this the static method is much simpler.
218 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
219 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
220 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
221 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
222 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
223 END_EVENT_TABLE()
224
225 // Create a new application object: this macro will allow wxWindows to create
226 // the application object during program execution (it's better than using a
227 // static object for many reasons) and also declares the accessor function
228 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
229 // not wxApp)
230 IMPLEMENT_APP(MyApp)
231
232 // ============================================================================
233 // implementation
234 // ============================================================================
235
236 // ----------------------------------------------------------------------------
237 // the application class
238 // ----------------------------------------------------------------------------
239
240 // 'Main program' equivalent: the program execution "starts" here
241 bool MyApp::OnInit()
242 {
243 wxImage::AddHandler(new wxGIFHandler);
244 wxXmlResource::Get()->InitAllHandlers();
245 wxXmlResource::Get()->Load("rc/resource.xrc");
246
247 MyFrame *frame = new MyFrame("XML resources demo",
248 wxPoint(50, 50), wxSize(450, 340));
249 frame->Show(TRUE);
250 return TRUE;
251 }
252
253 // ----------------------------------------------------------------------------
254 // main frame
255 // ----------------------------------------------------------------------------
256
257 // frame constructor
258 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
259 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
260 {
261 SetIcon(wxICON(appicon));
262
263 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
264 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
265 }
266
267
268 // event handlers
269
270 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
271 {
272 // TRUE is to force the frame to close
273 Close(TRUE);
274 }
275
276 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
277 {
278 wxString msg;
279 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
280 _T("Welcome to %s"), wxVERSION_STRING);
281
282 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
283 }
284
285 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
286 {
287 wxDialog dlg;
288 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
289 dlg.ShowModal();
290 }
291
292
293 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
294 {
295 wxDialog dlg;
296 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
297 dlg.ShowModal();
298 }
299 \end{verbatim}
300
301 \subsection{XRC resource file sample}\label{xrcsample}
302
303 This is the XML file (resource.xrc) for the XRC sample.
304
305 \begin{verbatim}
306 <?xml version="1.0"?>
307 <resource>
308 <object class="wxMenuBar" name="mainmenu">
309 <style>wxMB_DOCKABLE</style>
310 <object class="wxMenu" name="menu_file">
311 <label>$File</label>
312 <style>wxMENU_TEAROFF</style>
313 <object class="wxMenuItem" name="menu_about">
314 <label>$About...</label>
315 <bitmap>filesave.gif</bitmap>
316 </object>
317 <object class="separator"/>
318 <object class="wxMenuItem" name="menu_dlg1">
319 <label>Dialog 1</label>
320 </object>
321 <object class="wxMenuItem" name="menu_dlg2">
322 <label>Dialog 2</label>
323 </object>
324 <object class="separator"/>
325 <object class="wxMenuItem" name="menu_quit">
326 <label>E$xit\tAlt-X</label>
327 </object>
328 </object>
329 </object>
330 <object class="wxToolBar" name="toolbar">
331 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
332 <margins>2,2</margins>
333 <object class="tool" name="menu_open">
334 <bitmap>fileopen.gif</bitmap>
335 <tooltip>Open catalog</tooltip>
336 </object>
337 <object class="tool" name="menu_save">
338 <bitmap>filesave.gif</bitmap>
339 <tooltip>Save catalog</tooltip>
340 </object>
341 <object class="tool" name="menu_update">
342 <bitmap>update.gif</bitmap>
343 <tooltip>Update catalog - synchronize it with sources</tooltip>
344 </object>
345 <separator/>
346 <object class="tool" name="menu_quotes">
347 <bitmap>quotes.gif</bitmap>
348 <toggle>1</toggle>
349 <tooltip>Display quotes around the string?</tooltip>
350 </object>
351 <object class="separator"/>
352 <object class="tool" name="menu_fuzzy">
353 <bitmap>fuzzy.gif</bitmap>
354 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
355 <toggle>1</toggle>
356 </object>
357 </object>
358 <object class="wxDialog" name="dlg1">
359 <object class="wxBoxSizer">
360 <object class="sizeritem">
361 <object class="wxBitmapButton">
362 <bitmap>fuzzy.gif</bitmap>
363 <focus>fileopen.gif</focus>
364 </object>
365 </object>
366 <object class="sizeritem">
367 <object class="wxPanel">
368 <object class="wxStaticText">
369 <label>fdgdfgdfgdfg</label>
370 </object>
371 <style>wxSUNKEN_BORDER</style>
372 </object>
373 <flag>wxALIGN_CENTER</flag>
374 </object>
375 <object class="sizeritem">
376 <object class="wxButton">
377 <label>Buttonek</label>
378 </object>
379 <border>10d</border>
380 <flag>wxALL</flag>
381 </object>
382 <object class="sizeritem">
383 <object class="wxHtmlWindow">
384 <htmlcode>&lt;h1&gt;Hi,&lt;/h1&gt;man</htmlcode>
385 <size>100,45d</size>
386 </object>
387 </object>
388 <object class="sizeritem">
389 <object class="wxNotebook">
390 <object class="notebookpage">
391 <object class="wxPanel">
392 <object class="wxBoxSizer">
393 <object class="sizeritem">
394 <object class="wxHtmlWindow">
395 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
396 <size>50,50d</size>
397 </object>
398 <option>1</option>
399 </object>
400 </object>
401 </object>
402 <label>Page</label>
403 </object>
404 <object class="notebookpage">
405 <object class="wxPanel">
406 <object class="wxBoxSizer">
407 <object class="sizeritem">
408 <object class="wxHtmlWindow">
409 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
410 <size>50,50d</size>
411 </object>
412 </object>
413 </object>
414 </object>
415 <label>Page 2</label>
416 </object>
417 <usenotebooksizer>1</usenotebooksizer>
418 </object>
419 <flag>wxEXPAND</flag>
420 </object>
421 <orient>wxVERTICAL</orient>
422 </object>
423 </object>
424 <object class="wxDialog" name="dlg2">
425 <object class="wxBoxSizer">
426 <orient>wxVERTICAL</orient>
427 <object class="sizeritem" name="dfgdfg">
428 <object class="wxTextCtrl">
429 <size>200,200d</size>
430 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
431 <value>Hello, this is an ordinary multiline\n textctrl....</value>
432 </object>
433 <option>1</option>
434 <flag>wxEXPAND|wxALL</flag>
435 <border>10</border>
436 </object>
437 <object class="sizeritem">
438 <object class="wxBoxSizer">
439 <object class="sizeritem">
440 <object class="wxButton" name="wxID_OK">
441 <label>Ok</label>
442 <default>1</default>
443 </object>
444 </object>
445 <object class="sizeritem">
446 <object class="wxButton" name="wxID_CANCEL">
447 <label>Cancel</label>
448 </object>
449 <border>10</border>
450 <flag>wxLEFT</flag>
451 </object>
452 </object>
453 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
454 <border>10</border>
455 </object>
456 </object>
457 <title>Second testing dialog</title>
458 </object>
459 </resource>
460 \end{verbatim}
461
462 \subsection{XRC file format}\label{xrcfileformat}
463
464 Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWindows
465 distribution.
466
467 \subsection{Adding new resource handlers}\label{newresourcehandlers}
468
469 Coming soon.
470