The XML-based resource system, known as XRC, allows user interface elements such as dialogs, menu bars and toolbars, to be stored in text files and loaded into the application at run-time.
XRC files can also be compiled into binary XRS files or C++ code (the former makes it possible to store all resources in a single file and the latter is useful when you want to embed the resources into the executable).
There are several advantages to using XRC resources:
XRC was written by Vaclav Slavik.
Creating an XRC file
You will need to write an XRC file. Though this can be done by hand in a text editor, for all but the smallest files it is advisable to use a specialised tool. Examples of these include:
There's a more complete list at https://wiki.wxwidgets.org/Tools
This small demonstration XRC file contains a simple dialog:
You can keep all your XRC elements together in one file, or split them between several.
Loading XRC files
Before you can use XRC in an app, it must first be loaded. This code fragment shows how to load a single XRC file "resource.xrc" from the current working directory, plus all the *.xrc files contained in the subdirectory "rc".
It's normal to load any XRC files at the beginning of an app. Though it is possible to unload a file later, it's seldom necessary.
Using an XRC item
The XRC file(s) are now loaded into the app's virtual filesystem. From there, you must do another sort of load when you want to use an individual object. Yes, it's confusingly named, but you first Load() the file, and later load each top-level object when its needed.
This is how you would use the above simple dialog in your code.
See how simple the code is. All the instantiation is done invisibly by the XRC system.
Accessing XRC child controls
The last section showed how to load top-level windows like dialogs, but what about child windows like the wxTextCtrl named "text" that the dialog contains? You can't 'load' an individual child control in the same way. Instead you use the XRCCTRL macro to get a pointer to the child. To expand the previous code:
XRCCTRL takes a reference to the parent container and uses wxWindow::FindWindow to search inside it for a wxWindow with the supplied name (here "text"). It returns a pointer to that control, cast to the type in the third parameter; so a similar effect could be obtained by writing:
XRC and IDs
The ID of a control is often needed, e.g. for use in an event table or with wxEvtHandler::Bind. It can easily be found by passing the name of the control to the XRCID macro:
A few points to note:
Subclassing in XRC
You will often want to use subclassed wx controls in your code. There are three ways to do this from XRC:
Suppose you wanted the wxTextCtrl named "text" to be created as your derived class MyTextCtrl. The only change needed in the XRC file would be in this line:
The only change in your code would be to use MyTextCtrl in XRCCTRL. However for the subclass to be created successfully, it's important to ensure that it uses wxWidget's RTTI mechanism: see Subclassing for the details.
A major resource for learning how to use XRC is the XRC Sample. This demonstrates all of the standard uses of XRC, and some of the less common ones. It is strongly suggested that you run it, and look at the well-commented source code to see how it works.
To compile binary resource files, use the command-line
wxrc utility. It takes one or more file parameters (the input XRC files) and the following switches and options:
It is sometimes useful to embed resources in the executable itself instead of loading an external file (e.g. when your app is small and consists only of one exe file). XRC provides means to convert resources into regular C++ file that can be compiled and included in the executable.
-c switch to
wxrc utility to produce C++ file with embedded resources. This file will contain a function called
InitXmlResource (unless you override this with a command line switch). Use it to load the resource:
-e switch together with
-c, a C++ header file is written containing class definitions for the GUI windows defined in the XRC file. This code generation can make it easier to use XRC and automate program development. The classes can be used as basis for development, freeing the programmer from dealing with most of the XRC specifics (e.g.
For each top level window defined in the XRC file a C++ class definition is generated, containing as class members the named widgets of the window. A default constructor for each class is also generated. Inside the constructor all XRC loading is done and all class members representing widgets are initialized.
A simple example will help understand how the scheme works. Suppose you have a XRC file defining a top level window
TestWnd_Base, which subclasses wxFrame (any other class like
wxDialog will do also), and has subwidgets wxTextCtrl A and wxButton B.
The XRC file and corresponding class definition in the header file will be something like:
The generated window class can be used as basis for the full window class. The class members which represent widgets may be accessed by name instead of using
XRCCTRL every time you wish to reference them (note that they are
protected class members), though you must still use
XRCID to refer to widget IDs in the event table.
It is also possible to access the wxSizerItem of a sizer that is part of a resource. This can be done using
XRCSIZERITEM as shown.
The resource file can have something like this for a sizer item.
The code can then access the sizer item by using
Adding a new resource handler is pretty easy.
Typically, to add an handler for the
MyControl class, you'll want to create the
The header needs to contains the
MyControlXmlHandler class definition:
The implementation of your custom XML handler will typically look as:
You may want to check the wxXmlResourceHandler documentation to see how many built-in getters it contains. It's very easy to retrieve also complex structures out of XRC files using them.