various manpage update: tasklist, align, …
[awesome.git] / draw.c
blob9368125c6c81c1f2e0d12af16f797e08f30c9bc6
1 /*
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.
22 #include <cairo.h>
23 #include <cairo-ft.h>
24 #include <cairo-xlib.h>
25 #include <math.h>
26 #include "util.h"
27 #include "config.h"
29 extern AwesomeConf globalconf;
31 /** Get a draw context
32 * \param phys_screen physical screen id
33 * \param width width
34 * \param height height
35 * \return draw context ref
37 DrawCtx *
38 draw_get_context(int phys_screen, int width, int height)
40 DrawCtx *d = p_new(DrawCtx, 1);
42 d->phys_screen = phys_screen;
43 d->width = width;
44 d->height = height;
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);
51 return d;
54 /** Free a draw context and its drawable
55 * \param ctx the draw context to free
57 void
58 draw_free_context(DrawCtx *ctx)
60 XFreePixmap(globalconf.display, ctx->drawable);
61 p_delete(&ctx);
64 /** Draw text into a draw context
65 * \param x x coord
66 * \param y y coord
67 * \param w width
68 * \param h height
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
76 void
77 draw_text(DrawCtx *ctx,
78 int x, int y,
79 int w, int h,
80 int align,
81 int padding,
82 XftFont *font, const char *text,
83 XColor fg, XColor bg)
85 int nw = 0;
86 static char buf[256];
87 size_t len, olen;
88 cairo_font_face_t *font_face;
89 cairo_surface_t *surface;
90 cairo_t *cr;
92 draw_rectangle(ctx, x, y, w, h, True, bg);
94 olen = len = a_strlen(text);
96 if(!len)
97 return;
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);
109 buf[len] = 0;
110 while(len && (nw = (textwidth(font, buf)) + padding * 2) > w)
111 buf[--len] = 0;
112 if(nw > w)
113 return; /* too long */
114 if(len < olen)
116 if(len > 1)
117 buf[len - 1] = '.';
118 if(len > 2)
119 buf[len - 2] = '.';
120 if(len > 3)
121 buf[len - 3] = '.';
124 if(align == AlignLeft)
125 cairo_move_to(cr, x + padding, y + font->ascent + (ctx->height - font->height) / 2);
126 else if(align == AlignRight)
127 cairo_move_to(cr, x + (w - nw) + padding, y + font->ascent + (ctx->height - font->height) / 2);
128 else
129 cairo_move_to(cr, x + ((w - nw) / 2) + padding, y + font->ascent + (ctx->height - font->height) / 2);
130 cairo_show_text(cr, buf);
132 cairo_font_face_destroy(font_face);
133 cairo_destroy(cr);
134 cairo_surface_destroy(surface);
137 void
138 draw_rectangle(DrawCtx *ctx, int x, int y, int w, int h, Bool filled, XColor color)
140 cairo_surface_t *surface;
141 cairo_t *cr;
143 surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
144 cr = cairo_create (surface);
146 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
147 cairo_set_line_width(cr, 1.0);
148 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
149 if(filled)
151 cairo_rectangle(cr, x, y, w, h);
152 cairo_fill(cr);
154 else
155 cairo_rectangle(cr, x + 1, y, w - 1, h - 1);
157 cairo_stroke(cr);
159 cairo_destroy(cr);
160 cairo_surface_destroy(surface);
163 void
164 draw_circle(DrawCtx *ctx, int x, int y, int r, Bool filled, XColor color)
166 cairo_surface_t *surface;
167 cairo_t *cr;
169 surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
170 cr = cairo_create (surface);
171 cairo_set_line_width(cr, 1.0);
172 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
173 if(filled)
175 cairo_arc (cr, x + r, y + r, r, 0, 2 * M_PI);
176 cairo_fill(cr);
178 else
179 cairo_arc (cr, x + r, y + r, r - 1, 0, 2 * M_PI);
181 cairo_stroke(cr);
183 cairo_destroy(cr);
184 cairo_surface_destroy(surface);
187 void draw_image_from_argb_data(DrawCtx *ctx, int x, int y, int w, int h,
188 int wanted_h, unsigned char *data)
190 double ratio;
191 cairo_surface_t *surface, *source;
192 cairo_t *cr;
194 surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
195 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h, 0);
196 cr = cairo_create (surface);
197 if(wanted_h > 0 && h > 0)
199 ratio = (double) wanted_h / (double) h;
200 cairo_scale(cr, ratio, ratio);
201 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
203 else
204 cairo_set_source_surface(cr, source, x, y);
205 cairo_paint(cr);
207 cairo_destroy(cr);
208 cairo_surface_destroy(source);
209 cairo_surface_destroy(surface);
212 void
213 draw_image(DrawCtx *ctx, int x, int y, int wanted_h, const char *filename)
215 double ratio;
216 int h;
217 cairo_surface_t *surface, *source;
218 cairo_t *cr;
220 source = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
221 surface = cairo_image_surface_create_from_png(filename);
222 cr = cairo_create (source);
223 if(wanted_h > 0 && (h = cairo_image_surface_get_height(surface)) > 0)
225 ratio = (double) wanted_h / (double) h;
226 cairo_scale(cr, ratio, ratio);
227 cairo_set_source_surface(cr, surface, x / ratio, y / ratio);
229 else
230 cairo_set_source_surface(cr, surface, x, y);
231 cairo_paint(cr);
233 cairo_destroy(cr);
234 cairo_surface_destroy(source);
235 cairo_surface_destroy(surface);
240 Area
241 draw_get_image_size(const char *filename)
243 Area size;
244 cairo_surface_t *surface;
246 surface = cairo_image_surface_create_from_png(filename);
247 cairo_image_surface_get_width(surface);
248 size.width = cairo_image_surface_get_width(surface);
249 size.height = cairo_image_surface_get_height(surface);
250 cairo_surface_destroy(surface);
252 return size;
255 Drawable
256 draw_rotate(DrawCtx *ctx, int screen, double angle, int tx, int ty)
258 cairo_surface_t *surface, *source;
259 cairo_t *cr;
260 Drawable newdrawable;
262 newdrawable = XCreatePixmap(globalconf.display,
263 RootWindow(globalconf.display, screen),
264 ctx->height, ctx->width,
265 ctx->depth);
266 surface = cairo_xlib_surface_create(globalconf.display, newdrawable, ctx->visual, ctx->height, ctx->width);
267 source = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
268 cr = cairo_create (surface);
270 cairo_translate(cr, tx, ty);
271 cairo_rotate(cr, angle);
273 cairo_set_source_surface(cr, source, 0.0, 0.0);
274 cairo_paint(cr);
276 cairo_destroy(cr);
277 cairo_surface_destroy(source);
278 cairo_surface_destroy(surface);
280 return newdrawable;
283 unsigned short
284 textwidth(XftFont *font, char *text)
286 cairo_surface_t *surface;
287 cairo_t *cr;
288 cairo_font_face_t *font_face;
289 cairo_text_extents_t te;
291 if (!a_strlen(text))
292 return 0;
294 surface = cairo_xlib_surface_create(globalconf.display, DefaultScreen(globalconf.display),
295 DefaultVisual(globalconf.display, DefaultScreen(globalconf.display)),
296 DisplayWidth(globalconf.display, DefaultScreen(globalconf.display)),
297 DisplayHeight(globalconf.display, DefaultScreen(globalconf.display)));
298 cr = cairo_create(surface);
299 font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
300 cairo_set_font_face(cr, font_face);
301 cairo_set_font_size(cr, font->height);
302 cairo_text_extents(cr, text, &te);
303 cairo_destroy(cr);
304 cairo_surface_destroy(surface);
305 cairo_font_face_destroy(font_face);
307 return MAX(te.x_advance, te.width);
311 draw_get_align(const char *align)
313 if(!a_strncmp(align, "center", 6))
314 return AlignCenter;
315 else if(!a_strncmp(align, "right", 5))
316 return AlignRight;
318 return AlignLeft;
321 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80