zori_button.c 3.6 KB

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