gui_list.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /* gui_list.c - GUI component to display a selectable list of items. */
  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/gui_string_util.h>
  23. #include <grub/gfxmenu_view.h>
  24. #include <grub/gfxwidgets.h>
  25. #include <grub/color.h>
  26. enum scrollbar_slice_mode {
  27. SCROLLBAR_SLICE_WEST,
  28. SCROLLBAR_SLICE_CENTER,
  29. SCROLLBAR_SLICE_EAST
  30. };
  31. struct grub_gui_list_impl
  32. {
  33. struct grub_gui_list list;
  34. grub_gui_container_t parent;
  35. grub_video_rect_t bounds;
  36. char *id;
  37. int visible;
  38. int icon_width;
  39. int icon_height;
  40. int item_height;
  41. int item_padding;
  42. int item_icon_space;
  43. int item_spacing;
  44. grub_font_t item_font;
  45. int selected_item_font_inherit;
  46. grub_font_t selected_item_font;
  47. grub_video_rgba_color_t item_color;
  48. int selected_item_color_inherit;
  49. grub_video_rgba_color_t selected_item_color;
  50. int draw_scrollbar;
  51. int need_to_recreate_scrollbar;
  52. char *scrollbar_frame_pattern;
  53. char *scrollbar_thumb_pattern;
  54. grub_gfxmenu_box_t scrollbar_frame;
  55. grub_gfxmenu_box_t scrollbar_thumb;
  56. int scrollbar_thumb_overlay;
  57. int scrollbar_width;
  58. enum scrollbar_slice_mode scrollbar_slice;
  59. int scrollbar_left_pad;
  60. int scrollbar_right_pad;
  61. int scrollbar_top_pad;
  62. int scrollbar_bottom_pad;
  63. int first_shown_index;
  64. int need_to_recreate_boxes;
  65. char *theme_dir;
  66. char *menu_box_pattern;
  67. char *item_box_pattern;
  68. int selected_item_box_pattern_inherit;
  69. char *selected_item_box_pattern;
  70. grub_gfxmenu_box_t menu_box;
  71. grub_gfxmenu_box_t selected_item_box;
  72. grub_gfxmenu_box_t item_box;
  73. grub_gfxmenu_icon_manager_t icon_manager;
  74. grub_gfxmenu_view_t view;
  75. };
  76. typedef struct grub_gui_list_impl *list_impl_t;
  77. static void
  78. list_destroy (void *vself)
  79. {
  80. list_impl_t self = vself;
  81. grub_free (self->theme_dir);
  82. grub_free (self->menu_box_pattern);
  83. grub_free (self->item_box_pattern);
  84. grub_free (self->selected_item_box_pattern);
  85. if (self->menu_box)
  86. self->menu_box->destroy (self->menu_box);
  87. if (self->item_box)
  88. self->item_box->destroy (self->item_box);
  89. if (self->selected_item_box)
  90. self->selected_item_box->destroy (self->selected_item_box);
  91. if (self->icon_manager)
  92. grub_gfxmenu_icon_manager_destroy (self->icon_manager);
  93. if (self->scrollbar_thumb)
  94. self->scrollbar_thumb->destroy (self->scrollbar_thumb);
  95. if (self->scrollbar_frame)
  96. self->scrollbar_frame->destroy (self->scrollbar_frame);
  97. grub_free (self->scrollbar_thumb_pattern);
  98. grub_free (self->scrollbar_frame_pattern);
  99. grub_free (self);
  100. }
  101. static int
  102. get_num_shown_items (list_impl_t self)
  103. {
  104. int boxpad = self->item_padding;
  105. int item_vspace = self->item_spacing;
  106. int item_height = self->item_height;
  107. grub_gfxmenu_box_t box = self->menu_box;
  108. int box_top_pad = box->get_top_pad (box);
  109. int box_bottom_pad = box->get_bottom_pad (box);
  110. grub_gfxmenu_box_t itembox = self->item_box;
  111. grub_gfxmenu_box_t selbox = self->selected_item_box;
  112. int item_top_pad = itembox->get_top_pad (itembox);
  113. int item_bottom_pad = itembox->get_bottom_pad (itembox);
  114. int sel_top_pad = selbox->get_top_pad (selbox);
  115. int sel_bottom_pad = selbox->get_bottom_pad (selbox);
  116. int max_top_pad = grub_max (item_top_pad, sel_top_pad);
  117. int max_bottom_pad = grub_max (item_bottom_pad, sel_bottom_pad);
  118. if (item_height + item_vspace <= 0)
  119. return 1;
  120. return (self->bounds.height + item_vspace - 2 * boxpad
  121. - max_top_pad - max_bottom_pad
  122. - box_top_pad - box_bottom_pad) / (item_height + item_vspace);
  123. }
  124. static int
  125. check_boxes (list_impl_t self)
  126. {
  127. if (self->need_to_recreate_boxes)
  128. {
  129. grub_gui_recreate_box (&self->menu_box,
  130. self->menu_box_pattern,
  131. self->theme_dir);
  132. grub_gui_recreate_box (&self->item_box,
  133. self->item_box_pattern,
  134. self->theme_dir);
  135. grub_gui_recreate_box (&self->selected_item_box,
  136. self->selected_item_box_pattern,
  137. self->theme_dir);
  138. self->need_to_recreate_boxes = 0;
  139. }
  140. return (self->menu_box != 0 && self->selected_item_box != 0
  141. && self->item_box != 0);
  142. }
  143. static int
  144. check_scrollbar (list_impl_t self)
  145. {
  146. if (self->need_to_recreate_scrollbar)
  147. {
  148. grub_gui_recreate_box (&self->scrollbar_frame,
  149. self->scrollbar_frame_pattern,
  150. self->theme_dir);
  151. grub_gui_recreate_box (&self->scrollbar_thumb,
  152. self->scrollbar_thumb_pattern,
  153. self->theme_dir);
  154. self->need_to_recreate_scrollbar = 0;
  155. }
  156. if (self->scrollbar_frame == 0 || self->scrollbar_thumb == 0)
  157. return 0;
  158. /* Sanity checks. */
  159. grub_gfxmenu_box_t frame = self->scrollbar_frame;
  160. grub_gfxmenu_box_t thumb = self->scrollbar_thumb;
  161. grub_gfxmenu_box_t menu = self->menu_box;
  162. int min_width = frame->get_left_pad (frame)
  163. + frame->get_right_pad (frame);
  164. int min_height = frame->get_top_pad (frame)
  165. + frame->get_bottom_pad (frame)
  166. + self->scrollbar_top_pad + self->scrollbar_bottom_pad
  167. + menu->get_top_pad (menu)
  168. + menu->get_bottom_pad (menu);
  169. if (!self->scrollbar_thumb_overlay)
  170. {
  171. min_width += thumb->get_left_pad (thumb)
  172. + thumb->get_right_pad (thumb);
  173. min_height += thumb->get_top_pad (thumb)
  174. + thumb->get_bottom_pad (thumb);
  175. }
  176. if (min_width <= self->scrollbar_width
  177. && min_height <= (int) self->bounds.height)
  178. return 1;
  179. /* Unprintable dimenstions. */
  180. self->draw_scrollbar = 0;
  181. return 0;
  182. }
  183. static const char *
  184. list_get_id (void *vself)
  185. {
  186. list_impl_t self = vself;
  187. return self->id;
  188. }
  189. static int
  190. list_is_instance (void *vself __attribute__((unused)), const char *type)
  191. {
  192. return (grub_strcmp (type, "component") == 0
  193. || grub_strcmp (type, "list") == 0);
  194. }
  195. static struct grub_video_bitmap *
  196. get_item_icon (list_impl_t self, int item_index)
  197. {
  198. grub_menu_entry_t entry;
  199. entry = grub_menu_get_entry (self->view->menu, item_index);
  200. if (! entry)
  201. return 0;
  202. return grub_gfxmenu_icon_manager_get_icon (self->icon_manager, entry);
  203. }
  204. static void
  205. make_selected_item_visible (list_impl_t self)
  206. {
  207. int selected_index = self->view->selected;
  208. if (selected_index < 0)
  209. return; /* No item is selected. */
  210. int num_shown_items = get_num_shown_items (self);
  211. int last_shown_index = self->first_shown_index + (num_shown_items - 1);
  212. if (selected_index < self->first_shown_index)
  213. self->first_shown_index = selected_index;
  214. else if (selected_index > last_shown_index)
  215. self->first_shown_index = selected_index - (num_shown_items - 1);
  216. }
  217. /* Draw a scrollbar on the menu. */
  218. static void
  219. draw_scrollbar (list_impl_t self,
  220. int value, int extent, int min, int max,
  221. int scrollbar_width, int scrollbar_height)
  222. {
  223. unsigned thumby, thumbheight;
  224. grub_gfxmenu_box_t frame = self->scrollbar_frame;
  225. grub_gfxmenu_box_t thumb = self->scrollbar_thumb;
  226. int frame_vertical_pad = (frame->get_top_pad (frame)
  227. + frame->get_bottom_pad (frame));
  228. int frame_horizontal_pad = (frame->get_left_pad (frame)
  229. + frame->get_right_pad (frame));
  230. unsigned thumb_vertical_pad = (thumb->get_top_pad (thumb)
  231. + thumb->get_bottom_pad (thumb));
  232. int thumb_horizontal_pad = (thumb->get_left_pad (thumb)
  233. + thumb->get_right_pad (thumb));
  234. int tracktop = frame->get_top_pad (frame);
  235. unsigned tracklen;
  236. if (scrollbar_height <= frame_vertical_pad)
  237. tracklen = 0;
  238. else
  239. tracklen = scrollbar_height - frame_vertical_pad;
  240. frame->set_content_size (frame,
  241. scrollbar_width - frame_horizontal_pad,
  242. tracklen);
  243. if (self->scrollbar_thumb_overlay)
  244. {
  245. tracklen += thumb_vertical_pad;
  246. tracktop -= thumb->get_top_pad (thumb);
  247. }
  248. if (value <= min || max <= min)
  249. thumby = 0;
  250. else
  251. thumby = ((unsigned) tracklen * (value - min))
  252. / ((unsigned) (max - min));
  253. if (max <= min)
  254. thumbheight = 1;
  255. else
  256. thumbheight = ((unsigned) (tracklen * extent)
  257. / ((unsigned) (max - min))) + 1;
  258. /* Rare occasion: too many entries or too low height. */
  259. if (thumbheight < thumb_vertical_pad)
  260. {
  261. thumbheight = thumb_vertical_pad;
  262. if (value <= min || max <= extent
  263. || tracklen <= thumb_vertical_pad)
  264. thumby = 0;
  265. else
  266. thumby = ((unsigned) ((tracklen - thumb_vertical_pad) * (value - min))
  267. / ((unsigned)(max - extent)));
  268. }
  269. thumby += tracktop;
  270. int thumbx = frame->get_left_pad (frame);
  271. int thumbwidth = scrollbar_width - frame_horizontal_pad;
  272. if (!self->scrollbar_thumb_overlay)
  273. thumbwidth -= thumb_horizontal_pad;
  274. else
  275. thumbx -= thumb->get_left_pad (thumb);
  276. thumb->set_content_size (thumb, thumbwidth,
  277. thumbheight - thumb_vertical_pad);
  278. frame->draw (frame, 0, 0);
  279. thumb->draw (thumb, thumbx, thumby);
  280. }
  281. /* Draw the list of items. */
  282. static void
  283. draw_menu (list_impl_t self, int num_shown_items)
  284. {
  285. if (! self->menu_box || ! self->selected_item_box || ! self->item_box)
  286. return;
  287. int boxpad = self->item_padding;
  288. int icon_text_space = self->item_icon_space;
  289. int item_vspace = self->item_spacing;
  290. int ascent = grub_font_get_ascent (self->item_font);
  291. int descent = grub_font_get_descent (self->item_font);
  292. int selected_ascent = grub_font_get_ascent (self->selected_item_font);
  293. int selected_descent = grub_font_get_descent (self->selected_item_font);
  294. int text_box_height = self->item_height;
  295. make_selected_item_visible (self);
  296. grub_gfxmenu_box_t itembox = self->item_box;
  297. grub_gfxmenu_box_t selbox = self->selected_item_box;
  298. int item_leftpad = itembox->get_left_pad (itembox);
  299. int item_rightpad = itembox->get_right_pad (itembox);
  300. int item_border_width = item_leftpad + item_rightpad;
  301. int item_toppad = itembox->get_top_pad (itembox);
  302. int sel_leftpad = selbox->get_left_pad (selbox);
  303. int sel_rightpad = selbox->get_right_pad (selbox);
  304. int sel_border_width = sel_leftpad + sel_rightpad;
  305. int sel_toppad = selbox->get_top_pad (selbox);
  306. int max_leftpad = grub_max (item_leftpad, sel_leftpad);
  307. int max_toppad = grub_max (item_toppad, sel_toppad);
  308. int item_top = 0;
  309. int menu_index;
  310. int visible_index;
  311. struct grub_video_rect oviewport;
  312. grub_video_get_viewport (&oviewport.x, &oviewport.y,
  313. &oviewport.width, &oviewport.height);
  314. grub_video_set_viewport (oviewport.x + boxpad,
  315. oviewport.y + boxpad,
  316. oviewport.width - 2 * boxpad,
  317. oviewport.height - 2 * boxpad);
  318. int cwidth = oviewport.width - 2 * boxpad;
  319. itembox->set_content_size (itembox, cwidth - item_border_width,
  320. text_box_height);
  321. selbox->set_content_size (selbox, cwidth - sel_border_width,
  322. text_box_height);
  323. int text_left_offset = self->icon_width + icon_text_space;
  324. int item_text_top_offset = (text_box_height - (ascent + descent)) / 2 + ascent;
  325. int sel_text_top_offset = (text_box_height - (selected_ascent
  326. + selected_descent)) / 2
  327. + selected_ascent;
  328. grub_video_rect_t svpsave, sviewport;
  329. sviewport.x = max_leftpad + text_left_offset;
  330. int text_viewport_width = cwidth - sviewport.x;
  331. sviewport.height = text_box_height;
  332. grub_video_color_t item_color;
  333. grub_video_color_t sel_color;
  334. item_color = grub_video_map_rgba_color (self->item_color);
  335. sel_color = grub_video_map_rgba_color (self->selected_item_color);
  336. int item_box_top_offset = max_toppad - item_toppad;
  337. int sel_box_top_offset = max_toppad - sel_toppad;
  338. int item_viewport_width = text_viewport_width - item_rightpad;
  339. int sel_viewport_width = text_viewport_width - sel_rightpad;
  340. int tmp_icon_top_offset = (text_box_height - self->icon_height) / 2;
  341. int item_icon_top_offset = item_toppad + tmp_icon_top_offset;
  342. int sel_icon_top_offset = sel_toppad + tmp_icon_top_offset;
  343. for (visible_index = 0, menu_index = self->first_shown_index;
  344. visible_index < num_shown_items && menu_index < self->view->menu->size;
  345. visible_index++, menu_index++)
  346. {
  347. int is_selected = (menu_index == self->view->selected);
  348. struct grub_video_bitmap *icon;
  349. grub_font_t font;
  350. grub_video_color_t color;
  351. int text_top_offset;
  352. int top_pad;
  353. int icon_top_offset;
  354. int viewport_width;
  355. if (is_selected)
  356. {
  357. selbox->draw (selbox, 0, item_top + sel_box_top_offset);
  358. font = self->selected_item_font;
  359. color = sel_color;
  360. text_top_offset = sel_text_top_offset;
  361. top_pad = sel_toppad;
  362. icon_top_offset = sel_icon_top_offset;
  363. viewport_width = sel_viewport_width;
  364. }
  365. else
  366. {
  367. itembox->draw (itembox, 0, item_top + item_box_top_offset);
  368. font = self->item_font;
  369. color = item_color;
  370. text_top_offset = item_text_top_offset;
  371. top_pad = item_toppad;
  372. icon_top_offset = item_icon_top_offset;
  373. viewport_width = item_viewport_width;
  374. }
  375. icon = get_item_icon (self, menu_index);
  376. if (icon != 0)
  377. grub_video_blit_bitmap (icon, GRUB_VIDEO_BLIT_BLEND,
  378. max_leftpad,
  379. item_top + icon_top_offset,
  380. 0, 0, self->icon_width, self->icon_height);
  381. const char *item_title =
  382. grub_menu_get_entry (self->view->menu, menu_index)->title;
  383. sviewport.y = item_top + top_pad;
  384. sviewport.width = viewport_width;
  385. grub_gui_set_viewport (&sviewport, &svpsave);
  386. grub_font_draw_string (item_title,
  387. font,
  388. color,
  389. 0,
  390. text_top_offset);
  391. grub_gui_restore_viewport (&svpsave);
  392. item_top += text_box_height + item_vspace;
  393. }
  394. grub_video_set_viewport (oviewport.x,
  395. oviewport.y,
  396. oviewport.width,
  397. oviewport.height);
  398. }
  399. static void
  400. list_paint (void *vself, const grub_video_rect_t *region)
  401. {
  402. list_impl_t self = vself;
  403. grub_video_rect_t vpsave;
  404. if (! self->visible)
  405. return;
  406. if (!grub_video_have_common_points (region, &self->bounds))
  407. return;
  408. check_boxes (self);
  409. if (! self->menu_box || ! self->selected_item_box || ! self->item_box)
  410. return;
  411. grub_gui_set_viewport (&self->bounds, &vpsave);
  412. {
  413. grub_gfxmenu_box_t box = self->menu_box;
  414. int box_left_pad = box->get_left_pad (box);
  415. int box_top_pad = box->get_top_pad (box);
  416. int box_right_pad = box->get_right_pad (box);
  417. int box_bottom_pad = box->get_bottom_pad (box);
  418. grub_video_rect_t vpsave2, content_rect;
  419. int num_shown_items = get_num_shown_items (self);
  420. int drawing_scrollbar = (self->draw_scrollbar
  421. && (num_shown_items < self->view->menu->size)
  422. && check_scrollbar (self));
  423. int scrollbar_width = self->scrollbar_width;
  424. content_rect.x = box_left_pad;
  425. content_rect.y = box_top_pad;
  426. content_rect.width = self->bounds.width - box_left_pad - box_right_pad;
  427. content_rect.height = self->bounds.height - box_top_pad - box_bottom_pad;
  428. box->set_content_size (box, content_rect.width, content_rect.height);
  429. box->draw (box, 0, 0);
  430. switch (self->scrollbar_slice)
  431. {
  432. case SCROLLBAR_SLICE_WEST:
  433. content_rect.x += self->scrollbar_right_pad;
  434. content_rect.width -= self->scrollbar_right_pad;
  435. break;
  436. case SCROLLBAR_SLICE_CENTER:
  437. if (drawing_scrollbar)
  438. content_rect.width -= scrollbar_width + self->scrollbar_left_pad
  439. + self->scrollbar_right_pad;
  440. break;
  441. case SCROLLBAR_SLICE_EAST:
  442. content_rect.width -= self->scrollbar_left_pad;
  443. break;
  444. }
  445. grub_gui_set_viewport (&content_rect, &vpsave2);
  446. draw_menu (self, num_shown_items);
  447. grub_gui_restore_viewport (&vpsave2);
  448. if (drawing_scrollbar)
  449. {
  450. content_rect.y += self->scrollbar_top_pad;
  451. content_rect.height -= self->scrollbar_top_pad
  452. + self->scrollbar_bottom_pad;
  453. content_rect.width = scrollbar_width;
  454. switch (self->scrollbar_slice)
  455. {
  456. case SCROLLBAR_SLICE_WEST:
  457. if (box_left_pad > scrollbar_width)
  458. {
  459. content_rect.x = box_left_pad - scrollbar_width;
  460. content_rect.width = scrollbar_width;
  461. }
  462. else
  463. {
  464. content_rect.x = 0;
  465. content_rect.width = box_left_pad;
  466. }
  467. break;
  468. case SCROLLBAR_SLICE_CENTER:
  469. content_rect.x = self->bounds.width - box_right_pad
  470. - scrollbar_width - self->scrollbar_right_pad;
  471. content_rect.width = scrollbar_width;
  472. break;
  473. case SCROLLBAR_SLICE_EAST:
  474. content_rect.x = self->bounds.width - box_right_pad;
  475. content_rect.width = box_right_pad;
  476. break;
  477. }
  478. grub_gui_set_viewport (&content_rect, &vpsave2);
  479. draw_scrollbar (self,
  480. self->first_shown_index, num_shown_items,
  481. 0, self->view->menu->size,
  482. scrollbar_width,
  483. content_rect.height);
  484. grub_gui_restore_viewport (&vpsave2);
  485. }
  486. }
  487. grub_gui_restore_viewport (&vpsave);
  488. }
  489. static void
  490. list_set_parent (void *vself, grub_gui_container_t parent)
  491. {
  492. list_impl_t self = vself;
  493. self->parent = parent;
  494. }
  495. static grub_gui_container_t
  496. list_get_parent (void *vself)
  497. {
  498. list_impl_t self = vself;
  499. return self->parent;
  500. }
  501. static void
  502. list_set_bounds (void *vself, const grub_video_rect_t *bounds)
  503. {
  504. list_impl_t self = vself;
  505. self->bounds = *bounds;
  506. }
  507. static void
  508. list_get_bounds (void *vself, grub_video_rect_t *bounds)
  509. {
  510. list_impl_t self = vself;
  511. *bounds = self->bounds;
  512. }
  513. static void
  514. list_get_minimal_size (void *vself, unsigned *width, unsigned *height)
  515. {
  516. list_impl_t self = vself;
  517. if (check_boxes (self))
  518. {
  519. int boxpad = self->item_padding;
  520. int item_vspace = self->item_spacing;
  521. int item_height = self->item_height;
  522. int num_items = 3;
  523. grub_gfxmenu_box_t box = self->menu_box;
  524. int box_left_pad = box->get_left_pad (box);
  525. int box_top_pad = box->get_top_pad (box);
  526. int box_right_pad = box->get_right_pad (box);
  527. int box_bottom_pad = box->get_bottom_pad (box);
  528. unsigned width_s;
  529. grub_gfxmenu_box_t selbox = self->selected_item_box;
  530. int sel_top_pad = selbox->get_top_pad (selbox);
  531. int sel_bottom_pad = selbox->get_bottom_pad (selbox);
  532. int sel_left_pad = selbox->get_left_pad (selbox);
  533. int sel_right_pad = selbox->get_right_pad (selbox);
  534. grub_gfxmenu_box_t itembox = self->item_box;
  535. int item_top_pad = itembox->get_top_pad (itembox);
  536. int item_bottom_pad = itembox->get_bottom_pad (itembox);
  537. int item_left_pad = itembox->get_left_pad (itembox);
  538. int item_right_pad = itembox->get_right_pad (itembox);
  539. int max_left_pad = grub_max (item_left_pad, sel_left_pad);
  540. int max_right_pad = grub_max (item_right_pad, sel_right_pad);
  541. int max_top_pad = grub_max (item_top_pad, sel_top_pad);
  542. int max_bottom_pad = grub_max (item_bottom_pad, sel_bottom_pad);
  543. *width = grub_font_get_string_width (self->item_font, "Typical OS");
  544. width_s = grub_font_get_string_width (self->selected_item_font,
  545. "Typical OS");
  546. if (*width < width_s)
  547. *width = width_s;
  548. *width += 2 * boxpad + box_left_pad + box_right_pad
  549. + max_left_pad + max_right_pad
  550. + self->item_icon_space + self->icon_width;
  551. switch (self->scrollbar_slice)
  552. {
  553. case SCROLLBAR_SLICE_WEST:
  554. *width += self->scrollbar_right_pad;
  555. break;
  556. case SCROLLBAR_SLICE_CENTER:
  557. *width += self->scrollbar_width + self->scrollbar_left_pad
  558. + self->scrollbar_right_pad;
  559. break;
  560. case SCROLLBAR_SLICE_EAST:
  561. *width += self->scrollbar_left_pad;
  562. break;
  563. }
  564. /* Set the menu box height to fit the items. */
  565. *height = (item_height * num_items
  566. + item_vspace * (num_items - 1)
  567. + 2 * boxpad
  568. + box_top_pad + box_bottom_pad
  569. + max_top_pad + max_bottom_pad);
  570. }
  571. else
  572. {
  573. *width = 0;
  574. *height = 0;
  575. }
  576. }
  577. static grub_err_t
  578. list_set_property (void *vself, const char *name, const char *value)
  579. {
  580. list_impl_t self = vself;
  581. if (grub_strcmp (name, "item_font") == 0)
  582. {
  583. self->item_font = grub_font_get (value);
  584. if (self->selected_item_font_inherit)
  585. self->selected_item_font = self->item_font;
  586. }
  587. else if (grub_strcmp (name, "selected_item_font") == 0)
  588. {
  589. if (! value || grub_strcmp (value, "inherit") == 0)
  590. {
  591. self->selected_item_font = self->item_font;
  592. self->selected_item_font_inherit = 1;
  593. }
  594. else
  595. {
  596. self->selected_item_font = grub_font_get (value);
  597. self->selected_item_font_inherit = 0;
  598. }
  599. }
  600. else if (grub_strcmp (name, "item_color") == 0)
  601. {
  602. grub_video_rgba_color_t color;
  603. if (grub_video_parse_color (value, &color) == GRUB_ERR_NONE)
  604. {
  605. self->item_color = color;
  606. if (self->selected_item_color_inherit)
  607. self->selected_item_color = self->item_color;
  608. }
  609. }
  610. else if (grub_strcmp (name, "selected_item_color") == 0)
  611. {
  612. if (! value || grub_strcmp (value, "inherit") == 0)
  613. {
  614. self->selected_item_color = self->item_color;
  615. self->selected_item_color_inherit = 1;
  616. }
  617. else
  618. {
  619. grub_video_rgba_color_t color;
  620. if (grub_video_parse_color (value, &color)
  621. == GRUB_ERR_NONE)
  622. {
  623. self->selected_item_color = color;
  624. self->selected_item_color_inherit = 0;
  625. }
  626. }
  627. }
  628. else if (grub_strcmp (name, "icon_width") == 0)
  629. {
  630. self->icon_width = grub_strtol (value, 0, 10);
  631. grub_gfxmenu_icon_manager_set_icon_size (self->icon_manager,
  632. self->icon_width,
  633. self->icon_height);
  634. }
  635. else if (grub_strcmp (name, "icon_height") == 0)
  636. {
  637. self->icon_height = grub_strtol (value, 0, 10);
  638. grub_gfxmenu_icon_manager_set_icon_size (self->icon_manager,
  639. self->icon_width,
  640. self->icon_height);
  641. }
  642. else if (grub_strcmp (name, "item_height") == 0)
  643. {
  644. self->item_height = grub_strtol (value, 0, 10);
  645. }
  646. else if (grub_strcmp (name, "item_padding") == 0)
  647. {
  648. self->item_padding = grub_strtol (value, 0, 10);
  649. }
  650. else if (grub_strcmp (name, "item_icon_space") == 0)
  651. {
  652. self->item_icon_space = grub_strtol (value, 0, 10);
  653. }
  654. else if (grub_strcmp (name, "item_spacing") == 0)
  655. {
  656. self->item_spacing = grub_strtol (value, 0, 10);
  657. }
  658. else if (grub_strcmp (name, "visible") == 0)
  659. {
  660. self->visible = grub_strcmp (value, "false") != 0;
  661. }
  662. else if (grub_strcmp (name, "menu_pixmap_style") == 0)
  663. {
  664. self->need_to_recreate_boxes = 1;
  665. grub_free (self->menu_box_pattern);
  666. self->menu_box_pattern = value ? grub_strdup (value) : 0;
  667. }
  668. else if (grub_strcmp (name, "item_pixmap_style") == 0)
  669. {
  670. self->need_to_recreate_boxes = 1;
  671. grub_free (self->item_box_pattern);
  672. self->item_box_pattern = value ? grub_strdup (value) : 0;
  673. if (self->selected_item_box_pattern_inherit)
  674. {
  675. grub_free (self->selected_item_box_pattern);
  676. self->selected_item_box_pattern = value ? grub_strdup (value) : 0;
  677. }
  678. }
  679. else if (grub_strcmp (name, "selected_item_pixmap_style") == 0)
  680. {
  681. if (!value || grub_strcmp (value, "inherit") == 0)
  682. {
  683. grub_free (self->selected_item_box_pattern);
  684. char *tmp = self->item_box_pattern;
  685. self->selected_item_box_pattern = tmp ? grub_strdup (tmp) : 0;
  686. self->selected_item_box_pattern_inherit = 1;
  687. }
  688. else
  689. {
  690. self->need_to_recreate_boxes = 1;
  691. grub_free (self->selected_item_box_pattern);
  692. self->selected_item_box_pattern = grub_strdup (value);
  693. self->selected_item_box_pattern_inherit = 0;
  694. }
  695. }
  696. else if (grub_strcmp (name, "scrollbar_frame") == 0)
  697. {
  698. self->need_to_recreate_scrollbar = 1;
  699. grub_free (self->scrollbar_frame_pattern);
  700. self->scrollbar_frame_pattern = value ? grub_strdup (value) : 0;
  701. }
  702. else if (grub_strcmp (name, "scrollbar_thumb") == 0)
  703. {
  704. self->need_to_recreate_scrollbar = 1;
  705. grub_free (self->scrollbar_thumb_pattern);
  706. self->scrollbar_thumb_pattern = value ? grub_strdup (value) : 0;
  707. }
  708. else if (grub_strcmp (name, "scrollbar_thumb_overlay") == 0)
  709. {
  710. self->scrollbar_thumb_overlay = grub_strcmp (value, "true") == 0;
  711. }
  712. else if (grub_strcmp (name, "scrollbar_width") == 0)
  713. {
  714. self->scrollbar_width = grub_strtol (value, 0, 10);
  715. }
  716. else if (grub_strcmp (name, "scrollbar_slice") == 0)
  717. {
  718. if (grub_strcmp (value, "west") == 0)
  719. self->scrollbar_slice = SCROLLBAR_SLICE_WEST;
  720. else if (grub_strcmp (value, "center") == 0)
  721. self->scrollbar_slice = SCROLLBAR_SLICE_CENTER;
  722. else if (grub_strcmp (value, "east") == 0)
  723. self->scrollbar_slice = SCROLLBAR_SLICE_EAST;
  724. }
  725. else if (grub_strcmp (name, "scrollbar_left_pad") == 0)
  726. {
  727. self->scrollbar_left_pad = grub_strtol (value, 0, 10);
  728. }
  729. else if (grub_strcmp (name, "scrollbar_right_pad") == 0)
  730. {
  731. self->scrollbar_right_pad = grub_strtol (value, 0, 10);
  732. }
  733. else if (grub_strcmp (name, "scrollbar_top_pad") == 0)
  734. {
  735. self->scrollbar_top_pad = grub_strtol (value, 0, 10);
  736. }
  737. else if (grub_strcmp (name, "scrollbar_bottom_pad") == 0)
  738. {
  739. self->scrollbar_bottom_pad = grub_strtol (value, 0, 10);
  740. }
  741. else if (grub_strcmp (name, "scrollbar") == 0)
  742. {
  743. self->draw_scrollbar = grub_strcmp (value, "false") != 0;
  744. }
  745. else if (grub_strcmp (name, "theme_dir") == 0)
  746. {
  747. self->need_to_recreate_boxes = 1;
  748. grub_free (self->theme_dir);
  749. self->theme_dir = value ? grub_strdup (value) : 0;
  750. }
  751. else if (grub_strcmp (name, "id") == 0)
  752. {
  753. grub_free (self->id);
  754. if (value)
  755. self->id = grub_strdup (value);
  756. else
  757. self->id = 0;
  758. }
  759. return grub_errno;
  760. }
  761. /* Set necessary information that the gfxmenu view provides. */
  762. static void
  763. list_set_view_info (void *vself,
  764. grub_gfxmenu_view_t view)
  765. {
  766. list_impl_t self = vself;
  767. grub_gfxmenu_icon_manager_set_theme_path (self->icon_manager,
  768. view->theme_path);
  769. self->view = view;
  770. }
  771. /* Refresh list variables */
  772. static void
  773. list_refresh_info (void *vself,
  774. grub_gfxmenu_view_t view)
  775. {
  776. list_impl_t self = vself;
  777. if (view->nested)
  778. self->first_shown_index = 0;
  779. }
  780. static struct grub_gui_component_ops list_comp_ops =
  781. {
  782. .destroy = list_destroy,
  783. .get_id = list_get_id,
  784. .is_instance = list_is_instance,
  785. .paint = list_paint,
  786. .set_parent = list_set_parent,
  787. .get_parent = list_get_parent,
  788. .set_bounds = list_set_bounds,
  789. .get_bounds = list_get_bounds,
  790. .get_minimal_size = list_get_minimal_size,
  791. .set_property = list_set_property
  792. };
  793. static struct grub_gui_list_ops list_ops =
  794. {
  795. .set_view_info = list_set_view_info,
  796. .refresh_list = list_refresh_info
  797. };
  798. grub_gui_component_t
  799. grub_gui_list_new (void)
  800. {
  801. list_impl_t self;
  802. grub_font_t default_font;
  803. grub_video_rgba_color_t default_fg_color;
  804. self = grub_zalloc (sizeof (*self));
  805. if (! self)
  806. return 0;
  807. self->list.ops = &list_ops;
  808. self->list.component.ops = &list_comp_ops;
  809. self->visible = 1;
  810. default_font = grub_font_get ("Unknown Regular 16");
  811. default_fg_color = grub_video_rgba_color_rgb (0, 0, 0);
  812. self->icon_width = 32;
  813. self->icon_height = 32;
  814. self->item_height = 42;
  815. self->item_padding = 14;
  816. self->item_icon_space = 4;
  817. self->item_spacing = 16;
  818. self->item_font = default_font;
  819. self->selected_item_font_inherit = 1; /* Default to using the item_font. */
  820. self->selected_item_font = default_font;
  821. self->item_color = default_fg_color;
  822. self->selected_item_color_inherit = 1; /* Default to using the item_color. */
  823. self->selected_item_color = default_fg_color;
  824. self->draw_scrollbar = 1;
  825. self->need_to_recreate_scrollbar = 1;
  826. self->scrollbar_frame = 0;
  827. self->scrollbar_thumb = 0;
  828. self->scrollbar_frame_pattern = 0;
  829. self->scrollbar_thumb_pattern = 0;
  830. self->scrollbar_thumb_overlay = 0;
  831. self->scrollbar_width = 16;
  832. self->scrollbar_slice = SCROLLBAR_SLICE_EAST;
  833. self->scrollbar_left_pad = 2;
  834. self->scrollbar_right_pad = 0;
  835. self->scrollbar_top_pad = 0;
  836. self->scrollbar_bottom_pad = 0;
  837. self->first_shown_index = 0;
  838. self->need_to_recreate_boxes = 0;
  839. self->theme_dir = 0;
  840. self->menu_box_pattern = 0;
  841. self->item_box_pattern = 0;
  842. self->selected_item_box_pattern_inherit = 1;/*Default to using the item_box.*/
  843. self->selected_item_box_pattern = 0;
  844. self->menu_box = grub_gfxmenu_create_box (0, 0);
  845. self->item_box = grub_gfxmenu_create_box (0, 0);
  846. self->selected_item_box = grub_gfxmenu_create_box (0, 0);
  847. self->icon_manager = grub_gfxmenu_icon_manager_new ();
  848. if (! self->icon_manager)
  849. {
  850. self->list.component.ops->destroy (self);
  851. return 0;
  852. }
  853. grub_gfxmenu_icon_manager_set_icon_size (self->icon_manager,
  854. self->icon_width,
  855. self->icon_height);
  856. return (grub_gui_component_t) self;
  857. }