antmico has asked for the wisdom of the Perl Monks concerning the following question:

I'm a Perl novice and I'm trying to develop my skills by building a small GUI application using wxperl. Although this question does relate to wxperl, I suspect it is equally a fundamental OO question.

I'd like to make use of the wxVListBox control and to do so I understand that I must derive from it a subclass, however this has left me with a couple of questions.

Firstly, the demo code appears to derive from a class called Wx::PlVListBox rather than Wx::VListBox, so I'm not sure which of the two I should be using? I've experimented with both and if I base my subclass on Wx:VListBox the OnDrawItem method doesn't seem to get called.

If I base my subclass on Wx::PlVListBox, the control appears to work as expected but I'm not sure how I go about adding additional attributes given I have no knowledge of how the underlying object is stored? Many thanks for any assistance.

My test code:

#!/usr/bin/perl use strict; use warnings; package MyFrame; use Wx; use base qw(Wx::Frame); sub new { my $class = shift; my $self = $class->SUPER::new( undef, # parent window -1, # ID -1 means any 'wxPerl rules', # title [-1, -1], # default position [100, 200], # size ); my $panel = Wx::Panel->new( $self, # parent window -1, # ID ); my $vlistbox = MyWxVListBox->new( $self, -1, [-1, -1], [100, 200] +); return $self; } package MyApp; use base qw(Wx::App); sub OnInit { my $class = shift; my $frame = MyFrame->new(); $frame->Show( 1 ); } package MyWxVListBox; use base qw(Wx::PlVListBox); use Wx; sub new { my( $class, @args ) = @_; my $self = $class->SUPER::new( @args ); $self->SetItemCount( 10 ); return $self; } sub OnDrawItem { my( $self, $dc, $rect, $item ) = @_; print "Drawing item\n"; $dc->DrawText( $item, $rect->x + 1, $rect->y + 1); } sub OnMeasureItem { my( $self, $item ) = @_; return 20; } package main; my $app = MyApp->new(); $app->MainLoop;

Replies are listed 'Best First'.
Re: wxperl subclassing Wx::VListBox
by Anonymous Monk on Feb 12, 2011 at 12:18 UTC
    Firstly, the demo code appears to derive from a class called Wx::PlVListBox rather than Wx::VListBox, so I'm not sure which of the two I should be using?
    As you found out from demo/testing, you should be using Wx::PLVListBox :) Wx::NewClass explains the reason
    For abstract classes, XS++ will create an additional Perl-level class, called  Wx::Pl<classname>; in order to override the virtual methods, you must derive from this class, and not from  Wx::<classname>.
    but I'm not sure how I go about adding additional attributes given I have no knowledge of how the underlying object is stored?
    If you add use DDS; Dump($vlistbox); you might see (what I see) $Wx_VListBox1 = bless( \do { my $v = 14356976 }, 'Wx::VListBox' );, so you can't add attributes to it.

    I'm not sure if this is intended or desireable :)

      Many thanks for the clarification.
Re: wxperl subclassing Wx::VListBox
by Anonymous Monk on Feb 12, 2011 at 01:57 UTC
    Firstly, the demo code appears to derive from a class called Wx::PlVListBox rather than Wx::VListBox

    What demo code?

      The sample code included in Wx::Demo.