From 7400b756adb8c65e5e30d4a46dbd052d547b6104 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sat, 07 Aug 2021 19:48:44 -0400 Subject: tk: Make text widget optionally static --- (limited to 'src/tk/text.c') diff --git a/src/tk/text.c b/src/tk/text.c index 3b95d2a..ccfaf7f 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -41,6 +41,7 @@ struct mftk_text { int ascent; SDL_Color *color; SDL_Texture *texture; + int editable; int (*action)(void *, const char *); int (*submit)(void *); void *user_data; @@ -169,6 +170,12 @@ _mftk_text_mouse_event(struct mftk_widget *w, SDL_Event *e, int x __attribute__((__unused__)), int y __attribute__((__unused__))) { + struct mftk_text *t = (struct mftk_text *) w; + + if (t->editable == SDL_FALSE) { + return 0; + } + switch (e->type) { case SDL_MOUSEBUTTONUP: if (e->button.button == SDL_BUTTON_LEFT) { @@ -257,10 +264,12 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) SDL_GetError()); return -1; } - if (SDL_SetRenderDrawColor(renderer, t->color->r, t->color->g, - t->color->b, t->color->a) < 0 || - SDL_RenderDrawLine(renderer, - x, y + w->h, x + w->w, y + w->h) < 0) { + if (t->editable == SDL_TRUE && ( + SDL_SetRenderDrawColor(renderer, + t->color->r, t->color->g, + t->color->b, t->color->a) < 0 || + SDL_RenderDrawLine(renderer, + x, y + w->h, x + w->w, y + w->h) < 0)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", SDL_GetError()); @@ -289,7 +298,7 @@ _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 editable, int (*action)(void *, const char *), int (*submit)(void *), void *user_data) { @@ -298,7 +307,11 @@ mftk_text_new(char min_char, char max_char, int len, const char *val, int advance; char ch; - mftk_widget_init_focusable(w, t, text); + if (editable == SDL_TRUE) { + mftk_widget_init_focusable(w, t, text); + } else { + mftk_widget_init(w, t, text); + } t->min_char = min_char; t->max_char = max_char; @@ -308,6 +321,7 @@ 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->editable = editable; t->action = action; t->submit = submit; t->user_data = user_data; @@ -350,7 +364,7 @@ mftk_text_new(char min_char, char max_char, int len, const char *val, w->w = advance; } } - w->h = t->line_skip + 1; + w->h = t->line_skip + (editable == SDL_TRUE ? 1 : 0); return w; } -- cgit v0.9.1