7.1 Manipulating Text

Let us start with a very simple example where we want to display a given text in a text widget. Figure 7.1 shows a program that does the job.


T={New Tk.text tkInit(parent:W width:28 height:5 bg:white)}
{T tk(insert 'end' "The quick brown fox jumps over the lazy dog.")}

Figure 7.1: Displaying text.


scanning

Similar to listboxes, a text widget supports scanning: The text can be scanned by pressing the second mouse button and moving the mouse pointer while the button is still being pressed. And of course, in the same way as canvas widgets scrollbars can be attached to a text widget.

text wrapping

The text is wrapped where word boundaries (that is, spaces) are not taken into account. Changing the wrapping such that word boundaries are preserved can be done as follows:

{T tk(configure wrap:word)}

positions

Positions in the displayed text can be referred to by positions. A position can be denoted by a tickle p(L C), where L gives the line (starting from one) and C the position in that line (also starting from zero). Positions also can take modifiers, for more details on this issue see text. Another helpful position is 'end' which refers to the position after the last character.

getting text

Portions of the text can be retrieved. For example,

{T tkReturnAtom(get p(1 4) p(1 9) $)}

returns the atom quick.

inserting text

Positions also specify where to insert text, for example

{T tk(insert p(1 4) "very very ")}

inserts the text directly before quick.

deleting text

In the same way text can also be deleted. For example

{T tk(delete p(1 4) p(1 14))}

deletes again the text "very very ".

disabling input

We do not discuss here how to employ a text widget as a powerful editor, please see again text. If you try to place a cursor inside the text widget and make some character strokes, you will notice that by default a text widget accepts input. To prevent a user from altering the text in a display only situation the widget can be configured as follows:

{T tk(configure state:disabled)}


Christian Schulte
Version 1.4.0 (20080702)