gui_image.c 6.73 KB
Newer Older
Colin D Bennett's avatar
Colin D Bennett committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* gui_image.c - GUI component to display an image.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2008,2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/gui.h>
#include <grub/gui_string_util.h>
#include <grub/bitmap.h>
#include <grub/bitmap_scale.h>

struct grub_gui_image
{
29
  struct grub_gui_component component;
Colin D Bennett's avatar
Colin D Bennett committed
30 31 32 33

  grub_gui_container_t parent;
  grub_video_rect_t bounds;
  char *id;
34
  char *theme_dir;
Colin D Bennett's avatar
Colin D Bennett committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  struct grub_video_bitmap *raw_bitmap;
  struct grub_video_bitmap *bitmap;
};

typedef struct grub_gui_image *grub_gui_image_t;

static void
image_destroy (void *vself)
{
  grub_gui_image_t self = vself;

  /* Free the scaled bitmap, unless it's a reference to the raw bitmap.  */
  if (self->bitmap && (self->bitmap != self->raw_bitmap))
    grub_video_bitmap_destroy (self->bitmap);
  if (self->raw_bitmap)
    grub_video_bitmap_destroy (self->raw_bitmap);

  grub_free (self);
}

static const char *
image_get_id (void *vself)
{
  grub_gui_image_t self = vself;
  return self->id;
}

static int
image_is_instance (void *vself __attribute__((unused)), const char *type)
{
  return grub_strcmp (type, "component") == 0;
}

static void
69
image_paint (void *vself, const grub_video_rect_t *region)
Colin D Bennett's avatar
Colin D Bennett committed
70 71
{
  grub_gui_image_t self = vself;
72 73
  grub_video_rect_t vpsave;

Colin D Bennett's avatar
Colin D Bennett committed
74 75
  if (! self->bitmap)
    return;
76 77 78
  if (!grub_video_have_common_points (region, &self->bounds))
    return;

Colin D Bennett's avatar
Colin D Bennett committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  grub_gui_set_viewport (&self->bounds, &vpsave);
  grub_video_blit_bitmap (self->bitmap, GRUB_VIDEO_BLIT_BLEND,
                          0, 0, 0, 0,
                          grub_video_bitmap_get_width (self->bitmap),
                          grub_video_bitmap_get_height (self->bitmap));
  grub_gui_restore_viewport (&vpsave);
}

static void
image_set_parent (void *vself, grub_gui_container_t parent)
{
  grub_gui_image_t self = vself;
  self->parent = parent;
}

static grub_gui_container_t
image_get_parent (void *vself)
{
  grub_gui_image_t self = vself;
  return self->parent;
}

