2N/A/* gui_list.c - GUI component to display a selectable list of items. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/gui.h>
2N/A#include <grub/gui_string_util.h>
2N/A#include <grub/gfxmenu_view.h>
2N/A#include <grub/gfxwidgets.h>
2N/A
2N/Astruct grub_gui_list_impl
2N/A{
2N/A struct grub_gui_list list;
2N/A
2N/A grub_gui_container_t parent;
2N/A grub_video_rect_t bounds;
2N/A char *id;
2N/A int visible;
2N/A
2N/A int icon_width;
2N/A int icon_height;
2N/A int item_height;
2N/A int item_padding;
2N/A int item_icon_space;
2N/A int item_spacing;
2N/A grub_font_t item_font;
2N/A grub_font_t selected_item_font;
2N/A grub_video_rgba_color_t item_color;
2N/A int selected_item_color_set;
2N/A grub_video_rgba_color_t selected_item_color;
2N/A
2N/A int draw_scrollbar;
2N/A int need_to_recreate_scrollbar;
2N/A char *scrollbar_frame_pattern;
2N/A char *scrollbar_thumb_pattern;
2N/A grub_gfxmenu_box_t scrollbar_frame;
2N/A grub_gfxmenu_box_t scrollbar_thumb;
2N/A int scrollbar_width;
2N/A
2N/A int first_shown_index;
2N/A
2N/A int need_to_recreate_boxes;
2N/A char *theme_dir;
2N/A char *menu_box_pattern;
2N/A char *selected_item_box_pattern;
2N/A grub_gfxmenu_box_t menu_box;
2N/A grub_gfxmenu_box_t selected_item_box;
2N/A
2N/A grub_gfxmenu_icon_manager_t icon_manager;
2N/A
2N/A grub_gfxmenu_view_t view;
2N/A};
2N/A
2N/Atypedef struct grub_gui_list_impl *list_impl_t;
2N/A
2N/Astatic void
2N/Alist_destroy (void *vself)
2N/A{
2N/A list_impl_t self = vself;
2N/A
2N/A grub_free (self->theme_dir);
2N/A grub_free (self->menu_box_pattern);
2N/A grub_free (self->selected_item_box_pattern);
2N/A if (self->menu_box)
2N/A self->menu_box->destroy (self->menu_box);
2N/A if (self->selected_item_box)
2N/A self->selected_item_box->destroy (self->selected_item_box);
2N/A if (self->icon_manager)
2N/A grub_gfxmenu_icon_manager_destroy (self->icon_manager);
2N/A
2N/A grub_free (self);
2N/A}
2N/A
2N/Astatic int
2N/Aget_num_shown_items (list_impl_t self)
2N/A{
2N/A int boxpad = self->item_padding;
2N/A int item_vspace = self->item_spacing;
2N/A int item_height = self->item_height;
2N/A
2N/A grub_gfxmenu_box_t box = self->menu_box;
2N/A int box_top_pad = box->get_top_pad (box);
2N/A int box_bottom_pad = box->get_bottom_pad (box);
2N/A
2N/A return (self->bounds.height + item_vspace - 2 * boxpad
2N/A - box_top_pad - box_bottom_pad) / (item_height + item_vspace);
2N/A}
2N/A
2N/Astatic int
2N/Acheck_boxes (list_impl_t self)
2N/A{
2N/A if (self->need_to_recreate_boxes)
2N/A {
2N/A grub_gui_recreate_box (&self->menu_box,
2N/A self->menu_box_pattern,
2N/A self->theme_dir);
2N/A
2N/A grub_gui_recreate_box (&self->selected_item_box,
2N/A self->selected_item_box_pattern,
2N/A self->theme_dir);
2N/A
2N/A self->need_to_recreate_boxes = 0;
2N/A }
2N/A
2N/A return (self->menu_box != 0 && self->selected_item_box != 0);
2N/A}
2N/A
2N/Astatic int
2N/Acheck_scrollbar (list_impl_t self)
2N/A{
2N/A if (self->need_to_recreate_scrollbar)
2N/A {
2N/A grub_gui_recreate_box (&self->scrollbar_frame,
2N/A self->scrollbar_frame_pattern,
2N/A self->theme_dir);
2N/A
2N/A grub_gui_recreate_box (&self->scrollbar_thumb,
2N/A self->scrollbar_thumb_pattern,
2N/A self->theme_dir);
2N/A
2N/A self->need_to_recreate_scrollbar = 0;
2N/A }
2N/A
2N/A return (self->scrollbar_frame != 0 && self->scrollbar_thumb != 0);
2N/A}
2N/A
2N/Astatic const char *
2N/Alist_get_id (void *vself)
2N/A{
2N/A list_impl_t self = vself;
2N/A return self->id;
2N/A}
2N/A
2N/Astatic int
2N/Alist_is_instance (void *vself __attribute__((unused)), const char *type)
2N/A{
2N/A return (grub_strcmp (type, "component") == 0
2N/A || grub_strcmp (type, "list") == 0);
2N/A}
2N/A
2N/Astatic struct grub_video_bitmap *
2N/Aget_item_icon (list_impl_t self, int item_index)
2N/A{
2N/A grub_menu_entry_t entry;
2N/A entry = grub_menu_get_entry (self->view->menu, item_index);
2N/A if (! entry)
2N/A return 0;
2N/A
2N/A return grub_gfxmenu_icon_manager_get_icon (self->icon_manager, entry);
2N/A}
2N/A
2N/Astatic void
2N/Amake_selected_item_visible (list_impl_t self)
2N/A{
2N/A int selected_index = self->view->selected;
2N/A if (selected_index < 0)
2N/A return; /* No item is selected. */
2N/A int num_shown_items = get_num_shown_items (self);
2N/A int last_shown_index = self->first_shown_index + (num_shown_items - 1);
2N/A if (selected_index < self->first_shown_index)
2N/A self->first_shown_index = selected_index;
2N/A else if (selected_index > last_shown_index)
2N/A self->first_shown_index = selected_index - (num_shown_items - 1);
2N/A}
2N/A
2N/A/* Draw a scrollbar on the menu. */
2N/Astatic void
2N/Adraw_scrollbar (list_impl_t self,
2N/A int value, int extent, int min, int max,
2N/A int rightx, int topy, int height)
2N/A{
2N/A grub_gfxmenu_box_t frame = self->scrollbar_frame;
2N/A grub_gfxmenu_box_t thumb = self->scrollbar_thumb;
2N/A int frame_vertical_pad = (frame->get_top_pad (frame)
2N/A + frame->get_bottom_pad (frame));
2N/A int frame_horizontal_pad = (frame->get_left_pad (frame)
2N/A + frame->get_right_pad (frame));
2N/A int tracktop = topy + frame->get_top_pad (frame);
2N/A int tracklen = height - frame_vertical_pad;
2N/A frame->set_content_size (frame, self->scrollbar_width, tracklen);
2N/A int thumby = tracktop + tracklen * (value - min) / (max - min);
2N/A int thumbheight = tracklen * extent / (max - min) + 1;
2N/A thumb->set_content_size (thumb,
2N/A self->scrollbar_width - frame_horizontal_pad,
2N/A thumbheight - (thumb->get_top_pad (thumb)
2N/A + thumb->get_bottom_pad (thumb)));
2N/A frame->draw (frame,
2N/A rightx - (self->scrollbar_width + frame_horizontal_pad),
2N/A topy);
2N/A thumb->draw (thumb,
2N/A rightx - (self->scrollbar_width - frame->get_right_pad (frame)),
2N/A thumby);
2N/A}
2N/A
2N/A/* Draw the list of items. */
2N/Astatic void
2N/Adraw_menu (list_impl_t self, int num_shown_items)
2N/A{
2N/A if (! self->menu_box || ! self->selected_item_box)
2N/A return;
2N/A
2N/A int boxpad = self->item_padding;
2N/A int icon_text_space = self->item_icon_space;
2N/A int item_vspace = self->item_spacing;
2N/A
2N/A int ascent = grub_font_get_ascent (self->item_font);
2N/A int descent = grub_font_get_descent (self->item_font);
2N/A int item_height = self->item_height;
2N/A
2N/A make_selected_item_visible (self);
2N/A
2N/A grub_gfxmenu_box_t selbox = self->selected_item_box;
2N/A int sel_leftpad = selbox->get_left_pad (selbox);
2N/A int sel_toppad = selbox->get_top_pad (selbox);
2N/A int item_top = sel_toppad;
2N/A int menu_index;
2N/A int visible_index;
2N/A struct grub_video_rect oviewport;
2N/A
2N/A grub_video_get_viewport (&oviewport.x, &oviewport.y,
2N/A &oviewport.width, &oviewport.height);
2N/A grub_video_set_viewport (oviewport.x + boxpad,
2N/A oviewport.y + boxpad,
2N/A oviewport.width - 2 * boxpad,
2N/A oviewport.height - 2 * boxpad);
2N/A
2N/A for (visible_index = 0, menu_index = self->first_shown_index;
2N/A visible_index < num_shown_items && menu_index < self->view->menu->size;
2N/A visible_index++, menu_index++)
2N/A {
2N/A int is_selected = (menu_index == self->view->selected);
2N/A struct grub_video_bitmap *icon;
2N/A
2N/A if (is_selected)
2N/A {
2N/A int cwidth = oviewport.width - 2 * boxpad - 2;
2N/A if (selbox->get_border_width)
2N/A cwidth -= selbox->get_border_width (selbox);
2N/A selbox->set_content_size (selbox, cwidth, item_height - 1);
2N/A selbox->draw (selbox, 0,
2N/A item_top - sel_toppad);
2N/A }
2N/A
2N/A icon = get_item_icon (self, menu_index);
2N/A if (icon != 0)
2N/A grub_video_blit_bitmap (icon, GRUB_VIDEO_BLIT_BLEND,
2N/A sel_leftpad,
2N/A item_top + (item_height - self->icon_height) / 2,
2N/A 0, 0, self->icon_width, self->icon_height);
2N/A
2N/A const char *item_title =
2N/A grub_menu_get_entry (self->view->menu, menu_index)->title;
2N/A grub_font_t font =
2N/A (is_selected && self->selected_item_font
2N/A ? self->selected_item_font
2N/A : self->item_font);
2N/A grub_video_rgba_color_t text_color =
2N/A ((is_selected && self->selected_item_color_set)
2N/A ? self->selected_item_color
2N/A : self->item_color);
2N/A grub_font_draw_string (item_title,
2N/A font,
2N/A grub_video_map_rgba_color (text_color),
2N/A sel_leftpad + self->icon_width + icon_text_space,
2N/A (item_top + (item_height - (ascent + descent))
2N/A / 2 + ascent));
2N/A
2N/A item_top += item_height + item_vspace;
2N/A }
2N/A grub_video_set_viewport (oviewport.x,
2N/A oviewport.y,
2N/A oviewport.width,
2N/A oviewport.height);
2N/A}
2N/A
2N/Astatic void
2N/Alist_paint (void *vself, const grub_video_rect_t *region)
2N/A{
2N/A list_impl_t self = vself;
2N/A grub_video_rect_t vpsave;
2N/A
2N/A if (! self->visible)
2N/A return;
2N/A if (!grub_video_have_common_points (region, &self->bounds))
2N/A return;
2N/A
2N/A check_boxes (self);
2N/A
2N/A if (! self->menu_box || ! self->selected_item_box)
2N/A return;
2N/A
2N/A grub_gui_set_viewport (&self->bounds, &vpsave);
2N/A {
2N/A grub_gfxmenu_box_t box = self->menu_box;
2N/A int box_left_pad = box->get_left_pad (box);
2N/A int box_top_pad = box->get_top_pad (box);
2N/A int box_right_pad = box->get_right_pad (box);
2N/A int box_bottom_pad = box->get_bottom_pad (box);
2N/A grub_video_rect_t vpsave2, content_rect;
2N/A int num_shown_items = get_num_shown_items (self);
2N/A int drawing_scrollbar = (self->draw_scrollbar
2N/A && (num_shown_items < self->view->menu->size)
2N/A && check_scrollbar (self));
2N/A
2N/A content_rect.x = box_left_pad;
2N/A content_rect.y = box_top_pad;
2N/A content_rect.width = self->bounds.width - box_left_pad - box_right_pad;
2N/A content_rect.height = self->bounds.height - box_top_pad - box_bottom_pad;
2N/A
2N/A box->set_content_size (box, content_rect.width, content_rect.height);
2N/A
2N/A box->draw (box, 0, 0);
2N/A
2N/A grub_gui_set_viewport (&content_rect, &vpsave2);
2N/A draw_menu (self, num_shown_items);
2N/A grub_gui_restore_viewport (&vpsave2);
2N/A
2N/A if (drawing_scrollbar)
2N/A draw_scrollbar (self,
2N/A self->first_shown_index, num_shown_items,
2N/A 0, self->view->menu->size,
2N/A self->bounds.width - box_right_pad
2N/A + self->scrollbar_width,
2N/A box_top_pad,
2N/A self->bounds.height - box_top_pad - box_bottom_pad);
2N/A }
2N/A
2N/A grub_gui_restore_viewport (&vpsave);
2N/A}
2N/A
2N/Astatic void
2N/Alist_set_parent (void *vself, grub_gui_container_t parent)
2N/A{
2N/A list_impl_t self = vself;
2N/A self->parent = parent;
2N/A}
2N/A
2N/Astatic grub_gui_container_t
2N/Alist_get_parent (void *vself)
2N/A{
2N/A list_impl_t self = vself;
2N/A return self->parent;
2N/A}
2N/A
2N/Astatic void
2N/Alist_set_bounds (void *vself, const grub_video_rect_t *bounds)
2N/A{
2N/A list_impl_t self = vself;
2N/A self->bounds = *bounds;
2N/A}
2N/A
2N/Astatic void
2N/Alist_get_bounds (void *vself, grub_video_rect_t *bounds)
2N/A{
2N/A list_impl_t self = vself;
2N/A *bounds = self->bounds;
2N/A}
2N/A
2N/Astatic void
2N/Alist_get_minimal_size (void *vself, unsigned *width, unsigned *height)
2N/A{
2N/A list_impl_t self = vself;
2N/A
2N/A if (check_boxes (self))
2N/A {
2N/A int boxpad = self->item_padding;
2N/A int item_vspace = self->item_spacing;
2N/A int item_height = self->item_height;
2N/A int num_items = 3;
2N/A
2N/A grub_gfxmenu_box_t box = self->menu_box;
2N/A int box_left_pad = box->get_left_pad (box);
2N/A int box_top_pad = box->get_top_pad (box);
2N/A int box_right_pad = box->get_right_pad (box);
2N/A int box_bottom_pad = box->get_bottom_pad (box);
2N/A unsigned width_s;
2N/A
2N/A grub_gfxmenu_box_t selbox = self->selected_item_box;
2N/A int sel_toppad = selbox->get_top_pad (selbox);
2N/A
2N/A *width = grub_font_get_string_width (self->item_font, "Typical OS");
2N/A width_s = grub_font_get_string_width (self->selected_item_font,
2N/A "Typical OS");
2N/A if (*width < width_s)
2N/A *width = width_s;
2N/A
2N/A *width += 2 * boxpad + box_left_pad + box_right_pad;
2N/A
2N/A /* Set the menu box height to fit the items. */
2N/A *height = (item_height * num_items
2N/A + item_vspace * (num_items - 1)
2N/A + 2 * boxpad
2N/A + box_top_pad + box_bottom_pad + sel_toppad);
2N/A }
2N/A else
2N/A {
2N/A *width = 0;
2N/A *height = 0;
2N/A }
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Alist_set_property (void *vself, const char *name, const char *value)
2N/A{
2N/A list_impl_t self = vself;
2N/A if (grub_strcmp (name, "item_font") == 0)
2N/A {
2N/A self->item_font = grub_font_get (value);
2N/A }
2N/A else if (grub_strcmp (name, "selected_item_font") == 0)
2N/A {
2N/A if (! value || grub_strcmp (value, "inherit") == 0)
2N/A self->selected_item_font = 0;
2N/A else
2N/A self->selected_item_font = grub_font_get (value);
2N/A }
2N/A else if (grub_strcmp (name, "item_color") == 0)
2N/A {
2N/A grub_video_parse_color (value, &self->item_color);
2N/A }
2N/A else if (grub_strcmp (name, "selected_item_color") == 0)
2N/A {
2N/A if (! value || grub_strcmp (value, "inherit") == 0)
2N/A {
2N/A self->selected_item_color_set = 0;
2N/A }
2N/A else
2N/A {
2N/A if (grub_video_parse_color (value, &self->selected_item_color)
2N/A == GRUB_ERR_NONE)
2N/A self->selected_item_color_set = 1;
2N/A }
2N/A }
2N/A else if (grub_strcmp (name, "icon_width") == 0)
2N/A {
2N/A self->icon_width = grub_strtol (value, 0, 10);
2N/A grub_gfxmenu_icon_manager_set_icon_size (self->icon_manager,
2N/A self->icon_width,
2N/A self->icon_height);
2N/A }
2N/A else if (grub_strcmp (name, "icon_height") == 0)
2N/A {
2N/A self->icon_height = grub_strtol (value, 0, 10);
2N/A grub_gfxmenu_icon_manager_set_icon_size (self->icon_manager,
2N/A self->icon_width,
2N/A self->icon_height);
2N/A }
2N/A else if (grub_strcmp (name, "item_height") == 0)
2N/A {
2N/A self->item_height = grub_strtol (value, 0, 10);
2N/A }
2N/A else if (grub_strcmp (name, "item_padding") == 0)
2N/A {
2N/A self->item_padding = grub_strtol (value, 0, 10);
2N/A }
2N/A else if (grub_strcmp (name, "item_icon_space") == 0)
2N/A {
2N/A self->item_icon_space = grub_strtol (value, 0, 10);
2N/A }
2N/A else if (grub_strcmp (name, "item_spacing") == 0)
2N/A {
2N/A self->item_spacing = grub_strtol (value, 0, 10);
2N/A }
2N/A else if (grub_strcmp (name, "visible") == 0)
2N/A {
2N/A self->visible = grub_strcmp (value, "false") != 0;
2N/A }
2N/A else if (grub_strcmp (name, "menu_pixmap_style") == 0)
2N/A {
2N/A self->need_to_recreate_boxes = 1;
2N/A grub_free (self->menu_box_pattern);
2N/A self->menu_box_pattern = value ? grub_strdup (value) : 0;
2N/A }
2N/A else if (grub_strcmp (name, "selected_item_pixmap_style") == 0)
2N/A {
2N/A self->need_to_recreate_boxes = 1;
2N/A grub_free (self->selected_item_box_pattern);
2N/A self->selected_item_box_pattern = value ? grub_strdup (value) : 0;
2N/A }
2N/A else if (grub_strcmp (name, "scrollbar_frame") == 0)
2N/A {
2N/A self->need_to_recreate_scrollbar = 1;
2N/A grub_free (self->scrollbar_frame_pattern);
2N/A self->scrollbar_frame_pattern = value ? grub_strdup (value) : 0;
2N/A }
2N/A else if (grub_strcmp (name, "scrollbar_thumb") == 0)
2N/A {
2N/A self->need_to_recreate_scrollbar = 1;
2N/A grub_free (self->scrollbar_thumb_pattern);
2N/A self->scrollbar_thumb_pattern = value ? grub_strdup (value) : 0;
2N/A }
2N/A else if (grub_strcmp (name, "scrollbar_width") == 0)
2N/A {
2N/A self->scrollbar_width = grub_strtol (value, 0, 10);
2N/A }
2N/A else if (grub_strcmp (name, "scrollbar") == 0)
2N/A {
2N/A self->draw_scrollbar = grub_strcmp (value, "false") != 0;
2N/A }
2N/A else if (grub_strcmp (name, "theme_dir") == 0)
2N/A {
2N/A self->need_to_recreate_boxes = 1;
2N/A grub_free (self->theme_dir);
2N/A self->theme_dir = value ? grub_strdup (value) : 0;
2N/A }
2N/A else if (grub_strcmp (name, "id") == 0)
2N/A {
2N/A grub_free (self->id);
2N/A if (value)
2N/A self->id = grub_strdup (value);
2N/A else
2N/A self->id = 0;
2N/A }
2N/A return grub_errno;
2N/A}
2N/A
2N/A/* Set necessary information that the gfxmenu view provides. */
2N/Astatic void
2N/Alist_set_view_info (void *vself,
2N/A grub_gfxmenu_view_t view)
2N/A{
2N/A list_impl_t self = vself;
2N/A grub_gfxmenu_icon_manager_set_theme_path (self->icon_manager,
2N/A view->theme_path);
2N/A self->view = view;
2N/A}
2N/A
2N/Astatic struct grub_gui_component_ops list_comp_ops =
2N/A {
2N/A .destroy = list_destroy,
2N/A .get_id = list_get_id,
2N/A .is_instance = list_is_instance,
2N/A .paint = list_paint,
2N/A .set_parent = list_set_parent,
2N/A .get_parent = list_get_parent,
2N/A .set_bounds = list_set_bounds,
2N/A .get_bounds = list_get_bounds,
2N/A .get_minimal_size = list_get_minimal_size,
2N/A .set_property = list_set_property
2N/A };
2N/A
2N/Astatic struct grub_gui_list_ops list_ops =
2N/A{
2N/A .set_view_info = list_set_view_info
2N/A};
2N/A
2N/Agrub_gui_component_t
2N/Agrub_gui_list_new (void)
2N/A{
2N/A list_impl_t self;
2N/A grub_font_t default_font;
2N/A grub_video_rgba_color_t default_fg_color;
2N/A
2N/A self = grub_zalloc (sizeof (*self));
2N/A if (! self)
2N/A return 0;
2N/A
2N/A self->list.ops = &list_ops;
2N/A self->list.component.ops = &list_comp_ops;
2N/A
2N/A self->visible = 1;
2N/A
2N/A default_font = grub_font_get ("Unknown Regular 16");
2N/A default_fg_color = grub_video_rgba_color_rgb (0, 0, 0);
2N/A
2N/A self->icon_width = 32;
2N/A self->icon_height = 32;
2N/A self->item_height = 42;
2N/A self->item_padding = 14;
2N/A self->item_icon_space = 4;
2N/A self->item_spacing = 16;
2N/A self->item_font = default_font;
2N/A self->selected_item_font = 0; /* Default to using the item_font. */
2N/A self->item_color = default_fg_color;
2N/A self->selected_item_color_set = 0; /* Default to using the item_color. */
2N/A self->selected_item_color = default_fg_color;
2N/A
2N/A self->draw_scrollbar = 1;
2N/A self->need_to_recreate_scrollbar = 1;
2N/A self->scrollbar_frame = 0;
2N/A self->scrollbar_thumb = 0;
2N/A self->scrollbar_frame_pattern = 0;
2N/A self->scrollbar_thumb_pattern = 0;
2N/A self->scrollbar_width = 16;
2N/A
2N/A self->first_shown_index = 0;
2N/A
2N/A self->need_to_recreate_boxes = 0;
2N/A self->theme_dir = 0;
2N/A self->menu_box_pattern = 0;
2N/A self->selected_item_box_pattern = 0;
2N/A self->menu_box = grub_gfxmenu_create_box (0, 0);
2N/A self->selected_item_box = grub_gfxmenu_create_box (0, 0);
2N/A
2N/A self->icon_manager = grub_gfxmenu_icon_manager_new ();
2N/A if (! self->icon_manager)
2N/A {
2N/A self->list.component.ops->destroy (self);
2N/A return 0;
2N/A }
2N/A grub_gfxmenu_icon_manager_set_icon_size (self->icon_manager,
2N/A self->icon_width,
2N/A self->icon_height);
2N/A return (grub_gui_component_t) self;
2N/A}