Subclassing XRC
From WxPerl wiki
What doumentation there is for custom controls and the XRC resource system in wxPerl consists of a couple of letters in the wxperl-users archive and the examples in demo/XRCCustom.pl. The latter addresses only custom controls attached to XRC Unknown objects. If you just want to add (or remove) some functionality to(from) an existing control, it's much simpler.
First, you need a very simple package:
package XRCFactory;
use strict;
use Wx;
use base "Wx::XmlSubclassFactory";
sub Create {
return $_[1]->new;
}
Then you need the following one-liner among your xrc initialization code:
Wx::XmlResource::AddSubclassFactory( XRCFactory->new );
And finally you need your subclass. If you leave out function new() altogether, it will work out just fine. If you have a function new(), it must be able to call SUPER::new with NO arguments:
package MySubclass;
use strict;
use Wx;
use base "Wx::TextCtrl";
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
#Whatever other stuff needs to be done...
return $self;
}
The reason seems to be that XRC uses the two-stage creation process, and if you try to force arguments in the constructor, it dies for want of a parent. Note that even if you override Create, XmlResourceHandler won't use it – at least not with the XmlSubclassFactory demonstrated above. That means that if you need to set additional parameters on an object created through XRC, you'll have to do so explicitly after XRC has finished.
Once you have this coded, you simply use the subclass='MySubclass' when declaring the widget in your XrcResource:
<object class="wxPanel" name="Repository" subclass="MySubClass">
Now when you load the resource, the subclass will be created instead of the built-in widget.
