GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_widget_tbox.h
1//SPDX-License-Identifier: LGPL-2.0-or-later
2
3/*
4
5 Copyright (c) 2014-2021 Cyril Hrubis <metan@ucw.cz>
6
7 */
8
9#ifndef GP_WIDGET_TBOX_H
10#define GP_WIDGET_TBOX_H
11
12#include <utils/gp_seek.h>
13#include <utils/gp_utf_pos.h>
14
15enum gp_widget_tbox_type {
16 /* default */
17 GP_WIDGET_TBOX_NONE,
18 /* hidden text, e.g. password */
19 GP_WIDGET_TBOX_HIDDEN,
20 /* URL */
21 GP_WIDGET_TBOX_URL,
22 /* Filesystem path */
23 GP_WIDGET_TBOX_PATH,
24 /* File name */
25 GP_WIDGET_TBOX_FILENAME,
26 /* Terminator */
27 GP_WIDGET_TBOX_MAX,
28};
29
30struct gp_widget_tbox {
31 /* Text buffer */
32 char *buf;
33 size_t size;
34
35 /* Help text shown when tbox is empty */
36 char *help;
37
38 /*
39 * If not NULL the tbox can contain only characters from this
40 * string, this is used as a hint when minimal tbox size is
41 * accounted for.
42 */
43 const char *filter;
44
45 /*
46 * Delimiter list for double click selection.
47 *
48 * If NULL defaults to whitespaces.
49 *
50 * This is set automatically by a certain tbox types.
51 */
52 const char *delim;
53
54 /* enum gp_widget_tbox_type */
55 uint16_t type;
56
57 uint16_t alert:1;
58 uint16_t clear_on_input:1;
59
60 size_t max_size;
61
62 /* Cursor position */
63 gp_utf8_pos cur_pos;
64 gp_utf8_pos cur_pos_saved;
65 /* Offset on left size, part of a string that is not shown */
66 gp_utf8_pos off_left;
67
68 /* Selection */
69 gp_utf8_pos sel_left;
70 gp_utf8_pos sel_right;
71
72 gp_widget_tattr tattr;
73
74
75 //TODO: Move to event state
76 uint64_t last_click;
77 uint32_t click_cursor_x;
78
79 char payload[];
80};
81
85enum gp_widget_tbox_event_type {
87 GP_WIDGET_TBOX_TRIGGER,
89 GP_WIDGET_TBOX_PRE_FILTER,
91 GP_WIDGET_TBOX_POST_FILTER,
93 GP_WIDGET_TBOX_EDIT,
103 GP_WIDGET_TBOX_SET,
105 GP_WIDGET_TBOX_PASTE,
106};
107
108#define GP_TBOX_FILTER_INT "0123456789"
109#define GP_TBOX_FILTER_HEX "0123456789abcdefABCDEF"
110
124gp_widget *gp_widget_tbox_new(const char *text, gp_widget_tattr tattr,
125 unsigned int len, unsigned int max_len,
126 const char *filter, enum gp_widget_tbox_type type);
127
137int gp_widget_tbox_printf(gp_widget *self, const char *fmt, ...)
138 __attribute__((format (printf, 2, 3)));
139
140
147void gp_widget_tbox_set(gp_widget *self, const char *str);
148
154void gp_widget_tbox_clear(gp_widget *self);
155
162const char *gp_widget_tbox_text(gp_widget *self);
163
164
171static inline int gp_widget_tbox_is_empty(gp_widget *self)
172{
173 const char *text = gp_widget_tbox_text(self);
174
175 if (!text || !text[0])
176 return 1;
177
178 return 0;
179}
180
187gp_utf8_pos gp_widget_tbox_cursor_get(gp_widget *self);
188
200void gp_widget_tbox_cursor_set(gp_widget *self, ssize_t off,
201 enum gp_seek_whence whence);
202
216void gp_widget_tbox_ins(gp_widget *self, ssize_t off,
217 enum gp_seek_whence whence, const char *str);
218
225static inline void gp_widget_tbox_append(gp_widget *self, const char *str)
226{
227 gp_widget_tbox_ins(self, 0, GP_SEEK_CUR, str);
228}
229
243void gp_widget_tbox_del(gp_widget *self, ssize_t off,
244 enum gp_seek_whence whence, size_t len);
245
256void gp_widget_tbox_sel_set(gp_widget *self, ssize_t off,
257 enum gp_seek_whence whence, size_t len);
258
266void gp_widget_tbox_sel_all(gp_widget *self);
267
273void gp_widget_tbox_sel_clr(gp_widget *self);
274
280void gp_widget_tbox_sel_del(gp_widget *self);
281
290gp_utf8_pos gp_widget_tbox_sel_len(gp_widget *self);
291
300gp_utf8_pos gp_widget_tbox_sel_off(gp_widget *self);
301
307static inline int gp_widget_tbox_sel(gp_widget *self)
308{
309 return !!gp_widget_tbox_sel_len(self).bytes;
310}
311
323void gp_widget_tbox_sel_delim_set(gp_widget *self, const char *delim);
324
336void gp_widget_tbox_type_set(gp_widget *self, enum gp_widget_tbox_type type);
337
346void gp_widget_tbox_help_set(gp_widget *self, const char *help);
347
353void gp_widget_tbox_clear_on_input(gp_widget *self);
354
355#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.
Position in an UTF-8 string.
Definition gp_utf_pos.h:25
size_t bytes
Definition gp_utf_pos.h:27