Text input#

CEUI has a one line text field, ui_input, and a multi-line text area, ui_textarea. Both edit a zero-terminated char buffer of yours in place.

Text fields#

static char name[17];

if (ui_input("Name", name, sizeof name, UI_INPUT_UPPER)) submit(name);

size is the size of the whole buffer, terminator included, so name above holds up to 16 characters. When the buffer is full, further characters are ignored; nothing is ever written past size. The buffer must hold a valid string before the first frame; a static or zeroed buffer is an empty string.

ui_input returns true when the user presses [enter] in the field, and focus then moves to the next widget. That makes a form easy to fill from top to bottom.

The label is drawn on the left like other labeled controls. Pass NULL for a field the full width of its column.

Flags#

Flag

Effect

0

Starts in lower case mode

UI_INPUT_UPPER

Starts in upper case mode

UI_INPUT_NUMERIC

Starts in number mode and stays there; [alpha] does nothing

UI_INPUT_SECRET

Draws a * for each character, for passwords

Combine them with |, for example UI_INPUT_NUMERIC | UI_INPUT_SECRET for a PIN.

Modes#

A field has three modes. [alpha] cycles through them, and the focused field shows the mode letter at its right end.

Mode

Shown as

Keys type

Upper case

A

The green letter printed above each key, in upper case

Lower case

a

The same letters in lower case

Numbers

1

The digit or symbol printed on the key

Only the focused field has a cursor and a mode. When focus enters a field, the cursor goes to the end of the text and the mode goes back to the one its flags start in.

A name field with text typed in three modes, showing the mode letter 1

A, then [alpha] and b, then [alpha] again and 12. The field shows mode 1 on the right.#

Keys#

Key

Effect

Letter keys

Type a character from the key map below

[left], [right]

Move the cursor. A text field keeps both arrows, so they never move focus

[del]

Delete the character before the cursor. It repeats while held

[clear]

Empty the field. On an empty field the key goes on to your program

[up], [down]

Leave the field

[enter]

Submit: ui_input returns true and focus moves to the next widget

[alpha]

Next mode

Key map#

In A and a modes:

Key

Types

Key

Types

Key

Types

[math]

A

[apps]

B

[prgm]

C

[x⁻¹]

D

[sin]

E

[cos]

F

[tan]

G

[^]

H

[x²]

I

[,]

J

[(]

K

[)]

L

[÷]

M

[log]

N

[7]

O

[8]

P

[9]

Q

[×]

R

[ln]

S

[4]

T

[5]

U

[6]

V

[-]

W

[sto→]

X

[1]

Y

[2]

Z

[0]

space

[.]

:

[(-)]

?

[+]

"

[3], which has θ above it, types nothing.

In 1 mode, keys type 0 to 9, ., (, ), ,, +, -, *, / and ^, and [(-)] types -.

What is shown#

The field scrolls its text sideways so the cursor stays visible. It shows at most 80 characters around the cursor; longer buffers are still edited correctly.

A password field showing three stars and a caption under it

A secret field, with a caption shown only while it has focus.#

Text areas#

static char notes[400];
static ui_scroll_t notes_scroll;

ui_textarea(notes, sizeof notes, &notes_scroll, 72);
ui_caption(ui_fmt("%u of %u characters", (unsigned)strlen(notes), (unsigned)(sizeof notes - 1)));

The same editor over several wrapped lines, in a box h pixels high with its own scroll bar. It returns true in the frame the text changed.

The differences from a text field:

  • [enter] inserts a line break.

  • [up] and [down] move the cursor between lines, keeping its horizontal position. They leave the area at the first and the last line.

  • [clear] empties the whole text.

  • It starts in lower case mode, and [alpha] cycles the modes as in a text field.

  • The ui_scroll_t holds how far the area is scrolled. Keep it with the buffer. The area scrolls to keep the cursor visible.

A text area lays out at most 64 lines.

A notes area with two lines of text

Text in the notes area, with a line break typed with [enter].#

A complete form#

static struct {
  char name[17], password[17];
  int age;
  bool problem_open;
} form = {.age = 16};

ui_scroll_begin(&scroll, UI_FILL);
ui_input("Name", form.name, sizeof form.name, UI_INPUT_UPPER);
ui_input("Password", form.password, sizeof form.password, UI_INPUT_SECRET);
ui_number("Age", &form.age, 5, 120);
if (ui_button("Create")) form.problem_open = strlen(form.password) < 4;
ui_scroll_end();
ui_message(&form.problem_open, "Check the form", "The password needs at least 4 characters.");
The Forms screen with the name field focused

The Forms screen of the showcase, starting on the name field in upper case mode.#