in reply to Tags in TK Text widgets

I am not sure exactly what you are doing. Please post a short runnable piece of code that illustrates your issues.

Replies are listed 'Best First'.
Re^2: Tags in TK Text widgets
by colintu (Acolyte) on Aug 15, 2024 at 19:16 UTC

    Code below, press the validate button to see the problem with line wrap and 'newline chars' as per my original post.

    #!/usr/bin/perl # #Text widget word wrap problem demo # use strict; use Tk; use Tk::TFrame; my $mw = MainWindow->new; my $big_font = $mw->fontCreate( -size => 11, -family => 'Terminal', -weight => 'bold'); $mw->title("Demo App"); my $seg1_f = $mw->TFrame(-label => 'Segment 1', -relief => 'groove', - +borderwidth => 2); my $ts_box = $seg1_f->Text(-wrap => 'word', -width => 60, -height => +6, -font => $big_font); $ts_box->grid( -padx => 4, -pady => 4, -row => 0, -column => 4, -sticky => "nsew", -rowspan => + 4); $ts_box->menu(undef); $ts_box->tagConfigure("badc", -foreground => "red", -background => 'ye +llow'); my $val = $seg1_f->Button(-text => "Validate", -command => [\&valida +te_text, 4]); $val->grid(-row => 0, -column => 3, -padx => 4, -pady => 5); $seg1_f->grid(-row => 0, -column => 0, -sticky => "nsew",-padx => 2, - +pady => 2); $mw->resizable(0,0); $ts_box->insert('end', "This is some test text with some invalid @ cha +racters"); $ts_box->insert('end', " If you click the Validate button the bad char +s &should be"); $ts_box->insert('end', " highlighted! Note that a newline character"); $ts_box->insert('end', "\n hightlights to the end of the line not just + the single"); $ts_box->insert('end', " character position. Is this a bug?"); MainLoop; # Validate the text field sub validate_text{ my $i_st; my $idx; my $cnt; my $ch; $ts_box->tagRemove('badc', '1.0', 'end'); my $wstring = $ts_box->get("1.0", "end"); $idx = length($wstring)-1; if ($idx == 0) {return(0)} $cnt = 0; while ($idx != $cnt) { $ch = substr($wstring, $cnt, 1); if (!($ch =~ /[A-Za-z0-9 \/.,=?*+]/)) { $i_st = sprintf("1.%d", $cnt); # bad char $ts_box->tagAdd('badc', $i_st); } $cnt++; } }
      The behaviour doesn't need a Tk::TFrame, it stays the same using Tk::Frame, too.

      Moreover, it seems nothing is highlighted if the line ending in the newline has the maximum length, which would concern me much more. Maybe indicate the newlines by something (a pilcrow ¶ maybe?).

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        I tried replacing the newlines, but I'm having a problem, it doesn't do what I expect. Because I want the replace to appear in the text box I tried:

        $ts_box->FindAndReplaceAll(-regexp, -nocase, "[\n]", "%");
        which appeared to have no effect, so as a sanity check I tried:
        $ts_box->FindAndReplaceAll(-regexp, -nocase, "[+]", "%");
        which works and replaces all the '+' chars with '%' chars. What am I doing wrong to stop \n matching?

      Also note that nothing after a newline gets validated.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]