$f->Button(-text => "Sug", -command =>\&tged)->pack(-side => 'right');
####
sub tged {
@chars=$t->get('sel.first','sel.last'); #Extract chars in area selected by user and store in @chars.
#print "\nSelected word:";
#print @chars;
open TF,'>gotword.txt' or die $!; #Write selected word onto file.
print TF @chars;
close TF;
$gotfile="gotword.txt"; #Save filename in $gotfile.
@sugs; #Array for holding strings returned by &suggestcomb.
@sugs=&suggestcomb($gotfile);
#Call sub. &suggestcomb passing $gotfile as parameter.
#print"\nStrings in main:";
#print "@sugs\n";
$top = new MainWindow;
#Create new main window which pops up when
#the user selects a word and clicks on the 'Sug' button.
$lsugs=@sugs; #Length of the array @sugs.
$opt1="$sugs[$lsugs]";
#Last element in the suggestion array.
$opt2="$sugs[$lsugs-1]";
#Second last element in the suggestion array.
$opt3="$sugs[$lsugs-2]";
#Third last element in the suggestion array.
$l = $top->Label(-text => "Press right button\nfor popup menu.")->pack;
#Text label for new main window.
#Menu displayed in new main window.
$m = $top->Menu(-tearoff => 0,font => "{arial} 12 {bold}",
-menuitems => #Menu items displayed as buttons.
[
[Button => "$opt1", -command => \&replace1],
#Labels of buttons are the strings obtained from @sugs.
[Button => "$opt2", -command => \&replace2],
[Button => "$opt3", -command => \&replace3],
]
);
#When a button is clicked the corresp. sub. for
replacing is invoked.
$top->bind("" => sub { $m->Popup(-popover => "cursor",-popanchor => 'nw') });
}
1;
#Subroutines to replace words with selected strings.
sub replace1{
$chosen=$opt1;
$t->insert('sel.first',"$chosen");
#Insert selected suggestion at the start point of the word to be replaced i.e. at sel.first.
$t->delete('sel.first','sel.last');
#Delete the word to be replaced.
$t->tagConfigure("replaced",-foreground=>"black");
$t->tagAdd("replaced","sel.first","sel.last");
}
sub replace2{
$chosen=$opt2;
$t->insert('sel.first',"$chosen");
$t->delete('sel.first','sel.last');
$t->tagConfigure("replaced",-foreground=>"black");
$t->tagAdd("replaced","sel.first","sel.last");
}
sub replace3{
$chosen=$opt3;
$t->insert('sel.first',"$chosen");
$t->delete('sel.first','sel.last');
$t->tagConfigure("replaced",-foreground=>"black");
$t->tagAdd("replaced","sel.first","sel.last");
}
####
$t->tagConfigure("ch_clr",-foreground=>"red");
$t->tagAdd("ch_clr","$start","$end_pt");
####
$t->tagConfigure("replaced",-foreground=>"black");
$t->tagAdd("replaced","sel.first","sel.last");
####
$lsugs=@sugs;
print"\nThe length of the array:";
print"\n$lsugs";
for($elt=0;$elt<$lsugs;$elt++)
{
pop(@sugs);
}
print"\nThe array contents after popping:";
print "@sugs\n";