zori_widget.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. /* Magic comment for runcprotoall: @generate_cproto@ */
  9. bool zori_widget_is_type(struct zori_widget * widget, zori_widget_type type) {
  10. if(!widget) return false;
  11. return (type) == widget->type;
  12. }
  13. bool zori_widget_is_type_predicate(struct zori_widget * widget, void * extra) {
  14. zori_widget_type * type_ptr = extra;
  15. if(!type_ptr) return false;
  16. return zori_widget_is_type(widget, (*type_ptr));
  17. }
  18. struct zori_widget *
  19. zori_widget_get_parent_of_type(struct zori_widget * widget,
  20. zori_widget_type type) {
  21. return zori_widget_find_parent(widget, zori_widget_is_type_predicate, &type);
  22. }
  23. struct zori_screen * zori_widget_get_screen(struct zori_widget * widget) {
  24. struct zori_widget * screen_widget = zori_widget_get_parent_of_type(widget, ZORI_WIDGET_TYPE_SCREEN);
  25. return zori_widget_to_screen(screen_widget);
  26. }
  27. /** Returns whether or not a widget will even handle an event in the first place.
  28. * For example, a hidden widget won't draw, and a disabled widget won't
  29. * accept any system events, and a NULL widget doesn't accept events at all.. */
  30. int zori_widget_accepts_event(struct zori_widget * widget, union zori_event * event) {
  31. if (!widget) return 0;
  32. switch(event->any.type) {
  33. case ZORI_EVENT_DRAW:
  34. return zori_widget_visible(widget);
  35. default:
  36. return zori_widget_active(widget);
  37. }
  38. }
  39. /** Raises an event on the widget itself only. */
  40. int zori_widget_self_raise_event(struct zori_widget * widget,
  41. union zori_event * event) {
  42. enum zori_handle_result result;
  43. if (zori_widget_accepts_event(widget, event)) {
  44. event->any.widget = widget;
  45. return zori_handlers_handle(&widget->handlers, event);
  46. }
  47. /* If the event is not accepted, it is NOT passed on to the child widgets. */
  48. return ZORI_HANDLE_DONE;
  49. }
  50. /* Raises an event on the widget, and if necessary, propagates it on to it's
  51. * children automatically and recursively. */
  52. int
  53. zori_widget_raise_event(struct zori_widget * widget, union zori_event * event) {
  54. if (!widget) return ZORI_HANDLE_DONE;
  55. enum zori_handle_result result =
  56. zori_widget_self_raise_event(widget, event);
  57. if (zori_propagate_event_p(result)) {
  58. enum zori_handle_result sub;
  59. size_t index;
  60. for (index = 0; index < miao_size(&widget->children); index++) {
  61. struct zori_widget * child = miao_unsafe_get(&widget->children, index);
  62. event->any.widget = child;
  63. sub = zori_widget_raise_event(child, event);
  64. if (sub == ZORI_HANDLE_ERROR) {
  65. result = ZORI_HANDLE_ERROR;
  66. break;
  67. }
  68. }
  69. }
  70. return result;
  71. }
  72. int zori_widget_raise_system_event
  73. (struct zori_widget * widget, zori_system_event * sysev) {
  74. union zori_event event;
  75. event.sys.any.type = sysev->type;
  76. event.sys.any.widget = widget;
  77. event.sys.ev = sysev;
  78. return zori_widget_raise_event(widget, &event);
  79. }
  80. int zori_widget_raise_draw_event(struct zori_widget * widget) {
  81. union zori_event event;
  82. event.draw.any.type = ZORI_EVENT_DRAW;
  83. event.draw.any.widget = widget;
  84. return zori_widget_raise_event(widget, &event);
  85. }
  86. int zori_widget_raise_overdraw_event(struct zori_widget * widget) {
  87. union zori_event event;
  88. event.draw.any.type = ZORI_EVENT_OVERDRAW;
  89. event.draw.any.widget = widget;
  90. return zori_widget_raise_event(widget, &event);
  91. }
  92. int zori_widget_raise_done_event(struct zori_widget * widget) {
  93. union zori_event event;
  94. event.done.any.type = ZORI_EVENT_DONE;
  95. event.done.any.widget = widget;
  96. return zori_widget_raise_event(widget, &event);
  97. }
  98. int zori_widget_raise_free_event(struct zori_widget * widget) {
  99. union zori_event event;
  100. event.free.any.type = ZORI_EVENT_FREE;
  101. event.free.any.widget = widget;
  102. return zori_widget_raise_event(widget, &event);
  103. }
  104. int zori_widget_raise_update_event
  105. (struct zori_widget * widget, double dt) {
  106. union zori_event event;
  107. event.update.any.type = ZORI_EVENT_UPDATE;
  108. event.update.any.widget = widget;
  109. event.update.dt = dt;
  110. return zori_widget_raise_event(widget, &event);
  111. }
  112. int zori_widget_raise_action_event
  113. (struct zori_widget * widget) {
  114. union zori_event event;
  115. event.action.any.type = ZORI_EVENT_ACTION;
  116. event.action.any.widget = widget;
  117. return zori_widget_raise_event(widget, &event);
  118. }
  119. int zori_widget_raise_internal_action_event
  120. (struct zori_widget * widget) {
  121. union zori_event event;
  122. event.action.any.type = ZORI_INTERNAL_EVENT_ACTION;
  123. event.action.any.widget = widget;
  124. return zori_widget_raise_event(widget, &event);
  125. }
  126. int zori_widget_raise_close_event
  127. (struct zori_widget * widget, struct zori_widget * from) {
  128. union zori_event event;
  129. event.close.any.type = ZORI_EVENT_CLOSE;
  130. event.close.any.widget = widget;
  131. event.close.from = from;
  132. return zori_widget_raise_event(widget, &event);
  133. }
  134. int zori_widget_compare(const void * v1, const void * v2) {
  135. const struct zori_widget * w1 = v1, * w2 = v2;
  136. return (w2->id - w1->id);
  137. }
  138. struct zori_handler * zori_widget_add_handler
  139. (struct zori_widget * widget, zori_event_type type, zori_handler_func * handler, void * data) {
  140. if ((!widget) || (!handler)) return NULL;
  141. return zori_handlers_add(&widget->handlers, type, handler, data);
  142. }
  143. struct zori_handler * zori_widget_add_handlers
  144. (struct zori_widget * widget, struct zori_handler * handlers, size_t amount) {
  145. size_t i;
  146. for (i = 0; i < amount; i++) {
  147. struct zori_handler * handler = handlers + i;
  148. if (!zori_widget_add_handler(widget, handler->type, handler->handler, handler->data)) return NULL;
  149. }
  150. return handlers + amount - 1;
  151. }
  152. /* Gets the style to use for a marked widget. Looks up a screen widget and then
  153. * looks for the cursor t determine this. */
  154. struct zori_style * zori_widget_get_mark_style(struct zori_widget * widget) {
  155. struct zori_screen * screen = zori_widget_get_screen(widget);
  156. if (!screen) return &widget->style;
  157. return &(screen->cursors.keyjoy.target_style);
  158. }
  159. /* Gets the style to use for a hovered widget. Looks up a screen widget and then
  160. * looks for the cursor t determine this. */
  161. struct zori_style * zori_widget_get_hover_style(struct zori_widget * widget) {
  162. struct zori_screen * screen = zori_widget_get_screen(widget);
  163. if (!screen) return &widget->style;
  164. return &(screen->cursors.mouse.target_style);
  165. }
  166. void zori_widget_free(struct zori_widget * widget);
  167. struct zori_widget * zori_widget_done(struct zori_widget * widget) {
  168. size_t index;
  169. struct zori_widget * aid, * next;
  170. if (!widget) return widget;
  171. for (index = 0; index < miao_size(&widget->children); index ++) {
  172. struct zori_widget * child = miao_unsafe_get(&widget->children, index);
  173. zori_widget_free(child);
  174. }
  175. if (widget->destroy) {
  176. widget->destroy(widget);
  177. }
  178. miao_done(&widget->handlers);
  179. miao_done(&widget->children);
  180. return widget;
  181. }
  182. void zori_widget_free(struct zori_widget * widget) {
  183. if (widget) {
  184. zori_registry_remove(zori_get_registry(), widget->id);
  185. }
  186. zori_widget_done(widget);
  187. free(widget);
  188. }
  189. struct zori_widget *
  190. zori_widget_add_child(struct zori_widget * parent, struct zori_widget * child) {
  191. if (parent) {
  192. miao_push((&parent->children), child);
  193. child->parent = parent;
  194. }
  195. return child;
  196. }
  197. zori_id
  198. zori_widget_margins_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  199. if (!widget) return ZORI_ID_EINVAL;
  200. widget->outer = widget->box;
  201. widget->outer.at.x -= left;
  202. widget->outer.at.y -= top;
  203. widget->outer.size.x += (left + right);
  204. widget->outer.size.y += (top + bottom);
  205. return widget->id;
  206. }
  207. zori_id
  208. zori_widget_margin_(struct zori_widget * widget, int size) {
  209. return zori_widget_margins_(widget, size, size, size, size);
  210. }
  211. /* Draws a widget's frame based on it's style. If the background bitmap is set
  212. * then that is used, otherwise, the background color is used, including a border
  213. * if needed. */
  214. void zori_widget_draw_background(struct zori_widget * widget) {
  215. float dx, dy, dw, dh;
  216. struct zori_style * style = &widget->style;
  217. dx = rebox_x(&widget->box);
  218. dy = rebox_y(&widget->box);
  219. dw = rebox_w(&widget->box);
  220. dh = rebox_h(&widget->box);
  221. if (zori_widget_hover(widget)) {
  222. style = zori_widget_get_hover_style(widget);
  223. }
  224. if (zori_widget_marked(widget)) {
  225. style = zori_widget_get_mark_style(widget);
  226. }
  227. if (style->back.image) {
  228. zori_bitmap * background = style->back.image;
  229. image_blitscale9(background, dx, dy, dw, dh, -1, -1);
  230. } else {
  231. zori_color fillco = style->back.color;
  232. zori_color bordco = style->border.color;
  233. int thick = style->border.size;
  234. draw_frame(dx, dy, dw, dh, thick, bordco, fillco);
  235. }
  236. }
  237. enum zori_handle_result
  238. zori_widget_must_draw_children(struct zori_widget * widget) {
  239. float dx = rebox_x(&widget->box);
  240. float dy = rebox_y(&widget->box);
  241. if (zori_widget_visible(widget)) {
  242. draw_roundbox(dx, dy, 10, 10, 3, 3, al_map_rgb(64,16,255), 3);
  243. return ZORI_HANDLE_PASS;
  244. }
  245. draw_roundbox(dx, dy, 10, 10, 3, 3, al_map_rgb(255,16,64), 3);
  246. return ZORI_HANDLE_DONE;
  247. }
  248. int zori_widget_visible(struct zori_widget * widget) {
  249. return widget && ((widget->flags & ZORI_FLAG_HIDDEN) != ZORI_FLAG_HIDDEN);
  250. }
  251. int zori_widget_active(struct zori_widget * widget) {
  252. return widget && ((widget->flags & ZORI_FLAG_DISABLED) != ZORI_FLAG_DISABLED);
  253. }
  254. int zori_widget_active_(struct zori_widget * widget, int set) {
  255. if (set) {
  256. widget->flags = widget->flags & (~ZORI_FLAG_DISABLED);
  257. } else {
  258. widget->flags = widget->flags | ZORI_FLAG_DISABLED;
  259. }
  260. return zori_widget_active(widget);
  261. }
  262. int zori_widget_visible_(struct zori_widget * widget, int set) {
  263. if (set) {
  264. widget->flags = widget->flags & (~ZORI_FLAG_HIDDEN);
  265. } else {
  266. widget->flags = widget->flags | ZORI_FLAG_HIDDEN;
  267. }
  268. return zori_widget_active(widget);
  269. }
  270. /** Get active and visible */
  271. int zori_widget_live(struct zori_widget * widget) {
  272. return zori_widget_active(widget) && zori_widget_visible(widget);
  273. }
  274. /** Set active and visible */
  275. int zori_widget_live_(struct zori_widget * widget, int set) {
  276. zori_widget_active_(widget, set);
  277. zori_widget_visible_(widget, set);
  278. return set;
  279. }
  280. int zori_widget_hover(struct zori_widget * widget) {
  281. return widget && ((widget->flags & ZORI_FLAG_HOVERED) == ZORI_FLAG_HOVERED);
  282. }
  283. int zori_widget_hover_(struct zori_widget * widget, int set) {
  284. if (set) {
  285. widget->flags = widget->flags | ZORI_FLAG_HOVERED;
  286. } else {
  287. widget->flags = widget->flags & (~ZORI_FLAG_HOVERED);
  288. }
  289. return zori_widget_hover(widget);
  290. }
  291. int zori_widget_marked(struct zori_widget * widget) {
  292. return widget && ((widget->flags & ZORI_FLAG_MARKED) == ZORI_FLAG_MARKED);
  293. }
  294. int zori_widget_marked_(struct zori_widget * widget, int set) {
  295. if (set) {
  296. widget->flags = widget->flags | ZORI_FLAG_MARKED;
  297. } else {
  298. widget->flags = widget->flags & (~ZORI_FLAG_MARKED);
  299. }
  300. return zori_widget_marked(widget);
  301. }
  302. int zori_widget_ready(struct zori_widget * widget) {
  303. return widget && ((widget->flags & ZORI_FLAG_READY) == ZORI_FLAG_READY);
  304. }
  305. int zori_widget_ready_(struct zori_widget * widget, int set) {
  306. if (set) {
  307. widget->flags = widget->flags | ZORI_FLAG_READY;
  308. } else {
  309. widget->flags = widget->flags & (~ZORI_FLAG_READY);
  310. }
  311. return zori_widget_ready(widget);
  312. }
  313. int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y) {
  314. return (rebox_coordinates_inside_p(&widget->box, x, y));
  315. }
  316. /* Registers an event handler for a widget. */
  317. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  318. zori_font * zori_widget_font(struct zori_widget * widget) {
  319. return widget->style.text.font;
  320. }
  321. zori_color zori_widget_forecolor(struct zori_widget * widget) {
  322. return widget->style.text.color;
  323. }
  324. zori_color zori_widget_backcolor(struct zori_widget * widget) {
  325. return widget->style.back.color;
  326. }
  327. int zori_widget_h(struct zori_widget * widget) {
  328. return widget->box.size.y;
  329. }
  330. int zori_widget_w(struct zori_widget * widget) {
  331. return widget->box.size.x;
  332. }
  333. int zori_widget_x(struct zori_widget * widget) {
  334. return widget->box.at.x;
  335. }
  336. int zori_widget_y(struct zori_widget * widget) {
  337. return widget->box.at.y;
  338. }
  339. /* Gets amount of child widgets of this widget, 0 if none, -1 on error.*/
  340. int zori_widget_count_children(struct zori_widget * widget) {
  341. if (!widget) return -1;
  342. return miao_size(&widget->children);
  343. }
  344. /* Returns the index-th child of his widget, or NULL on error or if no such child exists. */
  345. struct zori_widget * zori_widget_get_child(struct zori_widget * widget, int index) {
  346. int count = zori_widget_count_children(widget);
  347. if (count < 1) return NULL;
  348. if (index < 0) return NULL;
  349. if (index >= count) return NULL;
  350. return miao_unsafe_get(&widget->children, index);
  351. }
  352. zori_id
  353. zori_widget_paddings_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  354. if (!widget) return ZORI_ID_EINVAL;
  355. widget->inner = widget->box;
  356. widget->inner.at.x += left;
  357. widget->inner.at.y += top;
  358. widget->inner.size.x -= (left + right);
  359. widget->inner.size.y -= (top + bottom);
  360. return widget->id;
  361. }
  362. zori_id
  363. zori_widget_padding_(struct zori_widget * widget, int size) {
  364. return zori_widget_paddings_(widget, size, size, size, size);
  365. }
  366. zori_font * zori_widget_text_font(struct zori_widget * widget) {
  367. struct zori_style * style;
  368. if (!widget) {
  369. style = zori_get_default_style();
  370. } else {
  371. style = & widget->style;
  372. }
  373. return style->text.font;
  374. }
  375. struct zori_widget * zori_widget_init
  376. ( struct zori_widget * widget,
  377. zori_widget_type type,
  378. zori_id id,
  379. struct zori_widget * parent,
  380. zori_rebox * box, struct zori_style * style) {
  381. if (!widget) return NULL;
  382. struct zori_style * default_style = zori_get_default_style();
  383. miao_init(&widget->children);
  384. miao_init(&widget->handlers);
  385. widget->destroy = NULL;
  386. widget->z = 0;
  387. widget->flags = 0;
  388. widget->type = type;
  389. widget->id = ( (id < 0) ? zori_get_unused_id() : id);
  390. if (style) {
  391. widget->style = *style;
  392. if (!widget->style.text.font) {
  393. widget->style.text.font = default_style->text.font;
  394. }
  395. } else if (parent) {
  396. widget->style = parent->style;
  397. } else {
  398. widget->style = *default_style;
  399. }
  400. if (box) {
  401. widget->box = *box;
  402. } else if (parent) {
  403. widget->box = parent->box;
  404. } else {
  405. widget->box = rebox_make(0, 0, ZORI_WIDGET_DEFAULT_W, ZORI_WIDGET_DEFAULT_H);
  406. }
  407. zori_widget_margin_(widget, ZORI_MARGIN_DEFAULT);
  408. zori_widget_padding_(widget, ZORI_PADDING_DEFAULT);
  409. zori_widget_add_child(parent, widget);
  410. zori_registry_add(zori_get_registry(), widget->id, widget);
  411. return widget;
  412. }
  413. struct zori_widget *
  414. zori_widget_initall(struct zori_widget * widget, zori_widget_type type, int id,
  415. struct zori_widget * parent, zori_rebox * box, struct zori_style * style,
  416. struct zori_handler * handlers, size_t amount) {
  417. if (zori_widget_init(widget, type, id, parent, box, style)) {
  418. zori_widget_add_handlers(widget, handlers, amount);
  419. }
  420. return widget;
  421. }
  422. /* Lets the widget handle an event and bubbles it on to it's children
  423. * recursively depending on the need to do that. */
  424. void zori_widget_handle_event(struct zori_widget * widget, union zori_event * event) {
  425. zori_widget_raise_event(widget, event);
  426. }
  427. /* Returns the first parent or super-parent of this widget for which
  428. * predicate() returns true.
  429. */
  430. struct zori_widget *
  431. zori_widget_find_parent(struct zori_widget * widget,
  432. bool (*predicate)(struct zori_widget * parent, void * extra),
  433. void * extra
  434. ) {
  435. struct zori_widget * parent;
  436. if (!widget) return NULL;
  437. parent = widget->parent;
  438. while (parent) {
  439. if (predicate(parent, extra)) return parent;
  440. if (parent == parent->parent) {
  441. LOG_ERROR("GUI setup not correct. Parent/child loop in widget %p.", widget);
  442. return NULL;
  443. }
  444. parent = parent->parent;
  445. }
  446. return NULL;
  447. }
  448. int zori_mark_widget(struct zori_widget * widget) {
  449. struct zori_screen * screen = zori_widget_get_screen(widget);
  450. if (screen) {
  451. screen->cursors.keyjoy.p = widget->box.at;
  452. }
  453. return ZORI_HANDLE_DONE;
  454. }
  455. zori_id zori_widget_set_int_result(struct zori_widget * widget, int value) {
  456. if (widget) {
  457. widget->result.value.integer = value;
  458. widget->result.type = ZORI_RESULT_TYPE_INTEGER;
  459. widget->result.ready = widget->id;
  460. return widget->id;
  461. }
  462. return ZORI_ID_EINVAL;
  463. }
  464. zori_id zori_widget_set_string_result(struct zori_widget * widget,
  465. zori_string * value) {
  466. if (widget) {
  467. widget->result.value.string = value;
  468. widget->result.type = ZORI_RESULT_TYPE_STRING;
  469. widget->result.ready = widget->id;
  470. return widget->id;
  471. }
  472. return ZORI_ID_EINVAL;
  473. }
  474. zori_id zori_widget_set_closed_result(struct zori_widget * widget, int value) {
  475. if (widget) {
  476. widget->result.value.closed = value;
  477. widget->result.type = ZORI_RESULT_TYPE_CLOSED;
  478. widget->result.ready = widget->id;
  479. return widget->id;
  480. }
  481. return ZORI_ID_EINVAL;
  482. }
  483. zori_id zori_set_mark_style(zori_id id, struct zori_style style) {
  484. struct zori_widget * widget = zori_get_widget(id);
  485. struct zori_screen * screen = zori_widget_get_screen(widget);
  486. if (screen) {
  487. screen->cursors.keyjoy.style = style;
  488. return id;
  489. } else {
  490. return ZORI_ID_EINVAL;
  491. }
  492. }
  493. zori_id zori_set_hover_style(zori_id id, struct zori_style style) {
  494. struct zori_widget * widget = zori_get_widget(id);
  495. struct zori_screen * screen = zori_widget_get_screen(widget);
  496. if (screen) {
  497. screen->cursors.mouse.style = style;
  498. return id;
  499. } else {
  500. return ZORI_ID_EINVAL;
  501. }
  502. }
  503. /* Moves the widget by the offset (dx, dy). Does not move it's children. */
  504. void zori_widget_move_self_by(struct zori_widget * widget, int dx, int dy) {
  505. int index, stop;
  506. widget->box.at.x += dx;
  507. widget->box.at.y += dy;
  508. widget->inner.at.x += dx;
  509. widget->inner.at.y += dy;
  510. widget->outer.at.x += dx;
  511. widget->outer.at.y += dy;
  512. }
  513. /* Moves resizes the widget by (dw, dh). Does not resize it's children.
  514. * Margin and paddding are resized also by dw and dh;
  515. */
  516. void zori_widget_resize_self_by(struct zori_widget * widget, int dw, int dh) {
  517. int index, stop;
  518. widget->box.size.x += dw;
  519. widget->box.size.y += dh;
  520. widget->inner.size.x += dw;
  521. widget->inner.size.y += dh;
  522. widget->outer.size.x += dw;
  523. widget->outer.size.y += dh;
  524. }
  525. /* Moves the widget by the offset (dx, dy). It's children will be moved as well recursively
  526. * keeping the offset */
  527. void zori_widget_move_by(struct zori_widget * widget, int dx, int dy) {
  528. int index, stop;
  529. zori_widget_move_self_by(widget, dx, dy);
  530. stop = zori_widget_count_children(widget);
  531. for (index= 0 ; index < stop; index ++) {
  532. struct zori_widget * child = zori_widget_get_child(widget, index);
  533. zori_widget_move_by(child, dx, dy);
  534. }
  535. }
  536. /* Moves the widget to (x, y). It's children will be moved as well recursively
  537. * keeping the offset */
  538. void zori_widget_move_to(struct zori_widget * widget, int x, int y) {
  539. int dx = widget->box.at.x - x;
  540. int dy = widget->box.at.y - y;
  541. zori_widget_move_by(widget, dx, dy);
  542. }
  543. /* Moves the widget to (x, y). Doesn't move children. */
  544. void zori_widget_move_self_to(struct zori_widget * widget, int x, int y) {
  545. int dx = widget->box.at.x - x;
  546. int dy = widget->box.at.y - y;
  547. zori_widget_move_self_by(widget, dx, dy);
  548. }
  549. /* Resizes the widget to (w, h). Doesn't resize children. Margins and padding will be
  550. * resized accordingly. */
  551. void zori_widget_resize_self_to(struct zori_widget * widget, int w, int h) {
  552. int dw = widget->box.size.x - w;
  553. int dh = widget->box.size.y - h;
  554. zori_widget_resize_self_by(widget, dw, dh);
  555. }
  556. /* Makes the widget fit itself to all direct children.
  557. * Does not resize nor move the children.
  558. */
  559. void zori_widget_fit_to_children(struct zori_widget * widget) {
  560. int index, stop;
  561. int min_x = 640;
  562. int min_y = 480;
  563. int min_w = 0;
  564. int min_h = 0;
  565. struct zori_screen * screen = zori_widget_get_screen(widget);
  566. if (screen) {
  567. min_x = screen->widget.box.size.x;
  568. min_y = screen->widget.box.size.y;
  569. }
  570. stop = zori_widget_count_children(widget);
  571. for (index= 0 ; index < stop; index ++) {
  572. struct zori_widget * child = zori_widget_get_child(widget, index);
  573. if (child->outer.at.x < min_x) {
  574. min_x = child->outer.at.x;
  575. }
  576. if (child->outer.at.y < min_y) {
  577. min_y = child->outer.at.y;
  578. }
  579. if ((child->outer.size.x) > min_w) {
  580. min_w = child->outer.size.x;
  581. }
  582. if ((child->outer.size.y) > min_h) {
  583. min_h = child->outer.size.y;
  584. }
  585. }
  586. zori_widget_move_self_to(widget, min_x, min_y);
  587. zori_widget_resize_self_to(widget, min_w, min_h);
  588. }