From a88d71431d4a41a10df3c0a9baa4223688bbc50b Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 06 Aug 2021 19:53:44 -0400 Subject: tk: Handle text input --- (limited to 'src/tk') diff --git a/src/tk/text.c b/src/tk/text.c index 1a84a14..c624678 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -29,7 +29,7 @@ struct mftk_text { struct mftk_widget parent; char min_char; char max_char; - size_t len; + int len; int cur; int y; int w; @@ -52,11 +52,13 @@ _mftk_text_layout(struct mftk_widget *w __attribute__((__unused__))) static void _mftk_text_focus(struct mftk_widget *w __attribute__((__unused__))) { + SDL_StartTextInput(); } static void _mftk_text_defocus(struct mftk_widget *w __attribute__((__unused__))) { + SDL_StopTextInput(); } static int @@ -65,6 +67,8 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e) struct mftk_text *t = (struct mftk_text *) w; int len; int i; + int newlen; + int j; switch (e->type) { case SDL_KEYDOWN: @@ -115,6 +119,27 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e) break; } break; + case SDL_TEXTEDITING: + /* TODO */ + break; + case SDL_TEXTINPUT: + len = strlen(t->val); + newlen = strlen(e->text.text); + if (len + newlen > t->len) { + newlen = t->len - len; + } + for (i = 0, j = 0; + i < newlen && e->text.text[j] != '\0'; + ++j) { + if (e->text.text[j] < t->min_char) continue; + if (e->text.text[j] > t->max_char) continue; + t->val[t->cur + i] = e->text.text[j]; + ++i; + } + t->cur += i; + SDL_DestroyTexture(t->texture); + t->texture = NULL; + break; default: break; } @@ -246,7 +271,7 @@ _mftk_text_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_text_new(char min_char, char max_char, size_t len, const char *val, +mftk_text_new(char min_char, char max_char, int len, const char *val, TTF_Font *font, SDL_Color *color) { struct mftk_widget *w; diff --git a/src/tk/window.c b/src/tk/window.c index 651a37b..5062f24 100644 --- a/src/tk/window.c +++ b/src/tk/window.c @@ -95,6 +95,9 @@ mftk_window_event(struct mftk_window *w, SDL_Event *e) e); } break; + case SDL_TEXTEDITING: + case SDL_TEXTINPUT: + return mftk_widget_key_event(w->focus, e); case SDL_MOUSEBUTTONUP: return mftk_widget_mouse_event(w->root, e, 0, 0); default: -- cgit v0.9.1