]>
Commit | Line | Data |
---|---|---|
1 | % Note: -e/C++ header generation documentation added by | |
2 | % Eduardo Marques <edrdo@netcabo.pt> | |
3 | % | |
4 | \section{XML-based resource system overview}\label{xrcoverview} | |
5 | ||
6 | Classes: \helpref{wxXmlResource}{wxxmlresource}, \helpref{wxXmlResourceHandler}{wxxmlresourcehandler} | |
7 | ||
8 | The XML-based resource system, known as XRC, allows user interface elements such as | |
9 | dialogs, menu bars and toolbars, to be stored in text files and loaded into | |
10 | the application at run-time. XRC files can also be compiled into binary XRS files or C++ | |
11 | code (the former makes it possible to store all resources in a single file and the latter | |
12 | is useful when you want to embed the resources into the executable). | |
13 | ||
14 | There are several advantages to using XRC resources. | |
15 | ||
16 | \begin{itemize}\itemsep=0pt | |
17 | \item Recompiling and linking an application is not necessary if the | |
18 | resources change. | |
19 | \item If you use a dialog designer that generates C++ code, it can be hard | |
20 | to reintegrate this into existing C++ code. Separation of resources and code | |
21 | is a more elegant solution. | |
22 | \item You can choose between different alternative resource files at run time, if necessary. | |
23 | \item The XRC format uses sizers for flexibility, allowing dialogs to be resizable | |
24 | and highly portable. | |
25 | \item The XRC format is a wxWidgets standard, | |
26 | and can be generated or postprocessed by any program that understands it. As it is based | |
27 | on the XML standard, existing XML editors can be used for simple editing purposes. | |
28 | \end{itemize} | |
29 | ||
30 | XRC was written by Vaclav Slavik. | |
31 | ||
32 | \subsection{XRC concepts}\label{xrcconcepts} | |
33 | ||
34 | These are the typical steps for using XRC files in your application. | |
35 | ||
36 | \begin{itemize}\itemsep=0pt | |
37 | \item Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice; | |
38 | \item If you are going to use \helpref{XRS files}{binaryresourcefiles}, install | |
39 | wxFileSystem archive handler first with {\tt wxFileSystem::AddHandler(new wxArchiveFSHandler);} | |
40 | \item call {\tt wxXmlResource::Get()->InitAllHandlers()} from your wxApp::OnInit function, | |
41 | and then call {\tt wxXmlResource::Get()->Load("myfile.xrc")} to load the resource file; | |
42 | \item to create a dialog from a resource, create it using the default constructor, and then | |
43 | load it using for example {\tt wxXmlResource::Get()->LoadDialog(\&dlg, this, "dlg1");} | |
44 | \item set up event tables as usual but use the {\tt XRCID(str)} macro to translate from XRC string names | |
45 | to a suitable integer identifier, for example {\tt EVT\_MENU(XRCID("quit"), MyFrame::OnQuit)}. | |
46 | \end{itemize} | |
47 | ||
48 | To create an XRC file, you can use one of the following methods. | |
49 | ||
50 | \begin{itemize}\itemsep=0pt | |
51 | \item Create the file by hand; | |
52 | \item use \urlref{wxDesigner}{http://www.roebling.de}, a commercial dialog designer/RAD tool; | |
53 | \item use \urlref{DialogBlocks}{http://www.anthemion.co.uk/dialogblocks}, a commercial dialog editor; | |
54 | \item use \urlref{XRCed}{http://xrced.sf.net}, a wxPython-based | |
55 | dialog editor that you can find in the {\tt wxPython/tools} subdirectory of the wxWidgets | |
56 | CVS archive; | |
57 | \item use \urlref{wxGlade}{http://wxglade.sf.net}, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC; | |
58 | \item convert WIN32 RC files to XRC with the tool in {\tt contrib/utils/convertrc}. | |
59 | \end{itemize} | |
60 | ||
61 | A complete list of third-party tools that write to XRC can be found at \urlref{www.wxwidgets.org/lnk\_tool.htm}{http://www.wxwidgets.org/lnk\_tool.htm}. | |
62 | ||
63 | It is highly recommended that you use a resource editing tool, since it's fiddly writing | |
64 | XRC files by hand. | |
65 | ||
66 | You can use \helpref{wxXmlResource::Load}{wxxmlresourceload} in a number of ways. | |
67 | You can pass an XRC file (XML-based text resource file) | |
68 | or a \helpref{zip-compressed file}{binaryresourcefiles} (extension ZIP or XRS) containing other XRC. | |
69 | ||
70 | You can also use \helpref{embedded C++ resources}{embeddedresource} | |
71 | ||
72 | \subsection{Using binary resource files}\label{binaryresourcefiles} | |
73 | ||
74 | To compile binary resource files, use the command-line wxrc utility. It takes one or more file parameters | |
75 | (the input XRC files) and the following switches and options: | |
76 | \begin{itemize}\itemsep=0pt | |
77 | \item -h (--help): show a help message | |
78 | \item -v (--verbose): show verbose logging information | |
79 | \item -c (--cpp-code): write C++ source rather than a XRS file | |
80 | \item -e (--extra-cpp-code): if used together with -c, generates C++ header file | |
81 | containing class definitions for the windows defined by the XRC file (see special subsection) | |
82 | \item -u (--uncompressed): do not compress XML files (C++ only) | |
83 | \item -g (--gettext): output underscore-wrapped strings that poEdit or gettext can scan. Outputs to stdout, or a file if -o is used | |
84 | \item -n (--function) <name>: specify C++ function name (use with -c) | |
85 | \item -o (--output) <filename>: specify the output file, such as resource.xrs or resource.cpp | |
86 | \item -l (--list-of-handlers) <filename>: output a list of necessary handlers to this file | |
87 | \end{itemize} | |
88 | ||
89 | For example: | |
90 | \begin{verbatim} | |
91 | % wxrc resource.xrc | |
92 | % wxrc resource.xrc -o resource.xrs | |
93 | % wxrc resource.xrc -v -c -o resource.cpp | |
94 | \end{verbatim} | |
95 | ||
96 | \wxheading{Note} | |
97 | ||
98 | XRS file is essentially a renamed ZIP archive which means that you can manipulate | |
99 | it with standard ZIP tools. Note that if you are using XRS files, you have | |
100 | to initialize the \helpref{wxFileSystem}{wxfilesystem} archive handler first! It is a simple | |
101 | thing to do: | |
102 | ||
103 | \begin{verbatim} | |
104 | #include <wx/filesys.h> | |
105 | #include <wx/fs_arc.h> | |
106 | ... | |
107 | wxFileSystem::AddHandler(new wxArchiveFSHandler); | |
108 | \end{verbatim} | |
109 | ||
110 | \subsection{Using embedded resources}\label{embeddedresource} | |
111 | ||
112 | It is sometimes useful to embed resources in the executable itself instead | |
113 | of loading an external file (e.g. when your app is small and consists only of one | |
114 | exe file). XRC provides means to convert resources into regular C++ file that | |
115 | can be compiled and included in the executable. | |
116 | ||
117 | Use the {\tt -c} switch to | |
118 | {\tt wxrc} utility to produce C++ file with embedded resources. This file will | |
119 | contain a function called {\it InitXmlResource} (unless you override this with | |
120 | a command line switch). Use it to load the resource: | |
121 | ||
122 | \begin{verbatim} | |
123 | extern void InitXmlResource(); // defined in generated file | |
124 | ... | |
125 | wxXmlResource::Get()->InitAllHandlers(); | |
126 | InitXmlResource(); | |
127 | ... | |
128 | \end{verbatim} | |
129 | ||
130 | \subsection{XRC C++ sample}\label{xrccppsample} | |
131 | ||
132 | This is the C++ source file (xrcdemo.cpp) for the XRC sample. | |
133 | ||
134 | \begin{verbatim} | |
135 | #include "wx/wx.h" | |
136 | #include "wx/image.h" | |
137 | #include "wx/xrc/xmlres.h" | |
138 | ||
139 | // the application icon | |
140 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) | |
141 | #include "rc/appicon.xpm" | |
142 | #endif | |
143 | ||
144 | // ---------------------------------------------------------------------------- | |
145 | // private classes | |
146 | // ---------------------------------------------------------------------------- | |
147 | ||
148 | // Define a new application type, each program should derive a class from wxApp | |
149 | class MyApp : public wxApp | |
150 | { | |
151 | public: | |
152 | // override base class virtuals | |
153 | // ---------------------------- | |
154 | ||
155 | // this one is called on application startup and is a good place for the app | |
156 | // initialization (doing it here and not in the ctor allows to have an error | |
157 | // return: if OnInit() returns false, the application terminates) | |
158 | virtual bool OnInit(); | |
159 | }; | |
160 | ||
161 | // Define a new frame type: this is going to be our main frame | |
162 | class MyFrame : public wxFrame | |
163 | { | |
164 | public: | |
165 | // ctor(s) | |
166 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
167 | ||
168 | // event handlers (these functions should _not_ be virtual) | |
169 | void OnQuit(wxCommandEvent& event); | |
170 | void OnAbout(wxCommandEvent& event); | |
171 | void OnDlg1(wxCommandEvent& event); | |
172 | void OnDlg2(wxCommandEvent& event); | |
173 | ||
174 | private: | |
175 | // any class wishing to process wxWidgets events must use this macro | |
176 | DECLARE_EVENT_TABLE() | |
177 | }; | |
178 | ||
179 | // ---------------------------------------------------------------------------- | |
180 | // event tables and other macros for wxWidgets | |
181 | // ---------------------------------------------------------------------------- | |
182 | ||
183 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
184 | EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit) | |
185 | EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout) | |
186 | EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1) | |
187 | EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2) | |
188 | END_EVENT_TABLE() | |
189 | ||
190 | IMPLEMENT_APP(MyApp) | |
191 | ||
192 | // ---------------------------------------------------------------------------- | |
193 | // the application class | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
196 | // 'Main program' equivalent: the program execution "starts" here | |
197 | bool MyApp::OnInit() | |
198 | { | |
199 | wxImage::AddHandler(new wxGIFHandler); | |
200 | wxXmlResource::Get()->InitAllHandlers(); | |
201 | wxXmlResource::Get()->Load("rc/resource.xrc"); | |
202 | ||
203 | MyFrame *frame = new MyFrame("XML resources demo", | |
204 | wxPoint(50, 50), wxSize(450, 340)); | |
205 | frame->Show(true); | |
206 | return true; | |
207 | } | |
208 | ||
209 | // ---------------------------------------------------------------------------- | |
210 | // main frame | |
211 | // ---------------------------------------------------------------------------- | |
212 | ||
213 | // frame constructor | |
214 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
215 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
216 | { | |
217 | SetIcon(wxICON(appicon)); | |
218 | ||
219 | SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu")); | |
220 | SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar")); | |
221 | } | |
222 | ||
223 | // event handlers | |
224 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
225 | { | |
226 | // true is to force the frame to close | |
227 | Close(true); | |
228 | } | |
229 | ||
230 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
231 | { | |
232 | wxString msg; | |
233 | msg.Printf( _T("This is the about dialog of XML resources demo.\n") | |
234 | _T("Welcome to %s"), wxVERSION_STRING); | |
235 | ||
236 | wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this); | |
237 | } | |
238 | ||
239 | void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) | |
240 | { | |
241 | wxDialog dlg; | |
242 | wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1"); | |
243 | dlg.ShowModal(); | |
244 | } | |
245 | ||
246 | void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event)) | |
247 | { | |
248 | wxDialog dlg; | |
249 | wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2"); | |
250 | dlg.ShowModal(); | |
251 | } | |
252 | \end{verbatim} | |
253 | ||
254 | \subsection{XRC resource file sample}\label{xrcsample} | |
255 | ||
256 | This is the XML file (resource.xrc) for the XRC sample. | |
257 | ||
258 | \begin{verbatim} | |
259 | <?xml version="1.0"?> | |
260 | <resource version="2.3.0.1"> | |
261 | <object class="wxMenuBar" name="mainmenu"> | |
262 | <style>wxMB_DOCKABLE</style> | |
263 | <object class="wxMenu" name="menu_file"> | |
264 | <label>_File</label> | |
265 | <style>wxMENU_TEAROFF</style> | |
266 | <object class="wxMenuItem" name="menu_about"> | |
267 | <label>_About...</label> | |
268 | <bitmap>filesave.gif</bitmap> | |
269 | </object> | |
270 | <object class="separator"/> | |
271 | <object class="wxMenuItem" name="menu_dlg1"> | |
272 | <label>Dialog 1</label> | |
273 | </object> | |
274 | <object class="wxMenuItem" name="menu_dlg2"> | |
275 | <label>Dialog 2</label> | |
276 | </object> | |
277 | <object class="separator"/> | |
278 | <object class="wxMenuItem" name="menu_quit"> | |
279 | <label>E_xit\tAlt-X</label> | |
280 | </object> | |
281 | </object> | |
282 | </object> | |
283 | <object class="wxToolBar" name="toolbar"> | |
284 | <style>wxTB_FLAT|wxTB_DOCKABLE</style> | |
285 | <margins>2,2</margins> | |
286 | <object class="tool" name="menu_open"> | |
287 | <bitmap>fileopen.gif</bitmap> | |
288 | <tooltip>Open catalog</tooltip> | |
289 | </object> | |
290 | <object class="tool" name="menu_save"> | |
291 | <bitmap>filesave.gif</bitmap> | |
292 | <tooltip>Save catalog</tooltip> | |
293 | </object> | |
294 | <object class="tool" name="menu_update"> | |
295 | <bitmap>update.gif</bitmap> | |
296 | <tooltip>Update catalog - synchronize it with sources</tooltip> | |
297 | </object> | |
298 | <separator/> | |
299 | <object class="tool" name="menu_quotes"> | |
300 | <bitmap>quotes.gif</bitmap> | |
301 | <toggle>1</toggle> | |
302 | <tooltip>Display quotes around the string?</tooltip> | |
303 | </object> | |
304 | <object class="separator"/> | |
305 | <object class="tool" name="menu_fuzzy"> | |
306 | <bitmap>fuzzy.gif</bitmap> | |
307 | <tooltip>Toggled if selected string is fuzzy translation</tooltip> | |
308 | <toggle>1</toggle> | |
309 | </object> | |
310 | </object> | |
311 | <object class="wxDialog" name="dlg1"> | |
312 | <object class="wxBoxSizer"> | |
313 | <object class="sizeritem"> | |
314 | <object class="wxBitmapButton"> | |
315 | <bitmap>fuzzy.gif</bitmap> | |
316 | <focus>fileopen.gif</focus> | |
317 | </object> | |
318 | </object> | |
319 | <object class="sizeritem"> | |
320 | <object class="wxPanel"> | |
321 | <object class="wxStaticText"> | |
322 | <label>fdgdfgdfgdfg</label> | |
323 | </object> | |
324 | <style>wxSUNKEN_BORDER</style> | |
325 | </object> | |
326 | <flag>wxALIGN_CENTER</flag> | |
327 | </object> | |
328 | <object class="sizeritem"> | |
329 | <object class="wxButton"> | |
330 | <label>Buttonek</label> | |
331 | </object> | |
332 | <border>10d</border> | |
333 | <flag>wxALL</flag> | |
334 | </object> | |
335 | <object class="sizeritem"> | |
336 | <object class="wxHtmlWindow"> | |
337 | <htmlcode><h1>Hi,</h1>man</htmlcode> | |
338 | <size>100,45d</size> | |
339 | </object> | |
340 | </object> | |
341 | <object class="sizeritem"> | |
342 | <object class="wxNotebook"> | |
343 | <object class="notebookpage"> | |
344 | <object class="wxPanel"> | |
345 | <object class="wxBoxSizer"> | |
346 | <object class="sizeritem"> | |
347 | <object class="wxHtmlWindow"> | |
348 | <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode> | |
349 | <size>50,50d</size> | |
350 | </object> | |
351 | <option>1</option> | |
352 | </object> | |
353 | </object> | |
354 | </object> | |
355 | <label>Page</label> | |
356 | </object> | |
357 | <object class="notebookpage"> | |
358 | <object class="wxPanel"> | |
359 | <object class="wxBoxSizer"> | |
360 | <object class="sizeritem"> | |
361 | <object class="wxHtmlWindow"> | |
362 | <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode> | |
363 | <size>50,50d</size> | |
364 | </object> | |
365 | </object> | |
366 | </object> | |
367 | </object> | |
368 | <label>Page 2</label> | |
369 | </object> | |
370 | <usenotebooksizer>1</usenotebooksizer> | |
371 | </object> | |
372 | <flag>wxEXPAND</flag> | |
373 | </object> | |
374 | <orient>wxVERTICAL</orient> | |
375 | </object> | |
376 | </object> | |
377 | <object class="wxDialog" name="dlg2"> | |
378 | <object class="wxBoxSizer"> | |
379 | <orient>wxVERTICAL</orient> | |
380 | <object class="sizeritem" name="dfgdfg"> | |
381 | <object class="wxTextCtrl"> | |
382 | <size>200,200d</size> | |
383 | <style>wxTE_MULTILINE|wxSUNKEN_BORDER</style> | |
384 | <value>Hello, this is an ordinary multiline\n textctrl....</value> | |
385 | </object> | |
386 | <option>1</option> | |
387 | <flag>wxEXPAND|wxALL</flag> | |
388 | <border>10</border> | |
389 | </object> | |
390 | <object class="sizeritem"> | |
391 | <object class="wxBoxSizer"> | |
392 | <object class="sizeritem"> | |
393 | <object class="wxButton" name="wxID_OK"> | |
394 | <label>Ok</label> | |
395 | <default>1</default> | |
396 | </object> | |
397 | </object> | |
398 | <object class="sizeritem"> | |
399 | <object class="wxButton" name="wxID_CANCEL"> | |
400 | <label>Cancel</label> | |
401 | </object> | |
402 | <border>10</border> | |
403 | <flag>wxLEFT</flag> | |
404 | </object> | |
405 | </object> | |
406 | <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag> | |
407 | <border>10</border> | |
408 | </object> | |
409 | </object> | |
410 | <title>Second testing dialog</title> | |
411 | </object> | |
412 | </resource> | |
413 | \end{verbatim} | |
414 | ||
415 | \subsection{XRC file format}\label{xrcfileformat} | |
416 | ||
417 | Please see Technical Note 14 (docs/tech/tn0014.txt) in your wxWidgets | |
418 | distribution. | |
419 | ||
420 | \subsection{C++ header file generation}\label{xrccppheader} | |
421 | ||
422 | Using the {\tt -e} switch together with {\tt -c}, a C++ header file is written | |
423 | containing class definitions for the GUI windows defined in the XRC file. | |
424 | This code generation can make it easier to use XRC and automate program | |
425 | development. | |
426 | The classes can be used as basis for development, freeing the | |
427 | programmer from dealing with most of the XRC specifics (e.g. {\tt XRCCTRL}). | |
428 | ||
429 | For each top level window defined in the XRC file a C++ class definition is | |
430 | generated, containing as class members the named widgets of the window. | |
431 | A default constructor for each class is also generated. Inside the constructor | |
432 | all XRC loading is done and all class members representing widgets are initialized. | |
433 | ||
434 | A simple example will help understand how the scheme works. Suppose you have | |
435 | a XRC file defining a top level window {\tt TestWnd\_Base}, which subclasses {\tt wxFrame} (any | |
436 | other class like {\tt wxDialog} will do also), and has subwidgets {\tt wxTextCtrl} A and {\tt wxButton} B. | |
437 | The XRC file and corresponding class definition in the header file will be something like: | |
438 | ||
439 | \begin{verbatim} | |
440 | <?xml version="1.0"?> | |
441 | <resource version="2.3.0.1"> | |
442 | <object class="wxFrame" name="TestWnd_Base"> | |
443 | <size>-1,-1</size> | |
444 | <title>Test</title> | |
445 | <object class="wxBoxSizer"> | |
446 | <orient>wxHORIZONTAL</orient> | |
447 | <object class="sizeritem"> | |
448 | <object class="wxTextCtrl" name="A"> | |
449 | <label>Test label</label> | |
450 | </object> | |
451 | </object> | |
452 | <object class="sizeritem"> | |
453 | <object class="wxButton" name="B"> | |
454 | <label>Test button</label> | |
455 | </object> | |
456 | </object> | |
457 | </object> | |
458 | </object> | |
459 | </resource> | |
460 | ||
461 | ||
462 | class TestWnd_Base : public wxFrame { | |
463 | protected: | |
464 | wxTextCtrl* A; | |
465 | wxButton* B; | |
466 | ||
467 | private: | |
468 | void InitWidgetsFromXRC(){ | |
469 | wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame"); | |
470 | A = XRCCTRL(*this,"A",wxTextCtrl); | |
471 | B = XRCCTRL(*this,"B",wxButton); | |
472 | } | |
473 | public: | |
474 | TestWnd::TestWnd(){ | |
475 | InitWidgetsFromXRC(); | |
476 | } | |
477 | }; | |
478 | \end{verbatim} | |
479 | ||
480 | The generated window class can be used as basis for the full window class. The | |
481 | class members which represent widgets may be accessed by name instead of using | |
482 | {\tt XRCCTRL} every time you wish to reference them (note that they are {\tt protected} class members), | |
483 | though you must still use {\tt XRCID} to refer to widget IDs in the event | |
484 | table. | |
485 | ||
486 | Example: | |
487 | ||
488 | \begin{verbatim} | |
489 | #include "resource.h" | |
490 | ||
491 | class TestWnd : public TestWnd_Base { | |
492 | public: | |
493 | TestWnd(){ | |
494 | // A, B already initialised at this point | |
495 | A->SetValue("Updated in TestWnd::TestWnd"); | |
496 | B->SetValue("Nice :)"); | |
497 | } | |
498 | void OnBPressed(wxEvent& event){ | |
499 | Close(); | |
500 | } | |
501 | DECLARE_EVENT_TABLE(); | |
502 | }; | |
503 | ||
504 | BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base) | |
505 | EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed) | |
506 | END_EVENT_TABLE() | |
507 | ||
508 | \end{verbatim} | |
509 | ||
510 | ||
511 | ||
512 | \subsection{Adding new resource handlers}\label{newresourcehandlers} | |
513 | ||
514 | Adding a new resource handler is pretty easy. | |
515 | Typically, to add an handler for the {\tt MyControl} class, you'll want to create | |
516 | the {\tt xh_mycontrol.h} {\tt xh_mycontrol.cpp} files. | |
517 | ||
518 | The header needs to contains the {\tt MyControlXmlHandler} class definition: | |
519 | ||
520 | \begin{verbatim} | |
521 | class MyControlXmlHandler : public wxXmlResourceHandler | |
522 | { | |
523 | public: | |
524 | ||
525 | // Constructor. | |
526 | MyControlXmlHandler(); | |
527 | ||
528 | // Creates the control and returns a pointer to it. | |
529 | virtual wxObject *DoCreateResource(); | |
530 | ||
531 | // Returns true if we know how to create a control for the given node. | |
532 | virtual bool CanHandle(wxXmlNode *node); | |
533 | ||
534 | // Register with wxWidgets' dynamic class subsystem. | |
535 | DECLARE_DYNAMIC_CLASS(MyControlXmlHandler) | |
536 | }; | |
537 | \end{verbatim} | |
538 | ||
539 | The implementation of your custom XML handler will typically look as: | |
540 | ||
541 | \begin{verbatim} | |
542 | // Register with wxWidgets' dynamic class subsystem. | |
543 | IMPLEMENT_DYNAMIC_CLASS(MyControlXmlHandler, wxXmlResourceHandler) | |
544 | ||
545 | MyControlXmlHandler::MyControlXmlHandler() | |
546 | { | |
547 | // this call adds support for all wxWindows class styles | |
548 | // (e.g. wxSIMPLE_BORDER, wxSUNKEN_BORDER, wxWS_EX_* etc etc) | |
549 | AddWindowStyles(); | |
550 | ||
551 | // if MyControl class supports e.g. MYCONTROL_DEFAULT_STYLE | |
552 | // you should use: | |
553 | // XRC_ADD_STYLE(MYCONTROL_DEFAULT_STYLE); | |
554 | } | |
555 | ||
556 | wxObject *MyControlXmlHandler::DoCreateResource() | |
557 | { | |
558 | // the following macro will init a pointer named "control" | |
559 | // with a new instance of the MyControl class, but will NOT | |
560 | // Create() it! | |
561 | XRC_MAKE_INSTANCE(control, MyControl) | |
562 | ||
563 | // this is the point where you'll typically need to do the most | |
564 | // important changes: here the control is created and initialized. | |
565 | // You'll want to use the wxXmlResourceHandler's getters to | |
566 | // do most of your work. | |
567 | // If e.g. the MyControl::Create function looks like: | |
568 | // | |
569 | // bool MyControl::Create(wxWindow *parent, int id, | |
570 | // const wxBitmap &first, const wxPoint &posFirst, | |
571 | // const wxBitmap &second, const wxPoint &posSecond, | |
572 | // const wxString &theTitle, const wxFont &titleFont, | |
573 | // const wxPoint &pos, const wxSize &size, | |
574 | // long style = MYCONTROL_DEFAULT_STYLE, | |
575 | // const wxString &name = wxT("MyControl")); | |
576 | // | |
577 | // then the XRC for your component should look like: | |
578 | // | |
579 | // <object class="MyControl" name="some_name"> | |
580 | // <first-bitmap>first.xpm</first-bitmap> | |
581 | // <second-bitmap>text.xpm</second-bitmap> | |
582 | // <first-pos>3,3</first-pos> | |
583 | // <second-pos>4,4</second-pos> | |
584 | // <the-title>a title</the-title> | |
585 | // <title-font> | |
586 | // <!-- the standard XRC tags for describing a font: <size>, <style>, <weight>, etc --> | |
587 | // </title-font> | |
588 | // <!-- XRC also accepts other usual tags for wxWindow-derived classes: | |
589 | // like e.g. <name>, <style>, <size>, <position>, etc --> | |
590 | // </object> | |
591 | // | |
592 | // and the code to read your custom tags from the XRC file is just: | |
593 | control->Create(m_parentAsWindow, GetID(), | |
594 | GetBitmap(wxT("first-bitmap")), | |
595 | GetPosition(wxT("first-pos")), | |
596 | GetBitmap(wxT("second-bitmap")), | |
597 | GetPosition(wxT("second-pos")), | |
598 | GetText(wxT("the-title")), | |
599 | GetFont(wxT("title-font")), | |
600 | GetPosition(), GetSize(), GetStyle(), GetName()); | |
601 | ||
602 | SetupWindow(control); | |
603 | ||
604 | return control; | |
605 | } | |
606 | ||
607 | bool MyControlXmlHandler::CanHandle(wxXmlNode *node) | |
608 | { | |
609 | // this function tells XRC system that this handler can parse | |
610 | // the <object class="MyControl"> tags | |
611 | return IsOfClass(node, wxT("MyControl")); | |
612 | } | |
613 | \end{verbatim} | |
614 | ||
615 | You may want to check the \helpref{wxXmlResourceHandler}{wxxmlresourcehandler} documentation | |
616 | to see how many built-in getters it contains. It's very easy to retrieve also complex structures | |
617 | out of XRC files using them. | |
618 |