blob: 4305ba690662dd316da865da46adb30918b0b4ca [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>
31#include <cairo.h>
32#include "cairo-util.h"
33
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050034#include "image-loader.h"
35#include "config-parser.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050036
Kristian Høgsberg87330262008-11-17 22:23:55 -050037#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
38
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050039void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020040surface_flush_device(cairo_surface_t *surface)
41{
42 cairo_device_t *device;
43
44 device = cairo_surface_get_device(surface);
45 if (device)
46 cairo_device_flush(device);
47}
48
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070049static int
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050050blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050051{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050052 int32_t width, height, stride, x, y, z, w;
53 uint8_t *src, *dst;
54 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050055 int i, j, k, size, half;
Kristian Høgsbergec323d22012-03-21 01:07:49 -040056 uint32_t kernel[71];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057 double f;
58
Kristian Høgsberg87330262008-11-17 22:23:55 -050059 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050060 width = cairo_image_surface_get_width(surface);
61 height = cairo_image_surface_get_height(surface);
62 stride = cairo_image_surface_get_stride(surface);
63 src = cairo_image_surface_get_data(surface);
64
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040065 dst = malloc(height * stride);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070066 if (dst == NULL)
67 return -1;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050068
69 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050070 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071 for (i = 0; i < size; i++) {
72 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040073 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050074 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050075 }
76
77 for (i = 0; i < height; i++) {
78 s = (uint32_t *) (src + i * stride);
79 d = (uint32_t *) (dst + i * stride);
80 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050081 if (margin < j && j < width - margin) {
82 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050083 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050084 }
85
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050086 x = 0;
87 y = 0;
88 z = 0;
89 w = 0;
90 for (k = 0; k < size; k++) {
91 if (j - half + k < 0 || j - half + k >= width)
92 continue;
93 p = s[j - half + k];
94
95 x += (p >> 24) * kernel[k];
96 y += ((p >> 16) & 0xff) * kernel[k];
97 z += ((p >> 8) & 0xff) * kernel[k];
98 w += (p & 0xff) * kernel[k];
99 }
100 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
101 }
102 }
103
104 for (i = 0; i < height; i++) {
105 s = (uint32_t *) (dst + i * stride);
106 d = (uint32_t *) (src + i * stride);
107 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500108 if (margin <= i && i < height - margin) {
109 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500110 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500111 }
112
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500113 x = 0;
114 y = 0;
115 z = 0;
116 w = 0;
117 for (k = 0; k < size; k++) {
118 if (i - half + k < 0 || i - half + k >= height)
119 continue;
120 s = (uint32_t *) (dst + (i - half + k) * stride);
121 p = s[j];
122
123 x += (p >> 24) * kernel[k];
124 y += ((p >> 16) & 0xff) * kernel[k];
125 z += ((p >> 8) & 0xff) * kernel[k];
126 w += (p & 0xff) * kernel[k];
127 }
128 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
129 }
130 }
131
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400132 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400133 cairo_surface_mark_dirty(surface);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700134
135 return 0;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400136}
137
138void
139tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400140 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400141{
142 cairo_pattern_t *pattern;
143 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400144 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400145
146 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
147 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400148 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400149
150 for (i = 0; i < 4; i++) {
151 fx = i & 1;
152 fy = i >> 1;
153
154 cairo_matrix_init_translate(&matrix,
155 -x + fx * (128 - width),
156 -y + fy * (128 - height));
157 cairo_pattern_set_matrix(pattern, &matrix);
158
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400159 if (fy)
160 vmargin = margin;
161 else
162 vmargin = top_margin;
163
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400164 cairo_reset_clip(cr);
165 cairo_rectangle(cr,
166 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400167 y + fy * (height - vmargin),
168 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400169 cairo_clip (cr);
170 cairo_mask(cr, pattern);
171 }
172
Tiago Vignatti0a266412012-02-09 19:06:56 +0200173 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400174 cairo_matrix_init_translate(&matrix, 60, 0);
175 cairo_matrix_scale(&matrix, 8.0 / width, 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400176 cairo_matrix_translate(&matrix, -x - width / 2, -y);
177 cairo_pattern_set_matrix(pattern, &matrix);
178 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
179
180 cairo_reset_clip(cr);
181 cairo_rectangle(cr,
182 x + margin,
183 y,
184 width - 2 * margin, margin);
185 cairo_clip (cr);
186 cairo_mask(cr, pattern);
187
Tiago Vignatti0a266412012-02-09 19:06:56 +0200188 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400189 cairo_matrix_translate(&matrix, 0, -height + 128);
190 cairo_pattern_set_matrix(pattern, &matrix);
191
192 cairo_reset_clip(cr);
193 cairo_rectangle(cr, x + margin, y + height - margin,
194 width - 2 * margin, margin);
195 cairo_clip (cr);
196 cairo_mask(cr, pattern);
197
Tiago Vignatti0a266412012-02-09 19:06:56 +0200198 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400199 cairo_matrix_init_translate(&matrix, 0, 60);
200 cairo_matrix_scale(&matrix, 1, 8.0 / height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400201 cairo_matrix_translate(&matrix, -x, -y - height / 2);
202 cairo_pattern_set_matrix(pattern, &matrix);
203 cairo_reset_clip(cr);
204 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
205 cairo_clip (cr);
206 cairo_mask(cr, pattern);
207
Tiago Vignatti0a266412012-02-09 19:06:56 +0200208 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400209 cairo_matrix_translate(&matrix, -width + 128, 0);
210 cairo_pattern_set_matrix(pattern, &matrix);
211 cairo_rectangle(cr, x + width - margin, y + margin,
212 margin, height - 2 * margin);
213 cairo_reset_clip(cr);
214 cairo_clip (cr);
215 cairo_mask(cr, pattern);
216
217 cairo_pattern_destroy(pattern);
218 cairo_reset_clip(cr);
219}
220
221void
222tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400223 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400224{
225 cairo_pattern_t *pattern;
226 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400227 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400228
229 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
230 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400231 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400232 cairo_set_source(cr, pattern);
233 cairo_pattern_destroy(pattern);
234
235 for (i = 0; i < 4; i++) {
236 fx = i & 1;
237 fy = i >> 1;
238
239 cairo_matrix_init_translate(&matrix,
240 -x + fx * (128 - width),
241 -y + fy * (128 - height));
242 cairo_pattern_set_matrix(pattern, &matrix);
243
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400244 if (fy)
245 vmargin = margin;
246 else
247 vmargin = top_margin;
248
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400249 cairo_rectangle(cr,
250 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400251 y + fy * (height - vmargin),
252 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400253 cairo_fill(cr);
254 }
255
Tiago Vignatti0a266412012-02-09 19:06:56 +0200256 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400257 cairo_matrix_init_translate(&matrix, 60, 0);
258 cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400259 cairo_matrix_translate(&matrix, -x - width / 2, -y);
260 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400261 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400262 cairo_fill(cr);
263
Tiago Vignatti0a266412012-02-09 19:06:56 +0200264 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400265 cairo_matrix_translate(&matrix, 0, -height + 128);
266 cairo_pattern_set_matrix(pattern, &matrix);
267 cairo_rectangle(cr, x + margin, y + height - margin,
268 width - 2 * margin, margin);
269 cairo_fill(cr);
270
Tiago Vignatti0a266412012-02-09 19:06:56 +0200271 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400272 cairo_matrix_init_translate(&matrix, 0, 60);
273 cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400274 cairo_matrix_translate(&matrix, -x, -y - height / 2);
275 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400276 cairo_rectangle(cr, x, y + top_margin,
277 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400278 cairo_fill(cr);
279
Tiago Vignatti0a266412012-02-09 19:06:56 +0200280 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400281 cairo_matrix_translate(&matrix, -width + 128, 0);
282 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400283 cairo_rectangle(cr, x + width - margin, y + top_margin,
284 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400285 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500286}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400287
288void
289rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
290{
291 cairo_move_to(cr, x0, y0 + radius);
292 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
293 cairo_line_to(cr, x1 - radius, y0);
294 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
295 cairo_line_to(cr, x1, y1 - radius);
296 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
297 cairo_line_to(cr, x0 + radius, y1);
298 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
299 cairo_close_path(cr);
300}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400301
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500302cairo_surface_t *
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400303load_cairo_surface(const char *filename)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500304{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400305 pixman_image_t *image;
306 int width, height, stride;
307 void *data;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500308
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400309 image = load_image(filename);
Ustun Ergenoglu6dc0f862012-03-14 22:07:58 +0200310 if (image == NULL) {
311 return NULL;
312 }
313
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400314 data = pixman_image_get_data(image);
315 width = pixman_image_get_width(image);
316 height = pixman_image_get_height(image);
317 stride = pixman_image_get_stride(image);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500318
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400319 return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
320 width, height, stride);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500321}
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400322
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400323struct theme *
324theme_create(void)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400325{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400326 struct theme *t;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400327 cairo_t *cr;
328 cairo_pattern_t *pattern;
329
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400330 t = malloc(sizeof *t);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700331 if (t == NULL)
332 return NULL;
333
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400334 t->margin = 32;
335 t->width = 6;
336 t->titlebar_height = 27;
337 t->frame_radius = 3;
338 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
339 cr = cairo_create(t->shadow);
340 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
341 cairo_set_source_rgba(cr, 0, 0, 0, 1);
342 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
343 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700344 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
345 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400346 cairo_destroy(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700347 if (blur_surface(t->shadow, 64) == -1)
348 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400349
350 t->active_frame =
351 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
352 cr = cairo_create(t->active_frame);
353 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
354
355 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
356 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
357 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
358 cairo_set_source(cr, pattern);
359 cairo_pattern_destroy(pattern);
360
361 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
362 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700363
364 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
365 goto err_active_frame;
366
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400367 cairo_destroy(cr);
368
369 t->inactive_frame =
370 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
371 cr = cairo_create(t->inactive_frame);
372 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
373 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
374 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
375 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700376
377 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
378 goto err_inactive_frame;
379
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400380 cairo_destroy(cr);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400381
382 return t;
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700383
384 err_inactive_frame:
385 cairo_surface_destroy(t->inactive_frame);
386 err_active_frame:
387 cairo_surface_destroy(t->active_frame);
388 err_shadow:
389 cairo_surface_destroy(t->shadow);
390 free(t);
391 return NULL;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400392}
393
394void
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400395theme_destroy(struct theme *t)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400396{
397 cairo_surface_destroy(t->active_frame);
398 cairo_surface_destroy(t->inactive_frame);
399 cairo_surface_destroy(t->shadow);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400400 free(t);
401}
402
403void
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600404theme_render_frame(struct theme *t,
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400405 cairo_t *cr, int width, int height,
406 const char *title, uint32_t flags)
407{
408 cairo_text_extents_t extents;
409 cairo_font_extents_t font_extents;
410 cairo_surface_t *source;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600411 int x, y, margin;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400412
413 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
414 cairo_set_source_rgba(cr, 0, 0, 0, 0);
415 cairo_paint(cr);
416
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600417 if (flags & THEME_FRAME_MAXIMIZED)
418 margin = 0;
419 else {
420 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
421 tile_mask(cr, t->shadow,
422 2, 2, width + 8, height + 8,
423 64, 64);
424 margin = t->margin;
425 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400426
427 if (flags & THEME_FRAME_ACTIVE)
428 source = t->active_frame;
429 else
430 source = t->inactive_frame;
431
432 tile_source(cr, source,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600433 margin, margin,
434 width - margin * 2, height - margin * 2,
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400435 t->width, t->titlebar_height);
436
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600437 cairo_rectangle (cr, margin + t->width, margin,
438 width - (margin + t->width) * 2,
Martin Minarik5f3eddc2012-07-02 23:05:50 +0200439 t->titlebar_height - t->width);
440 cairo_clip(cr);
441
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400442 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
443 cairo_select_font_face(cr, "sans",
444 CAIRO_FONT_SLANT_NORMAL,
445 CAIRO_FONT_WEIGHT_BOLD);
446 cairo_set_font_size(cr, 14);
447 cairo_text_extents(cr, title, &extents);
448 cairo_font_extents (cr, &font_extents);
449 x = (width - extents.width) / 2;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600450 y = margin +
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400451 (t->titlebar_height -
452 font_extents.ascent - font_extents.descent) / 2 +
453 font_extents.ascent;
454
455 if (flags & THEME_FRAME_ACTIVE) {
456 cairo_move_to(cr, x + 1, y + 1);
457 cairo_set_source_rgb(cr, 1, 1, 1);
458 cairo_show_text(cr, title);
459 cairo_move_to(cr, x, y);
460 cairo_set_source_rgb(cr, 0, 0, 0);
461 cairo_show_text(cr, title);
462 } else {
463 cairo_move_to(cr, x, y);
464 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
465 cairo_show_text(cr, title);
466 }
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400467}
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400468
469enum theme_location
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600470theme_get_location(struct theme *t, int x, int y,
471 int width, int height, int flags)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400472{
473 int vlocation, hlocation, location;
474 const int grip_size = 8;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600475 int margin;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400476
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600477 margin = (flags & THEME_FRAME_MAXIMIZED) ? 0 : t->margin;
478
479 if (x < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400480 hlocation = THEME_LOCATION_EXTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600481 else if (margin <= x && x < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400482 hlocation = THEME_LOCATION_RESIZING_LEFT;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600483 else if (x < width - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400484 hlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600485 else if (x < width - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400486 hlocation = THEME_LOCATION_RESIZING_RIGHT;
487 else
488 hlocation = THEME_LOCATION_EXTERIOR;
489
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600490 if (y < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400491 vlocation = THEME_LOCATION_EXTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600492 else if (margin <= y && y < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400493 vlocation = THEME_LOCATION_RESIZING_TOP;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600494 else if (y < height - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400495 vlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600496 else if (y < height - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400497 vlocation = THEME_LOCATION_RESIZING_BOTTOM;
498 else
499 vlocation = THEME_LOCATION_EXTERIOR;
500
501 location = vlocation | hlocation;
502 if (location & THEME_LOCATION_EXTERIOR)
503 location = THEME_LOCATION_EXTERIOR;
504 if (location == THEME_LOCATION_INTERIOR &&
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600505 y < margin + t->titlebar_height)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400506 location = THEME_LOCATION_TITLEBAR;
507 else if (location == THEME_LOCATION_INTERIOR)
508 location = THEME_LOCATION_CLIENT_AREA;
509
510 return location;
511}