Using Custom Control in XRC Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
annamalai
In need of some credit
In need of some credit
Posts: 8
Joined: Fri Feb 22, 2013 6:32 am

Using Custom Control in XRC

Post by annamalai » Fri Feb 22, 2013 7:06 am

Hi,
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"))
return;
pLabel = XRCCTRL(dlg, "m_staticText1", wxStaticText);
----------------------------------------------------------------------------------------------------------------------------
// for referring my custom control "CustomBook" from XRC
----------------------------------------------------------------------------------------------------------------------------
CustomBook* m_book;
wxXmlResource::Get()->LoadObject(m_book,&dlg,"my_book","CustomBook");
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,
Annamalai
Last edited by annamalai on Sat Feb 23, 2013 11:34 am, edited 1 time in total.

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: Using Custom Control in XRC

Post by Radek » Sat Feb 23, 2013 5:05 am

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).
(3)

Code: Select all

wxXmlResource::Get()->AttachUnknownControl(
                                            wxT("name_of_the_custom_control_in_XRC"),
                                            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.
Last edited by DavidHart on Mon Feb 25, 2013 12:24 pm, edited 1 time in total.
Reason: Changed dialog name for confidentiality reasons

annamalai
In need of some credit
In need of some credit
Posts: 8
Joined: Fri Feb 22, 2013 6:32 am

Re: Using Custom Control in XRC

Post by annamalai » Mon Feb 25, 2013 1:25 pm

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.......

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: Using Custom Control in XRC

Post by Radek » Mon Feb 25, 2013 3:13 pm

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().

Post Reply