]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/txrc.tex
Added missing 'break' which caused spurious </FONT></TD> markup before
[wxWidgets.git] / docs / latex / wx / txrc.tex
CommitLineData
d958c9bd
JS
1\section{XML-based resource system overview}\label{xrcoverview}
2
3Classes: \helpref{wxXmlResource}{wxxmlresource}, \helpref{wxXmlResourceHandler}{wxxmlresourcehandler}
4
5The XML-based resource system, known as XRC, allows user interface elements such as
6dialogs, menu bars and toolbars, to be stored in text files and loaded into
7the application at run-time. XRC files can also be compiled into binary XRS files or C++
8code, so an XML parser does not need to be linked with the application and load times
9are faster.
10
11There are several advantages to using XRC resources.
12
13\begin{itemize}\itemsep=0pt
14\item Recompiling and linking an application is not necessary if the
15resources change.
16\item If you use a dialog designers that generates C++ code, it can be hard
17to reintegrate this into existing C++ code. Separation of resources and code
18is a more elegant solution.
19\item You can choose between different alternative resource files at run time, if necessary.
20\item The XRC format uses sizers for flexibility, allowing dialogs to be resizable
21and highly portable.
22\item The XRC format is a wxWindows standard,
23and can be generated or postprocessed by any program that understands it. As it is based
24on the XML standard, existing XML editors can be used for simple editing purposes.
25\end{itemize}
26
27XRC was written by Vaclav Slavik.
28
29\subsection{Compiling and using XRC}\label{compilingxrc}
30
31XRC can be found under the 'contrib' hierarchy, in the following directories:
32
33\begin{verbatim}
34 contrib/src/xrc ; XRC source
35 contrib/include/wx/xrc ; XRC headers
36 contrib/samples/xrc ; XRC sample
37 contrib/utils/wxrc ; XRC resource compiler
38 contrib/utils/wxrcedit ; XRC editor (in progress)
39\end{verbatim}
40
41To compile XRC:
42
43\begin{itemize}\itemsep=0pt
44\item Under Windows using VC++, open the contrib/src/xrc/XrcVC.dsw project
45and compile. Also compile contrib/utils/wxrc using wxBase if you wish to compile
46resource files.
47\item Under Unix, XRC should be configured when you configured
48wxWindows. Make XRC by changing directory to contrib/src/xrc and
49type 'make'. Similarly compile contrib/utils/wxrc using wxBase if you wish to compile
50resource files. {\bf Note:} there is currently a
51problem with the wxWindows build system that means that
52only the static version of library can be built at present.
53\end{itemize}
54
55To use XRC:
56
57\begin{itemize}\itemsep=0pt
58\item Under Windows using VC++, link with wxxrc[d].lib.
59\item Under Unix, link with libwxxrc[d].a.
60\end{itemize}
61
62\subsection{XRC concepts}\label{xrcconcepts}
63
64These are the typical steps for using XRC files in your application.
65
66\begin{itemize}\itemsep=0pt
67\item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
68\item call \verb$wxXmlResource::Get()->InitAllHandlers()$ from your wxApp::OnInit function,
69and then call \verb$wxXmlResource::Get()->Load("myfile.xrc")$ to load the resource file;
70\item to create a dialog from a resource, create it using the default constructor, and then
71load using for example \verb$wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1")$;
72\item set up event tables as usual but use the \verb$XMLID(str)$ macro to translate from XRC string names
73to a suitable integer identifier, for example \verb$EVT_MENU(XMLID("quit"), MyFrame::OnQuit)$.
74\end{itemize}
75
76To create an XRC file, use one of the following methods.
77
78\begin{itemize}\itemsep=0
79\item Create the file by hand;
80\item use \urlref{wxDesigner}{http://www.roebling.de};
81\item use wxWorkshop (not yet available);
82\item use wxrcedit (the status of this tool is unknown);
83\item convert WIN32 RC files with the tool in contrib/utils/convertrc (the status of this tool is
84unknown).
85\end{itemize}
86
87It is highly recommended that you use a tool such as wxDesigner, since it's fiddly writing
88XRC files by hand.
89
90You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways.
91You can pass an XRC file (XML-based text resource file), an XMB file (compiled binary file)
92or a zip-compressed file (extension ZIP or RSC) containing other XRC or XMB files.
93
94TODO: is the compiled binary format XMB or XRS? How do you handle a C++ resource file?
95
96\subsection{Using binary resource files}\label{binaryresourcefiles}
97
98To compile binary resource files, use the command-line wxrc utility. It takes a single file parameter (the
99input XRC file) and the following switches and options.
100
101\begin{itemize}\itemsep=0
102\item -h (--help): show a help message
103\item -v (--verbose): show verbose logging information
104\item -c (--cpp-code): write C++ source rather than a RSC file
105\item -u (--uncompressed): do not compress XML files (C++ only)
106\item -g (--gettext): output .po catalog (to stdout, or a file if -o is used)
107\item -n (--function) <name>: specify C++ function name (use with -c)
108\item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp
109\item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file
110\end{itemize}
111
112For example:
113
114\begin{verbatim}
115 % wxrc resource.wrc
116 % wxrc resource.wrc -o resource.wrs
117 % wxrc resource.wrc -v -c -o resource.cpp
118\end{verbatim}
119
120\subsection{XRC C++ sample}\label{xrccppsample}
121
122This is the C++ source file (xrcdemo.cpp) for the XRC sample.
123
124\begin{verbatim}
125/////////////////////////////////////////////////////////////////////////////
126// Name: xmldemo.cpp
127// Purpose: XML resources sample
128// Author: Vaclav Slavik
129// RCS-ID: $Id$
130// Copyright: (c) Vaclav Slavik
131// Licence: wxWindows licence
132/////////////////////////////////////////////////////////////////////////////
133
134// ============================================================================
135// declarations
136// ============================================================================
137
138// ----------------------------------------------------------------------------
139// headers
140// ----------------------------------------------------------------------------
141#ifdef __GNUG__
142 #pragma implementation "xrcdemo.cpp"
143 #pragma interface "xrcdemo.cpp"
144#endif
145
146// For compilers that support precompilation, includes "wx/wx.h".
147#include "wx/wxprec.h"
148
149#ifdef __BORLANDC__
150 #pragma hdrstop
151#endif
152
153// for all others, include the necessary headers (this file is usually all you
154// need because it includes almost all "standard" wxWindows headers)
155#ifndef WX_PRECOMP
156 #include "wx/wx.h"
157#endif
158
159#include "wx/image.h"
160#include "wx/xrc/xmlres.h"
161
162// ----------------------------------------------------------------------------
163// resources
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
175class MyApp : public wxApp
176{
177public:
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
188class MyFrame : public wxFrame
189{
190public:
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
200private:
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// the event tables connect the wxWindows events with the functions (event
210// handlers) which process them. It can be also done at run-time, but for the
211// simple menu events like this the static method is much simpler.
212BEGIN_EVENT_TABLE(MyFrame, wxFrame)
213 EVT_MENU(XMLID("menu_quit"), MyFrame::OnQuit)
214 EVT_MENU(XMLID("menu_about"), MyFrame::OnAbout)
215 EVT_MENU(XMLID("menu_dlg1"), MyFrame::OnDlg1)
216 EVT_MENU(XMLID("menu_dlg2"), MyFrame::OnDlg2)
217END_EVENT_TABLE()
218
219// Create a new application object: this macro will allow wxWindows to create
220// the application object during program execution (it's better than using a
221// static object for many reasons) and also declares the accessor function
222// wxGetApp() which will return the reference of the right type (i.e. MyApp and
223// not wxApp)
224IMPLEMENT_APP(MyApp)
225
226// ============================================================================
227// implementation
228// ============================================================================
229
230// ----------------------------------------------------------------------------
231// the application class
232// ----------------------------------------------------------------------------
233
234// 'Main program' equivalent: the program execution "starts" here
235bool MyApp::OnInit()
236{
237 wxImage::AddHandler(new wxGIFHandler);
238 wxXmlResource::Get()->InitAllHandlers();
239 wxXmlResource::Get()->Load("rc/resource.xrc");
240
241 MyFrame *frame = new MyFrame("XML resources demo",
242 wxPoint(50, 50), wxSize(450, 340));
243 frame->Show(TRUE);
244 return TRUE;
245}
246
247// ----------------------------------------------------------------------------
248// main frame
249// ----------------------------------------------------------------------------
250
251// frame constructor
252MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
253 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
254{
255 SetIcon(wxICON(appicon));
256
257 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
258 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
259}
260
261
262// event handlers
263
264void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
265{
266 // TRUE is to force the frame to close
267 Close(TRUE);
268}
269
270void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
271{
272 wxString msg;
273 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
274 _T("Welcome to %s"), wxVERSION_STRING);
275
276 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
277}
278
279void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
280{
281 wxDialog dlg;
282 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
283 dlg.ShowModal();
284}
285
286
287void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
288{
289 wxDialog dlg;
290 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
291 dlg.ShowModal();
292}
293\end{verbatim}
294
295\subsection{XRC resource file sample}\label{xrcsample}
296
297This is the XML file (resource.xrc) for the XRC sample.
298
299\begin{verbatim}
300<?xml version="1.0"?>
301<resource>
302 <object class="wxMenuBar" name="mainmenu">
303 <style>wxMB_DOCKABLE</style>
304 <object class="wxMenu" name="menu_file">
305 <label>$File</label>
306 <style>wxMENU_TEAROFF</style>
307 <object class="wxMenuItem" name="menu_about">
308 <label>$About...</label>
309 <bitmap>filesave.gif</bitmap>
310 </object>
311 <object class="separator"/>
312 <object class="wxMenuItem" name="menu_dlg1">
313 <label>Dialog 1</label>
314 </object>
315 <object class="wxMenuItem" name="menu_dlg2">
316 <label>Dialog 2</label>
317 </object>
318 <object class="separator"/>
319 <object class="wxMenuItem" name="menu_quit">
320 <label>E$xit\tAlt-X</label>
321 </object>
322 </object>
323 </object>
324 <object class="wxToolBar" name="toolbar">
325 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
326 <margins>2,2</margins>
327 <object class="tool" name="menu_open">
328 <bitmap>fileopen.gif</bitmap>
329 <tooltip>Open catalog</tooltip>
330 </object>
331 <object class="tool" name="menu_save">
332 <bitmap>filesave.gif</bitmap>
333 <tooltip>Save catalog</tooltip>
334 </object>
335 <object class="tool" name="menu_update">
336 <bitmap>update.gif</bitmap>
337 <tooltip>Update catalog - synchronize it with sources</tooltip>
338 </object>
339 <separator/>
340 <object class="tool" name="menu_quotes">
341 <bitmap>quotes.gif</bitmap>
342 <toggle>1</toggle>
343 <tooltip>Display quotes around the string?</tooltip>
344 </object>
345 <object class="separator"/>
346 <object class="tool" name="menu_fuzzy">
347 <bitmap>fuzzy.gif</bitmap>
348 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
349 <toggle>1</toggle>
350 </object>
351 </object>
352 <object class="wxDialog" name="dlg1">
353 <object class="wxBoxSizer">
354 <object class="sizeritem">
355 <object class="wxBitmapButton">
356 <bitmap>fuzzy.gif</bitmap>
357 <focus>fileopen.gif</focus>
358 </object>
359 </object>
360 <object class="sizeritem">
361 <object class="wxPanel">
362 <object class="wxStaticText">
363 <label>fdgdfgdfgdfg</label>
364 </object>
365 <style>wxSUNKEN_BORDER</style>
366 </object>
367 <flag>wxALIGN_CENTER</flag>
368 </object>
369 <object class="sizeritem">
370 <object class="wxButton">
371 <label>Buttonek</label>
372 </object>
373 <border>10d</border>
374 <flag>wxALL</flag>
375 </object>
376 <object class="sizeritem">
377 <object class="wxHtmlWindow">
378 <htmlcode>&lt;h1&gt;Hi,&lt;/h1&gt;man</htmlcode>
379 <size>100,45d</size>
380 </object>
381 </object>
382 <object class="sizeritem">
383 <object class="wxNotebook">
384 <object class="notebookpage">
385 <object class="wxPanel">
386 <object class="wxBoxSizer">
387 <object class="sizeritem">
388 <object class="wxHtmlWindow">
389 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
390 <size>50,50d</size>
391 </object>
392 <option>1</option>
393 </object>
394 </object>
395 </object>
396 <label>Page</label>
397 </object>
398 <object class="notebookpage">
399 <object class="wxPanel">
400 <object class="wxBoxSizer">
401 <object class="sizeritem">
402 <object class="wxHtmlWindow">
403 <htmlcode>Hello, we are inside a &lt;u&gt;NOTEBOOK&lt;/u&gt;...</htmlcode>
404 <size>50,50d</size>
405 </object>
406 </object>
407 </object>
408 </object>
409 <label>Page 2</label>
410 </object>
411 <usenotebooksizer>1</usenotebooksizer>
412 </object>
413 <flag>wxEXPAND</flag>
414 </object>
415 <orient>wxVERTICAL</orient>
416 </object>
417 </object>
418 <object class="wxDialog" name="dlg2">
419 <object class="wxBoxSizer">
420 <orient>wxVERTICAL</orient>
421 <object class="sizeritem" name="dfgdfg">
422 <object class="wxTextCtrl">
423 <size>200,200d</size>
424 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
425 <value>Hello, this is an ordinary multiline\n textctrl....</value>
426 </object>
427 <option>1</option>
428 <flag>wxEXPAND|wxALL</flag>
429 <border>10</border>
430 </object>
431 <object class="sizeritem">
432 <object class="wxBoxSizer">
433 <object class="sizeritem">
434 <object class="wxButton" name="wxID_OK">
435 <label>Ok</label>
436 <default>1</default>
437 </object>
438 </object>
439 <object class="sizeritem">
440 <object class="wxButton" name="wxID_CANCEL">
441 <label>Cancel</label>
442 </object>
443 <border>10</border>
444 <flag>wxLEFT</flag>
445 </object>
446 </object>
447 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
448 <border>10</border>
449 </object>
450 </object>
451 <title>Second testing dialog</title>
452 </object>
453</resource>
454\end{verbatim}
455
456\subsection{XRC file format}\label{xrcfileformat}
457
458This section to be written.
459
460\subsection{Adding new resource handlers}\label{newresourcehandlers}
461
462This section to be written.
463
464