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

Dear all,

It is widely known that user can create megawidgets using perlTk (either pure-perl, or complicated libraries such as tkZinc or many more)

Is it possible to create pure-perl megawidgets with wxPerl? (or may be similar concept there...))
How about Gtk?
other package?

Which other widget toolkits, available to Perl, allow this?

Please share your knowledge,
TIA

Courage, the Cowardly Dog

  • Comment on Which GUI libraries allow creation of megawidgets? (wxPerl?Gtk?)

Replies are listed 'Best First'.
Re: Which GUI libraries allow creation of megawidgets? (wxPerl?Gtk?)
by William G. Davis (Friar) on Jan 07, 2005 at 14:35 UTC

    It's possible with wxPerl.

    wxWidgets makes heavy use of subclassing, so "mega" widgets are not an uncommon occurrence. In fact, with the C++ API you almost always end up sublcassing at least several widgets (typically wxFrames, wxPanels, and wxBoxSizers) to process any significant number of events.

    Here's an example of a wxPerl "mega" widget called LabelEntry. It creates a label, a text entry field, and a submit button. When the user clicks the submit button, the text is assigned to the variable specified by the caller and the caller's callback routine is invoked with the mega widget as the only argument. (Note that I've never used wxPerl before, so this will look somewhat rough):

    #!/usr/bin/perl -w use strict; use Wx; my $wxobj = new MyApp; $wxobj->MainLoop; package MyApp; use Wx qw(wxOK wxICON_INFORMATION); use base 'Wx::App'; sub OnInit { my $self = shift; my $frame = Wx::Frame->new( undef, -1, 'MegaWidget test', [0, 0], [300, 200] ); $self->SetTopWindow($frame); $frame->Show(1); my $name; my $labentry = LabelEntry->new( $frame, "Your name: ", \$name, "Submit", sub { Wx::MessageBox( "Your name is: $name.", "Name", wxOK|wxICON_INFORMATION, $frame ); } ); return 1; } package LabelEntry; use Wx; use Wx::Event qw(EVT_BUTTON); use base 'Wx::Panel'; sub new { my $invo = shift; my $class = ref $invo || $invo; my ($window, $label, $var_ref, $button_label, $callback) = @_; my $self = $class->SUPER::new($window, -1, [0,0], [300, 30]); bless($self, $class); new Wx::StaticText ($self, -1, $label, [0, 0]); my $text_control = new Wx::TextCtrl ( $self, -1, "", [70, 0] ); my $button_id = 2; my $button = new Wx::Button ( $self, $button_id, $button_label, [170, 0] ); EVT_BUTTON( $self, $button_id, sub { $$var_ref = $text_control->GetLineText(0); $callback->($self); } ); }

      William G Davis: an excellent example, with a very lean/concise amount of code.

      Thank you

      Helen

Re: Which GUI libraries allow creation of megawidgets? (wxPerl?Gtk?)
by mowgli (Friar) on Jan 07, 2005 at 13:48 UTC
    This may be a stupid question, but I have to ask - what *is* a megawidget?

    --
    mowgli

Re: Which GUI libraries allow creation of megawidgets? (wxPerl?Gtk?)
by zentara (Cardinal) on Jan 07, 2005 at 13:56 UTC
    Tk makes it easy to make a mega widget. Read perldoc Tk::mega. It basically means taking an existing widget or widgets, and combining them into a "customized widget". Here is a simple example. It takes a button and adds "mouse enter notification" to a Button. (Mastering Perl/Tk in Chapter 14 discusses custom widgets).
    #!/usr/bin/perl use Tk; use strict; package MyButton; # see perldoc Tk::mega use base qw/Tk::Derived Tk::Button/; Construct Tk::Widget 'MyButton'; sub ClassInit { my ($class, $mw) = @_; $class->SUPER::ClassInit($mw); # $mw->bind($class, '<Enter>', sub{print "Entered a MyButton\n"}); } sub Populate { my ($self, $args) = @_; $self->SUPER::Populate($args); $self->bind('<Enter>', sub{print "Entered a MyButton\n"}); # my (@bt) = $self->bindtags; # $self->bindtags( [ @bt[ 1, 0, 2, 3 ] ] ); } 1; package main; my $mw = MainWindow->new; $mw->Button(-text => 'NormalButton')->pack; $mw->MyButton(-text => 'MyButton')->pack; MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      I have no single doubt it is possible with perlTk, as my very first sentence states

      I was intended to gather information about whether it is possible everywhere else, and probably in which extent a user able to do this everywhere else.

      Thanks for your example, anyway.

        Damn! Got bit on the *ss again, by my speed reading. I could swear I didn't see that first sentence when I read it. :-)

        I'm not really a human, but I play one on earth. flash japh
Re: Which GUI libraries allow creation of megawidgets? (wxPerl?Gtk?)
by zentara (Cardinal) on Jan 15, 2005 at 20:39 UTC