]>
Commit | Line | Data |
---|---|---|
d920a32a CB |
1 | /* gui_canvas.c - GUI container allowing manually placed components. */ |
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 | ||
20 | #include <grub/mm.h> | |
21 | #include <grub/misc.h> | |
22 | #include <grub/gui.h> | |
23 | #include <grub/gui_string_util.h> | |
24 | ||
25 | /* TODO Add layering so that components can be properly overlaid. */ | |
26 | ||
27 | struct component_node | |
28 | { | |
29 | grub_gui_component_t component; | |
30 | struct component_node *next; | |
31 | }; | |
32 | ||
33 | struct grub_gui_canvas | |
34 | { | |
9a175884 | 35 | struct grub_gui_container container; |
d920a32a CB |
36 | |
37 | grub_gui_container_t parent; | |
38 | grub_video_rect_t bounds; | |
39 | char *id; | |
d920a32a CB |
40 | /* Component list (dummy head node). */ |
41 | struct component_node components; | |
42 | }; | |
43 | ||
44 | typedef struct grub_gui_canvas *grub_gui_canvas_t; | |
45 | ||
46 | static void | |
47 | canvas_destroy (void *vself) | |
48 | { | |
49 | grub_gui_canvas_t self = vself; | |
50 | struct component_node *cur; | |
51 | struct component_node *next; | |
52 | for (cur = self->components.next; cur; cur = next) | |
53 | { | |
54 | /* Copy the 'next' pointer, since we need it for the next iteration, | |
55 | and we're going to free the memory it is stored in. */ | |
56 | next = cur->next; | |
57 | /* Destroy the child component. */ | |
58 | cur->component->ops->destroy (cur->component); | |
59 | /* Free the linked list node. */ | |
60 | grub_free (cur); | |
61 | } | |
62 | grub_free (self); | |
63 | } | |
64 | ||
65 | static const char * | |
66 | canvas_get_id (void *vself) | |
67 | { | |
68 | grub_gui_canvas_t self = vself; | |
69 | return self->id; | |
70 | } | |
71 | ||
72 | static int | |
73 | canvas_is_instance (void *vself __attribute__((unused)), const char *type) | |
74 | { | |
75 | return (grub_strcmp (type, "component") == 0 | |
76 | || grub_strcmp (type, "container") == 0); | |
77 | } | |
78 | ||
79 | static void | |
947fa16c | 80 | canvas_paint (void *vself, const grub_video_rect_t *region) |
d920a32a CB |
81 | { |
82 | grub_gui_canvas_t self = vself; | |
83 | struct component_node *cur; | |
84 | grub_video_rect_t vpsave; | |
85 | ||
86 | grub_gui_set_viewport (&self->bounds, &vpsave); | |
87 | for (cur = self->components.next; cur; cur = cur->next) | |
88 | { | |
d920a32a CB |
89 | grub_video_rect_t r; |
90 | grub_gui_component_t comp; | |
b9da1700 | 91 | signed x, y, w, h; |
d920a32a CB |
92 | |
93 | comp = cur->component; | |
94 | ||
b9da1700 VS |
95 | w = grub_fixed_sfs_multiply (self->bounds.width, comp->wfrac) + comp->w; |
96 | h = grub_fixed_sfs_multiply (self->bounds.height, comp->hfrac) + comp->h; | |
97 | x = grub_fixed_sfs_multiply (self->bounds.width, comp->xfrac) + comp->x; | |
98 | y = grub_fixed_sfs_multiply (self->bounds.height, comp->yfrac) + comp->y; | |
9a175884 VS |
99 | |
100 | if (comp->ops->get_minimal_size) | |
101 | { | |
102 | unsigned mw; | |
103 | unsigned mh; | |
104 | comp->ops->get_minimal_size (comp, &mw, &mh); | |
b9da1700 VS |
105 | if (w < (signed) mw) |
106 | w = mw; | |
107 | if (h < (signed) mh) | |
108 | h = mh; | |
9a175884 VS |
109 | } |
110 | ||
b9da1700 VS |
111 | /* Sanity checks. */ |
112 | if (w <= 0) | |
113 | w = 32; | |
114 | if (h <= 0) | |
115 | h = 32; | |
116 | ||
117 | if (x >= (signed) self->bounds.width) | |
118 | x = self->bounds.width - 32; | |
119 | if (y >= (signed) self->bounds.height) | |
120 | y = self->bounds.height - 32; | |
121 | ||
122 | if (x < 0) | |
123 | x = 0; | |
124 | if (y < 0) | |
125 | y = 0; | |
126 | ||
127 | if (x + w >= (signed) self->bounds.width) | |
128 | w = self->bounds.width - x; | |
129 | if (y + h >= (signed) self->bounds.height) | |
130 | h = self->bounds.height - y; | |
131 | ||
132 | r.x = x; | |
133 | r.y = y; | |
134 | r.width = w; | |
135 | r.height = h; | |
9a175884 | 136 | comp->ops->set_bounds (comp, &r); |
d920a32a CB |
137 | |
138 | /* Paint the child. */ | |
947fa16c VS |
139 | if (grub_video_have_common_points (region, &r)) |
140 | comp->ops->paint (comp, region); | |
d920a32a CB |
141 | } |
142 | grub_gui_restore_viewport (&vpsave); | |
143 | } | |
144 | ||
145 | static void | |
146 | canvas_set_parent (void *vself, grub_gui_container_t parent) | |
147 | { | |
148 | grub_gui_canvas_t self = vself; | |
149 | self->parent = parent; | |
150 | } | |
151 | ||
152 | static grub_gui_container_t | |
153 | canvas_get_parent (void *vself) | |
154 | { | |
155 | grub_gui_canvas_t self = vself; | |
156 | return self->parent; | |
157 | } | |
158 | ||
159 | static void | |
160 | canvas_set_bounds (void *vself, const grub_video_rect_t *bounds) | |
161 | { | |
162 | grub_gui_canvas_t self = vself; | |
163 | self->bounds = *bounds; | |
164 | } | |
165 | ||
166 | static void | |
167 | canvas_get_bounds (void *vself, grub_video_rect_t *bounds) | |
168 | { | |
169 | grub_gui_canvas_t self = vself; | |
170 | *bounds = self->bounds; | |
171 | } | |
172 | ||
d920a32a CB |
173 | static grub_err_t |
174 | canvas_set_property (void *vself, const char *name, const char *value) | |
175 | { | |
176 | grub_gui_canvas_t self = vself; | |
177 | if (grub_strcmp (name, "id") == 0) | |
178 | { | |
179 | grub_free (self->id); | |
180 | if (value) | |
181 | { | |
182 | self->id = grub_strdup (value); | |
183 | if (! self->id) | |
184 | return grub_errno; | |
185 | } | |
186 | else | |
187 | self->id = 0; | |
188 | } | |
d920a32a CB |
189 | return grub_errno; |
190 | } | |
191 | ||
192 | static void | |
193 | canvas_add (void *vself, grub_gui_component_t comp) | |
194 | { | |
195 | grub_gui_canvas_t self = vself; | |
196 | struct component_node *node; | |
197 | node = grub_malloc (sizeof (*node)); | |
198 | if (! node) | |
199 | return; /* Note: probably should handle the error. */ | |
200 | node->component = comp; | |
201 | node->next = self->components.next; | |
202 | self->components.next = node; | |
203 | comp->ops->set_parent (comp, (grub_gui_container_t) self); | |
204 | } | |
205 | ||
206 | static void | |
207 | canvas_remove (void *vself, grub_gui_component_t comp) | |
208 | { | |
209 | grub_gui_canvas_t self = vself; | |
210 | struct component_node *cur; | |
211 | struct component_node *prev; | |
212 | prev = &self->components; | |
213 | for (cur = self->components.next; cur; prev = cur, cur = cur->next) | |
214 | { | |
215 | if (cur->component == comp) | |
216 | { | |
217 | /* Unlink 'cur' from the list. */ | |
218 | prev->next = cur->next; | |
219 | /* Free the node's memory (but don't destroy the component). */ | |
220 | grub_free (cur); | |
221 | /* Must not loop again, since 'cur' would be dereferenced! */ | |
222 | return; | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | static void | |
228 | canvas_iterate_children (void *vself, | |
229 | grub_gui_component_callback cb, void *userdata) | |
230 | { | |
231 | grub_gui_canvas_t self = vself; | |
232 | struct component_node *cur; | |
233 | for (cur = self->components.next; cur; cur = cur->next) | |
234 | cb (cur->component, userdata); | |
235 | } | |
236 | ||
9a175884 VS |
237 | static struct grub_gui_component_ops canvas_comp_ops = |
238 | { | |
239 | .destroy = canvas_destroy, | |
240 | .get_id = canvas_get_id, | |
241 | .is_instance = canvas_is_instance, | |
242 | .paint = canvas_paint, | |
243 | .set_parent = canvas_set_parent, | |
244 | .get_parent = canvas_get_parent, | |
245 | .set_bounds = canvas_set_bounds, | |
246 | .get_bounds = canvas_get_bounds, | |
247 | .set_property = canvas_set_property | |
248 | }; | |
249 | ||
d920a32a CB |
250 | static struct grub_gui_container_ops canvas_ops = |
251 | { | |
d920a32a CB |
252 | .add = canvas_add, |
253 | .remove = canvas_remove, | |
254 | .iterate_children = canvas_iterate_children | |
255 | }; | |
256 | ||
257 | grub_gui_container_t | |
258 | grub_gui_canvas_new (void) | |
259 | { | |
260 | grub_gui_canvas_t canvas; | |
9a175884 | 261 | canvas = grub_zalloc (sizeof (*canvas)); |
d920a32a CB |
262 | if (! canvas) |
263 | return 0; | |
9a175884 VS |
264 | canvas->container.ops = &canvas_ops; |
265 | canvas->container.component.ops = &canvas_comp_ops; | |
d920a32a CB |
266 | return (grub_gui_container_t) canvas; |
267 | } |