XRC – The XML Based Resource System in wxWidgets

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 be compiled in to two formats.
    1. XRS : To store all resources in a single file.
    2. C++ : To embed resource into executable.

Why do we use XRC

  • Resource change doesn’t need recompiling and linking.
  • Separation of resources and code.
  • 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.

XRC Basics

  • Include Header : wx/xrc/xmlres.h.
  • wxFileSystem archive handler is needed for XRS files. Install wxFileSystem archive handler first with wxFileSystem::AddHandler(new wxArchiveFSHandler);
  • To load resource file
    • From your wxApp::OnInit() function call wxXmlResource::Get()->InitAllHandlers()
    • Then call wxXmlResource::Get()->Load(“myfile.xrc”)
  • to create a dialog from a resource, create it using the default constructor, and then load it. For example wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
  • set up event tables as usual but use the XRCID(str) macro to translate from XRC string names to a suitable integer identifier, for exampleEVT_MENU(XRCID("quit"), MyFrame::OnQuit).
XRC File Creation

To create an XRC file, you can use one of the following methods.

  • Create the file by hand;
  • use wxFormBuilder, Open source designer supporting C++, Python and XRC.
  • use DialogBlocks, a commercial dialog editor;
  • use XRCed, a wxPython-based dialog editor that you can find in the wxPython/tools subdirectory of the wxWidgets CVS archive;
  • use wxGlade, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
  • convert WIN32 RC files to XRC with the tool in contrib/utils/convertrc.

A complete list of third-party tools that write to XRC can be found at www.wxwidgets.org/wiki/index.php/Tools.

You can use wxXmlResource::Load in a number of ways. You can pass an XRC file (XML-based text resource file) or a zip-compressed file (extension ZIP or XRS) containing other XRC.