widget-box.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* widget_box.c - Pixmap-stylized box widget. */
  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/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/video.h>
  24. #include <grub/bitmap.h>
  25. #include <grub/bitmap_scale.h>
  26. #include <grub/gfxwidgets.h>
  27. enum box_pixmaps
  28. {
  29. BOX_PIXMAP_NW, BOX_PIXMAP_NE, BOX_PIXMAP_SE, BOX_PIXMAP_SW,
  30. BOX_PIXMAP_N, BOX_PIXMAP_E, BOX_PIXMAP_S, BOX_PIXMAP_W,
  31. BOX_PIXMAP_CENTER
  32. };
  33. static const char *box_pixmap_names[] = {
  34. /* Corners: */
  35. "nw", "ne", "se", "sw",
  36. /* Sides: */
  37. "n", "e", "s", "w",
  38. /* Center: */
  39. "c"
  40. };
  41. #define BOX_NUM_PIXMAPS (sizeof(box_pixmap_names)/sizeof(*box_pixmap_names))
  42. static int
  43. get_height (struct grub_video_bitmap *bitmap)
  44. {
  45. if (bitmap)
  46. return grub_video_bitmap_get_height (bitmap);
  47. else
  48. return 0;
  49. }
  50. static int
  51. get_width (struct grub_video_bitmap *bitmap)
  52. {
  53. if (bitmap)
  54. return grub_video_bitmap_get_width (bitmap);
  55. else
  56. return 0;
  57. }
  58. static void
  59. blit (grub_gfxmenu_box_t self, int pixmap_index, int x, int y)
  60. {
  61. struct grub_video_bitmap *bitmap;
  62. bitmap = self->scaled_pixmaps[pixmap_index];
  63. if (! bitmap)
  64. return;
  65. grub_video_blit_bitmap (bitmap, GRUB_VIDEO_BLIT_BLEND,
  66. x, y, 0, 0,
  67. grub_video_bitmap_get_width (bitmap),
  68. grub_video_bitmap_get_height (bitmap));
  69. }
  70. static void
  71. draw (grub_gfxmenu_box_t self, int x, int y)
  72. {
  73. int height_n;
  74. int width_w;
  75. int tmp;
  76. /* Count maximum height of NW, N, NE. */
  77. height_n = get_height (self->scaled_pixmaps[BOX_PIXMAP_NW]);
  78. tmp = get_height (self->scaled_pixmaps[BOX_PIXMAP_N]);
  79. if (tmp > height_n)
  80. height_n = tmp;
  81. tmp = get_height (self->scaled_pixmaps[BOX_PIXMAP_NE]);
  82. if (tmp > height_n)
  83. height_n = tmp;
  84. /* Count maximum width of NW, W, SW. */
  85. width_w = get_width (self->scaled_pixmaps[BOX_PIXMAP_NW]);
  86. tmp = get_width (self->scaled_pixmaps[BOX_PIXMAP_W]);
  87. if (tmp > width_w)
  88. width_w = tmp;
  89. tmp = get_width (self->scaled_pixmaps[BOX_PIXMAP_SW]);
  90. if (tmp > width_w)
  91. width_w = tmp;
  92. /* Draw sides. */
  93. blit (self, BOX_PIXMAP_N, x + width_w, y);
  94. blit (self, BOX_PIXMAP_S, x + width_w, y + height_n + self->content_height);
  95. blit (self, BOX_PIXMAP_E, x + width_w + self->content_width, y + height_n);
  96. blit (self, BOX_PIXMAP_W, x, y + height_n);
  97. /* Draw corners. */
  98. blit (self, BOX_PIXMAP_NW, x, y);
  99. blit (self, BOX_PIXMAP_NE, x + width_w + self->content_width, y);
  100. blit (self, BOX_PIXMAP_SE,
  101. x + width_w + self->content_width,
  102. y + height_n + self->content_height);
  103. blit (self, BOX_PIXMAP_SW, x, y + height_n + self->content_height);
  104. /* Draw center. */
  105. blit (self, BOX_PIXMAP_CENTER, x + width_w, y + height_n);
  106. }
  107. static grub_err_t
  108. scale_pixmap (grub_gfxmenu_box_t self, int i, int w, int h)
  109. {
  110. struct grub_video_bitmap **scaled = &self->scaled_pixmaps[i];
  111. struct grub_video_bitmap *raw = self->raw_pixmaps[i];
  112. if (raw == 0)
  113. return grub_errno;
  114. if (w == -1)
  115. w = grub_video_bitmap_get_width (raw);
  116. if (h == -1)
  117. h = grub_video_bitmap_get_height (raw);
  118. if (*scaled == 0
  119. || ((int) grub_video_bitmap_get_width (*scaled) != w)
  120. || ((int) grub_video_bitmap_get_height (*scaled) != h))
  121. {
  122. if (*scaled)
  123. {
  124. grub_video_bitmap_destroy (*scaled);
  125. *scaled = 0;
  126. }
  127. /* Don't try to create a bitmap with a zero dimension. */
  128. if (w != 0 && h != 0)
  129. grub_video_bitmap_create_scaled (scaled, w, h, raw,
  130. GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
  131. }
  132. return grub_errno;
  133. }
  134. static void
  135. set_content_size (grub_gfxmenu_box_t self,
  136. int width, int height)
  137. {
  138. self->content_width = width;
  139. self->content_height = height;
  140. /* Resize sides to match the width and height. */
  141. /* It is assumed that the corners width/height match the adjacent sides. */
  142. /* Resize N and S sides to match width. */
  143. if (scale_pixmap(self, BOX_PIXMAP_N, width, -1) != GRUB_ERR_NONE)
  144. return;
  145. if (scale_pixmap(self, BOX_PIXMAP_S, width, -1) != GRUB_ERR_NONE)
  146. return;
  147. /* Resize E and W sides to match height. */
  148. if (scale_pixmap(self, BOX_PIXMAP_E, -1, height) != GRUB_ERR_NONE)
  149. return;
  150. if (scale_pixmap(self, BOX_PIXMAP_W, -1, height) != GRUB_ERR_NONE)
  151. return;
  152. /* Don't scale the corners--they are assumed to match the sides. */
  153. if (scale_pixmap(self, BOX_PIXMAP_NW, -1, -1) != GRUB_ERR_NONE)
  154. return;
  155. if (scale_pixmap(self, BOX_PIXMAP_SW, -1, -1) != GRUB_ERR_NONE)
  156. return;
  157. if (scale_pixmap(self, BOX_PIXMAP_NE, -1, -1) != GRUB_ERR_NONE)
  158. return;
  159. if (scale_pixmap(self, BOX_PIXMAP_SE, -1, -1) != GRUB_ERR_NONE)
  160. return;
  161. /* Scale the center area. */
  162. if (scale_pixmap(self, BOX_PIXMAP_CENTER, width, height) != GRUB_ERR_NONE)
  163. return;
  164. }
  165. static int
  166. get_border_width (grub_gfxmenu_box_t self)
  167. {
  168. return (get_width (self->raw_pixmaps[BOX_PIXMAP_E])
  169. + get_width (self->raw_pixmaps[BOX_PIXMAP_W]));
  170. }
  171. static int
  172. get_left_pad (grub_gfxmenu_box_t self)
  173. {
  174. int v, c;
  175. v = get_width (self->raw_pixmaps[BOX_PIXMAP_W]);
  176. c = get_width (self->raw_pixmaps[BOX_PIXMAP_NW]);
  177. if (c > v)
  178. v = c;
  179. c = get_width (self->raw_pixmaps[BOX_PIXMAP_SW]);
  180. if (c > v)
  181. v = c;
  182. return v;
  183. }
  184. static int
  185. get_top_pad (grub_gfxmenu_box_t self)
  186. {
  187. int v, c;
  188. v = get_height (self->raw_pixmaps[BOX_PIXMAP_N]);
  189. c = get_height (self->raw_pixmaps[BOX_PIXMAP_NW]);
  190. if (c > v)
  191. v = c;
  192. c = get_height (self->raw_pixmaps[BOX_PIXMAP_NE]);
  193. if (c > v)
  194. v = c;
  195. return v;
  196. }
  197. static int
  198. get_right_pad (grub_gfxmenu_box_t self)
  199. {
  200. int v, c;
  201. v = get_width (self->raw_pixmaps[BOX_PIXMAP_E]);
  202. c = get_width (self->raw_pixmaps[BOX_PIXMAP_NE]);
  203. if (c > v)
  204. v = c;
  205. c = get_width (self->raw_pixmaps[BOX_PIXMAP_SE]);
  206. if (c > v)
  207. v = c;
  208. return v;
  209. }
  210. static int
  211. get_bottom_pad (grub_gfxmenu_box_t self)
  212. {
  213. int v, c;
  214. v = get_height (self->raw_pixmaps[BOX_PIXMAP_S]);
  215. c = get_height (self->raw_pixmaps[BOX_PIXMAP_SW]);
  216. if (c > v)
  217. v = c;
  218. c = get_height (self->raw_pixmaps[BOX_PIXMAP_SE]);
  219. if (c > v)
  220. v = c;
  221. return v;
  222. }
  223. static void
  224. destroy (grub_gfxmenu_box_t self)
  225. {
  226. unsigned i;
  227. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  228. {
  229. if (self->raw_pixmaps[i])
  230. grub_video_bitmap_destroy(self->raw_pixmaps[i]);
  231. self->raw_pixmaps[i] = 0;
  232. if (self->scaled_pixmaps[i])
  233. grub_video_bitmap_destroy(self->scaled_pixmaps[i]);
  234. self->scaled_pixmaps[i] = 0;
  235. }
  236. grub_free (self->raw_pixmaps);
  237. self->raw_pixmaps = 0;
  238. grub_free (self->scaled_pixmaps);
  239. self->scaled_pixmaps = 0;
  240. /* Free self: must be the last step! */
  241. grub_free (self);
  242. }
  243. /* Create a new box. If PIXMAPS_PREFIX and PIXMAPS_SUFFIX are both non-null,
  244. then an attempt is made to load the north, south, east, west, northwest,
  245. northeast, southeast, southwest, and center pixmaps.
  246. If either PIXMAPS_PREFIX or PIXMAPS_SUFFIX is 0, then no pixmaps are
  247. loaded, and the box has zero-width borders and is drawn transparent. */
  248. grub_gfxmenu_box_t
  249. grub_gfxmenu_create_box (const char *pixmaps_prefix,
  250. const char *pixmaps_suffix)
  251. {
  252. unsigned i;
  253. grub_gfxmenu_box_t box;
  254. box = (grub_gfxmenu_box_t) grub_malloc (sizeof (*box));
  255. if (! box)
  256. return 0;
  257. box->content_width = 0;
  258. box->content_height = 0;
  259. box->raw_pixmaps =
  260. (struct grub_video_bitmap **)
  261. grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
  262. box->scaled_pixmaps =
  263. (struct grub_video_bitmap **)
  264. grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
  265. /* Initialize all pixmap pointers to NULL so that proper destruction can
  266. be performed if an error is encountered partway through construction. */
  267. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  268. box->raw_pixmaps[i] = 0;
  269. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  270. box->scaled_pixmaps[i] = 0;
  271. /* Load the pixmaps. */
  272. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  273. {
  274. if (pixmaps_prefix && pixmaps_suffix)
  275. {
  276. char *path;
  277. char *path_end;
  278. path = grub_malloc (grub_strlen (pixmaps_prefix)
  279. + grub_strlen (box_pixmap_names[i])
  280. + grub_strlen (pixmaps_suffix)
  281. + 1);
  282. if (! path)
  283. goto fail_and_destroy;
  284. /* Construct the specific path for this pixmap. */
  285. path_end = grub_stpcpy (path, pixmaps_prefix);
  286. path_end = grub_stpcpy (path_end, box_pixmap_names[i]);
  287. path_end = grub_stpcpy (path_end, pixmaps_suffix);
  288. grub_video_bitmap_load (&box->raw_pixmaps[i], path);
  289. grub_free (path);
  290. /* Ignore missing pixmaps. */
  291. grub_errno = GRUB_ERR_NONE;
  292. }
  293. }
  294. box->draw = draw;
  295. box->set_content_size = set_content_size;
  296. box->get_border_width = get_border_width;
  297. box->get_left_pad = get_left_pad;
  298. box->get_top_pad = get_top_pad;
  299. box->get_right_pad = get_right_pad;
  300. box->get_bottom_pad = get_bottom_pad;
  301. box->destroy = destroy;
  302. return box;
  303. fail_and_destroy:
  304. destroy (box);
  305. return 0;
  306. }