From 92caa6344f71f3681ed255b56ecfa9294f497ab9 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 27 Jan 2022 18:46:46 -0500 Subject: tk: Add border with focus indication to button --- (limited to 'src/tk/button.c') diff --git a/src/tk/button.c b/src/tk/button.c index c0a0c61..fa0cb65 100644 --- a/src/tk/button.c +++ b/src/tk/button.c @@ -26,6 +26,7 @@ struct mftk_button { struct mftk_widget parent; struct mftk_widget *label; int padding; + int border; int (*action)(void *); void *user_data; }; @@ -37,8 +38,8 @@ _mftk_button_layout(struct mftk_widget *w) mftk_widget_layout(b->label); - w->w = b->label->w + b->padding * 2; - w->h = b->label->h + b->padding * 2; + w->w = b->label->w + b->padding * 2 + b->border * 2; + w->h = b->label->h + b->padding * 2 + b->border * 2; } static void @@ -107,11 +108,32 @@ _mftk_button_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) { struct mftk_button *b = (struct mftk_button *) w; SDL_Rect rect; + SDL_Color border; rect.x = x; rect.y = y; rect.w = w->w; rect.h = w->h; + if (w->focused == SDL_TRUE) { + border.r = MFTK_COLOR_BDRF_R, border.g = MFTK_COLOR_BDRF_G; + border.b = MFTK_COLOR_BDRF_B, border.a = MFTK_COLOR_BDRF_A; + } else { + border.r = MFTK_COLOR_BDRN_R, border.g = MFTK_COLOR_BDRN_G; + border.b = MFTK_COLOR_BDRN_B, border.a = MFTK_COLOR_BDRN_A; + } + if (SDL_SetRenderDrawColor(renderer, + border.r, border.g, border.b, border.a) < 0 || + SDL_RenderFillRect(renderer, &rect) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't render widget: %s", + SDL_GetError()); + return -1; + } + + rect.x += b->border; + rect.y += b->border; + rect.w -= b->border * 2; + rect.h -= b->border * 2; if (SDL_SetRenderDrawColor(renderer, MFTK_COLOR_BACK_R, MFTK_COLOR_BACK_G, MFTK_COLOR_BACK_B, MFTK_COLOR_BACK_A) < 0 || @@ -139,7 +161,7 @@ _mftk_button_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_button_new(TTF_Font *font, const char *text, int padding, +mftk_button_new(TTF_Font *font, const char *text, int padding, int border, int (*action)(void *), void *user_data, SDL_Renderer *renderer) { struct mftk_widget *w; @@ -154,6 +176,7 @@ mftk_button_new(TTF_Font *font, const char *text, int padding, } b->padding = padding; + b->border = border; b->action = action; b->user_data = user_data; -- cgit v0.9.1