For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > September 2004 > Hyperlinks within a text widget









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Hyperlinks within a text widget
Mike Ratcliffe

2004-09-15, 3:59 pm

I am struggling to add hyperlinks within a text widget in an
application. The output includes homeshares and links to various
files, I would like these to appear as hyperlinks. Here is some
example output:

########################################
###############################
Checking for package readme file to locate groups...
Readme file located at
\\FIWSINF01\CODE$\kits\english\osan632a1
0\osan632a10_readme.txt

Creating directory: \\FIRSF01\lfinla01$\config\netlogon\inst
all\004osan632a10.WNT
Creating File: \\FIWSINF01\CODE$\kits\english\osan632a1
0\SmallStub\osan632a10U.bat
Populating file with:
\\FIWSINF01\CODE$\kits\english\osan632a1
0\SmallStub\osan632a10U.bat
@appsdat: oraw7,english,u
Checking for package readme file to locate groups...
Readme file located at
\\FIWSINF01\CODE$\kits\english\oraw7\ora
w7_readme.txt

Creating directory: \\FIRSF01\lfinla01$\config\netlogon\inst
all\005oraw7.WNT
Creating File: \\FIWSINF01\CODE$\kits\english\oraw7\Sma
llStub\oraw7U.bat
Populating file with:
\\FIWSINF01\CODE$\kits\english\oraw7\Sma
llStub\oraw7U.bat
########################################
###############################

I would like the filenames to appear as hyperlinks to the files and am
using Windows. My function to write to the text widget follows:

########################################
###############################
sub WriteSummary() { # WriteSummary($string) Writes string to txtMain
my $string=$_[0];
my $color=$_[1];

if ($color) {
$txtMain->insert('end',"$string\n",$color);
}
else {
$txtMain->insert('end',"$string\n",'black');
}

$txtMain->see('end');
$mw->update;

return(1); # For -validatecommand
}
########################################
###############################

Any Ideas?
$_@_.%_

2004-09-15, 3:59 pm


ratcliffe_mike@hotmail.com (Mike Ratcliffe) wrote in message-id:
<5a118382.0409150053.357fbaf3@posting.google.com>
>
>I am struggling to add hyperlinks within a text widget in an
>application. The output includes homeshares and links to various
>files, I would like these to appear as hyperlinks. Here is some
>example output:
>
> ########################################
###############################
>Checking for package readme file to locate groups...
>Readme file located at
> \\FIWSINF01\CODE$\kits\english\osan632a1
0\osan632a10_readme.txt
>
>Creating directory: \\FIRSF01\lfinla01$\config\netlogon\inst
all\004osan632a10.WNT
>Creating File: \\FIWSINF01\CODE$\kits\english\osan632a1
0\SmallStub\osan632a10U.bat
>Populating file with:
> \\FIWSINF01\CODE$\kits\english\osan632a1
0\SmallStub\osan632a10U.bat
>@appsdat: oraw7,english,u
>Checking for package readme file to locate groups...
>Readme file located at
> \\FIWSINF01\CODE$\kits\english\oraw7\ora
w7_readme.txt
>
>Creating directory: \\FIRSF01\lfinla01$\config\netlogon\inst
all\005oraw7.WNT
>Creating File: \\FIWSINF01\CODE$\kits\english\oraw7\Sma
llStub\oraw7U.bat
>Populating file with:
> \\FIWSINF01\CODE$\kits\english\oraw7\Sma
llStub\oraw7U.bat
> ########################################
###############################
>
>I would like the filenames to appear as hyperlinks to the files and am
>using Windows. My function to write to the text widget follows:
>
> ########################################
###############################
>sub WriteSummary() { # WriteSummary($string) Writes string to txtMain
> my $string=$_[0];
> my $color=$_[1];
>
> if ($color) {
> $txtMain->insert('end',"$string\n",$color);
> }
> else {
> $txtMain->insert('end',"$string\n",'black');
> }
>
> $txtMain->see('end');
> $mw->update;
>
> return(1); # For -validatecommand
>}
> ########################################
###############################
>
>Any Ideas?


you could put flat buttons into the text widget using a window.
its also possible to configure the button to not depress when clicked.


zentara

2004-09-15, 3:59 pm

On 15 Sep 2004 01:53:10 -0700, ratcliffe_mike@hotmail.com (Mike
Ratcliffe) wrote:

>I am struggling to add hyperlinks within a text widget in an
>application. The output includes homeshares and links to various
>files, I would like these to appear as hyperlinks. Here is some
>example output:


>Any Ideas?


I didn't write this, but here is an example I found somewhere.

#!/usr/bin/perl
use Tk;
$mw = MainWindow->new( -title => "hyperlinks" );
$t = $mw->Scrolled('Text')->pack;
$tag = "tag000";
foreach (<DATA> ) {
chomp;
split (/(http:\S+)/);
foreach (@_) {
if (/(http:\S+)/) {
$t->insert( 'end', $_, $tag );
$t->tagConfigure( $tag, -foreground => 'blue' );
$t->tagBind( $tag,
'<Any-Enter>' => [ \&manipulate_link, $tag, 'raised',
'hand2' ]
);
$t->tagBind( $tag,
'<Any-Leave>' => [ \&manipulate_link, $tag, 'flat',
'xterm' ] );
$t->tagBind( $tag,
'<Button-1>' => [ \&manipulate_link, $tag, 'sunken' ] );
$t->tagBind( $tag,
'<ButtonRelease-1>' =>
[ \&manipulate_link, $tag, 'raised', undef, \&printme
] );
$tag++;
}
else {
$t->insert( 'end', $_ );
}
}
$t->insert( 'end', "\n" );
}
MainLoop;

