/* gui_image.c - GUI component to display an image. */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2008,2009 Free Software Foundation, Inc. * * VAS_EBOOT 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. * * VAS_EBOOT 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 VAS_EBOOT. If not, see . */ #include #include #include #include #include #include struct VasEBoot_gui_image { struct VasEBoot_gui_component component; VasEBoot_gui_container_t parent; VasEBoot_video_rect_t bounds; char *id; char *theme_dir; struct VasEBoot_video_bitmap *raw_bitmap; struct VasEBoot_video_bitmap *bitmap; }; typedef struct VasEBoot_gui_image *VasEBoot_gui_image_t; static void image_destroy (void *vself) { VasEBoot_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)) VasEBoot_video_bitmap_destroy (self->bitmap); if (self->raw_bitmap) VasEBoot_video_bitmap_destroy (self->raw_bitmap); VasEBoot_free (self); } static const char * image_get_id (void *vself) { VasEBoot_gui_image_t self = vself; return self->id; } static int image_is_instance (void *vself __attribute__((unused)), const char *type) { return VasEBoot_strcmp (type, "component") == 0; } static void image_paint (void *vself, const VasEBoot_video_rect_t *region) { VasEBoot_gui_image_t self = vself; VasEBoot_video_rect_t vpsave; if (! self->bitmap) return; if (!VasEBoot_video_have_common_points (region, &self->bounds)) return; VasEBoot_gui_set_viewport (&self->bounds, &vpsave); VasEBoot_video_blit_bitmap (self->bitmap, VAS_EBOOT_VIDEO_BLIT_BLEND, 0, 0, 0, 0, VasEBoot_video_bitmap_get_width (self->bitmap), VasEBoot_video_bitmap_get_height (self->bitmap)); VasEBoot_gui_restore_viewport (&vpsave); } static void image_set_parent (void *vself, VasEBoot_gui_container_t parent) { VasEBoot_gui_image_t self = vself; self->parent = parent; } static VasEBoot_gui_container_t image_get_parent (void *vself) { VasEBoot_gui_image_t self = vself; return self->parent; } static VasEBoot_err_t rescale_image (VasEBoot_gui_image_t self) { signed width; signed height; if (! self->raw_bitmap) { if (self->bitmap) { VasEBoot_video_bitmap_destroy (self->bitmap); self->bitmap = 0; } return VasEBoot_errno; } width = self->bounds.width; height = self->bounds.height; if (self->bitmap && ((signed) VasEBoot_video_bitmap_get_width (self->bitmap) == width) && ((signed) VasEBoot_video_bitmap_get_height (self->bitmap) == height)) { /* Nothing to do; already the right size. */ return VasEBoot_errno; } /* Free any old scaled bitmap, *unless* it's a reference to the raw bitmap. */ if (self->bitmap && (self->bitmap != self->raw_bitmap)) VasEBoot_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. */ if ((signed) VasEBoot_video_bitmap_get_width (self->raw_bitmap) == width && (signed) VasEBoot_video_bitmap_get_height (self->raw_bitmap) == height) { self->bitmap = self->raw_bitmap; return VasEBoot_errno; } /* Don't scale to an invalid size. */ if (width <= 0 || height <= 0) return VasEBoot_errno; /* Create the scaled bitmap. */ VasEBoot_video_bitmap_create_scaled (&self->bitmap, width, height, self->raw_bitmap, VAS_EBOOT_VIDEO_BITMAP_SCALE_METHOD_BEST); return VasEBoot_errno; } static void image_set_bounds (void *vself, const VasEBoot_video_rect_t *bounds) { VasEBoot_gui_image_t self = vself; self->bounds = *bounds; rescale_image (self); } static void image_get_bounds (void *vself, VasEBoot_video_rect_t *bounds) { VasEBoot_gui_image_t self = vself; *bounds = self->bounds; } /* FIXME: inform rendering system it's not forced minimum. */ static void image_get_minimal_size (void *vself, unsigned *width, unsigned *height) { VasEBoot_gui_image_t self = vself; if (self->raw_bitmap) { *width = VasEBoot_video_bitmap_get_width (self->raw_bitmap); *height = VasEBoot_video_bitmap_get_height (self->raw_bitmap); } else { *width = 0; *height = 0; } } static VasEBoot_err_t load_image (VasEBoot_gui_image_t self, const char *path) { struct VasEBoot_video_bitmap *bitmap; if (VasEBoot_video_bitmap_load (&bitmap, path) != VAS_EBOOT_ERR_NONE) return VasEBoot_errno; if (self->bitmap && (self->bitmap != self->raw_bitmap)) VasEBoot_video_bitmap_destroy (self->bitmap); if (self->raw_bitmap) VasEBoot_video_bitmap_destroy (self->raw_bitmap); /* * Either self->bitmap is being freed or it shares memory with * self->raw_bitmap which is being freed. To ensure self->bitmap doesn't * point to memory that has been freed, we can set it to NULL. */ self->bitmap = NULL; self->raw_bitmap = bitmap; return rescale_image (self); } static VasEBoot_err_t image_set_property (void *vself, const char *name, const char *value) { VasEBoot_gui_image_t self = vself; if (VasEBoot_strcmp (name, "theme_dir") == 0) { VasEBoot_free (self->theme_dir); self->theme_dir = VasEBoot_strdup (value); } else if (VasEBoot_strcmp (name, "file") == 0) { char *absvalue; VasEBoot_err_t err; /* Resolve to an absolute path. */ if (! self->theme_dir) return VasEBoot_error (VAS_EBOOT_ERR_BUG, "unspecified theme_dir"); absvalue = VasEBoot_resolve_relative_path (self->theme_dir, value); if (! absvalue) return VasEBoot_errno; err = load_image (self, absvalue); VasEBoot_free (absvalue); return err; } else if (VasEBoot_strcmp (name, "id") == 0) { VasEBoot_free (self->id); if (value) self->id = VasEBoot_strdup (value); else self->id = 0; } return VasEBoot_errno; } static struct VasEBoot_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, .get_minimal_size = image_get_minimal_size, .set_property = image_set_property }; VasEBoot_gui_component_t VasEBoot_gui_image_new (void) { VasEBoot_gui_image_t image; image = VasEBoot_zalloc (sizeof (*image)); if (! image) return 0; image->component.ops = &image_ops; return (VasEBoot_gui_component_t) image; }