1
0

container.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. case RTGUI_EVENT_RESIZE:
  137. rtgui_container_dispatch_event(container, event);
  138. break;
  139. case RTGUI_EVENT_UPDATE_TOPLVL:
  140. /* call parent handler */
  141. rtgui_widget_onupdate_toplvl(object, event);
  142. /* update the children */
  143. rtgui_container_broadcast_event(container, event);
  144. break;
  145. default:
  146. /* call parent widget event handler */
  147. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  148. }
  149. return RT_FALSE;
  150. }
  151. RTM_EXPORT(rtgui_container_event_handler);
  152. rtgui_container_t* rtgui_container_create(void)
  153. {
  154. struct rtgui_container* container;
  155. /* allocate container */
  156. container = (struct rtgui_container*) rtgui_widget_create (RTGUI_CONTAINER_TYPE);
  157. return container;
  158. }
  159. RTM_EXPORT(rtgui_container_create);
  160. void rtgui_container_destroy(rtgui_container_t* container)
  161. {
  162. rtgui_widget_destroy(RTGUI_WIDGET(container));
  163. }
  164. RTM_EXPORT(rtgui_container_destroy);
  165. /*
  166. * This function will add a child to a container widget
  167. * Note: this function will not change the widget layout
  168. * the layout is the responsibility of layout widget, such as box.
  169. */
  170. void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child)
  171. {
  172. RT_ASSERT(container != RT_NULL);
  173. RT_ASSERT(child != RT_NULL);
  174. /* set parent and toplevel widget */
  175. child->parent = RTGUI_WIDGET(container);
  176. /* put widget to parent's children list */
  177. rtgui_list_append(&(container->children), &(child->sibling));
  178. /* update children toplevel */
  179. if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
  180. RTGUI_IS_WIN(RTGUI_WIDGET(container)->toplevel))
  181. {
  182. struct rtgui_event_update_toplvl eup;
  183. RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup);
  184. eup.toplvl = RTGUI_WIDGET(container)->toplevel;
  185. rtgui_container_broadcast_event(container, (struct rtgui_event*)&eup);
  186. }
  187. }
  188. RTM_EXPORT(rtgui_container_add_child);
  189. /* remove a child to widget */
  190. void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child)
  191. {
  192. RT_ASSERT(container != RT_NULL);
  193. RT_ASSERT(child != RT_NULL);
  194. rtgui_widget_unfocus(child);
  195. /* remove widget from parent's children list */
  196. rtgui_list_remove(&(container->children), &(child->sibling));
  197. /* set parent and toplevel widget */
  198. child->parent = RT_NULL;
  199. child->toplevel = RT_NULL;
  200. }
  201. RTM_EXPORT(rtgui_container_remove_child);
  202. /* destroy all children of container */
  203. void rtgui_container_destroy_children(rtgui_container_t *container)
  204. {
  205. struct rtgui_list_node* node;
  206. if (container == RT_NULL)
  207. return;
  208. node = container->children.next;
  209. while (node != RT_NULL)
  210. {
  211. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  212. if (RTGUI_IS_CONTAINER(child))
  213. {
  214. /* break parent firstly */
  215. child->parent = RT_NULL;
  216. /* destroy children of child */
  217. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  218. }
  219. /* remove widget from parent's children list */
  220. rtgui_list_remove(&(container->children), &(child->sibling));
  221. /* set parent and toplevel widget */
  222. child->parent = RT_NULL;
  223. /* destroy object and remove from parent */
  224. rtgui_object_destroy(RTGUI_OBJECT(child));
  225. node = container->children.next;
  226. }
  227. container->children.next = RT_NULL;
  228. /* update widget clip */
  229. rtgui_win_update_clip(RTGUI_WIN(RTGUI_WIDGET(container)->toplevel));
  230. }
  231. RTM_EXPORT(rtgui_container_destroy_children);
  232. rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container)
  233. {
  234. rtgui_widget_t* child = RT_NULL;
  235. RT_ASSERT(container != RT_NULL);
  236. if (container->children.next != RT_NULL)
  237. {
  238. child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
  239. }
  240. return child;
  241. }
  242. RTM_EXPORT(rtgui_container_get_first_child);
  243. void rtgui_container_set_box(rtgui_container_t* container, struct rtgui_box* box)
  244. {
  245. if (container == RT_NULL || box == RT_NULL)
  246. return;
  247. container->layout_box = box;
  248. box->container = container;
  249. }
  250. RTM_EXPORT(rtgui_container_set_box);
  251. void rtgui_container_layout(struct rtgui_container* container)
  252. {
  253. if (container == RT_NULL || container->layout_box == RT_NULL)
  254. return;
  255. rtgui_box_layout(container->layout_box);
  256. }
  257. RTM_EXPORT(rtgui_container_layout);