zori_button.c 4.2 KB

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