1 /* gui_label.c - GUI component to display a line of text. */
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/>.
21 #include <grub/misc.h>
23 #include <grub/font.h>
24 #include <grub/gui_string_util.h>
25 #include <grub/i18n.h>
27 static const char *align_options
[] =
43 struct grub_gui_component comp
;
45 grub_gui_container_t parent
;
46 grub_video_rect_t bounds
;
52 grub_video_rgba_color_t color
;
54 enum align_mode align
;
57 typedef struct grub_gui_label
*grub_gui_label_t
;
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);
70 label_get_id (void *vself
)
72 grub_gui_label_t self
= vself
;
77 label_is_instance (void *vself
__attribute__((unused
)), const char *type
)
79 return grub_strcmp (type
, "component") == 0;
83 label_paint (void *vself
, const grub_video_rect_t
*region
)
85 grub_gui_label_t self
= vself
;
90 if (!grub_video_have_common_points (region
, &self
->bounds
))
93 /* Calculate the starting x coordinate. */
95 if (self
->align
== align_left
)
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
));
104 return; /* Invalid alignment. */
106 if (left_x
< 0 || left_x
> (int) self
->bounds
.width
)
109 grub_video_rect_t vpsave
;
110 grub_gui_set_viewport (&self
->bounds
, &vpsave
);
111 grub_font_draw_string (self
->text
,
113 grub_video_map_rgba_color (self
->color
),
115 grub_font_get_ascent (self
->font
));
116 grub_gui_restore_viewport (&vpsave
);
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
;
134 label_set_bounds (void *vself
, const grub_video_rect_t
*bounds
)
136 grub_gui_label_t self
= vself
;
137 self
->bounds
= *bounds
;
141 label_get_bounds (void *vself
, grub_video_rect_t
*bounds
)
143 grub_gui_label_t self
= vself
;
144 *bounds
= self
->bounds
;
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
));
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
);
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);
177 self
->template = NULL
;
178 self
->text
= grub_strdup ("");
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)
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. */
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
);
226 self
->id
= grub_strdup (value
);
229 if (self
->id
&& grub_strcmp (self
->id
, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID
)
231 grub_gfxmenu_timeout_register ((grub_gui_component_t
) self
,
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
252 grub_gui_label_new (void)
254 grub_gui_label_t label
;
255 label
= grub_zalloc (sizeof (*label
));
258 label
->comp
.ops
= &label_ops
;
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
;