1 /* gui_image.c - GUI component to display an image. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
21 #include <grub/misc.h>
23 #include <grub/gui_string_util.h>
24 #include <grub/bitmap.h>
25 #include <grub/bitmap_scale.h>
29 struct grub_gui_component component
;
31 grub_gui_container_t parent
;
32 grub_video_rect_t bounds
;
35 struct grub_video_bitmap
*raw_bitmap
;
36 struct grub_video_bitmap
*bitmap
;
39 typedef struct grub_gui_image
*grub_gui_image_t
;
42 image_destroy (void *vself
)
44 grub_gui_image_t self
= vself
;
46 /* Free the scaled bitmap, unless it's a reference to the raw bitmap. */
47 if (self
->bitmap
&& (self
->bitmap
!= self
->raw_bitmap
))
48 grub_video_bitmap_destroy (self
->bitmap
);
50 grub_video_bitmap_destroy (self
->raw_bitmap
);
56 image_get_id (void *vself
)
58 grub_gui_image_t self
= vself
;
63 image_is_instance (void *vself
__attribute__((unused
)), const char *type
)
65 return grub_strcmp (type
, "component") == 0;
69 image_paint (void *vself
, const grub_video_rect_t
*region
)
71 grub_gui_image_t self
= vself
;
72 grub_video_rect_t vpsave
;
76 if (!grub_video_have_common_points (region
, &self
->bounds
))
79 grub_gui_set_viewport (&self
->bounds
, &vpsave
);
80 grub_video_blit_bitmap (self
->bitmap
, GRUB_VIDEO_BLIT_BLEND
,
82 grub_video_bitmap_get_width (self
->bitmap
),
83 grub_video_bitmap_get_height (self
->bitmap
));
84 grub_gui_restore_viewport (&vpsave
);
88 image_set_parent (void *vself
, grub_gui_container_t parent
)
90 grub_gui_image_t self
= vself
;
91 self
->parent
= parent
;
94 static grub_gui_container_t
95 image_get_parent (void *vself
)
97 grub_gui_image_t self
= vself
;
102 rescale_image (grub_gui_image_t self
)
107 if (! self
->raw_bitmap
)
111 grub_video_bitmap_destroy (self
->bitmap
);
117 width
= self
->bounds
.width
;
118 height
= self
->bounds
.height
;
121 && ((signed) grub_video_bitmap_get_width (self
->bitmap
) == width
)
122 && ((signed) grub_video_bitmap_get_height (self
->bitmap
) == height
))
124 /* Nothing to do; already the right size. */
128 /* Free any old scaled bitmap,
129 *unless* it's a reference to the raw bitmap. */
130 if (self
->bitmap
&& (self
->bitmap
!= self
->raw_bitmap
))
131 grub_video_bitmap_destroy (self
->bitmap
);
135 /* Create a scaled bitmap, unless the requested size is the same
136 as the raw size -- in that case a reference is made. */
137 if ((signed) grub_video_bitmap_get_width (self
->raw_bitmap
) == width
138 && (signed) grub_video_bitmap_get_height (self
->raw_bitmap
) == height
)
140 self
->bitmap
= self
->raw_bitmap
;
144 /* Don't scale to an invalid size. */
145 if (width
<= 0 || height
<= 0)
148 /* Create the scaled bitmap. */
149 grub_video_bitmap_create_scaled (&self
->bitmap
,
153 GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST
);
158 image_set_bounds (void *vself
, const grub_video_rect_t
*bounds
)
160 grub_gui_image_t self
= vself
;
161 self
->bounds
= *bounds
;
162 rescale_image (self
);
166 image_get_bounds (void *vself
, grub_video_rect_t
*bounds
)
168 grub_gui_image_t self
= vself
;
169 *bounds
= self
->bounds
;
172 /* FIXME: inform rendering system it's not forced minimum. */
174 image_get_minimal_size (void *vself
, unsigned *width
, unsigned *height
)
176 grub_gui_image_t self
= vself
;
178 if (self
->raw_bitmap
)
180 *width
= grub_video_bitmap_get_width (self
->raw_bitmap
);
181 *height
= grub_video_bitmap_get_height (self
->raw_bitmap
);
191 load_image (grub_gui_image_t self
, const char *path
)
193 struct grub_video_bitmap
*bitmap
;
194 if (grub_video_bitmap_load (&bitmap
, path
) != GRUB_ERR_NONE
)
197 if (self
->bitmap
&& (self
->bitmap
!= self
->raw_bitmap
))
198 grub_video_bitmap_destroy (self
->bitmap
);
199 if (self
->raw_bitmap
)
200 grub_video_bitmap_destroy (self
->raw_bitmap
);
202 self
->raw_bitmap
= bitmap
;
203 return rescale_image (self
);
207 image_set_property (void *vself
, const char *name
, const char *value
)
209 grub_gui_image_t self
= vself
;
210 if (grub_strcmp (name
, "theme_dir") == 0)
212 grub_free (self
->theme_dir
);
213 self
->theme_dir
= grub_strdup (value
);
215 else if (grub_strcmp (name
, "file") == 0)
220 /* Resolve to an absolute path. */
221 if (! self
->theme_dir
)
222 return grub_error (GRUB_ERR_BUG
, "unspecified theme_dir");
223 absvalue
= grub_resolve_relative_path (self
->theme_dir
, value
);
227 err
= load_image (self
, absvalue
);
228 grub_free (absvalue
);
232 else if (grub_strcmp (name
, "id") == 0)
234 grub_free (self
->id
);
236 self
->id
= grub_strdup (value
);
243 static struct grub_gui_component_ops image_ops
=
245 .destroy
= image_destroy
,
246 .get_id
= image_get_id
,
247 .is_instance
= image_is_instance
,
248 .paint
= image_paint
,
249 .set_parent
= image_set_parent
,
250 .get_parent
= image_get_parent
,
251 .set_bounds
= image_set_bounds
,
252 .get_bounds
= image_get_bounds
,
253 .get_minimal_size
= image_get_minimal_size
,
254 .set_property
= image_set_property
258 grub_gui_image_new (void)
260 grub_gui_image_t image
;
261 image
= grub_zalloc (sizeof (*image
));
264 image
->component
.ops
= &image_ops
;
265 return (grub_gui_component_t
) image
;