From a6908296b6ac83ab3eb5474ad6b4a509c8f5099c Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 27 Jan 2022 04:59:39 -0500 Subject: tk: Blink text cursor --- (limited to 'src/tk/text.c') diff --git a/src/tk/text.c b/src/tk/text.c index 9ba9faa..76bb27a 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -41,6 +41,8 @@ struct mftk_text { int ascent; SDL_Texture *texture; int editable; + int blink_state; + Uint32 blink_timer; int (*action)(void *, const char *); int (*submit)(void *); void *user_data; @@ -60,7 +62,12 @@ _mftk_text_layout(struct mftk_widget *w __attribute__((__unused__))) static void _mftk_text_focus(struct mftk_widget *w __attribute__((__unused__))) { + struct mftk_text *t = (struct mftk_text *) w; + SDL_StartTextInput(); + + t->blink_state = SDL_TRUE; + t->blink_timer = SDL_GetTicks(); } static void @@ -78,6 +85,9 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e) int newlen; int j; + t->blink_state = SDL_TRUE; + t->blink_timer = SDL_GetTicks(); + switch (e->type) { case SDL_KEYDOWN: switch (e->key.keysym.sym) { @@ -249,6 +259,7 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) struct mftk_text *t = (struct mftk_text *) w; SDL_Rect rect; int cur_x; + Uint32 timer; if (t->val[0] != '\0' && t->texture == NULL && _mftk_text_render_val(t, renderer) < 0) { @@ -282,7 +293,17 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) SDL_GetError()); return -1; } - if (w->focused == SDL_TRUE && SDL_RenderDrawLine(renderer, + timer = SDL_GetTicks(); + if (timer - t->blink_timer >= MFTK_CURSOR_BLINK_MS) { + if (t->blink_state == SDL_TRUE) { + t->blink_state = SDL_FALSE; + } else if (t->blink_state == SDL_FALSE) { + t->blink_state = SDL_TRUE; + } + t->blink_timer = timer; + } + if (w->focused == SDL_TRUE && t->blink_state == SDL_TRUE && + SDL_RenderDrawLine(renderer, x + cur_x, y + t->y, x + cur_x, y + t->h) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", @@ -327,6 +348,8 @@ mftk_text_new(const char *allowed_chars, int len, const char *val, t->ascent = TTF_FontAscent (font); t->texture = NULL; t->editable = editable; + t->blink_state = SDL_TRUE; + t->blink_timer = 0; t->action = action; t->submit = submit; t->user_data = user_data; -- cgit v0.9.1