gui_box.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* gui_box.c - GUI container that stack components. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB 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 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/mm.h>
  20. #include <grub/misc.h>
  21. #include <grub/gui.h>
  22. #include <grub/gui_string_util.h>
  23. struct component_node
  24. {
  25. grub_gui_component_t component;
  26. struct component_node *next;
  27. struct component_node *prev;
  28. };
  29. typedef struct grub_gui_box *grub_gui_box_t;
  30. typedef void (*layout_func_t) (grub_gui_box_t self, int modify_layout,
  31. unsigned *minimal_width,
  32. unsigned *minimal_height);
  33. struct grub_gui_box
  34. {
  35. struct grub_gui_container container;
  36. grub_gui_container_t parent;
  37. grub_video_rect_t bounds;
  38. char *id;
  39. /* Doubly linked list of components with dummy head & tail nodes. */
  40. struct component_node chead;
  41. struct component_node ctail;
  42. /* The layout function: differs for vertical and horizontal boxes. */
  43. layout_func_t layout_func;
  44. };
  45. static void
  46. box_destroy (void *vself)
  47. {
  48. grub_gui_box_t self = vself;
  49. struct component_node *cur;
  50. struct component_node *next;
  51. for (cur = self->chead.next; cur != &self->ctail; cur = next)
  52. {
  53. /* Copy the 'next' pointer, since we need it for the next iteration,
  54. and we're going to free the memory it is stored in. */
  55. next = cur->next;
  56. /* Destroy the child component. */
  57. cur->component->ops->destroy (cur->component);
  58. /* Free the linked list node. */
  59. grub_free (cur);
  60. }
  61. grub_free (self);
  62. }
  63. static const char *
  64. box_get_id (void *vself)
  65. {
  66. grub_gui_box_t self = vself;
  67. return self->id;
  68. }
  69. static int
  70. box_is_instance (void *vself __attribute__((unused)), const char *type)
  71. {
  72. return (grub_strcmp (type, "component") == 0
  73. || grub_strcmp (type, "container") == 0);
  74. }
  75. static void
  76. layout_horizontally (grub_gui_box_t self, int modify_layout,
  77. unsigned *min_width, unsigned *min_height)
  78. {
  79. /* Start at the left (chead) and set the x coordinates as we go right. */
  80. /* All components have their width set to the box's width. */
  81. struct component_node *cur;
  82. unsigned w = 0, mwfrac = 0, h = 0, x = 0;
  83. grub_fixed_signed_t wfrac = 0;
  84. int bogus_frac = 0;
  85. for (cur = self->chead.next; cur != &self->ctail; cur = cur->next)
  86. {
  87. grub_gui_component_t c = cur->component;
  88. unsigned mw = 0, mh = 0;
  89. if (c->ops->get_minimal_size)
  90. c->ops->get_minimal_size (c, &mw, &mh);
  91. if (c->h > (signed) h)
  92. h = c->h;
  93. if (mh > h)
  94. h = mh;
  95. wfrac += c->wfrac;
  96. w += c->w;
  97. if (mw - c->w > 0)
  98. mwfrac += mw - c->w;
  99. }
  100. if (wfrac > GRUB_FIXED_1 || (w > 0 && wfrac == GRUB_FIXED_1))
  101. bogus_frac = 1;
  102. if (min_width)
  103. {
  104. if (wfrac < GRUB_FIXED_1)
  105. *min_width = grub_fixed_sfs_divide (w, GRUB_FIXED_1 - wfrac);
  106. else
  107. *min_width = w;
  108. if (*min_width < w + mwfrac)
  109. *min_width = w + mwfrac;
  110. }
  111. if (min_height)
  112. *min_height = h;
  113. if (!modify_layout)
  114. return;
  115. for (cur = self->chead.next; cur != &self->ctail; cur = cur->next)
  116. {
  117. grub_video_rect_t r;
  118. grub_gui_component_t c = cur->component;
  119. unsigned mw = 0, mh = 0;
  120. r.x = x;
  121. r.y = 0;
  122. r.height = h;
  123. if (c->ops->get_minimal_size)
  124. c->ops->get_minimal_size (c, &mw, &mh);
  125. r.width = c->w;
  126. if (!bogus_frac)
  127. r.width += grub_fixed_sfs_multiply (self->bounds.width, c->wfrac);
  128. if (r.width < mw)
  129. r.width = mw;
  130. c->ops->set_bounds (c, &r);
  131. x += r.width;
  132. }
  133. }
  134. static void
  135. layout_vertically (grub_gui_box_t self, int modify_layout,
  136. unsigned *min_width, unsigned *min_height)
  137. {
  138. /* Start at the top (chead) and set the y coordinates as we go rdown. */
  139. /* All components have their height set to the box's height. */
  140. struct component_node *cur;
  141. unsigned h = 0, mhfrac = 0, w = 0, y = 0;
  142. grub_fixed_signed_t hfrac = 0;
  143. int bogus_frac = 0;
  144. for (cur = self->chead.next; cur != &self->ctail; cur = cur->next)
  145. {
  146. grub_gui_component_t c = cur->component;
  147. unsigned mw = 0, mh = 0;
  148. if (c->ops->get_minimal_size)
  149. c->ops->get_minimal_size (c, &mw, &mh);
  150. if (c->w > (signed) w)
  151. w = c->w;
  152. if (mw > w)
  153. w = mw;
  154. hfrac += c->hfrac;
  155. h += c->h;
  156. if (mh - c->h > 0)
  157. mhfrac += mh - c->h;
  158. }
  159. if (hfrac > GRUB_FIXED_1 || (h > 0 && hfrac == GRUB_FIXED_1))
  160. bogus_frac = 1;
  161. if (min_height)
  162. {
  163. if (hfrac < GRUB_FIXED_1)
  164. *min_height = grub_fixed_sfs_divide (h, GRUB_FIXED_1 - hfrac);
  165. else
  166. *min_height = h;
  167. if (*min_height < h + mhfrac)
  168. *min_height = h + mhfrac;
  169. }
  170. if (min_width)
  171. *min_width = w;
  172. if (!modify_layout)
  173. return;
  174. for (cur = self->chead.next; cur != &self->ctail; cur = cur->next)
  175. {
  176. grub_video_rect_t r;
  177. grub_gui_component_t c = cur->component;
  178. unsigned mw = 0, mh = 0;
  179. r.x = 0;
  180. r.y = y;
  181. r.width = w;
  182. if (c->ops->get_minimal_size)
  183. c->ops->get_minimal_size (c, &mw, &mh);
  184. r.height = c->h;
  185. if (!bogus_frac)
  186. r.height += grub_fixed_sfs_multiply (self->bounds.height, c->hfrac);
  187. if (r.height < mh)
  188. r.height = mh;
  189. c->ops->set_bounds (c, &r);
  190. y += r.height;
  191. }
  192. }
  193. static void
  194. box_paint (void *vself, const grub_video_rect_t *region)
  195. {
  196. grub_gui_box_t self = vself;
  197. struct component_node *cur;
  198. grub_video_rect_t vpsave;
  199. grub_gui_set_viewport (&self->bounds, &vpsave);
  200. for (cur = self->chead.next; cur != &self->ctail; cur = cur->next)
  201. {
  202. grub_gui_component_t comp = cur->component;
  203. comp->ops->paint (comp, region);
  204. }
  205. grub_gui_restore_viewport (&vpsave);
  206. }
  207. static void
  208. box_set_parent (void *vself, grub_gui_container_t parent)
  209. {
  210. grub_gui_box_t self = vself;
  211. self->parent = parent;
  212. }
  213. static grub_gui_container_t
  214. box_get_parent (void *vself)
  215. {
  216. grub_gui_box_t self = vself;
  217. return self->parent;
  218. }
  219. static void
  220. box_set_bounds (void *vself, const grub_video_rect_t *bounds)
  221. {
  222. grub_gui_box_t self = vself;
  223. self->bounds = *bounds;
  224. self->layout_func (self, 1, 0, 0); /* Relayout the children. */
  225. }
  226. static void
  227. box_get_bounds (void *vself, grub_video_rect_t *bounds)
  228. {
  229. grub_gui_box_t self = vself;
  230. *bounds = self->bounds;
  231. }
  232. /* The box's preferred size is based on the preferred sizes
  233. of its children. */
  234. static void
  235. box_get_minimal_size (void *vself, unsigned *width, unsigned *height)
  236. {
  237. grub_gui_box_t self = vself;
  238. self->layout_func (self, 0, width, height); /* Just calculate the size. */
  239. }
  240. static grub_err_t
  241. box_set_property (void *vself, const char *name, const char *value)
  242. {
  243. grub_gui_box_t self = vself;
  244. if (grub_strcmp (name, "id") == 0)
  245. {
  246. grub_free (self->id);
  247. if (value)
  248. {
  249. self->id = grub_strdup (value);
  250. if (! self->id)
  251. return grub_errno;
  252. }
  253. else
  254. self->id = 0;
  255. }
  256. return grub_errno;
  257. }
  258. static void
  259. box_add (void *vself, grub_gui_component_t comp)
  260. {
  261. grub_gui_box_t self = vself;
  262. struct component_node *node;
  263. node = grub_malloc (sizeof (*node));
  264. if (! node)
  265. return; /* Note: probably should handle the error. */
  266. node->component = comp;
  267. /* Insert the node before the tail. */
  268. node->prev = self->ctail.prev;
  269. node->prev->next = node;
  270. node->next = &self->ctail;
  271. node->next->prev = node;
  272. comp->ops->set_parent (comp, (grub_gui_container_t) self);
  273. self->layout_func (self, 1, 0, 0); /* Relayout the children. */
  274. }
  275. static void
  276. box_remove (void *vself, grub_gui_component_t comp)
  277. {
  278. grub_gui_box_t self = vself;
  279. struct component_node *cur;
  280. for (cur = self->chead.next; cur != &self->ctail; cur = cur->next)
  281. {
  282. if (cur->component == comp)
  283. {
  284. /* Unlink 'cur' from the list. */
  285. cur->prev->next = cur->next;
  286. cur->next->prev = cur->prev;
  287. /* Free the node's memory (but don't destroy the component). */
  288. grub_free (cur);
  289. /* Must not loop again, since 'cur' would be dereferenced! */
  290. return;
  291. }
  292. }
  293. }
  294. static void
  295. box_iterate_children (void *vself,
  296. grub_gui_component_callback cb, void *userdata)
  297. {
  298. grub_gui_box_t self = vself;
  299. struct component_node *cur;
  300. for (cur = self->chead.next; cur != &self->ctail; cur = cur->next)
  301. cb (cur->component, userdata);
  302. }
  303. static struct grub_gui_component_ops box_comp_ops =
  304. {
  305. .destroy = box_destroy,
  306. .get_id = box_get_id,
  307. .is_instance = box_is_instance,
  308. .paint = box_paint,
  309. .set_parent = box_set_parent,
  310. .get_parent = box_get_parent,
  311. .set_bounds = box_set_bounds,
  312. .get_bounds = box_get_bounds,
  313. .get_minimal_size = box_get_minimal_size,
  314. .set_property = box_set_property
  315. };
  316. static struct grub_gui_container_ops box_ops =
  317. {
  318. .add = box_add,
  319. .remove = box_remove,
  320. .iterate_children = box_iterate_children
  321. };
  322. /* Box constructor. Specify the appropriate layout function to create
  323. a horizontal or vertical stacking box. */
  324. static grub_gui_box_t
  325. box_new (layout_func_t layout_func)
  326. {
  327. grub_gui_box_t box;
  328. box = grub_zalloc (sizeof (*box));
  329. if (! box)
  330. return 0;
  331. box->container.ops = &box_ops;
  332. box->container.component.ops = &box_comp_ops;
  333. box->chead.next = &box->ctail;
  334. box->ctail.prev = &box->chead;
  335. box->layout_func = layout_func;
  336. return box;
  337. }
  338. /* Create a new container that stacks its child components horizontally,
  339. from left to right. Each child get a width corresponding to its
  340. preferred width. The height of each child is set the maximum of the
  341. preferred heights of all children. */
  342. grub_gui_container_t
  343. grub_gui_hbox_new (void)
  344. {
  345. return (grub_gui_container_t) box_new (layout_horizontally);
  346. }
  347. /* Create a new container that stacks its child components verticallyj,
  348. from top to bottom. Each child get a height corresponding to its
  349. preferred height. The width of each child is set the maximum of the
  350. preferred widths of all children. */
  351. grub_gui_container_t
  352. grub_gui_vbox_new (void)
  353. {
  354. return (grub_gui_container_t) box_new (layout_vertically);
  355. }