]>
Commit | Line | Data |
---|---|---|
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 (the former makes it possible to store all resources in since file and the latter | |
13 | is useful when you want to embed the resources into the executable). | |
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 If you are going to use \helpref{XRS files}{binaryresourcefiles}, install | |
73 | wxFileSystem ZIP handler first with {\tt wxFileSystem::AddHandler(new wxZipFSHandler);} | |
74 | \item call {\tt wxXmlResource::Get()->InitAllHandlers()} from your wxApp::OnInit function, | |
75 | and then call {\tt wxXmlResource::Get()->Load("myfile.xrc")} to load the resource file; | |
76 | \item to create a dialog from a resource, create it using the default constructor, and then | |
77 | load using for example {\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");} | |
78 | \item set up event tables as usual but use the {\tt XRCID(str)} macro to translate from XRC string names | |
79 | to a suitable integer identifier, for example {\tt EVT\_MENU(XRCID("quit"), MyFrame::OnQuit)}. | |
80 | \end{itemize} | |
81 | ||
82 | To create an XRC file, you can use one of the following methods. | |
83 | ||
84 | \begin{itemize}\itemsep=0pt | |
85 | \item Create the file by hand; | |
86 | \item use \urlref{wxDesigner}{http://www.roebling.de}, a commercial dialog designer/RAD tool; | |
87 | \item use \urlref{DialogBlocks}{http://www.anthemion.co.uk/dialogblocks}, a commercial dialog editor; | |
88 | \item use \urlref{XRCed}{http://xrced.sf.net}, a wxPython-based | |
89 | dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWindows | |
90 | CVS archive; | |
91 | \item use \urlref{Glade}{http://wxglade.sf.net}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC; | |
92 | \item use wxrcedit ({\tt utils/contrib/wxrcedit}) (under development); | |
93 | \item convert WIN32 RC files to XRC with the tool in {\tt contrib/utils/convertrc}. | |
94 | \end{itemize} | |
95 | ||
96 | A complete list of third-party tools that write to XRC can be found at \urlref{www.wxwindows.org/lnk_tool.htm}{http://www.wxwindows.org/lnk_tool.in}. | |
97 | ||
98 | It is highly recommended that you use a resource editing tool, since it's fiddly writing | |
99 | XRC files by hand. | |
100 | ||
101 | You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways. | |
102 | You can pass an XRC file (XML-based text resource file) | |
103 | or a \helpref{zip-compressed file}{binaryresourcefiles} (extension ZIP or XRS) containing other XRC. | |
104 | ||
105 | You can also use \helpref{embedded C++ resources}{embeddedresource} | |
106 | ||
107 | \subsection{Using binary resource files}\label{binaryresourcefiles} | |
108 | ||
109 | To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters | |
110 | (the input XRC files) and the following switches and options: | |
111 | \begin{itemize}\itemsep=0pt | |
112 | \item -h (--help): show a help message | |
113 | \item -v (--verbose): show verbose logging information | |
114 | \item -c (--cpp-code): write C++ source rather than a XRS file | |
115 | \item -u (--uncompressed): do not compress XML files (C++ only) | |
116 | \item -g (--gettext): output .po catalog (to stdout, or a file if -o is used) | |
117 | \item -n (--function) <name>: specify C++ function name (use with -c) | |
118 | \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp | |
119 | \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file | |
120 | \end{itemize} | |
121 | ||
122 | For example: | |
123 | \begin{verbatim} | |
124 | % wxrc resource.wrc | |
125 | % wxrc resource.wrc -o resource.wrs | |
126 | % wxrc resource.wrc -v -c -o resource.cpp | |
127 | \end{verbatim} | |
128 | ||
129 | \wxheading{Note} | |
130 | ||
131 | XRS file is esentially a renamed ZIP archive which means that you can manipulate | |
132 | it with standard ZIP tools. Note that if you are using XRS files, you have | |
133 | to initialize \helpref{wxFileSystem}{wxfilesystem} ZIP handler first! It is a simple | |
134 | thing to do: | |
135 | \begin{verbatim} | |
136 | #include <wx/filesys.h> | |
137 | #include <wx/fs_zip.h> | |
138 | ... | |
139 | wxFileSystem::AddHandler(new wxZipFSHandler); | |
140 | \end{verbatim} | |
141 | ||
142 | \subsection{Using embedded resources}\label{embeddedresource} | |
143 | ||
144 | It is sometimes useful to embed resources in the executable itself instead | |
145 | of loading external file (e.g. when your app is small and consists only of one | |
146 | exe file). XRC provides means to convert resources into regular C++ file that | |
147 | can be compiled and included in the executable. | |
148 | ||
149 | Use the {\tt -c} switch to | |
150 | {\tt wxrc} utility to produce C++ file with embedded resources. This file will | |
151 | contain a function called {\it InitXmlResource} (unless you override this with | |
152 | a command line switch). Use it to load the resource: | |
153 | \begin{verbatim} | |
154 | extern void InitXMLResource(); // defined in generated file | |
155 | ... | |
156 | wxXmlResource::Get()->InitAllHandlers(); | |
157 | InitXmlResource(); | |
158 | ... | |
159 | \end{verbatim} | |
160 | ||
161 | \subsection{XRC C++ sample}\label{xrccppsample} | |
162 | ||
163 | This is the C++ source file (xrcdemo.cpp) for the XRC sample. | |
164 | ||
165 | \begin{verbatim} | |
166 | #include "wx/wx.h" | |
167 | #include "wx/image.h" | |
168 | #include "wx/xrc/xmlres.h" | |
169 | ||
170 | // the application icon | |
171 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) | |
172 | #include "rc/appicon.xpm" | |
173 | #endif | |
174 | ||
175 | // ---------------------------------------------------------------------------- | |
176 | // private classes | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
179 | // Define a new application type, each program should derive a class from wxApp | |
180 | class MyApp : public wxApp | |
181 | { | |
182 | public: | |
183 | // override base class virtuals | |
184 | // ---------------------------- | |
185 | ||
186 | // this one is called on application startup and is a good place for the app | |
187 | // initialization (doing it here and not in the ctor allows to have an error | |
188 | // return: if OnInit() returns false, the application terminates) | |
189 | virtual bool OnInit(); | |
190 | }; | |
191 | ||
192 | // Define a new frame type: this is going to be our main frame | |
193 | class MyFrame : public wxFrame | |
194 | { | |
195 | public: | |
196 | // ctor(s) | |
197 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
198 | ||
199 | // event handlers (these functions should _not_ be virtual) | |
200 | void OnQuit(wxCommandEvent& event); | |
201 | void OnAbout(wxCommandEvent& event); | |
202 | void OnDlg1(wxCommandEvent& event); | |
203 | void OnDlg2(wxCommandEvent& event); | |
204 | ||
205 | private: | |
206 | // any class wishing to process wxWindows events must use this macro | |
207 | DECLARE_EVENT_TABLE() | |
208 | }; | |
209 | ||
210 | // ---------------------------------------------------------------------------- | |
211 | // event tables and other macros for wxWindows | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
214 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
215 | EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit) | |
216 | EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout) | |
217 | EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1) | |
218 | EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2) | |
219 | END_EVENT_TABLE() | |
220 | ||
221 | IMPLEMENT_APP(MyApp) | |
222 | ||
223 | // ---------------------------------------------------------------------------- | |
224 | // the application class | |
225 | // ---------------------------------------------------------------------------- | |
226 | ||
227 | // 'Main program' equivalent: the program execution "starts" here | |
228 | bool MyApp::OnInit() | |
229 | { | |
230 | wxImage::AddHandler(new wxGIFHandler); | |
231 | wxXmlResource::Get()->InitAllHandlers(); | |
232 | wxXmlResource::Get()->Load("rc/resource.xrc"); | |
233 | ||
234 | MyFrame *frame = new MyFrame("XML resources demo", | |
235 | wxPoint(50, 50), wxSize(450, 340)); | |
236 | frame->Show(true); | |
237 | return true; | |
238 | } | |
239 | ||
240 | // ---------------------------------------------------------------------------- | |
241 | // main frame | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | // frame constructor | |
245 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
246 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
247 | { | |
248 | SetIcon(wxICON(appicon)); | |
249 | ||
250 | SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu")); | |
251 | SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar")); | |
252 | } | |
253 | ||
254 | // event handlers | |
255 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
256 | { | |
257 | // true is to force the frame to close | |
258 | Close(true); | |
259 | } | |
260 | ||
261 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
262 | { | |
263 | wxString msg; | |
264 | msg.Printf( _T("This is the about dialog of XML resources demo.\n") | |
265 | _T("Welcome to %s"), wxVERSION_STRING); | |
266 | ||
267 | wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this); | |
268 | } | |
269 | ||
270 | void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) | |
271 | { | |
272 | wxDialog dlg; | |
273 | wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1"); | |
274 | dlg.ShowModal(); | |
275 | } | |
276 | ||
277 | void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event)) | |
278 | { | |
279 | wxDialog dlg; | |
280 | wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2"); | |
281 | dlg.ShowModal(); | |
282 | } | |
283 | \end{verbatim} | |
284 | ||
285 | \subsection{XRC resource file sample}\label{xrcsample} | |
286 | ||
287 | This is the XML file (resource.xrc) for the XRC sample. | |
288 | ||
289 | \begin{verbatim} | |
290 | <?xml version="1.0"?> | |
291 | <resource version="2.3.0.1"> | |
292 | <object class="wxMenuBar" name="mainmenu"> | |
293 | <style>wxMB_DOCKABLE</style> | |
294 | <object class="wxMenu" name="menu_file"> | |
295 | <label>_File</label> | |
296 | <style>wxMENU_TEAROFF</style> | |
297 | <object class="wxMenuItem" name="menu_about"> | |
298 | <label>_About...</label> | |
299 | <bitmap>filesave.gif</bitmap> | |
300 | </object> | |
301 | <object class="separator"/> | |
302 | <object class="wxMenuItem" name="menu_dlg1"> | |
303 | <label>Dialog 1</label> | |
304 | </object> | |
305 | <object class="wxMenuItem" name="menu_dlg2"> | |
306 | <label>Dialog 2</label> | |
307 | </object> | |
308 | <object class="separator"/> | |
309 | <object class="wxMenuItem" name="menu_quit"> | |
310 | <label>E_xit\tAlt-X</label> | |
311 | </object> | |
312 | </object> | |
313 | </object> | |
314 | <object class="wxToolBar" name="toolbar"> | |
315 | <style>wxTB_FLAT|wxTB_DOCKABLE</style> | |
316 | <margins>2,2</margins> | |
317 | <object class="tool" name="menu_open"> | |
318 | <bitmap>fileopen.gif</bitmap> | |
319 | <tooltip>Open catalog</tooltip> | |
320 | </object> | |
321 | <object class="tool" name="menu_save"> | |
322 | <bitmap>filesave.gif</bitmap> | |
323 | <tooltip>Save catalog</tooltip> | |
324 | </object> | |
325 | <object class="tool" name="menu_update"> | |
326 | <bitmap>update.gif</bitmap> | |
327 | <tooltip>Update catalog - synchronize it with sources</tooltip> | |
328 | </object> | |
329 | <separator/> | |
330 | <object class="tool" name="menu_quotes"> | |
331 | <bitmap>quotes.gif</bitmap> | |
332 | <toggle>1</toggle> | |
333 | <tooltip>Display quotes around the string?</tooltip> | |
334 | </object> | |
335 | <object class="separator"/> | |
336 | <object class="tool" name="menu_fuzzy"> | |
337 | <bitmap>fuzzy.gif</bitmap> | |
338 | <tooltip>Toggled if selected string is fuzzy translation</tooltip> | |
339 | <toggle>1</toggle> | |
340 | </object> | |
341 | </object> | |
342 | <object class="wxDialog" name="dlg1"> | |
343 | <object class="wxBoxSizer"> | |
344 | <object class="sizeritem"> | |
345 | <object class="wxBitmapButton"> | |
346 | <bitmap>fuzzy.gif</bitmap> | |
347 | <focus>fileopen.gif</focus> | |
348 | </object> | |
349 | </object> | |
350 | <object class="sizeritem"> | |
351 | <object class="wxPanel"> | |
352 | <object class="wxStaticText"> | |
353 | <label>fdgdfgdfgdfg</label> | |
354 | </object> | |
355 | <style>wxSUNKEN_BORDER</style> | |
356 | </object> | |
357 | <flag>wxALIGN_CENTER</flag> | |
358 | </object> | |
359 | <object class="sizeritem"> | |
360 | <object class="wxButton"> | |
361 | <label>Buttonek</label> | |
362 | </object> | |
363 | <border>10d</border> | |
364 | <flag>wxALL</flag> | |
365 | </object> | |
366 | <object class="sizeritem"> | |
367 | <object class="wxHtmlWindow"> | |
368 | <htmlcode><h1>Hi,</h1>man</htmlcode> | |
369 | <size>100,45d</size> | |
370 | </object> | |
371 | </object> | |
372 | <object class="sizeritem"> | |
373 | <object class="wxNotebook"> | |
374 | <object class="notebookpage"> | |
375 | <object class="wxPanel"> | |
376 | <object class="wxBoxSizer"> | |
377 | <object class="sizeritem"> | |
378 | <object class="wxHtmlWindow"> | |
379 | <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode> | |
380 | <size>50,50d</size> | |
381 | </object> | |
382 | <option>1</option> | |
383 | </object> | |
384 | </object> | |
385 | </object> | |
386 | <label>Page</label> | |
387 | </object> | |
388 | <object class="notebookpage"> | |
389 | <object class="wxPanel"> | |
390 | <object class="wxBoxSizer"> | |
391 | <object class="sizeritem"> | |
392 | <object class="wxHtmlWindow"> | |
393 | <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode> | |
394 | <size>50,50d</size> | |
395 | </object> | |
396 | </object> | |
397 | </object> | |
398 | </object> | |
399 | <label>Page 2</label> | |
400 | </object> | |
401 | <usenotebooksizer>1</usenotebooksizer> | |
402 | </object> | |
403 | <flag>wxEXPAND</flag> | |
404 | </object> | |
405 | <orient>wxVERTICAL</orient> | |
406 | </object> | |
407 | </object> | |
408 | <object class="wxDialog" name="dlg2"> | |
409 | <object class="wxBoxSizer"> | |
410 | <orient>wxVERTICAL</orient> | |
411 | <object class="sizeritem" name="dfgdfg"> | |
412 | <object class="wxTextCtrl"> | |
413 | <size>200,200d</size> | |
414 | <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style> | |
415 | <value>Hello, this is an ordinary multiline\n textctrl....</value> | |
416 | </object> | |
417 | <option>1</option> | |
418 | <flag>wxEXPAND|wxALL</flag> | |
419 | <border>10</border> | |
420 | </object> | |
421 | <object class="sizeritem"> | |
422 | <object class="wxBoxSizer"> | |
423 | <object class="sizeritem"> | |
424 | <object class="wxButton" name="wxID_OK"> | |
425 | <label>Ok</label> | |
426 | <default>1</default> | |
427 | </object> | |
428 | </object> | |
429 | <object class="sizeritem"> | |
430 | <object class="wxButton" name="wxID_CANCEL"> | |
431 | <label>Cancel</label> | |
432 | </object> | |
433 | <border>10</border> | |
434 | <flag>wxLEFT</flag> | |
435 | </object> | |
436 | </object> | |
437 | <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag> | |
438 | <border>10</border> | |
439 | </object> | |
440 | </object> | |
441 | <title>Second testing dialog</title> | |
442 | </object> | |
443 | </resource> | |
444 | \end{verbatim} | |
445 | ||
446 | \subsection{XRC file format}\label{xrcfileformat} | |
447 | ||
448 | Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWindows | |
449 | distribution. | |
450 | ||
451 | \subsection{Adding new resource handlers}\label{newresourcehandlers} | |
452 | ||
453 | Coming soon. | |
454 |