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 --- diff --git a/src/splash.c b/src/splash.c index 3c743a3..5c9adb7 100644 --- a/src/splash.c +++ b/src/splash.c @@ -32,6 +32,14 @@ #include "util.h" static int +_mf_splash_seed(void *user_data, const char *seed) +{ + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Seed: %ld", atol(seed)); + + return 0; +} + +static int _mf_splash_size(void *user_data, int state) { switch (state) { @@ -116,7 +124,7 @@ _mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font, mftk_label_new(text_font, "Seed", text_color, renderer), MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T, mftk_text_new('0', '9', rand_max_len, seed, text_font, - text_color), + text_color, _mf_splash_seed, NULL), MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T, mftk_label_new(text_font, "Size", text_color, renderer), MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T, diff --git a/src/tk.h b/src/tk.h index c7c5a51..28533e9 100644 --- a/src/tk.h +++ b/src/tk.h @@ -76,7 +76,8 @@ mftk_radio_new(int butn_width, int butn_padding, SDL_Color *butn_color, 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 * mftk_blank_new(void); 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