container.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * File : box.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/container.h>
  15. #include <rtgui/widgets/window.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(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 box as focusable widget */
  24. RTGUI_WIDGET_FLAG(container) |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  25. }
  26. static void _rtgui_container_destructor(rtgui_container_t *container)
  27. {
  28. /* destroy child of box */
  29. rtgui_container_destroy_children(container);
  30. }
  31. static void _rtgui_container_update_toplevel(rtgui_container_t* container)
  32. {
  33. rtgui_list_t* 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 top */
  38. child->toplevel = RTGUI_WIDGET(rtgui_widget_get_toplevel(container));
  39. if(RTGUI_IS_CONTAINER(child))
  40. {
  41. _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
  42. }
  43. }
  44. }
  45. rtgui_type_t *rtgui_container_type_get(void)
  46. {
  47. static rtgui_type_t *container_type = RT_NULL;
  48. if(!container_type)
  49. {
  50. container_type = rtgui_type_create("container", RTGUI_WIDGET_TYPE,
  51. sizeof(rtgui_container_t),
  52. RTGUI_CONSTRUCTOR(_rtgui_container_constructor),
  53. RTGUI_DESTRUCTOR(_rtgui_container_destructor));
  54. }
  55. return container_type;
  56. }
  57. rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event)
  58. {
  59. /* handle in child widget */
  60. rtgui_list_t* node;
  61. rtgui_list_foreach(node, &(container->children))
  62. {
  63. rtgui_widget_t* w;
  64. w = rtgui_list_entry(node, rtgui_widget_t, sibling);
  65. if(RTGUI_WIDGET_IS_HIDE(w)) continue; //控件是隐藏的则不绘制了
  66. //if(RTGUI_IS_WIN(w)) continue;
  67. if(RTGUI_WIDGET_EVENT_HANDLE(w) != RT_NULL)
  68. RTGUI_WIDGET_EVENT_CALL(w, event);
  69. }
  70. return RT_FALSE;
  71. }
  72. rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, rtgui_event_mouse_t* event)
  73. {
  74. /* handle in child widget */
  75. rtgui_list_t* node;
  76. rtgui_widget_t *focus;
  77. /* get focus widget on toplevel */
  78. focus = RTGUI_CONTAINER(RTGUI_WIDGET(container)->toplevel)->focused;
  79. rtgui_list_foreach(node, &(container->children))
  80. {
  81. rtgui_widget_t* w;
  82. w = rtgui_list_entry(node, rtgui_widget_t, sibling);
  83. if(RTGUI_WIDGET_IS_HIDE(w))continue;//隐藏的控件不处理
  84. if(rtgui_rect_contains_point(&(w->extent), event->x, event->y) == RT_EOK)
  85. {//检测到当前点在某个控件内
  86. if ((focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
  87. rtgui_widget_focus(w);
  88. if(RTGUI_WIDGET_EVENT_HANDLE(w) != RT_NULL)
  89. return RTGUI_WIDGET_EVENT_CALL(w,(rtgui_event_t*)event);
  90. }
  91. }
  92. return RT_FALSE;
  93. }
  94. rt_bool_t rtgui_container_event_handler(PVOID wdt, rtgui_event_t* event)
  95. {
  96. rtgui_widget_t *widget = (rtgui_widget_t*)wdt;
  97. rtgui_container_t *container = RTGUI_CONTAINER(widget);
  98. switch (event->type)
  99. {
  100. case RTGUI_EVENT_KBD:
  101. if(widget->on_key != RT_NULL)
  102. {
  103. return widget->on_key(widget, event);
  104. }
  105. else
  106. {
  107. /* let parent to handle keyboard event */
  108. if(widget->parent != RT_NULL && widget->parent != widget->toplevel)
  109. {
  110. if(RTGUI_WIDGET_EVENT_HANDLE(widget->parent) != RT_NULL)
  111. return RTGUI_WIDGET_EVENT_CALL(widget->parent, event);
  112. }
  113. }
  114. break;
  115. case RTGUI_EVENT_MOUSE_BUTTON:
  116. /* handle in child widget */
  117. if(rtgui_container_dispatch_mouse_event(container,(rtgui_event_mouse_t*)event) == RT_FALSE)
  118. {
  119. /* handle event in current widget */
  120. if(widget->on_mouseclick != RT_NULL)
  121. {
  122. return widget->on_mouseclick(widget, event);
  123. }
  124. }
  125. else return RT_TRUE;
  126. break;
  127. case RTGUI_EVENT_MOUSE_MOTION:
  128. if(rtgui_container_dispatch_mouse_event(container,(rtgui_event_mouse_t*)event) == RT_FALSE)
  129. {
  130. #if 0
  131. /* handle event in current widget */
  132. if(widget->on_mousemotion != RT_NULL)
  133. {
  134. return widget->on_mousemotion(widget, event);
  135. }
  136. #endif
  137. }
  138. else
  139. return RT_TRUE;
  140. break;
  141. case RTGUI_EVENT_TIMER:
  142. {
  143. rtgui_timer_t* timer;
  144. rtgui_event_timer_t* etimer = (rtgui_event_timer_t*) event;
  145. timer = etimer->timer;
  146. if(timer->timeout != RT_NULL)
  147. {
  148. /* call timeout function */
  149. timer->timeout(timer, timer->user_data);
  150. }
  151. }
  152. break;
  153. case RTGUI_EVENT_COMMAND:
  154. if(rtgui_container_dispatch_event(container, event) == RT_FALSE)
  155. {
  156. if(widget->on_command != RT_NULL)
  157. {
  158. return widget->on_command(widget, event);
  159. }
  160. }
  161. else
  162. return RT_TRUE;
  163. break;
  164. case RTGUI_EVENT_RESIZE:
  165. if(rtgui_container_dispatch_event(container, event) == RT_FALSE)
  166. {
  167. if(widget->on_size != RT_NULL)
  168. return widget->on_size(widget, event);
  169. }
  170. else return RT_TRUE;
  171. break;
  172. default:
  173. /* call parent widget event handler */
  174. return rtgui_widget_event_handler(widget, event);
  175. }
  176. return RT_FALSE;
  177. }
  178. /*
  179. * This function will add a child to a box widget
  180. * Note: this function will not change the widget layout
  181. * the layout is the responsibility of layout widget, such as box.
  182. */
  183. void rtgui_container_add_child(PVOID cbox, PVOID wdt)
  184. {
  185. rtgui_container_t *container = (rtgui_container_t*)cbox;
  186. rtgui_widget_t* child = (rtgui_widget_t*)wdt;
  187. RT_ASSERT(container != RT_NULL);
  188. RT_ASSERT(child != RT_NULL);
  189. RT_ASSERT(RTGUI_IS_CONTAINER(container));
  190. /* set parent and toplevel widget */
  191. child->parent = RTGUI_WIDGET(container);
  192. /* put widget to parent's child list */
  193. rtgui_list_append(&(container->children), &(child->sibling));
  194. /* update child toplevel */
  195. if(RTGUI_WIDGET(container)->toplevel != RT_NULL && RTGUI_IS_CONTAINER(RTGUI_WIDGET(container)->toplevel))
  196. {
  197. child->toplevel = RTGUI_WIDGET(rtgui_widget_get_toplevel(container));
  198. /* update all child toplevel */
  199. if(RTGUI_IS_CONTAINER(child))
  200. {
  201. _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
  202. }
  203. }
  204. rtgui_widget_update_clip(child);
  205. }
  206. /* remove a child to widget */
  207. void rtgui_container_remove_child(rtgui_container_t *container, PVOID wdt)
  208. {
  209. rtgui_widget_t *child = (rtgui_widget_t*)wdt;
  210. RT_ASSERT(container != RT_NULL);
  211. RT_ASSERT(child != RT_NULL);
  212. if(child == container->focused)
  213. {
  214. /* set focused to itself */
  215. container->focused = RTGUI_WIDGET(container);
  216. rtgui_widget_focus(container);
  217. }
  218. /* remove widget from parent's child list */
  219. rtgui_list_remove(&(container->children), &(child->sibling));
  220. /* set parent and top widget */
  221. child->parent = RT_NULL;
  222. child->toplevel = RT_NULL;
  223. rtgui_widget_update_clip(container);
  224. }
  225. /* destroy all child of box */
  226. void rtgui_container_destroy_children(rtgui_container_t *container)
  227. {
  228. rtgui_list_t* node;
  229. if(container == RT_NULL) return;
  230. node = container->children.next;
  231. while(node != RT_NULL)
  232. {
  233. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  234. if(RTGUI_IS_CONTAINER(child))
  235. {
  236. /* break parent firstly */
  237. child->parent = RT_NULL;
  238. /* destroy child of child */
  239. rtgui_container_destroy_children(RTGUI_CONTAINER(child));
  240. }
  241. /* remove widget from parent's child list */
  242. rtgui_list_remove(&(container->children), &(child->sibling));
  243. /* set parent and top widget */
  244. child->parent = RT_NULL;
  245. /* destroy object and remove from parent */
  246. rtgui_object_destroy(RTGUI_OBJECT(child));
  247. //node = box->child.next;
  248. node = node->next;
  249. }
  250. container->children.next = RT_NULL;
  251. container->focused = RTGUI_WIDGET(container);
  252. if(RTGUI_WIDGET_PARENT(container) != RT_NULL)
  253. rtgui_widget_focus(container);
  254. /* update widget clip */
  255. rtgui_widget_update_clip(RTGUI_WIDGET(container)->toplevel);
  256. }
  257. rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container)
  258. {
  259. rtgui_widget_t* child = RT_NULL;
  260. if(container->children.next != RT_NULL)
  261. {
  262. child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
  263. }
  264. return child;
  265. }