gui.h 7.6 KB

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