]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/xrc.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 @page overview_xrc XML Based Resource System (XRC)
13 Classes: wxXmlResource, wxXmlResourceHandler
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
22 There are several advantages to using XRC resources:
24 @li Recompiling and linking an application is not necessary if the resources
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
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.
37 XRC was written by Vaclav Slavik.
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
44 @li @ref overview_xrc_cppheader
45 @li @ref overview_xrc_newresourcehandlers
47 See also the separate @ref overview_xrcformat page for details about the XRC file format.
52 @section overview_xrc_concepts XRC Concepts
54 These are the typical steps for using XRC files in your application.
56 @li Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice.
57 @li If you are going to use XRS files (see
58 @ref overview_xrc_binaryresourcefiles), install wxFileSystem archive
59 handler first with wxFileSystem::AddHandler(new wxArchiveFSHandler);
60 @li Call wxXmlResource::Get()->InitAllHandlers() from your wxApp::OnInit
61 function, and then call wxXmlResource::Get()->Load("myfile.xrc") to load
63 @li To create a dialog from a resource, create it using the default
64 constructor, and then load it. For example:
65 wxXmlResource::Get()->LoadDialog(dlg, this, "dlg1");
66 @li Set up event tables as usual but use the XRCID(str) macro to translate from
67 XRC string names to a suitable integer identifier, for example
68 <tt>EVT_MENU(XRCID("quit"), MyFrame::OnQuit)</tt>.
70 To create an XRC file, you can use one of the following methods.
72 @li Create the file by hand.
73 @li Use wxDesigner <http://www.roebling.de/>, a commercial dialog designer/RAD
75 @li Use DialogBlocks <http://www.anthemion.co.uk/dialogblocks/>, a commercial
77 @li Use XRCed <http://xrced.sf.net/>, a wxPython-based dialog editor that you
78 can find in the wxPython/tools subdirectory of the wxWidgets SVN archive.
79 @li Use wxGlade <http://wxglade.sf.net/>, a GUI designer written in wxPython.
80 At the moment it can generate Python, C++ and XRC.
82 A complete list of third-party tools that write to XRC can be found at
83 <http://www.wxwidgets.org/wiki/index.php/Tools>.
85 It is highly recommended that you use a resource editing tool, since it's
86 fiddly writing XRC files by hand.
88 You can use wxXmlResource::Load in a number of ways. You can pass an XRC file
89 (XML-based text resource file) or a zip-compressed file (see
90 @ref overview_xrc_binaryresourcefiles), with extension ZIP or XRS, containing
93 You can also use embedded C++ resources (see
94 @ref overview_xrc_embeddedresource).
97 @section overview_xrc_binaryresourcefiles Using Binary Resource Files
99 To compile binary resource files, use the command-line @c wxrc utility. It
100 takes one or more file parameters (the input XRC files) and the following
101 switches and options:
103 @li -h (--help): Show a help message.
104 @li -v (--verbose): Show verbose logging information.
105 @li -c (--cpp-code): Write C++ source rather than a XRS file.
106 @li -e (--extra-cpp-code): If used together with -c, generates C++ header file
107 containing class definitions for the windows defined by the XRC file (see
109 @li -u (--uncompressed): Do not compress XML files (C++ only).
110 @li -g (--gettext): Output underscore-wrapped strings that poEdit or gettext
111 can scan. Outputs to stdout, or a file if -o is used.
112 @li -n (--function) @<name@>: Specify C++ function name (use with -c).
113 @li -o (--output) @<filename@>: Specify the output file, such as resource.xrs
115 @li -l (--list-of-handlers) @<filename@>: Output a list of necessary handlers
122 $ wxrc resource.xrc -o resource.xrs
123 $ wxrc resource.xrc -v -c -o resource.cpp
126 @note XRS file is essentially a renamed ZIP archive which means that you can
127 manipulate it with standard ZIP tools. Note that if you are using XRS files,
128 you have to initialize the wxFileSystem archive handler first! It is a simple
132 #include <wx/filesys.h>
133 #include <wx/fs_arc.h>
135 wxFileSystem::AddHandler(new wxArchiveFSHandler);
139 @section overview_xrc_embeddedresource Using Embedded Resources
141 It is sometimes useful to embed resources in the executable itself instead of
142 loading an external file (e.g. when your app is small and consists only of one
143 exe file). XRC provides means to convert resources into regular C++ file that
144 can be compiled and included in the executable.
146 Use the @c -c switch to @c wxrc utility to produce C++ file with embedded
147 resources. This file will contain a function called @c InitXmlResource (unless
148 you override this with a command line switch). Use it to load the resource:
151 extern void InitXmlResource(); // defined in generated file
153 wxXmlResource::Get()->InitAllHandlers();
159 @section overview_xrc_cppsample XRC C++ Sample
161 This is the C++ source file (xrcdemo.cpp) for the XRC sample.
165 #include "wx/image.h"
166 #include "wx/xrc/xmlres.h"
168 // the application icon
169 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
170 #include "rc/appicon.xpm"
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 // Define a new application type, each program should derive a class from wxApp
178 class MyApp : public wxApp
181 // override base class virtuals
182 // ----------------------------
184 // this one is called on application startup and is a good place for the
185 // app initialization (doing it here and not in the ctor allows to have an
186 // error return: if OnInit() returns false, the application terminates)
187 virtual bool OnInit();
190 // Define a new frame type: this is going to be our main frame
191 class MyFrame : public wxFrame
195 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
197 // event handlers (these functions should _not_ be virtual)
198 void OnQuit(wxCommandEvent& event);
199 void OnAbout(wxCommandEvent& event);
200 void OnDlg1(wxCommandEvent& event);
201 void OnDlg2(wxCommandEvent& event);
204 // any class wishing to process wxWidgets events must use this macro
205 DECLARE_EVENT_TABLE()
208 // ----------------------------------------------------------------------------
209 // event tables and other macros for wxWidgets
210 // ----------------------------------------------------------------------------
212 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
213 EVT_MENU(XRCID("menu_quit"), MyFrame::OnQuit)
214 EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
215 EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
216 EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
221 // ----------------------------------------------------------------------------
222 // the application class
223 // ----------------------------------------------------------------------------
225 // 'Main program' equivalent: the program execution "starts" here
228 wxImage::AddHandler(new wxGIFHandler);
229 wxXmlResource::Get()->InitAllHandlers();
230 wxXmlResource::Get()->Load("rc/resource.xrc");
232 MyFrame *frame = new MyFrame("XML resources demo",
233 wxPoint(50, 50), wxSize(450, 340));
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
243 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
244 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
246 SetIcon(wxICON(appicon));
248 SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
249 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
253 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
255 // true is to force the frame to close
259 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
262 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
263 _T("Welcome to %s"), wxVERSION_STRING);
265 wxMessageBox(msg, "About XML resources demo",
266 wxOK | wxICON_INFORMATION, this);
269 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
272 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
276 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
279 wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
285 @section overview_xrc_sample XRC Resource File Sample
287 This is the XML file (resource.xrc) for the XRC sample.
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">
296 <style>wxMENU_TEAROFF</style>
297 <object class="wxMenuItem" name="menu_about">
298 <label>_About...</label>
299 <bitmap>filesave.gif</bitmap>
301 <object class="separator"/>
302 <object class="wxMenuItem" name="menu_dlg1">
303 <label>Dialog 1</label>
305 <object class="wxMenuItem" name="menu_dlg2">
306 <label>Dialog 2</label>
308 <object class="separator"/>
309 <object class="wxMenuItem" name="menu_quit">
310 <label>E_xit\tAlt-X</label>
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>
321 <object class="tool" name="menu_save">
322 <bitmap>filesave.gif</bitmap>
323 <tooltip>Save catalog</tooltip>
325 <object class="tool" name="menu_update">
326 <bitmap>update.gif</bitmap>
327 <tooltip>Update catalog - synchronize it with sources</tooltip>
330 <object class="tool" name="menu_quotes">
331 <bitmap>quotes.gif</bitmap>
333 <tooltip>Display quotes around the string?</tooltip>
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>
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>
350 <object class="sizeritem">
351 <object class="wxPanel">
352 <object class="wxStaticText">
353 <label>fdgdfgdfgdfg</label>
355 <style>wxBORDER\_SUNKEN</style>
357 <flag>wxALIGN_CENTER</flag>
359 <object class="sizeritem">
360 <object class="wxButton">
361 <label>Buttonek</label>
366 <object class="sizeritem">
367 <object class="wxHtmlWindow">
368 <htmlcode><h1>Hi,</h1>man</htmlcode>
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>
389 <object class="notebookpage">
390 <object class="wxPanel">
391 <object class="wxBoxSizer">
392 <object class="sizeritem">
393 <object class="wxHtmlWindow">
394 <htmlcode>Hello, we are inside a <u>NOTEBOOK</u>...</htmlcode>
400 <label>Page 2</label>
403 <usenotebooksizer>1</usenotebooksizer>
406 <bitmap stock_id="wxART_QUESTION"/>
407 <bitmap stock_id="wxART_WARNING"/>
410 <flag>wxEXPAND</flag>
412 <orient>wxVERTICAL</orient>
415 <object class="wxDialog" name="dlg2">
416 <object class="wxBoxSizer">
417 <orient>wxVERTICAL</orient>
418 <object class="sizeritem" name="dfgdfg">
419 <object class="wxTextCtrl">
420 <size>200,200d</size>
421 <style>wxTE_MULTILINE|wxBORDER_SUNKEN</style>
422 <value>Hello, this is an ordinary multiline\n textctrl....</value>
425 <flag>wxEXPAND|wxALL</flag>
428 <object class="sizeritem">
429 <object class="wxBoxSizer">
430 <object class="sizeritem">
431 <object class="wxButton" name="wxID_OK">
436 <object class="sizeritem">
437 <object class="wxButton" name="wxID_CANCEL">
438 <label>Cancel</label>
444 <flag>wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT</flag>
448 <title>Second testing dialog</title>
454 @section overview_xrc_cppheader C++ header file generation
456 Using the @c -e switch together with @c -c, a C++ header file is written
457 containing class definitions for the GUI windows defined in the XRC file. This
458 code generation can make it easier to use XRC and automate program development.
459 The classes can be used as basis for development, freeing the programmer from
460 dealing with most of the XRC specifics (e.g. @c XRCCTRL).
462 For each top level window defined in the XRC file a C++ class definition is
463 generated, containing as class members the named widgets of the window. A
464 default constructor for each class is also generated. Inside the constructor
465 all XRC loading is done and all class members representing widgets are
468 A simple example will help understand how the scheme works. Suppose you have a
469 XRC file defining a top level window @c TestWnd_Base, which subclasses wxFrame
470 (any other class like @c wxDialog will do also), and has subwidgets wxTextCtrl A
473 The XRC file and corresponding class definition in the header file will be
477 <?xml version="1.0"?>
478 <resource version="2.3.0.1">
479 <object class="wxFrame" name="TestWnd_Base">
482 <object class="wxBoxSizer">
483 <orient>wxHORIZONTAL</orient>
484 <object class="sizeritem">
485 <object class="wxTextCtrl" name="A">
486 <label>Test label</label>
489 <object class="sizeritem">
490 <object class="wxButton" name="B">
491 <label>Test button</label>
499 class TestWnd_Base : public wxFrame
506 void InitWidgetsFromXRC()
508 wxXmlResource::Get()->LoadObject(this, NULL, "TestWnd", "wxFrame");
509 A = XRCCTRL(*this, "A", wxTextCtrl);
510 B = XRCCTRL(*this, "B", wxButton);
515 InitWidgetsFromXRC();
520 The generated window class can be used as basis for the full window class. The
521 class members which represent widgets may be accessed by name instead of using
522 @c XRCCTRL every time you wish to reference them (note that they are
523 @c protected class members), though you must still use @c XRCID to refer to
524 widget IDs in the event table.
529 #include "resource.h"
531 class TestWnd : public TestWnd_Base
536 // A, B already initialised at this point
537 A->SetValue("Updated in TestWnd::TestWnd");
538 B->SetValue("Nice :)");
540 void OnBPressed(wxEvent& event)
544 DECLARE_EVENT_TABLE();
547 BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
548 EVT_BUTTON(XRCID("B"), TestWnd::OnBPressed)
552 It is also possible to access the wxSizerItem of a sizer that is part of a
553 resource. This can be done using @c XRCSIZERITEM as shown.
555 The resource file can have something like this for a sizer item.
558 <object class="spacer" name="area">
559 <size>400, 300</size>
563 The code can then access the sizer item by using @c XRCSIZERITEM and @c XRCID
567 wxSizerItem* item = XRCSIZERITEM(*this, "area");
571 @section overview_xrc_newresourcehandlers Adding New Resource Handlers
573 Adding a new resource handler is pretty easy.
575 Typically, to add an handler for the @c MyControl class, you'll want to create
576 the @c xh_mycontrol.h and @c xh_mycontrol.cpp files.
578 The header needs to contains the @c MyControlXmlHandler class definition:
581 class MyControlXmlHandler : public wxXmlResourceHandler
585 MyControlXmlHandler();
587 // Creates the control and returns a pointer to it.
588 virtual wxObject *DoCreateResource();
590 // Returns true if we know how to create a control for the given node.
591 virtual bool CanHandle(wxXmlNode *node);
593 // Register with wxWidgets' dynamic class subsystem.
594 DECLARE_DYNAMIC_CLASS(MyControlXmlHandler)
598 The implementation of your custom XML handler will typically look as:
601 // Register with wxWidgets' dynamic class subsystem.
602 IMPLEMENT_DYNAMIC_CLASS(MyControlXmlHandler, wxXmlResourceHandler)
604 MyControlXmlHandler::MyControlXmlHandler()
606 // this call adds support for all wxWindows class styles
607 // (e.g. wxBORDER_SIMPLE, wxBORDER_SUNKEN, wxWS_EX_* etc etc)
610 // if MyControl class supports e.g. MYCONTROL_DEFAULT_STYLE
612 // XRC_ADD_STYLE(MYCONTROL_DEFAULT_STYLE);
615 wxObject *MyControlXmlHandler::DoCreateResource()
617 // the following macro will init a pointer named "control"
618 // with a new instance of the MyControl class, but will NOT
620 XRC_MAKE_INSTANCE(control, MyControl)
622 // this is the point where you'll typically need to do the most
623 // important changes: here the control is created and initialized.
624 // You'll want to use the wxXmlResourceHandler's getters to
625 // do most of your work.
626 // If e.g. the MyControl::Create function looks like:
628 // bool MyControl::Create(wxWindow *parent, int id,
629 // const wxBitmap &first, const wxPoint &posFirst,
630 // const wxBitmap &second, const wxPoint &posSecond,
631 // const wxString &theTitle, const wxFont &titleFont,
632 // const wxPoint &pos, const wxSize &size,
633 // long style = MYCONTROL_DEFAULT_STYLE,
634 // const wxString &name = wxT("MyControl"));
636 // Then the XRC for your component should look like:
638 // <object class="MyControl" name="some_name">
639 // <first-bitmap>first.xpm</first-bitmap>
640 // <second-bitmap>text.xpm</second-bitmap>
641 // <first-pos>3,3</first-pos>
642 // <second-pos>4,4</second-pos>
643 // <the-title>a title</the-title>
645 // <!-- Standard XRC tags for a font: <size>, <style>, <weight>, etc -->
647 // <!-- XRC also accepts other usual tags for wxWindow-derived classes:
648 // like e.g. <name>, <style>, <size>, <position>, etc -->
651 // And the code to read your custom tags from the XRC file is just:
652 control->Create(m_parentAsWindow, GetID(),
653 GetBitmap(wxT("first-bitmap")),
654 GetPosition(wxT("first-pos")),
655 GetBitmap(wxT("second-bitmap")),
656 GetPosition(wxT("second-pos")),
657 GetText(wxT("the-title")),
658 GetFont(wxT("title-font")),
659 GetPosition(), GetSize(), GetStyle(), GetName());
661 SetupWindow(control);
666 bool MyControlXmlHandler::CanHandle(wxXmlNode *node)
668 // this function tells XRC system that this handler can parse
669 // the <object class="MyControl"> tags
670 return IsOfClass(node, wxT("MyControl"));
674 You may want to check the wxXmlResourceHandler documentation to see how many
675 built-in getters it contains. It's very easy to retrieve also complex
676 structures out of XRC files using them.