box.c 6.8 KB

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