widget-box.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 height_s;
  75. int height_e;
  76. int height_w;
  77. int width_n;
  78. int width_s;
  79. int width_e;
  80. int width_w;
  81. height_n = get_height (self->scaled_pixmaps[BOX_PIXMAP_N]);
  82. height_s = get_height (self->scaled_pixmaps[BOX_PIXMAP_S]);
  83. height_e = get_height (self->scaled_pixmaps[BOX_PIXMAP_E]);
  84. height_w = get_height (self->scaled_pixmaps[BOX_PIXMAP_W]);
  85. width_n = get_width (self->scaled_pixmaps[BOX_PIXMAP_N]);
  86. width_s = get_width (self->scaled_pixmaps[BOX_PIXMAP_S]);
  87. width_e = get_width (self->scaled_pixmaps[BOX_PIXMAP_E]);
  88. width_w = get_width (self->scaled_pixmaps[BOX_PIXMAP_W]);
  89. /* Draw sides. */
  90. blit (self, BOX_PIXMAP_N, x + width_w, y);
  91. blit (self, BOX_PIXMAP_S, x + width_w, y + height_n + self->content_height);
  92. blit (self, BOX_PIXMAP_E, x + width_w + self->content_width, y + height_n);
  93. blit (self, BOX_PIXMAP_W, x, y + height_n);
  94. /* Draw corners. */
  95. blit (self, BOX_PIXMAP_NW, x, y);
  96. blit (self, BOX_PIXMAP_NE, x + width_w + self->content_width, y);
  97. blit (self, BOX_PIXMAP_SE,
  98. x + width_w + self->content_width,
  99. y + height_n + self->content_height);
  100. blit (self, BOX_PIXMAP_SW, x, y + height_n + self->content_height);
  101. /* Draw center. */
  102. blit (self, BOX_PIXMAP_CENTER, x + width_w, y + height_n);
  103. }
  104. static grub_err_t
  105. scale_pixmap (grub_gfxmenu_box_t self, int i, int w, int h)
  106. {
  107. struct grub_video_bitmap **scaled = &self->scaled_pixmaps[i];
  108. struct grub_video_bitmap *raw = self->raw_pixmaps[i];
  109. if (raw == 0)
  110. return grub_errno;
  111. if (w == -1)
  112. w = grub_video_bitmap_get_width (raw);
  113. if (h == -1)
  114. h = grub_video_bitmap_get_height (raw);
  115. if (*scaled == 0
  116. || ((int) grub_video_bitmap_get_width (*scaled) != w)
  117. || ((int) grub_video_bitmap_get_height (*scaled) != h))
  118. {
  119. if (*scaled)
  120. {
  121. grub_video_bitmap_destroy (*scaled);
  122. *scaled = 0;
  123. }
  124. /* Don't try to create a bitmap with a zero dimension. */
  125. if (w != 0 && h != 0)
  126. grub_video_bitmap_create_scaled (scaled, w, h, raw,
  127. GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST, 0);
  128. if (grub_errno != GRUB_ERR_NONE)
  129. {
  130. grub_error_push ();
  131. grub_error (grub_errno,
  132. "failed to scale bitmap for styled box pixmap #%d", i);
  133. }
  134. }
  135. return grub_errno;
  136. }
  137. static void
  138. set_content_size (grub_gfxmenu_box_t self,
  139. int width, int height)
  140. {
  141. self->content_width = width;
  142. self->content_height = height;
  143. /* Resize sides to match the width and height. */
  144. /* It is assumed that the corners width/height match the adjacent sides. */
  145. /* Resize N and S sides to match width. */
  146. if (scale_pixmap(self, BOX_PIXMAP_N, width, -1) != GRUB_ERR_NONE)
  147. return;
  148. if (scale_pixmap(self, BOX_PIXMAP_S, width, -1) != GRUB_ERR_NONE)
  149. return;
  150. /* Resize E and W sides to match height. */
  151. if (scale_pixmap(self, BOX_PIXMAP_E, -1, height) != GRUB_ERR_NONE)
  152. return;
  153. if (scale_pixmap(self, BOX_PIXMAP_W, -1, height) != GRUB_ERR_NONE)
  154. return;
  155. /* Don't scale the corners--they are assumed to match the sides. */
  156. if (scale_pixmap(self, BOX_PIXMAP_NW, -1, -1) != GRUB_ERR_NONE)
  157. return;
  158. if (scale_pixmap(self, BOX_PIXMAP_SW, -1, -1) != GRUB_ERR_NONE)
  159. return;
  160. if (scale_pixmap(self, BOX_PIXMAP_NE, -1, -1) != GRUB_ERR_NONE)
  161. return;
  162. if (scale_pixmap(self, BOX_PIXMAP_SE, -1, -1) != GRUB_ERR_NONE)
  163. return;
  164. /* Scale the center area. */
  165. if (scale_pixmap(self, BOX_PIXMAP_CENTER, width, height) != GRUB_ERR_NONE)
  166. return;
  167. }
  168. static int
  169. get_left_pad (grub_gfxmenu_box_t self)
  170. {
  171. return get_width (self->raw_pixmaps[BOX_PIXMAP_W]);
  172. }
  173. static int
  174. get_top_pad (grub_gfxmenu_box_t self)
  175. {
  176. return get_height (self->raw_pixmaps[BOX_PIXMAP_N]);
  177. }
  178. static int
  179. get_right_pad (grub_gfxmenu_box_t self)
  180. {
  181. return get_width (self->raw_pixmaps[BOX_PIXMAP_E]);
  182. }
  183. static int
  184. get_bottom_pad (grub_gfxmenu_box_t self)
  185. {
  186. return get_height (self->raw_pixmaps[BOX_PIXMAP_S]);
  187. }
  188. static void
  189. destroy (grub_gfxmenu_box_t self)
  190. {
  191. unsigned i;
  192. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  193. {
  194. if (self->raw_pixmaps[i])
  195. grub_video_bitmap_destroy(self->raw_pixmaps[i]);
  196. self->raw_pixmaps[i] = 0;
  197. if (self->scaled_pixmaps[i])
  198. grub_video_bitmap_destroy(self->scaled_pixmaps[i]);
  199. self->scaled_pixmaps[i] = 0;
  200. }
  201. grub_free (self->raw_pixmaps);
  202. self->raw_pixmaps = 0;
  203. grub_free (self->scaled_pixmaps);
  204. self->scaled_pixmaps = 0;
  205. /* Free self: must be the last step! */
  206. grub_free (self);
  207. }
  208. /* Create a new box. If PIXMAPS_PREFIX and PIXMAPS_SUFFIX are both non-null,
  209. then an attempt is made to load the north, south, east, west, northwest,
  210. northeast, southeast, southwest, and center pixmaps.
  211. If either PIXMAPS_PREFIX or PIXMAPS_SUFFIX is 0, then no pixmaps are
  212. loaded, and the box has zero-width borders and is drawn transparent. */
  213. grub_gfxmenu_box_t
  214. grub_gfxmenu_create_box (const char *pixmaps_prefix,
  215. const char *pixmaps_suffix)
  216. {
  217. unsigned i;
  218. grub_gfxmenu_box_t box;
  219. box = (grub_gfxmenu_box_t) grub_malloc (sizeof (*box));
  220. if (! box)
  221. return 0;
  222. box->content_width = 0;
  223. box->content_height = 0;
  224. box->raw_pixmaps =
  225. (struct grub_video_bitmap **)
  226. grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
  227. box->scaled_pixmaps =
  228. (struct grub_video_bitmap **)
  229. grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
  230. /* Initialize all pixmap pointers to NULL so that proper destruction can
  231. be performed if an error is encountered partway through construction. */
  232. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  233. box->raw_pixmaps[i] = 0;
  234. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  235. box->scaled_pixmaps[i] = 0;
  236. /* Load the pixmaps. */
  237. for (i = 0; i < BOX_NUM_PIXMAPS; i++)
  238. {
  239. if (pixmaps_prefix && pixmaps_suffix)
  240. {
  241. char *path;
  242. char *path_end;
  243. path = grub_malloc (grub_strlen (pixmaps_prefix)
  244. + grub_strlen (box_pixmap_names[i])
  245. + grub_strlen (pixmaps_suffix)
  246. + 1);
  247. if (! path)
  248. goto fail_and_destroy;
  249. /* Construct the specific path for this pixmap. */
  250. path_end = grub_stpcpy (path, pixmaps_prefix);
  251. path_end = grub_stpcpy (path_end, box_pixmap_names[i]);
  252. path_end = grub_stpcpy (path_end, pixmaps_suffix);
  253. grub_video_bitmap_load (&box->raw_pixmaps[i], path);
  254. grub_free (path);
  255. /* Ignore missing pixmaps. */
  256. grub_errno = GRUB_ERR_NONE;
  257. }
  258. }
  259. box->draw = draw;
  260. box->set_content_size = set_content_size;
  261. box->get_left_pad = get_left_pad;
  262. box->get_top_pad = get_top_pad;
  263. box->get_right_pad = get_right_pad;
  264. box->get_bottom_pad = get_bottom_pad;
  265. box->destroy = destroy;
  266. return box;
  267. fail_and_destroy:
  268. destroy (box);
  269. return 0;
  270. }