2N/A/* widget_box.c - Pixmap-stylized box widget. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/types.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/err.h>
2N/A#include <grub/video.h>
2N/A#include <grub/bitmap.h>
2N/A#include <grub/bitmap_scale.h>
2N/A#include <grub/gfxwidgets.h>
2N/A
2N/Aenum box_pixmaps
2N/A{
2N/A BOX_PIXMAP_NW, BOX_PIXMAP_NE, BOX_PIXMAP_SE, BOX_PIXMAP_SW,
2N/A BOX_PIXMAP_N, BOX_PIXMAP_E, BOX_PIXMAP_S, BOX_PIXMAP_W,
2N/A BOX_PIXMAP_CENTER
2N/A};
2N/A
2N/Astatic const char *box_pixmap_names[] = {
2N/A /* Corners: */
2N/A "nw", "ne", "se", "sw",
2N/A /* Sides: */
2N/A "n", "e", "s", "w",
2N/A /* Center: */
2N/A "c"
2N/A};
2N/A
2N/A#define BOX_NUM_PIXMAPS (sizeof(box_pixmap_names)/sizeof(*box_pixmap_names))
2N/A
2N/Astatic int
2N/Aget_height (struct grub_video_bitmap *bitmap)
2N/A{
2N/A if (bitmap)
2N/A return grub_video_bitmap_get_height (bitmap);
2N/A else
2N/A return 0;
2N/A}
2N/A
2N/Astatic int
2N/Aget_width (struct grub_video_bitmap *bitmap)
2N/A{
2N/A if (bitmap)
2N/A return grub_video_bitmap_get_width (bitmap);
2N/A else
2N/A return 0;
2N/A}
2N/A
2N/Astatic void
2N/Ablit (grub_gfxmenu_box_t self, int pixmap_index, int x, int y)
2N/A{
2N/A struct grub_video_bitmap *bitmap;
2N/A bitmap = self->scaled_pixmaps[pixmap_index];
2N/A if (! bitmap)
2N/A return;
2N/A grub_video_blit_bitmap (bitmap, GRUB_VIDEO_BLIT_BLEND,
2N/A x, y, 0, 0,
2N/A grub_video_bitmap_get_width (bitmap),
2N/A grub_video_bitmap_get_height (bitmap));
2N/A}
2N/A
2N/Astatic void
2N/Adraw (grub_gfxmenu_box_t self, int x, int y)
2N/A{
2N/A int height_n;
2N/A int width_w;
2N/A
2N/A height_n = get_height (self->scaled_pixmaps[BOX_PIXMAP_N]);
2N/A width_w = get_width (self->scaled_pixmaps[BOX_PIXMAP_W]);
2N/A
2N/A /* Draw sides. */
2N/A blit (self, BOX_PIXMAP_N, x + width_w, y);
2N/A blit (self, BOX_PIXMAP_S, x + width_w, y + height_n + self->content_height);
2N/A blit (self, BOX_PIXMAP_E, x + width_w + self->content_width, y + height_n);
2N/A blit (self, BOX_PIXMAP_W, x, y + height_n);
2N/A
2N/A /* Draw corners. */
2N/A blit (self, BOX_PIXMAP_NW, x, y);
2N/A blit (self, BOX_PIXMAP_NE, x + width_w + self->content_width, y);
2N/A blit (self, BOX_PIXMAP_SE,
2N/A x + width_w + self->content_width,
2N/A y + height_n + self->content_height);
2N/A blit (self, BOX_PIXMAP_SW, x, y + height_n + self->content_height);
2N/A
2N/A /* Draw center. */
2N/A blit (self, BOX_PIXMAP_CENTER, x + width_w, y + height_n);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Ascale_pixmap (grub_gfxmenu_box_t self, int i, int w, int h)
2N/A{
2N/A struct grub_video_bitmap **scaled = &self->scaled_pixmaps[i];
2N/A struct grub_video_bitmap *raw = self->raw_pixmaps[i];
2N/A
2N/A if (raw == 0)
2N/A return grub_errno;
2N/A
2N/A if (w == -1)
2N/A w = grub_video_bitmap_get_width (raw);
2N/A if (h == -1)
2N/A h = grub_video_bitmap_get_height (raw);
2N/A
2N/A if (*scaled == 0
2N/A || ((int) grub_video_bitmap_get_width (*scaled) != w)
2N/A || ((int) grub_video_bitmap_get_height (*scaled) != h))
2N/A {
2N/A if (*scaled)
2N/A {
2N/A grub_video_bitmap_destroy (*scaled);
2N/A *scaled = 0;
2N/A }
2N/A
2N/A /* Don't try to create a bitmap with a zero dimension. */
2N/A if (w != 0 && h != 0)
2N/A grub_video_bitmap_create_scaled (scaled, w, h, raw,
2N/A GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
2N/A if (grub_errno != GRUB_ERR_NONE)
2N/A {
2N/A grub_error_push ();
2N/A grub_error (grub_errno,
2N/A "failed to scale bitmap for styled box pixmap #%d", i);
2N/A }
2N/A }
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic void
2N/Aset_content_size (grub_gfxmenu_box_t self,
2N/A int width, int height)
2N/A{
2N/A self->content_width = width;
2N/A self->content_height = height;
2N/A
2N/A /* Resize sides to match the width and height. */
2N/A /* It is assumed that the corners width/height match the adjacent sides. */
2N/A
2N/A /* Resize N and S sides to match width. */
2N/A if (scale_pixmap(self, BOX_PIXMAP_N, width, -1) != GRUB_ERR_NONE)
2N/A return;
2N/A if (scale_pixmap(self, BOX_PIXMAP_S, width, -1) != GRUB_ERR_NONE)
2N/A return;
2N/A
2N/A /* Resize E and W sides to match height. */
2N/A if (scale_pixmap(self, BOX_PIXMAP_E, -1, height) != GRUB_ERR_NONE)
2N/A return;
2N/A if (scale_pixmap(self, BOX_PIXMAP_W, -1, height) != GRUB_ERR_NONE)
2N/A return;
2N/A
2N/A /* Don't scale the corners--they are assumed to match the sides. */
2N/A if (scale_pixmap(self, BOX_PIXMAP_NW, -1, -1) != GRUB_ERR_NONE)
2N/A return;
2N/A if (scale_pixmap(self, BOX_PIXMAP_SW, -1, -1) != GRUB_ERR_NONE)
2N/A return;
2N/A if (scale_pixmap(self, BOX_PIXMAP_NE, -1, -1) != GRUB_ERR_NONE)
2N/A return;
2N/A if (scale_pixmap(self, BOX_PIXMAP_SE, -1, -1) != GRUB_ERR_NONE)
2N/A return;
2N/A
2N/A /* Scale the center area. */
2N/A if (scale_pixmap(self, BOX_PIXMAP_CENTER, width, height) != GRUB_ERR_NONE)
2N/A return;
2N/A}
2N/A
2N/Astatic int
2N/Aget_border_width (grub_gfxmenu_box_t self)
2N/A{
2N/A return (get_width (self->raw_pixmaps[BOX_PIXMAP_E])
2N/A + get_width (self->raw_pixmaps[BOX_PIXMAP_W]));
2N/A}
2N/A
2N/Astatic int
2N/Aget_left_pad (grub_gfxmenu_box_t self)
2N/A{
2N/A int v, c;
2N/A
2N/A v = get_width (self->raw_pixmaps[BOX_PIXMAP_W]);
2N/A c = get_width (self->raw_pixmaps[BOX_PIXMAP_NW]);
2N/A if (c > v)
2N/A v = c;
2N/A c = get_width (self->raw_pixmaps[BOX_PIXMAP_SW]);
2N/A if (c > v)
2N/A v = c;
2N/A
2N/A return v;
2N/A}
2N/A
2N/Astatic int
2N/Aget_top_pad (grub_gfxmenu_box_t self)
2N/A{
2N/A int v, c;
2N/A
2N/A v = get_height (self->raw_pixmaps[BOX_PIXMAP_N]);
2N/A c = get_height (self->raw_pixmaps[BOX_PIXMAP_NW]);
2N/A if (c > v)
2N/A v = c;
2N/A c = get_height (self->raw_pixmaps[BOX_PIXMAP_NE]);
2N/A if (c > v)
2N/A v = c;
2N/A
2N/A return v;
2N/A}
2N/A
2N/Astatic int
2N/Aget_right_pad (grub_gfxmenu_box_t self)
2N/A{
2N/A int v, c;
2N/A
2N/A v = get_width (self->raw_pixmaps[BOX_PIXMAP_E]);
2N/A c = get_width (self->raw_pixmaps[BOX_PIXMAP_NE]);
2N/A if (c > v)
2N/A v = c;
2N/A c = get_width (self->raw_pixmaps[BOX_PIXMAP_SE]);
2N/A if (c > v)
2N/A v = c;
2N/A
2N/A return v;
2N/A}
2N/A
2N/Astatic int
2N/Aget_bottom_pad (grub_gfxmenu_box_t self)
2N/A{
2N/A int v, c;
2N/A
2N/A v = get_height (self->raw_pixmaps[BOX_PIXMAP_S]);
2N/A c = get_height (self->raw_pixmaps[BOX_PIXMAP_SW]);
2N/A if (c > v)
2N/A v = c;
2N/A c = get_height (self->raw_pixmaps[BOX_PIXMAP_SE]);
2N/A if (c > v)
2N/A v = c;
2N/A
2N/A return v;
2N/A}
2N/A
2N/Astatic void
2N/Adestroy (grub_gfxmenu_box_t self)
2N/A{
2N/A unsigned i;
2N/A for (i = 0; i < BOX_NUM_PIXMAPS; i++)
2N/A {
2N/A if (self->raw_pixmaps[i])
2N/A grub_video_bitmap_destroy(self->raw_pixmaps[i]);
2N/A self->raw_pixmaps[i] = 0;
2N/A
2N/A if (self->scaled_pixmaps[i])
2N/A grub_video_bitmap_destroy(self->scaled_pixmaps[i]);
2N/A self->scaled_pixmaps[i] = 0;
2N/A }
2N/A grub_free (self->raw_pixmaps);
2N/A self->raw_pixmaps = 0;
2N/A grub_free (self->scaled_pixmaps);
2N/A self->scaled_pixmaps = 0;
2N/A
2N/A /* Free self: must be the last step! */
2N/A grub_free (self);
2N/A}
2N/A
2N/A
2N/A/* Create a new box. If PIXMAPS_PREFIX and PIXMAPS_SUFFIX are both non-null,
2N/A then an attempt is made to load the north, south, east, west, northwest,
2N/A northeast, southeast, southwest, and center pixmaps.
2N/A If either PIXMAPS_PREFIX or PIXMAPS_SUFFIX is 0, then no pixmaps are
2N/A loaded, and the box has zero-width borders and is drawn transparent. */
2N/Agrub_gfxmenu_box_t
2N/Agrub_gfxmenu_create_box (const char *pixmaps_prefix,
2N/A const char *pixmaps_suffix)
2N/A{
2N/A unsigned i;
2N/A grub_gfxmenu_box_t box;
2N/A
2N/A box = (grub_gfxmenu_box_t) grub_malloc (sizeof (*box));
2N/A if (! box)
2N/A return 0;
2N/A
2N/A box->content_width = 0;
2N/A box->content_height = 0;
2N/A box->raw_pixmaps =
2N/A (struct grub_video_bitmap **)
2N/A grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
2N/A box->scaled_pixmaps =
2N/A (struct grub_video_bitmap **)
2N/A grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
2N/A
2N/A /* Initialize all pixmap pointers to NULL so that proper destruction can
2N/A be performed if an error is encountered partway through construction. */
2N/A for (i = 0; i < BOX_NUM_PIXMAPS; i++)
2N/A box->raw_pixmaps[i] = 0;
2N/A for (i = 0; i < BOX_NUM_PIXMAPS; i++)
2N/A box->scaled_pixmaps[i] = 0;
2N/A
2N/A /* Load the pixmaps. */
2N/A for (i = 0; i < BOX_NUM_PIXMAPS; i++)
2N/A {
2N/A if (pixmaps_prefix && pixmaps_suffix)
2N/A {
2N/A char *path;
2N/A char *path_end;
2N/A
2N/A path = grub_malloc (grub_strlen (pixmaps_prefix)
2N/A + grub_strlen (box_pixmap_names[i])
2N/A + grub_strlen (pixmaps_suffix)
2N/A + 1);
2N/A if (! path)
2N/A goto fail_and_destroy;
2N/A
2N/A /* Construct the specific path for this pixmap. */
2N/A path_end = grub_stpcpy (path, pixmaps_prefix);
2N/A path_end = grub_stpcpy (path_end, box_pixmap_names[i]);
2N/A path_end = grub_stpcpy (path_end, pixmaps_suffix);
2N/A
2N/A grub_video_bitmap_load (&box->raw_pixmaps[i], path);
2N/A grub_free (path);
2N/A
2N/A /* Ignore missing pixmaps. */
2N/A grub_errno = GRUB_ERR_NONE;
2N/A }
2N/A }
2N/A
2N/A box->draw = draw;
2N/A box->set_content_size = set_content_size;
2N/A box->get_border_width = get_border_width;
2N/A
2N/A box->get_left_pad = get_left_pad;
2N/A box->get_top_pad = get_top_pad;
2N/A box->get_right_pad = get_right_pad;
2N/A box->get_bottom_pad = get_bottom_pad;
2N/A box->destroy = destroy;
2N/A return box;
2N/A
2N/Afail_and_destroy:
2N/A destroy (box);
2N/A return 0;
2N/A}