box.c 8.7 KB

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