Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / gfxmenu / gui_label.c
blob637578f77a702fc5b673207c589e54dbd2532c64
1 /* gui_label.c - GUI component to display a line of text. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/mm.h>
21 #include <grub/misc.h>
22 #include <grub/gui.h>
23 #include <grub/font.h>
24 #include <grub/gui_string_util.h>
25 #include <grub/i18n.h>
27 static const char *align_options[] =
29 "left",
30 "center",
31 "right",
35 enum align_mode {
36 align_left,
37 align_center,
38 align_right
41 struct grub_gui_label
43 struct grub_gui_component comp;
45 grub_gui_container_t parent;
46 grub_video_rect_t bounds;
47 char *id;
48 int visible;
49 char *text;
50 char *template;
51 grub_font_t font;
52 grub_video_rgba_color_t color;
53 int value;
54 enum align_mode align;
57 typedef struct grub_gui_label *grub_gui_label_t;
59 static void
60 label_destroy (void *vself)
62 grub_gui_label_t self = vself;
63 grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
64 grub_free (self->text);
65 grub_free (self->template);
66 grub_free (self);
69 static const char *
70 label_get_id (void *vself)
72 grub_gui_label_t self = vself;
73 return self->id;
76 static int
77 label_is_instance (void *vself __attribute__((unused)), const char *type)
79 return grub_strcmp (type, "component") == 0;
82 static void
83 label_paint (void *vself, const grub_video_rect_t *region)
85 grub_gui_label_t self = vself;
87 if (! self->visible)
88 return;
90 if (!grub_video_have_common_points (region, &self->bounds))
91 return;
93 /* Calculate the starting x coordinate. */
94 int left_x;
95 if (self->align == align_left)
96 left_x = 0;
97 else if (self->align == align_center)
98 left_x = (self->bounds.width
99 - grub_font_get_string_width (self->font, self->text)) / 2;
100 else if (self->align == align_right)
101 left_x = (self->bounds.width
102 - grub_font_get_string_width (self->font, self->text));
103 else
104 return; /* Invalid alignment. */
106 if (left_x < 0 || left_x > (int) self->bounds.width)
107 left_x = 0;
109 grub_video_rect_t vpsave;
110 grub_gui_set_viewport (&self->bounds, &vpsave);
111 grub_font_draw_string (self->text,
112 self->font,
113 grub_video_map_rgba_color (self->color),
114 left_x,
115 grub_font_get_ascent (self->font));
116 grub_gui_restore_viewport (&vpsave);
119 static void
120 label_set_parent (void *vself, grub_gui_container_t parent)
122 grub_gui_label_t self = vself;
123 self->parent = parent;
126 static grub_gui_container_t
127 label_get_parent (void *vself)
129 grub_gui_label_t self = vself;
130 return self->parent;
133 static void
134 label_set_bounds (void *vself, const grub_video_rect_t *bounds)
136 grub_gui_label_t self = vself;
137 self->bounds = *bounds;
140 static void
141 label_get_bounds (void *vself, grub_video_rect_t *bounds)
143 grub_gui_label_t self = vself;
144 *bounds = self->bounds;
147 static void
148 label_get_minimal_size (void *vself, unsigned *width, unsigned *height)
150 grub_gui_label_t self = vself;
151 *width = grub_font_get_string_width (self->font, self->text);
152 *height = (grub_font_get_ascent (self->font)
153 + grub_font_get_descent (self->font));
156 static void
157 label_set_state (void *vself, int visible, int start __attribute__ ((unused)),
158 int current, int end __attribute__ ((unused)))
160 grub_gui_label_t self = vself;
161 self->value = -current;
162 self->visible = visible;
163 grub_free (self->text);
164 self->text = grub_xasprintf (self->template ? : "%d", self->value);
167 static grub_err_t
168 label_set_property (void *vself, const char *name, const char *value)
170 grub_gui_label_t self = vself;
171 if (grub_strcmp (name, "text") == 0)
173 grub_free (self->text);
174 grub_free (self->template);
175 if (! value)
177 self->template = NULL;
178 self->text = grub_strdup ("");
180 else
182 if (grub_strcmp (value, "@KEYMAP_LONG@") == 0)
183 value = _("Press enter to boot the selected OS, "
184 "`e' to edit the commands before booting "
185 "or `c' for a command-line. ESC to return previous menu.");
186 else if (grub_strcmp (value, "@KEYMAP_MIDDLE@") == 0)
187 value = _("Press enter to boot the selected OS, "
188 "`e' to edit the commands before booting "
189 "or `c' for a command-line.");
190 else if (grub_strcmp (value, "@KEYMAP_SHORT@") == 0)
191 value = _("enter: boot, `e': options, `c': cmd-line");
192 /* FIXME: Add more templates here if needed. */
193 self->template = grub_strdup (value);
194 self->text = grub_xasprintf (value, self->value);
197 else if (grub_strcmp (name, "font") == 0)
199 self->font = grub_font_get (value);
201 else if (grub_strcmp (name, "color") == 0)
203 grub_video_parse_color (value, &self->color);
205 else if (grub_strcmp (name, "align") == 0)
207 int i;
208 for (i = 0; align_options[i]; i++)
210 if (grub_strcmp (align_options[i], value) == 0)
212 self->align = i; /* Set the alignment mode. */
213 break;
217 else if (grub_strcmp (name, "visible") == 0)
219 self->visible = grub_strcmp (value, "false") != 0;
221 else if (grub_strcmp (name, "id") == 0)
223 grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
224 grub_free (self->id);
225 if (value)
226 self->id = grub_strdup (value);
227 else
228 self->id = 0;
229 if (self->id && grub_strcmp (self->id, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID)
230 == 0)
231 grub_gfxmenu_timeout_register ((grub_gui_component_t) self,
232 label_set_state);
234 return GRUB_ERR_NONE;
237 static struct grub_gui_component_ops label_ops =
239 .destroy = label_destroy,
240 .get_id = label_get_id,
241 .is_instance = label_is_instance,
242 .paint = label_paint,
243 .set_parent = label_set_parent,
244 .get_parent = label_get_parent,
245 .set_bounds = label_set_bounds,
246 .get_bounds = label_get_bounds,
247 .get_minimal_size = label_get_minimal_size,
248 .set_property = label_set_property
251 grub_gui_component_t
252 grub_gui_label_new (void)
254 grub_gui_label_t label;
255 label = grub_zalloc (sizeof (*label));
256 if (! label)
257 return 0;
258 label->comp.ops = &label_ops;
259 label->visible = 1;
260 label->text = grub_strdup ("");
261 label->font = grub_font_get ("Unknown Regular 16");
262 label->color.red = 0;
263 label->color.green = 0;
264 label->color.blue = 0;
265 label->color.alpha = 255;
266 label->align = align_left;
267 return (grub_gui_component_t) label;