gui_progress_bar.c 12.4 KB
Newer Older
Colin D Bennett's avatar
Colin D Bennett committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* gui_progress_bar.c - GUI progress bar component.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2008,2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/gui.h>
#include <grub/font.h>
#include <grub/gui_string_util.h>
#include <grub/gfxmenu_view.h>
#include <grub/gfxwidgets.h>
27
#include <grub/i18n.h>
28
#include <grub/color.h>
Colin D Bennett's avatar
Colin D Bennett committed
29 30 31

struct grub_gui_progress_bar
{
32
  struct grub_gui_progress progress;
Colin D Bennett's avatar
Colin D Bennett committed
33 34 35 36 37 38 39 40

  grub_gui_container_t parent;
  grub_video_rect_t bounds;
  char *id;
  int visible;
  int start;
  int end;
  int value;
41
  char *template;
Colin D Bennett's avatar
Colin D Bennett committed
42
  grub_font_t font;
43 44 45 46
  grub_video_rgba_color_t text_color;
  grub_video_rgba_color_t border_color;
  grub_video_rgba_color_t bg_color;
  grub_video_rgba_color_t fg_color;
Colin D Bennett's avatar
Colin D Bennett committed
47 48 49

  char *theme_dir;
  int need_to_recreate_pixmaps;
50
  int pixmapbar_available;
Colin D Bennett's avatar
Colin D Bennett committed
51 52 53 54
  char *bar_pattern;
  char *highlight_pattern;
  grub_gfxmenu_box_t bar_box;
  grub_gfxmenu_box_t highlight_box;
55
  int highlight_overlay;
Colin D Bennett's avatar
Colin D Bennett committed
56 57 58 59 60 61 62 63
};

typedef struct grub_gui_progress_bar *grub_gui_progress_bar_t;

static void
progress_bar_destroy (void *vself)
{
  grub_gui_progress_bar_t self = vself;
64 65 66
  grub_free (self->theme_dir);
  grub_free (self->template);
  grub_free (self->id);
67
  grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
Colin D Bennett's avatar
Colin D Bennett committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  grub_free (self);
}

static const char *
progress_bar_get_id (void *vself)
{
  grub_gui_progress_bar_t self = vself;
  return self->id;
}

static int
progress_bar_is_instance (void *vself __attribute__((unused)), const char *type)
{
  return grub_strcmp (type, "component") == 0;
}

static int
check_pixmaps (grub_gui_progress_bar_t self)
{
87 88
  if (!self->pixmapbar_available)
    return 0;
Colin D Bennett's avatar
Colin D Bennett committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  if (self->need_to_recreate_pixmaps)
    {
      grub_gui_recreate_box (&self->bar_box,
                             self->bar_pattern,
                             self->theme_dir);

      grub_gui_recreate_box (&self->highlight_box,
                             self->highlight_pattern,
                             self->theme_dir);

      self->need_to_recreate_pixmaps = 0;
    }

  return (self->bar_box != 0 && self->highlight_box != 0);
}

static void
draw_filled_rect_bar (grub_gui_progress_bar_t self)
{
  /* Set the progress bar's frame.  */
  grub_video_rect_t f;
  f.x = 1;
  f.y = 1;
  f.width = self->bounds.width - 2;
  f.height = self->bounds.height - 2;

  /* Border.  */
116
  grub_video_fill_rect (grub_video_map_rgba_color (self->border_color),
Colin D Bennett's avatar
Colin D Bennett committed
117 118 119 120
                        f.x - 1, f.y - 1,
                        f.width + 2, f.height + 2);

  /* Bar background.  */
121 122 123 124 125 126 127 128 129
  unsigned barwidth;

  if (self->end <= self->start
      || self->value <= self->start)
    barwidth = 0;
  else
    barwidth = (f.width
		* (self->value - self->start)
		/ (self->end - self->start));
130
  grub_video_fill_rect (grub_video_map_rgba_color (self->bg_color),
Colin D Bennett's avatar
Colin D Bennett committed
131 132 133 134
                        f.x + barwidth, f.y,
                        f.width - barwidth, f.height);

  /* Bar foreground.  */
135
  grub_video_fill_rect (grub_video_map_rgba_color (self->fg_color),
Colin D Bennett's avatar
Colin D Bennett committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                        f.x, f.y,
                        barwidth, f.height);
}

