0

tkBind.c has three functions: Tk_BindEvent, MatchPattern, and ExpandPercents, which are called continuously for any events that occur in the text or entry widget. They have pointers e.g., eventPtr, bindPtr and disPtr which are storing any kind of event that occurred on the widget. These pointers are also storing text but character-wise; I want to know where the whole string or text is stored? Which linked list is that, or where is the pointer to that linked list?

1 Answer 1

1

The overall text being edited in a particular widget is not stored in that file at all. All that file does is turn an event coming in from the display engine (“a key was pressed!”, “the mouse was moved!”, “a button was clicked!”, “a widget was resized!”, etc.) into a callback of Tcl code that was registered with the bind command.

For example, with the entry widget, a normal key press ends up being routed via the script that was registered via (in …/library/entry.tcl in the source distribution of Tk):

bind Entry <KeyPress> {
    tk::CancelRepeat
    tk::EntryInsert %W %A
}

This is a class binding (as it is on Entry) of a generic key event (<KeyPress>, rather than a more specific one like <KeyPress-BackSpace>) and it executes two commands in sequence. One cancels any key repetition in progress, and the other does the actual insert into the current widget (%W) of the relevant character sequence (%A). Here's what the insertion code looks like:

proc ::tk::EntryInsert {w s} {
    if {$s eq ""} {
        return
    }
    catch {
        set insert [$w index insert]
        if {([$w index sel.first] <= $insert)
                && ([$w index sel.last] >= $insert)} {
            $w delete sel.first sel.last
        }
    }
    $w insert insert $s
    EntrySeeInsert $w
}

This does nothing if there was no actual character sequence (as can happen for some key sequences), tries to delete what was selected (which fails silently if no selection was present), does the actual insertion at the place where the insert cursor is ($w insert insert $s) and then tries to make sure that the new location of the insertion cursor is visible to the user.

The string being edited is actually stored in the internal model of the entry widget, where it is held as UTF-8 in the string field of the Entry structure (declared in …/generic/tkEntry.h). This value can also be reflected out to a Tcl variable if the user desires (see the -textvariable configuration option); that's pretty common in practice, but not universal or required.

1
  • Strictly it's not UTF-8, but rather a CESU-8 variant. Tcl and Tk actually use a variation that consistently represents a \u0000 as a denormalised multibyte sequence — the only permitted non-normalized value — so that strings can be logically NUL-safe. This nicety won't matter for input and output of any human-readable text! Nov 7, 2013 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.