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="2.3.0.1">
    <object class="wxFrame" name="TestWnd_Base">
        <size>-1,-1</size>
        <title>Test</title>
        <object class="wxBoxSizer">
            <orient>wxHORIZONTAL</orient>
            <object class="sizeritem">
                <object class="wxTextCtrl" name="A">
                    <label>Test label</label>
                </object>
            </object>
            <object class="sizeritem">
                <object class="wxButton" name="B">
                    <label>Test button</label>
                </object>
            </object>
        </object>
    </object>
</resource>


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

private:
 void InitWidgetsFromXRC(){
  wxXmlResource::Get()->LoadObject(this,NULL,"TestWnd","wxFrame");
  A = XRCCTRL(*this,"A",wxTextCtrl);
  B = XRCCTRL(*this,"B",wxButton);
 }
public:
TestWnd::TestWnd(){
  InitWidgetsFromXRC();
 }
};

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.

Example:

#include "resource.h"

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

BEGIN_EVENT_TABLE(TestWnd,TestWnd_Base)
EVT_BUTTON(XRCID("B"),TestWnd::OnBPressed)
END_EVENT_TABLE()

ymasuda 平成17年11月19日