zori_button.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "monolog.h"
  2. #include "zori.h"
  3. #include "zori_widget.h"
  4. #include "zori_button.h"
  5. struct zori_button * zori_widget_to_button(struct zori_widget * widget) {
  6. return ZORI_CONTAINER_OF(widget, struct zori_button, widget);
  7. }
  8. /** Handles a mouse axis event and pass it on. */
  9. int zori_button_on_mouse_axes(union zori_event * event) {
  10. struct zori_widget * widget = event->any.widget;
  11. struct zori_button * button = zori_widget_to_button(widget);
  12. float x = event->sys.ev->mouse.x;
  13. float y = event->sys.ev->mouse.y;
  14. if (zori_xy_inside_widget_p(widget, x, y)) {
  15. zori_widget_hover_(widget, TRUE);
  16. } else {
  17. zori_widget_hover_(widget, FALSE);
  18. }
  19. return ZORI_HANDLE_PASS;
  20. }
  21. /** Handles a mouse click event and pass it on. */
  22. int zori_button_on_mouse_click(union zori_event * event) {
  23. struct zori_widget * widget = event->any.widget;
  24. struct zori_button * button = zori_widget_to_button(widget);
  25. float x = event->sys.ev->mouse.x;
  26. float y = event->sys.ev->mouse.y;
  27. if (zori_xy_inside_widget_p(widget, x, y)) {
  28. widget->result = widget->id;
  29. LOG_NOTE("Button %d clicked: %d, result %d.\n", widget->id, widget->result);
  30. return zori_widget_raise_action_event(widget);
  31. }
  32. return ZORI_HANDLE_PASS;
  33. }
  34. void zori_draw_button(struct zori_button * button) {
  35. float x, y, w, h;
  36. struct zori_style * style = &button->widget.style;
  37. x = rebox_x(&button->widget.inner);
  38. y = rebox_y(&button->widget.inner);
  39. w = rebox_w(&button->widget.inner);
  40. h = rebox_h(&button->widget.inner);
  41. zori_widget_draw_background(&button->widget);
  42. zori_caption_draw(&button->caption, &button->widget.inner, &button->widget.style);
  43. };
  44. int zori_button_on_draw(union zori_event * event) {
  45. struct zori_button * button = zori_widget_to_button(event->any.widget);
  46. zori_draw_button(button);
  47. return ZORI_HANDLE_PASS;
  48. }
  49. struct zori_handler zori_button_handlers[] = {
  50. { ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN , zori_button_on_mouse_click , NULL },
  51. { ZORI_SYSTEM_EVENT_MOUSE_AXES , zori_button_on_mouse_axes , NULL },
  52. { ZORI_EVENT_DRAW , zori_button_on_draw , NULL },
  53. { -1, NULL, NULL }
  54. };
  55. struct zori_button *
  56. zori_button_text_(struct zori_button * button, zori_string * text) {
  57. if (button) {
  58. zori_caption_set(&button->caption, text);
  59. }
  60. return button;
  61. }
  62. struct zori_button *
  63. zori_button_init(struct zori_button * button,const char * text) {
  64. if (button) {
  65. zori_caption_init(&button->caption, text);
  66. }
  67. return button;
  68. }
  69. struct zori_button * zori_button_new(zori_id id, zori_id parent_id,
  70. zori_box * box, const char * text) {
  71. struct zori_button * button = NULL;
  72. button = calloc(1, sizeof(*button));
  73. if (!button) return NULL;
  74. zori_widget_initall(&button->widget, ZORI_WIDGET_TYPE_BUTTON, id, zori_get_widget(parent_id),
  75. box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_button_handlers));
  76. if (!zori_button_init(button, text)) {
  77. free(button);
  78. button = NULL;
  79. }
  80. zori_widget_hover_(&button->widget, 0);
  81. return button;
  82. }
  83. zori_id zori_new_button(zori_id id, zori_id parent, zori_box * box, const char * text) {
  84. struct zori_button * button = zori_button_new(id, parent, box, text);
  85. if (!button) return ZORI_ID_ENOMEM;
  86. return button->widget.id;
  87. }