Version 1 of Text Widget Newline Wrapping

Updated 2002-02-25 23:37:09

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} {

                switch -- $cmd {
                        insert {
                        set currentIndex [lindex [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"                
                                        } else {                        
                                        ._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 ...