1
0

box.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_OBJECT_TYPE,
  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 = 0;
  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) space_count ++;
  65. else total_height += widget->mini_height;
  66. }
  67. /* calculate the height for each spaces */
  68. if (space_count != 0)
  69. {
  70. space_height = (rtgui_rect_height(*extent) - total_height - (box->border_size << 1)) / space_count;
  71. }
  72. /* init (x, y) and box width */
  73. next_x = extent->x1 + box->border_size;
  74. next_y = extent->y1 + box->border_size;
  75. box_width = rtgui_rect_width(*extent) - (box->border_size << 1);
  76. /* layout each widget */
  77. rtgui_list_foreach(node, &(box->container->children))
  78. {
  79. struct rtgui_rect *rect;
  80. rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  81. /* get extent of widget */
  82. rect = &(widget->extent);
  83. /* reset rect */
  84. rtgui_rect_moveto(rect, -rect->x1, -rect->y1);
  85. rect->x2 = widget->mini_width;
  86. rect->y2 = widget->mini_height;
  87. /* left in default */
  88. rtgui_rect_moveto(rect, next_x, next_y);
  89. if (widget->align & RTGUI_ALIGN_EXPAND)
  90. {
  91. /* expand on horizontal */
  92. rect->x2 = rect->x1 + (rt_int16_t)box_width;
  93. }
  94. if (widget->align & RTGUI_ALIGN_CENTER_VERTICAL)
  95. {
  96. /* center */
  97. rt_uint32_t mid;
  98. mid = box_width - rtgui_rect_width(*rect);
  99. mid = mid / 2;
  100. rect->x1 = next_x + mid;
  101. rect->x2 = next_x + box_width - mid;
  102. }
  103. else if (widget->align & RTGUI_ALIGN_RIGHT)
  104. {
  105. /* right */
  106. rect->x1 = next_x + box_width - rtgui_rect_width(*rect);
  107. rect->x2 = next_x + box_width;
  108. }
  109. if (widget->align & RTGUI_ALIGN_STRETCH)
  110. {
  111. rect->y2 = rect->y1 + space_height;
  112. }
  113. /* process resize event */
  114. size_event.x = rect->x1;
  115. size_event.y = rect->y1;
  116. size_event.w = rect->x2 - rect->x1;
  117. size_event.h = rect->y2 - rect->y1;
  118. RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget),
  119. &size_event.parent);
  120. /* point to next height */
  121. next_y = rect->y2 + box->border_size;
  122. }
  123. }
  124. static void rtgui_box_layout_horizontal(struct rtgui_box *box, struct rtgui_rect *extent)
  125. {
  126. rtgui_list_t *node;
  127. rt_int32_t box_height;
  128. rt_int32_t space_count;
  129. rt_int32_t next_x, next_y;
  130. rt_int32_t total_width, space_width;
  131. struct rtgui_event_resize size_event;
  132. /* prepare the resize event */
  133. RTGUI_EVENT_RESIZE_INIT(&size_event);
  134. /* find spaces */
  135. space_count = 0;
  136. total_width = 0;
  137. space_width = 0;
  138. rtgui_list_foreach(node, &(box->container->children))
  139. {
  140. rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  141. if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++;
  142. else total_width += widget->mini_width;
  143. }
  144. if (space_count != 0)
  145. {
  146. /* calculate the height for each spaces */
  147. space_width = (rtgui_rect_width(*extent) - total_width) / space_count;
  148. }
  149. /* init (x, y) and box height */
  150. next_x = extent->x1 + box->border_size;
  151. next_y = extent->y1 + box->border_size;
  152. box_height = rtgui_rect_height(*extent) - (box->border_size << 1);
  153. /* layout each widget */
  154. rtgui_list_foreach(node, &(box->container->children))
  155. {
  156. rtgui_rect_t *rect;
  157. rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  158. /* get extent of widget */
  159. rect = &(widget->extent);
  160. /* reset rect */
  161. rtgui_rect_moveto(rect, -rect->x1, -rect->y1);
  162. rect->x2 = widget->mini_width;
  163. rect->y2 = widget->mini_height;
  164. /* top in default */
  165. rtgui_rect_moveto(rect, next_x, next_y);
  166. if (widget->align & RTGUI_ALIGN_EXPAND)
  167. {
  168. /* expand on vertical */
  169. rect->y2 = rect->y1 + box_height;
  170. }
  171. if (widget->align & RTGUI_ALIGN_CENTER_HORIZONTAL)
  172. {
  173. /* center */
  174. rt_uint32_t mid;
  175. mid = box_height - rtgui_rect_height(*rect);
  176. mid = mid / 2;
  177. rect->y1 = next_y + mid;
  178. rect->y2 = next_y + box_height - mid;
  179. }
  180. else if (widget->align & RTGUI_ALIGN_RIGHT)
  181. {
  182. /* right */
  183. rect->y1 = next_y + box_height - rtgui_rect_height(*rect);
  184. rect->y2 = next_y + box_height;
  185. }
  186. if (widget->align & RTGUI_ALIGN_STRETCH)
  187. {
  188. rect->x2 = rect->x1 + space_width;
  189. }
  190. /* process resize event */
  191. size_event.x = rect->x1;
  192. size_event.y = rect->y1;
  193. size_event.w = rect->x2 - rect->x1;
  194. size_event.h = rect->y2 - rect->y1;
  195. RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget),
  196. &size_event.parent);
  197. /* point to next width */
  198. next_x = rect->x2 + box->border_size;
  199. }
  200. }
  201. void rtgui_box_layout(rtgui_box_t *box)
  202. {
  203. struct rtgui_rect extent;
  204. RT_ASSERT(box != RT_NULL);
  205. if (box->container == RT_NULL) return;
  206. rtgui_widget_get_extent(RTGUI_WIDGET(box->container), &extent);
  207. if (box->orient & RTGUI_VERTICAL)
  208. {
  209. rtgui_box_layout_vertical(box, &extent);
  210. }
  211. else
  212. {
  213. rtgui_box_layout_horizontal(box, &extent);
  214. }
  215. /* update box and its children clip */
  216. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box->container)))
  217. {
  218. rtgui_widget_update_clip(RTGUI_WIDGET(box->container));
  219. }
  220. }
  221. RTM_EXPORT(rtgui_box_layout);
  222. void rtgui_box_layout_rect(rtgui_box_t *box, struct rtgui_rect *rect)
  223. {
  224. RT_ASSERT(box != RT_NULL);
  225. if (box->container == RT_NULL) return;
  226. if (box->orient & RTGUI_VERTICAL)
  227. {
  228. rtgui_box_layout_vertical(box, rect);
  229. }
  230. else
  231. {
  232. rtgui_box_layout_horizontal(box, rect);
  233. }
  234. /* update box and its children clip */
  235. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box->container)))
  236. {
  237. rtgui_widget_update_clip(RTGUI_WIDGET(box->container));
  238. }
  239. }
  240. RTM_EXPORT(rtgui_box_layout_rect);