~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to grub-core/gfxmenu/gui_image.c

Tags: upstream-1.99~20101122
ImportĀ upstreamĀ versionĀ 1.99~20101122

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
20
#include <grub/mm.h>
 
21
#include <grub/misc.h>
 
22
#include <grub/gui.h>
 
23
#include <grub/gui_string_util.h>
 
24
#include <grub/bitmap.h>
 
25
#include <grub/bitmap_scale.h>
 
26
 
 
27
struct grub_gui_image
 
28
{
 
29
  struct grub_gui_component component;
 
30
 
 
31
  grub_gui_container_t parent;
 
32
  grub_video_rect_t bounds;
 
33
  char *id;
 
34
  char *theme_dir;
 
35
  struct grub_video_bitmap *raw_bitmap;
 
36
  struct grub_video_bitmap *bitmap;
 
37
};
 
38
 
 
39
typedef struct grub_gui_image *grub_gui_image_t;
 
40
 
 
41
static void
 
42
image_destroy (void *vself)
 
43
{
 
44
  grub_gui_image_t self = vself;
 
45
 
 
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);
 
49
  if (self->raw_bitmap)
 
50
    grub_video_bitmap_destroy (self->raw_bitmap);
 
51
 
 
52
  grub_free (self);
 
53
}
 
54
 
 
55
static const char *
 
56
image_get_id (void *vself)
 
57
{
 
58
  grub_gui_image_t self = vself;
 
59
  return self->id;
 
60
}
 
61
 
 
62
static int
 
63
image_is_instance (void *vself __attribute__((unused)), const char *type)
 
64
{
 
65
  return grub_strcmp (type, "component") == 0;
 
66
}
 
67
 
 
68
static void
 
69
image_paint (void *vself, const grub_video_rect_t *region)
 
70
{
 
71
  grub_gui_image_t self = vself;
 
72
  grub_video_rect_t vpsave;
 
73
 
 
74
  if (! self->bitmap)
 
75
    return;
 
76
  if (!grub_video_have_common_points (region, &self->bounds))
 
77
    return;
 
78
 
 
79
  grub_gui_set_viewport (&self->bounds, &vpsave);
 
80
  grub_video_blit_bitmap (self->bitmap, GRUB_VIDEO_BLIT_BLEND,
 
81
                          0, 0, 0, 0,
 
82
                          grub_video_bitmap_get_width (self->bitmap),
 
83
                          grub_video_bitmap_get_height (self->bitmap));
 
84
  grub_gui_restore_viewport (&vpsave);
 
85
}
 
86
 
 
87
static void
 
88
image_set_parent (void *vself, grub_gui_container_t parent)
 
89
{
 
90
  grub_gui_image_t self = vself;
 
91
  self->parent = parent;
 
92
}
 
93
 
 
94
static grub_gui_container_t
 
95
image_get_parent (void *vself)
 
96
{
 
97
  grub_gui_image_t self = vself;
 
98
  return self->parent;
 
99
}
 
100
 
 
101
static grub_err_t
 
102
rescale_image (grub_gui_image_t self)
 
103
{
 
104
  if (! self->raw_bitmap)
 
105
    {
 
106
      if (self->bitmap)
 
107
        {
 
108
          grub_video_bitmap_destroy (self->bitmap);
 
109
          self->bitmap = 0;
 
110
        }
 
111
      return grub_errno;
 
112
    }
 
113
 
 
114
  unsigned width = self->bounds.width;
 
115
  unsigned height = self->bounds.height;
 
116
 
 
117
  if (self->bitmap
 
118
      && (grub_video_bitmap_get_width (self->bitmap) == width)
 
119
      && (grub_video_bitmap_get_height (self->bitmap) == height))
 
120
    {
 
121
      /* Nothing to do; already the right size.  */
 
122
      return grub_errno;
 
123
    }
 
124
 
 
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);
 
129
 
 
130
  self->bitmap = 0;
 
131
 
 
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)
 
