container.c 8.2 KB

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