blob: 73e3ae70f329f20812fedee9a0274edeac7421bd [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 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050025
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050026#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050030#include <math.h>
Boyan Ding850a5142014-08-05 15:22:04 +080031#include <wayland-util.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050032#include <cairo.h>
33#include "cairo-util.h"
34
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050035#include "image-loader.h"
36#include "config-parser.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050037
Kristian Høgsberg87330262008-11-17 22:23:55 -050038#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
39
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050040void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020041surface_flush_device(cairo_surface_t *surface)
42{
43 cairo_device_t *device;
44
45 device = cairo_surface_get_device(surface);
46 if (device)
47 cairo_device_flush(device);
48}
49
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070050static int
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050051blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050052{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050053 int32_t width, height, stride, x, y, z, w;
54 uint8_t *src, *dst;
55 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050056 int i, j, k, size, half;
Kristian Høgsbergec323d22012-03-21 01:07:49 -040057 uint32_t kernel[71];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058 double f;
59
Kristian Høgsberg87330262008-11-17 22:23:55 -050060 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050061 width = cairo_image_surface_get_width(surface);
62 height = cairo_image_surface_get_height(surface);
63 stride = cairo_image_surface_get_stride(surface);
64 src = cairo_image_surface_get_data(surface);
65
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040066 dst = malloc(height * stride);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070067 if (dst == NULL)
68 return -1;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050069
70 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050071 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072 for (i = 0; i < size; i++) {
73 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040074 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050075 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050076 }
77
78 for (i = 0; i < height; i++) {
79 s = (uint32_t *) (src + i * stride);
80 d = (uint32_t *) (dst + i * stride);
81 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050082 if (margin < j && j < width - margin) {
83 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050084 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050085 }
86
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050087 x = 0;
88 y = 0;
89 z = 0;
90 w = 0;
91 for (k = 0; k < size; k++) {
92 if (j - half + k < 0 || j - half + k >= width)
93 continue;
94 p = s[j - half + k];
95
96 x += (p >> 24) * kernel[k];
97 y += ((p >> 16) & 0xff) * kernel[k];
98 z += ((p >> 8) & 0xff) * kernel[k];
99 w += (p & 0xff) * kernel[k];
100 }
101 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
102 }
103 }
104
105 for (i = 0; i < height; i++) {
106 s = (uint32_t *) (dst + i * stride);
107 d = (uint32_t *) (src + i * stride);
108 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500109 if (margin <= i && i < height - margin) {
110 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500111 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500112 }
113
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500114 x = 0;
115 y = 0;
116 z = 0;
117 w = 0;
118 for (k = 0; k < size; k++) {
119 if (i - half + k < 0 || i - half + k >= height)
120 continue;
121 s = (uint32_t *) (dst + (i - half + k) * stride);
122 p = s[j];
123
124 x += (p >> 24) * kernel[k];
125 y += ((p >> 16) & 0xff) * kernel[k];
126 z += ((p >> 8) & 0xff) * kernel[k];
127 w += (p & 0xff) * kernel[k];
128 }
129 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
130 }
131 }
132
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400133 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400134 cairo_surface_mark_dirty(surface);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700135
136 return 0;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400137}
138
139void
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100140render_shadow(cairo_t *cr, cairo_surface_t *surface,
141 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400142{
143 cairo_pattern_t *pattern;
144 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400145 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400146
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100147 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400148 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
149 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400150 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400151
152 for (i = 0; i < 4; i++) {
153 fx = i & 1;
154 fy = i >> 1;
155
156 cairo_matrix_init_translate(&matrix,
157 -x + fx * (128 - width),
158 -y + fy * (128 - height));
159 cairo_pattern_set_matrix(pattern, &matrix);
160
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400161 if (fy)
162 vmargin = margin;
163 else
164 vmargin = top_margin;
165
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400166 cairo_reset_clip(cr);
167 cairo_rectangle(cr,
168 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400169 y + fy * (height - vmargin),
170 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400171 cairo_clip (cr);
172 cairo_mask(cr, pattern);
173 }
174
Tiago Vignatti0a266412012-02-09 19:06:56 +0200175 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400176 cairo_matrix_init_translate(&matrix, 60, 0);
177 cairo_matrix_scale(&matrix, 8.0 / width, 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400178 cairo_matrix_translate(&matrix, -x - width / 2, -y);
179 cairo_pattern_set_matrix(pattern, &matrix);
180 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
181
182 cairo_reset_clip(cr);
183 cairo_rectangle(cr,
184 x + margin,
185 y,
186 width - 2 * margin, margin);
187 cairo_clip (cr);
188 cairo_mask(cr, pattern);
189
Tiago Vignatti0a266412012-02-09 19:06:56 +0200190 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400191 cairo_matrix_translate(&matrix, 0, -height + 128);
192 cairo_pattern_set_matrix(pattern, &matrix);
193
194 cairo_reset_clip(cr);
195 cairo_rectangle(cr, x + margin, y + height - margin,
196 width - 2 * margin, margin);
197 cairo_clip (cr);
198 cairo_mask(cr, pattern);
199
Tiago Vignatti0a266412012-02-09 19:06:56 +0200200 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400201 cairo_matrix_init_translate(&matrix, 0, 60);
202 cairo_matrix_scale(&matrix, 1, 8.0 / height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400203 cairo_matrix_translate(&matrix, -x, -y - height / 2);
204 cairo_pattern_set_matrix(pattern, &matrix);
205 cairo_reset_clip(cr);
206 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
207 cairo_clip (cr);
208 cairo_mask(cr, pattern);
209
Tiago Vignatti0a266412012-02-09 19:06:56 +0200210 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400211 cairo_matrix_translate(&matrix, -width + 128, 0);
212 cairo_pattern_set_matrix(pattern, &matrix);
213 cairo_rectangle(cr, x + width - margin, y + margin,
214 margin, height - 2 * margin);
215 cairo_reset_clip(cr);
216 cairo_clip (cr);
217 cairo_mask(cr, pattern);
218
219 cairo_pattern_destroy(pattern);
220 cairo_reset_clip(cr);
221}
222
223void
224tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400225 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400226{
227 cairo_pattern_t *pattern;
228 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400229 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400230
231 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
232 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400233 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400234 cairo_set_source(cr, pattern);
235 cairo_pattern_destroy(pattern);
236
237 for (i = 0; i < 4; i++) {
238 fx = i & 1;
239 fy = i >> 1;
240
241 cairo_matrix_init_translate(&matrix,
242 -x + fx * (128 - width),
243 -y + fy * (128 - height));
244 cairo_pattern_set_matrix(pattern, &matrix);
245
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400246 if (fy)
247 vmargin = margin;
248 else
249 vmargin = top_margin;
250
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400251 cairo_rectangle(cr,
252 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400253 y + fy * (height - vmargin),
254 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400255 cairo_fill(cr);
256 }
257
Tiago Vignatti0a266412012-02-09 19:06:56 +0200258 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400259 cairo_matrix_init_translate(&matrix, 60, 0);
260 cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400261 cairo_matrix_translate(&matrix, -x - width / 2, -y);
262 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400263 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400264 cairo_fill(cr);
265
Tiago Vignatti0a266412012-02-09 19:06:56 +0200266 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400267 cairo_matrix_translate(&matrix, 0, -height + 128);
268 cairo_pattern_set_matrix(pattern, &matrix);
269 cairo_rectangle(cr, x + margin, y + height - margin,
270 width - 2 * margin, margin);
271 cairo_fill(cr);
272
Tiago Vignatti0a266412012-02-09 19:06:56 +0200273 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400274 cairo_matrix_init_translate(&matrix, 0, 60);
275 cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400276 cairo_matrix_translate(&matrix, -x, -y - height / 2);
277 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400278 cairo_rectangle(cr, x, y + top_margin,
279 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400280 cairo_fill(cr);
281
Tiago Vignatti0a266412012-02-09 19:06:56 +0200282 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400283 cairo_matrix_translate(&matrix, -width + 128, 0);
284 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400285 cairo_rectangle(cr, x + width - margin, y + top_margin,
286 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400287 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500288}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400289
290void
291rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
292{
293 cairo_move_to(cr, x0, y0 + radius);
294 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
295 cairo_line_to(cr, x1 - radius, y0);
296 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
297 cairo_line_to(cr, x1, y1 - radius);
298 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
299 cairo_line_to(cr, x0 + radius, y1);
300 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
301 cairo_close_path(cr);
302}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400303
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500304cairo_surface_t *
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400305load_cairo_surface(const char *filename)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500306{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400307 pixman_image_t *image;
308 int width, height, stride;
309 void *data;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500310
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400311 image = load_image(filename);
Ustun Ergenoglu6dc0f862012-03-14 22:07:58 +0200312 if (image == NULL) {
313 return NULL;
314 }
315
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400316 data = pixman_image_get_data(image);
317 width = pixman_image_get_width(image);
318 height = pixman_image_get_height(image);
319 stride = pixman_image_get_stride(image);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500320
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400321 return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
322 width, height, stride);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500323}
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400324
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700325void
326theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags)
327{
328 cairo_pattern_t *pattern;
329
330 if (flags & THEME_FRAME_ACTIVE) {
331 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
332 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
333 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
334 cairo_set_source(cr, pattern);
335 cairo_pattern_destroy(pattern);
336 } else {
337 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
338 }
339}
340
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400341struct theme *
342theme_create(void)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400343{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400344 struct theme *t;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400345 cairo_t *cr;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400346
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400347 t = malloc(sizeof *t);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700348 if (t == NULL)
349 return NULL;
350
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400351 t->margin = 32;
352 t->width = 6;
353 t->titlebar_height = 27;
354 t->frame_radius = 3;
355 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
356 cr = cairo_create(t->shadow);
357 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
358 cairo_set_source_rgba(cr, 0, 0, 0, 1);
359 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
360 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700361 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
362 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400363 cairo_destroy(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700364 if (blur_surface(t->shadow, 64) == -1)
365 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400366
367 t->active_frame =
368 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
369 cr = cairo_create(t->active_frame);
370 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
371
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700372 theme_set_background_source(t, cr, THEME_FRAME_ACTIVE);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400373 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
374 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700375
376 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
377 goto err_active_frame;
378
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400379 cairo_destroy(cr);
380
381 t->inactive_frame =
382 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
383 cr = cairo_create(t->inactive_frame);
384 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700385 theme_set_background_source(t, cr, 0);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400386 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
387 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700388
389 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
390 goto err_inactive_frame;
391
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400392 cairo_destroy(cr);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400393
394 return t;
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700395
396 err_inactive_frame:
397 cairo_surface_destroy(t->inactive_frame);
398 err_active_frame:
399 cairo_surface_destroy(t->active_frame);
400 err_shadow:
401 cairo_surface_destroy(t->shadow);
402 free(t);
403 return NULL;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400404}
405
406void
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400407theme_destroy(struct theme *t)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400408{
409 cairo_surface_destroy(t->active_frame);
410 cairo_surface_destroy(t->inactive_frame);
411 cairo_surface_destroy(t->shadow);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400412 free(t);
413}
414
415void
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600416theme_render_frame(struct theme *t,
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400417 cairo_t *cr, int width, int height,
Boyan Ding850a5142014-08-05 15:22:04 +0800418 const char *title, struct wl_list *buttons,
419 uint32_t flags)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400420{
421 cairo_text_extents_t extents;
422 cairo_font_extents_t font_extents;
423 cairo_surface_t *source;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700424 int x, y, margin, top_margin;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400425
426 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
427 cairo_set_source_rgba(cr, 0, 0, 0, 0);
428 cairo_paint(cr);
429
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600430 if (flags & THEME_FRAME_MAXIMIZED)
431 margin = 0;
432 else {
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100433 render_shadow(cr, t->shadow,
434 2, 2, width + 8, height + 8,
435 64, 64);
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600436 margin = t->margin;
437 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400438
439 if (flags & THEME_FRAME_ACTIVE)
440 source = t->active_frame;
441 else
442 source = t->inactive_frame;
443
Boyan Ding850a5142014-08-05 15:22:04 +0800444 if (title || !wl_list_empty(buttons))
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700445 top_margin = t->titlebar_height;
446 else
447 top_margin = t->width;
448
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400449 tile_source(cr, source,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600450 margin, margin,
451 width - margin * 2, height - margin * 2,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700452 t->width, top_margin);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400453
Boyan Ding850a5142014-08-05 15:22:04 +0800454 if (title || !wl_list_empty(buttons)) {
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700455 cairo_rectangle (cr, margin + t->width, margin,
456 width - (margin + t->width) * 2,
457 t->titlebar_height - t->width);
458 cairo_clip(cr);
Martin Minarik5f3eddc2012-07-02 23:05:50 +0200459
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700460 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
461 cairo_select_font_face(cr, "sans",
462 CAIRO_FONT_SLANT_NORMAL,
463 CAIRO_FONT_WEIGHT_BOLD);
464 cairo_set_font_size(cr, 14);
465 cairo_text_extents(cr, title, &extents);
466 cairo_font_extents (cr, &font_extents);
467 x = (width - extents.width) / 2;
468 y = margin +
469 (t->titlebar_height -
470 font_extents.ascent - font_extents.descent) / 2 +
471 font_extents.ascent;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400472
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700473 if (flags & THEME_FRAME_ACTIVE) {
474 cairo_move_to(cr, x + 1, y + 1);
475 cairo_set_source_rgb(cr, 1, 1, 1);
476 cairo_show_text(cr, title);
477 cairo_move_to(cr, x, y);
478 cairo_set_source_rgb(cr, 0, 0, 0);
479 cairo_show_text(cr, title);
480 } else {
481 cairo_move_to(cr, x, y);
482 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
483 cairo_show_text(cr, title);
484 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400485 }
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400486}
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400487
488enum theme_location
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600489theme_get_location(struct theme *t, int x, int y,
490 int width, int height, int flags)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400491{
492 int vlocation, hlocation, location;
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400493 int margin, top_margin, grip_size;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400494
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400495 if (flags & THEME_FRAME_MAXIMIZED) {
496 margin = 0;
497 grip_size = 0;
498 } else {
499 margin = t->margin;
500 grip_size = 8;
501 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600502
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700503 if (flags & THEME_FRAME_NO_TITLE)
504 top_margin = t->width;
505 else
506 top_margin = t->titlebar_height;
507
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600508 if (x < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400509 hlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400510 else if (x < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400511 hlocation = THEME_LOCATION_RESIZING_LEFT;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600512 else if (x < width - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400513 hlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600514 else if (x < width - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400515 hlocation = THEME_LOCATION_RESIZING_RIGHT;
516 else
517 hlocation = THEME_LOCATION_EXTERIOR;
518
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600519 if (y < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400520 vlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400521 else if (y < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400522 vlocation = THEME_LOCATION_RESIZING_TOP;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600523 else if (y < height - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400524 vlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600525 else if (y < height - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400526 vlocation = THEME_LOCATION_RESIZING_BOTTOM;
527 else
528 vlocation = THEME_LOCATION_EXTERIOR;
529
530 location = vlocation | hlocation;
531 if (location & THEME_LOCATION_EXTERIOR)
532 location = THEME_LOCATION_EXTERIOR;
533 if (location == THEME_LOCATION_INTERIOR &&
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700534 y < margin + top_margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400535 location = THEME_LOCATION_TITLEBAR;
536 else if (location == THEME_LOCATION_INTERIOR)
537 location = THEME_LOCATION_CLIENT_AREA;
538
539 return location;
540}