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:

<?xml version="1.0"?>
<resource version="">
    <object class="wxFrame" name="TestWnd_Base">
        <object class="wxBoxSizer">
            <object class="sizeritem">
                <object class="wxTextCtrl" name="A">
                    <label>Test label</label>
            <object class="sizeritem">
                <object class="wxButton" name="B">
                    <label>Test button</label>

class TestWnd_Base : public wxFrame {
 wxTextCtrl* A;
 wxButton* B;

 void InitWidgetsFromXRC(){
  A = XRCCTRL(*this,"A",wxTextCtrl);
  B = XRCCTRL(*this,"B",wxButton);

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.


#include "resource.h"

class TestWnd : public TestWnd_Base {
			// A, B already initialised at this point
			A->SetValue("Updated in TestWnd::TestWnd");
			B->SetValue("Nice :)");
		void OnBPressed(wxEvent& event){


ymasuda 平成17年11月19日