1
/* gui_circular_process.c - GUI circular progress indicator component. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
21
#include <grub/misc.h>
23
#include <grub/font.h>
24
#include <grub/gui_string_util.h>
25
#include <grub/gfxmenu_view.h>
26
#include <grub/gfxwidgets.h>
27
#include <grub/trig.h>
29
struct grub_gui_circular_progress
31
struct grub_gui_progress progress;
33
grub_gui_container_t parent;
34
grub_video_rect_t bounds;
44
int need_to_load_pixmaps;
47
struct grub_video_bitmap *center_bitmap;
48
struct grub_video_bitmap *tick_bitmap;
51
typedef struct grub_gui_circular_progress *circular_progress_t;
54
circprog_destroy (void *vself)
56
circular_progress_t self = vself;
57
grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
62
circprog_get_id (void *vself)
64
circular_progress_t self = vself;
69
circprog_is_instance (void *vself __attribute__((unused)), const char *type)
71
return grub_strcmp (type, "component") == 0;
74
static struct grub_video_bitmap *
75
load_bitmap (const char *dir, const char *file)
77
struct grub_video_bitmap *bitmap;
80
/* Check arguments. */
84
/* Resolve to an absolute path. */
85
abspath = grub_resolve_relative_path (dir, file);
90
grub_errno = GRUB_ERR_NONE;
91
grub_video_bitmap_load (&bitmap, abspath);
92
grub_errno = GRUB_ERR_NONE;
99
check_pixmaps (circular_progress_t self)
101
if (self->need_to_load_pixmaps)
103
if (self->center_bitmap)
104
grub_video_bitmap_destroy (self->center_bitmap);
105
self->center_bitmap = load_bitmap (self->theme_dir, self->center_file);
106
self->tick_bitmap = load_bitmap (self->theme_dir, self->tick_file);
107
self->need_to_load_pixmaps = 0;
110
return (self->center_bitmap != 0 && self->tick_bitmap != 0);
114
circprog_paint (void *vself, const grub_video_rect_t *region)
116
circular_progress_t self = vself;
121
if (!grub_video_have_common_points (region, &self->bounds))
124
if (! check_pixmaps (self))
127
grub_video_rect_t vpsave;
128
grub_gui_set_viewport (&self->bounds, &vpsave);
130
int width = self->bounds.width;
131
int height = self->bounds.height;
132
int center_width = grub_video_bitmap_get_width (self->center_bitmap);
133
int center_height = grub_video_bitmap_get_height (self->center_bitmap);
134
int tick_width = grub_video_bitmap_get_width (self->tick_bitmap);
135
int tick_height = grub_video_bitmap_get_height (self->tick_bitmap);
136
grub_video_blit_bitmap (self->center_bitmap, GRUB_VIDEO_BLIT_BLEND,
137
(width - center_width) / 2,
138
(height - center_height) / 2, 0, 0,
139
center_width, center_height);
141
int radius = width / 2 - tick_width / 2 - 1;
145
if (self->end == self->start)
148
nticks = (self->num_ticks
149
* (self->value - self->start)
150
/ (self->end - self->start));
151
/* Do ticks appear or disappear as the value approached the end? */
152
if (self->ticks_disappear)
155
tick_end = self->num_ticks - 1;
160
tick_end = nticks - 1;
164
for (i = tick_begin; i < tick_end; i++)
170
/* Calculate the location of the tick. */
171
angle = self->start_angle + i * GRUB_TRIG_ANGLE_MAX / self->num_ticks;
172
x = width / 2 + (grub_cos (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
173
y = height / 2 + (grub_sin (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
175
/* Adjust (x,y) so the tick is centered. */
177
y -= tick_height / 2;
180
grub_video_blit_bitmap (self->tick_bitmap, GRUB_VIDEO_BLIT_BLEND,
181
x, y, 0, 0, tick_width, tick_height);
184
grub_gui_restore_viewport (&vpsave);
188
circprog_set_parent (void *vself, grub_gui_container_t parent)
190
circular_progress_t self = vself;
191
self->parent = parent;
194
static grub_gui_container_t
195
circprog_get_parent (void *vself)
197
circular_progress_t self = vself;
202
circprog_set_bounds (void *vself, const grub_video_rect_t *bounds)
204
circular_progress_t self = vself;
205
self->bounds = *bounds;
209
circprog_get_bounds (void *vself, grub_video_rect_t *bounds)
211
circular_progress_t self = vself;
212
*bounds = self->bounds;
216
circprog_set_state (void *vself, int visible, int start,
217
int current, int end)
219
circular_progress_t self = vself;
220
self->visible = visible;
222
self->value = current;
227
circprog_set_property (void *vself, const char *name, const char *value)
229
circular_progress_t self = vself;
230
if (grub_strcmp (name, "num_ticks") == 0)
232
self->num_ticks = grub_strtol (value, 0, 10);
234
else if (grub_strcmp (name, "start_angle") == 0)
236
self->start_angle = grub_strtol (value, 0, 10);
238
else if (grub_strcmp (name, "ticks_disappear") == 0)
240
self->ticks_disappear = grub_strcmp (value, "false") != 0;
242
else if (grub_strcmp (name, "center_bitmap") == 0)
244
self->need_to_load_pixmaps = 1;
245
grub_free (self->center_file);
246
self->center_file = value ? grub_strdup (value) : 0;
248
else if (grub_strcmp (name, "tick_bitmap") == 0)
250
self->need_to_load_pixmaps = 1;
251
grub_free (self->tick_file);
252
self->tick_file = value ? grub_strdup (value) : 0;
254
else if (grub_strcmp (name, "theme_dir") == 0)
256
self->need_to_load_pixmaps = 1;
257
grub_free (self->theme_dir);
258
self->theme_dir = value ? grub_strdup (value) : 0;
260
else if (grub_strcmp (name, "id") == 0)
262
grub_gfxmenu_timeout_unregister ((grub_gui_component_t) self);
263
grub_free (self->id);
265
self->id = grub_strdup (value);
268
if (self->id && grub_strcmp (self->id, GRUB_GFXMENU_TIMEOUT_COMPONENT_ID)
270
grub_gfxmenu_timeout_register ((grub_gui_component_t) self,
276
static struct grub_gui_component_ops circprog_ops =
278
.destroy = circprog_destroy,
279
.get_id = circprog_get_id,
280
.is_instance = circprog_is_instance,
281
.paint = circprog_paint,
282
.set_parent = circprog_set_parent,
283
.get_parent = circprog_get_parent,
284
.set_bounds = circprog_set_bounds,
285
.get_bounds = circprog_get_bounds,
286
.set_property = circprog_set_property
289
static struct grub_gui_progress_ops circprog_prog_ops =
291
.set_state = circprog_set_state
295
grub_gui_circular_progress_new (void)
297
circular_progress_t self;
298
self = grub_zalloc (sizeof (*self));
301
self->progress.ops = &circprog_prog_ops;
302
self->progress.component.ops = &circprog_ops;
304
self->num_ticks = 64;
305
self->start_angle = -64;
307
return (grub_gui_component_t) self;