Mercurial > molko
view libmlk-ui/mlk/ui/button.c @ 497:8d5a266a1774
ui: ui -> mlk_ui
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 28 Feb 2023 13:42:24 +0100 |
parents | 2af25db99273 |
children | ab2816f9551c |
line wrap: on
line source
/* * button.c -- GUI button * * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> #include <string.h> #include <mlk/core/event.h> #include <mlk/core/maths.h> #include <mlk/core/painter.h> #include <mlk/core/trace.h> #include "align.h" #include "button.h" #include "label.h" #include "theme.h" static int is_boxed(const struct mlk_button *button, const struct mlk_event_click *click) { assert(button); assert(click); assert(click->type == MLK_EVENT_CLICKDOWN || click->type == MLK_EVENT_CLICKUP); return mlk_maths_is_boxed(button->x, button->y, button->w, button->h, click->x, click->y); } void mlk_button_draw_default(const struct mlk_theme *t, const struct mlk_button *button) { assert(t); assert(button); (void)t; struct mlk_label label = { .text = button->text, }; unsigned int lw, lh; mlk_label_query(&label, &lw, &lh); if (lw > button->w) mlk_tracef("button width is too small for text: %u < %u", button->w, lw); if (lh > button->h) mlk_tracef("button height is too small for text: %u < %u", button->h, lh); mlk_align(MLK_ALIGN_CENTER, &label.x, &label.y, lw, lh, button->x, button->y, button->w, button->h); mlk_painter_set_color(0x577277ff); mlk_painter_draw_rectangle(button->x, button->y, button->w, button->h); mlk_label_draw(&label); } int mlk_button_handle(struct mlk_button *button, const union mlk_event *ev) { assert(button); assert(ev); switch (ev->type) { case MLK_EVENT_CLICKDOWN: if (is_boxed(button, &ev->click)) button->state = MLK_BUTTON_STATE_PRESSED; break; case MLK_EVENT_CLICKUP: /* * If the button was pressed, indicate that the button is * finally activated. This let the user to move the cursor * outside the button to "forget" the press. */ if (!is_boxed(button, &ev->click)) button->state = MLK_BUTTON_STATE_NONE; else if (button->state == MLK_BUTTON_STATE_PRESSED) button->state = MLK_BUTTON_STATE_ACTIVATED; break; default: break; } return button->state == MLK_BUTTON_STATE_ACTIVATED; } void mlk_button_reset(struct mlk_button *button) { assert(button); button->state = MLK_BUTTON_STATE_NONE; } void mlk_button_draw(const struct mlk_button *button) { assert(button); mlk_theme_draw_button(button->theme, button); }