Mercurial > molko
diff libmlk-ui/mlk/ui/checkbox.h @ 606:35e58271d820
ui: do the same with checkbox
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 11 Aug 2023 12:58:02 +0200 |
parents | 6e8f6640e05b |
children | b8fd5c112538 |
line wrap: on
line diff
--- a/libmlk-ui/mlk/ui/checkbox.h Fri Aug 11 19:22:00 2023 +0200 +++ b/libmlk-ui/mlk/ui/checkbox.h Fri Aug 11 12:58:02 2023 +0200 @@ -19,49 +19,181 @@ #ifndef MLK_UI_CHECKBOX_H #define MLK_UI_CHECKBOX_H -union mlk_event; +/** + * \file mlk/ui/checkbox.h + * \brief GUI checkbox. + */ struct mlk_checkbox; +struct mlk_checkbox_delegate; +struct mlk_style; -struct mlk_checkbox_style { - unsigned long bg_color; - unsigned long check_color; - unsigned long border_color; - unsigned int border_size; +union mlk_event; + +/** + * \struct mlk_checkbox + * \brief UI clickable checkbox. + */ +struct mlk_checkbox { + /** + * (read-write) + * + * Position in x. + */ + int x; + + /** + * (read-write) + * + * Position in y. + */ + int y; + + /** + * (read-write) + * + * Checkbox width. + */ + unsigned int w; + + /** + * (read-write) + * + * Checkbox height. + */ + unsigned int h; + + /** + * (read-write) + * + * Checkbox status, non-zero if activated. + */ + int checked; + + /** + * (read-write, borrowed) + * + * Checkbox delegate. + */ + struct mlk_checkbox_delegate *delegate; + + /** + * (read-write, borrowed) + * + * Checkbox style. + */ + struct mlk_style *style; }; +/** + * \struct mlk_checkbox_delegate + * \brief Checkbox delegate. + */ struct mlk_checkbox_delegate { + /* + * (read-write, borrowed, optional) + * + * Arbitrary user data. + */ void *data; - void (*update)(struct mlk_checkbox_delegate *, struct mlk_checkbox *, unsigned int); - void (*draw)(struct mlk_checkbox_delegate *, const struct mlk_checkbox *); + + /** + * (read-write, optional) + * + * Handle an event. + * + * \param self this delegate + * \param cb the checkbox + * \param ev the event + * \return the current checkbox status (1 or 0) + */ + int (*handle)(struct mlk_checkbox_delegate *self, + struct mlk_checkbox *cb, + const union mlk_event *ev); + + /** + * (read-write, optional) + * + * Update the checkbox. + * + * \param self this delegate + * \param cb the checkbox to update + * \param ticks number of ticks since last frame + */ + void (*update)(struct mlk_checkbox_delegate *self, + struct mlk_checkbox *cb, + unsigned int ticks); + + /** + * (read-write, optional) + * + * Draw this checkbox. + * + * \param self this delegate + * \param cb the checkbox to update + */ + void (*draw)(struct mlk_checkbox_delegate *self, + const struct mlk_checkbox *cb); + + /** + * (read-write, optional) + * + * Cleanup this delegate associated with the checkbox. + * + * \param self this delegate + * \param cb the underlying checkbox + */ + void (*finish)(struct mlk_checkbox_delegate *self, + struct mlk_checkbox *cb); }; -struct mlk_checkbox { - int x, y; - unsigned int w, h; - int checked; - struct mlk_checkbox_delegate *delegate; - struct mlk_checkbox_style *style; -}; - -extern struct mlk_checkbox_style mlk_checkbox_style; +/** + * \brief Default stateless delegate for checkbox. + */ extern struct mlk_checkbox_delegate mlk_checkbox_delegate; #if defined(__cplusplus) extern "C" { #endif -int -mlk_checkbox_ok(const struct mlk_checkbox *cb); +/** + * Initialize the checkbox with default values. + * + * This is not required if you use designated initializers. + * + * \pre cb != NULL + * \param cb the checkbox to default initialize + * \param st style to use (or NULL to use a default) + * \param dt delegate to use (or NULL to use a default) + */ +void +mlk_checkbox_init(struct mlk_checkbox *cb, + struct mlk_checkbox_delegate *dt, + struct mlk_style *st); +/** + * Invoke ::mlk_checkbox_delegate::update. + */ int -mlk_checkbox_handle(struct mlk_checkbox *, const union mlk_event *); +mlk_checkbox_handle(struct mlk_checkbox *cb, const union mlk_event *ev); +/** + * Invoke ::mlk_checkbox_delegate::update. + */ void mlk_checkbox_update(struct mlk_checkbox *cb, unsigned int ticks); +/** + * Invoke ::mlk_checkbox_delegate::draw. + */ void -mlk_checkbox_draw(const struct mlk_checkbox *); +mlk_checkbox_draw(const struct mlk_checkbox *cb); + +/** + * Invoke ::mlk_checkbox_delegate::finish. + */ +void +mlk_checkbox_finish(struct mlk_checkbox *cb); #if defined(__cplusplus) }