gui_image.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* gui_image.c - GUI component to display an image. */
  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. #include <grub/bitmap.h>
  24. #include <grub/bitmap_scale.h>
  25. struct grub_gui_image
  26. {
  27. struct grub_gui_component component;
  28. grub_gui_container_t parent;
  29. grub_video_rect_t bounds;
  30. char *id;
  31. char *theme_dir;
  32. struct grub_video_bitmap *raw_bitmap;
  33. struct grub_video_bitmap *bitmap;
  34. };
  35. typedef struct grub_gui_image *grub_gui_image_t;
  36. static void
  37. image_destroy (void *vself)
  38. {
  39. grub_gui_image_t self = vself;
  40. /* Free the scaled bitmap, unless it's a reference to the raw bitmap. */
  41. if (self->bitmap && (self->bitmap != self->raw_bitmap))
  42. grub_video_bitmap_destroy (self->bitmap);
  43. if (self->raw_bitmap)
  44. grub_video_bitmap_destroy (self->raw_bitmap);
  45. grub_free (self);
  46. }
  47. static const char *
  48. image_get_id (void *vself)
  49. {
  50. grub_gui_image_t self = vself;
  51. return self->id;
  52. }
  53. static int
  54. image_is_instance (void *vself __attribute__((unused)), const char *type)
  55. {
  56. return grub_strcmp (type, "component") == 0;
  57. }
  58. static void
  59. image_paint (void *vself, const grub_video_rect_t *region)
  60. {
  61. grub_gui_image_t self = vself;
  62. grub_video_rect_t vpsave;
  63. if (! self->bitmap)
  64. return;
  65. if (!grub_video_have_common_points (region, &self->bounds))
  66. return;
  67. grub_gui_set_viewport (&self->bounds, &vpsave);
  68. grub_video_blit_bitmap (self->bitmap, GRUB_VIDEO_BLIT_BLEND,
  69. 0, 0, 0, 0,
  70. grub_video_bitmap_get_width (self->bitmap),
  71. grub_video_bitmap_get_height (self->bitmap));
  72. grub_gui_restore_viewport (&vpsave);
  73. }
  74. static void
  75. image_set_parent (void *vself, grub_gui_container_t parent)
  76. {
  77. grub_gui_image_t self = vself;
  78. self->parent = parent;
  79. }
  80. static grub_gui_container_t
  81. image_get_parent (void *vself)
  82. {
  83. grub_gui_image_t self = vself;
  84. return self->parent;
  85. }
  86. static grub_err_t
  87. rescale_image (grub_gui_image_t self)
  88. {
  89. if (! self->raw_bitmap)
  90. {
  91. if (self->bitmap)
  92. {
  93. grub_video_bitmap_destroy (self->bitmap);
  94. self->bitmap = 0;
  95. }
  96. return grub_errno;
  97. }
  98. unsigned width = self->bounds.width;
  99. unsigned height = self->bounds.height;
  100. if (self->bitmap
  101. && (grub_video_bitmap_get_width (self->bitmap) == width)
  102. && (grub_video_bitmap_get_height (self->bitmap) == height))
  103. {
  104. /* Nothing to do; already the right size. */
  105. return grub_errno;
  106. }
  107. /* Free any old scaled bitmap,
  108. *unless* it's a reference to the raw bitmap. */
  109. if (self->bitmap && (self->bitmap != self->raw_bitmap))
  110. grub_video_bitmap_destroy (self->bitmap);
  111. self->bitmap = 0;
  112. /* Create a scaled bitmap, unless the requested size is the same
  113. as the raw size -- in that case a reference is made. */
  114. if (grub_video_bitmap_get_width (self->raw_bitmap) == width
  115. && grub_video_bitmap_get_height (self->raw_bitmap) == height)
  116. {
  117. self->bitmap = self->raw_bitmap;
  118. return grub_errno;
  119. }
  120. /* Don't scale to an invalid size. */
  121. if (width == 0 || height == 0)
  122. return grub_errno;
  123. /* Create the scaled bitmap. */
  124. grub_video_bitmap_create_scaled (&self->bitmap,
  125. width,
  126. height,
  127. self->raw_bitmap,
  128. GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST, 0);
  129. if (grub_errno != GRUB_ERR_NONE)
  130. {
  131. grub_error_push ();
  132. grub_error (grub_errno, "failed to scale bitmap for image component");
  133. }
  134. return grub_errno;
  135. }
  136. static void
  137. image_set_bounds (void *vself, const grub_video_rect_t *bounds)
  138. {
  139. grub_gui_image_t self = vself;
  140. self->bounds = *bounds;
  141. rescale_image (self);
  142. }
  143. static void
  144. image_get_bounds (void *vself, grub_video_rect_t *bounds)
  145. {
  146. grub_gui_image_t self = vself;
  147. *bounds = self->bounds;
  148. }
  149. /* FIXME: inform rendering system it's not forced minimum. */
  150. static void
  151. image_get_minimal_size (void *vself, unsigned *width, unsigned *height)
  152. {
  153. grub_gui_image_t self = vself;
  154. if (self->raw_bitmap)
  155. {
  156. *width = grub_video_bitmap_get_width (self->raw_bitmap);
  157. *height = grub_video_bitmap_get_height (self->raw_bitmap);
  158. }
  159. else
  160. {
  161. *width = 0;
  162. *height = 0;
  163. }
  164. }
  165. static grub_err_t
  166. load_image (grub_gui_image_t self, const char *path)
  167. {
  168. struct grub_video_bitmap *bitmap;
  169. if (grub_video_bitmap_load (&bitmap, path) != GRUB_ERR_NONE)
  170. return grub_errno;
  171. if (self->bitmap && (self->bitmap != self->raw_bitmap))
  172. grub_video_bitmap_destroy (self->bitmap);
  173. if (self->raw_bitmap)
  174. grub_video_bitmap_destroy (self->raw_bitmap);
  175. self->raw_bitmap = bitmap;
  176. return rescale_image (self);
  177. }
  178. static grub_err_t
  179. image_set_property (void *vself, const char *name, const char *value)
  180. {
  181. grub_gui_image_t self = vself;
  182. if (grub_strcmp (name, "theme_dir") == 0)
  183. {
  184. grub_free (self->theme_dir);
  185. self->theme_dir = grub_strdup (value);
  186. }
  187. else if (grub_strcmp (name, "file") == 0)
  188. {
  189. char *absvalue;
  190. grub_err_t err;
  191. /* Resolve to an absolute path. */
  192. if (! self->theme_dir)
  193. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unspecified theme_dir");
  194. absvalue = grub_resolve_relative_path (self->theme_dir, value);
  195. if (! absvalue)
  196. return grub_errno;
  197. err = load_image (self, absvalue);
  198. grub_free (absvalue);
  199. return err;
  200. }
  201. else if (grub_strcmp (name, "id") == 0)
  202. {
  203. grub_free (self->id);
  204. if (value)
  205. self->id = grub_strdup (value);
  206. else
  207. self->id = 0;
  208. }
  209. return grub_errno;
  210. }
  211. static struct grub_gui_component_ops image_ops =
  212. {
  213. .destroy = image_destroy,
  214. .get_id = image_get_id,
  215. .is_instance = image_is_instance,
  216. .paint = image_paint,
  217. .set_parent = image_set_parent,
  218. .get_parent = image_get_parent,
  219. .set_bounds = image_set_bounds,
  220. .get_bounds = image_get_bounds,
  221. .get_minimal_size = image_get_minimal_size,
  222. .set_property = image_set_property
  223. };
  224. grub_gui_component_t
  225. grub_gui_image_new (void)
  226. {
  227. grub_gui_image_t image;
  228. image = grub_zalloc (sizeof (*image));
  229. if (! image)
  230. return 0;
  231. image->component.ops = &image_ops;
  232. return (grub_gui_component_t) image;
  233. }