static void
draw_pixmap_bar (grub_gui_progress_bar_t self)
{
  grub_gfxmenu_box_t bar = self->bar_box;
  grub_gfxmenu_box_t hl = self->highlight_box;
  int w = self->bounds.width;
  int h = self->bounds.height;
  int bar_l_pad = bar->get_left_pad (bar);
  int bar_r_pad = bar->get_right_pad (bar);
  int bar_t_pad = bar->get_top_pad (bar);
  int bar_b_pad = bar->get_bottom_pad (bar);
  int bar_h_pad = bar_l_pad + bar_r_pad;
  int bar_v_pad = bar_t_pad + bar_b_pad;
153 154 155 156 157 158
  int hl_l_pad = hl->get_left_pad (hl);
  int hl_r_pad = hl->get_right_pad (hl);
  int hl_t_pad = hl->get_top_pad (hl);
  int hl_b_pad = hl->get_bottom_pad (hl);
  int hl_h_pad = hl_l_pad + hl_r_pad;
  int hl_v_pad = hl_t_pad + hl_b_pad;
Colin D Bennett's avatar
Colin D Bennett committed
159 160
  int tracklen = w - bar_h_pad;
  int trackheight = h - bar_v_pad;
161
  int barwidth;
162 163 164
  int hlheight = trackheight;
  int hlx = bar_l_pad;
  int hly = bar_t_pad;
165

Colin D Bennett's avatar
Colin D Bennett committed
166
  bar->set_content_size (bar, tracklen, trackheight);
167
  bar->draw (bar, 0, 0);
Colin D Bennett's avatar
Colin D Bennett committed
168

169 170 171 172 173 174 175 176 177
  if (self->highlight_overlay)
    {
      tracklen += hl_h_pad;
      hlx -= hl_l_pad;
      hly -= hl_t_pad;
    }
  else
    hlheight -= hl_v_pad;

178 179 180 181 182 183
  if (self->value <= self->start
      || self->end <= self->start)
    barwidth = 0;
  else
    barwidth = ((unsigned) (tracklen * (self->value - self->start))
		/ ((unsigned) (self->end - self->start)));
184

185 186
  if (barwidth >= hl_h_pad)
    {
187 188
      hl->set_content_size (hl, barwidth - hl_h_pad, hlheight);
      hl->draw (hl, hlx, hly);
189
    }
Colin D Bennett's avatar
Colin D Bennett committed
190 191
}

192 193
#pragma GCC diagnostic ignored "-Wformat-nonliteral"

Colin D Bennett's avatar
Colin D Bennett committed
194 195 196
static void
draw_text (grub_gui_progress_bar_t self)
{
197
  if (self->template)
Colin D Bennett's avatar
Colin D Bennett committed
198 199
    {
      grub_font_t font = self->font;
200 201
      grub_video_color_t text_color =
	grub_video_map_rgba_color (self->text_color);
Colin D Bennett's avatar
Colin D Bennett committed
202 203
      int width = self->bounds.width;
      int height = self->bounds.height;
204 205 206
      char *text;
      text = grub_xasprintf (self->template,
			     self->value > 0 ? self->value : -self->value);
207 208 209 210 211 212
      if (!text)
	{
	  grub_print_error ();
	  grub_errno = GRUB_ERR_NONE;
	  return;
	}
Colin D Bennett's avatar
Colin D Bennett committed
213 214 215 216 217 218
      /* Center the text. */
      int text_width = grub_font_get_string_width (font, text);
      int x = (width - text_width) / 2;
      int y = ((height - grub_font_get_descent (font)) / 2
               + grub_font_get_ascent (font) / 2);
      grub_font_draw_string (text, font, text_color, x, y);
219
      grub_free (text);
Colin D Bennett's avatar
Colin D Bennett committed
220 221 222
    }
}

223 224
#pragma GCC diagnostic error "-Wformat-nonliteral"

Colin D Bennett's avatar
Colin D Bennett committed
225
static void
226
progress_bar_paint (void *vself, const grub_video_rect_t *region)
Colin D Bennett's avatar
Colin D Bennett committed
227 228
{
  grub_gui_progress_bar_t self = vself;
229 230
  grub_video_rect_t vpsave;

Colin D Bennett's avatar
Colin D Bennett committed
231 232
  if (! self->visible)
    return;
233 234
  if (!grub_video_have_common_points (region, &self->bounds))
    return;
Colin D Bennett's avatar
Colin D Bennett committed
235

236 237 238
  if (self->end == self->start)
    return;

Colin D Bennett's avatar
Colin D Bennett committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  grub_gui_set_viewport (&self->bounds, &vpsave);

  if (check_pixmaps (self))
    draw_pixmap_bar (self);
  else
    draw_filled_rect_bar (self);

  draw_text (self);

  grub_gui_restore_viewport (&vpsave);
}