static grub_err_t
rescale_image (grub_gui_image_t self)
{
104 105 106
  signed width;
  signed height;

Colin D Bennett's avatar
Colin D Bennett committed
107 108 109 110 111 112 113 114 115 116
  if (! self->raw_bitmap)
    {
      if (self->bitmap)
        {
          grub_video_bitmap_destroy (self->bitmap);
          self->bitmap = 0;
        }
      return grub_errno;
    }

117 118
  width = self->bounds.width;
  height = self->bounds.height;
Colin D Bennett's avatar
Colin D Bennett committed
119 120

  if (self->bitmap
121 122
      && ((signed) grub_video_bitmap_get_width (self->bitmap) == width)
      && ((signed) grub_video_bitmap_get_height (self->bitmap) == height))
Colin D Bennett's avatar
Colin D Bennett committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136
    {
      /* Nothing to do; already the right size.  */
      return grub_errno;
    }

  /* Free any old scaled bitmap,
     *unless* it's a reference to the raw bitmap.  */
  if (self->bitmap && (self->bitmap != self->raw_bitmap))
    grub_video_bitmap_destroy (self->bitmap);

  self->bitmap = 0;

  /* Create a scaled bitmap, unless the requested size is the same
     as the raw size -- in that case a reference is made.  */
137 138
  if ((signed) grub_video_bitmap_get_width (self->raw_bitmap) == width
      && (signed) grub_video_bitmap_get_height (self->raw_bitmap) == height)
Colin D Bennett's avatar
Colin D Bennett committed
139 140 141 142 143 144
    {
      self->bitmap = self->raw_bitmap;
      return grub_errno;
    }

  /* Don't scale to an invalid size.  */
145
  if (width <= 0 || height <= 0)
Colin D Bennett's avatar
Colin D Bennett committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    return grub_errno;

  /* Create the scaled bitmap.  */
  grub_video_bitmap_create_scaled (&self->bitmap,
                                   width,
                                   height,
                                   self->raw_bitmap,
                                   GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
  return grub_errno;
}

static void
image_set_bounds (void *vself, const grub_video_rect_t *bounds)
{
  grub_gui_image_t self = vself;
  self->bounds = *bounds;
  rescale_image (self);
}

static void
image_get_bounds (void *vself, grub_video_rect_t *bounds)
{
  grub_gui_image_t self = vself;
  *bounds = self->bounds;
}

172
/* FIXME: inform rendering system it's not forced minimum.  */
Colin D Bennett's avatar
Colin D Bennett committed
173
static void
174
image_get_minimal_size (void *vself, unsigned *width, unsigned *height)
Colin D Bennett's avatar
Colin D Bennett committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{
  grub_gui_image_t self = vself;

  if (self->raw_bitmap)
    {
      *width = grub_video_bitmap_get_width (self->raw_bitmap);
      *height = grub_video_bitmap_get_height (self->raw_bitmap);
    }
  else
    {
      *width = 0;
      *height = 0;
    }
}

static grub_err_t
load_image (grub_gui_image_t self, const char *path)
{
  struct grub_video_bitmap *bitmap;
  if (grub_video_bitmap_load (&bitmap, path) != GRUB_ERR_NONE)
    return grub_errno;

  if (self->bitmap && (self->bitmap != self->raw_bitmap))
    grub_video_bitmap_destroy (self->bitmap);
  if (self->raw_bitmap)
    grub_video_bitmap_destroy (self->raw_bitmap);

  self->raw_bitmap = bitmap;
  return rescale_image (self);
}

static grub_err_t
image_set_property (void *vself, const char *name, const char *value)
{
  grub_gui_image_t self = vself;
210 211 212 213 214 215 216 217 218 219 220 221
  if (grub_strcmp (name, "theme_dir") == 0)
    {
      grub_free (self->theme_dir);
      self->theme_dir = grub_strdup (value);
    }
  else if (grub_strcmp (name, "file") == 0)
    {
      char *absvalue;
      grub_err_t err;

      /* Resolve to an absolute path.  */
      if (! self->theme_dir)
222
	return grub_error (GRUB_ERR_BUG, "unspecified theme_dir");
223 224 225 226 227 228 229 230 231
      absvalue = grub_resolve_relative_path (self->theme_dir, value);
      if (! absvalue)
	return grub_errno;

      err = load_image (self, absvalue);
      grub_free (absvalue);

      return err;
    }
Colin D Bennett's avatar
Colin D Bennett committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  else if (grub_strcmp (name, "id") == 0)
    {
      grub_free (self->id);
      if (value)
        self->id = grub_strdup (value);
      else
        self->id = 0;
    }
  return grub_errno;
}

static struct grub_gui_component_ops image_ops =
{
  .destroy = image_destroy,
  .get_id = image_get_id,
  .is_instance = image_is_instance,
  .paint = image_paint,
  .set_parent = image_set_parent,
  .get_parent = image_get_parent,
  .set_bounds = image_set_bounds,
  .get_bounds = image_get_bounds,
253
  .get_minimal_size = image_get_minimal_size,
Colin D Bennett's avatar
Colin D Bennett committed
254 255 256 257 258 259 260
  .set_property = image_set_property
};

grub_gui_component_t
grub_gui_image_new (void)
{
  grub_gui_image_t image;
261
  image = grub_zalloc (sizeof (*image));
Colin D Bennett's avatar
Colin D Bennett committed
262 263
  if (! image)
    return 0;
264
  image->component.ops = &image_ops;
Colin D Bennett's avatar
Colin D Bennett committed
265 266 267
  return (grub_gui_component_t) image;
}