gui_progress_bar.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* gui_progress_bar.c - GUI progress bar 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/i18n.h>
  27. #include <grub/color.h>
  28. struct grub_gui_progress_bar
  29. {
  30. struct grub_gui_progress progress;
  31. grub_gui_container_t parent;
  32. grub_video_rect_t bounds;
  33. char *id;
  34. int visible;
  35. int start;
  36. int end;
  37. int value;
  38. char *template;
  39. grub_font_t font;
  40. grub_video_rgba_color_t text_color;
  41. grub_video_rgba_color_t border_color;
  42. grub_video_rgba_color_t bg_color;
  43. grub_video_rgba_color_t fg_color;
  44. char *theme_dir;
  45. int need_to_recreate_pixmaps;
  46. int pixmapbar_available;
  47. char *bar_pattern;
  48. char *highlight_pattern;
  49. grub_gfxmenu_box_t bar_box;
  50. grub_gfxmenu_box_t highlight_box;
  51. int highlight_overlay;
  52. };
  53. typedef struct grub_gui_progress_bar *grub_gui_progress_bar_t;
  54. static void
  55. progress_bar_destroy (void *vself)
  56. {
  57. grub_gui_progress_bar_t self = vself;
  58. grub_free (self->theme_dir);
  59. grub_free (self->template);
  60. grub_free (self->id);
  61. grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
  62. grub_free (self);
  63. }
  64. static const char *
  65. progress_bar_get_id (void *vself)
  66. {
  67. grub_gui_progress_bar_t self = vself;
  68. return self->id;
  69. }
  70. static int
  71. progress_bar_is_instance (void *vself __attribute__((unused)), const char *type)
  72. {
  73. return grub_strcmp (type, "component") == 0;
  74. }
  75. static int
  76. check_pixmaps (grub_gui_progress_bar_t self)
  77. {
  78. if (!self->pixmapbar_available)
  79. return 0;
  80. if (self->need_to_recreate_pixmaps)
  81. {
  82. grub_gui_recreate_box (&self->bar_box,
  83. self->bar_pattern,
  84. self->theme_dir);
  85. grub_gui_recreate_box (&self->highlight_box,
  86. self->highlight_pattern,
  87. self->theme_dir);
  88. self->need_to_recreate_pixmaps = 0;
  89. }
  90. return (self->bar_box != 0 && self->highlight_box != 0);
  91. }
  92. static void
  93. draw_filled_rect_bar (grub_gui_progress_bar_t self)
  94. {
  95. /* Set the progress bar's frame. */
  96. grub_video_rect_t f;
  97. f.x = 1;
  98. f.y = 1;
  99. f.width = self->bounds.width - 2;
  100. f.height = self->bounds.height - 2;
  101. /* Border. */
  102. grub_video_fill_rect (grub_video_map_rgba_color (self->border_color),
  103. f.x - 1, f.y - 1,
  104. f.width + 2, f.height + 2);
  105. /* Bar background. */
  106. unsigned barwidth;
  107. if (self->end <= self->start
  108. || self->value <= self->start)
  109. barwidth = 0;
  110. else
  111. barwidth = (f.width
  112. * (self->value - self->start)
  113. / (self->end - self->start));
  114. grub_video_fill_rect (grub_video_map_rgba_color (self->bg_color),
  115. f.x + barwidth, f.y,
  116. f.width - barwidth, f.height);
  117. /* Bar foreground. */
  118. grub_video_fill_rect (grub_video_map_rgba_color (self->fg_color),
  119. f.x, f.y,
  120. barwidth, f.height);
  121. }
  122. static void
  123. draw_pixmap_bar (grub_gui_progress_bar_t self)
  124. {
  125. grub_gfxmenu_box_t bar = self->bar_box;
  126. grub_gfxmenu_box_t hl = self->highlight_box;
  127. int w = self->bounds.width;
  128. int h = self->bounds.height;
  129. int bar_l_pad = bar->get_left_pad (bar);
  130. int bar_r_pad = bar->get_right_pad (bar);
  131. int bar_t_pad = bar->get_top_pad (bar);
  132. int bar_b_pad = bar->get_bottom_pad (bar);
  133. int bar_h_pad = bar_l_pad + bar_r_pad;
  134. int bar_v_pad = bar_t_pad + bar_b_pad;
  135. int hl_l_pad = hl->get_left_pad (hl);
  136. int hl_r_pad = hl->get_right_pad (hl);
  137. int hl_t_pad = hl->get_top_pad (hl);
  138. int hl_b_pad = hl->get_bottom_pad (hl);
  139. int hl_h_pad = hl_l_pad + hl_r_pad;
  140. int hl_v_pad = hl_t_pad + hl_b_pad;
  141. int tracklen = w - bar_h_pad;
  142. int trackheight = h - bar_v_pad;
  143. int barwidth;
  144. int hlheight = trackheight;
  145. int hlx = bar_l_pad;
  146. int hly = bar_t_pad;
  147. bar->set_content_size (bar, tracklen, trackheight);
  148. bar->draw (bar, 0, 0);
  149. if (self->highlight_overlay)
  150. {
  151. tracklen += hl_h_pad;
  152. hlx -= hl_l_pad;
  153. hly -= hl_t_pad;
  154. }
  155. else
  156. hlheight -= hl_v_pad;
  157. if (self->value <= self->start
  158. || self->end <= self->start)
  159. barwidth = 0;
  160. else
  161. barwidth = ((unsigned) (tracklen * (self->value - self->start))
  162. / ((unsigned) (self->end - self->start)));
  163. if (barwidth >= hl_h_pad)
  164. {
  165. hl->set_content_size (hl, barwidth - hl_h_pad, hlheight);
  166. hl->draw (hl, hlx, hly);
  167. }
  168. }
  169. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  170. static void
  171. draw_text (grub_gui_progress_bar_t self)
  172. {
  173. if (self->template)
  174. {
  175. grub_font_t font = self->font;
  176. grub_video_color_t text_color =
  177. grub_video_map_rgba_color (self->text_color);
  178. int width = self->bounds.width;
  179. int height = self->bounds.height;
  180. char *text;
  181. text = grub_xasprintf (self->template,
  182. self->value > 0 ? self->value : -self->value);
  183. if (!text)
  184. {
  185. grub_print_error ();
  186. grub_errno = GRUB_ERR_NONE;
  187. return;
  188. }
  189. /* Center the text. */
  190. int text_width = grub_font_get_string_width (font, text);
  191. int x = (width - text_width) / 2;
  192. int y = ((height - grub_font_get_descent (font)) / 2
  193. + grub_font_get_ascent (font) / 2);
  194. grub_font_draw_string (text, font, text_color, x, y);
  195. grub_free (text);
  196. }
  197. }
  198. #pragma GCC diagnostic error "-Wformat-nonliteral"
  199. static void
  200. progress_bar_paint (void *vself, const grub_video_rect_t *region)
  201. {
  202. grub_gui_progress_bar_t self = vself;
  203. grub_video_rect_t vpsave;
  204. if (! self->visible)
  205. return;
  206. if (!grub_video_have_common_points (region, &self->bounds))
  207. return;
  208. if (self->end == self->start)
  209. return;
  210. grub_gui_set_viewport (&self->bounds, &vpsave);
  211. if (check_pixmaps (self))
  212. draw_pixmap_bar (self);
  213. else
  214. draw_filled_rect_bar (self);
  215. draw_text (self);
  216. grub_gui_restore_viewport (&vpsave);
  217. }
  218. static void
  219. progress_bar_set_parent (void *vself, grub_gui_container_t parent)
  220. {
  221. grub_gui_progress_bar_t self = vself;
  222. self->parent = parent;
  223. }
  224. static grub_gui_container_t
  225. progress_bar_get_parent (void *vself)
  226. {
  227. grub_gui_progress_bar_t self = vself;
  228. return self->parent;
  229. }
  230. static void
  231. progress_bar_set_bounds (void *vself, const grub_video_rect_t *bounds)
  232. {
  233. grub_gui_progress_bar_t self = vself;
  234. self->bounds = *bounds;
  235. }
  236. static void
  237. progress_bar_get_bounds (void *vself, grub_video_rect_t *bounds)
  238. {
  239. grub_gui_progress_bar_t self = vself;
  240. *bounds = self->bounds;
  241. }
  242. static void
  243. progress_bar_get_minimal_size (void *vself,
  244. unsigned *width, unsigned *height)
  245. {
  246. unsigned min_width = 0;
  247. unsigned min_height = 0;
  248. grub_gui_progress_bar_t self = vself;
  249. if (self->template)
  250. {
  251. min_width = grub_font_get_string_width (self->font, self->template);
  252. min_width += grub_font_get_string_width (self->font, "XXXXXXXXXX");
  253. min_height = grub_font_get_descent (self->font)
  254. + grub_font_get_ascent (self->font);
  255. }
  256. if (check_pixmaps (self))
  257. {
  258. grub_gfxmenu_box_t bar = self->bar_box;
  259. grub_gfxmenu_box_t hl = self->highlight_box;
  260. min_width += bar->get_left_pad (bar) + bar->get_right_pad (bar);
  261. min_height += bar->get_top_pad (bar) + bar->get_bottom_pad (bar);
  262. if (!self->highlight_overlay)
  263. {
  264. min_width += hl->get_left_pad (hl) + hl->get_right_pad (hl);
  265. min_height += hl->get_top_pad (hl) + hl->get_bottom_pad (hl);
  266. }
  267. }
  268. else
  269. {
  270. min_height += 2;
  271. min_width += 2;
  272. }
  273. *width = 200;
  274. if (*width < min_width)
  275. *width = min_width;
  276. *height = 28;
  277. if (*height < min_height)
  278. *height = min_height;
  279. }
  280. static void
  281. progress_bar_set_state (void *vself, int visible, int start,
  282. int current, int end)
  283. {
  284. grub_gui_progress_bar_t self = vself;
  285. self->visible = visible;
  286. self->start = start;
  287. self->value = current;
  288. self->end = end;
  289. }
  290. static grub_err_t
  291. progress_bar_set_property (void *vself, const char *name, const char *value)
  292. {
  293. grub_gui_progress_bar_t self = vself;
  294. if (grub_strcmp (name, "text") == 0)
  295. {
  296. grub_free (self->template);
  297. if (grub_strcmp (value, "@TIMEOUT_NOTIFICATION_LONG@") == 0)
  298. value
  299. = _("The highlighted entry will be executed automatically in %ds.");
  300. else if (grub_strcmp (value, "@TIMEOUT_NOTIFICATION_MIDDLE@") == 0)
  301. /* TRANSLATORS: 's' stands for seconds.
  302. It's a standalone timeout notification.
  303. Please use the short form in your language. */
  304. value = _("%ds remaining.");
  305. else if (grub_strcmp (value, "@TIMEOUT_NOTIFICATION_SHORT@") == 0)
  306. /* TRANSLATORS: 's' stands for seconds.
  307. It's a standalone timeout notification.
  308. Please use the shortest form available in you language. */
  309. value = _("%ds");
  310. if (grub_printf_fmt_check(value, "%d") != GRUB_ERR_NONE)
  311. value = ""; /* Unsupported format. */
  312. self->template = grub_strdup (value);
  313. }
  314. else if (grub_strcmp (name, "font") == 0)
  315. {
  316. self->font = grub_font_get (value);
  317. }
  318. else if (grub_strcmp (name, "text_color") == 0)
  319. {
  320. grub_video_parse_color (value, &self->text_color);
  321. }
  322. else if (grub_strcmp (name, "border_color") == 0)
  323. {
  324. grub_video_parse_color (value, &self->border_color);
  325. }
  326. else if (grub_strcmp (name, "bg_color") == 0)
  327. {
  328. grub_video_parse_color (value, &self->bg_color);
  329. }
  330. else if (grub_strcmp (name, "fg_color") == 0)
  331. {
  332. grub_video_parse_color (value, &self->fg_color);
  333. }
  334. else if (grub_strcmp (name, "bar_style") == 0)
  335. {
  336. self->need_to_recreate_pixmaps = 1;
  337. self->pixmapbar_available = 1;
  338. grub_free (self->bar_pattern);
  339. self->bar_pattern = value ? grub_strdup (value) : 0;
  340. }
  341. else if (grub_strcmp (name, "highlight_style") == 0)
  342. {
  343. self->need_to_recreate_pixmaps = 1;
  344. self->pixmapbar_available = 1;
  345. grub_free (self->highlight_pattern);
  346. self->highlight_pattern = value ? grub_strdup (value) : 0;
  347. }
  348. else if (grub_strcmp (name, "highlight_overlay") == 0)
  349. {
  350. self->highlight_overlay = grub_strcmp (value, "true") == 0;
  351. }
  352. else if (grub_strcmp (name, "theme_dir") == 0)
  353. {
  354. self->need_to_recreate_pixmaps = 1;
  355. grub_free (self->theme_dir);
  356. self->theme_dir = value ? grub_strdup (value) : 0;
  357. }
  358. else if (grub_strcmp (name, "id") == 0)
  359. {
  360. grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
  361. grub_free (self->id);
  362. if (value)
  363. self->id = grub_strdup (value);
  364. else
  365. self->id = 0;
  366. if (self->id && grub_strcmp (self->id, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID)
  367. == 0)
  368. grub_gfxmenu_timeout_register ((grub_gui_component_t) self,
  369. progress_bar_set_state);
  370. }
  371. return grub_errno;
  372. }
  373. static struct grub_gui_component_ops progress_bar_ops =
  374. {
  375. .destroy = progress_bar_destroy,
  376. .get_id = progress_bar_get_id,
  377. .is_instance = progress_bar_is_instance,
  378. .paint = progress_bar_paint,
  379. .set_parent = progress_bar_set_parent,
  380. .get_parent = progress_bar_get_parent,
  381. .set_bounds = progress_bar_set_bounds,
  382. .get_bounds = progress_bar_get_bounds,
  383. .get_minimal_size = progress_bar_get_minimal_size,
  384. .set_property = progress_bar_set_property
  385. };
  386. static struct grub_gui_progress_ops progress_bar_pb_ops =
  387. {
  388. .set_state = progress_bar_set_state
  389. };
  390. grub_gui_component_t
  391. grub_gui_progress_bar_new (void)
  392. {
  393. grub_gui_progress_bar_t self;
  394. self = grub_zalloc (sizeof (*self));
  395. if (! self)
  396. return 0;
  397. self->progress.ops = &progress_bar_pb_ops;
  398. self->progress.component.ops = &progress_bar_ops;
  399. self->visible = 1;
  400. self->font = grub_font_get ("Unknown Regular 16");
  401. grub_video_rgba_color_t black = { .red = 0, .green = 0, .blue = 0, .alpha = 255 };
  402. grub_video_rgba_color_t gray = { .red = 128, .green = 128, .blue = 128, .alpha = 255 };
  403. grub_video_rgba_color_t lightgray = { .red = 200, .green = 200, .blue = 200, .alpha = 255 };
  404. self->text_color = black;
  405. self->border_color = black;
  406. self->bg_color = gray;
  407. self->fg_color = lightgray;
  408. self->highlight_overlay = 0;
  409. return (grub_gui_component_t) self;
  410. }