Looking inside the Text Widget

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 33.  The Text Widget


The text widget has several operations that let you examine its contents. The simplest is get, which returns a range of text from the widget. You can get all the text with this command:

 $t get 1.0 end 

Looking at Tags

The tag names command returns all the tag names, or the names of the tags at a specified index:

 $t tag names ?index? 

A text tag can be applied to many different ranges of text. The tag ranges operation returns a list of indices that alternate between the start and end of tag ranges. The foreach command with two loop variables makes it easy to iterate through all the ranges:

 foreach {start end}[$t tag ranges $tag] {    # start is the beginning of a range    # end is the end of a range } 

The tag nextrange and tag prevrange operations return two indices that delimit the next and previous range of a tag. They take a starting index and an optional ending index. The tag nextrange operation skips to the next range if the tag is present at the starting index, unless the starting index is right at the start of a range. The tag prevrange operation is complementary. It does not skip the current range, unless the starting index is at the beginning of the range. These rules are used in Example 33-6 that defines a procedure to return the current range:

Example 33-6 Finding the current range of a text tag.
 proc Text_CurrentRange { t tag mark } {    set range [$t tag prevrange $tag $mark]    set end [lindex $range 1]    if {[llength $range] == 0 || [$t compare $end < $mark]} {       # This occurs when the mark is at the       # very beginning of the node       set range [$t tag nextrange $tag $mark]       if {[llength $range] == 0 ||              [$t compare $mark < [lindex $range 0]]} {          return {}       }    }    return $range } 

Looking at Marks

The mark names operation returns the names of all the marks. Unlike tag names, you cannot supply an index to find out if there are marks there. You must use the dump operation described later. The mark next and mark previous operations search from a given index for a mark. The mark next operation will find a mark if it is at the starting index.

Dumping the Contents

The dump operation provides the most general way to examine the contents of the text widget. The general form of the command is:

 $t dump ?options? ix1 ?ix2? 

The dump operation returns information for the elements from ix1 to ix2, or just for the elements at ix1 if ix2 is not specified. You can limit what information is returned with options that indicate what to return: -text, -mark, -tag, -image, -window, or -all.

Three pieces of information are returned for each element of the text widget: the type, the value, and the index. The possible types are text, tagon, tagoff, mark, image, and window. The information reflects the way the text widget represents its contents. Tags are represented as tagon and tagoff elements. Text is stored in segments that do not include any marks, tag elements, windows, or images. In addition, a newline ends a text segment.

Example 33-7 prints out the contents of the text widget:

Example 33-7 Dumping the text widget.
 proc Text_Dump {t {start 1.0} {end end}} {    foreach {key value index}[$t dump $start $end] {       if {$key == "text"} {          puts "$index \"$value\""       } else {          puts "$index $key $value"       }    } } 

Instead of having dump return all the information, you can have it call a Tcl command to process each element. The command gets passed three pieces of information for each element: the type, the value, and the index. Example 33-8 shows another way to print out the text widget contents:

Example 33-8 Dumping the text widget with a command callback.
 proc Text_Dump {t {start 1.0} {end end}} {    $t dump -command TextDump $start $end } proc TextDump {key value index} {    if {$key == "text"} {       puts "$index \"$value\""    } else {       puts "$index $key $value"    } } 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net