gui.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* gui.h - GUI components header file. */
  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/types.h>
  20. #include <grub/err.h>
  21. #include <grub/video.h>
  22. #include <grub/bitmap.h>
  23. #include <grub/gfxmenu_view.h>
  24. #include <grub/mm.h>
  25. #ifndef GRUB_GUI_H
  26. #define GRUB_GUI_H 1
  27. /* The component ID identifying GUI components to be updated as the timeout
  28. status changes. */
  29. #define GRUB_GFXMENU_TIMEOUT_COMPONENT_ID "__timeout__"
  30. /* A representation of a color. Unlike grub_video_color_t, this
  31. representation is independent of any video mode specifics. */
  32. typedef struct grub_gui_color
  33. {
  34. grub_uint8_t red;
  35. grub_uint8_t green;
  36. grub_uint8_t blue;
  37. grub_uint8_t alpha;
  38. } grub_gui_color_t;
  39. typedef struct grub_gui_component *grub_gui_component_t;
  40. typedef struct grub_gui_container *grub_gui_container_t;
  41. typedef struct grub_gui_list *grub_gui_list_t;
  42. typedef void (*grub_gui_component_callback) (grub_gui_component_t component,
  43. void *userdata);
  44. /* Component interface. */
  45. struct grub_gui_component_ops
  46. {
  47. void (*destroy) (void *self);
  48. const char * (*get_id) (void *self);
  49. int (*is_instance) (void *self, const char *type);
  50. void (*paint) (void *self, const grub_video_rect_t *bounds);
  51. void (*set_parent) (void *self, grub_gui_container_t parent);
  52. grub_gui_container_t (*get_parent) (void *self);
  53. void (*set_bounds) (void *self, const grub_video_rect_t *bounds);
  54. void (*get_bounds) (void *self, grub_video_rect_t *bounds);
  55. void (*get_minimal_size) (void *self, unsigned *width, unsigned *height);
  56. grub_err_t (*set_property) (void *self, const char *name, const char *value);
  57. void (*repaint) (void *self, int second_pass);
  58. };
  59. struct grub_gui_container_ops
  60. {
  61. void (*add) (void *self, grub_gui_component_t comp);
  62. void (*remove) (void *self, grub_gui_component_t comp);
  63. void (*iterate_children) (void *self,
  64. grub_gui_component_callback cb, void *userdata);
  65. };
  66. struct grub_gui_list_ops
  67. {
  68. void (*set_view_info) (void *self,
  69. grub_gfxmenu_view_t view);
  70. };
  71. struct grub_gui_progress_ops
  72. {
  73. void (*set_state) (void *self, int visible, int start, int current, int end);
  74. };
  75. typedef void (*grub_gfxmenu_set_state_t) (void *self, int visible, int start,
  76. int current, int end);
  77. struct grub_gfxmenu_timeout_notify
  78. {
  79. struct grub_gfxmenu_timeout_notify *next;
  80. grub_gfxmenu_set_state_t set_state;
  81. grub_gui_component_t self;
  82. };
  83. extern struct grub_gfxmenu_timeout_notify *grub_gfxmenu_timeout_notifications;
  84. static inline grub_err_t
  85. grub_gfxmenu_timeout_register (grub_gui_component_t self,
  86. grub_gfxmenu_set_state_t set_state)
  87. {
  88. struct grub_gfxmenu_timeout_notify *ne = grub_malloc (sizeof (*ne));
  89. if (!ne)
  90. return grub_errno;
  91. ne->set_state = set_state;
  92. ne->self = self;
  93. ne->next = grub_gfxmenu_timeout_notifications;
  94. grub_gfxmenu_timeout_notifications = ne;
  95. return GRUB_ERR_NONE;
  96. }
  97. static inline void
  98. grub_gfxmenu_timeout_unregister (grub_gui_component_t self)
  99. {
  100. struct grub_gfxmenu_timeout_notify **p, *q;
  101. for (p = &grub_gfxmenu_timeout_notifications, q = *p;
  102. q; p = &(q->next), q = q->next)
  103. if (q->self == self)
  104. {
  105. *p = q->next;
  106. break;
  107. }
  108. }
  109. typedef signed grub_fixed_signed_t;
  110. #define GRUB_FIXED_1 0x10000
  111. static inline signed
  112. grub_fixed_sfs_divide (signed a, grub_fixed_signed_t b)
  113. {
  114. return (a * GRUB_FIXED_1) / b;
  115. }
  116. static inline grub_fixed_signed_t
  117. grub_fixed_fsf_divide (grub_fixed_signed_t a, signed b)
  118. {
  119. return a / b;
  120. }
  121. static inline signed
  122. grub_fixed_sfs_multiply (signed a, grub_fixed_signed_t b)
  123. {
  124. return (a * b) / GRUB_FIXED_1;
  125. }
  126. static inline signed
  127. grub_fixed_to_signed (grub_fixed_signed_t in)
  128. {
  129. return in / GRUB_FIXED_1;
  130. }
  131. static inline grub_fixed_signed_t
  132. grub_signed_to_fixed (signed in)
  133. {
  134. return in * GRUB_FIXED_1;
  135. }
  136. struct grub_gui_component
  137. {
  138. struct grub_gui_component_ops *ops;
  139. signed x;
  140. grub_fixed_signed_t xfrac;
  141. signed y;
  142. grub_fixed_signed_t yfrac;
  143. signed w;
  144. grub_fixed_signed_t wfrac;
  145. signed h;
  146. grub_fixed_signed_t hfrac;
  147. };
  148. struct grub_gui_progress
  149. {
  150. struct grub_gui_component component;
  151. struct grub_gui_progress_ops *ops;
  152. };
  153. struct grub_gui_container
  154. {
  155. struct grub_gui_component component;
  156. struct grub_gui_container_ops *ops;
  157. };
  158. struct grub_gui_list
  159. {
  160. struct grub_gui_component component;
  161. struct grub_gui_list_ops *ops;
  162. };
  163. /* Interfaces to concrete component classes. */
  164. grub_gui_container_t grub_gui_canvas_new (void);
  165. grub_gui_container_t grub_gui_vbox_new (void);
  166. grub_gui_container_t grub_gui_hbox_new (void);
  167. grub_gui_component_t grub_gui_label_new (void);
  168. grub_gui_component_t grub_gui_image_new (void);
  169. grub_gui_component_t grub_gui_progress_bar_new (void);
  170. grub_gui_component_t grub_gui_list_new (void);
  171. grub_gui_component_t grub_gui_circular_progress_new (void);
  172. /* Manipulation functions. */
  173. /* Visit all components with the specified ID. */
  174. void grub_gui_find_by_id (grub_gui_component_t root,
  175. const char *id,
  176. grub_gui_component_callback cb,
  177. void *userdata);
  178. /* Visit all components. */
  179. void grub_gui_iterate_recursively (grub_gui_component_t root,
  180. grub_gui_component_callback cb,
  181. void *userdata);
  182. /* Helper functions. */
  183. static __inline void
  184. grub_gui_save_viewport (grub_video_rect_t *r)
  185. {
  186. grub_video_get_viewport ((unsigned *) &r->x,
  187. (unsigned *) &r->y,
  188. (unsigned *) &r->width,
  189. (unsigned *) &r->height);
  190. }
  191. static __inline void
  192. grub_gui_restore_viewport (const grub_video_rect_t *r)
  193. {
  194. grub_video_set_viewport (r->x, r->y, r->width, r->height);
  195. }
  196. /* Set a new viewport relative the the current one, saving the current
  197. viewport in OLD so it can be later restored. */
  198. static __inline void
  199. grub_gui_set_viewport (const grub_video_rect_t *r, grub_video_rect_t *old)
  200. {
  201. grub_gui_save_viewport (old);
  202. grub_video_set_viewport (old->x + r->x,
  203. old->y + r->y,
  204. r->width,
  205. r->height);
  206. }
  207. static __inline grub_gui_color_t
  208. grub_gui_color_rgb (int r, int g, int b)
  209. {
  210. grub_gui_color_t c;
  211. c.red = r;
  212. c.green = g;
  213. c.blue = b;
  214. c.alpha = 255;
  215. return c;
  216. }
  217. static __inline grub_video_color_t
  218. grub_gui_map_color (grub_gui_color_t c)
  219. {
  220. return grub_video_map_rgba (c.red, c.green, c.blue, c.alpha);
  221. }
  222. static inline int
  223. grub_video_have_common_points (const grub_video_rect_t *a,
  224. const grub_video_rect_t *b)
  225. {
  226. if (!((a->x <= b->x && b->x <= a->x + a->width)
  227. || (b->x <= a->x && a->x <= b->x + b->width)))
  228. return 0;
  229. if (!((a->y <= b->y && b->y <= a->y + a->height)
  230. || (b->y <= a->y && a->y <= b->y + b->height)))
  231. return 0;
  232. return 1;
  233. }
  234. #endif /* ! GRUB_GUI_H */