blob: d71e0ed48c73ac19af118f4f033f25baaa2946a4 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Kristian Høgsberg902865c2012-02-08 10:11:42 -05003 * Copyright © 2012 Intel Corporation
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050012 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050028
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050029#include <stdint.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050033#include <math.h>
Boyan Ding850a5142014-08-05 15:22:04 +080034#include <wayland-util.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050035#include <cairo.h>
36#include "cairo-util.h"
37
Jon Cruz35b2eaa2015-06-15 15:37:08 -070038#include "shared/helpers.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050039#include "image-loader.h"
40#include "config-parser.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050041
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -050042#ifdef HAVE_PANGO
43#include <pango/pangocairo.h>
44#endif
45
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050046void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020047surface_flush_device(cairo_surface_t *surface)
48{
49 cairo_device_t *device;
50
51 device = cairo_surface_get_device(surface);
52 if (device)
53 cairo_device_flush(device);
54}
55
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070056static int
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050057blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050059 int32_t width, height, stride, x, y, z, w;
60 uint8_t *src, *dst;
61 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050062 int i, j, k, size, half;
Kristian Høgsbergec323d22012-03-21 01:07:49 -040063 uint32_t kernel[71];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064 double f;
65
Kristian Høgsberg87330262008-11-17 22:23:55 -050066 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050067 width = cairo_image_surface_get_width(surface);
68 height = cairo_image_surface_get_height(surface);
69 stride = cairo_image_surface_get_stride(surface);
70 src = cairo_image_surface_get_data(surface);
71
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040072 dst = malloc(height * stride);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070073 if (dst == NULL)
74 return -1;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050075
76 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050077 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050078 for (i = 0; i < size; i++) {
79 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040080 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050081 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050082 }
83
84 for (i = 0; i < height; i++) {
85 s = (uint32_t *) (src + i * stride);
86 d = (uint32_t *) (dst + i * stride);
87 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050088 if (margin < j && j < width - margin) {
89 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050090 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050091 }
92
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050093 x = 0;
94 y = 0;
95 z = 0;
96 w = 0;
97 for (k = 0; k < size; k++) {
98 if (j - half + k < 0 || j - half + k >= width)
99 continue;
100 p = s[j - half + k];
101
102 x += (p >> 24) * kernel[k];
103 y += ((p >> 16) & 0xff) * kernel[k];
104 z += ((p >> 8) & 0xff) * kernel[k];
105 w += (p & 0xff) * kernel[k];
106 }
107 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
108 }
109 }
110
111 for (i = 0; i < height; i++) {
112 s = (uint32_t *) (dst + i * stride);
113 d = (uint32_t *) (src + i * stride);
114 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500115 if (margin <= i && i < height - margin) {
116 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500117 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500118 }
119
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500120 x = 0;
121 y = 0;
122 z = 0;
123 w = 0;
124 for (k = 0; k < size; k++) {
125 if (i - half + k < 0 || i - half + k >= height)
126 continue;
127 s = (uint32_t *) (dst + (i - half + k) * stride);
128 p = s[j];
129
130 x += (p >> 24) * kernel[k];
131 y += ((p >> 16) & 0xff) * kernel[k];
132 z += ((p >> 8) & 0xff) * kernel[k];
133 w += (p & 0xff) * kernel[k];
134 }
135 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
136 }
137 }
138
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400139 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400140 cairo_surface_mark_dirty(surface);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700141
142 return 0;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400143}
144
145void
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100146render_shadow(cairo_t *cr, cairo_surface_t *surface,
147 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400148{
149 cairo_pattern_t *pattern;
150 cairo_matrix_t matrix;
Marek Chalupaeaea4702014-10-29 14:51:23 +0100151 int i, fx, fy, shadow_height, shadow_width;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400152
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100153 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400154 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
155 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400156 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400157
158 for (i = 0; i < 4; i++) {
Marek Chalupaeaea4702014-10-29 14:51:23 +0100159 /* when fy is set, then we are working with lower corners,
160 * when fx is set, then we are working with right corners
161 *
162 * 00 ------- 01
163 * | |
164 * | |
165 * 10 ------- 11
166 */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400167 fx = i & 1;
168 fy = i >> 1;
169
170 cairo_matrix_init_translate(&matrix,
171 -x + fx * (128 - width),
172 -y + fy * (128 - height));
173 cairo_pattern_set_matrix(pattern, &matrix);
174
Marek Chalupaeaea4702014-10-29 14:51:23 +0100175 shadow_width = margin;
176 shadow_height = fy ? margin : top_margin;
177
178 /* if the shadows together are greater than the surface, we need
179 * to fix it - set the shadow size to the half of
180 * the size of surface. Also handle the case when the size is
181 * not divisible by 2. In that case we need one part of the
182 * shadow to be one pixel greater. !fy or !fx, respectively,
183 * will do the work.
184 */
185 if (height < 2 * shadow_height)
186 shadow_height = (height + !fy) / 2;
187
188 if (width < 2 * shadow_width)
189 shadow_width = (width + !fx) / 2;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400190
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400191 cairo_reset_clip(cr);
192 cairo_rectangle(cr,
Marek Chalupaeaea4702014-10-29 14:51:23 +0100193 x + fx * (width - shadow_width),
194 y + fy * (height - shadow_height),
195 shadow_width, shadow_height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400196 cairo_clip (cr);
197 cairo_mask(cr, pattern);
198 }
199
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400200
Marek Chalupaeaea4702014-10-29 14:51:23 +0100201 shadow_width = width - 2 * margin;
202 shadow_height = top_margin;
203 if (height < 2 * shadow_height)
204 shadow_height = height / 2;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400205
Marek Chalupaeaea4702014-10-29 14:51:23 +0100206 if (shadow_width > 0 && shadow_height) {
207 /* Top stretch */
208 cairo_matrix_init_translate(&matrix, 60, 0);
209 cairo_matrix_scale(&matrix, 8.0 / width, 1);
210 cairo_matrix_translate(&matrix, -x - width / 2, -y);
211 cairo_pattern_set_matrix(pattern, &matrix);
212 cairo_rectangle(cr, x + margin, y, shadow_width, shadow_height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400213
Marek Chalupaeaea4702014-10-29 14:51:23 +0100214 cairo_reset_clip(cr);
215 cairo_rectangle(cr,
216 x + margin, y,
217 shadow_width, shadow_height);
218 cairo_clip (cr);
219 cairo_mask(cr, pattern);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400220
Marek Chalupaeaea4702014-10-29 14:51:23 +0100221 /* Bottom stretch */
222 cairo_matrix_translate(&matrix, 0, -height + 128);
223 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400224
Marek Chalupaeaea4702014-10-29 14:51:23 +0100225 cairo_reset_clip(cr);
226 cairo_rectangle(cr, x + margin, y + height - margin,
227 shadow_width, margin);
228 cairo_clip (cr);
229 cairo_mask(cr, pattern);
230 }
231
232 shadow_width = margin;
233 if (width < 2 * shadow_width)
234 shadow_width = width / 2;
235
236 shadow_height = height - margin - top_margin;
237
238 /* if height is smaller than sum of margins,
239 * then the shadow is already done by the corners */
240 if (shadow_height > 0 && shadow_width) {
241 /* Left stretch */
242 cairo_matrix_init_translate(&matrix, 0, 60);
243 cairo_matrix_scale(&matrix, 1, 8.0 / height);
244 cairo_matrix_translate(&matrix, -x, -y - height / 2);
245 cairo_pattern_set_matrix(pattern, &matrix);
246 cairo_reset_clip(cr);
247 cairo_rectangle(cr, x, y + top_margin,
248 shadow_width, shadow_height);
249 cairo_clip (cr);
250 cairo_mask(cr, pattern);
251
252 /* Right stretch */
253 cairo_matrix_translate(&matrix, -width + 128, 0);
254 cairo_pattern_set_matrix(pattern, &matrix);
255 cairo_rectangle(cr, x + width - shadow_width, y + top_margin,
256 shadow_width, shadow_height);
257 cairo_reset_clip(cr);
258 cairo_clip (cr);
259 cairo_mask(cr, pattern);
260 }
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400261
262 cairo_pattern_destroy(pattern);
263 cairo_reset_clip(cr);
264}
265
266void
267tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400268 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400269{
270 cairo_pattern_t *pattern;
271 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400272 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400273
274 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
275 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400276 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400277 cairo_set_source(cr, pattern);
278 cairo_pattern_destroy(pattern);
279
280 for (i = 0; i < 4; i++) {
281 fx = i & 1;
282 fy = i >> 1;
283
284 cairo_matrix_init_translate(&matrix,
285 -x + fx * (128 - width),
286 -y + fy * (128 - height));
287 cairo_pattern_set_matrix(pattern, &matrix);
288
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400289 if (fy)
290 vmargin = margin;
291 else
292 vmargin = top_margin;
293
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400294 cairo_rectangle(cr,
295 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400296 y + fy * (height - vmargin),
297 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400298 cairo_fill(cr);
299 }
300
Tiago Vignatti0a266412012-02-09 19:06:56 +0200301 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400302 cairo_matrix_init_translate(&matrix, 60, 0);
303 cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400304 cairo_matrix_translate(&matrix, -x - width / 2, -y);
305 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400306 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400307 cairo_fill(cr);
308
Tiago Vignatti0a266412012-02-09 19:06:56 +0200309 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400310 cairo_matrix_translate(&matrix, 0, -height + 128);
311 cairo_pattern_set_matrix(pattern, &matrix);
312 cairo_rectangle(cr, x + margin, y + height - margin,
313 width - 2 * margin, margin);
314 cairo_fill(cr);
315
Tiago Vignatti0a266412012-02-09 19:06:56 +0200316 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400317 cairo_matrix_init_translate(&matrix, 0, 60);
318 cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400319 cairo_matrix_translate(&matrix, -x, -y - height / 2);
320 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400321 cairo_rectangle(cr, x, y + top_margin,
322 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400323 cairo_fill(cr);
324
Tiago Vignatti0a266412012-02-09 19:06:56 +0200325 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400326 cairo_matrix_translate(&matrix, -width + 128, 0);
327 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400328 cairo_rectangle(cr, x + width - margin, y + top_margin,
329 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400330 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500331}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400332
333void
334rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
335{
336 cairo_move_to(cr, x0, y0 + radius);
337 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
338 cairo_line_to(cr, x1 - radius, y0);
339 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
340 cairo_line_to(cr, x1, y1 - radius);
341 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
342 cairo_line_to(cr, x0 + radius, y1);
343 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
344 cairo_close_path(cr);
345}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400346
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500347cairo_surface_t *
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400348load_cairo_surface(const char *filename)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500349{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400350 pixman_image_t *image;
351 int width, height, stride;
352 void *data;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500353
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400354 image = load_image(filename);
Ustun Ergenoglu6dc0f862012-03-14 22:07:58 +0200355 if (image == NULL) {
356 return NULL;
357 }
358
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400359 data = pixman_image_get_data(image);
360 width = pixman_image_get_width(image);
361 height = pixman_image_get_height(image);
362 stride = pixman_image_get_stride(image);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500363
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400364 return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
365 width, height, stride);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500366}
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400367
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700368void
369theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags)
370{
371 cairo_pattern_t *pattern;
372
373 if (flags & THEME_FRAME_ACTIVE) {
374 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
375 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
376 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
377 cairo_set_source(cr, pattern);
378 cairo_pattern_destroy(pattern);
379 } else {
380 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
381 }
382}
383
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400384struct theme *
385theme_create(void)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400386{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400387 struct theme *t;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400388 cairo_t *cr;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400389
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400390 t = malloc(sizeof *t);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700391 if (t == NULL)
392 return NULL;
393
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400394 t->margin = 32;
395 t->width = 6;
396 t->titlebar_height = 27;
397 t->frame_radius = 3;
398 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
399 cr = cairo_create(t->shadow);
400 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
401 cairo_set_source_rgba(cr, 0, 0, 0, 1);
402 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
403 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700404 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
405 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400406 cairo_destroy(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700407 if (blur_surface(t->shadow, 64) == -1)
408 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400409
410 t->active_frame =
411 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
412 cr = cairo_create(t->active_frame);
413 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
414
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700415 theme_set_background_source(t, cr, THEME_FRAME_ACTIVE);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400416 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
417 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700418
419 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
420 goto err_active_frame;
421
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400422 cairo_destroy(cr);
423
424 t->inactive_frame =
425 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
426 cr = cairo_create(t->inactive_frame);
427 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700428 theme_set_background_source(t, cr, 0);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400429 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
430 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700431
432 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
433 goto err_inactive_frame;
434
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400435 cairo_destroy(cr);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400436
437 return t;
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700438
439 err_inactive_frame:
440 cairo_surface_destroy(t->inactive_frame);
441 err_active_frame:
442 cairo_surface_destroy(t->active_frame);
443 err_shadow:
444 cairo_surface_destroy(t->shadow);
445 free(t);
446 return NULL;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400447}
448
449void
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400450theme_destroy(struct theme *t)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400451{
452 cairo_surface_destroy(t->active_frame);
453 cairo_surface_destroy(t->inactive_frame);
454 cairo_surface_destroy(t->shadow);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400455 free(t);
456}
457
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500458#ifdef HAVE_PANGO
459static PangoLayout *
460create_layout(cairo_t *cr, const char *title)
461{
462 PangoLayout *layout;
463 PangoFontDescription *desc;
464
465 layout = pango_cairo_create_layout(cr);
466 pango_layout_set_text(layout, title, -1);
467 desc = pango_font_description_from_string("Sans Bold 10");
468 pango_layout_set_font_description(layout, desc);
469 pango_font_description_free(desc);
470 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
471 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
472 pango_layout_set_auto_dir (layout, FALSE);
473 pango_layout_set_single_paragraph_mode (layout, TRUE);
474 pango_layout_set_width (layout, -1);
475
476 return layout;
477}
478#endif
479
480#ifdef HAVE_PANGO
481#define SHOW_TEXT(cr) \
482 pango_cairo_show_layout(cr, title_layout)
483#else
484#define SHOW_TEXT(cr) \
485 cairo_show_text(cr, title)
486#endif
487
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400488void
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600489theme_render_frame(struct theme *t,
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400490 cairo_t *cr, int width, int height,
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500491 const char *title, cairo_rectangle_int_t *title_rect,
492 struct wl_list *buttons, uint32_t flags)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400493{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400494 cairo_surface_t *source;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700495 int x, y, margin, top_margin;
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500496 int text_width, text_height;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400497
498 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
499 cairo_set_source_rgba(cr, 0, 0, 0, 0);
500 cairo_paint(cr);
501
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600502 if (flags & THEME_FRAME_MAXIMIZED)
503 margin = 0;
504 else {
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100505 render_shadow(cr, t->shadow,
506 2, 2, width + 8, height + 8,
507 64, 64);
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600508 margin = t->margin;
509 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400510
511 if (flags & THEME_FRAME_ACTIVE)
512 source = t->active_frame;
513 else
514 source = t->inactive_frame;
515
Boyan Ding850a5142014-08-05 15:22:04 +0800516 if (title || !wl_list_empty(buttons))
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700517 top_margin = t->titlebar_height;
518 else
519 top_margin = t->width;
520
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400521 tile_source(cr, source,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600522 margin, margin,
523 width - margin * 2, height - margin * 2,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700524 t->width, top_margin);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400525
Boyan Ding850a5142014-08-05 15:22:04 +0800526 if (title || !wl_list_empty(buttons)) {
Martin Minarik5f3eddc2012-07-02 23:05:50 +0200527
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500528 cairo_rectangle (cr, title_rect->x, title_rect->y,
529 title_rect->width, title_rect->height);
530 cairo_clip(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700531 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500532
533#ifdef HAVE_PANGO
534 PangoLayout *title_layout;
535 PangoRectangle logical;
536
537 title_layout = create_layout(cr, title);
538
539 pango_layout_get_pixel_extents (title_layout, NULL, &logical);
540 text_width = MIN(title_rect->width, logical.width);
541 text_height = logical.height;
542 if (text_width < logical.width)
543 pango_layout_set_width (title_layout, text_width * PANGO_SCALE);
544
545#else
546 cairo_text_extents_t extents;
547 cairo_font_extents_t font_extents;
548
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700549 cairo_select_font_face(cr, "sans",
550 CAIRO_FONT_SLANT_NORMAL,
551 CAIRO_FONT_WEIGHT_BOLD);
552 cairo_set_font_size(cr, 14);
553 cairo_text_extents(cr, title, &extents);
554 cairo_font_extents (cr, &font_extents);
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500555 text_width = extents.width;
556 text_height = font_extents.descent - font_extents.ascent;
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500557#endif
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500558
559 x = (width - text_width) / 2;
560 y = margin + (t->titlebar_height - text_height) / 2;
561 if (x < title_rect->x)
562 x = title_rect->x;
563 else if (x + text_width > (title_rect->x + title_rect->width))
564 x = (title_rect->x + title_rect->width) - text_width;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400565
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700566 if (flags & THEME_FRAME_ACTIVE) {
567 cairo_move_to(cr, x + 1, y + 1);
568 cairo_set_source_rgb(cr, 1, 1, 1);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500569 SHOW_TEXT(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700570 cairo_move_to(cr, x, y);
571 cairo_set_source_rgb(cr, 0, 0, 0);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500572 SHOW_TEXT(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700573 } else {
574 cairo_move_to(cr, x, y);
575 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500576 SHOW_TEXT(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700577 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400578 }
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400579}
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400580
581enum theme_location
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600582theme_get_location(struct theme *t, int x, int y,
583 int width, int height, int flags)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400584{
585 int vlocation, hlocation, location;
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400586 int margin, top_margin, grip_size;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400587
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400588 if (flags & THEME_FRAME_MAXIMIZED) {
589 margin = 0;
590 grip_size = 0;
591 } else {
592 margin = t->margin;
593 grip_size = 8;
594 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600595
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700596 if (flags & THEME_FRAME_NO_TITLE)
597 top_margin = t->width;
598 else
599 top_margin = t->titlebar_height;
600
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600601 if (x < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400602 hlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400603 else if (x < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400604 hlocation = THEME_LOCATION_RESIZING_LEFT;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600605 else if (x < width - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400606 hlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600607 else if (x < width - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400608 hlocation = THEME_LOCATION_RESIZING_RIGHT;
609 else
610 hlocation = THEME_LOCATION_EXTERIOR;
611
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600612 if (y < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400613 vlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400614 else if (y < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400615 vlocation = THEME_LOCATION_RESIZING_TOP;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600616 else if (y < height - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400617 vlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600618 else if (y < height - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400619 vlocation = THEME_LOCATION_RESIZING_BOTTOM;
620 else
621 vlocation = THEME_LOCATION_EXTERIOR;
622
623 location = vlocation | hlocation;
624 if (location & THEME_LOCATION_EXTERIOR)
625 location = THEME_LOCATION_EXTERIOR;
626 if (location == THEME_LOCATION_INTERIOR &&
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700627 y < margin + top_margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400628 location = THEME_LOCATION_TITLEBAR;
629 else if (location == THEME_LOCATION_INTERIOR)
630 location = THEME_LOCATION_CLIENT_AREA;
631
632 return location;
633}