gui_label.c 6.2 KB

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