136
    {
 
137
      self->bitmap = self->raw_bitmap;
 
138
      return grub_errno;
 
139
    }
 
140
 
 
141
  /* Don't scale to an invalid size.  */
 
142
  if (width == 0 || height == 0)
 
143
    return grub_errno;
 
144
 
 
145
  /* Create the scaled bitmap.  */
 
146
  grub_video_bitmap_create_scaled (&self->bitmap,
 
147
                                   width,
 
148
                                   height,
 
149
                                   self->raw_bitmap,
 
150
                                   GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
 
151
  if (grub_errno != GRUB_ERR_NONE)
 
152
    {
 
153
      grub_error_push ();
 
154
      grub_error (grub_errno, "failed to scale bitmap for image component");
 
155
    }
 
156
  return grub_errno;
 
157
}
 
158
 
 
159
static void
 
160
image_set_bounds (void *vself, const grub_video_rect_t *bounds)
 
161
{
 
162
  grub_gui_image_t self = vself;
 
163
  self->bounds = *bounds;
 
164
  rescale_image (self);
 
165
}
 
166
 
 
167
static void
 
168
image_get_bounds (void *vself, grub_video_rect_t *bounds)
 
169
{
 
170
  grub_gui_image_t self = vself;
 
171
  *bounds = self->bounds;
 
172
}
 
173
 
 
174
/* FIXME: inform rendering system it's not forced minimum.  */
 
175
static void
 
176
image_get_minimal_size (void *vself, unsigned *width, unsigned *height)
 
177
{
 
178
  grub_gui_image_t self = vself;
 
179
 
 
180
  if (self->raw_bitmap)
 
181
    {
 
182
      *width = grub_video_bitmap_get_width (self->raw_bitmap);
 
183
      *height = grub_video_bitmap_get_height (self->raw_bitmap);
 
184
    }
 
185
  else
 
186
    {
 
187
      *width = 0;
 
188
      *height = 0;
 
189
    }
 
190
}
 
191
 
 
192
static grub_err_t
 
193
load_image (grub_gui_image_t self, const char *path)
 
194
{
 
195
  struct grub_video_bitmap *bitmap;
 
196
  if (grub_video_bitmap_load (&bitmap, path) != GRUB_ERR_NONE)
 
197
    return grub_errno;
 
198
 
 
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);
 
203
 
 
204
  self->raw_bitmap = bitmap;
 
205
  return rescale_image (self);
 
206
}
 
207
 
 
208
static grub_err_t
 
209
image_set_property (void *vself, const char *name, const char *value)
 
210
{
 
211
  grub_gui_image_t self = vself;
 
212
  if (grub_strcmp (name, "theme_dir") == 0)
 
213
    {
 
214
      grub_free (self->theme_dir);
 
215
      self->theme_dir = grub_strdup (value);
 
216
    }
 
217
  else if (grub_strcmp (name, "file") == 0)
 
218
    {
 
219
      char *absvalue;
 
220
      grub_err_t err;
 
221
 
 
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);
 
226
      if (! absvalue)
 
227
        return grub_errno;
 
228
 
 
229
      err = load_image (self, absvalue);
 
230
      grub_free (absvalue);
 
231
 
 
232
      return err;
 
233
    }
 
234
  else if (grub_strcmp (name, "id") == 0)
 
235
    {
 
236
      grub_free (self->id);
 
237
      if (value)
 
238
        self->id = grub_strdup (value);
 
239
      else
 
240
        self->id = 0;
 
241
    }
 
242
  return grub_errno;
 
243
}
 
244
 
 
245
static struct grub_gui_component_ops image_ops =
 
246
{
 
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
 
257
};
 
258
 
 
259
grub_gui_component_t
 
260
grub_gui_image_new (void)
 
261
{
 
262
  grub_gui_image_t image;
 
263
  image = grub_zalloc (sizeof (*image));
 
264
  if (! image)
 
265
    return 0;
 
266
  image->component.ops = &image_ops;
 
267
  return (grub_gui_component_t) image;
 
268
}
 
269