From 6a45a31b8b7e1a88dc5b6354f12ad40ea43e7dd3 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 06 Aug 2021 20:04:00 -0400 Subject: tk: Add text widget event callback --- (limited to 'src/tk/text.c') diff --git a/src/tk/text.c b/src/tk/text.c index c624678..6a89b44 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -26,21 +26,23 @@ #include "widget.h" struct mftk_text { - struct mftk_widget parent; - char min_char; - char max_char; - int len; - int cur; - int y; - int w; - int h; - char *val; - char *curval; - TTF_Font *font; - int line_skip; - int ascent; - SDL_Color *color; - SDL_Texture *texture; + struct mftk_widget parent; + char min_char; + char max_char; + int len; + int cur; + int y; + int w; + int h; + char *val; + char *curval; + TTF_Font *font; + int line_skip; + int ascent; + SDL_Color *color; + SDL_Texture *texture; + int (*action)(void *, const char *); + void *user_data; }; static void @@ -103,7 +105,10 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e) } SDL_DestroyTexture(t->texture); t->texture = NULL; - break; + if (t->action == NULL) { + return 0; + } + return t->action(t->user_data, t->val); case SDLK_DELETE: len = strlen(t->val); if (t->cur >= len) { @@ -114,7 +119,10 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e) } SDL_DestroyTexture(t->texture); t->texture = NULL; - break; + if (t->action == NULL) { + return 0; + } + return t->action(t->user_data, t->val); default: break; } @@ -139,7 +147,10 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e) t->cur += i; SDL_DestroyTexture(t->texture); t->texture = NULL; - break; + if (t->action == NULL) { + return 0; + } + return t->action(t->user_data, t->val); default: break; } @@ -272,7 +283,8 @@ _mftk_text_destroy(struct mftk_widget *w) struct mftk_widget * mftk_text_new(char min_char, char max_char, int len, const char *val, - TTF_Font *font, SDL_Color *color) + TTF_Font *font, SDL_Color *color, + int (*action)(void *, const char *), void *user_data) { struct mftk_widget *w; struct mftk_text *t; @@ -290,6 +302,8 @@ mftk_text_new(char min_char, char max_char, int len, const char *val, t->ascent = TTF_FontAscent (font); t->color = color; t->texture = NULL; + t->action = action; + t->user_data = user_data; t->val = calloc(len + 1, sizeof(*t->val)); if (t->val == NULL) { -- cgit v0.9.1