Re: Fwd: (fwd) binding to a derived canvas widget
Nick Ing-Simmons <[email protected]>
| Newsgroups | gmane.comp.lang.perl.tk |
|---|---|
| Message-ID | <[email protected]> |
Steve Lidie <[email protected]> writes: >Here's how to do it (in Populate): > > $self->{'real_can'} = $self->{'can'}->Subwidget('scrolled'); >######## more on this later > my( @bindtags ) = $self->{'real_can'}->bindtags; > $self->{'real_can'}->bindtags( [ @bindtags, 'CanvasDirTree-B1' ] ); > $self->{'real_can'}->CanvasBind( 'CanvasDirTree-B1', '<Button-1>' >=> [\&pick_one, $self] ); All that is fine. But bindtags includes the name of the class already. This is so that one can assoicate class bindings with it. So what I intended for one to do was: $self->{'real_can'}->CanvasBind( ref($self), '<Button-1>' ...); But that is I hope unnecessarily complicated - see below. >> >> sub ClassInit >> { >> my ($class, $mw) = @_; >> $class->SUPER::ClassInit($mw); What that will do by the way is call Canvas::ClassInit. This isn't quite a no-op - it calls XYscrollBind Which binds mouse wheel and <Up>, <Down> etc. so that scrolls happen (if widget has the focus for keystroke cases). Once that has happened - and of course if you don't want that you don't have to call it! We can now add bindings to the class name. Which would look like: $mw->bind( $class, '<Button-1>', 'pick_one' ); >> } At this point $mw is _a_ widget (the MainWindow) which is all we need to talk to core Tk. It isn't a Canvas so no spurious binds are in scope. And it is called as a "static" method so 1st arg is the class name. As 'pick_one' is a sub in our class we treat it as a method just use its name in the binding. When a particular _instance_ of the class gets a Button-1 the class binding is called and the sub is called (as a method) passing the instance widget as 1st arg. This achieves the same as original ... '<Button-1>' => [\&pick_one, $self] ); But with the additional advantage that if someone derives from _your_ class they can override the pick_one method if your pick_one isn't quite what they want. -++**==--++**==--++**==--++**==--++**==--++**==--++**== This message was posted through the Stanford campus mailing list server. If you wish to unsubscribe from this mailing list, send the message body of "unsubscribe ptk" to [email protected]