gui_label.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/mm.h>
  20. #include <grub/misc.h>
  21. #include <grub/gui.h>
  22. #include <grub/font.h>
  23. #include <grub/gui_string_util.h>
  24. #include <grub/i18n.h>
  25. #include <grub/color.h>
  26. static const char *align_options[] =
  27. {
  28. "left",
  29. "center",
  30. "right",
  31. 0
  32. };
  33. enum align_mode {
  34. align_left,
  35. align_center,
  36. align_right
  37. };
  38. struct grub_gui_label
  39. {
  40. struct grub_gui_component comp;
  41. grub_gui_container_t parent;
  42. grub_video_rect_t bounds;
  43. char *id;
  44. int visible;
  45. char *text;
  46. char *template;
  47. grub_font_t font;
  48. grub_video_rgba_color_t color;
  49. int value;
  50. enum align_mode align;
  51. };
  52. typedef struct grub_gui_label *grub_gui_label_t;
  53. static void
  54. label_destroy (void *vself)
  55. {
  56. grub_gui_label_t self = vself;
  57. grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
  58. grub_free (self->text);
  59. grub_free (self->template);
  60. grub_free (self);
  61. }
  62. static const char *
  63. label_get_id (void *vself)
  64. {
  65. grub_gui_label_t self = vself;
  66. return self->id;
  67. }
  68. static int
  69. label_is_instance (void *vself __attribute__((unused)), const char *type)
  70. {
  71. return grub_strcmp (type, "component") == 0;
  72. }
  73. static void
  74. label_paint (void *vself, const grub_video_rect_t *region)
  75. {
  76. grub_gui_label_t self = vself;
  77. if (! self->visible)
  78. return;
  79. if (!grub_video_have_common_points (region, &self->bounds))
  80. return;
  81. /* Calculate the starting x coordinate. */
  82. int left_x;
  83. if (self->align == align_left)
  84. left_x = 0;
  85. else if (self->align == align_center)
  86. left_x = (self->bounds.width
  87. - grub_font_get_string_width (self->font, self->text)) / 2;
  88. else if (self->align == align_right)
  89. left_x = (self->bounds.width
  90. - grub_font_get_string_width (self->font, self->text));
  91. else
  92. return; /* Invalid alignment. */
  93. if (left_x < 0 || left_x > (int) self->bounds.width)
  94. left_x = 0;
  95. grub_video_rect_t vpsave;
  96. grub_gui_set_viewport (&self->bounds, &vpsave);
  97. grub_font_draw_string (self->text,
  98. self->font,
  99. grub_video_map_rgba_color (self->color),
  100. left_x,
  101. grub_font_get_ascent (self->font));
  102. grub_gui_restore_viewport (&vpsave);
  103. }
  104. static void
  105. label_set_parent (void *vself, grub_gui_container_t parent)
  106. {
  107. grub_gui_label_t self = vself;
  108. self->parent = parent;
  109. }
  110. static grub_gui_container_t
  111. label_get_parent (void *vself)
  112. {
  113. grub_gui_label_t self = vself;
  114. return self->parent;
  115. }
  116. static void
  117. label_set_bounds (void *vself, const grub_video_rect_t *bounds)
  118. {
  119. grub_gui_label_t self = vself;
  120. self->bounds = *bounds;
  121. }
  122. static void
  123. label_get_bounds (void *vself, grub_video_rect_t *bounds)
  124. {
  125. grub_gui_label_t self = vself;
  126. *bounds = self->bounds;
  127. }
  128. static void
  129. label_get_minimal_size (void *vself, unsigned *width, unsigned *height)
  130. {
  131. grub_gui_label_t self = vself;
  132. *width = grub_font_get_string_width (self->font, self->text);
  133. *height = (grub_font_get_ascent (self->font)
  134. + grub_font_get_descent (self->font));
  135. }
  136. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  137. static void
  138. label_set_state (void *vself, int visible, int start __attribute__ ((unused)),
  139. int current, int end __attribute__ ((unused)))
  140. {
  141. grub_gui_label_t self = vself;
  142. self->value = -current;
  143. self->visible = visible;
  144. grub_free (self->text);
  145. self->text = grub_xasprintf (self->template ? : "%d", self->value);
  146. }
  147. static grub_err_t
  148. label_set_property (void *vself, const char *name, const char *value)
  149. {
  150. grub_gui_label_t self = vself;
  151. if (grub_strcmp (name, "text") == 0)
  152. {
  153. grub_free (self->text);
  154. grub_free (self->template);
  155. if (! value)
  156. {
  157. self->template = NULL;
  158. self->text = grub_strdup ("");
  159. }
  160. else
  161. {
  162. if (grub_strcmp (value, "@KEYMAP_LONG@") == 0)
  163. value = _("Press enter to boot the selected OS, "
  164. "`e' to edit the commands before booting "
  165. "or `c' for a command-line. ESC to return previous menu.");
  166. else if (grub_strcmp (value, "@KEYMAP_MIDDLE@") == 0)
  167. value = _("Press enter to boot the selected OS, "
  168. "`e' to edit the commands before booting "
  169. "or `c' for a command-line.");
  170. else if (grub_strcmp (value, "@KEYMAP_SHORT@") == 0)
  171. value = _("enter: boot, `e': options, `c': cmd-line");
  172. /* FIXME: Add more templates here if needed. */
  173. self->template = grub_strdup (value);
  174. self->text = grub_xasprintf (value, self->value);
  175. }
  176. }
  177. else if (grub_strcmp (name, "font") == 0)
  178. {
  179. self->font = grub_font_get (value);
  180. }
  181. else if (grub_strcmp (name, "color") == 0)
  182. {
  183. grub_video_parse_color (value, &self->color);
  184. }
  185. else if (grub_strcmp (name, "align") == 0)
  186. {
  187. int i;
  188. for (i = 0; align_options[i]; i++)
  189. {
  190. if (grub_strcmp (align_options[i], value) == 0)
  191. {
  192. self->align = i; /* Set the alignment mode. */
  193. break;
  194. }
  195. }
  196. }
  197. else if (grub_strcmp (name, "visible") == 0)
  198. {
  199. self->visible = grub_strcmp (value, "false") != 0;
  200. }
  201. else if (grub_strcmp (name, "id") == 0)
  202. {
  203. grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
  204. grub_free (self->id);
  205. if (value)
  206. self->id = grub_strdup (value);
  207. else
  208. self->id = 0;
  209. if (self->id && grub_strcmp (self->id, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID)
  210. == 0)
  211. grub_gfxmenu_timeout_register ((grub_gui_component_t) self,
  212. label_set_state);
  213. }
  214. return GRUB_ERR_NONE;
  215. }
  216. #pragma GCC diagnostic error "-Wformat-nonliteral"
  217. static struct grub_gui_component_ops label_ops =
  218. {
  219. .destroy = label_destroy,
  220. .get_id = label_get_id,
  221. .is_instance = label_is_instance,
  222. .paint = label_paint,
  223. .set_parent = label_set_parent,
  224. .get_parent = label_get_parent,
  225. .set_bounds = label_set_bounds,
  226. .get_bounds = label_get_bounds,
  227. .get_minimal_size = label_get_minimal_size,
  228. .set_property = label_set_property
  229. };
  230. grub_gui_component_t
  231. grub_gui_label_new (void)
  232. {
  233. grub_gui_label_t label;
  234. label = grub_zalloc (sizeof (*label));
  235. if (! label)
  236. return 0;
  237. label->comp.ops = &label_ops;
  238. label->visible = 1;
  239. label->text = grub_strdup ("");
  240. label->font = grub_font_get ("Unknown Regular 16");
  241. label->color.red = 0;
  242. label->color.green = 0;
  243. label->color.blue = 0;
  244. label->color.alpha = 255;
  245. label->align = align_left;
  246. return (grub_gui_component_t) label;
  247. }