Hello

I am trying to write a Tk widget wich will extend functionality of all widgets based on TK::Text. Inheritance should be determined dynamically like in Tk::Scrolled.

Lets say my class name is TextConfig and I want to inject ability to add live links into different widgets. So we can write

$text = $mw->TextConfig('Text'); # or $text = $mw->TextConfig('SuperText'); # or $text = $mw->TextConfig('ROText'); # etc
and receive appropriate widget with extended functionality.

Code of Tk/TextConfig.pm

package Tk::TextConfig; use strict; use Tk; our @ISA; use base qw(Tk::Derived); Construct Tk::Widget 'TextConfig'; sub new { my $package = shift; my $parent = shift; my $kind = shift; my $targetclass = "Tk::$kind"; eval "require $targetclass"; push @ISA, $targetclass; my $object = $targetclass->new($parent, @_); bless $object, "Tk::TextConfig"; # reblessing ? return $object; } my $LinkTagPrefix = 'Link_'; sub insertLink { my ($self, $index, $chars, $action) = @_; my $cnt = ++$self->{'TCcounters'}{'link'}; my $uniquename = $LinkTagPrefix.$cnt; $self->insert($index, $chars, $uniquename); $self->tagConfigure($uniquename, qw/-foreground blue -underline 1 +-data/, $action); # ...... $self->tagBind($uniquename, '<KeyPress>', 'keyLink'); # Wrong # This kind of binding has strange behaviour (bug?) # It is related to the 'current' mark instead of the 'insert'. # So it depends on mouse position # Anyway, it seems that Tk::Text tag bindings has noting common with + widget's bindings # Actually we cannot do Tk->break from sub keyLink }

Code of test.pl

use strict; use Tk; use Tk::TextConfig; my $mw = MainWindow->new; my $text = $mw->TextConfig(qw/Text -height 30/)->pack(qw/-expand 1 -fi +ll both/); for my $i (1..200) { $text->insertLink('end', 'Linktext', 'Some data'); $text->insert('end', ". \n -"); } foreach my $tag ($text->bindtags) { print " bindtag tag '$tag' has these bindings:\n"; print " ", $text->bind($tag), "\n"; } MainLoop();

It looks working. But there is problems with bindtags.

  bindtag tag 'Tk::Text' has these bindings:
  <List of Tk::Text bindings>
  
Tk::Text instead of Tk::TextConfig. Manipulations with bindtags() will lead to more problems.

So questions is:
1) How to solve problem with bindtags? (I would like to redefine some bindings.)
2) Is it correct at all? How to implement dynamic inheritance for Tk widgets?


In reply to Dynamic inheritance and bindings for TK::Text widgets by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.