box.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/dc.h>
  15. #include <rtgui/widgets/box.h>
  16. static void _rtgui_box_constructor(rtgui_box_t *box)
  17. {
  18. /* init widget and set event handler */
  19. rtgui_object_set_event_handler(RTGUI_OBJECT(box), RT_NULL);
  20. /* set proper of control */
  21. box->orient = RTGUI_HORIZONTAL;
  22. box->border_size = RTGUI_BORDER_DEFAULT_WIDTH;
  23. box->container = RT_NULL;
  24. }
  25. DEFINE_CLASS_TYPE(box, "box",
  26. RTGUI_PARENT_TYPE(object),
  27. _rtgui_box_constructor,
  28. RT_NULL,
  29. sizeof(struct rtgui_box));
  30. struct rtgui_box *rtgui_box_create(int orientation, int border_size)
  31. {
  32. struct rtgui_box *box;
  33. box = (struct rtgui_box *) rtgui_object_create(RTGUI_BOX_TYPE);
  34. if (box != RT_NULL)
  35. {
  36. box->orient = orientation;
  37. box->border_size = border_size;
  38. }
  39. return box;
  40. }
  41. RTM_EXPORT(rtgui_box_create);
  42. void rtgui_box_destroy(struct rtgui_box *box)
  43. {
  44. rtgui_object_destroy(RTGUI_OBJECT(box));
  45. }
  46. RTM_EXPORT(rtgui_box_destroy);
  47. static void rtgui_box_layout_vertical(struct rtgui_box *box, struct rtgui_rect *extent)
  48. {
  49. rtgui_list_t *node;
  50. rt_int32_t box_width;
  51. rt_int32_t space_count;
  52. rt_int32_t next_x, next_y;
  53. rt_int32_t total_height, space_height;
  54. struct rtgui_event_resize size_event;
  55. /* prepare the resize event */
  56. RTGUI_EVENT_RESIZE_INIT(&size_event);
  57. /* find spaces */
  58. space_count = 0;
  59. total_height = box->border_size;
  60. space_height = 0;
  61. rtgui_list_foreach(node, &(box->container->children))
  62. {
  63. rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  64. if (widget->align & RTGUI_ALIGN_STRETCH)
  65. {
  66. space_count ++;
  67. }
  68. else
  69. {
  70. total_height += widget->min_height;
  71. }
  72. total_height += box->border_size;
  73. }
  74. /* calculate the height for each spaces */
  75. if (space_count != 0 && rtgui_rect_height(*extent) > total_height)
  76. {
  77. space_height = (rtgui_rect_height(*extent) - total_height) / space_count;
  78. }
  79. /* init (x, y) and box width */
  80. next_x = extent->x1 + box->border_size;
  81. next_y = extent->y1 + box->border_size;
  82. box_width = rtgui_rect_width(*extent) - box->border_size * 2;
  83. /* layout each widget */
  84. rtgui_list_foreach(node, &(box->container->children))
  85. {
  86. struct rtgui_rect *rect;
  87. rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  88. /* get extent of widget */
  89. rect = &(widget->extent);
  90. /* reset rect */
  91. rtgui_rect_init(rect, 0, 0, widget->min_width, widget->min_height);
  92. /* left in default */
  93. rtgui_rect_moveto(rect, next_x, next_y);
  94. if (widget->align & RTGUI_ALIGN_EXPAND)
  95. {
  96. /* expand on horizontal */
  97. rect->x2 = rect->x1 + (rt_int16_t)box_width;
  98. }
  99. if (widget->align & RTGUI_ALIGN_CENTER_VERTICAL)
  100. {
  101. /* center */
  102. rt_uint32_t mid;
  103. mid = box_width - rtgui_rect_width(*rect);
  104. mid = mid / 2;
  105. rect->x1 = next_x + mid;
  106. rect->x2 = next_x + box_width - mid;
  107. }
  108. else if (widget->align & RTGUI_ALIGN_RIGHT)
  109. {
  110. /* right */
  111. rect->x1 = next_x + box_width - rtgui_rect_width(*rect);
  112. rect->x2 = next_x + box_width;
  113. }
  114. if (widget->align & RTGUI_ALIGN_STRETCH)
  115. {
  116. rect->y2 = rect->y1 + space_height;
  117. }
  118. /* process resize event */
  119. size_event.x = rect->x1;
  120. size_event.y = rect->y1;
  121. size_event.w = rect->x2 - rect->x1;
  122. size_event.h = rect->y2 - rect->y1;
  123. RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget),
  124. &size_event.parent);
  125. /* point to next height */
  126. next_y = rect->y2 + box->border_size;
  127. }
  128. }
  129. static void rtgui_box_layout_horizontal(struct rtgui_box *box, struct rtgui_rect *extent)
  130. {
  131. rtgui_list_t *node;
  132. rt_int32_t box_height;
  133. rt_int32_t space_count;
  134. rt_int32_t next_x, next_y;
  135. rt_int32_t total_width, space_width;
  136. struct rtgui_event_resize size_event;
  137. /* prepare the resize event */
  138. RTGUI_EVENT_RESIZE_INIT(&size_event);
  139. /* find spaces */
  140. space_count = 0;
  141. total_width = 0;
  142. space_width = 0;
  143. rtgui_list_foreach(node, &(box->container->children))
  144. {
  145. rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  146. if (widget->align & RTGUI_ALIGN_STRETCH)
  147. {
  148. space_count ++;
  149. }
  150. else
  151. {
  152. total_width += widget->min_width;
  153. }
  154. total_width += box->border_size;
  155. }
  156. if (space_count != 0)
  157. {
  158. /* calculate the height for each spaces */
  159. space_width = (rtgui_rect_width(*extent) - total_width) / space_count;
  160. }
  161. /* init (x, y) and box height */
  162. next_x = extent->x1 + box->border_size;
  163. next_y = extent->y1 + box->border_size;
  164. box_height = rtgui_rect_height(*extent) - (box->border_size << 1);
  165. /* layout each widget */
  166. rtgui_list_foreach(node, &(box->container->children))
  167. {
  168. rtgui_rect_t *rect;
  169. rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  170. /* get extent of widget */
  171. rect = &(widget->extent);
  172. /* reset rect */
  173. rtgui_rect_moveto(rect, -rect->x1, -rect->y1);
  174. rect->x2 = widget->min_width;
  175. rect->y2 = widget->min_height;
  176. /* top in default */
  177. rtgui_rect_moveto(rect, next_x, next_y);
  178. if (widget->align & RTGUI_ALIGN_EXPAND)
  179. {
  180. /* expand on vertical */
  181. rect->y2 = rect->y1 + box_height;
  182. }
  183. if (widget->align & RTGUI_ALIGN_CENTER_HORIZONTAL)
  184. {
  185. /* center */
  186. rt_uint32_t mid;
  187. mid = box_height - rtgui_rect_height(*rect);
  188. mid = mid / 2;
  189. rect->y1 = next_y + mid;
  190. rect->y2 = next_y + box_height - mid;
  191. }
  192. else if (widget->align & RTGUI_ALIGN_RIGHT)
  193. {
  194. /* right */
  195. rect->y1 = next_y + box_height - rtgui_rect_height(*rect);
  196. rect->y2 = next_y + box_height;
  197. }
  198. if (widget->align & RTGUI_ALIGN_STRETCH)
  199. {
  200. rect->x2 = rect->x1 + space_width;
  201. }
  202. /* process resize event */
  203. size_event.x = rect->x1;
  204. size_event.y = rect->y1;
  205. size_event.w = rect->x2 - rect->x1;
  206. size_event.h = rect->y2 - rect->y1;
  207. RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget),
  208. &size_event.parent);
  209. /* point to next width */
  210. next_x = rect->x2 + box->border_size;
  211. }
  212. }
  213. void rtgui_box_layout(rtgui_box_t *box)
  214. {
  215. struct rtgui_rect extent;
  216. RT_ASSERT(box != RT_NULL);
  217. if (box->container == RT_NULL) return;
  218. rtgui_widget_get_extent(RTGUI_WIDGET(box->container), &extent);
  219. if (box->orient & RTGUI_VERTICAL)
  220. {
  221. rtgui_box_layout_vertical(box, &extent);
  222. }
  223. else
  224. {
  225. rtgui_box_layout_horizontal(box, &extent);
  226. }
  227. /* update box and its children clip */
  228. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box->container)))
  229. {
  230. rtgui_widget_update_clip(RTGUI_WIDGET(box->container));
  231. }
  232. }
  233. RTM_EXPORT(rtgui_box_layout);
  234. void rtgui_box_layout_rect(rtgui_box_t *box, struct rtgui_rect *rect)
  235. {
  236. RT_ASSERT(box != RT_NULL);
  237. if (box->container == RT_NULL) return;
  238. if (box->orient & RTGUI_VERTICAL)
  239. {
  240. rtgui_box_layout_vertical(box, rect);
  241. }
  242. else
  243. {
  244. rtgui_box_layout_horizontal(box, rect);
  245. }
  246. /* update box and its children clip */
  247. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box->container)))
  248. {
  249. rtgui_widget_update_clip(RTGUI_WIDGET(box->container));
  250. }
  251. }
  252. RTM_EXPORT(rtgui_box_layout_rect);