gui/line_edit: Allow setting input filters
This commit is contained in:
parent
f5c7e88b26
commit
e00ed40c9c
|
@ -13,6 +13,13 @@ extern "C"
|
|||
{
|
||||
#endif
|
||||
|
||||
enum gui_line_edit_filter
|
||||
{
|
||||
GUI_LINE_EDIT_FILTER_NONE,
|
||||
GUI_LINE_EDIT_FILTER_ALPHA = 1 << 1,
|
||||
GUI_LINE_EDIT_FILTER_NUM = 1 << 2
|
||||
};
|
||||
|
||||
struct gui_line_edit
|
||||
{
|
||||
struct gui_common common;
|
||||
|
@ -22,6 +29,8 @@ struct gui_line_edit
|
|||
unsigned blt;
|
||||
char *text;
|
||||
size_t i, sz;
|
||||
/* Use with gui_line_edit_filter. */
|
||||
int filters;
|
||||
};
|
||||
|
||||
void gui_line_edit_init(struct gui_line_edit *l, char *buf, size_t sz);
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <mouse.h>
|
||||
#include <pad.h>
|
||||
#include <peripheral.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
struct sprite gui_line_edit_sprites[MAX_GUI_LINE_EDIT_SPRITES];
|
||||
|
@ -115,8 +116,20 @@ static void get_dim(const struct gui_common *const g,
|
|||
static void on_char(const char ch, void *const user)
|
||||
{
|
||||
struct gui_line_edit *const l = user;
|
||||
bool allowed = false;
|
||||
|
||||
if (l->i + 1 < l->sz)
|
||||
if (l->filters)
|
||||
{
|
||||
if (((l->filters & GUI_LINE_EDIT_FILTER_ALPHA)
|
||||
&& isalpha((unsigned char)ch))
|
||||
|| ((l->filters & GUI_LINE_EDIT_FILTER_NUM)
|
||||
&& isdigit((unsigned char)ch)))
|
||||
allowed = true;
|
||||
}
|
||||
else
|
||||
allowed = true;
|
||||
|
||||
if (allowed && l->i + 1 < l->sz)
|
||||
l->text[l->i++] = ch;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue