container.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event)
  41. {
  42. /* handle in child widget */
  43. struct rtgui_list_node* node;
  44. rtgui_list_foreach(node, &(container->children))
  45. {
  46. struct rtgui_widget* w;
  47. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  48. if (RTGUI_OBJECT(w)->event_handler &&
  49. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event) == RT_TRUE)
  50. return RT_TRUE;
  51. }
  52. return RT_FALSE;
  53. }
  54. /* broadcast means that the return value of event handlers will be ignored. The
  55. * events will always reach every child.*/
  56. rt_bool_t rtgui_container_broadcast_event(struct rtgui_container *container, struct rtgui_event *event)
  57. {
  58. struct rtgui_list_node* node;
  59. rtgui_list_foreach(node, &(container->children))
  60. {
  61. struct rtgui_widget* w;
  62. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  63. if (RTGUI_OBJECT(w)->event_handler)
  64. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event) == RT_TRUE;
  65. }
  66. return RT_FALSE;
  67. }
  68. rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse* event)
  69. {
  70. /* handle in child widget */
  71. struct rtgui_list_node* node;
  72. struct rtgui_widget *old_focus;
  73. old_focus = RTGUI_WIDGET(container)->toplevel->focused_widget;
  74. rtgui_list_foreach(node, &(container->children))
  75. {
  76. struct rtgui_widget* w;
  77. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  78. if (rtgui_rect_contains_point(&(w->extent),
  79. event->x, event->y) == RT_EOK)
  80. {
  81. if ((old_focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
  82. rtgui_widget_focus(w);
  83. if (RTGUI_OBJECT(w)->event_handler &&
  84. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w),
  85. (rtgui_event_t*)event) == RT_TRUE)
  86. return RT_TRUE;
  87. }
  88. }
  89. return RT_FALSE;
  90. }
  91. rt_bool_t rtgui_container_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  92. {
  93. struct rtgui_container *container;
  94. struct rtgui_widget *widget;
  95. RT_ASSERT(object != RT_NULL);
  96. RT_ASSERT(event != RT_NULL);
  97. container = RTGUI_CONTAINER(object);
  98. widget = RTGUI_WIDGET(object);
  99. switch (event->type)
  100. {
  101. case RTGUI_EVENT_PAINT:
  102. {
  103. struct rtgui_dc* dc;
  104. struct rtgui_rect rect;
  105. dc = rtgui_dc_begin_drawing(widget);
  106. if (dc == RT_NULL)
  107. return RT_FALSE;
  108. rtgui_widget_get_rect(widget, &rect);
  109. /* fill container with background */
  110. rtgui_dc_fill_rect(dc, &rect);
  111. /* paint on each child */
  112. rtgui_container_dispatch_event(container, event);
  113. rtgui_dc_end_drawing(dc);
  114. }
  115. break;
  116. case RTGUI_EVENT_KBD:
  117. break;
  118. case RTGUI_EVENT_MOUSE_BUTTON:
  119. case RTGUI_EVENT_MOUSE_MOTION:
  120. /* handle in child widget */
  121. return rtgui_container_dispatch_mouse_event(container,
  122. (struct rtgui_event_mouse*)event);
  123. case RTGUI_EVENT_SHOW:
  124. rtgui_widget_onshow(RTGUI_OBJECT(container), event);
  125. rtgui_container_dispatch_event(container, event);
  126. break;
  127. case RTGUI_EVENT_HIDE:
  128. rtgui_widget_onhide(RTGUI_OBJECT(container), event);
  129. rtgui_container_dispatch_event(container, event);
  130. break;
  131. case RTGUI_EVENT_COMMAND:
  132. case RTGUI_EVENT_RESIZE:
  133. rtgui_container_dispatch_event(container, event);
  134. break;
  135. case RTGUI_EVENT_UPDATE_TOPLVL:
  136. /* call parent handler */
  137. rtgui_widget_onupdate_toplvl(object, event);
  138. /* update the children */
  139. rtgui_container_broadcast_event(container, event);
  140. break;
  141. default:
  142. /* call parent widget event handler */
  143. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  144. }
  145. return RT_FALSE;
  146. }
  147. rtgui_container_t* rtgui_container_create(void)
  148. {
  149. struct rtgui_container* container;
  150. /* allocate container */
  151. container = (struct rtgui_container*) rtgui_widget_create (RTGUI_CONTAINER_TYPE);
  152. return container;
  153. }
  154. void rtgui_container_destroy(rtgui_container_t* container)
  155. {
  156. rtgui_widget_destroy(RTGUI_WIDGET(container));
  157. }
  158. /*
  159. * This function will add a child to a container widget
  160. * Note: this function will not change the widget layout
  161. * the layout is the responsibility of layout widget, such as box.
  162. */
  163. void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child)
  164. {
  165. RT_ASSERT(container != RT_NULL);
  166. RT_ASSERT(child != RT_NULL);
  167. /* set parent and toplevel widget */
  168. child->parent = RTGUI_WIDGET(container);
  169. /* put widget to parent's children list */
  170. rtgui_list_append(&(container->children), &(child->sibling));
  171. /* update children toplevel */
  172. if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
  173. RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(container)->toplevel))
  174. {
  175. struct rtgui_event_update_toplvl eup;
  176. RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup);
  177. eup.toplvl = RTGUI_WIDGET(container)->toplevel;
  178. rtgui_container_broadcast_event(container, (struct rtgui_event*)&eup);
  179. }
  180. }
  181. /* remove a child to widget */
  182. void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child)
  183. {
  184. RT_ASSERT(container != RT_NULL);
  185. RT_ASSERT(child != RT_NULL);
  186. rtgui_widget_unfocus(child);
  187. /* remove widget from parent's children list */
  188. rtgui_list_remove(&(container->children), &(child->sibling));
  189. /* set parent and toplevel widget */
  190. child->parent = RT_NULL;
  191. child->toplevel = RT_NULL;
  192. }
  193. /* destroy all children of container */
  194. void rtgui_container_destroy_children(rtgui_container_t *container)
  195. {
  196. struct rtgui_list_node* node;
  197. if (container == RT_NULL)
  198. return;
  199. node = container->children.next;
  200. while (node != RT_NULL)
  201. {
  202. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  203. if (RTGUI_IS_CONTAINER(child))
  204. {
  205. /* break parent firstly */
  206. child->parent = RT_NULL;
  207. /* destroy children of child */
  208. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  209. }
  210. /* remove widget from parent's children list */
  211. rtgui_list_remove(&(container->children), &(child->sibling));
  212. /* set parent and toplevel widget */
  213. child->parent = RT_NULL;
  214. /* destroy object and remove from parent */
  215. rtgui_object_destroy(RTGUI_OBJECT(child));
  216. node = container->children.next;
  217. }
  218. container->children.next = RT_NULL;
  219. /* update widget clip */
  220. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel));
  221. }
  222. rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container)
  223. {
  224. rtgui_widget_t* child = RT_NULL;
  225. RT_ASSERT(container != RT_NULL);
  226. if (container->children.next != RT_NULL)
  227. {
  228. child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
  229. }
  230. return child;
  231. }
  232. void rtgui_container_set_box(rtgui_container_t* container, struct rtgui_box* box)
  233. {
  234. if (container == RT_NULL || box == RT_NULL)
  235. return;
  236. container->layout_box = box;
  237. box->container = container;
  238. }
  239. void rtgui_container_layout(struct rtgui_container* container)
  240. {
  241. if (container == RT_NULL || container->layout_box == RT_NULL)
  242. return;
  243. rtgui_box_layout(container->layout_box);
  244. }