Version 5 of Text Widget Newline Wrapping

Updated 2003-10-13 17:25:21

Vaclav Snajdr posted to comp.lang.tcl:

Hi, to fill out correctly formulars with fixed number of lines and character per line I want use the widget text. I declare text .t -font 10x20 -width 50 -height 5 so the number of characters per line is controlled (skip after 50 to the next line) but not the number of lines not (more than 5 lines are editable).

Question: there is an another atribute for it or must it be controlled by a method?

Thanks

GPS: Here is my solution:

  #!/usr/local/bin/wish8.3

  proc main argv {
  pack [text .t -wrap none]

  rename .t ._t

    proc .t {cmd args} {
                                atribute for it or must it be controlled by a method?''
      switch -- $cmd {
        insert {
          set currentIndex [lindex [split [._t index insert] .] 1]
                                ex [split [._t index insert] .] 1]
          if {$currentIndex >= 50} {
            set wordStartIndex [._t index "insert-1c wordstart"]              
            if {$wordStartIndex != [._t index "insert linestart"]} {
              ._t insert $wordStartIndex "\n"           nestart"]} {
            } else {                     "\n"           nestart"]} {
              ._t insert insert "\n"
            }
          }
          eval ._t insert $args
         }
       default {
         eval ._t $cmd $args
       }
     }
   }
 }

main $argv


GPS answered a different question in c.l.t: how does one calculate the number of displayed lines in a text widget, as distinguished from the number of logical (\n-separated) lines:

    set start [.t index @0,0]
    set end [.t index @0,[winfo height .t]]  
    expr $end - $start + 1 ...

This returns the number of logical (\n-separated) lines currently displayed, which is not the same as the number of display lines on screen (which is very hard to calculate if font sizes differ). But, with tip 155:

    expr [.t count -displaylines @0,0 @0,[winfo height .t]] + 1

does the job.


Category GUI