gui_circular_progress.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. unsigned 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. if (self->num_ticks)
  121. {
  122. int radius = grub_min (height, width) / 2 - grub_max (tick_height, tick_width) / 2 - 1;
  123. unsigned nticks;
  124. unsigned tick_begin;
  125. unsigned tick_end;
  126. if (self->end <= self->start
  127. || self->value <= self->start)
  128. nticks = 0;
  129. else
  130. nticks = ((unsigned) (self->num_ticks
  131. * (self->value - self->start)))
  132. / ((unsigned) (self->end - self->start));
  133. /* Do ticks appear or disappear as the value approached the end? */
  134. if (self->ticks_disappear)
  135. {
  136. tick_begin = nticks;
  137. tick_end = self->num_ticks;
  138. }
  139. else
  140. {
  141. tick_begin = 0;
  142. tick_end = nticks;
  143. }
  144. unsigned i;
  145. for (i = tick_begin; i < tick_end; i++)
  146. {
  147. int x;
  148. int y;
  149. int angle;
  150. /* Calculate the location of the tick. */
  151. angle = self->start_angle
  152. + i * GRUB_TRIG_ANGLE_MAX / self->num_ticks;
  153. x = width / 2 + (grub_cos (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
  154. y = height / 2 + (grub_sin (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
  155. /* Adjust (x,y) so the tick is centered. */
  156. x -= tick_width / 2;
  157. y -= tick_height / 2;
  158. /* Draw the tick. */
  159. grub_video_blit_bitmap (self->tick_bitmap, GRUB_VIDEO_BLIT_BLEND,
  160. x, y, 0, 0, tick_width, tick_height);
  161. }
  162. }
  163. grub_gui_restore_viewport (&vpsave);
  164. }
  165. static void
  166. circprog_set_parent (void *vself, grub_gui_container_t parent)
  167. {
  168. circular_progress_t self = vself;
  169. self->parent = parent;
  170. }
  171. static grub_gui_container_t
  172. circprog_get_parent (void *vself)
  173. {
  174. circular_progress_t self = vself;
  175. return self->parent;
  176. }
  177. static void
  178. circprog_set_bounds (void *vself, const grub_video_rect_t *bounds)
  179. {
  180. circular_progress_t self = vself;
  181. self->bounds = *bounds;
  182. }
  183. static void
  184. circprog_get_bounds (void *vself, grub_video_rect_t *bounds)
  185. {
  186. circular_progress_t self = vself;
  187. *bounds = self->bounds;
  188. }
  189. static void
  190. circprog_set_state (void *vself, int visible, int start,
  191. int current, int end)
  192. {
  193. circular_progress_t self = vself;
  194. self->visible = visible;
  195. self->start = start;
  196. self->value = current;
  197. self->end = end;
  198. }
  199. static int
  200. parse_angle (const char *value)
  201. {
  202. const char *ptr;
  203. int angle;
  204. angle = grub_strtol (value, &ptr, 10);
  205. if (grub_errno)
  206. return 0;
  207. while (grub_isspace (*ptr))
  208. ptr++;
  209. if (grub_strcmp (ptr, "deg") == 0
  210. /* Unicode symbol of degrees (a circle, U+b0). Put here in UTF-8 to
  211. avoid potential problem with text file reesncoding */
  212. || grub_strcmp (ptr, "\xc2\xb0") == 0)
  213. angle = grub_divide_round (angle * 64, 90);
  214. return angle;
  215. }
  216. static grub_err_t
  217. circprog_set_property (void *vself, const char *name, const char *value)
  218. {
  219. circular_progress_t self = vself;
  220. if (grub_strcmp (name, "num_ticks") == 0)
  221. {
  222. self->num_ticks = grub_strtoul (value, 0, 10);
  223. }
  224. else if (grub_strcmp (name, "start_angle") == 0)
  225. {
  226. self->start_angle = parse_angle (value);
  227. }
  228. else if (grub_strcmp (name, "ticks_disappear") == 0)
  229. {
  230. self->ticks_disappear = grub_strcmp (value, "false") != 0;
  231. }
  232. else if (grub_strcmp (name, "center_bitmap") == 0)
  233. {
  234. self->need_to_load_pixmaps = 1;
  235. grub_free (self->center_file);
  236. self->center_file = value ? grub_strdup (value) : 0;
  237. }
  238. else if (grub_strcmp (name, "tick_bitmap") == 0)
  239. {
  240. self->need_to_load_pixmaps = 1;
  241. grub_free (self->tick_file);
  242. self->tick_file = value ? grub_strdup (value) : 0;
  243. }
  244. else if (grub_strcmp (name, "theme_dir") == 0)
  245. {
  246. self->need_to_load_pixmaps = 1;
  247. grub_free (self->theme_dir);
  248. self->theme_dir = value ? grub_strdup (value) : 0;
  249. }
  250. else if (grub_strcmp (name, "id") == 0)
  251. {
  252. grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
  253. grub_free (self->id);
  254. if (value)
  255. self->id = grub_strdup (value);
  256. else
  257. self->id = 0;
  258. if (self->id && grub_strcmp (self->id, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID)
  259. == 0)
  260. grub_gfxmenu_timeout_register ((grub_gui_component_t) self,
  261. circprog_set_state);
  262. }
  263. return grub_errno;
  264. }
  265. static struct grub_gui_component_ops circprog_ops =
  266. {
  267. .destroy = circprog_destroy,
  268. .get_id = circprog_get_id,
  269. .is_instance = circprog_is_instance,
  270. .paint = circprog_paint,
  271. .set_parent = circprog_set_parent,
  272. .get_parent = circprog_get_parent,
  273. .set_bounds = circprog_set_bounds,
  274. .get_bounds = circprog_get_bounds,
  275. .set_property = circprog_set_property
  276. };
  277. static struct grub_gui_progress_ops circprog_prog_ops =
  278. {
  279. .set_state = circprog_set_state
  280. };
  281. grub_gui_component_t
  282. grub_gui_circular_progress_new (void)
  283. {
  284. circular_progress_t self;
  285. self = grub_zalloc (sizeof (*self));
  286. if (! self)
  287. return 0;
  288. self->progress.ops = &circprog_prog_ops;
  289. self->progress.component.ops = &circprog_ops;
  290. self->visible = 1;
  291. self->num_ticks = 64;
  292. self->start_angle = -64;
  293. return (grub_gui_component_t) self;
  294. }