diff options
| author | Arslaan Pathan <[email protected]> | 2026-05-01 21:38:49 +1200 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2026-05-01 21:38:49 +1200 |
| commit | 6ad244502d1c3c09a26012c5e7fad901c95cc38e (patch) | |
| tree | b0b8106fca78a49e47313f35d8245eeb6980d3ac /src/saffron_button.c | |
| parent | ccb8f9316c9bc7aded2dd5d74d2e82445bf2f647 (diff) | |
| download | saffron-6ad244502d1c3c09a26012c5e7fad901c95cc38e.tar.xz saffron-6ad244502d1c3c09a26012c5e7fad901c95cc38e.zip | |
Fix the buttons and implement hooks for future webkit bindings to build on top of
Diffstat (limited to 'src/saffron_button.c')
| -rw-r--r-- | src/saffron_button.c | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/saffron_button.c b/src/saffron_button.c index bef0018..b7db204 100644 --- a/src/saffron_button.c +++ b/src/saffron_button.c @@ -7,9 +7,9 @@ static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) { SaffronTheme* theme = widget->theme; if (!theme) return; - SaffronColor secondary = theme->secondary; + SaffronColor bg = theme->bg; SaffronColor tertiary = theme->tertiary; - SDL_SetRenderDrawColor(renderer, secondary.r, secondary.g, secondary.b, secondary.a); + SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, bg.a); SDL_FRect rect = {widget->x, widget->y, widget->w, widget->h}; SDL_RenderFillRect(renderer, &rect); @@ -17,16 +17,41 @@ static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) { SDL_RenderRect(renderer, &rect); } +static void saffron_button_onclick(SaffronWidget* self) { + SaffronButton* btn = (SaffronButton*)self; + btn->callback(btn); +} + SaffronButton* saffron_button_new(bool enabled, void (*callback)(SaffronButton* self), int width, int height) { SaffronButton* button = malloc(sizeof(SaffronButton)); - saffron_widget_init((SaffronWidget*)button); + if (!button) return NULL; + + SaffronBox* box = saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 5, 5, 0, width, height); + if (!box) { + free(button); + return NULL; + } + + memcpy(&button->base, box, sizeof(SaffronBox)); + free(box); - ((SaffronWidget*)button)->type = SAFFRON_WIDGET_BUTTON; - ((SaffronWidget*)button)->draw = saffron_button_draw; + SaffronWidget* widget = (SaffronWidget*)button; + // We set it to type BOX because the layout engine needs to treat it as one: I'll remove the BUTTON enum later or make the layout engine check for both + widget->type = SAFFRON_WIDGET_BOX; + widget->draw = saffron_button_draw; + widget->on_click = saffron_button_onclick; + widget->children = NULL; + widget->child_count = 0; button->callback = callback; button->enabled = enabled; - button->base = *saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 5, 5, 0, width, height); + + return button; +} + +SaffronButton* saffron_button_new_with_text(SaffronText* text, bool enabled, void (*callback)(SaffronButton* self), int width, int height) { + SaffronButton* button = saffron_button_new(enabled, callback, width, height); + saffron_widget_add_child((SaffronWidget*)button, (SaffronWidget*)text); return button; } |
