GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_widget_tbox.h
Go to the documentation of this file.
1//SPDX-License-Identifier: LGPL-2.0-or-later
2
3/*
4
5 Copyright (c) 2014-2024 Cyril Hrubis <metan@ucw.cz>
6
7 */
8
27#ifndef GP_WIDGET_TBOX_H
28#define GP_WIDGET_TBOX_H
29
30#include <utils/gp_seek.h>
31#include <utils/gp_utf_pos.h>
32
69
70struct gp_widget_tbox {
71 /* Text buffer */
72 char *buf;
73 size_t size;
74
75 /* Help text shown when tbox is empty */
76 char *help;
77
78 /*
79 * If not NULL the tbox can contain only characters from this
80 * string, this is used as a hint when minimal tbox size is
81 * accounted for.
82 */
83 const char *filter;
84
85 /*
86 * Delimiter list for double click selection.
87 *
88 * If NULL defaults to whitespaces.
89 *
90 * This is set automatically by a certain tbox types.
91 */
92 const char *delim;
93
94 /* enum gp_widget_tbox_type */
95 uint16_t type;
96
97 uint16_t alert:1;
98 uint16_t clear_on_input:1;
99
100 size_t max_size;
101
102 /* Cursor position */
103 gp_utf8_pos cur_pos;
104 gp_utf8_pos cur_pos_saved;
105 /* Offset on left size, part of a string that is not shown */
106 gp_utf8_pos off_left;
107
108 /* Selection */
109 gp_utf8_pos sel_left;
110 gp_utf8_pos sel_right;
111
112 gp_widget_tattr tattr;
113
114
115 //TODO: Move to event state
116 uint64_t last_click;
117 uint32_t click_cursor_x;
118
119 char payload[];
120};
121
177
178#define GP_TBOX_FILTER_INT "0123456789"
179#define GP_TBOX_FILTER_HEX "0123456789abcdefABCDEF"
180
194 unsigned int len, unsigned int max_len,
195 const char *filter, enum gp_widget_tbox_type type);
196
206int gp_widget_tbox_printf(gp_widget *self, const char *fmt, ...)
207 __attribute__((format (printf, 2, 3)));
208
209
216void gp_widget_tbox_set(gp_widget *self, const char *str);
217
224
233
234
242static inline int gp_widget_tbox_is_empty(gp_widget *self)
243{
244 const char *text = gp_widget_tbox_text(self);
245
246 if (!text || !text[0])
247 return 1;
248
249 return 0;
250}
251
260
272void gp_widget_tbox_cursor_set(gp_widget *self, ssize_t off,
273 enum gp_seek_whence whence);
274
288void gp_widget_tbox_ins(gp_widget *self, ssize_t off,
289 enum gp_seek_whence whence, const char *str);
290
297static inline void gp_widget_tbox_append(gp_widget *self, const char *str)
298{
299 gp_widget_tbox_ins(self, 0, GP_SEEK_CUR, str);
300}
301
315void gp_widget_tbox_del(gp_widget *self, ssize_t off,
316 enum gp_seek_whence whence, size_t len);
317
328void gp_widget_tbox_sel_set(gp_widget *self, ssize_t off,
329 enum gp_seek_whence whence, size_t len);
330
339
346
353
362
370
378static inline int gp_widget_tbox_sel(gp_widget *self)
379{
380 return !!gp_widget_tbox_sel_len(self).bytes;
381}
382
394void gp_widget_tbox_sel_delim_set(gp_widget *self, const char *delim);
395
408
419void gp_widget_tbox_help_set(gp_widget *self, const char *help);
420
427
428#endif /* GP_WIDGET_TBOX_H */
Seek contants and transformations.
gp_seek_whence
Seek constants.
Definition gp_seek.h:24
An iterator for an UTF-8 string.
gp_widget_tattr
Text attribute flags.
void gp_widget_tbox_help_set(gp_widget *self, const char *help)
Sets textbox help text.
void gp_widget_tbox_ins(gp_widget *self, ssize_t off, enum gp_seek_whence whence, const char *str)
Inserts a string into a tbox.
void gp_widget_tbox_sel_all(gp_widget *self)
Selects whole textbox text.
void gp_widget_tbox_clear_on_input(gp_widget *self)
Sets one time flag that clears the text on next input event.
const char * gp_widget_tbox_text(gp_widget *self)
Returns a tbox string.
void gp_widget_tbox_sel_set(gp_widget *self, ssize_t off, enum gp_seek_whence whence, size_t len)
Sets selection.
void gp_widget_tbox_sel_delim_set(gp_widget *self, const char *delim)
Sets textbox selection delimiters.
static int gp_widget_tbox_is_empty(gp_widget *self)
Returns true if tbox is empty.
void gp_widget_tbox_sel_del(gp_widget *self)
Deletes selected characters.
void gp_widget_tbox_cursor_set(gp_widget *self, ssize_t off, enum gp_seek_whence whence)
Moves a cursor to a defined position.
static void gp_widget_tbox_append(gp_widget *self, const char *str)
Appends a string to the textbox.
int gp_widget_tbox_printf(gp_widget *self, const char *fmt,...) __attribute__((format(printf
A printf-like function to set a tbox text.
gp_widget * gp_widget_tbox_new(const char *text, gp_widget_tattr tattr, unsigned int len, unsigned int max_len, const char *filter, enum gp_widget_tbox_type type)
Create a tbox widget.
gp_widget_tbox_event_type
A gp_widget_event::sub_type for tbox widget events.
@ GP_WIDGET_TBOX_POST_FILTER
Emitted to filter out a character after text modified.
@ GP_WIDGET_TBOX_SET
Emitted when text box is modified.
@ GP_WIDGET_TBOX_EDIT
Emitted after text is entered.
@ GP_WIDGET_TBOX_PRE_FILTER
Emitted to filter out a character before text was modified.
@ GP_WIDGET_TBOX_TRIGGER
Emitted when enter is pressed.
@ GP_WIDGET_TBOX_PASTE
Emitted before text is pasted.
gp_utf8_pos gp_widget_tbox_cursor_get(gp_widget *self)
Returns current cursor postion.
gp_utf8_pos gp_widget_tbox_sel_off(gp_widget *self)
Returns selection offset, i.e. first character.
void gp_widget_tbox_type_set(gp_widget *self, enum gp_widget_tbox_type type)
Sets textbox type.
static int gp_widget_tbox_sel(gp_widget *self)
Returns true if text is selected.
void gp_widget_tbox_sel_clr(gp_widget *self)
Clears selection.
int void gp_widget_tbox_set(gp_widget *self, const char *str)
Sets a tbox text.
gp_utf8_pos gp_widget_tbox_sel_len(gp_widget *self)
Returns selection lenght.
gp_widget_tbox_type
Text box type.
@ GP_WIDGET_TBOX_PATH
A filesystem path.
@ GP_WIDGET_TBOX_FILENAME
A file name.
@ GP_WIDGET_TBOX_HIDDEN
Hidden text, e.g. password.
@ GP_WIDGET_TBOX_NONE
No type, this is default.
@ GP_WIDGET_TBOX_URL
An URL.
void gp_widget_tbox_clear(gp_widget *self)
Clears the tbox text.
void gp_widget_tbox_del(gp_widget *self, ssize_t off, enum gp_seek_whence whence, size_t len)
Deletes a len characters from a tbox.
Position in an UTF-8 string.
Definition gp_utf_pos.h:25
size_t bytes
Definition gp_utf_pos.h:27
A widget base.
Definition gp_widget.h:28