box.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. static void rtgui_box_layout_vertical(rtgui_box_t* box)
  42. {
  43. rtgui_list_t *node;
  44. rt_int32_t box_width;
  45. rt_int32_t space_count;
  46. rt_int32_t next_x, next_y;
  47. rt_int32_t total_height, space_height;
  48. struct rtgui_event_resize size_event;
  49. struct rtgui_widget *container_widget;
  50. /* prepare the resize event */
  51. RTGUI_EVENT_RESIZE_INIT(&size_event);
  52. container_widget = RTGUI_WIDGET(box->container);
  53. /* find spaces */
  54. space_count = 0;
  55. total_height = 0;
  56. space_height = 0;
  57. rtgui_list_foreach(node, &(box->container->children))
  58. {
  59. rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  60. if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++;
  61. else total_height += widget->mini_height;
  62. }
  63. /* calculate the height for each spaces */
  64. if (space_count != 0)
  65. {
  66. space_height = (rtgui_rect_height(container_widget->extent) - total_height - (box->border_size << 1)) / space_count;
  67. }
  68. /* init (x, y) and box width */
  69. next_x = container_widget->extent.x1 + box->border_size;
  70. next_y = container_widget->extent.y1 + box->border_size;
  71. box_width = rtgui_rect_width(container_widget->extent) - (box->border_size << 1);
  72. /* layout each widget */
  73. rtgui_list_foreach(node, &(box->container->children))
  74. {
  75. rtgui_rect_t *rect;
  76. rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
  77. /* get extent of widget */
  78. rect = &(widget->extent);
  79. /* reset rect */
  80. rtgui_rect_moveto(rect, -rect->x1, -rect->y1);
  81. rect->x2 = widget->mini_width;
  82. rect->y2 = widget->mini_height;
  83. /* left in default */
  84. rtgui_rect_moveto(rect, next_x, next_y);
  85. if (widget->align & RTGUI_ALIGN_EXPAND)
  86. {
  87. /* expand on horizontal */
  88. rect->x2 = rect->x1 + (rt_int16_t)box_width;
  89. }
  90. if (widget->align & RTGUI_ALIGN_CENTER_VERTICAL)
  91. {
  92. /* center */
  93. rt_uint32_t mid;
  94. mid = box_width - rtgui_rect_width(*rect);
  95. mid = mid /2;
  96. rect->x1 = next_x + mid;
  97. rect->x2 = next_x + box_width - mid;
  98. }
  99. else if (widget->align & RTGUI_ALIGN_RIGHT)
  100. {
  101. /* right */
  102. rect->x1 = next_x + box_width - rtgui_rect_width(*rect);
  103. rect->x2 = next_x + box_width;
  104. }
  105. if (widget->align & RTGUI_ALIGN_STRETCH)
  106. {
  107. rect->y2 = rect->y1 + space_height;
  108. }
  109. /* process resize event */
  110. size_event.x = rect->x1;
  111. size_event.y = rect->y1;
  112. size_event.w = rect->x2 - rect->x1;
  113. size_event.h = rect->y2 - rect->y1;
  114. RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget),
  115. &size_event.parent);
  116. /* point to next height */
  117. next_y = rect->y2 + box->border_size;
  118. }
  119. }
  120. static void rtgui_box_layout_horizontal(rtgui_box_t* box)
  121. {
  122. rtgui_list_t *node;
  123. rt_int32_t box_height;
  124. rt_int32_t space_count;
  125. rt_int32_t next_x, next_y;
  126. rt_int32_t total_width, space_width;
  127. struct rtgui_event_resize size_event;
  128. struct rtgui_widget *container_widget;
  129. /* prepare the resize event */
  130. RTGUI_EVENT_RESIZE_INIT(&size_event);
  131. container_widget = RTGUI_WIDGET(box->container);
  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(container_widget->extent) - total_width) / space_count;
  146. }
  147. /* init (x, y) and box height */
  148. next_x = container_widget->extent.x1 + box->border_size;
  149. next_y = container_widget->extent.y1 + box->border_size;
  150. box_height = rtgui_rect_height(container_widget->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. RT_ASSERT(box != RT_NULL);
  202. if (box->container == RT_NULL) return;
  203. if (box->orient & RTGUI_VERTICAL)
  204. {
  205. rtgui_box_layout_vertical(box);
  206. }
  207. else
  208. {
  209. rtgui_box_layout_horizontal(box);
  210. }
  211. /* update box and its children clip */
  212. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box->container)))
  213. {
  214. rtgui_widget_update_clip(RTGUI_WIDGET(box->container));
  215. }
  216. }