container.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_application.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 is used to 'contain'(show) widgets and dispatch events to
  27. * them, not interact with user. So no need to grab focus. If we did it,
  28. * some widget inherited from container(e.g. notebook) will grab the focus
  29. * annoyingly.
  30. *
  31. * For example, a focusable notebook N has a widget W. When the user press
  32. * W, N will gain the focus and W will lose it at first. Then N will set
  33. * focus to W because it is W that eventually interact with people. N will
  34. * yield focus and W will gain the focus again. This loop will make W
  35. * repaint twice every time user press it.
  36. *
  37. * Just eliminate it.
  38. */
  39. RTGUI_WIDGET(container)->flag &= ~RTGUI_WIDGET_FLAG_FOCUSABLE;
  40. }
  41. static void _rtgui_container_destructor(rtgui_container_t *container)
  42. {
  43. rtgui_container_destroy_children(container);
  44. }
  45. static void _rtgui_container_update_toplevel(rtgui_container_t* container)
  46. {
  47. struct rtgui_win *window;
  48. struct rtgui_list_node* node;
  49. window = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));
  50. rtgui_list_foreach(node, &(container->children))
  51. {
  52. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  53. /* set child toplevel */
  54. child->toplevel = window;
  55. if (RTGUI_IS_CONTAINER(child))
  56. {
  57. _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
  58. }
  59. }
  60. }
  61. DEFINE_CLASS_TYPE(container, "container",
  62. RTGUI_WIDGET_TYPE,
  63. _rtgui_container_constructor,
  64. _rtgui_container_destructor,
  65. sizeof(struct rtgui_container));
  66. rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event)
  67. {
  68. /* handle in child widget */
  69. struct rtgui_list_node* node;
  70. rtgui_list_foreach(node, &(container->children))
  71. {
  72. struct rtgui_widget* w;
  73. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  74. if (RTGUI_OBJECT(w)->event_handler &&
  75. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event) == RT_TRUE)
  76. return RT_TRUE;
  77. }
  78. return RT_FALSE;
  79. }
  80. rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse* event)
  81. {
  82. /* handle in child widget */
  83. struct rtgui_list_node* node;
  84. struct rtgui_widget *old_focus;
  85. old_focus = RTGUI_WIDGET(container)->toplevel->focused_widget;
  86. rtgui_list_foreach(node, &(container->children))
  87. {
  88. struct rtgui_widget* w;
  89. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  90. if (rtgui_rect_contains_point(&(w->extent),
  91. event->x, event->y) == RT_EOK)
  92. {
  93. if ((old_focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
  94. rtgui_widget_focus(w);
  95. if (RTGUI_OBJECT(w)->event_handler &&
  96. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w),
  97. (rtgui_event_t*)event) == RT_TRUE)
  98. return RT_TRUE;
  99. }
  100. }
  101. return RT_FALSE;
  102. }
  103. rt_bool_t rtgui_container_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  104. {
  105. struct rtgui_container *container;
  106. struct rtgui_widget *widget;
  107. RT_ASSERT(object != RT_NULL);
  108. RT_ASSERT(event != RT_NULL);
  109. container = RTGUI_CONTAINER(object);
  110. widget = RTGUI_WIDGET(object);
  111. switch (event->type)
  112. {
  113. case RTGUI_EVENT_PAINT:
  114. {
  115. struct rtgui_dc* dc;
  116. struct rtgui_rect rect;
  117. dc = rtgui_dc_begin_drawing(widget);
  118. if (dc == RT_NULL)
  119. return RT_FALSE;
  120. rtgui_widget_get_rect(widget, &rect);
  121. /* fill container with background */
  122. rtgui_dc_fill_rect(dc, &rect);
  123. /* paint on each child */
  124. rtgui_container_dispatch_event(container, event);
  125. rtgui_dc_end_drawing(dc);
  126. }
  127. break;
  128. case RTGUI_EVENT_KBD:
  129. /* let parent to handle keyboard event */
  130. if (widget->parent != RT_NULL &&
  131. widget->parent != RTGUI_WIDGET(widget->toplevel) &&
  132. RTGUI_OBJECT(widget->parent)->event_handler)
  133. {
  134. return RTGUI_OBJECT(widget->parent)->event_handler(
  135. RTGUI_OBJECT(widget->parent),
  136. event);
  137. }
  138. break;
  139. case RTGUI_EVENT_MOUSE_BUTTON:
  140. case RTGUI_EVENT_MOUSE_MOTION:
  141. /* handle in child widget */
  142. return rtgui_container_dispatch_mouse_event(container,
  143. (struct rtgui_event_mouse*)event);
  144. case RTGUI_EVENT_COMMAND:
  145. case RTGUI_EVENT_RESIZE:
  146. rtgui_container_dispatch_event(container, event);
  147. break;
  148. default:
  149. /* call parent widget event handler */
  150. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  151. }
  152. return RT_FALSE;
  153. }
  154. rtgui_container_t* rtgui_container_create(void)
  155. {
  156. struct rtgui_container* container;
  157. /* allocate container */
  158. container = (struct rtgui_container*) rtgui_widget_create (RTGUI_CONTAINER_TYPE);
  159. return container;
  160. }
  161. void rtgui_container_destroy(rtgui_container_t* container)
  162. {
  163. rtgui_container_hide(container);
  164. rtgui_widget_destroy(RTGUI_WIDGET(container));
  165. }
  166. /*
  167. * This function will add a child to a container widget
  168. * Note: this function will not change the widget layout
  169. * the layout is the responsibility of layout widget, such as box.
  170. */
  171. void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child)
  172. {
  173. RT_ASSERT(container != RT_NULL);
  174. RT_ASSERT(child != RT_NULL);
  175. /* set parent and toplevel widget */
  176. child->parent = RTGUI_WIDGET(container);
  177. /* put widget to parent's children list */
  178. rtgui_list_append(&(container->children), &(child->sibling));
  179. /* update children toplevel */
  180. if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
  181. RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(container)->toplevel))
  182. {
  183. child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));
  184. /* update all child toplevel */
  185. if (RTGUI_IS_CONTAINER(child))
  186. {
  187. _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
  188. }
  189. }
  190. }
  191. /* remove a child to widget */
  192. void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child)
  193. {
  194. RT_ASSERT(container != RT_NULL);
  195. RT_ASSERT(child != RT_NULL);
  196. rtgui_widget_unfocus(child);
  197. /* remove widget from parent's children list */
  198. rtgui_list_remove(&(container->children), &(child->sibling));
  199. /* set parent and toplevel widget */
  200. child->parent = RT_NULL;
  201. child->toplevel = RT_NULL;
  202. }
  203. /* destroy all children of container */
  204. void rtgui_container_destroy_children(rtgui_container_t *container)
  205. {
  206. struct rtgui_list_node* node;
  207. if (container == RT_NULL)
  208. return;
  209. node = container->children.next;
  210. while (node != RT_NULL)
  211. {
  212. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  213. if (RTGUI_IS_CONTAINER(child))
  214. {
  215. /* break parent firstly */
  216. child->parent = RT_NULL;
  217. /* destroy children of child */
  218. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  219. }
  220. /* remove widget from parent's children list */
  221. rtgui_list_remove(&(container->children), &(child->sibling));
  222. /* set parent and toplevel widget */
  223. child->parent = RT_NULL;
  224. /* destroy object and remove from parent */
  225. rtgui_object_destroy(RTGUI_OBJECT(child));
  226. node = container->children.next;
  227. }
  228. container->children.next = RT_NULL;
  229. /* update widget clip */
  230. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel));
  231. }
  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. #ifndef RTGUI_USING_SMALL_SIZE
  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. rtgui_container_add_child(RTGUI_CONTAINER(container), RTGUI_WIDGET(box));
  248. rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(container)->extent));
  249. }
  250. #endif
  251. void rtgui_container_hide(rtgui_container_t* container)
  252. {
  253. if (container == RT_NULL)
  254. return;
  255. if (RTGUI_WIDGET(container)->parent == RT_NULL)
  256. {
  257. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(container));
  258. return;
  259. }
  260. }