Page 1 of 1

Using Custom Control in XRC

Posted: Fri Feb 22, 2013 7:06 am
by annamalai
I am developing an XRC version of wxWidgets form design in wxFormBuilder. When i am using built in class like wxButton, wxStaticText in XRC and reading the elements in .cpp, it worked fine. I tried the same method for using custom control in XRC. But, i am not sure, whether its the proper way to handle them. I am using a custom navigation enabled wxBookCtrlBase, as my parent class, for developing my custom control. Also, i don't have the XRC definition for the developed custom control.

At present, My code is as follows : Code after initializing handlers & loading XRC

// for referring static text from XRC
wxDialog dlg;
if (!wxXmlResource::Get()->LoadDialog(&dlg, NULL, "MyDialog"))
pLabel = XRCCTRL(dlg, "m_staticText1", wxStaticText);
// for referring my custom control "CustomBook" from XRC
CustomBook* m_book;
m_book = XRCCTRL(dlg,"my_book",CustomBook);
and my XRC code is as like : <object name="my_book" class="CustomBook"/>

Let me know, what are the steps to be followed, while creating XRC with custom controls and reading them in .cpp.

Thanks in advance,

Re: Using Custom Control in XRC

Posted: Sat Feb 23, 2013 5:05 am
by Radek
The simplest (and most flexible) method is using AttachUnknownControl().

(1) In the wxFormBuilder, create a "custom" control and give it class "custom" (the class must be "custom").
(2) In the MyDialog ctor create the CustomBook (using new).

Code: Select all

                                            pointer_to your_CustomBook,
                                            pointer_to_parent_of_CustomBook               // pointer to the dialog
Since now, the custom control is integrated into the XRC system. You can forget the pointer to the custom control now, it can be got by XRCCTRL. The ID of the control can be got by XRCID. The control will be displayed at the place of the "unknown" control. If the CustomBook is a child of the dialog, the CustomBook get destroyed automatically when the dialog get destroyed.

You can attach any custom control this way as long as the control is a descendant of wxWindow.

Another approach is using "subclass" ability of wxFormBuilder. You need to create a control of the base class of CustomBook in wxFormBuilder and give it a subclass "CustomBook". The class CustomBook now needs to follow certain rules (see wxWiki). IMO, it's less flexible than AttachUnknownControl() because of the rules and because of necessity of having the base class of CustomBook in the wxFormBuilder.

Re: Using Custom Control in XRC

Posted: Mon Feb 25, 2013 1:25 pm
by annamalai
Hi Radek,
Thanks for the Reply. Its more useful to us. And can u explain more about handling XRC Custom control events and defining it with XRC file.....
If i define it with XRC like,
<object class="custom" name="m_devBook" />
is it enough for handling events or any more needed with defining base class.......

Re: Using Custom Control in XRC

Posted: Mon Feb 25, 2013 3:13 pm
by Radek
Nothing more is needed. No need to specify a base class or something similar. If you have wxFormBuilder at hand, try to create a frame and put a wxRichTextCtrl into it. Check what you get in the XRC file and what code will be generated. wxRichTextCtrl isn't known to XRC so that wxFormBuilder smuggles a wxRichTextCtrl into the XRC file using AttachUnknownControl().