zori_widget.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 NOT passed on to the child widgets. */
  29. return ZORI_HANDLE_DONE;
  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_internal_action_event
  101. (struct zori_widget * widget) {
  102. union zori_event event;
  103. event.action.any.type = ZORI_INTERNAL_EVENT_ACTION;
  104. event.action.any.widget = widget;
  105. return zori_widget_raise_event(widget, &event);
  106. }
  107. int zori_widget_raise_close_event
  108. (struct zori_widget * widget, struct zori_widget * from) {
  109. union zori_event event;
  110. event.close.any.type = ZORI_EVENT_CLOSE;
  111. event.close.any.widget = widget;
  112. event.close.from = from;
  113. return zori_widget_raise_event(widget, &event);
  114. }
  115. int zori_widget_compare(const void * v1, const void * v2) {
  116. const struct zori_widget * w1 = v1, * w2 = v2;
  117. return (w2->id - w1->id);
  118. }
  119. struct zori_handler * zori_widget_add_handler
  120. (struct zori_widget * widget, zori_event_type type, zori_handler_func * handler, void * data) {
  121. if ((!widget) || (!handler)) return NULL;
  122. return zori_handlers_add(&widget->handlers, type, handler, data);
  123. }
  124. struct zori_handler * zori_widget_add_handlers
  125. (struct zori_widget * widget, struct zori_handler * handlers, size_t amount) {
  126. size_t i;
  127. for (i = 0; i < amount; i++) {
  128. struct zori_handler * handler = handlers + i;
  129. if (!zori_widget_add_handler(widget, handler->type, handler->handler, handler->data)) return NULL;
  130. }
  131. return handlers + amount - 1;
  132. }
  133. void zori_widget_free(struct zori_widget * widget);
  134. struct zori_widget * zori_widget_done(struct zori_widget * widget) {
  135. size_t index;
  136. struct zori_widget * aid, * next;
  137. if (!widget) return widget;
  138. for (index = 0; index < miao_size(&widget->children); index ++) {
  139. struct zori_widget * child = miao_unsafe_get(&widget->children, index);
  140. zori_widget_free(child);
  141. }
  142. if (widget->destroy) {
  143. widget->destroy(widget);
  144. }
  145. miao_done(&widget->handlers);
  146. miao_done(&widget->children);
  147. return widget;
  148. }
  149. void zori_widget_free(struct zori_widget * widget) {
  150. if (widget) {
  151. zori_registry_remove(zori_get_registry(), widget->id);
  152. }
  153. zori_widget_done(widget);
  154. free(widget);
  155. }
  156. struct zori_widget *
  157. zori_widget_add_child(struct zori_widget * parent, struct zori_widget * child) {
  158. if (parent) {
  159. miao_push((&parent->children), child);
  160. child->parent = parent;
  161. }
  162. return child;
  163. }
  164. zori_id
  165. zori_widget_margins_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  166. if (!widget) return ZORI_ID_EINVAL;
  167. widget->outer = widget->box;
  168. widget->outer.at.x -= left;
  169. widget->outer.at.y -= top;
  170. widget->outer.size.x += (left + right);
  171. widget->outer.size.y += (top + bottom);
  172. return widget->id;
  173. }
  174. zori_id
  175. zori_widget_margin_(struct zori_widget * widget, int size) {
  176. return zori_widget_margins_(widget, size, size, size, size);
  177. }
  178. /* Draws a widget's frame based on it's style. If the background bitmap is set
  179. * then that is used, otherwise, the background color is used, including a border
  180. * if needed. */
  181. void zori_widget_draw_background(struct zori_widget * widget) {
  182. float dx, dy, dw, dh;
  183. struct zori_style * style = &widget->style;
  184. zori_bitmap * background = style->back.bitmap;
  185. zori_color color = style->back.color;
  186. struct zori_graphic_style * graphic_style = & style->back;
  187. dx = rebox_x(&widget->box);
  188. dy = rebox_y(&widget->box);
  189. dw = rebox_w(&widget->box);
  190. dh = rebox_h(&widget->box);
  191. if (zori_widget_hover(widget)) {
  192. graphic_style = &style->hover;
  193. }
  194. if (zori_widget_marked(widget)) {
  195. graphic_style = &style->mark;
  196. }
  197. if (graphic_style->bitmap) {
  198. background = graphic_style->bitmap;
  199. }
  200. color = graphic_style->color;
  201. if (background) {
  202. image_blitscale9(background, dx, dy, dw, dh, -1, -1);
  203. } else {
  204. draw_frame(dx, dy, dw, dh, 3, style->border.color, color);
  205. }
  206. }
  207. enum zori_handle_result
  208. zori_widget_must_draw_children(struct zori_widget * widget) {
  209. float dx = rebox_x(&widget->box);
  210. float dy = rebox_y(&widget->box);
  211. if (zori_widget_visible(widget)) {
  212. draw_roundbox(dx, dy, 10, 10, 3, 3, al_map_rgb(64,16,255), 3);
  213. return ZORI_HANDLE_PASS;
  214. }
  215. draw_roundbox(dx, dy, 10, 10, 3, 3, al_map_rgb(255,16,64), 3);
  216. return ZORI_HANDLE_DONE;
  217. }
  218. int zori_widget_visible(struct zori_widget * widget) {
  219. return widget && ((widget->flags & ZORI_FLAG_HIDDEN) != ZORI_FLAG_HIDDEN);
  220. }
  221. int zori_widget_active(struct zori_widget * widget) {
  222. return widget && ((widget->flags & ZORI_FLAG_DISABLED) != ZORI_FLAG_DISABLED);
  223. }
  224. int zori_widget_active_(struct zori_widget * widget, int set) {
  225. if (set) {
  226. widget->flags = widget->flags & (~ZORI_FLAG_DISABLED);
  227. } else {
  228. widget->flags = widget->flags | ZORI_FLAG_DISABLED;
  229. }
  230. return zori_widget_active(widget);
  231. }
  232. int zori_widget_visible_(struct zori_widget * widget, int set) {
  233. if (set) {
  234. widget->flags = widget->flags & (~ZORI_FLAG_HIDDEN);
  235. } else {
  236. widget->flags = widget->flags | ZORI_FLAG_HIDDEN;
  237. }
  238. return zori_widget_active(widget);
  239. }
  240. int zori_widget_hover(struct zori_widget * widget) {
  241. return widget && ((widget->flags & ZORI_FLAG_HOVERED) == ZORI_FLAG_HOVERED);
  242. }
  243. int zori_widget_hover_(struct zori_widget * widget, int set) {
  244. if (set) {
  245. widget->flags = widget->flags | ZORI_FLAG_HOVERED;
  246. } else {
  247. widget->flags = widget->flags & (~ZORI_FLAG_HOVERED);
  248. }
  249. return zori_widget_hover(widget);
  250. }
  251. int zori_widget_marked(struct zori_widget * widget) {
  252. return widget && ((widget->flags & ZORI_FLAG_MARKED) == ZORI_FLAG_MARKED);
  253. }
  254. int zori_widget_marked_(struct zori_widget * widget, int set) {
  255. if (set) {
  256. widget->flags = widget->flags | ZORI_FLAG_MARKED;
  257. } else {
  258. widget->flags = widget->flags & (~ZORI_FLAG_MARKED);
  259. }
  260. return zori_widget_marked(widget);
  261. }
  262. int zori_widget_ready(struct zori_widget * widget) {
  263. return widget && ((widget->flags & ZORI_FLAG_READY) == ZORI_FLAG_READY);
  264. }
  265. int zori_widget_ready_(struct zori_widget * widget, int set) {
  266. if (set) {
  267. widget->flags = widget->flags | ZORI_FLAG_READY;
  268. } else {
  269. widget->flags = widget->flags & (~ZORI_FLAG_READY);
  270. }
  271. return zori_widget_ready(widget);
  272. }
  273. int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y) {
  274. return (rebox_coordinates_inside_p(&widget->box, x, y));
  275. }
  276. /* Registers an event handler for a widget. */
  277. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  278. zori_font * zori_widget_font(struct zori_widget * widget) {
  279. return widget->style.text.font;
  280. }
  281. zori_color zori_widget_forecolor(struct zori_widget * widget) {
  282. return widget->style.text.color;
  283. }
  284. zori_color zori_widget_backcolor(struct zori_widget * widget) {
  285. return widget->style.back.color;
  286. }
  287. int zori_widget_h(struct zori_widget * widget) {
  288. return widget->box.size.y;
  289. }
  290. int zori_widget_w(struct zori_widget * widget) {
  291. return widget->box.size.x;
  292. }
  293. int zori_widget_x(struct zori_widget * widget) {
  294. return widget->box.at.x;
  295. }
  296. int zori_widget_y(struct zori_widget * widget) {
  297. return widget->box.at.y;
  298. }
  299. /* Gets amount of child widgets of this widget, 0 if none, -1 on error.*/
  300. int zori_widget_count_children(struct zori_widget * widget) {
  301. if (!widget) return -1;
  302. return miao_size(&widget->children);
  303. }
  304. /* Returns the index-th child of his widget, or NULL on error or if no such child exists. */
  305. struct zori_widget * zori_widget_get_child(struct zori_widget * widget, int index) {
  306. int count = zori_widget_count_children(widget);
  307. if (count < 1) return NULL;
  308. if (index < 0) return NULL;
  309. if (index >= count) return NULL;
  310. return miao_unsafe_get(&widget->children, index);
  311. }
  312. zori_id
  313. zori_widget_paddings_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  314. if (!widget) return ZORI_ID_EINVAL;
  315. widget->inner = widget->box;
  316. widget->inner.at.x += left;
  317. widget->inner.at.y += top;
  318. widget->inner.size.x -= (left + right);
  319. widget->inner.size.y -= (top + bottom);
  320. return widget->id;
  321. }
  322. zori_id
  323. zori_widget_padding_(struct zori_widget * widget, int size) {
  324. return zori_widget_paddings_(widget, size, size, size, size);
  325. }
  326. zori_font * zori_widget_text_font(struct zori_widget * widget) {
  327. struct zori_style * style;
  328. if (!widget) {
  329. style = zori_get_default_style();
  330. } else {
  331. style = & widget->style;
  332. }
  333. return style->text.font;
  334. }
  335. struct zori_widget * zori_widget_init
  336. ( struct zori_widget * widget,
  337. zori_widget_type type,
  338. zori_id id,
  339. struct zori_widget * parent,
  340. zori_rebox * box, struct zori_style * style) {
  341. if (!widget) return NULL;
  342. struct zori_style * default_style = zori_get_default_style();
  343. miao_init(&widget->children);
  344. miao_init(&widget->handlers);
  345. widget->destroy = NULL;
  346. widget->z = 0;
  347. widget->flags = 0;
  348. widget->type = type;
  349. widget->id = ( (id < 0) ? zori_get_unused_id() : id);
  350. if (style) {
  351. widget->style = *style;
  352. if (!widget->style.text.font) {
  353. widget->style.text.font = default_style->text.font;
  354. }
  355. } else {
  356. widget->style = *default_style;
  357. }
  358. if (box) {
  359. widget->box = *box;
  360. } else if (parent) {
  361. widget->box = parent->box;
  362. } else {
  363. widget->box = rebox_make(0, 0, ZORI_WIDGET_DEFAULT_W, ZORI_WIDGET_DEFAULT_H);
  364. }
  365. zori_widget_margin_(widget, ZORI_MARGIN_DEFAULT);
  366. zori_widget_padding_(widget, ZORI_PADDING_DEFAULT);
  367. zori_widget_add_child(parent, widget);
  368. zori_registry_add(zori_get_registry(), widget->id, widget);
  369. return widget;
  370. }
  371. struct zori_widget *
  372. zori_widget_initall(struct zori_widget * widget, zori_widget_type type, int id,
  373. struct zori_widget * parent, zori_rebox * box, struct zori_style * style,
  374. struct zori_handler * handlers, size_t amount) {
  375. if (zori_widget_init(widget, type, id, parent, box, style)) {
  376. zori_widget_add_handlers(widget, handlers, amount);
  377. }
  378. return widget;
  379. }
  380. /* Lets the widget handle an event and bubbles it on to it's children
  381. * recursively depending on the need to do that. */
  382. void zori_widget_handle_event(struct zori_widget * widget, union zori_event * event) {
  383. zori_widget_raise_event(widget, event);
  384. }
  385. /* Returns the first parent or super-parent of this widget for which
  386. * predicate() returns true.
  387. */
  388. struct zori_widget *
  389. zori_widget_find_parent(struct zori_widget * widget,
  390. bool (*predicate)(struct zori_widget * parent, void * extra),
  391. void * extra
  392. ) {
  393. struct zori_widget * parent;
  394. if (!widget) return NULL;
  395. parent = widget->parent;
  396. while (parent) {
  397. if (predicate(parent, extra)) return parent;
  398. if (parent == parent->parent) {
  399. LOG_ERROR("GUI setup not correct. Parent/child loop in widget %p.", widget);
  400. return NULL;
  401. }
  402. parent = parent->parent;
  403. }
  404. return NULL;
  405. }
  406. bool zori_widget_is_type(struct zori_widget * widget, zori_widget_type type) {
  407. if(!widget) return false;
  408. return (type) == widget->type;
  409. }
  410. bool zori_widget_is_type_predicate(struct zori_widget * widget, void * extra) {
  411. zori_widget_type * type_ptr = extra;
  412. if(!type_ptr) return false;
  413. return zori_widget_is_type(widget, (*type_ptr));
  414. }
  415. struct zori_widget *
  416. zori_widget_get_parent_of_type(struct zori_widget * widget,
  417. zori_widget_type type) {
  418. return zori_widget_find_parent(widget, zori_widget_is_type_predicate, &type);
  419. }
  420. struct zori_screen * zori_widget_get_screen(struct zori_widget * widget) {
  421. struct zori_widget * screen_widget = zori_widget_get_parent_of_type(widget, ZORI_WIDGET_TYPE_SCREEN);
  422. return zori_widget_to_screen(screen_widget);
  423. }
  424. int zori_mark_widget(struct zori_widget * widget) {
  425. struct zori_screen * screen = zori_widget_get_screen(widget);
  426. if (screen) {
  427. screen->cursors.keyjoy.p = widget->box.at;
  428. }
  429. return ZORI_HANDLE_DONE;
  430. }
  431. zori_id zori_widget_set_int_result(struct zori_widget * widget, int value) {
  432. if (widget) {
  433. widget->result.value.integer = value;
  434. widget->result.type = ZORI_RESULT_TYPE_INTEGER;
  435. widget->result.ready = widget->id;
  436. return widget->id;
  437. }
  438. return ZORI_ID_EINVAL;
  439. }
  440. zori_id zori_widget_set_string_result(struct zori_widget * widget,
  441. zori_string * value) {
  442. if (widget) {
  443. widget->result.value.string = value;
  444. widget->result.type = ZORI_RESULT_TYPE_STRING;
  445. widget->result.ready = widget->id;
  446. return widget->id;
  447. }
  448. return ZORI_ID_EINVAL;
  449. }
  450. zori_id zori_widget_set_closed_result(struct zori_widget * widget, int value) {
  451. if (widget) {
  452. widget->result.value.closed = value;
  453. widget->result.type = ZORI_RESULT_TYPE_CLOSED;
  454. widget->result.ready = widget->id;
  455. return widget->id;
  456. }
  457. return ZORI_ID_EINVAL;
  458. }