gui_image.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. signed width;
  90. signed height;
  91. if (! self->raw_bitmap)
  92. {
  93. if (self->bitmap)
  94. {
  95. grub_video_bitmap_destroy (self->bitmap);
  96. self->bitmap = 0;
  97. }
  98. return grub_errno;
  99. }
  100. width = self->bounds.width;
  101. height = self->bounds.height;
  102. if (self->bitmap
  103. && ((signed) grub_video_bitmap_get_width (self->bitmap) == width)
  104. && ((signed) grub_video_bitmap_get_height (self->bitmap) == height))
  105. {
  106. /* Nothing to do; already the right size. */
  107. return grub_errno;
  108. }
  109. /* Free any old scaled bitmap,
  110. *unless* it's a reference to the raw bitmap. */
  111. if (self->bitmap && (self->bitmap != self->raw_bitmap))
  112. grub_video_bitmap_destroy (self->bitmap);
  113. self->bitmap = 0;
  114. /* Create a scaled bitmap, unless the requested size is the same
  115. as the raw size -- in that case a reference is made. */
  116. if ((signed) grub_video_bitmap_get_width (self->raw_bitmap) == width
  117. && (signed) grub_video_bitmap_get_height (self->raw_bitmap) == height)
  118. {
  119. self->bitmap = self->raw_bitmap;
  120. return grub_errno;
  121. }
  122. /* Don't scale to an invalid size. */
  123. if (width <= 0 || height <= 0)
  124. return grub_errno;
  125. /* Create the scaled bitmap. */
  126. grub_video_bitmap_create_scaled (&self->bitmap,
  127. width,
  128. height,
  129. self->raw_bitmap,
  130. GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
  131. return grub_errno;
  132. }
  133. static void
  134. image_set_bounds (void *vself, const grub_video_rect_t *bounds)
  135. {
  136. grub_gui_image_t self = vself;
  137. self->bounds = *bounds;
  138. rescale_image (self);
  139. }
  140. static void
  141. image_get_bounds (void *vself, grub_video_rect_t *bounds)
  142. {
  143. grub_gui_image_t self = vself;
  144. *bounds = self->bounds;
  145. }
  146. /* FIXME: inform rendering system it's not forced minimum. */
  147. static void
  148. image_get_minimal_size (void *vself, unsigned *width, unsigned *height)
  149. {
  150. grub_gui_image_t self = vself;
  151. if (self->raw_bitmap)
  152. {
  153. *width = grub_video_bitmap_get_width (self->raw_bitmap);
  154. *height = grub_video_bitmap_get_height (self->raw_bitmap);
  155. }
  156. else
  157. {
  158. *width = 0;
  159. *height = 0;
  160. }
  161. }
  162. static grub_err_t
  163. load_image (grub_gui_image_t self, const char *path)
  164. {
  165. struct grub_video_bitmap *bitmap;
  166. if (grub_video_bitmap_load (&bitmap, path) != GRUB_ERR_NONE)
  167. return grub_errno;
  168. if (self->bitmap && (self->bitmap != self->raw_bitmap))
  169. grub_video_bitmap_destroy (self->bitmap);
  170. if (self->raw_bitmap)
  171. grub_video_bitmap_destroy (self->raw_bitmap);
  172. self->raw_bitmap = bitmap;
  173. return rescale_image (self);
  174. }
  175. static grub_err_t
  176. image_set_property (void *vself, const char *name, const char *value)
  177. {
  178. grub_gui_image_t self = vself;
  179. if (grub_strcmp (name, "theme_dir") == 0)
  180. {
  181. grub_free (self->theme_dir);
  182. self->theme_dir = grub_strdup (value);
  183. }
  184. else if (grub_strcmp (name, "file") == 0)
  185. {
  186. char *absvalue;
  187. grub_err_t err;
  188. /* Resolve to an absolute path. */
  189. if (! self->theme_dir)
  190. return grub_error (GRUB_ERR_BUG, "unspecified theme_dir");
  191. absvalue = grub_resolve_relative_path (self->theme_dir, value);
  192. if (! absvalue)
  193. return grub_errno;
  194. err = load_image (self, absvalue);
  195. grub_free (absvalue);
  196. return err;
  197. }
  198. else if (grub_strcmp (name, "id") == 0)
  199. {
  200. grub_free (self->id);
  201. if (value)
  202. self->id = grub_strdup (value);
  203. else
  204. self->id = 0;
  205. }
  206. return grub_errno;
  207. }
  208. static struct grub_gui_component_ops image_ops =
  209. {
  210. .destroy = image_destroy,
  211. .get_id = image_get_id,
  212. .is_instance = image_is_instance,
  213. .paint = image_paint,
  214. .set_parent = image_set_parent,
  215. .get_parent = image_get_parent,
  216. .set_bounds = image_set_bounds,
  217. .get_bounds = image_get_bounds,
  218. .get_minimal_size = image_get_minimal_size,
  219. .set_property = image_set_property
  220. };
  221. grub_gui_component_t
  222. grub_gui_image_new (void)
  223. {
  224. grub_gui_image_t image;
  225. image = grub_zalloc (sizeof (*image));
  226. if (! image)
  227. return 0;
  228. image->component.ops = &image_ops;
  229. return (grub_gui_component_t) image;
  230. }