1
0

container.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. */
  14. #include <rtgui/widgets/toplevel.h>
  15. #include <rtgui/widgets/container.h>
  16. static void _rtgui_container_constructor(rtgui_container_t *container)
  17. {
  18. /* set event handler and init field */
  19. rtgui_widget_set_event_handler(RTGUI_WIDGET(container), rtgui_container_event_handler);
  20. rtgui_list_init(&(container->children));
  21. /* set focused widget to itself */
  22. container->focused = RTGUI_WIDGET(container);
  23. /* set container as focusable widget */
  24. RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  25. }
  26. static void _rtgui_container_destructor(rtgui_container_t *container)
  27. {
  28. /* destroy children of container */
  29. rtgui_container_destroy_children(container);
  30. }
  31. static void _rtgui_container_update_toplevel(rtgui_container_t* container)
  32. {
  33. struct rtgui_list_node* node;
  34. rtgui_list_foreach(node, &(container->children))
  35. {
  36. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  37. /* set child toplevel */
  38. child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));
  39. if (RTGUI_IS_CONTAINER(child))
  40. {
  41. _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
  42. }
  43. }
  44. }
  45. rtgui_type_t *rtgui_container_type_get(void)
  46. {
  47. static rtgui_type_t *container_type = RT_NULL;
  48. if (!container_type)
  49. {
  50. container_type = rtgui_type_create("container", RTGUI_WIDGET_TYPE,
  51. sizeof(rtgui_container_t),
  52. RTGUI_CONSTRUCTOR(_rtgui_container_constructor),
  53. RTGUI_DESTRUCTOR(_rtgui_container_destructor));
  54. }
  55. return container_type;
  56. }
  57. rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event)
  58. {
  59. /* handle in child widget */
  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 (w->event_handler(w, event) == RT_TRUE) return RT_TRUE;
  66. }
  67. return RT_FALSE;
  68. }
  69. rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse* event)
  70. {
  71. /* handle in child widget */
  72. struct rtgui_list_node* node;
  73. rtgui_list_foreach(node, &(container->children))
  74. {
  75. struct rtgui_widget* w;
  76. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  77. if (rtgui_rect_contains_point(&(w->extent), event->x, event->y) == RT_EOK)
  78. {
  79. if (w->event_handler(w, (rtgui_event_t*)event) == RT_TRUE) return RT_TRUE;
  80. }
  81. }
  82. return RT_FALSE;
  83. }
  84. rt_bool_t rtgui_container_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  85. {
  86. rtgui_container_t *container = RTGUI_CONTAINER(widget);
  87. switch (event->type)
  88. {
  89. case RTGUI_EVENT_PAINT:
  90. #ifndef RTGUI_USING_SMALL_SIZE
  91. if (widget->on_draw != RT_NULL)
  92. {
  93. return widget->on_draw(widget, event);
  94. }
  95. #endif
  96. rtgui_container_dispatch_event(container, event);
  97. break;
  98. case RTGUI_EVENT_KBD:
  99. #ifndef RTGUI_USING_SMALL_SIZE
  100. if (widget->on_key != RT_NULL)
  101. {
  102. return widget->on_key(widget, event);
  103. }
  104. else
  105. #endif
  106. {
  107. /* let parent to handle keyboard event */
  108. if (widget->parent != RT_NULL && widget->parent != widget->toplevel)
  109. {
  110. return widget->parent->event_handler(widget->parent, event);
  111. }
  112. }
  113. break;
  114. case RTGUI_EVENT_MOUSE_BUTTON:
  115. /* handle in child widget */
  116. if (rtgui_container_dispatch_mouse_event(container,
  117. (struct rtgui_event_mouse*)event) == RT_FALSE)
  118. {
  119. #ifndef RTGUI_USING_SMALL_SIZE
  120. /* handle event in current widget */
  121. if (widget->on_mouseclick != RT_NULL)
  122. {
  123. return widget->on_mouseclick(widget, event);
  124. }
  125. #endif
  126. }
  127. else return RT_TRUE;
  128. break;
  129. case RTGUI_EVENT_MOUSE_MOTION:
  130. if (rtgui_container_dispatch_mouse_event(container,
  131. (struct rtgui_event_mouse*)event) == RT_FALSE)
  132. {
  133. #ifndef RTGUI_USING_SMALL_SIZE
  134. #if 0
  135. /* handle event in current widget */
  136. if (widget->on_mousemotion != RT_NULL)
  137. {
  138. return widget->on_mousemotion(widget, event);
  139. }
  140. #endif
  141. #endif
  142. }
  143. else return RT_TRUE;
  144. break;
  145. case RTGUI_EVENT_COMMAND:
  146. if (rtgui_container_dispatch_event(container, event) == RT_FALSE)
  147. {
  148. #ifndef RTGUI_USING_SMALL_SIZE
  149. if (widget->on_command != RT_NULL)
  150. {
  151. return widget->on_command(widget, event);
  152. }
  153. #endif
  154. }
  155. else return RT_TRUE;
  156. break;
  157. case RTGUI_EVENT_RESIZE:
  158. if (rtgui_container_dispatch_event(container, event) == RT_FALSE)
  159. {
  160. #ifndef RTGUI_USING_SMALL_SIZE
  161. if (widget->on_size != RT_NULL)
  162. return widget->on_size(widget, event);
  163. #endif
  164. }
  165. else return RT_TRUE;
  166. break;
  167. default:
  168. /* call parent widget event handler */
  169. return rtgui_widget_event_handler(widget, event);
  170. }
  171. return RT_FALSE;
  172. }
  173. /*
  174. * This function will add a child to a container widget
  175. * Note: this function will not change the widget layout
  176. * the layout is the responsibility of layout widget, such as box.
  177. */
  178. void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child)
  179. {
  180. RT_ASSERT(container != RT_NULL);
  181. RT_ASSERT(child != RT_NULL);
  182. /* set parent and toplevel widget */
  183. child->parent = RTGUI_WIDGET(container);
  184. /* put widget to parent's children list */
  185. rtgui_list_append(&(container->children), &(child->sibling));
  186. /* update children toplevel */
  187. if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
  188. RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(container)->toplevel))
  189. {
  190. child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));
  191. /* update all child toplevel */
  192. if (RTGUI_IS_CONTAINER(child))
  193. {
  194. _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
  195. }
  196. }
  197. }
  198. /* remove a child to widget */
  199. void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child)
  200. {
  201. RT_ASSERT(container != RT_NULL);
  202. RT_ASSERT(child != RT_NULL);
  203. if (child == container->focused)
  204. {
  205. /* set focused to itself */
  206. container->focused = RTGUI_WIDGET(container);
  207. rtgui_widget_focus(RTGUI_WIDGET(container));
  208. }
  209. /* remove widget from parent's children list */
  210. rtgui_list_remove(&(container->children), &(child->sibling));
  211. /* set parent and toplevel widget */
  212. child->parent = RT_NULL;
  213. child->toplevel = RT_NULL;
  214. }
  215. /* destroy all children of container */
  216. void rtgui_container_destroy_children(rtgui_container_t *container)
  217. {
  218. struct rtgui_list_node* node;
  219. if (container == RT_NULL) return;
  220. node = container->children.next;
  221. while (node != RT_NULL)
  222. {
  223. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  224. if (RTGUI_IS_CONTAINER(child))
  225. {
  226. /* break parent firstly */
  227. child->parent = RT_NULL;
  228. /* destroy children of child */
  229. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  230. }
  231. /* remove widget from parent's children list */
  232. rtgui_list_remove(&(container->children), &(child->sibling));
  233. /* set parent and toplevel widget */
  234. child->parent = RT_NULL;
  235. /* destroy object and remove from parent */
  236. rtgui_object_destroy(RTGUI_OBJECT(child));
  237. node = container->children.next;
  238. }
  239. container->children.next = RT_NULL;
  240. container->focused = RTGUI_WIDGET(container);
  241. if (RTGUI_WIDGET(container)->parent != RT_NULL)
  242. rtgui_widget_focus(RTGUI_WIDGET(container));
  243. /* update widget clip */
  244. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel));
  245. }
  246. rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container)
  247. {
  248. rtgui_widget_t* child = RT_NULL;
  249. if (container->children.next != RT_NULL)
  250. {
  251. child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
  252. }
  253. return child;
  254. }