blob: 21fcbea52fc7ab9beedf4639482aeb26b737e0ec [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
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050042void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020043surface_flush_device(cairo_surface_t *surface)
44{
45 cairo_device_t *device;
46
47 device = cairo_surface_get_device(surface);
48 if (device)
49 cairo_device_flush(device);
50}
51
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070052static int
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050053blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050054{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050055 int32_t width, height, stride, x, y, z, w;
56 uint8_t *src, *dst;
57 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050058 int i, j, k, size, half;
Kristian Høgsbergec323d22012-03-21 01:07:49 -040059 uint32_t kernel[71];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050060 double f;
61
Kristian Høgsberg87330262008-11-17 22:23:55 -050062 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063 width = cairo_image_surface_get_width(surface);
64 height = cairo_image_surface_get_height(surface);
65 stride = cairo_image_surface_get_stride(surface);
66 src = cairo_image_surface_get_data(surface);
67
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040068 dst = malloc(height * stride);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070069 if (dst == NULL)
70 return -1;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071
72 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050073 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074 for (i = 0; i < size; i++) {
75 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040076 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050077 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050078 }
79
80 for (i = 0; i < height; i++) {
81 s = (uint32_t *) (src + i * stride);
82 d = (uint32_t *) (dst + i * stride);
83 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050084 if (margin < j && j < width - margin) {
85 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050086 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050087 }
88
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050089 x = 0;
90 y = 0;
91 z = 0;
92 w = 0;
93 for (k = 0; k < size; k++) {
94 if (j - half + k < 0 || j - half + k >= width)
95 continue;
96 p = s[j - half + k];
97
98 x += (p >> 24) * kernel[k];
99 y += ((p >> 16) & 0xff) * kernel[k];
100 z += ((p >> 8) & 0xff) * kernel[k];
101 w += (p & 0xff) * kernel[k];
102 }
103 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
104 }
105 }
106
107 for (i = 0; i < height; i++) {
108 s = (uint32_t *) (dst + i * stride);
109 d = (uint32_t *) (src + i * stride);
110 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500111 if (margin <= i && i < height - margin) {
112 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500113 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500114 }
115
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500116 x = 0;
117 y = 0;
118 z = 0;
119 w = 0;
120 for (k = 0; k < size; k++) {
121 if (i - half + k < 0 || i - half + k >= height)
122 continue;
123 s = (uint32_t *) (dst + (i - half + k) * stride);
124 p = s[j];
125
126 x += (p >> 24) * kernel[k];
127 y += ((p >> 16) & 0xff) * kernel[k];
128 z += ((p >> 8) & 0xff) * kernel[k];
129 w += (p & 0xff) * kernel[k];
130 }
131 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
132 }
133 }
134
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400135 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400136 cairo_surface_mark_dirty(surface);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700137
138 return 0;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400139}
140
141void
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100142render_shadow(cairo_t *cr, cairo_surface_t *surface,
143 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400144{
145 cairo_pattern_t *pattern;
146 cairo_matrix_t matrix;
Marek Chalupaeaea4702014-10-29 14:51:23 +0100147 int i, fx, fy, shadow_height, shadow_width;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400148
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100149 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400150 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
151 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400152 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400153
154 for (i = 0; i < 4; i++) {
Marek Chalupaeaea4702014-10-29 14:51:23 +0100155 /* when fy is set, then we are working with lower corners,
156 * when fx is set, then we are working with right corners
157 *
158 * 00 ------- 01
159 * | |
160 * | |
161 * 10 ------- 11
162 */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400163 fx = i & 1;
164 fy = i >> 1;
165
166 cairo_matrix_init_translate(&matrix,
167 -x + fx * (128 - width),
168 -y + fy * (128 - height));
169 cairo_pattern_set_matrix(pattern, &matrix);
170
Marek Chalupaeaea4702014-10-29 14:51:23 +0100171 shadow_width = margin;
172 shadow_height = fy ? margin : top_margin;
173
174 /* if the shadows together are greater than the surface, we need
175 * to fix it - set the shadow size to the half of
176 * the size of surface. Also handle the case when the size is
177 * not divisible by 2. In that case we need one part of the
178 * shadow to be one pixel greater. !fy or !fx, respectively,
179 * will do the work.
180 */
181 if (height < 2 * shadow_height)
182 shadow_height = (height + !fy) / 2;
183
184 if (width < 2 * shadow_width)
185 shadow_width = (width + !fx) / 2;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400186
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400187 cairo_reset_clip(cr);
188 cairo_rectangle(cr,
Marek Chalupaeaea4702014-10-29 14:51:23 +0100189 x + fx * (width - shadow_width),
190 y + fy * (height - shadow_height),
191 shadow_width, shadow_height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400192 cairo_clip (cr);
193 cairo_mask(cr, pattern);
194 }
195
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400196
Marek Chalupaeaea4702014-10-29 14:51:23 +0100197 shadow_width = width - 2 * margin;
198 shadow_height = top_margin;
199 if (height < 2 * shadow_height)
200 shadow_height = height / 2;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400201
Marek Chalupaeaea4702014-10-29 14:51:23 +0100202 if (shadow_width > 0 && shadow_height) {
203 /* Top stretch */
204 cairo_matrix_init_translate(&matrix, 60, 0);
205 cairo_matrix_scale(&matrix, 8.0 / width, 1);
206 cairo_matrix_translate(&matrix, -x - width / 2, -y);
207 cairo_pattern_set_matrix(pattern, &matrix);
208 cairo_rectangle(cr, x + margin, y, shadow_width, shadow_height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400209
Marek Chalupaeaea4702014-10-29 14:51:23 +0100210 cairo_reset_clip(cr);
211 cairo_rectangle(cr,
212 x + margin, y,
213 shadow_width, shadow_height);
214 cairo_clip (cr);
215 cairo_mask(cr, pattern);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400216
Marek Chalupaeaea4702014-10-29 14:51:23 +0100217 /* Bottom stretch */
218 cairo_matrix_translate(&matrix, 0, -height + 128);
219 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400220
Marek Chalupaeaea4702014-10-29 14:51:23 +0100221 cairo_reset_clip(cr);
222 cairo_rectangle(cr, x + margin, y + height - margin,
223 shadow_width, margin);
224 cairo_clip (cr);
225 cairo_mask(cr, pattern);
226 }
227
228 shadow_width = margin;
229 if (width < 2 * shadow_width)
230 shadow_width = width / 2;
231
232 shadow_height = height - margin - top_margin;
233
234 /* if height is smaller than sum of margins,
235 * then the shadow is already done by the corners */
236 if (shadow_height > 0 && shadow_width) {
237 /* Left stretch */
238 cairo_matrix_init_translate(&matrix, 0, 60);
239 cairo_matrix_scale(&matrix, 1, 8.0 / height);
240 cairo_matrix_translate(&matrix, -x, -y - height / 2);
241 cairo_pattern_set_matrix(pattern, &matrix);
242 cairo_reset_clip(cr);
243 cairo_rectangle(cr, x, y + top_margin,
244 shadow_width, shadow_height);
245 cairo_clip (cr);
246 cairo_mask(cr, pattern);
247
248 /* Right stretch */
249 cairo_matrix_translate(&matrix, -width + 128, 0);
250 cairo_pattern_set_matrix(pattern, &matrix);
251 cairo_rectangle(cr, x + width - shadow_width, y + top_margin,
252 shadow_width, shadow_height);
253 cairo_reset_clip(cr);
254 cairo_clip (cr);
255 cairo_mask(cr, pattern);
256 }
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400257
258 cairo_pattern_destroy(pattern);
259 cairo_reset_clip(cr);
260}
261
262void
263tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400264 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400265{
266 cairo_pattern_t *pattern;
267 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400268 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400269
270 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
271 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400272 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400273 cairo_set_source(cr, pattern);
274 cairo_pattern_destroy(pattern);
275
276 for (i = 0; i < 4; i++) {
277 fx = i & 1;
278 fy = i >> 1;
279
280 cairo_matrix_init_translate(&matrix,
281 -x + fx * (128 - width),
282 -y + fy * (128 - height));
283 cairo_pattern_set_matrix(pattern, &matrix);
284
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400285 if (fy)
286 vmargin = margin;
287 else
288 vmargin = top_margin;
289
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400290 cairo_rectangle(cr,
291 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400292 y + fy * (height - vmargin),
293 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400294 cairo_fill(cr);
295 }
296
Tiago Vignatti0a266412012-02-09 19:06:56 +0200297 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400298 cairo_matrix_init_translate(&matrix, 60, 0);
299 cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400300 cairo_matrix_translate(&matrix, -x - width / 2, -y);
301 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400302 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400303 cairo_fill(cr);
304
Tiago Vignatti0a266412012-02-09 19:06:56 +0200305 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400306 cairo_matrix_translate(&matrix, 0, -height + 128);
307 cairo_pattern_set_matrix(pattern, &matrix);
308 cairo_rectangle(cr, x + margin, y + height - margin,
309 width - 2 * margin, margin);
310 cairo_fill(cr);
311
Tiago Vignatti0a266412012-02-09 19:06:56 +0200312 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400313 cairo_matrix_init_translate(&matrix, 0, 60);
314 cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400315 cairo_matrix_translate(&matrix, -x, -y - height / 2);
316 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400317 cairo_rectangle(cr, x, y + top_margin,
318 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400319 cairo_fill(cr);
320
Tiago Vignatti0a266412012-02-09 19:06:56 +0200321 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400322 cairo_matrix_translate(&matrix, -width + 128, 0);
323 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400324 cairo_rectangle(cr, x + width - margin, y + top_margin,
325 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400326 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500327}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400328
329void
330rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
331{
332 cairo_move_to(cr, x0, y0 + radius);
333 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
334 cairo_line_to(cr, x1 - radius, y0);
335 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
336 cairo_line_to(cr, x1, y1 - radius);
337 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
338 cairo_line_to(cr, x0 + radius, y1);
339 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
340 cairo_close_path(cr);
341}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400342
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500343cairo_surface_t *
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400344load_cairo_surface(const char *filename)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500345{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400346 pixman_image_t *image;
347 int width, height, stride;
348 void *data;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500349
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400350 image = load_image(filename);
Ustun Ergenoglu6dc0f862012-03-14 22:07:58 +0200351 if (image == NULL) {
352 return NULL;
353 }
354
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400355 data = pixman_image_get_data(image);
356 width = pixman_image_get_width(image);
357 height = pixman_image_get_height(image);
358 stride = pixman_image_get_stride(image);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500359
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400360 return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
361 width, height, stride);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500362}
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400363
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700364void
365theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags)
366{
367 cairo_pattern_t *pattern;
368
369 if (flags & THEME_FRAME_ACTIVE) {
370 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
371 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
372 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
373 cairo_set_source(cr, pattern);
374 cairo_pattern_destroy(pattern);
375 } else {
376 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
377 }
378}
379
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400380struct theme *
381theme_create(void)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400382{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400383 struct theme *t;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400384 cairo_t *cr;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400385
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400386 t = malloc(sizeof *t);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700387 if (t == NULL)
388 return NULL;
389
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400390 t->margin = 32;
391 t->width = 6;
392 t->titlebar_height = 27;
393 t->frame_radius = 3;
394 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
395 cr = cairo_create(t->shadow);
396 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
397 cairo_set_source_rgba(cr, 0, 0, 0, 1);
398 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
399 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700400 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
401 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400402 cairo_destroy(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700403 if (blur_surface(t->shadow, 64) == -1)
404 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400405
406 t->active_frame =
407 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
408 cr = cairo_create(t->active_frame);
409 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
410
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700411 theme_set_background_source(t, cr, THEME_FRAME_ACTIVE);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400412 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
413 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700414
415 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
416 goto err_active_frame;
417
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400418 cairo_destroy(cr);
419
420 t->inactive_frame =
421 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
422 cr = cairo_create(t->inactive_frame);
423 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700424 theme_set_background_source(t, cr, 0);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400425 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
426 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700427
428 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
429 goto err_inactive_frame;
430
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400431 cairo_destroy(cr);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400432
433 return t;
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700434
435 err_inactive_frame:
436 cairo_surface_destroy(t->inactive_frame);
437 err_active_frame:
438 cairo_surface_destroy(t->active_frame);
439 err_shadow:
440 cairo_surface_destroy(t->shadow);
441 free(t);
442 return NULL;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400443}
444
445void
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400446theme_destroy(struct theme *t)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400447{
448 cairo_surface_destroy(t->active_frame);
449 cairo_surface_destroy(t->inactive_frame);
450 cairo_surface_destroy(t->shadow);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400451 free(t);
452}
453
454void
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600455theme_render_frame(struct theme *t,
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400456 cairo_t *cr, int width, int height,
Boyan Ding850a5142014-08-05 15:22:04 +0800457 const char *title, struct wl_list *buttons,
458 uint32_t flags)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400459{
460 cairo_text_extents_t extents;
461 cairo_font_extents_t font_extents;
462 cairo_surface_t *source;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700463 int x, y, margin, top_margin;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400464
465 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
466 cairo_set_source_rgba(cr, 0, 0, 0, 0);
467 cairo_paint(cr);
468
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600469 if (flags & THEME_FRAME_MAXIMIZED)
470 margin = 0;
471 else {
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100472 render_shadow(cr, t->shadow,
473 2, 2, width + 8, height + 8,
474 64, 64);
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600475 margin = t->margin;
476 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400477
478 if (flags & THEME_FRAME_ACTIVE)
479 source = t->active_frame;
480 else
481 source = t->inactive_frame;
482
Boyan Ding850a5142014-08-05 15:22:04 +0800483 if (title || !wl_list_empty(buttons))
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700484 top_margin = t->titlebar_height;
485 else
486 top_margin = t->width;
487
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400488 tile_source(cr, source,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600489 margin, margin,
490 width - margin * 2, height - margin * 2,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700491 t->width, top_margin);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400492
Boyan Ding850a5142014-08-05 15:22:04 +0800493 if (title || !wl_list_empty(buttons)) {
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700494 cairo_rectangle (cr, margin + t->width, margin,
495 width - (margin + t->width) * 2,
496 t->titlebar_height - t->width);
497 cairo_clip(cr);
Martin Minarik5f3eddc2012-07-02 23:05:50 +0200498
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700499 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
500 cairo_select_font_face(cr, "sans",
501 CAIRO_FONT_SLANT_NORMAL,
502 CAIRO_FONT_WEIGHT_BOLD);
503 cairo_set_font_size(cr, 14);
504 cairo_text_extents(cr, title, &extents);
505 cairo_font_extents (cr, &font_extents);
506 x = (width - extents.width) / 2;
507 y = margin +
508 (t->titlebar_height -
509 font_extents.ascent - font_extents.descent) / 2 +
510 font_extents.ascent;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400511
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700512 if (flags & THEME_FRAME_ACTIVE) {
513 cairo_move_to(cr, x + 1, y + 1);
514 cairo_set_source_rgb(cr, 1, 1, 1);
515 cairo_show_text(cr, title);
516 cairo_move_to(cr, x, y);
517 cairo_set_source_rgb(cr, 0, 0, 0);
518 cairo_show_text(cr, title);
519 } else {
520 cairo_move_to(cr, x, y);
521 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
522 cairo_show_text(cr, title);
523 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400524 }
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400525}
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400526
527enum theme_location
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600528theme_get_location(struct theme *t, int x, int y,
529 int width, int height, int flags)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400530{
531 int vlocation, hlocation, location;
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400532 int margin, top_margin, grip_size;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400533
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400534 if (flags & THEME_FRAME_MAXIMIZED) {
535 margin = 0;
536 grip_size = 0;
537 } else {
538 margin = t->margin;
539 grip_size = 8;
540 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600541
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700542 if (flags & THEME_FRAME_NO_TITLE)
543 top_margin = t->width;
544 else
545 top_margin = t->titlebar_height;
546
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600547 if (x < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400548 hlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400549 else if (x < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400550 hlocation = THEME_LOCATION_RESIZING_LEFT;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600551 else if (x < width - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400552 hlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600553 else if (x < width - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400554 hlocation = THEME_LOCATION_RESIZING_RIGHT;
555 else
556 hlocation = THEME_LOCATION_EXTERIOR;
557
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600558 if (y < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400559 vlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400560 else if (y < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400561 vlocation = THEME_LOCATION_RESIZING_TOP;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600562 else if (y < height - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400563 vlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600564 else if (y < height - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400565 vlocation = THEME_LOCATION_RESIZING_BOTTOM;
566 else
567 vlocation = THEME_LOCATION_EXTERIOR;
568
569 location = vlocation | hlocation;
570 if (location & THEME_LOCATION_EXTERIOR)
571 location = THEME_LOCATION_EXTERIOR;
572 if (location == THEME_LOCATION_INTERIOR &&
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700573 y < margin + top_margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400574 location = THEME_LOCATION_TITLEBAR;
575 else if (location == THEME_LOCATION_INTERIOR)
576 location = THEME_LOCATION_CLIENT_AREA;
577
578 return location;
579}