gui_circular_progress.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* gui_circular_process.c - GUI circular progress indicator component. */
  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/gfxmenu_view.h>
  25. #include <grub/gfxwidgets.h>
  26. #include <grub/trig.h>
  27. struct grub_gui_circular_progress
  28. {
  29. struct grub_gui_progress progress;
  30. grub_gui_container_t parent;
  31. grub_video_rect_t bounds;
  32. char *id;
  33. int visible;
  34. int start;
  35. int end;
  36. int value;
  37. int num_ticks;
  38. int start_angle;
  39. int ticks_disappear;
  40. char *theme_dir;
  41. int need_to_load_pixmaps;
  42. char *center_file;
  43. char *tick_file;
  44. struct grub_video_bitmap *center_bitmap;
  45. struct grub_video_bitmap *tick_bitmap;
  46. };
  47. typedef struct grub_gui_circular_progress *circular_progress_t;
  48. static void
  49. circprog_destroy (void *vself)
  50. {
  51. circular_progress_t self = vself;
  52. grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
  53. grub_free (self);
  54. }
  55. static const char *
  56. circprog_get_id (void *vself)
  57. {
  58. circular_progress_t self = vself;
  59. return self->id;
  60. }
  61. static int
  62. circprog_is_instance (void *vself __attribute__((unused)), const char *type)
  63. {
  64. return grub_strcmp (type, "component") == 0;
  65. }
  66. static struct grub_video_bitmap *
  67. load_bitmap (const char *dir, const char *file)
  68. {
  69. struct grub_video_bitmap *bitmap;
  70. char *abspath;
  71. /* Check arguments. */
  72. if (! dir || ! file)
  73. return 0;
  74. /* Resolve to an absolute path. */
  75. abspath = grub_resolve_relative_path (dir, file);
  76. if (! abspath)
  77. return 0;
  78. /* Load the image. */
  79. grub_errno = GRUB_ERR_NONE;
  80. grub_video_bitmap_load (&bitmap, abspath);
  81. grub_errno = GRUB_ERR_NONE;
  82. grub_free (abspath);
  83. return bitmap;
  84. }
  85. static int
  86. check_pixmaps (circular_progress_t self)
  87. {
  88. if (self->need_to_load_pixmaps)
  89. {
  90. if (self->center_bitmap)
  91. grub_video_bitmap_destroy (self->center_bitmap);
  92. self->center_bitmap = load_bitmap (self->theme_dir, self->center_file);
  93. self->tick_bitmap = load_bitmap (self->theme_dir, self->tick_file);
  94. self->need_to_load_pixmaps = 0;
  95. }
  96. return (self->center_bitmap != 0 && self->tick_bitmap != 0);
  97. }
  98. static void
  99. circprog_paint (void *vself, const grub_video_rect_t *region)
  100. {
  101. circular_progress_t self = vself;
  102. if (! self->visible)
  103. return;
  104. if (!grub_video_have_common_points (region, &self->bounds))
  105. return;
  106. if (! check_pixmaps (self))
  107. return;
  108. grub_video_rect_t vpsave;
  109. grub_gui_set_viewport (&self->bounds, &vpsave);
  110. int width = self->bounds.width;
  111. int height = self->bounds.height;
  112. int center_width = grub_video_bitmap_get_width (self->center_bitmap);
  113. int center_height = grub_video_bitmap_get_height (self->center_bitmap);
  114. int tick_width = grub_video_bitmap_get_width (self->tick_bitmap);
  115. int tick_height = grub_video_bitmap_get_height (self->tick_bitmap);
  116. grub_video_blit_bitmap (self->center_bitmap, GRUB_VIDEO_BLIT_BLEND,
  117. (width - center_width) / 2,
  118. (height - center_height) / 2, 0, 0,
  119. center_width, center_height);
  120. int radius = width / 2 - tick_width / 2 - 1;
  121. int nticks;
  122. int tick_begin;
  123. int tick_end;
  124. if (self->end == self->start)
  125. nticks = 0;
  126. else
  127. nticks = (self->num_ticks
  128. * (self->value - self->start)
  129. / (self->end - self->start));
  130. /* Do ticks appear or disappear as the value approached the end? */
  131. if (self->ticks_disappear)
  132. {
  133. tick_begin = nticks;
  134. tick_end = self->num_ticks - 1;
  135. }
  136. else
  137. {
  138. tick_begin = 0;
  139. tick_end = nticks - 1;
  140. }
  141. int i;
  142. for (i = tick_begin; i < tick_end; i++)
  143. {
  144. int x;
  145. int y;
  146. int angle;
  147. /* Calculate the location of the tick. */
  148. angle = self->start_angle + i * GRUB_TRIG_ANGLE_MAX / self->num_ticks;
  149. x = width / 2 + (grub_cos (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
  150. y = height / 2 + (grub_sin (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
  151. /* Adjust (x,y) so the tick is centered. */
  152. x -= tick_width / 2;
  153. y -= tick_height / 2;
  154. /* Draw the tick. */
  155. grub_video_blit_bitmap (self->tick_bitmap, GRUB_VIDEO_BLIT_BLEND,
  156. x, y, 0, 0, tick_width, tick_height);
  157. }
  158. grub_gui_restore_viewport (&vpsave);
  159. }
  160. static void
  161. circprog_set_parent (void *vself, grub_gui_container_t parent)
  162. {
  163. circular_progress_t self = vself;
  164. self->parent = parent;
  165. }
  166. static grub_gui_container_t
  167. circprog_get_parent (void *vself)
  168. {
  169. circular_progress_t self = vself;
  170. return self->parent;
  171. }
  172. static void
  173. circprog_set_bounds (void *vself, const grub_video_rect_t *bounds)
  174. {
  175. circular_progress_t self = vself;
  176. self->bounds = *bounds;
  177. }
  178. static void
  179. circprog_get_bounds (void *vself, grub_video_rect_t *bounds)
  180. {
  181. circular_progress_t self = vself;
  182. *bounds = self->bounds;
  183. }
  184. static void
  185. circprog_set_state (void *vself, int visible, int start,
  186. int current, int end)
  187. {
  188. circular_progress_t self = vself;
  189. self->visible = visible;
  190. self->start = start;
  191. self->value = current;
  192. self->end = end;
  193. }
  194. static grub_err_t
  195. circprog_set_property (void *vself, const char *name, const char *value)
  196. {
  197. circular_progress_t self = vself;
  198. if (grub_strcmp (name, "num_ticks") == 0)
  199. {
  200. self->num_ticks = grub_strtol (value, 0, 10);
  201. }
  202. else if (grub_strcmp (name, "start_angle") == 0)
  203. {
  204. self->start_angle = grub_strtol (value, 0, 10);
  205. }
  206. else if (grub_strcmp (name, "ticks_disappear") == 0)
  207. {
  208. self->ticks_disappear = grub_strcmp (value, "false") != 0;
  209. }
  210. else if (grub_strcmp (name, "center_bitmap") == 0)
  211. {
  212. self->need_to_load_pixmaps = 1;
  213. grub_free (self->center_file);
  214. self->center_file = value ? grub_strdup (value) : 0;
  215. }
  216. else if (grub_strcmp (name, "tick_bitmap") == 0)
  217. {
  218. self->need_to_load_pixmaps = 1;
  219. grub_free (self->tick_file);
  220. self->tick_file = value ? grub_strdup (value) : 0;
  221. }
  222. else if (grub_strcmp (name, "theme_dir") == 0)
  223. {
  224. self->need_to_load_pixmaps = 1;
  225. grub_free (self->theme_dir);
  226. self->theme_dir = value ? grub_strdup (value) : 0;
  227. }
  228. else if (grub_strcmp (name, "id") == 0)
  229. {
  230. grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
  231. grub_free (self->id);
  232. if (value)
  233. self->id = grub_strdup (value);
  234. else
  235. self->id = 0;
  236. if (self->id && grub_strcmp (self->id, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID)
  237. == 0)
  238. grub_gfxmenu_timeout_register ((grub_gui_component_t) self,
  239. circprog_set_state);
  240. }
  241. return grub_errno;
  242. }
  243. static struct grub_gui_component_ops circprog_ops =
  244. {
  245. .destroy = circprog_destroy,
  246. .get_id = circprog_get_id,
  247. .is_instance = circprog_is_instance,
  248. .paint = circprog_paint,
  249. .set_parent = circprog_set_parent,
  250. .get_parent = circprog_get_parent,
  251. .set_bounds = circprog_set_bounds,
  252. .get_bounds = circprog_get_bounds,
  253. .set_property = circprog_set_property
  254. };
  255. static struct grub_gui_progress_ops circprog_prog_ops =
  256. {
  257. .set_state = circprog_set_state
  258. };
  259. grub_gui_component_t
  260. grub_gui_circular_progress_new (void)
  261. {
  262. circular_progress_t self;
  263. self = grub_zalloc (sizeof (*self));
  264. if (! self)
  265. return 0;
  266. self->progress.ops = &circprog_prog_ops;
  267. self->progress.component.ops = &circprog_ops;
  268. self->visible = 1;
  269. self->num_ticks = 64;
  270. self->start_angle = -64;
  271. return (grub_gui_component_t) self;
  272. }