static void
progress_bar_set_parent (void *vself, grub_gui_container_t parent)
{
  grub_gui_progress_bar_t self = vself;
  self->parent = parent;
}

static grub_gui_container_t
progress_bar_get_parent (void *vself)
{
  grub_gui_progress_bar_t self = vself;
  return self->parent;
}

static void
progress_bar_set_bounds (void *vself, const grub_video_rect_t *bounds)
{
  grub_gui_progress_bar_t self = vself;
  self->bounds = *bounds;
}

static void
progress_bar_get_bounds (void *vself, grub_video_rect_t *bounds)
{
  grub_gui_progress_bar_t self = vself;
  *bounds = self->bounds;
}

static void
280
progress_bar_get_minimal_size (void *vself,
281
			       unsigned *width, unsigned *height)
Colin D Bennett's avatar
Colin D Bennett committed
282
{
283 284
  unsigned min_width = 0;
  unsigned min_height = 0;
285 286 287 288
  grub_gui_progress_bar_t self = vself;

  if (self->template)
    {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
      min_width = grub_font_get_string_width (self->font, self->template);
      min_width += grub_font_get_string_width (self->font, "XXXXXXXXXX");
      min_height = grub_font_get_descent (self->font)
                   + grub_font_get_ascent (self->font);
    }
  if (check_pixmaps (self))
    {
      grub_gfxmenu_box_t bar = self->bar_box;
      grub_gfxmenu_box_t hl = self->highlight_box;
      min_width += bar->get_left_pad (bar) + bar->get_right_pad (bar);
      min_height += bar->get_top_pad (bar) + bar->get_bottom_pad (bar);
      if (!self->highlight_overlay)
        {
          min_width += hl->get_left_pad (hl) + hl->get_right_pad (hl);
          min_height += hl->get_top_pad (hl) + hl->get_bottom_pad (hl);
        }
    }
  else
    {
      min_height += 2;
      min_width += 2;
310
    }
Colin D Bennett's avatar
Colin D Bennett committed
311
  *width = 200;
312 313
  if (*width < min_width)
    *width = min_width;
Colin D Bennett's avatar
Colin D Bennett committed
314
  *height = 28;
315 316
  if (*height < min_height)
    *height = min_height;
317 318 319 320 321 322 323 324 325 326 327
}

static void
progress_bar_set_state (void *vself, int visible, int start,
			int current, int end)
{
  grub_gui_progress_bar_t self = vself;  
  self->visible = visible;
  self->start = start;
  self->value = current;
  self->end = end;
Colin D Bennett's avatar
Colin D Bennett committed
328 329 330 331 332 333
}

