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
{\tt wxXmlResource::Get()->InitAllHandlers()
} from your wxApp::OnInit function,
73 and then call
{\tt 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
{\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");
}
76 \item set up event tables as usual but use the
{\tt XRCID(str)
} macro to translate from XRC string names
77 to a suitable integer identifier, for example
{\tt EVT
\_MENU(XRCID("quit"), MyFrame::OnQuit)
}.
80 To create an XRC file, use one of the following methods.
82 \begin{itemize
}\itemsep=
0pt
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=
0pt
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.
132 #include "wx/image.h"
133 #include "wx/xrc/xmlres.h"
135 // the application icon
136 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
137 #include "rc/appicon.xpm"
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 // Define a new application type, each program should derive a class from wxApp
145 class MyApp : public wxApp
148 // override base class virtuals
149 // ----------------------------
151 // this one is called on application startup and is a good place for the app
152 // initialization (doing it here and not in the ctor allows to have an error
153 // return: if OnInit() returns false, the application terminates)
154 virtual bool OnInit();
157 // Define a new frame type: this is going to be our main frame
158 class MyFrame : public wxFrame
162 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
164 // event handlers (these functions should _not_ be virtual)
165 void OnQuit(wxCommandEvent& event);
166 void OnAbout(wxCommandEvent& event);
167 void OnDlg1(wxCommandEvent& event);
168 void OnDlg2(wxCommandEvent& event);
171 // any class wishing to process wxWindows events must use this macro
172 DECLARE_EVENT_TABLE()
175 // ----------------------------------------------------------------------------
176 // event tables and other macros for wxWindows
177 // ----------------------------------------------------------------------------
179 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
180 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
181 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
182 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
183 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
188 // ----------------------------------------------------------------------------
189 // the application class
190 // ----------------------------------------------------------------------------
192 // 'Main program' equivalent: the program execution "starts" here
195 wxImage::AddHandler(new wxGIFHandler);
196 wxXmlResource::Get()->InitAllHandlers();
197 wxXmlResource::Get()->Load("rc/resource.xrc");
199 MyFrame *frame = new MyFrame("XML resources demo",
200 wxPoint(
50,
50), wxSize(
450,
340));
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
210 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
211 : wxFrame((wxFrame *)NULL, -
1, title, pos, size)
213 SetIcon(wxICON(appicon));
215 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
216 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
220 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
222 // TRUE is to force the frame to close
226 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
229 msg.Printf( _T("This is the about dialog of XML resources demo.
\n")
230 _T("Welcome to
%s"), wxVERSION_STRING);
232 wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
235 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
238 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
242 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
245 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
250 \subsection{XRC resource file sample
}\label{xrcsample
}
252 This is the XML file (resource.xrc) for the XRC sample.
255 <?xml version="
1.0"?>
256 <resource version="
2.3.0.1">
257 <object class="wxMenuBar" name="mainmenu">
258 <style>wxMB_DOCKABLE</style>
259 <object class="wxMenu" name="menu_file">
261 <style>wxMENU_TEAROFF</style>
262 <object class="wxMenuItem" name="menu_about">
263 <label>_About...</label>
264 <bitmap>filesave.gif</bitmap>
266 <object class="separator"/>
267 <object class="wxMenuItem" name="menu_dlg1">
268 <label>Dialog
1</label>
270 <object class="wxMenuItem" name="menu_dlg2">
271 <label>Dialog
2</label>
273 <object class="separator"/>
274 <object class="wxMenuItem" name="menu_quit">
275 <label>E_xit
\tAlt-X</label>
279 <object class="wxToolBar" name="toolbar">
280 <style>wxTB_FLAT|wxTB_DOCKABLE</style>
281 <margins>
2,
2</margins>
282 <object class="tool" name="menu_open">
283 <bitmap>fileopen.gif</bitmap>
284 <tooltip>Open catalog</tooltip>
286 <object class="tool" name="menu_save">
287 <bitmap>filesave.gif</bitmap>
288 <tooltip>Save catalog</tooltip>
290 <object class="tool" name="menu_update">
291 <bitmap>update.gif</bitmap>
292 <tooltip>Update catalog - synchronize it with sources</tooltip>
295 <object class="tool" name="menu_quotes">
296 <bitmap>quotes.gif</bitmap>
298 <tooltip>Display quotes around the string?</tooltip>
300 <object class="separator"/>
301 <object class="tool" name="menu_fuzzy">
302 <bitmap>fuzzy.gif</bitmap>
303 <tooltip>Toggled if selected string is fuzzy translation</tooltip>
307 <object class="wxDialog" name="dlg1">
308 <object class="wxBoxSizer">
309 <object class="sizeritem">
310 <object class="wxBitmapButton">
311 <bitmap>fuzzy.gif</bitmap>
312 <focus>fileopen.gif</focus>
315 <object class="sizeritem">
316 <object class="wxPanel">
317 <object class="wxStaticText">
318 <label>fdgdfgdfgdfg</label>
320 <style>wxSUNKEN_BORDER</style>
322 <flag>wxALIGN_CENTER</flag>
324 <object class="sizeritem">
325 <object class="wxButton">
326 <label>Buttonek</label>
331 <object class="sizeritem">
332 <object class="wxHtmlWindow">
333 <htmlcode><h1>Hi,</h1>man</htmlcode>
337 <object class="sizeritem">
338 <object class="wxNotebook">
339 <object class="notebookpage">
340 <object class="wxPanel">
341 <object class="wxBoxSizer">
342 <object class="sizeritem">
343 <object class="wxHtmlWindow">
344 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
353 <object class="notebookpage">
354 <object class="wxPanel">
355 <object class="wxBoxSizer">
356 <object class="sizeritem">
357 <object class="wxHtmlWindow">
358 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
364 <label>Page
2</label>
366 <usenotebooksizer>
1</usenotebooksizer>
368 <flag>wxEXPAND</flag>
370 <orient>wxVERTICAL</orient>
373 <object class="wxDialog" name="dlg2">
374 <object class="wxBoxSizer">
375 <orient>wxVERTICAL</orient>
376 <object class="sizeritem" name="dfgdfg">
377 <object class="wxTextCtrl">
378 <size>
200,
200d</size>
379 <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style>
380 <value>Hello, this is an ordinary multiline
\n textctrl....</value>
383 <flag>wxEXPAND|wxALL</flag>
386 <object class="sizeritem">
387 <object class="wxBoxSizer">
388 <object class="sizeritem">
389 <object class="wxButton" name="wxID_OK">
394 <object class="sizeritem">
395 <object class="wxButton" name="wxID_CANCEL">
396 <label>Cancel</label>
402 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
406 <title>Second testing dialog</title>
411 \subsection{XRC file format
}\label{xrcfileformat
}
413 Please see Technical Note
14 (docs/tech/tn0014.txt) in your wxWindows
416 \subsection{Adding new resource handlers
}\label{newresourcehandlers
}