1
0

container.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * File : container.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. * 2010-09-24 Bernard fix container destroy issue
  14. */
  15. #include <rtgui/dc.h>
  16. #include <rtgui/rtgui_system.h>
  17. #include <rtgui/rtgui_app.h>
  18. #include <rtgui/widgets/container.h>
  19. #include <rtgui/widgets/window.h>
  20. static void _rtgui_container_constructor(rtgui_container_t *container)
  21. {
  22. /* init container */
  23. rtgui_object_set_event_handler(RTGUI_OBJECT(container),
  24. rtgui_container_event_handler);
  25. rtgui_list_init(&(container->children));
  26. container->layout_box = RT_NULL;
  27. RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  28. }
  29. static void _rtgui_container_destructor(rtgui_container_t *container)
  30. {
  31. rtgui_container_destroy_children(container);
  32. if (container->layout_box != RT_NULL)
  33. rtgui_object_destroy(RTGUI_OBJECT(container->layout_box));
  34. }
  35. DEFINE_CLASS_TYPE(container, "container",
  36. RTGUI_WIDGET_TYPE,
  37. _rtgui_container_constructor,
  38. _rtgui_container_destructor,
  39. sizeof(struct rtgui_container));
  40. RTM_EXPORT(_rtgui_container);
  41. rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t *event)
  42. {
  43. /* handle in child widget */
  44. struct rtgui_list_node *node;
  45. rtgui_list_foreach(node, &(container->children))
  46. {
  47. struct rtgui_widget *w;
  48. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  49. if (RTGUI_OBJECT(w)->event_handler &&
  50. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event) == RT_TRUE)
  51. return RT_TRUE;
  52. }
  53. return RT_FALSE;
  54. }
  55. RTM_EXPORT(rtgui_container_dispatch_event);
  56. /* broadcast means that the return value of event handlers will be ignored. The
  57. * events will always reach every child.*/
  58. rt_bool_t rtgui_container_broadcast_event(struct rtgui_container *container, struct rtgui_event *event)
  59. {
  60. struct rtgui_list_node *node;
  61. rtgui_list_foreach(node, &(container->children))
  62. {
  63. struct rtgui_widget *w;
  64. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  65. if (RTGUI_OBJECT(w)->event_handler)
  66. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event);
  67. }
  68. return RT_FALSE;
  69. }
  70. RTM_EXPORT(rtgui_container_broadcast_event);
  71. rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse *event)
  72. {
  73. /* handle in child widget */
  74. struct rtgui_list_node *node;
  75. struct rtgui_widget *old_focus;
  76. old_focus = RTGUI_WIDGET(container)->toplevel->focused_widget;
  77. rtgui_list_foreach(node, &(container->children))
  78. {
  79. struct rtgui_widget *w;
  80. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  81. if (rtgui_rect_contains_point(&(w->extent),
  82. event->x, event->y) == RT_EOK)
  83. {
  84. if ((old_focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
  85. rtgui_widget_focus(w);
  86. if (RTGUI_OBJECT(w)->event_handler &&
  87. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w),
  88. (rtgui_event_t *)event) == RT_TRUE)
  89. return RT_TRUE;
  90. }
  91. }
  92. return RT_FALSE;
  93. }
  94. RTM_EXPORT(rtgui_container_dispatch_mouse_event);
  95. rt_bool_t rtgui_container_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  96. {
  97. struct rtgui_container *container;
  98. struct rtgui_widget *widget;
  99. RT_ASSERT(object != RT_NULL);
  100. RT_ASSERT(event != RT_NULL);
  101. container = RTGUI_CONTAINER(object);
  102. widget = RTGUI_WIDGET(object);
  103. switch (event->type)
  104. {
  105. case RTGUI_EVENT_PAINT:
  106. {
  107. struct rtgui_dc *dc;
  108. struct rtgui_rect rect;
  109. dc = rtgui_dc_begin_drawing(widget);
  110. if (dc == RT_NULL)
  111. return RT_FALSE;
  112. rtgui_widget_get_rect(widget, &rect);
  113. /* fill container with background */
  114. rtgui_dc_fill_rect(dc, &rect);
  115. /* paint on each child */
  116. rtgui_container_dispatch_event(container, event);
  117. rtgui_dc_end_drawing(dc);
  118. }
  119. break;
  120. case RTGUI_EVENT_KBD:
  121. break;
  122. case RTGUI_EVENT_MOUSE_BUTTON:
  123. case RTGUI_EVENT_MOUSE_MOTION:
  124. /* handle in child widget */
  125. return rtgui_container_dispatch_mouse_event(container,
  126. (struct rtgui_event_mouse *)event);
  127. case RTGUI_EVENT_SHOW:
  128. rtgui_widget_onshow(RTGUI_OBJECT(container), event);
  129. rtgui_container_dispatch_event(container, event);
  130. break;
  131. case RTGUI_EVENT_HIDE:
  132. rtgui_widget_onhide(RTGUI_OBJECT(container), event);
  133. rtgui_container_dispatch_event(container, event);
  134. break;
  135. case RTGUI_EVENT_COMMAND:
  136. rtgui_container_dispatch_event(container, event);
  137. break;
  138. case RTGUI_EVENT_UPDATE_TOPLVL:
  139. /* call parent handler */
  140. rtgui_widget_onupdate_toplvl(object, event);
  141. /* update the children */
  142. rtgui_container_broadcast_event(container, event);
  143. break;
  144. case RTGUI_EVENT_RESIZE:
  145. /* re-layout container */
  146. rtgui_container_layout(container);
  147. break;
  148. default:
  149. /* call parent widget event handler */
  150. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  151. }
  152. return RT_FALSE;
  153. }
  154. RTM_EXPORT(rtgui_container_event_handler);
  155. rtgui_container_t *rtgui_container_create(void)
  156. {
  157. struct rtgui_container *container;
  158. /* allocate container */
  159. container = (struct rtgui_container *) rtgui_widget_create(RTGUI_CONTAINER_TYPE);
  160. return container;
  161. }
  162. RTM_EXPORT(rtgui_container_create);
  163. void rtgui_container_destroy(rtgui_container_t *container)
  164. {
  165. rtgui_widget_destroy(RTGUI_WIDGET(container));
  166. }
  167. RTM_EXPORT(rtgui_container_destroy);
  168. /*
  169. * This function will add a child to a container widget
  170. * Note: this function will not change the widget layout
  171. * the layout is the responsibility of layout widget, such as box.
  172. */
  173. void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t *child)
  174. {
  175. RT_ASSERT(container != RT_NULL);
  176. RT_ASSERT(child != RT_NULL);
  177. /* set parent and toplevel widget */
  178. child->parent = RTGUI_WIDGET(container);
  179. /* put widget to parent's children list */
  180. rtgui_list_append(&(container->children), &(child->sibling));
  181. /* update children toplevel */
  182. if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
  183. RTGUI_IS_WIN(RTGUI_WIDGET(container)->toplevel))
  184. {
  185. struct rtgui_event_update_toplvl eup;
  186. RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup);
  187. eup.toplvl = RTGUI_WIDGET(container)->toplevel;
  188. rtgui_container_broadcast_event(container, (struct rtgui_event *)&eup);
  189. }
  190. }
  191. RTM_EXPORT(rtgui_container_add_child);
  192. /* remove a child to widget */
  193. void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t *child)
  194. {
  195. RT_ASSERT(container != RT_NULL);
  196. RT_ASSERT(child != RT_NULL);
  197. rtgui_widget_unfocus(child);
  198. /* remove widget from parent's children list */
  199. rtgui_list_remove(&(container->children), &(child->sibling));
  200. /* set parent and toplevel widget */
  201. child->parent = RT_NULL;
  202. child->toplevel = RT_NULL;
  203. }
  204. RTM_EXPORT(rtgui_container_remove_child);
  205. /* destroy all children of container */
  206. void rtgui_container_destroy_children(rtgui_container_t *container)
  207. {
  208. struct rtgui_list_node *node;
  209. if (container == RT_NULL)
  210. return;
  211. node = container->children.next;
  212. while (node != RT_NULL)
  213. {
  214. rtgui_widget_t *child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  215. if (RTGUI_IS_CONTAINER(child))
  216. {
  217. /* break parent firstly */
  218. child->parent = RT_NULL;
  219. /* destroy children of child */
  220. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  221. }
  222. /* remove widget from parent's children list */
  223. rtgui_list_remove(&(container->children), &(child->sibling));
  224. /* set parent and toplevel widget */
  225. child->parent = RT_NULL;
  226. /* destroy object and remove from parent */
  227. rtgui_object_destroy(RTGUI_OBJECT(child));
  228. node = container->children.next;
  229. }
  230. container->children.next = RT_NULL;
  231. /* update widget clip */
  232. rtgui_win_update_clip(RTGUI_WIN(RTGUI_WIDGET(container)->toplevel));
  233. }
  234. RTM_EXPORT(rtgui_container_destroy_children);
  235. rtgui_widget_t *rtgui_container_get_first_child(rtgui_container_t *container)
  236. {
  237. rtgui_widget_t *child = RT_NULL;
  238. RT_ASSERT(container != RT_NULL);
  239. if (container->children.next != RT_NULL)
  240. {
  241. child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
  242. }
  243. return child;
  244. }
  245. RTM_EXPORT(rtgui_container_get_first_child);
  246. void rtgui_container_set_box(rtgui_container_t *container, struct rtgui_box *box)
  247. {
  248. if (container == RT_NULL || box == RT_NULL)
  249. return;
  250. container->layout_box = box;
  251. box->container = container;
  252. }
  253. RTM_EXPORT(rtgui_container_set_box);
  254. void rtgui_container_layout(struct rtgui_container *container)
  255. {
  256. if (container == RT_NULL || container->layout_box == RT_NULL)
  257. return;
  258. rtgui_box_layout(container->layout_box);
  259. }
  260. RTM_EXPORT(rtgui_container_layout);