static grub_err_t
progress_bar_set_property (void *vself, const char *name, const char *value)
{
  grub_gui_progress_bar_t self = vself;
334
  if (grub_strcmp (name, "text") == 0)
Colin D Bennett's avatar
Colin D Bennett committed
335
    {
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
      grub_free (self->template);
      if (grub_strcmp (value, "@TIMEOUT_NOTIFICATION_LONG@") == 0)
	value 
	  = _("The highlighted entry will be executed automatically in %ds.");
      else if (grub_strcmp (value, "@TIMEOUT_NOTIFICATION_MIDDLE@") == 0)
	/* TRANSLATORS:  's' stands for seconds.
	   It's a standalone timeout notification.
	   Please use the short form in your language.  */
	value = _("%ds remaining.");
      else if (grub_strcmp (value, "@TIMEOUT_NOTIFICATION_SHORT@") == 0)
	/* TRANSLATORS:  's' stands for seconds.
	   It's a standalone timeout notification.
	   Please use the shortest form available in you language.  */
	value = _("%ds");

      self->template = grub_strdup (value);
Colin D Bennett's avatar
Colin D Bennett committed
352 353 354 355 356 357 358
    }
  else if (grub_strcmp (name, "font") == 0)
    {
      self->font = grub_font_get (value);
    }
  else if (grub_strcmp (name, "text_color") == 0)
    {
359
      grub_video_parse_color (value, &self->text_color);
Colin D Bennett's avatar
Colin D Bennett committed
360 361 362
    }
  else if (grub_strcmp (name, "border_color") == 0)
    {
363
       grub_video_parse_color (value, &self->border_color);
Colin D Bennett's avatar
Colin D Bennett committed
364 365 366
    }
  else if (grub_strcmp (name, "bg_color") == 0)
    {
367
       grub_video_parse_color (value, &self->bg_color);
Colin D Bennett's avatar
Colin D Bennett committed
368 369 370
    }
  else if (grub_strcmp (name, "fg_color") == 0)
    {
371
      grub_video_parse_color (value, &self->fg_color);
Colin D Bennett's avatar
Colin D Bennett committed
372 373 374 375
    }
  else if (grub_strcmp (name, "bar_style") == 0)
    {
      self->need_to_recreate_pixmaps = 1;
376
      self->pixmapbar_available = 1;
Colin D Bennett's avatar
Colin D Bennett committed
377 378 379 380 381 382
      grub_free (self->bar_pattern);
      self->bar_pattern = value ? grub_strdup (value) : 0;
    }
  else if (grub_strcmp (name, "highlight_style") == 0)
    {
      self->need_to_recreate_pixmaps = 1;
383
      self->pixmapbar_available = 1;
Colin D Bennett's avatar
Colin D Bennett committed
384 385 386
      grub_free (self->highlight_pattern);
      self->highlight_pattern = value ? grub_strdup (value) : 0;
    }
387 388 389 390
  else if (grub_strcmp (name, "highlight_overlay") == 0)
    {
      self->highlight_overlay = grub_strcmp (value, "true") == 0;
    }
Colin D Bennett's avatar
Colin D Bennett committed
391 392 393 394 395 396 397 398
  else if (grub_strcmp (name, "theme_dir") == 0)
    {
      self->need_to_recreate_pixmaps = 1;
      grub_free (self->theme_dir);
      self->theme_dir = value ? grub_strdup (value) : 0;
    }
  else if (grub_strcmp (name, "id") == 0)
    {
399
      grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
Colin D Bennett's avatar
Colin D Bennett committed
400 401 402 403 404
      grub_free (self->id);
      if (value)
        self->id = grub_strdup (value);
      else
        self->id = 0;
405 406
      if (self->id && grub_strcmp (self->id, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID)
	  == 0)
407 408
	grub_gfxmenu_timeout_register ((grub_gui_component_t) self,
				       progress_bar_set_state);
Colin D Bennett's avatar
Colin D Bennett committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    }
  return grub_errno;
}

static struct grub_gui_component_ops progress_bar_ops =
{
  .destroy = progress_bar_destroy,
  .get_id = progress_bar_get_id,
  .is_instance = progress_bar_is_instance,
  .paint = progress_bar_paint,
  .set_parent = progress_bar_set_parent,
  .get_parent = progress_bar_get_parent,
  .set_bounds = progress_bar_set_bounds,
  .get_bounds = progress_bar_get_bounds,
423
  .get_minimal_size = progress_bar_get_minimal_size,
Colin D Bennett's avatar
Colin D Bennett committed
424 425 426
  .set_property = progress_bar_set_property
};

427 428 429 430 431
static struct grub_gui_progress_ops progress_bar_pb_ops =
  {
    .set_state = progress_bar_set_state
  };

Colin D Bennett's avatar
Colin D Bennett committed
432 433 434 435
grub_gui_component_t
grub_gui_progress_bar_new (void)
{
  grub_gui_progress_bar_t self;
436
  self = grub_zalloc (sizeof (*self));
Colin D Bennett's avatar
Colin D Bennett committed
437 438
  if (! self)
    return 0;
439

440 441
  self->progress.ops = &progress_bar_pb_ops;
  self->progress.component.ops = &progress_bar_ops;
Colin D Bennett's avatar
Colin D Bennett committed
442
  self->visible = 1;
443
  self->font = grub_font_get ("Unknown Regular 16");
444 445 446
  grub_video_rgba_color_t black = { .red = 0, .green = 0, .blue = 0, .alpha = 255 };
  grub_video_rgba_color_t gray = { .red = 128, .green = 128, .blue = 128, .alpha = 255 };
  grub_video_rgba_color_t lightgray = { .red = 200, .green = 200, .blue = 200, .alpha = 255 };
Colin D Bennett's avatar
Colin D Bennett committed
447 448 449 450
  self->text_color = black;
  self->border_color = black;
  self->bg_color = gray;
  self->fg_color = lightgray;
451
  self->highlight_overlay = 0;
Colin D Bennett's avatar
Colin D Bennett committed
452 453 454

  return (grub_gui_component_t) self;
}