1 \section{XML-based resource system overview
}\label{xrcoverview
}
3 Classes:
\helpref{wxXmlResource
}{wxxmlresource
},
\helpref{wxXmlResourceHandler
}{wxxmlresourcehandler
}
5 The XML-based resource system, known as XRC, allows user interface elements such as
6 dialogs, menu bars and toolbars, to be stored in text files and loaded into
7 the application at run-time. XRC files can also be compiled into binary XRS files or C++
8 code, so an XML parser does not need to be linked with the application and load times
11 There are several advantages to using XRC resources.
13 \begin{itemize
}\itemsep=
0pt
14 \item Recompiling and linking an application is not necessary if the
16 \item If you use a dialog designers that generates C++ code, it can be hard
17 to reintegrate this into existing C++ code. Separation of resources and code
18 is 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
22 \item The XRC format is a wxWindows standard,
23 and can be generated or postprocessed by any program that understands it. As it is based
24 on the XML standard, existing XML editors can be used for simple editing purposes.
27 XRC was written by Vaclav Slavik.
29 \subsection{Compiling and using XRC
}\label{compilingxrc
}
31 XRC can be found under the 'contrib' hierarchy, in the following directories:
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)
43 \begin{itemize
}\itemsep=
0pt
44 \item Under Windows using VC++, open the contrib/src/xrc/XrcVC.dsw project
45 and compile. Also compile contrib/utils/wxrc using wxBase if you wish to compile
47 \item Under Unix, XRC should be configured when you configured
48 wxWindows. Make XRC by changing directory to contrib/src/xrc and
49 type 'make'. Similarly compile contrib/utils/wxrc using wxBase if you wish to compile
50 resource files.
{\bf Note:
} there is currently a
51 problem with the wxWindows build system that means that
52 only the static version of library can be built at present.
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.
62 \subsection{XRC concepts
}\label{xrcconcepts
}
64 These are the typical steps for using XRC files in your application.
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,
69 and 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
71 load 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
73 to a suitable integer identifier, for example
\verb$EVT_MENU(XMLID("quit"), MyFrame::OnQuit)$.
76 To create an XRC file, use one of the following methods.
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
87 It is highly recommended that you use a tool such as wxDesigner, since it's fiddly writing
90 You can use
\helpref{wxXmlResource::Load
}{wxxmlresourceload
} in a number of ways.
91 You can pass an XRC file (XML-based text resource file), an XMB file (compiled binary file)
92 or a zip-compressed file (extension ZIP or RSC) containing other XRC or XMB files.
94 TODO: is the compiled binary format XMB or XRS? How do you handle a C++ resource file?
96 \subsection{Using binary resource files
}\label{binaryresourcefiles
}
98 To compile binary resource files, use the command-line wxrc utility. It takes a single file parameter (the
99 input XRC file) and the following switches and options.
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
116 % wxrc resource.wrc -o resource.wrs
117 % wxrc resource.wrc -v -c -o resource.cpp
120 \subsection{XRC C++ sample
}\label{xrccppsample
}
122 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
125 /////////////////////////////////////////////////////////////////////////////
127 // Purpose: XML resources sample
128 // Author: Vaclav Slavik
130 // Copyright: (c) Vaclav Slavik
131 // Licence: wxWindows licence
132 /////////////////////////////////////////////////////////////////////////////
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 #pragma implementation "xrcdemo.cpp"
143 #pragma interface "xrcdemo.cpp"
146 // For compilers that support precompilation, includes "wx/wx.h".
147 #include "wx/wxprec.h"
153 // for all others, include the necessary headers (this file is usually all you
154 // need because it includes almost all "standard" wxWindows headers)
159 #include "wx/image.h"
160 #include "wx/xrc/xmlres.h"
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
165 // the application icon
166 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
167 #include "rc/appicon.xpm"
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 // Define a new application type, each program should derive a class from wxApp
175 class MyApp : public wxApp
178 // override base class virtuals
179 // ----------------------------
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();
187 // Define a new frame type: this is going to be our main frame
188 class MyFrame : public wxFrame
192 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
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);
201 // any class wishing to process wxWindows events must use this macro
202 DECLARE_EVENT_TABLE()
205 // ----------------------------------------------------------------------------
206 // event tables and other macros for wxWindows
207 // ----------------------------------------------------------------------------
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.
212 BEGIN_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)
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
226 // ============================================================================
228 // ============================================================================
230 // ----------------------------------------------------------------------------
231 // the application class
232 // ----------------------------------------------------------------------------
234 // 'Main program' equivalent: the program execution "starts" here
237 wxImage::AddHandler(new wxGIFHandler);
238 wxXmlResource::Get()->InitAllHandlers();
239 wxXmlResource::Get()->Load("rc/resource.xrc");
241 MyFrame *frame = new MyFrame("XML resources demo",
242 wxPoint(
50,
50), wxSize(
450,
340));
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
252 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
253 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
255 SetIcon(wxICON(appicon));
257 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
258 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
264 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
266 // TRUE is to force the frame to close
270 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
273 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
274 _T("Welcome to
%s"), wxVERSION_STRING);
276 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
279 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
282 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
287 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
290 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
295 \subsection{XRC resource file sample
}\label{xrcsample
}
297 This is the XML file (resource.xrc) for the XRC sample.
300 <?xml version="
1.0"?>
302 <object class="wxMenuBar" name="mainmenu">
303 <style>wxMB_DOCKABLE</style>
304 <object class="wxMenu" name="menu_file">
306 <style>wxMENU_TEAROFF</style>
307 <object class="wxMenuItem" name="menu_about">
308 <label>$About...</label>
309 <bitmap>filesave.gif</bitmap>
311 <object class="separator"/>
312 <object class="wxMenuItem" name="menu_dlg1">
313 <label>Dialog
1</label>
315 <object class="wxMenuItem" name="menu_dlg2">
316 <label>Dialog
2</label>
318 <object class="separator"/>
319 <object class="wxMenuItem" name="menu_quit">
320 <label>E$xit
\tAlt-X</label>
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>
331 <object class="tool" name="menu_save">
332 <bitmap>filesave.gif</bitmap>
333 <tooltip>Save catalog</tooltip>
335 <object class="tool" name="menu_update">
336 <bitmap>update.gif</bitmap>
337 <tooltip>Update catalog - synchronize it with sources</tooltip>
340 <object class="tool" name="menu_quotes">
341 <bitmap>quotes.gif</bitmap>
343 <tooltip>Display quotes around the string?</tooltip>
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>
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>
360 <object class="sizeritem">
361 <object class="wxPanel">
362 <object class="wxStaticText">
363 <label>fdgdfgdfgdfg</label>
365 <style>wxSUNKEN_BORDER</style>
367 <flag>wxALIGN_CENTER</flag>
369 <object class="sizeritem">
370 <object class="wxButton">
371 <label>Buttonek</label>
376 <object class="sizeritem">
377 <object class="wxHtmlWindow">
378 <htmlcode><h1>Hi,</h1>man</htmlcode>
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 <u>NOTEBOOK</u>...</htmlcode>
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 <u>NOTEBOOK</u>...</htmlcode>
409 <label>Page
2</label>
411 <usenotebooksizer>
1</usenotebooksizer>
413 <flag>wxEXPAND</flag>
415 <orient>wxVERTICAL</orient>
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>
428 <flag>wxEXPAND|wxALL</flag>
431 <object class="sizeritem">
432 <object class="wxBoxSizer">
433 <object class="sizeritem">
434 <object class="wxButton" name="wxID_OK">
439 <object class="sizeritem">
440 <object class="wxButton" name="wxID_CANCEL">
441 <label>Cancel</label>
447 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
451 <title>Second testing dialog</title>
456 \subsection{XRC file format
}\label{xrcfileformat
}
458 This section to be written.
460 \subsection{Adding new resource handlers
}\label{newresourcehandlers
}
462 This section to be written.