sub printme {
local ($,) = " ";
print "printme:", @_, "\n";
}

sub manipulate_link {

# manipulate the link as you press the mouse key

my ($a) = shift;
my ($tag) = shift;
my ($relief) = shift;
my ($cursor) = shift;
my ($after) = shift;

# by configuring the relief (to simulate a button press)
$a->tagConfigure( $tag, -relief => $relief, -borderwidth => 1 );

# by changing the cursor between hand and xterm
$a->configure( -cursor => $cursor ) if ($cursor);

# and by scheduling the specified action to run "soon"
if ($after) {
my ($s) = $a->get( $a->tagRanges($tag) );
$main::mw->after( 200, [ $after, $a, $s, $tag, @_ ] ) if
($after);
}
}
__DATA__
Hi there. This is text.
THis is more text but http://this.is.a/hyperlink in a line.
http://this.is.another/hyperlink followed by
http://this.is.a.third/hyperlink
__END__



--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Mike Ratcliffe

2004-09-15, 3:59 pm

$_@_.%_ wrote in message news:<nGT1d.2026$Tg7.532@trndny05>...
> ratcliffe_mike@hotmail.com (Mike Ratcliffe) wrote in message-id:
> <5a118382.0409150053.357fbaf3@posting.google.com>
>
> you could put flat buttons into the text widget using a window.
> its also possible to configure the button to not depress when clicked.


I have considered buttons but I also need to be able to copy and paste
from the text widget :o(

There must surely be some way to achieve this ... does anybody know of
any kind of HTML widget of similar? If I could output to an HTML
widget surely it would be simple to produce links ... in fact that
would be a very powerful widget.

Anyhow, any more ideas anybody?
Ala Qumsieh

2004-09-15, 3:59 pm

Mike Ratcliffe wrote:

[snip]

> I would like the filenames to appear as hyperlinks to the files and am
> using Windows.


[snip]

> Any Ideas?


The widget demo that comes with Tk has a demo called "Hypertext" which
does exactly what you want. Type 'widget' in your command prompt to get it.

--Ala
Eric Bohlman

2004-09-16, 5:01 am

ratcliffe_mike@hotmail.com (Mike Ratcliffe) wrote in
news:5a118382.0409150554.3f67ba4c@posting.google.com:

> There must surely be some way to achieve this ... does anybody know of
> any kind of HTML widget of similar? If I could output to an HTML
> widget surely it would be simple to produce links ... in fact that
> would be a very powerful widget.


There's Tk::HTML, which basically uses the methods described by Ala and
others in this thread (tagging entries in a text widget). Unfortunately,
it doesn't seem to be documented.
Mike Ratcliffe

2004-09-17, 8:59 am

In the end I used a combination of the code from the widget demo and
my own code ... I include it here for others attempting to solve this
problem:

use Tk;
use strict;

my $currtag=0;
my @tags;

my $mw = MainWindow->new;
my $txtMain = $mw->Scrolled('Text',
-width => '60',
-height => '24',
-scrollbars => 'e',
-wrap => 'word');

$txtMain->pack(-expand => 'yes',
-fill => 'both');

$txtMain->tagConfigure('red',-foreground => 'red');

&ShowHTML();

sub ShowHTML() {
my(@bold, @normal, $tag);

@bold = (-background => '#43ce80', -relief => 'raised',
-borderwidth => '1');
@normal = (-background => undef, -relief => 'flat');

&WriteSummary("Just testing this in red\n",'red');
&WriteSummary("Just testing this in black\nThis Should Display ");
&WriteSummary("My Homeshare","\\\\sdpsf02\\mira3005\$");
&WriteSummary("\nAnd this Should Display ");
&WriteSummary("Sabines Homeshare","\\\\sdpsf04\\sara2504\$");

foreach $tag (@tags) {
$txtMain->tagBind($tag, '<Any-Enter>' =>
sub {my $l = shift; $l->tagConfigure($tag, @bold);
$l->configure(-cursor => 'hand2');}
);
$txtMain->tagBind($tag, '<Any-Leave>' =>
sub {my $l = shift; $l->tagConfigure($tag, @normal);
$l->configure(-cursor => 'arrow');}
);
$txtMain->tagConfigure($tag,-underline => 1);
}

MainLoop;
}

sub WriteSummary() {
my $string=$_[0];
my $tag=$_[1];
my $tagname;

if (!(defined($tag))) {
$txtMain->insert('end',"$string");
}
elsif ($tag=~/\\\\/) {
$tagname="tag".$currtag++;
$txtMain->tagBind($tagname,"<1>" => sub {`explorer.exe $tag`;});
$txtMain->insert('end',"$string",$tagname);
print "\$tagname: $tagname\n";
push(@tags,$tagname);
}
else {
$txtMain->insert('end',"$string",$tag);
}

$txtMain->see('end');

return(1); # For -validatecommand
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com