container.c 7.1 KB

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