Documentation
¶
Overview ¶
Package widget collects the small leaf views that live inside a Window or Dialog: static text, labels, buttons, input lines, clusters, scrollbars, and so on.
v0.2 phase 3 ships the first batch (StaticText, Label, Button, InputLine). Lists, scrollbars, and clusters arrive in phase 4.
Index ¶
- Constants
- func HistoryAdd(id HistoryID, s string)
- func HistoryCount(id HistoryID) int
- func HistoryReset()
- func HistoryStr(id HistoryID, n int) string
- type Button
- type ButtonFlags
- type CheckBoxes
- type Cluster
- type GetTextFunc
- type History
- type HistoryID
- type HistoryWindow
- type InputLine
- type Label
- type ListBox
- type ListViewer
- type Memo
- type Orientation
- type ParamText
- type RadioButtons
- type ScrollBar
- type StaticText
Constants ¶
const ( SlotDialogText byte = 14 SlotDialogShortcut byte = 17 SlotButtonNormal byte = 18 SlotButtonFocused byte = 19 SlotButtonDisabled byte = 20 SlotButtonShortcut byte = 21 SlotInputNormal byte = 22 SlotInputFocused byte = 23 SlotInputSelection byte = 24 SlotClusterNormal byte = 25 SlotClusterFocused byte = 26 SlotClusterSelect byte = 27 SlotClusterHotKey byte = 28 SlotScrollPage byte = 29 SlotScrollArrow byte = 30 SlotListNormal byte = 31 SlotListFocused byte = 32 SlotListSelected byte = 33 SlotListDisabled byte = 34 )
Palette slot constants used by widgets. They line up with the slots added to vio.BorlandClassic for dialog content. Widgets default to these but callers may override PaletteIndex if a host theme adds extra slots.
const HistoryLimit = 16
HistoryLimit caps the number of strings kept per ID. Older entries fall off the back when the ring fills up.
Variables ¶
This section is empty.
Functions ¶
func HistoryAdd ¶
HistoryAdd pushes s onto the front of the ring for id. If s already exists in the ring it is moved to the front (no duplicates). The ring is capped at HistoryLimit; the oldest entry is discarded when it overflows. Empty strings and id == 0 are ignored.
func HistoryCount ¶
HistoryCount returns the number of stored strings for id.
func HistoryReset ¶
func HistoryReset()
HistoryReset clears all rings. Tests use this to start clean.
func HistoryStr ¶
HistoryStr returns the n-th string in the ring for id, with 0 the most recent. Out-of-range indices return "".
Types ¶
type Button ¶
type Button struct {
*view.View
Title string
Command event.CommandID
Flags ButtonFlags
// contains filtered or unexported fields
}
Button is a clickable control that emits a command. Mouse-down inside the bounds, Space or Enter while focused, or Alt-<mnemonic> all fire the same command.
func NewButton ¶
NewButton returns a Button sized to bounds. The title may include a '~x~' marker to advertise an Alt-x hot-key.
func (*Button) Draw ¶
Draw paints the button. The title sits inside square brackets when the button is the default; otherwise plain. The focused button uses the focused palette slot.
func (*Button) HandleEvent ¶
HandleEvent fires the command on Space, Enter, mouse-down inside the bounds, or Alt-<mnemonic>.
type ButtonFlags ¶
type ButtonFlags uint8
ButtonFlags is a bitmask of behavior flags for a Button. The names match Turbo Vision's bfXxx constants.
const ( BfNormal ButtonFlags = 0 BfDefault ButtonFlags = 1 << iota // fired on Enter regardless of focus BfLeftJust // left-justify the title (default is centered) BfBroadcast // emit ClassBroadcast instead of ClassCommand )
Button flag bits.
type CheckBoxes ¶
type CheckBoxes struct {
*Cluster
}
CheckBoxes is a Cluster where each item is independently toggled. Value is a bitmask: bit i is set when item i is checked.
func NewCheckBoxes ¶
func NewCheckBoxes(bounds vio.Rect, items []string) *CheckBoxes
NewCheckBoxes returns a check-box cluster sized to bounds. Each item title may include a '~x~' mnemonic marker for Alt-x dispatch.
func (*CheckBoxes) HandleEvent ¶
func (c *CheckBoxes) HandleEvent(e *event.Event)
HandleEvent moves focus, toggles on Space/Enter, and dispatches Alt-mnemonic.
func (*CheckBoxes) Mark ¶
func (c *CheckBoxes) Mark(i int) bool
Mark reports whether item i is checked.
type Cluster ¶
type Cluster struct {
*view.View
Items []string
Value uint32
// contains filtered or unexported fields
}
Cluster is the shared backing for CheckBoxes and RadioButtons. It holds the option titles, the parsed mnemonic letters, the focused item index, and a Value field whose meaning is owned by the concrete type (bitmask for check boxes, index for radio buttons).
type GetTextFunc ¶
GetTextFunc returns the rendered string for item index when at most maxLen runes are wanted. It is the abstract entry point ListViewer uses to fetch row text; ListBox installs one that reads from a string slice. Callers can install any function (e.g. backed by a log buffer) without subtyping.
type History ¶
History is the small dropdown-arrow button that sits next to an InputLine. A click or a focused Enter sets Open to true; the host dialog watches Open and pushes a HistoryWindow when it flips. Phase 4 keeps the trigger simple; richer integration with ExecView lands alongside the menu/cmd work in phase 5.
func NewHistory ¶
NewHistory returns a History trigger attached to id.
func (*History) HandleEvent ¶
HandleEvent flips Open on click or Enter. Hosts reset Open after they spawn the dropdown window.
type HistoryID ¶
type HistoryID uint16
HistoryID groups history entries by purpose: each numeric ID owns its own ring of recent strings. Two views that share an ID share their history. Zero means "no history" and HistoryAdd ignores it.
type HistoryWindow ¶
type HistoryWindow struct {
*view.Group
ID HistoryID
List *ListBox
Selected string
// contains filtered or unexported fields
}
HistoryWindow is the dropdown list that appears under a History trigger. It hosts a ListBox seeded from the ring for ID. Enter on the focused row stores Selected and sets Result to CmdOk; Esc sets Result to CmdCancel. Hosts insert a HistoryWindow into a Group and poll Result like any other modal view.
func NewHistoryWindow ¶
func NewHistoryWindow(bounds vio.Rect, id HistoryID) *HistoryWindow
NewHistoryWindow returns a dropdown sized to bounds, populated from the ring for id. The internal ListBox is inserted as the first child.
func (*HistoryWindow) HandleEvent ¶
func (w *HistoryWindow) HandleEvent(e *event.Event)
HandleEvent intercepts Enter and Esc, then defers everything else to the embedded Group (which feeds the ListBox).
func (*HistoryWindow) Result ¶
func (w *HistoryWindow) Result() event.CommandID
Result returns the modal command, or CmdNone while still open.
type InputLine ¶
type InputLine struct {
*view.View
Data []rune
MaxLen int
// contains filtered or unexported fields
}
InputLine is a single-line text editor. It exposes Data for the caller to read or seed; the cursor and selection are kept in private fields and updated by HandleEvent.
Phase 3 supports printable insertion, Backspace, Delete, arrow-key navigation, Home/End, Ctrl-Y (clear), and shift-extend selection. Word-wrap, undo, and the History dropdown ride along in phase 4.
func NewInputLine ¶
NewInputLine returns an InputLine with the given bounds and length cap. maxLen <= 0 disables the cap.
func (*InputLine) CursorIndex ¶
CursorIndex returns the insertion point as a rune index. It does not shadow the View.Cursor field, which holds the on-screen cell offset for the rendered caret.
func (*InputLine) Draw ¶
Draw paints the field. The selection is highlighted in the selection slot; otherwise the line uses normal/focused color.
func (*InputLine) HandleEvent ¶
HandleEvent processes one keystroke. Non-key events fall through.
func (*InputLine) Selection ¶
Selection returns the selected range [start, end) as rune indices. start == end when nothing is selected.
type Label ¶
type Label struct {
*view.View
Title string
Peer view.Viewer
// contains filtered or unexported fields
}
Label is a non-focusable text element bound to a peer view. Pressing Alt-<mnemonic> on the dialog moves focus to the peer. The mnemonic is the rune surrounded by '~' in the title (e.g. "~N~ame:" advertises Alt-N and underlines the N).
func NewLabel ¶
NewLabel returns a Label sized to bounds. peer may be nil for a caption that just announces text without claiming a hot-key.
func (*Label) Draw ¶
Draw paints the label. The mnemonic rune is drawn in the dialog shortcut color so it stands out.
func (*Label) HandleEvent ¶
HandleEvent moves focus to the peer when the user presses Alt-<mnemonic>. Without a peer, or without a mnemonic, the label is inert.
type ListBox ¶
type ListBox struct {
*ListViewer
// contains filtered or unexported fields
}
ListBox is a ListViewer backed by a string slice. Items can be updated at any time with SetItems; the list resizes accordingly.
func NewListBox ¶
NewListBox returns a list box sized to bounds with the given items and an optional vertical scrollbar. Pass a nil scrollbar to omit it.
func (*ListBox) Items ¶
Items returns the current item slice. The slice is shared with the list box; callers must not modify it concurrently with redraws.
type ListViewer ¶
type ListViewer struct {
*view.View
Range int
Focused int
HScroll *ScrollBar
VScroll *ScrollBar
GetText GetTextFunc
// contains filtered or unexported fields
}
ListViewer is the scrolling-list base. It owns the focused item, the top-visible row, optional scrollbars, and a GetText function that yields the row text. The view paints rows top-down, highlights the focused row when StateFocused is set, and broadcasts no commands; concrete lists wire commands themselves.
func NewListViewer ¶
func NewListViewer(bounds vio.Rect, vScroll, hScroll *ScrollBar) *ListViewer
NewListViewer returns a list viewer sized to bounds. The caller must set GetText (and Range) before the first Draw, otherwise rows render blank.
func (*ListViewer) Draw ¶
func (l *ListViewer) Draw(s *vio.Surface)
Draw paints all visible rows. Rows beyond Range are blanked. The focused row is highlighted when the view holds StateFocused.
func (*ListViewer) FocusItem ¶
func (l *ListViewer) FocusItem(i int)
FocusItem moves the focused row to i (clamped) and pages the view if i would be off-screen.
func (*ListViewer) HandleEvent ¶
func (l *ListViewer) HandleEvent(e *event.Event)
HandleEvent moves Focused with the arrow keys, pages with PgUp/Dn, jumps with Home/End, and selects on mouse-down inside the bounds. It also listens for CmdScrollBarChanged broadcasts from its own vertical scrollbar and snaps Focused to the new value.
func (*ListViewer) SetRange ¶
func (l *ListViewer) SetRange(n int)
SetRange updates Range and clamps Focused and topItem to fit. It also reconfigures the vertical scrollbar if one is attached.
func (*ListViewer) TopItem ¶
func (l *ListViewer) TopItem() int
TopItem returns the first visible row index.
type Memo ¶
Memo is a multi-line text editor. Lines are stored as a slice of rune slices; the cursor is a (line, col) pair. v0.2 phase 4 covers printable insertion, Backspace/Delete, Enter inserts a newline, Tab inserts spaces, arrow/Home/End/PgUp/PgDn navigation, and viewport scrolling. Undo, clipboard, word-wrap, and selection ride along in later phases.
func NewMemo ¶
NewMemo returns a Memo sized to bounds with one empty line. TabWidth defaults to 4; callers can change it before the first edit.
func (*Memo) HandleEvent ¶
HandleEvent dispatches one event. Non-key events fall through.
func (*Memo) Lines ¶
Lines returns the current line slice. The slice and its rune slices are shared with the memo; callers must not mutate concurrently.
func (*Memo) SetString ¶
SetString replaces the buffer with s, splitting on newlines, and puts the cursor at the end of the last line.
type Orientation ¶
type Orientation uint8
Orientation distinguishes a horizontal scrollbar (W > H) from a vertical one (H >= W). Callers don't construct this directly; the ScrollBar constructor picks the orientation from the bounds.
const ( Horizontal Orientation = iota Vertical )
Orientation values.
type ParamText ¶
ParamText is a read-only text view whose payload is rendered through fmt.Sprintf. Use it for status fields with live values: "Line %d, Col %d" and so on. The format string is fixed at construction; SetParams updates the slice of values, redraw paints the formatted result.
func NewParamText ¶
NewParamText returns a ParamText sized to bounds with the given format string. The initial parameter list is empty.
func (*ParamText) Draw ¶
Draw paints the formatted text. Lines longer than the bounds width are clipped; only the first row is used.
type RadioButtons ¶
type RadioButtons struct {
*Cluster
}
RadioButtons is a Cluster with single selection: Value is the index of the chosen item.
func NewRadioButtons ¶
func NewRadioButtons(bounds vio.Rect, items []string) *RadioButtons
NewRadioButtons returns a radio-button cluster sized to bounds.
func (*RadioButtons) Draw ¶
func (r *RadioButtons) Draw(s *vio.Surface)
Draw paints the radio-button list.
func (*RadioButtons) HandleEvent ¶
func (r *RadioButtons) HandleEvent(e *event.Event)
HandleEvent moves focus, selects on Space/Enter, and dispatches Alt-mnemonic. Selecting an item also moves focus to that item.
func (*RadioButtons) Mark ¶
func (r *RadioButtons) Mark(i int) bool
Mark reports whether item i is the chosen one.
type ScrollBar ¶
type ScrollBar struct {
*view.View
Min, Max int
Value int
PgStep int
ArStep int
// contains filtered or unexported fields
}
ScrollBar is the strip with arrow caps at each end and a thumb that represents the current value inside the [Min, Max] range. PgStep is the increment when the user clicks the page area; ArStep is the increment for the arrow caps. Whenever Value changes, ScrollBar broadcasts a CmdScrollBarChanged command via its owner.
func NewScrollBar ¶
NewScrollBar returns a scrollbar sized to bounds. Orientation is horizontal when bounds.W > bounds.H. Step defaults are 1 for the arrow caps and max(1, length-2) for the page region.
func (*ScrollBar) HandleEvent ¶
HandleEvent maps mouse-down on the arrow caps to ArStep increments and clicks on the page region to PgStep jumps. Releases and drag are not implemented in v0.2 phase 4; they ride along in a later polish pass.
func (*ScrollBar) Orientation ¶
func (s *ScrollBar) Orientation() Orientation
Orientation returns whether the bar is horizontal or vertical.
type StaticText ¶
StaticText is a non-selectable text view that paints its Text into its bounds. Newlines start new rows; lines longer than the bounds width are clipped (no word-wrap in v0.2 phase 3).
func NewStaticText ¶
func NewStaticText(bounds vio.Rect, text string) *StaticText
NewStaticText returns a StaticText sized to bounds with the given text.
func (*StaticText) Draw ¶
func (s *StaticText) Draw(surf *vio.Surface)
Draw paints the static text. Empty text leaves the bounds blank.