Task 15: entry
This task uses the loop of task 04 for the caret timer. It does not depend on the loop of ztcl.
Goal
The entry widget edits one line of left-to-right text with the keyboard and the pointer, shows a caret and a selection, and links to a variable.
Files
src/text/edit.zig: the edit model, with no dependence on a widgetsrc/widgets/entry.zigsrc/cmds.zig: the registration and the class bindingssrc/themes/modern.css: theEntryrules
Interfaces
// src/text/edit.zig. All indexes count characters (code points).
pub const Edit = struct {
pub fn init(gpa: std.mem.Allocator) Edit;
pub fn deinit(e: *Edit) void;
pub fn text(e: *const Edit) []const u8; // UTF-8
pub fn len(e: *const Edit) usize; // characters
pub fn setText(e: *Edit, utf8: []const u8) !void; // clamps the caret and clears the selection
pub fn insert(e: *Edit, index: usize, utf8: []const u8) !void;
pub fn delete(e: *Edit, first: usize, last: usize) void; // [first, last)
caret: usize = 0,
/// The selection is [sel_first, sel_last). Both 0 and equal: none.
sel_first: usize = 0,
sel_last: usize = 0,
anchor: usize = 0,
pub fn hasSelection(e: *const Edit) bool;
pub fn select(e: *Edit, first: usize, last: usize) void;
pub fn clearSelection(e: *Edit) void;
/// Moves the caret. With `extend`, the selection goes from the anchor
/// to the new caret.
pub fn moveTo(e: *Edit, index: usize, extend: bool) void;
pub const Step = enum { prev_char, next_char, prev_word, next_word, line_start, line_end };
pub fn target(e: *const Edit, step: Step) usize;
/// Replaces the selection, or inserts at the caret.
pub fn typeText(e: *Edit, utf8: []const u8) !void;
pub fn backspace(e: *Edit) void; // deletes the selection, or the character before the caret
pub fn deleteForward(e: *Edit) void;
pub fn wordAt(e: *const Edit, index: usize) [2]usize;
/// The byte offset of a character index.
pub fn byteOf(e: *const Edit, index: usize) usize;
};
// src/widgets/entry.zig
pub const class: Class; // command "entry", type name "Entry", focusable
Steps
-
Read
doc/entry.n: the sections "WIDGET-SPECIFIC OPTIONS", "INDICES", "WIDGET COMMAND", and "DEFAULT BINDINGS". -
The edit model is a pure data structure. A word is a run of letters, digits, and
_.prev_wordandnext_wordgo to the start of the word before the caret and to the end of the word after it. Stage 4 changes the word and character rules to UAX #29. -
The options of Stage 1:
-textvariable,-show,-state(normal,disabled,readonly),-width(in characters, default 20),-font,-foregroundand-fg,-backgroundand-bg,-borderwidthand-bd,-relief,-justify(onlyleftworks),-insertwidth(default 2),-insertontime(default 600),-insertofftime(default 300),-selectbackground,-selectforeground,-exportselection(stored),-takefocus,-cursor. Each other option of the Tk entry givesunknown option. Add them todocs/stage1-limits.md. -
The widget subcommands, with the messages of
wish:getinsert index text.wishgiveswrong # args: should be ".e insert index text"for a wrong count.delete first ?last?. With nolast, it deletes one character.icursor indexindex index. It gives a number.selection clear,selection present,selection range start end. The other words (adjust,from,to) giveselection <word> is not supported in this stage. A wrong word givesbad selection option "bogus": must be adjust, clear, from, present, range, or to.bbox,scan,validate, andxviewgive<name> is not supported in this stage.- A wrong name gives
bad option "bogus": must be bbox, cget, configure, delete, get, icursor, index, insert, scan, selection, validate, or xview.
-
The index forms: a number,
end,insert,anchor,sel.first,sel.last, and@x(the character at that x in the widget). A number below 0 is 0, and a number above the length is the length:wishgives5for.e index 99with five characters.sel.firstwith no selection givesselection isn't in widget .e. A wrong form givesbad entry index "bogus". -
insertanddeletehave no effect in thedisabledandreadonlystates, and give no error.wishleaves the text as it is for.e insert end zzzon a readonly entry. -
-textvariable. On creation and onconfigure, when the variable exists, the entry shows its value. When it does not exist, the entry creates it with its text. Each edit writes the variable. A write to the variable by a script changes the text: use the script trace of task 11 with a flag that stops the loop when the entry itself writes.wishgivesinitfor.e getafterset tv init; .e configure -textvariable tv. -
-show cdraws the first character of the value in place of each character.getstill gives the real text. -
Measure: the width is
-widthtimes the width of0, plus the insets of the style. The height is the line height plus the insets. -
The scroll offset. The entry keeps
left, the logical pixel offset of the text. After each change of the caret, changeleftso that the caret is inside the content rectangle. When the text is shorter than the rectangle,leftis 0. -
Paint, in this order: the box, a clip of the content rectangle, the selection box (the style of the part
::selection: itsbackground-colorandcolor), the text, and the caret. The caret is a box of-insertwidthlogical pixels in thecaret-color, with the height of the line. The glyphs inside the selection have the selection text color: make two clips, or split the run at the selection. -
The caret shows only when the entry has the
focusbit and the state isnormal. It is on for-insertontimeand off for-insertofftime, withLoop.addNative. An off time of 0 means no blink. Each key and each click starts the on time again. When the focus leaves, cancel the timer. -
The class bindings on the tag
Entry. Each script is one call of::ztk::priv::entry <action> %Wwith the fields it needs:Pattern Action <Button-1>Set the caret at %x, clear the selection, set the anchor, take the focus<B1-Motion>Extend the selection to %x<Double-Button-1>Select the word at %x<Triple-Button-1>Select all <Shift-Button-1>Extend the selection to %x<Key-Left>,<Key-Right>Move one character and clear the selection <Shift-Key-Left>,<Shift-Key-Right>Move one character and extend <Control-Key-Left>,<Control-Key-Right>Move one word <Key-Home>,<Key-End>Move to the start or the end <Shift-Key-Home>,<Shift-Key-End>Move and extend <Key-BackSpace>,<Key-Delete>Delete <Control-Key-a>Move to the start, as Tk on X11 does <Control-Key-e>Move to the end <Control-Key-slash>Select all <Key>Insert %Awhen it is not empty and is not a control character<Control-Key>,<Alt-Key>,<Key-Return>,<Key-Escape>,<Key-Tab>No edit. The script is a comment ( # no edit). It is more specific than<Key>, so the<Key>binding does not insert the character.wishlists the same five patterns inbind Entry.Tk uses virtual events (
<<PrevChar>>,<<SelectAll>>) for many of these. Stage 2 changes to them whenevent addexists. The clipboard bindings are Stage 2. -
A key that the entry handled keeps the default flow: the tags
.andallstill run, soTabstill moves the focus and a<Return>binding of the script on the entry runs. -
The
Entryrules of the modern theme: the field background, a 1 pixel border, the radius, the padding5px 8px, a:focusborder in the accent color, a:disabledlook, andEntry::selectionwith the accent background and its text color.
Tests
zig build test -Dtest-filter=edit
zig build test -Dtest-filter=entry
Unit tests of Edit:
insert(0, "héllo")gives the length 5.byteOf(2)is 3.delete(1, 3)ofhellogiveshlo.typeTextwith a selection replaces it and clears the selection.backspaceat index 0 has no effect.backspaceafterédeletes two bytes.target(.prev_word)infoo bar|is 4.target(.next_word)in|foo baris 3.moveTo(3, true)from the caret 1 with the anchor 1 selects [1, 3).moveTo(0, true)then selects [0, 1).wordAt(5)infoo bar bazis [4, 7).setTextwith a shorter text clamps the caret.
Script tests with a Harness. The values are from wish 8.6.16.
.e insert 0 hello; .e index endgives5..e icursor 2; .e index insertgives2..e selection range 1 3; list [.e selection present] [.e index sel.first] [.e index sel.last]gives1 1 3..e delete sel.first sel.last; .e getgiveshlo..e selection clear; .e index sel.firstgives the error of step 5..e delete 0 end; .e insert end abc; .e insert 1 XY; .e getgivesaXYbc..e delete 2; .e getgivesaXbc..e index 99gives4..e index -5gives0..e index bogusgives the error.- The readonly and disabled states do not change the text.
-textvariablein both directions:set tv init, configure,getgivesinit..e insert end Q; set tvgivesinitQ.set tv new; .e getgivesnew.-show *:getgives the real text, and the scene dump has only the glyph of*.focus .eandh.head.typeText("Ada"):getgivesAda.Left,Left,x:Axda.BackSpace:Ada.Home,Delete:da.End,!:da!.Shift-Lefttwo times andxreplaces two characters.Control-aand thenzinserts at the start.- A click at the x of the middle of the text sets the caret there (compare with
Run.indexAt). - A press, a move, and a release select the characters between the two points.
- A double click on a word selects it.
bind .e <Return> {set got [.e get]}runs onh.head.press("Return"), and the text has no new character.Tabmoves the focus to the next widget, and the text has no tab character.- A long text: after 60 characters in an entry of 20, the caret x in the scene dump is inside the content rectangle.
- The caret: with the manual clock, the scene has the caret quad at 0 ms, has no caret quad at 700 ms, and has it again at 950 ms. An entry with no focus has no caret quad and no timer.
- An entry with no focus and no pending change draws no frame in 5 000 ms of the manual clock.
Golden images and scene dumps, with the modern theme, 260 by 60:
entry-empty-focus: the caret at the start and the focus border.entry-text: a text and no focus.entry-selection: a part of the text is selected.entry-scrolled: a long text with the caret at the end.entry-disabledentry-show: a password with-show *.
Acceptance
- All tests pass.
- The reviewer opens the six images. The caret has the height of the line. The selection covers the selected characters exactly. No glyph is outside the field.
src/text/edit.zigimports no widget code and no ztcl.- Each key of a test goes through
dispatch.handleand the class bindings. No test calls the edit model directly for a key.
Out of scope
- The clipboard, the primary selection, and
-exportselection. - Validation (
-validate,-validatecommand,-invalidcommand). xview,-xscrollcommand,scan, andbbox.-justify centerandright.- Input methods, right-to-left text, and undo.