]> git.proxmox.com Git - grub2.git/blob - gfxmenu/widget-box.c
Merge multiterm into gfxmenu+multiterm
[grub2.git] / gfxmenu / widget-box.c
1 /* widget_box.c - Pixmap-stylized box widget. */
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/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/video.h>
25 #include <grub/bitmap.h>
26 #include <grub/bitmap_scale.h>
27 #include <grub/gfxwidgets.h>
28
29 enum box_pixmaps
30 {
31 BOX_PIXMAP_NW, BOX_PIXMAP_NE, BOX_PIXMAP_SE, BOX_PIXMAP_SW,
32 BOX_PIXMAP_N, BOX_PIXMAP_E, BOX_PIXMAP_S, BOX_PIXMAP_W,
33 BOX_PIXMAP_CENTER
34 };
35
36 static const char *box_pixmap_names[] = {
37 /* Corners: */
38 "nw", "ne", "se", "sw",
39 /* Sides: */
40 "n", "e", "s", "w",
41 /* Center: */
42 "c"
43 };
44
45 #define BOX_NUM_PIXMAPS (sizeof(box_pixmap_names)/sizeof(*box_pixmap_names))
46
47 static int
48 get_height (struct grub_video_bitmap *bitmap)
49 {
50 if (bitmap)
51 return grub_video_bitmap_get_height (bitmap);
52 else
53 return 0;
54 }
55
56 static int
57 get_width (struct grub_video_bitmap *bitmap)
58 {
59 if (bitmap)
60 return grub_video_bitmap_get_width (bitmap);
61 else
62 return 0;
63 }
64
65 static void
66 blit (grub_gfxmenu_box_t self, int pixmap_index, int x, int y)
67 {
68 struct grub_video_bitmap *bitmap;
69 bitmap = self->scaled_pixmaps[pixmap_index];
70 if (! bitmap)
71 return;
72 grub_video_blit_bitmap (bitmap, GRUB_VIDEO_BLIT_BLEND,
73 x, y, 0, 0,
74 grub_video_bitmap_get_width (bitmap),
75 grub_video_bitmap_get_height (bitmap));
76 }
77
78 static void
79 draw (grub_gfxmenu_box_t self, int x, int y)
80 {
81 int height_n;
82 int height_s;
83 int height_e;
84 int height_w;
85 int width_n;
86 int width_s;
87 int width_e;
88 int width_w;
89
90 height_n = get_height (self->scaled_pixmaps[BOX_PIXMAP_N]);
91 height_s = get_height (self->scaled_pixmaps[BOX_PIXMAP_S]);
92 height_e = get_height (self->scaled_pixmaps[BOX_PIXMAP_E]);
93 height_w = get_height (self->scaled_pixmaps[BOX_PIXMAP_W]);
94 width_n = get_width (self->scaled_pixmaps[BOX_PIXMAP_N]);
95 width_s = get_width (self->scaled_pixmaps[BOX_PIXMAP_S]);
96 width_e = get_width (self->scaled_pixmaps[BOX_PIXMAP_E]);
97 width_w = get_width (self->scaled_pixmaps[BOX_PIXMAP_W]);
98
99 /* Draw sides. */
100 blit (self, BOX_PIXMAP_N, x + width_w, y);
101 blit (self, BOX_PIXMAP_S, x + width_w, y + height_n + self->content_height);
102 blit (self, BOX_PIXMAP_E, x + width_w + self->content_width, y + height_n);
103 blit (self, BOX_PIXMAP_W, x, y + height_n);
104
105 /* Draw corners. */
106 blit (self, BOX_PIXMAP_NW, x, y);
107 blit (self, BOX_PIXMAP_NE, x + width_w + self->content_width, y);
108 blit (self, BOX_PIXMAP_SE,
109 x + width_w + self->content_width,
110 y + height_n + self->content_height);
111 blit (self, BOX_PIXMAP_SW, x, y + height_n + self->content_height);
112
113 /* Draw center. */
114 blit (self, BOX_PIXMAP_CENTER, x + width_w, y + height_n);
115 }
116
117 static grub_err_t
118 scale_pixmap (grub_gfxmenu_box_t self, int i, int w, int h)
119 {
120 struct grub_video_bitmap **scaled = &self->scaled_pixmaps[i];
121 struct grub_video_bitmap *raw = self->raw_pixmaps[i];
122
123 if (raw == 0)
124 return grub_errno;
125
126 if (w == -1)
127 w = grub_video_bitmap_get_width (raw);
128 if (h == -1)
129 h = grub_video_bitmap_get_height (raw);
130
131 if (*scaled == 0
132 || ((int) grub_video_bitmap_get_width (*scaled) != w)
133 || ((int) grub_video_bitmap_get_height (*scaled) != h))
134 {
135 if (*scaled)
136 {
137 grub_video_bitmap_destroy (*scaled);
138 *scaled = 0;
139 }
140
141 /* Don't try to create a bitmap with a zero dimension. */
142 if (w != 0 && h != 0)
143 grub_video_bitmap_create_scaled (scaled, w, h, raw,
144 GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
145 if (grub_errno != GRUB_ERR_NONE)
146 {
147 grub_error_push ();
148 grub_error (grub_errno,
149 "failed to scale bitmap for styled box pixmap #%d", i);
150 }
151 }
152
153 return grub_errno;
154 }
155
156 static void
157 set_content_size (grub_gfxmenu_box_t self,
158 int width, int height)
159 {
160 self->content_width = width;
161 self->content_height = height;
162
163 /* Resize sides to match the width and height. */
164 /* It is assumed that the corners width/height match the adjacent sides. */
165
166 /* Resize N and S sides to match width. */
167 if (scale_pixmap(self, BOX_PIXMAP_N, width, -1) != GRUB_ERR_NONE)
168 return;
169 if (scale_pixmap(self, BOX_PIXMAP_S, width, -1) != GRUB_ERR_NONE)
170 return;
171
172 /* Resize E and W sides to match height. */
173 if (scale_pixmap(self, BOX_PIXMAP_E, -1, height) != GRUB_ERR_NONE)
174 return;
175 if (scale_pixmap(self, BOX_PIXMAP_W, -1, height) != GRUB_ERR_NONE)
176 return;
177
178 /* Don't scale the corners--they are assumed to match the sides. */
179 if (scale_pixmap(self, BOX_PIXMAP_NW, -1, -1) != GRUB_ERR_NONE)
180 return;
181 if (scale_pixmap(self, BOX_PIXMAP_SW, -1, -1) != GRUB_ERR_NONE)
182 return;
183 if (scale_pixmap(self, BOX_PIXMAP_NE, -1, -1) != GRUB_ERR_NONE)
184 return;
185 if (scale_pixmap(self, BOX_PIXMAP_SE, -1, -1) != GRUB_ERR_NONE)
186 return;
187
188 /* Scale the center area. */
189 if (scale_pixmap(self, BOX_PIXMAP_CENTER, width, height) != GRUB_ERR_NONE)
190 return;
191 }
192
193 static int
194 get_left_pad (grub_gfxmenu_box_t self)
195 {
196 return get_width (self->raw_pixmaps[BOX_PIXMAP_W]);
197 }
198
199 static int
200 get_top_pad (grub_gfxmenu_box_t self)
201 {
202 return get_height (self->raw_pixmaps[BOX_PIXMAP_N]);
203 }
204
205 static int
206 get_right_pad (grub_gfxmenu_box_t self)
207 {
208 return get_width (self->raw_pixmaps[BOX_PIXMAP_E]);
209 }
210
211 static int
212 get_bottom_pad (grub_gfxmenu_box_t self)
213 {
214 return get_height (self->raw_pixmaps[BOX_PIXMAP_S]);
215 }
216
217 static void
218 destroy (grub_gfxmenu_box_t self)
219 {
220 unsigned i;
221 for (i = 0; i < BOX_NUM_PIXMAPS; i++)
222 {
223 if (self->raw_pixmaps[i])
224 grub_video_bitmap_destroy(self->raw_pixmaps[i]);
225 self->raw_pixmaps[i] = 0;
226
227 if (self->scaled_pixmaps[i])
228 grub_video_bitmap_destroy(self->scaled_pixmaps[i]);
229 self->scaled_pixmaps[i] = 0;
230 }
231 grub_free (self->raw_pixmaps);
232 self->raw_pixmaps = 0;
233 grub_free (self->scaled_pixmaps);
234 self->scaled_pixmaps = 0;
235
236 /* Free self: must be the last step! */
237 grub_free (self);
238 }
239
240
241 /* Create a new box. If PIXMAPS_PREFIX and PIXMAPS_SUFFIX are both non-null,
242 then an attempt is made to load the north, south, east, west, northwest,
243 northeast, southeast, southwest, and center pixmaps.
244 If either PIXMAPS_PREFIX or PIXMAPS_SUFFIX is 0, then no pixmaps are
245 loaded, and the box has zero-width borders and is drawn transparent. */
246 grub_gfxmenu_box_t
247 grub_gfxmenu_create_box (const char *pixmaps_prefix,
248 const char *pixmaps_suffix)
249 {
250 unsigned i;
251 grub_gfxmenu_box_t box;
252
253 box = (grub_gfxmenu_box_t) grub_malloc (sizeof (*box));
254 if (! box)
255 return 0;
256
257 box->content_width = 0;
258 box->content_height = 0;
259 box->raw_pixmaps =
260 (struct grub_video_bitmap **)
261 grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
262 box->scaled_pixmaps =
263 (struct grub_video_bitmap **)
264 grub_malloc (BOX_NUM_PIXMAPS * sizeof (struct grub_video_bitmap *));
265
266 /* Initialize all pixmap pointers to NULL so that proper destruction can
267 be performed if an error is encountered partway through construction. */
268 for (i = 0; i < BOX_NUM_PIXMAPS; i++)
269 box->raw_pixmaps[i] = 0;
270 for (i = 0; i < BOX_NUM_PIXMAPS; i++)
271 box->scaled_pixmaps[i] = 0;
272
273 /* Load the pixmaps. */
274 for (i = 0; i < BOX_NUM_PIXMAPS; i++)
275 {
276 if (pixmaps_prefix && pixmaps_suffix)
277 {
278 char *path;
279 char *path_end;
280
281 path = grub_malloc (grub_strlen (pixmaps_prefix)
282 + grub_strlen (box_pixmap_names[i])
283 + grub_strlen (pixmaps_suffix)
284 + 1);
285 if (! path)
286 goto fail_and_destroy;
287
288 /* Construct the specific path for this pixmap. */
289 path_end = grub_stpcpy (path, pixmaps_prefix);
290 path_end = grub_stpcpy (path_end, box_pixmap_names[i]);
291 path_end = grub_stpcpy (path_end, pixmaps_suffix);
292
293 grub_video_bitmap_load (&box->raw_pixmaps[i], path);
294 grub_free (path);
295
296 /* Ignore missing pixmaps. */
297 grub_errno = GRUB_ERR_NONE;
298 }
299 }
300
301 box->draw = draw;
302 box->set_content_size = set_content_size;
303 box->get_left_pad = get_left_pad;
304 box->get_top_pad = get_top_pad;
305 box->get_right_pad = get_right_pad;
306 box->get_bottom_pad = get_bottom_pad;
307 box->destroy = destroy;
308 return box;
309
310 fail_and_destroy:
311 destroy (box);
312 return 0;
313 }