/* gui_label.c - GUI component to display a line of text. */ /* * 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 #include static const char *align_options[] = { "left", "center", "right", 0 }; enum align_mode { align_left, align_center, align_right }; struct VasEBoot_gui_label { struct VasEBoot_gui_component comp; VasEBoot_gui_container_t parent; VasEBoot_video_rect_t bounds; char *id; int visible; char *text; char *template; VasEBoot_font_t font; VasEBoot_video_rgba_color_t color; int value; enum align_mode align; }; typedef struct VasEBoot_gui_label *VasEBoot_gui_label_t; static void label_destroy (void *vself) { VasEBoot_gui_label_t self = vself; VasEBoot_gfxmenu_timeout_unregister ((VasEBoot_gui_component_t) self); VasEBoot_free (self->text); VasEBoot_free (self->template); VasEBoot_free (self); } static const char * label_get_id (void *vself) { VasEBoot_gui_label_t self = vself; return self->id; } static int label_is_instance (void *vself __attribute__((unused)), const char *type) { return VasEBoot_strcmp (type, "component") == 0; } static void label_paint (void *vself, const VasEBoot_video_rect_t *region) { VasEBoot_gui_label_t self = vself; if (! self->visible) return; if (!VasEBoot_video_have_common_points (region, &self->bounds)) return; /* Calculate the starting x coordinate. */ int left_x; if (self->align == align_left) left_x = 0; else if (self->align == align_center) left_x = (self->bounds.width - VasEBoot_font_get_string_width (self->font, self->text)) / 2; else if (self->align == align_right) left_x = (self->bounds.width - VasEBoot_font_get_string_width (self->font, self->text)); else return; /* Invalid alignment. */ if (left_x < 0 || left_x > (int) self->bounds.width) left_x = 0; VasEBoot_video_rect_t vpsave; VasEBoot_gui_set_viewport (&self->bounds, &vpsave); VasEBoot_font_draw_string (self->text, self->font, VasEBoot_video_map_rgba_color (self->color), left_x, VasEBoot_font_get_ascent (self->font)); VasEBoot_gui_restore_viewport (&vpsave); } static void label_set_parent (void *vself, VasEBoot_gui_container_t parent) { VasEBoot_gui_label_t self = vself; self->parent = parent; } static VasEBoot_gui_container_t label_get_parent (void *vself) { VasEBoot_gui_label_t self = vself; return self->parent; } static void label_set_bounds (void *vself, const VasEBoot_video_rect_t *bounds) { VasEBoot_gui_label_t self = vself; self->bounds = *bounds; } static void label_get_bounds (void *vself, VasEBoot_video_rect_t *bounds) { VasEBoot_gui_label_t self = vself; *bounds = self->bounds; } static void label_get_minimal_size (void *vself, unsigned *width, unsigned *height) { VasEBoot_gui_label_t self = vself; *width = VasEBoot_font_get_string_width (self->font, self->text); *height = (VasEBoot_font_get_ascent (self->font) + VasEBoot_font_get_descent (self->font)); } #pragma GCC diagnostic ignored "-Wformat-nonliteral" static void label_set_state (void *vself, int visible, int start __attribute__ ((unused)), int current, int end __attribute__ ((unused))) { VasEBoot_gui_label_t self = vself; self->value = -current; self->visible = visible; VasEBoot_free (self->text); self->text = VasEBoot_xasprintf (self->template ? : "%d", self->value); } static VasEBoot_err_t label_set_property (void *vself, const char *name, const char *value) { VasEBoot_gui_label_t self = vself; if (VasEBoot_strcmp (name, "text") == 0) { VasEBoot_free (self->text); VasEBoot_free (self->template); if (! value) { self->template = NULL; self->text = VasEBoot_strdup (""); } else { if (VasEBoot_strcmp (value, "@KEYMAP_LONG@") == 0) value = _("Press enter to boot the selected OS, " "`e' to edit the commands before booting " "or `c' for a command-line. ESC to return previous menu."); else if (VasEBoot_strcmp (value, "@KEYMAP_MIDDLE@") == 0) value = _("Press enter to boot the selected OS, " "`e' to edit the commands before booting " "or `c' for a command-line."); else if (VasEBoot_strcmp (value, "@KEYMAP_SHORT@") == 0) value = _("enter: boot, `e': options, `c': cmd-line"); /* FIXME: Add more templates here if needed. */ if (VasEBoot_printf_fmt_check(value, "%d") != VAS_EBOOT_ERR_NONE) value = ""; /* Unsupported format. */ self->template = VasEBoot_strdup (value); self->text = VasEBoot_xasprintf (value, self->value); } } else if (VasEBoot_strcmp (name, "font") == 0) { self->font = VasEBoot_font_get (value); } else if (VasEBoot_strcmp (name, "color") == 0) { VasEBoot_video_parse_color (value, &self->color); } else if (VasEBoot_strcmp (name, "align") == 0) { int i; for (i = 0; align_options[i]; i++) { if (VasEBoot_strcmp (align_options[i], value) == 0) { self->align = i; /* Set the alignment mode. */ break; } } } else if (VasEBoot_strcmp (name, "visible") == 0) { self->visible = VasEBoot_strcmp (value, "false") != 0; } else if (VasEBoot_strcmp (name, "id") == 0) { VasEBoot_gfxmenu_timeout_unregister ((VasEBoot_gui_component_t) self); VasEBoot_free (self->id); if (value) self->id = VasEBoot_strdup (value); else self->id = 0; if (self->id && VasEBoot_strcmp (self->id, VAS_EBOOT_GFXMENU_TIMEOUT_COMPONENT_ID) == 0) VasEBoot_gfxmenu_timeout_register ((VasEBoot_gui_component_t) self, label_set_state); } return VAS_EBOOT_ERR_NONE; } #pragma GCC diagnostic error "-Wformat-nonliteral" static struct VasEBoot_gui_component_ops label_ops = { .destroy = label_destroy, .get_id = label_get_id, .is_instance = label_is_instance, .paint = label_paint, .set_parent = label_set_parent, .get_parent = label_get_parent, .set_bounds = label_set_bounds, .get_bounds = label_get_bounds, .get_minimal_size = label_get_minimal_size, .set_property = label_set_property }; VasEBoot_gui_component_t VasEBoot_gui_label_new (void) { VasEBoot_gui_label_t label; label = VasEBoot_zalloc (sizeof (*label)); if (! label) return 0; label->comp.ops = &label_ops; label->visible = 1; label->text = VasEBoot_strdup (""); label->font = VasEBoot_font_get ("Unknown Regular 16"); label->color.red = 0; label->color.green = 0; label->color.blue = 0; label->color.alpha = 255; label->align = align_left; return (VasEBoot_gui_component_t) label; }