zori_widget.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #include "zori.h"
  2. #include "zori_widget.h"
  3. #include "zori_registry.h"
  4. #include "zori_style.h"
  5. #include "zori_screen.h"
  6. #include "monolog.h"
  7. #include "draw.h"
  8. /** Returns whether or not a widget will even handle an event in the first place.
  9. * For example, a hidden widget won't draw, and a disabled widget won't
  10. * accept any system events, and a NULL widget doesn't accept events at all.. */
  11. int zori_widget_accepts_event(struct zori_widget * widget, union zori_event * event) {
  12. if (!widget) return 0;
  13. switch(event->any.type) {
  14. case ZORI_EVENT_DRAW:
  15. return zori_widget_visible(widget);
  16. default:
  17. return zori_widget_active(widget);
  18. }
  19. }
  20. /** Raises an event on the widget itself only. */
  21. int zori_widget_self_raise_event(struct zori_widget * widget,
  22. union zori_event * event) {
  23. enum zori_handle_result result;
  24. if (zori_widget_accepts_event(widget, event)) {
  25. event->any.widget = widget;
  26. return zori_handlers_handle(&widget->handlers, event);
  27. }
  28. /* If the event is not accepted, it is passed on to the child widgets. */
  29. return ZORI_HANDLE_PASS;
  30. }
  31. /* Raises an event on the widget, and if necessary, propagates it on to it's
  32. * children automatically and recursively. */
  33. int
  34. zori_widget_raise_event(struct zori_widget * widget, union zori_event * event) {
  35. if (!widget) return ZORI_HANDLE_DONE;
  36. enum zori_handle_result result =
  37. zori_widget_self_raise_event(widget, event);
  38. if (zori_propagate_event_p(result)) {
  39. enum zori_handle_result sub;
  40. size_t index;
  41. for (index = 0; index < miao_size(&widget->children); index++) {
  42. struct zori_widget * child = miao_unsafe_get(&widget->children, index);
  43. event->any.widget = child;
  44. sub = zori_widget_raise_event(child, event);
  45. if (sub == ZORI_HANDLE_DONE) {
  46. result = ZORI_HANDLE_DONE;
  47. break;
  48. }
  49. }
  50. }
  51. return result;
  52. }
  53. int zori_widget_raise_system_event
  54. (struct zori_widget * widget, zori_system_event * sysev) {
  55. union zori_event event;
  56. event.sys.any.type = sysev->type;
  57. event.sys.any.widget = widget;
  58. event.sys.ev = sysev;
  59. return zori_widget_raise_event(widget, &event);
  60. }
  61. int zori_widget_raise_draw_event(struct zori_widget * widget) {
  62. union zori_event event;
  63. event.draw.any.type = ZORI_EVENT_DRAW;
  64. event.draw.any.widget = widget;
  65. return zori_widget_raise_event(widget, &event);
  66. }
  67. int zori_widget_raise_overdraw_event(struct zori_widget * widget) {
  68. union zori_event event;
  69. event.draw.any.type = ZORI_EVENT_OVERDRAW;
  70. event.draw.any.widget = widget;
  71. return zori_widget_raise_event(widget, &event);
  72. }
  73. int zori_widget_raise_done_event(struct zori_widget * widget) {
  74. union zori_event event;
  75. event.done.any.type = ZORI_EVENT_DONE;
  76. event.done.any.widget = widget;
  77. return zori_widget_raise_event(widget, &event);
  78. }
  79. int zori_widget_raise_free_event(struct zori_widget * widget) {
  80. union zori_event event;
  81. event.free.any.type = ZORI_EVENT_FREE;
  82. event.free.any.widget = widget;
  83. return zori_widget_raise_event(widget, &event);
  84. }
  85. int zori_widget_raise_update_event
  86. (struct zori_widget * widget, double dt) {
  87. union zori_event event;
  88. event.update.any.type = ZORI_EVENT_DONE;
  89. event.update.any.widget = widget;
  90. event.update.dt = dt;
  91. return zori_widget_raise_event(widget, &event);
  92. }
  93. int zori_widget_raise_action_event
  94. (struct zori_widget * widget) {
  95. union zori_event event;
  96. event.action.any.type = ZORI_EVENT_ACTION;
  97. event.action.any.widget = widget;
  98. return zori_widget_raise_event(widget, &event);
  99. }
  100. int zori_widget_raise_close_event
  101. (struct zori_widget * widget, struct zori_widget * from) {
  102. union zori_event event;
  103. event.close.any.type = ZORI_EVENT_CLOSE;
  104. event.close.any.widget = widget;
  105. event.close.from = from;
  106. return zori_widget_raise_event(widget, &event);
  107. }
  108. int zori_widget_compare(const void * v1, const void * v2) {
  109. const struct zori_widget * w1 = v1, * w2 = v2;
  110. return (w2->id - w1->id);
  111. }
  112. struct zori_handler * zori_widget_add_handler
  113. (struct zori_widget * widget, zori_event_type type, zori_handler_func * handler, void * data) {
  114. if ((!widget) || (!handler)) return NULL;
  115. return zori_handlers_add(&widget->handlers, type, handler, data);
  116. }
  117. struct zori_handler * zori_widget_add_handlers
  118. (struct zori_widget * widget, struct zori_handler * handlers, size_t amount) {
  119. size_t i;
  120. for (i = 0; i < amount; i++) {
  121. struct zori_handler * handler = handlers + i;
  122. if (!zori_widget_add_handler(widget, handler->type, handler->handler, handler->data)) return NULL;
  123. }
  124. return handlers + amount - 1;
  125. }
  126. void zori_widget_free(struct zori_widget * widget);
  127. struct zori_widget * zori_widget_done(struct zori_widget * widget) {
  128. size_t index;
  129. struct zori_widget * aid, * next;
  130. if (!widget) return widget;
  131. for (index = 0; index < miao_size(&widget->children); index ++) {
  132. struct zori_widget * child = miao_unsafe_get(&widget->children, index);
  133. zori_widget_free(child);
  134. }
  135. if (widget->destroy) {
  136. widget->destroy(widget);
  137. }
  138. miao_done(&widget->handlers);
  139. miao_done(&widget->children);
  140. return widget;
  141. }
  142. void zori_widget_free(struct zori_widget * widget) {
  143. if (widget) {
  144. zori_registry_remove(zori_get_registry(), widget->id);
  145. }
  146. zori_widget_done(widget);
  147. free(widget);
  148. }
  149. struct zori_widget *
  150. zori_widget_add_child(struct zori_widget * parent, struct zori_widget * child) {
  151. if (parent) {
  152. miao_push((&parent->children), child);
  153. child->parent = parent;
  154. }
  155. return child;
  156. }
  157. zori_id
  158. zori_widget_margins_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  159. if (!widget) return ZORI_ID_EINVAL;
  160. widget->outer = widget->box;
  161. widget->outer.at.x -= left;
  162. widget->outer.at.y -= top;
  163. widget->outer.size.x += (left + right);
  164. widget->outer.size.y += (top + bottom);
  165. return widget->id;
  166. }
  167. zori_id
  168. zori_widget_margin_(struct zori_widget * widget, int size) {
  169. return zori_widget_margins_(widget, size, size, size, size);
  170. }
  171. /* Draws a widget's frame based on it's style. If the background bitmap is set
  172. * then that is used, otherwise, the background color is used, including a border
  173. * if needed. */
  174. void zori_widget_draw_background(struct zori_widget * widget) {
  175. float dx, dy, dw, dh;
  176. struct zori_style * style = &widget->style;
  177. zori_bitmap * background = style->back.bitmap;
  178. zori_color color = style->back.color;
  179. struct zori_stylepart * part = & style->back;
  180. dx = rebox_x(&widget->box);
  181. dy = rebox_y(&widget->box);
  182. dw = rebox_w(&widget->box);
  183. dh = rebox_h(&widget->box);
  184. if (zori_widget_hover(widget)) {
  185. part = &style->hover;
  186. }
  187. if (zori_widget_marked(widget)) {
  188. part = &style->mark;
  189. }
  190. if (part->bitmap) {
  191. background = part->bitmap;
  192. }
  193. color = part->color;
  194. if (background) {
  195. image_blitscale9(background, dx, dy, dw, dh, -1, -1);
  196. } else {
  197. draw_frame(dx, dy, dw, dh, 3, style->border.color, color);
  198. }
  199. }
  200. int zori_widget_visible(struct zori_widget * widget) {
  201. return widget && ((widget->flags & ZORI_FLAG_HIDDEN) != ZORI_FLAG_HIDDEN);
  202. }
  203. int zori_widget_active(struct zori_widget * widget) {
  204. return widget && ((widget->flags & ZORI_FLAG_DISABLED) != ZORI_FLAG_DISABLED);
  205. }
  206. int zori_widget_active_(struct zori_widget * widget, int set) {
  207. if (set) {
  208. widget->flags = widget->flags & (~ZORI_FLAG_DISABLED);
  209. } else {
  210. widget->flags = widget->flags | ZORI_FLAG_DISABLED;
  211. }
  212. return zori_widget_active(widget);
  213. }
  214. int zori_widget_visible_(struct zori_widget * widget, int set) {
  215. if (set) {
  216. widget->flags = widget->flags & (~ZORI_FLAG_HIDDEN);
  217. } else {
  218. widget->flags = widget->flags | ZORI_FLAG_HIDDEN;
  219. }
  220. return zori_widget_active(widget);
  221. }
  222. int zori_widget_hover(struct zori_widget * widget) {
  223. return widget && ((widget->flags & ZORI_FLAG_HOVERED) == ZORI_FLAG_HOVERED);
  224. }
  225. int zori_widget_hover_(struct zori_widget * widget, int set) {
  226. if (set) {
  227. widget->flags = widget->flags | ZORI_FLAG_HOVERED;
  228. } else {
  229. widget->flags = widget->flags & (~ZORI_FLAG_HOVERED);
  230. }
  231. return zori_widget_hover(widget);
  232. }
  233. int zori_widget_marked(struct zori_widget * widget) {
  234. return widget && ((widget->flags & ZORI_FLAG_MARKED) == ZORI_FLAG_MARKED);
  235. }
  236. int zori_widget_marked_(struct zori_widget * widget, int set) {
  237. if (set) {
  238. widget->flags = widget->flags | ZORI_FLAG_MARKED;
  239. } else {
  240. widget->flags = widget->flags & (~ZORI_FLAG_MARKED);
  241. }
  242. return zori_widget_hover(widget);
  243. }
  244. int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y) {
  245. return (rebox_coordinates_inside_p(&widget->box, x, y));
  246. }
  247. /* Registers an event handler for a widget. */
  248. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  249. zori_font * zori_widget_font(struct zori_widget * widget) {
  250. return widget->style.text.font;
  251. }
  252. zori_color zori_widget_forecolor(struct zori_widget * widget) {
  253. return widget->style.text.color;
  254. }
  255. zori_color zori_widget_backcolor(struct zori_widget * widget) {
  256. return widget->style.back.color;
  257. }
  258. int zori_widget_h(struct zori_widget * widget) {
  259. return widget->box.size.y;
  260. }
  261. int zori_widget_w(struct zori_widget * widget) {
  262. return widget->box.size.x;
  263. }
  264. int zori_widget_x(struct zori_widget * widget) {
  265. return widget->box.at.x;
  266. }
  267. int zori_widget_y(struct zori_widget * widget) {
  268. return widget->box.at.y;
  269. }
  270. /* Gets amount of child widgets of this widget, 0 if none, -1 on error.*/
  271. int zori_widget_count_children(struct zori_widget * widget) {
  272. if (!widget) return -1;
  273. return miao_size(&widget->children);
  274. }
  275. /* Returns the index-th child of his widget, or NULL on error or if no such child exists. */
  276. struct zori_widget * zori_widget_get_child(struct zori_widget * widget, int index) {
  277. int count = zori_widget_count_children(widget);
  278. if (count < 1) return NULL;
  279. if (index < 0) return NULL;
  280. if (index >= count) return NULL;
  281. return miao_unsafe_get(&widget->children, index);
  282. }
  283. zori_id
  284. zori_widget_paddings_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  285. if (!widget) return ZORI_ID_EINVAL;
  286. widget->inner = widget->box;
  287. widget->inner.at.x += left;
  288. widget->inner.at.y += top;
  289. widget->inner.size.x -= (left + right);
  290. widget->inner.size.y -= (top + bottom);
  291. return widget->id;
  292. }
  293. zori_id
  294. zori_widget_padding_(struct zori_widget * widget, int size) {
  295. return zori_widget_paddings_(widget, size, size, size, size);
  296. }
  297. zori_font * zori_widget_text_font(struct zori_widget * widget) {
  298. struct zori_style * style;
  299. if (!widget) {
  300. style = zori_get_default_style();
  301. } else {
  302. style = & widget->style;
  303. }
  304. return style->text.font;
  305. }
  306. struct zori_widget * zori_widget_init
  307. ( struct zori_widget * widget,
  308. zori_widget_type type,
  309. zori_id id,
  310. struct zori_widget * parent,
  311. zori_rebox * box, struct zori_style * style) {
  312. if (!widget) return NULL;
  313. struct zori_style * default_style = zori_get_default_style();
  314. miao_init(&widget->children);
  315. miao_init(&widget->handlers);
  316. widget->destroy = NULL;
  317. widget->z = 0;
  318. widget->flags = 0;
  319. widget->type = type;
  320. widget->id = ( (id < 0) ? zori_get_unused_id() : id);
  321. if (style) {
  322. widget->style = *style;
  323. if (!widget->style.text.font) {
  324. widget->style.text.font = default_style->text.font;
  325. }
  326. } else {
  327. widget->style = *default_style;
  328. }
  329. if (box) {
  330. widget->box = *box;
  331. } else if (parent) {
  332. widget->box = parent->box;
  333. } else {
  334. widget->box = rebox_make(0, 0, ZORI_WIDGET_DEFAULT_W, ZORI_WIDGET_DEFAULT_H);
  335. }
  336. zori_widget_margin_(widget, ZORI_MARGIN_DEFAULT);
  337. zori_widget_padding_(widget, ZORI_PADDING_DEFAULT);
  338. zori_widget_add_child(parent, widget);
  339. zori_registry_add(zori_get_registry(), widget->id, widget);
  340. return widget;
  341. }
  342. struct zori_widget *
  343. zori_widget_initall(struct zori_widget * widget, zori_widget_type type, int id,
  344. struct zori_widget * parent, zori_rebox * box, struct zori_style * style,
  345. struct zori_handler * handlers, size_t amount) {
  346. if (zori_widget_init(widget, type, id, parent, box, style)) {
  347. zori_widget_add_handlers(widget, handlers, amount);
  348. }
  349. return widget;
  350. }
  351. /* Lets the widget handle an event and bubbles it on to it's children
  352. * recursively depending on the need to do that. */
  353. void zori_widget_handle_event(struct zori_widget * widget, union zori_event * event) {
  354. zori_widget_raise_event(widget, event);
  355. }
  356. /* Returns the first parent or super-parent of this widget for which
  357. * predicate() returns true.
  358. */
  359. struct zori_widget *
  360. zori_widget_find_parent(struct zori_widget * widget,
  361. bool (*predicate)(struct zori_widget * parent, void * extra),
  362. void * extra
  363. ) {
  364. struct zori_widget * parent;
  365. if (!widget) return NULL;
  366. parent = widget->parent;
  367. while (parent) {
  368. if (predicate(parent, extra)) return parent;
  369. if (parent == parent->parent) {
  370. LOG_ERROR("GUI setup not correct. Parent/child loop in widget %p.", widget);
  371. return NULL;
  372. }
  373. parent = parent->parent;
  374. }
  375. return NULL;
  376. }
  377. bool zori_widget_is_type(struct zori_widget * widget, zori_widget_type type) {
  378. if(!widget) return false;
  379. return (type) == widget->type;
  380. }
  381. bool zori_widget_is_type_predicate(struct zori_widget * widget, void * extra) {
  382. zori_widget_type * type_ptr = extra;
  383. if(!type_ptr) return false;
  384. return zori_widget_is_type(widget, (*type_ptr));
  385. }
  386. struct zori_widget *
  387. zori_widget_get_parent_of_type(struct zori_widget * widget,
  388. zori_widget_type type) {
  389. return zori_widget_find_parent(widget, zori_widget_is_type_predicate, &type);
  390. }
  391. struct zori_screen * zori_widget_get_screen(struct zori_widget * widget) {
  392. struct zori_widget * screen_widget = zori_widget_get_parent_of_type(widget, ZORI_WIDGET_TYPE_SCREEN);
  393. return zori_widget_to_screen(screen_widget);
  394. }
  395. int zori_mark_widget(struct zori_widget * widget) {
  396. struct zori_screen * screen = zori_widget_get_screen(widget);
  397. if (screen) {
  398. screen->cursors.keyjoy.p = widget->box.at;
  399. }
  400. return ZORI_HANDLE_DONE;
  401. }