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)
104
if (! self->raw_bitmap)
108
grub_video_bitmap_destroy (self->bitmap);
114
unsigned width = self->bounds.width;
115
unsigned height = self->bounds.height;
118
&& (grub_video_bitmap_get_width (self->bitmap) == width)
119
&& (grub_video_bitmap_get_height (self->bitmap) == height))
121
/* Nothing to do; already the right size. */
125
/* Free any old scaled bitmap,
126
*unless* it's a reference to the raw bitmap. */
127
if (self->bitmap && (self->bitmap != self->raw_bitmap))
128
grub_video_bitmap_destroy (self->bitmap);
132
/* Create a scaled bitmap, unless the requested size is the same
133
as the raw size -- in that case a reference is made. */
134
if (grub_video_bitmap_get_width (self->raw_bitmap) == width
135
&& grub_video_bitmap_get_height (self->raw_bitmap) == height)
137
self->bitmap = self->raw_bitmap;
141
/* Don't scale to an invalid size. */
142
if (width == 0 || height == 0)
145
/* Create the scaled bitmap. */
146
grub_video_bitmap_create_scaled (&self->bitmap,
150
GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
151
if (grub_errno != GRUB_ERR_NONE)
154
grub_error (grub_errno, "failed to scale bitmap for image component");
160
image_set_bounds (void *vself, const grub_video_rect_t *bounds)
162
grub_gui_image_t self = vself;
163
self->bounds = *bounds;
164
rescale_image (self);
168
image_get_bounds (void *vself, grub_video_rect_t *bounds)
170
grub_gui_image_t self = vself;
171
*bounds = self->bounds;
174
/* FIXME: inform rendering system it's not forced minimum. */
176
image_get_minimal_size (void *vself, unsigned *width, unsigned *height)
178
grub_gui_image_t self = vself;
180
if (self->raw_bitmap)
182
*width = grub_video_bitmap_get_width (self->raw_bitmap);
183
*height = grub_video_bitmap_get_height (self->raw_bitmap);
193
load_image (grub_gui_image_t self, const char *path)
195
struct grub_video_bitmap *bitmap;
196
if (grub_video_bitmap_load (&bitmap, path) != GRUB_ERR_NONE)
199
if (self->bitmap && (self->bitmap != self->raw_bitmap))
200
grub_video_bitmap_destroy (self->bitmap);
201
if (self->raw_bitmap)
202
grub_video_bitmap_destroy (self->raw_bitmap);
204
self->raw_bitmap = bitmap;
205
return rescale_image (self);
209
image_set_property (void *vself, const char *name, const char *value)
211
grub_gui_image_t self = vself;
212
if (grub_strcmp (name, "theme_dir") == 0)
214
grub_free (self->theme_dir);
215
self->theme_dir = grub_strdup (value);
217
else if (grub_strcmp (name, "file") == 0)
222
/* Resolve to an absolute path. */
223
if (! self->theme_dir)
224
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unspecified theme_dir");
225
absvalue = grub_resolve_relative_path (self->theme_dir, value);
229
err = load_image (self, absvalue);
230
grub_free (absvalue);
234
else if (grub_strcmp (name, "id") == 0)
236
grub_free (self->id);
238
self->id = grub_strdup (value);
245
static struct grub_gui_component_ops image_ops =
247
.destroy = image_destroy,
248
.get_id = image_get_id,
249
.is_instance = image_is_instance,
250
.paint = image_paint,
251
.set_parent = image_set_parent,
252
.get_parent = image_get_parent,
253
.set_bounds = image_set_bounds,
254
.get_bounds = image_get_bounds,
255
.get_minimal_size = image_get_minimal_size,
256
.set_property = image_set_property
260
grub_gui_image_new (void)
262
grub_gui_image_t image;
263
image = grub_zalloc (sizeof (*image));
266
image->component.ops = &image_ops;
267
return (grub_gui_component_t) image;