2 * draw.c - draw functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <cairo-xlib.h>
29 extern AwesomeConf globalconf
;
31 /** Get a draw context
32 * \param phys_screen physical screen id
34 * \param height height
35 * \return draw context ref
38 draw_get_context(int phys_screen
, int width
, int height
)
40 DrawCtx
*d
= p_new(DrawCtx
, 1);
42 d
->phys_screen
= phys_screen
;
45 d
->depth
= DefaultDepth(globalconf
.display
, phys_screen
);
46 d
->visual
= DefaultVisual(globalconf
.display
, phys_screen
);
47 d
->drawable
= XCreatePixmap(globalconf
.display
,
48 RootWindow(globalconf
.display
, phys_screen
),
49 width
, height
, d
->depth
);
54 /** Free a draw context and its drawable
55 * \param ctx the draw context to free
58 draw_free_context(DrawCtx
*ctx
)
60 XFreePixmap(globalconf
.display
, ctx
->drawable
);
64 /** Draw text into a draw context
69 * \param align alignment
70 * \param padding padding to add before drawing the text
71 * \param font font to use
72 * \param text text to draw
73 * \param fg foreground color
74 * \param bg background color
77 draw_text(DrawCtx
*ctx
,
82 XftFont
*font
, const char *text
,
88 cairo_font_face_t
*font_face
;
89 cairo_surface_t
*surface
;
92 draw_rectangle(ctx
, x
, y
, w
, h
, True
, bg
);
94 olen
= len
= a_strlen(text
);
99 surface
= cairo_xlib_surface_create(globalconf
.display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
100 cr
= cairo_create(surface
);
101 font_face
= cairo_ft_font_face_create_for_pattern(font
->pattern
);
102 cairo_set_font_face(cr
, font_face
);
103 cairo_set_font_size(cr
, font
->height
);
104 cairo_set_source_rgb(cr
, fg
.red
/ 65535.0, fg
.green
/ 65535.0, fg
.blue
/ 65535.0);
106 if(len
>= sizeof(buf
))
107 len
= sizeof(buf
) - 1;
108 memcpy(buf
, text
, len
);
110 while(len
&& (nw
= (textwidth(font
, buf
)) + padding
* 2) > w
)
113 return; /* too long */
127 cairo_move_to(cr
, x
+ padding
, y
+ font
->ascent
+ (ctx
->height
- font
->height
) / 2);
130 cairo_move_to(cr
, x
+ (w
- nw
) + padding
, y
+ font
->ascent
+ (ctx
->height
- font
->height
) / 2);
133 cairo_move_to(cr
, x
+ ((w
- nw
) / 2) + padding
, y
+ font
->ascent
+ (ctx
->height
- font
->height
) / 2);
136 cairo_show_text(cr
, buf
);
138 cairo_font_face_destroy(font_face
);
140 cairo_surface_destroy(surface
);
144 draw_rectangle(DrawCtx
*ctx
, int x
, int y
, int w
, int h
, Bool filled
, XColor color
)
146 cairo_surface_t
*surface
;
149 surface
= cairo_xlib_surface_create(globalconf
.display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
150 cr
= cairo_create (surface
);
152 cairo_set_antialias(cr
, CAIRO_ANTIALIAS_NONE
);
153 cairo_set_line_width(cr
, 1.0);
154 cairo_set_source_rgb(cr
, color
.red
/ 65535.0, color
.green
/ 65535.0, color
.blue
/ 65535.0);
157 cairo_rectangle(cr
, x
, y
, w
, h
);
161 cairo_rectangle(cr
, x
+ 1, y
, w
- 1, h
- 1);
166 cairo_surface_destroy(surface
);
170 draw_circle(DrawCtx
*ctx
, int x
, int y
, int r
, Bool filled
, XColor color
)
172 cairo_surface_t
*surface
;
175 surface
= cairo_xlib_surface_create(globalconf
.display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
176 cr
= cairo_create (surface
);
177 cairo_set_line_width(cr
, 1.0);
178 cairo_set_source_rgb(cr
, color
.red
/ 65535.0, color
.green
/ 65535.0, color
.blue
/ 65535.0);
181 cairo_arc (cr
, x
+ r
, y
+ r
, r
, 0, 2 * M_PI
);
185 cairo_arc (cr
, x
+ r
, y
+ r
, r
- 1, 0, 2 * M_PI
);
190 cairo_surface_destroy(surface
);
193 void draw_image_from_argb_data(DrawCtx
*ctx
, int x
, int y
, int w
, int h
,
194 int wanted_h
, unsigned char *data
)
197 cairo_surface_t
*surface
, *source
;
200 surface
= cairo_xlib_surface_create(globalconf
.display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
201 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
, 0);
202 cr
= cairo_create (surface
);
203 if(wanted_h
> 0 && h
> 0)
205 ratio
= (double) wanted_h
/ (double) h
;
206 cairo_scale(cr
, ratio
, ratio
);
207 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
210 cairo_set_source_surface(cr
, source
, x
, y
);
214 cairo_surface_destroy(source
);
215 cairo_surface_destroy(surface
);
219 draw_image(DrawCtx
*ctx
, int x
, int y
, int wanted_h
, const char *filename
)
223 cairo_surface_t
*surface
, *source
;
226 source
= cairo_xlib_surface_create(globalconf
.display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
227 surface
= cairo_image_surface_create_from_png(filename
);
228 cr
= cairo_create (source
);
229 if(wanted_h
> 0 && (h
= cairo_image_surface_get_height(surface
)) > 0)
231 ratio
= (double) wanted_h
/ (double) h
;
232 cairo_scale(cr
, ratio
, ratio
);
233 cairo_set_source_surface(cr
, surface
, x
/ ratio
, y
/ ratio
);
236 cairo_set_source_surface(cr
, surface
, x
, y
);
240 cairo_surface_destroy(source
);
241 cairo_surface_destroy(surface
);
247 draw_get_image_size(const char *filename
)
250 cairo_surface_t
*surface
;
252 surface
= cairo_image_surface_create_from_png(filename
);
253 cairo_image_surface_get_width(surface
);
256 size
.width
= cairo_image_surface_get_width(surface
);
257 size
.height
= cairo_image_surface_get_height(surface
);
258 cairo_surface_destroy(surface
);
264 draw_rotate(DrawCtx
*ctx
, int screen
, double angle
, int tx
, int ty
)
266 cairo_surface_t
*surface
, *source
;
268 Drawable newdrawable
;
270 newdrawable
= XCreatePixmap(globalconf
.display
,
271 RootWindow(globalconf
.display
, screen
),
272 ctx
->height
, ctx
->width
,
274 surface
= cairo_xlib_surface_create(globalconf
.display
, newdrawable
, ctx
->visual
, ctx
->height
, ctx
->width
);
275 source
= cairo_xlib_surface_create(globalconf
.display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
276 cr
= cairo_create (surface
);
278 cairo_translate(cr
, tx
, ty
);
279 cairo_rotate(cr
, angle
);
281 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
285 cairo_surface_destroy(source
);
286 cairo_surface_destroy(surface
);
292 textwidth(XftFont
*font
, char *text
)
294 cairo_surface_t
*surface
;
296 cairo_font_face_t
*font_face
;
297 cairo_text_extents_t te
;
302 surface
= cairo_xlib_surface_create(globalconf
.display
, DefaultScreen(globalconf
.display
),
303 DefaultVisual(globalconf
.display
, DefaultScreen(globalconf
.display
)),
304 DisplayWidth(globalconf
.display
, DefaultScreen(globalconf
.display
)),
305 DisplayHeight(globalconf
.display
, DefaultScreen(globalconf
.display
)));
306 cr
= cairo_create(surface
);
307 font_face
= cairo_ft_font_face_create_for_pattern(font
->pattern
);
308 cairo_set_font_face(cr
, font_face
);
309 cairo_set_font_size(cr
, font
->height
);
310 cairo_text_extents(cr
, text
, &te
);
312 cairo_surface_destroy(surface
);
313 cairo_font_face_destroy(font_face
);
315 return MAX(te
.x_advance
, te
.width
);
319 draw_get_align(const char *align
)
321 if(!a_strncmp(align
, "center", 6))
323 else if(!a_strncmp(align
, "right", 5))
329 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80