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