123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include "zori.h"
- #include "zori_widget.h"
- #include "zori_button.h"
- struct zori_button * zori_widget_to_button(struct zori_widget * widget) {
- return ZORI_CONTAINER_OF(widget, struct zori_button, widget);
- }
- /** Handles a mouse axis event and pass it on. */
- int zori_button_on_mouse_axes(union zori_event * event) {
- struct zori_widget * widget = event->any.widget;
- struct zori_button * button = zori_widget_to_button(widget);
- float x = event->sys.ev->mouse.x;
- float y = event->sys.ev->mouse.y;
- if (zori_xy_inside_widget_p(widget, x, y)) {
- zori_widget_hover_(widget, TRUE);
- } else {
- zori_widget_hover_(widget, FALSE);
- }
-
- return ZORI_HANDLE_PASS;
- }
- /** Handles a mouse click event and pass it on. */
- int zori_button_on_mouse_click(union zori_event * event) {
- struct zori_widget * widget = event->any.widget;
- struct zori_button * button = zori_widget_to_button(widget);
- float x = event->sys.ev->mouse.x;
- float y = event->sys.ev->mouse.y;
- if (zori_xy_inside_widget_p(widget, x, y)) {
- return zori_widget_raise_action_event(widget);
- }
- return ZORI_HANDLE_PASS;
- }
- void zori_draw_button(struct zori_button * button) {
- float x, y, w, h;
- struct zori_style * style = &button->widget.style;
- x = rebox_x(&button->widget.box) + 5;
- y = rebox_y(&button->widget.box) + 5;
- w = rebox_w(&button->widget.box) - 10;
- h = rebox_h(&button->widget.box) - 10;
-
- zori_widget_draw_background(&button->widget);
- if (button->text) {
- zori_font * font = style->text.font;
- zori_color color = style->text.color;
- al_draw_multiline_ustr(font, color, x, y, w, -1, ALLEGRO_ALIGN_LEFT, button->text);
- }
-
- };
-
- int zori_button_on_draw(union zori_event * event) {
- struct zori_button * button = zori_widget_to_button(event->any.widget);
- zori_draw_button(button);
- return ZORI_HANDLE_PASS;
- }
- struct zori_handler zori_button_handlers[] = {
- { ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN , zori_button_on_mouse_click , NULL },
- { ZORI_SYSTEM_EVENT_MOUSE_AXES , zori_button_on_mouse_axes , NULL },
- { ZORI_EVENT_DRAW , zori_button_on_draw , NULL },
- { -1, NULL, NULL }
- };
- struct zori_button *
- zori_button_text_(struct zori_button * button, zori_string * text) {
- if (button->text) {
- al_ustr_free(button->text);
- button->text = NULL;
- }
- if (text) {
- button->text = al_ustr_dup(text);
- }
- return button;
- }
- struct zori_button *
- zori_button_init(struct zori_button * button, zori_string * text) {
- button->text = NULL;
- return zori_button_text_(button, text);
- }
- struct zori_button * zori_button_new(zori_id id, zori_id parent,
- zori_box * box, zori_string * text) {
- struct zori_button * button = NULL;
- button = calloc(1, sizeof(*button));
- if (!button) return NULL;
- zori_widget_initall(&button->widget, id, zori_get_widget(parent),
- box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_button_handlers));
- if (!zori_button_init(button, text)) {
- free(button);
- button = NULL;
- }
- return button;
- }
- zori_id zori_new_button(zori_id id, zori_id parent, zori_box * box, zori_string * text) {
- struct zori_button * button = zori_button_new(id, parent, box, text);
- if (!button) return ZORI_ID_ENOMEM;
- return button->widget.id;
- }
|