|
From: Mark W. <ma...@ne...> - 2006-11-08 06:29:39
|
Hi John
I have been using the method detailed in the URL you mentioned ... this
works fine for loading derived classes using XRC subclassing BUT has some
limitations described in my previous email and in the URL I mentioned. I
have attached my derived class of Wx::TreeCtrl
I have had many occasions where I would like to perform some further
initialisation of a control loaded by XRC based on the contents of other
controls on a dialog but have been unable to. This is a solution to that
problem. Perhaps you do not encounter these problems on your platform /
build?
I do not profess to be an expert maybe I have missed something. I hope I
have supplied enough information for you to see the problem.
Regards
Mark
<snip>
#
# this file MyTreeCtrl is automatically loaded
# by XRC using the subclass mechanism as:
# <object class="wxTreeCtrl" name="MyTree" subclass="MyTreeCtrl">
#
package MyTreeCtrl;
use strict;
use base qw( Wx::TreeCtrl );
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
# these commands just have NO effect
my $root = $self->AddRoot( 'Root item' );
$self->AppendItem( $root, 'Node #1' );
$self->AppendItem( $root, 'Node #2' );
$self->Expand( $root );
# the GetParent() does not return anything YET!
my $parent = $self->GetParent();
print "Parent = $parent\n";
EVT_CREATE( $self, \&OnCreateWindow );
return $self;
}
sub OnCreateWindow {
my ( $self, $event ) = @_;
# if I put the commands here - THEY WORK!
my $root = $self->AddRoot( 'Root item' );
$self->AppendItem( $root, 'Node #1' );
$self->AppendItem( $root, 'Node #2' );
$self->Expand( $root );
# here the GetParent() does return the correct control!
my $parent = $self->GetParent();
print "Parent = $parent\n";
}
sub EVT_CREATE($$) { $_[0]->Connect( -1, -1, &Wx::wxEVT_CREATE, $_[1] ) }
1;
</snip>
_____
From: wxp...@li...
[mailto:wxp...@li...] On Behalf Of John Ralls
Sent: 07 November 2006 06:50 PM
To: wxperl-users
Subject: Re: [wxperl-users] XRC Subclassing and control initialisation
On Nov 7, 2006, at 6:03 AM, Mark Wardell wrote:
Hi All,
**Warning - fairly long post - patience required.
In another attempt at using subclassed control in XRC I come unstuck with
code initialisation.
In the new() method the windows have not been created so the window
hierarchy does not exist, so calls like GetParent() do not work. I tried to
init() my derived TreeCtrl by putting AddRoot() and AppendItem() calls in
the new() method but this did not work (nothing appears in the tree).
So I started looking around and found this page below in the wxPython wiki
which sums up the situation nicely. I recommend this wiki to all wxPerl
people as it contains a huge amount of information that can be adapted for
wxPerl developers, I often visit looking for new info.
http://wiki.wxpython.org/index.cgi/SubclassingListCtrlWithXrc
The key area here is binding to the EVT_WINDOW_CREATE event. I could not
find this in the wxPerl sources but there is a EVT_CREATE event defined on
line 929 in Constant.xs. So I created the following sub in my class as is
the normal way as seen in Event.pm:
sub EVT_CREATE($$) { $_[0]->Connect( -1, -1, &Wx::wxEVT_CREATE, $_[1] ) }
So now in my code I can bind to the event using:
EVT_CREATE( $self, \&OnCreateWindow );
Then I can put my code in my derived class like below:
sub OnCreateWindow { # own code goes here }
This seems to work perfectly although I haven't had much time to test and
find any pitfalls.
Has anyone alse tried using this technique?
Does this sound like a reasonable solution Mattia? (Nobody understand the
event tables and refcounting like you)
If I find this works without problems then I will look at a generic way of
providing this functionality for ALL objects created using XRC subclassing.
Hope this helps others that find themselves in a similar position.
Regards
Mark
Mark,
Sounds too complicated, so I must be missing something. Could you post some
code snippets which show why the approach documented in
http://wxperl.pvoice.org/kwiki/index.cgi?SubclassingXRC
doesn't work?
Regards,
John Ralls
|