container.c 7.7 KB

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