container.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. DEFINE_CLASS_TYPE(container, "container",
  46. RTGUI_WIDGET_TYPE,
  47. _rtgui_container_constructor,
  48. _rtgui_container_destructor,
  49. sizeof(struct rtgui_container));
  50. rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event)
  51. {
  52. /* handle in child widget */
  53. struct rtgui_list_node* node;
  54. rtgui_list_foreach(node, &(container->children))
  55. {
  56. struct rtgui_widget* w;
  57. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  58. if (w->event_handler(w, event) == RT_TRUE) return RT_TRUE;
  59. }
  60. return RT_FALSE;
  61. }
  62. rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse* event)
  63. {
  64. /* handle in child widget */
  65. struct rtgui_list_node* node;
  66. rtgui_widget_t *focus;
  67. /* get focus widget on toplevel */
  68. focus = RTGUI_CONTAINER(RTGUI_WIDGET(container)->toplevel)->focused;
  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 ((focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
  76. rtgui_widget_focus(w);
  77. if (w->event_handler(w, (rtgui_event_t*)event) == RT_TRUE) return RT_TRUE;
  78. }
  79. }
  80. return RT_FALSE;
  81. }
  82. rt_bool_t rtgui_container_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  83. {
  84. rtgui_container_t *container = RTGUI_CONTAINER(widget);
  85. switch (event->type)
  86. {
  87. case RTGUI_EVENT_PAINT:
  88. case RTGUI_EVENT_COMMAND:
  89. case RTGUI_EVENT_RESIZE:
  90. rtgui_container_dispatch_event(container, event);
  91. break;
  92. case RTGUI_EVENT_KBD:
  93. {
  94. /* let parent to handle keyboard event */
  95. if (widget->parent != RT_NULL && widget->parent != widget->toplevel)
  96. {
  97. return widget->parent->event_handler(widget->parent, event);
  98. }
  99. }
  100. break;
  101. case RTGUI_EVENT_MOUSE_BUTTON:
  102. /* handle in child widget */
  103. return rtgui_container_dispatch_mouse_event(container,
  104. (struct rtgui_event_mouse*)event);
  105. break;
  106. case RTGUI_EVENT_MOUSE_MOTION:
  107. return rtgui_container_dispatch_mouse_event(container,
  108. (struct rtgui_event_mouse*)event);
  109. break;
  110. default:
  111. /* call parent widget event handler */
  112. return rtgui_widget_event_handler(widget, event);
  113. }
  114. return RT_FALSE;
  115. }
  116. /*
  117. * This function will add a child to a container widget
  118. * Note: this function will not change the widget layout
  119. * the layout is the responsibility of layout widget, such as box.
  120. */
  121. void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child)
  122. {
  123. RT_ASSERT(container != RT_NULL);
  124. RT_ASSERT(child != RT_NULL);
  125. /* set parent and toplevel widget */
  126. child->parent = RTGUI_WIDGET(container);
  127. /* put widget to parent's children list */
  128. rtgui_list_append(&(container->children), &(child->sibling));
  129. /* update children toplevel */
  130. if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
  131. RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(container)->toplevel))
  132. {
  133. child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));
  134. /* update all child toplevel */
  135. if (RTGUI_IS_CONTAINER(child))
  136. {
  137. _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
  138. }
  139. }
  140. }
  141. /* remove a child to widget */
  142. void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child)
  143. {
  144. RT_ASSERT(container != RT_NULL);
  145. RT_ASSERT(child != RT_NULL);
  146. if (child == container->focused)
  147. {
  148. /* set focused to itself */
  149. container->focused = RTGUI_WIDGET(container);
  150. rtgui_widget_focus(RTGUI_WIDGET(container));
  151. }
  152. /* remove widget from parent's children list */
  153. rtgui_list_remove(&(container->children), &(child->sibling));
  154. /* set parent and toplevel widget */
  155. child->parent = RT_NULL;
  156. child->toplevel = RT_NULL;
  157. }
  158. /* destroy all children of container */
  159. void rtgui_container_destroy_children(rtgui_container_t *container)
  160. {
  161. struct rtgui_list_node* node;
  162. if (container == RT_NULL) return;
  163. node = container->children.next;
  164. while (node != RT_NULL)
  165. {
  166. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  167. if (RTGUI_IS_CONTAINER(child))
  168. {
  169. /* break parent firstly */
  170. child->parent = RT_NULL;
  171. /* destroy children of child */
  172. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  173. }
  174. /* remove widget from parent's children list */
  175. rtgui_list_remove(&(container->children), &(child->sibling));
  176. /* set parent and toplevel widget */
  177. child->parent = RT_NULL;
  178. /* destroy object and remove from parent */
  179. rtgui_object_destroy(RTGUI_OBJECT(child));
  180. node = container->children.next;
  181. }
  182. container->children.next = RT_NULL;
  183. container->focused = RTGUI_WIDGET(container);
  184. if (RTGUI_WIDGET(container)->parent != RT_NULL)
  185. rtgui_widget_focus(RTGUI_WIDGET(container));
  186. /* update widget clip */
  187. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel));
  188. }
  189. rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container)
  190. {
  191. rtgui_widget_t* child = RT_NULL;
  192. if (container->children.next != RT_NULL)
  193. {
  194. child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
  195. }
  196. return child;
  197. }