1 \section{XML-based resource system overview
}\label{xrcoverview
}
3 Classes:
\helpref{wxXmlResource
}{wxxmlresource
},
\helpref{wxXmlResourceHandler
}{wxxmlresourcehandler
}
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.
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
15 There are several advantages to using XRC resources.
17 \begin{itemize
}\itemsep=
0pt
18 \item Recompiling and linking an application is not necessary if the
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
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.
31 XRC was written by Vaclav Slavik.
33 \subsection{Compiling and using XRC
}\label{compilingxrc
}
35 XRC can be found under the 'contrib' hierarchy, in the following directories:
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)
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
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.
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.
66 \subsection{XRC concepts
}\label{xrcconcepts
}
68 These are the typical steps for using XRC files in your application.
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$XMLID(str)$ macro to translate from XRC string names
77 to a suitable integer identifier, for example
\verb$EVT_MENU(XMLID("quit"), MyFrame::OnQuit)$.
80 To create an XRC file, use one of the following methods.
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
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
}.
93 It is highly recommended that you use a tool such as wxDesigner, since it's fiddly writing
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.
100 TODO: is the compiled binary format XMB or XRS? How do you handle a C++ resource file?
102 \subsection{Using binary resource files
}\label{binaryresourcefiles
}
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.
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
122 % wxrc resource.wrc -o resource.wrs
123 % wxrc resource.wrc -v -c -o resource.cpp
126 \subsection{XRC C++ sample
}\label{xrccppsample
}
128 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
131 /////////////////////////////////////////////////////////////////////////////
133 // Purpose: XML resources sample
134 // Author: Vaclav Slavik
136 // Copyright: (c) Vaclav Slavik
137 // Licence: wxWindows licence
138 /////////////////////////////////////////////////////////////////////////////
140 // ============================================================================
142 // ============================================================================
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 #pragma implementation "xrcdemo.cpp"
149 #pragma interface "xrcdemo.cpp"
152 // For compilers that support precompilation, includes "wx/wx.h".
153 #include "wx/wxprec.h"
159 // for all others, include the necessary headers (this file is usually all you
160 // need because it includes almost all "standard" wxWindows headers)
165 #include "wx/image.h"
166 #include "wx/xrc/xmlres.h"
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
171 // the application icon
172 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
173 #include "rc/appicon.xpm"
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 // Define a new application type, each program should derive a class from wxApp
181 class MyApp : public wxApp
184 // override base class virtuals
185 // ----------------------------
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();
193 // Define a new frame type: this is going to be our main frame
194 class MyFrame : public wxFrame
198 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
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);
207 // any class wishing to process wxWindows events must use this macro
208 DECLARE_EVENT_TABLE()
211 // ----------------------------------------------------------------------------
212 // event tables and other macros for wxWindows
213 // ----------------------------------------------------------------------------
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(XMLID("menu_quit"), MyFrame::OnQuit)
220 EVT_MENU(XMLID("menu_about"), MyFrame::OnAbout)
221 EVT_MENU(XMLID("menu_dlg1"), MyFrame::OnDlg1)
222 EVT_MENU(XMLID("menu_dlg2"), MyFrame::OnDlg2)
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
232 // ============================================================================
234 // ============================================================================
236 // ----------------------------------------------------------------------------
237 // the application class
238 // ----------------------------------------------------------------------------
240 // 'Main program' equivalent: the program execution "starts" here
243 wxImage::AddHandler(new wxGIFHandler);
244 wxXmlResource::Get()->InitAllHandlers();
245 wxXmlResource::Get()->Load("rc/resource.xrc");
247 MyFrame *frame = new MyFrame("XML resources demo",
248 wxPoint(
50,
50), wxSize(
450,
340));
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
258 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
259 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
261 SetIcon(wxICON(appicon));
263 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
264 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
270 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
272 // TRUE is to force the frame to close
276 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
279 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
280 _T("Welcome to
%s"), wxVERSION_STRING);
282 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
285 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
288 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
293 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
296 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
301 \subsection{XRC resource file sample
}\label{xrcsample
}
303 This is the XML file (resource.xrc) for the XRC sample.
306 <?xml version="
1.0"?>
308 <object class="wxMenuBar" name="mainmenu">
309 <style>wxMB_DOCKABLE</style>
310 <object class="wxMenu" name="menu_file">
312 <style>wxMENU_TEAROFF</style>
313 <object class="wxMenuItem" name="menu_about">
314 <label>$About...</label>
315 <bitmap>filesave.gif</bitmap>
317 <object class="separator"/>
318 <object class="wxMenuItem" name="menu_dlg1">
319 <label>Dialog
1</label>
321 <object class="wxMenuItem" name="menu_dlg2">
322 <label>Dialog
2</label>
324 <object class="separator"/>
325 <object class="wxMenuItem" name="menu_quit">
326 <label>E$xit
\tAlt-X</label>
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>
337 <object class="tool" name="menu_save">
338 <bitmap>filesave.gif</bitmap>
339 <tooltip>Save catalog</tooltip>
341 <object class="tool" name="menu_update">
342 <bitmap>update.gif</bitmap>
343 <tooltip>Update catalog - synchronize it with sources</tooltip>
346 <object class="tool" name="menu_quotes">
347 <bitmap>quotes.gif</bitmap>
349 <tooltip>Display quotes around the string?</tooltip>
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>
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>
366 <object class="sizeritem">
367 <object class="wxPanel">
368 <object class="wxStaticText">
369 <label>fdgdfgdfgdfg</label>
371 <style>wxSUNKEN_BORDER</style>
373 <flag>wxALIGN_CENTER</flag>
375 <object class="sizeritem">
376 <object class="wxButton">
377 <label>Buttonek</label>
382 <object class="sizeritem">
383 <object class="wxHtmlWindow">
384 <htmlcode><h1>Hi,</h1>man</htmlcode>
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 <u>NOTEBOOK</u>...</htmlcode>
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 <u>NOTEBOOK</u>...</htmlcode>
415 <label>Page
2</label>
417 <usenotebooksizer>
1</usenotebooksizer>
419 <flag>wxEXPAND</flag>
421 <orient>wxVERTICAL</orient>
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>
434 <flag>wxEXPAND|wxALL</flag>
437 <object class="sizeritem">
438 <object class="wxBoxSizer">
439 <object class="sizeritem">
440 <object class="wxButton" name="wxID_OK">
445 <object class="sizeritem">
446 <object class="wxButton" name="wxID_CANCEL">
447 <label>Cancel</label>
453 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
457 <title>Second testing dialog</title>
462 \subsection{XRC file format
}\label{xrcfileformat
}
464 \subsubsection{Introduction to the XRC file format
}\label{xrcfileformatintro
}
466 This note describes the file format used for storing XRC resources that are
467 used by wxXmlResource class. It is probably only useful for those implementing
468 dialog editors with XRC support, or for those writing XRC files by hand.
470 If you only want to use the resources, you can choose from a number of editors,
471 as listed in
\helpref{XRC concepts
}{xrcconcepts
}.
473 The XRC format is based on XML
1.0 (please consult W3C's specification). There
474 is no DTD available since it is not possible to fully describe the format with
475 the limited expressive power of DTDs.
477 \subsubsection{XRC terminology
}\label{xrcterminology
}
479 The usual XML terminology applies. In particular, we shall use the terms
480 {\it node
},
{\it property
} and
{\it value
} in the XML sense:
483 <node property1="value1" property2="value2">...</node>
486 The term "attribute" is specific to XRC and refers to a property-less subnode
487 of an <object> or <object_ref> node. In the example bellow, <pos>, <label> and
488 <style> are attributes, while neither <resource> nor either of <object>s is:
491 <?xml version="
1.0" encoding="utf-
8">
492 <resource version="
2.3.0.1">
493 <object class="wxPanel">
494 <style>wxSUNKEN_BORDER</style>
495 <object class="wxStaticText">
496 <label>A label</label>
503 \subsubsection{XRC format high-level description
}
505 An XRC resource file is a well-formed XML
1.0 document.
507 The root node of XRC
document must be <resource>. The <resource> node has
508 optional
{\it version
} property. Default version (in absence of the version
509 property) is "
0.0.0.0". The version consists of four integers separated by
510 periods. Version of XRC format changes only if there was an incompatible
511 change introduced (i.e. either the library cannot understand old resource
512 files or older versions of the library wouldn't understand the new format).
513 The first three integers are major, minor and release number of the wxWindows
514 release when the change was introduced, the last one is revision number and
515 is
0 for the first incompatible change in given wxWindows release,
1 for
516 the second, and so on.
518 Differences between versions are described within this
document in paragraphs
519 entitled
{\it Version Note
}.
521 The <resource> node is only allowed to have <object> and <object_ref>
522 subnodes, all of which must have the "name" property.
524 <object> - TODO (name, class, subclass)
526 <object_ref> - TODO (name, ref, subclass)
528 \subsubsection{Common XRC attributes
}
532 \subsubsection{Supported classes
}
536 \subsection{Adding new resource handlers
}\label{newresourcehandlers
}