Table of Contents

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:

Recompiling and linking an application is not necessary if the resources change.

If you use a dialog designer that generates C++ code, it can be hard to reintegrate this into existing C++ code. Separation of resources and code is a more elegant solution.

You can choose between different alternative resource files at run time, if necessary.

The XRC format uses sizers for flexibility, allowing dialogs to be resizable and highly portable.

The XRC format is a wxWidgets standard, and can be generated or postprocessed by any program that understands it. As it is based on the XML standard, existing XML editors can be used for simple editing purposes.

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.

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:

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:

<objectclass="wxTextCtrl" name="text" subclass="MyTextCtrl"/>

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.

The XRC sample

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.

Binary Resource Files

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:

-h (--help): Show a help message.

-v (--verbose): Show verbose logging information.

-c (--cpp-code): Write C++ source rather than a XRS file.

-e (--extra-cpp-code): If used together with -c, generates C++ header file containing class definitions for the windows defined by the XRC file (see special subsection).

-u (--uncompressed): Do not compress XML files (C++ only).

-g (--gettext): Output underscore-wrapped strings that poEdit or gettext can scan. Outputs to stdout, or a file if -o is used.

-n (--function) <name>: Specify C++ function name (use with -c).

-o (--output) <filename>: Specify the output file, such as resource.xrs or resource.cpp.

-l (--list-of-handlers) <filename>: Output a list of necessary handlers to this file.

For example:

$ wxrc resource.xrc

$ wxrc resource.xrc -o resource.xrs

$ wxrc resource.xrc -v -c -o resource.cpp

Note

XRS file is essentially a renamed ZIP archive which means that you can manipulate it with standard ZIP tools. Note that if you are using XRS files, you have to initialize the wxFileSystem archive handler first! It is a simple thing to do:

Using Embedded Resources

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.

Use the -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:

externvoid InitXmlResource(); // defined in generated file

...

wxXmlResource::Get()->InitAllHandlers();

InitXmlResource();

...

C++ header file generation

Using the -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. XRCCTRL).

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.