container.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * File : container.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-16 Bernard first version
  23. * 2010-09-24 Bernard fix container destroy issue
  24. */
  25. #include <rtgui/dc.h>
  26. #include <rtgui/rtgui_system.h>
  27. #include <rtgui/rtgui_app.h>
  28. #include <rtgui/widgets/container.h>
  29. #include <rtgui/widgets/window.h>
  30. static void _rtgui_container_constructor(rtgui_container_t *container)
  31. {
  32. /* init container */
  33. rtgui_object_set_event_handler(RTGUI_OBJECT(container),
  34. rtgui_container_event_handler);
  35. rtgui_list_init(&(container->children));
  36. container->layout_box = RT_NULL;
  37. RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  38. }
  39. static void _rtgui_container_destructor(rtgui_container_t *container)
  40. {
  41. rtgui_container_destroy_children(container);
  42. if (container->layout_box != RT_NULL)
  43. rtgui_object_destroy(RTGUI_OBJECT(container->layout_box));
  44. }
  45. DEFINE_CLASS_TYPE(container, "container",
  46. RTGUI_PARENT_TYPE(widget),
  47. _rtgui_container_constructor,
  48. _rtgui_container_destructor,
  49. sizeof(struct rtgui_container));
  50. RTM_EXPORT(_rtgui_container);
  51. rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t *event)
  52. {
  53. /* handle in child widget */
  54. struct rtgui_list_node *node;
  55. rtgui_event_t _event, save_event = *event;
  56. rtgui_widget_t *widget = (rtgui_widget_t *)container;
  57. rtgui_list_foreach(node, &(container->children))
  58. {
  59. struct rtgui_widget *w;
  60. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  61. _event = save_event;
  62. if (RTGUI_EVENT_PAINT & _event.type)
  63. {
  64. if (widget->extent.x1 > w->extent.x2)
  65. {
  66. _event.type &= !RTGUI_EVENT_PAINT;
  67. }
  68. else if (widget->extent.x2 < w->extent.x1)
  69. {
  70. _event.type &= !RTGUI_EVENT_PAINT;
  71. }
  72. else if (widget->extent.y1 > w->extent.y2)
  73. {
  74. _event.type &= !RTGUI_EVENT_PAINT;
  75. }
  76. else if (widget->extent.y2 < w->extent.y1)
  77. {
  78. _event.type &= !RTGUI_EVENT_PAINT;
  79. }
  80. }
  81. if (RTGUI_OBJECT(w)->event_handler &&
  82. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), &_event) == RT_TRUE)
  83. {
  84. return RT_TRUE;
  85. }
  86. }
  87. return RT_FALSE;
  88. }
  89. RTM_EXPORT(rtgui_container_dispatch_event);
  90. /* broadcast means that the return value of event handlers will be ignored. The
  91. * events will always reach every child.*/
  92. rt_bool_t rtgui_container_broadcast_event(struct rtgui_container *container, struct rtgui_event *event)
  93. {
  94. struct rtgui_list_node *node;
  95. rtgui_list_foreach(node, &(container->children))
  96. {
  97. struct rtgui_widget *w;
  98. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  99. if (RTGUI_OBJECT(w)->event_handler)
  100. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event);
  101. }
  102. return RT_FALSE;
  103. }
  104. RTM_EXPORT(rtgui_container_broadcast_event);
  105. rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse *event)
  106. {
  107. /* handle in child widget */
  108. struct rtgui_list_node *node;
  109. struct rtgui_widget *old_focus;
  110. old_focus = RTGUI_WIDGET(container)->toplevel->focused_widget;
  111. rtgui_list_foreach(node, &(container->children))
  112. {
  113. struct rtgui_widget *w;
  114. w = rtgui_list_entry(node, struct rtgui_widget, sibling);
  115. if (rtgui_rect_contains_point(&(w->extent),
  116. event->x, event->y) == RT_EOK)
  117. {
  118. if ((old_focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
  119. rtgui_widget_focus(w);
  120. if (RTGUI_OBJECT(w)->event_handler &&
  121. RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w),
  122. (rtgui_event_t *)event) == RT_TRUE)
  123. return RT_TRUE;
  124. }
  125. }
  126. return RT_FALSE;
  127. }
  128. RTM_EXPORT(rtgui_container_dispatch_mouse_event);
  129. rt_bool_t rtgui_container_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  130. {
  131. struct rtgui_container *container;
  132. struct rtgui_widget *widget;
  133. RT_ASSERT(object != RT_NULL);
  134. RT_ASSERT(event != RT_NULL);
  135. container = RTGUI_CONTAINER(object);
  136. widget = RTGUI_WIDGET(object);
  137. switch (event->type)
  138. {
  139. case RTGUI_EVENT_PAINT:
  140. {
  141. struct rtgui_dc *dc;
  142. struct rtgui_rect rect;
  143. dc = rtgui_dc_begin_drawing(widget);
  144. if (dc == RT_NULL)
  145. return RT_FALSE;
  146. rtgui_widget_get_rect(widget, &rect);
  147. /* fill container with background */
  148. rtgui_dc_fill_rect(dc, &rect);
  149. /* paint on each child */
  150. rtgui_container_dispatch_event(container, event);
  151. rtgui_dc_end_drawing(dc, 1);
  152. }
  153. break;
  154. case RTGUI_EVENT_KBD:
  155. break;
  156. case RTGUI_EVENT_MOUSE_BUTTON:
  157. case RTGUI_EVENT_MOUSE_MOTION:
  158. /* handle in child widget */
  159. return rtgui_container_dispatch_mouse_event(container,
  160. (struct rtgui_event_mouse *)event);
  161. case RTGUI_EVENT_SHOW:
  162. rtgui_widget_onshow(RTGUI_OBJECT(container), event);
  163. rtgui_container_dispatch_event(container, event);
  164. break;
  165. case RTGUI_EVENT_HIDE:
  166. rtgui_container_dispatch_event(container, event);
  167. rtgui_widget_onhide(RTGUI_OBJECT(container), event);
  168. break;
  169. case RTGUI_EVENT_COMMAND:
  170. rtgui_container_dispatch_event(container, event);
  171. break;
  172. case RTGUI_EVENT_UPDATE_TOPLVL:
  173. /* call parent handler */
  174. rtgui_widget_onupdate_toplvl(object, event);
  175. /* update the children */
  176. rtgui_container_broadcast_event(container, event);
  177. break;
  178. case RTGUI_EVENT_RESIZE:
  179. /* re-layout container */
  180. rtgui_container_layout(container);
  181. break;
  182. default:
  183. /* call parent widget event handler */
  184. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  185. }
  186. return RT_FALSE;
  187. }
  188. RTM_EXPORT(rtgui_container_event_handler);
  189. rtgui_container_t *rtgui_container_create(void)
  190. {
  191. struct rtgui_container *container;
  192. /* allocate container */
  193. container = (struct rtgui_container *) rtgui_widget_create(RTGUI_CONTAINER_TYPE);
  194. return container;
  195. }
  196. RTM_EXPORT(rtgui_container_create);
  197. void rtgui_container_destroy(rtgui_container_t *container)
  198. {
  199. rtgui_widget_destroy(RTGUI_WIDGET(container));
  200. }
  201. RTM_EXPORT(rtgui_container_destroy);
  202. /*
  203. * This function will add a child to a container widget
  204. * Note: this function will not change the widget layout
  205. * the layout is the responsibility of layout widget, such as box.
  206. */
  207. void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t *child)
  208. {
  209. RT_ASSERT(container != RT_NULL);
  210. RT_ASSERT(child != RT_NULL);
  211. if (child->parent == RTGUI_WIDGET(container))
  212. return;
  213. RT_ASSERT(child->parent == RT_NULL);
  214. /* set parent and toplevel widget */
  215. child->parent = RTGUI_WIDGET(container);
  216. /* put widget to parent's children list */
  217. rtgui_list_append(&(container->children), &(child->sibling));
  218. /* update children toplevel */
  219. if (RTGUI_WIDGET(container)->toplevel != RT_NULL)
  220. {
  221. struct rtgui_win *toplevel;
  222. struct rtgui_event_update_toplvl eup;
  223. RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup);
  224. eup.toplvl = RTGUI_WIDGET(container)->toplevel;
  225. rtgui_object_handle(RTGUI_OBJECT(container), &eup.parent);
  226. /* update window clip */
  227. toplevel = RTGUI_WIDGET(container)->toplevel;
  228. if ((toplevel->flag & RTGUI_WIN_FLAG_CONNECTED) &&
  229. (RTGUI_WIDGET(toplevel)->flag & RTGUI_WIDGET_FLAG_SHOWN))
  230. {
  231. rtgui_win_update_clip(RTGUI_WIN(RTGUI_WIDGET(container)->toplevel));
  232. }
  233. }
  234. }
  235. RTM_EXPORT(rtgui_container_add_child);
  236. /* remove a child to widget */
  237. void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t *child)
  238. {
  239. RT_ASSERT(container != RT_NULL);
  240. RT_ASSERT(child != RT_NULL);
  241. rtgui_widget_unfocus(child);
  242. /* remove widget from parent's children list */
  243. rtgui_list_remove(&(container->children), &(child->sibling));
  244. /* set parent and toplevel widget */
  245. child->parent = RT_NULL;
  246. child->toplevel = RT_NULL;
  247. /* update window clip */
  248. if (RTGUI_WIDGET(container)->toplevel)
  249. {
  250. rtgui_win_update_clip(RTGUI_WIN(RTGUI_WIDGET(container)->toplevel));
  251. }
  252. }
  253. RTM_EXPORT(rtgui_container_remove_child);
  254. /* destroy all children of container */
  255. void rtgui_container_destroy_children(rtgui_container_t *container)
  256. {
  257. struct rtgui_list_node *node;
  258. if (container == RT_NULL)
  259. return;
  260. node = container->children.next;
  261. while (node != RT_NULL)
  262. {
  263. rtgui_widget_t *child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  264. if (RTGUI_IS_CONTAINER(child))
  265. {
  266. /* break parent firstly */
  267. child->parent = RT_NULL;
  268. /* destroy children of child */
  269. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  270. }
  271. /* remove widget from parent's children list */
  272. rtgui_list_remove(&(container->children), &(child->sibling));
  273. /* set parent and toplevel widget */
  274. child->parent = RT_NULL;
  275. /* destroy object and remove from parent */
  276. rtgui_object_destroy(RTGUI_OBJECT(child));
  277. node = container->children.next;
  278. }
  279. container->children.next = RT_NULL;
  280. /* update widget clip */
  281. rtgui_win_update_clip(RTGUI_WIN(RTGUI_WIDGET(container)->toplevel));
  282. }
  283. RTM_EXPORT(rtgui_container_destroy_children);
  284. rtgui_widget_t *rtgui_container_get_first_child(rtgui_container_t *container)
  285. {
  286. rtgui_widget_t *child = RT_NULL;
  287. RT_ASSERT(container != RT_NULL);
  288. if (container->children.next != RT_NULL)
  289. {
  290. child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
  291. }
  292. return child;
  293. }
  294. RTM_EXPORT(rtgui_container_get_first_child);
  295. void rtgui_container_set_box(rtgui_container_t *container, struct rtgui_box *box)
  296. {
  297. if (container == RT_NULL || box == RT_NULL)
  298. return;
  299. container->layout_box = box;
  300. box->container = container;
  301. }
  302. RTM_EXPORT(rtgui_container_set_box);
  303. void rtgui_container_layout(struct rtgui_container *container)
  304. {
  305. if (container == RT_NULL || container->layout_box == RT_NULL)
  306. return;
  307. rtgui_box_layout(container->layout_box);
  308. }
  309. RTM_EXPORT(rtgui_container_layout);
  310. struct rtgui_object* rtgui_container_get_object(struct rtgui_container *container,
  311. rt_uint32_t id)
  312. {
  313. struct rtgui_list_node *node;
  314. rtgui_list_foreach(node, &(container->children))
  315. {
  316. struct rtgui_object *o;
  317. o = RTGUI_OBJECT(rtgui_list_entry(node, struct rtgui_widget, sibling));
  318. if (o->id == id)
  319. return o;
  320. if (RTGUI_IS_CONTAINER(o))
  321. {
  322. struct rtgui_object *obj;
  323. obj = rtgui_container_get_object(RTGUI_CONTAINER(o), id);
  324. if (obj)
  325. return obj;
  326. }
  327. }
  328. return RT_NULL;
  329. }
  330. RTM_EXPORT(rtgui_container_get_object);