simplify unmanage code
[awesome.git] / draw.c
blob20d095b2518bcf8903fc73d802ca44ffe97f9efe
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 switch(align)
126 case AlignLeft:
127 cairo_move_to(cr, x + padding, y + font->ascent + (ctx->height - font->height) / 2);
128 break;
129 case AlignRight:
130 cairo_move_to(cr, x + (w - nw) + padding, y + font->ascent + (ctx->height - font->height) / 2);
131 break;
132 default:
133 cairo_move_to(cr, x + ((w - nw) / 2) + padding, y + font->ascent + (ctx->height - font->height) / 2);
134 break;
136 cairo_show_text(cr, buf);
138 cairo_font_face_destroy(font_face);
139 cairo_destroy(cr);
140 cairo_surface_destroy(surface);
143 void
144 draw_rectangle(DrawCtx *ctx, int x, int y, int w, int h, Bool filled, XColor color)
146 cairo_surface_t *surface;
147 cairo_t *cr;
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);
155 if(filled)
157 cairo_rectangle(cr, x, y, w, h);
158 cairo_fill(cr);
160 else
161 cairo_rectangle(cr, x + 1, y, w - 1, h - 1);
163 cairo_stroke(cr);
165 cairo_destroy(cr);
166 cairo_surface_destroy(surface);
169 /* draws a graph; it takes the line-lengths from 'h' (w = size of h)
170 * It cycles backwards through it, beginning at position h_index, until
171 * h_index is reached again (wrapped around). */
173 void
174 draw_graph(DrawCtx *ctx, int x, int y, int w, int *h, int h_index, XColor color)
176 cairo_surface_t *surface;
177 cairo_t *cr;
178 int i, i_tmp;
180 surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
181 cr = cairo_create (surface);
183 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
184 cairo_set_line_width(cr, 1.0);
185 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
187 i_tmp = h_index;
188 i = -1;
189 while(++i < w)
191 cairo_move_to(cr, x, y);
192 cairo_line_to(cr, x, y - h[i_tmp]);
193 x++;
195 if (--i_tmp < 0)
196 i_tmp = w - 1;
199 cairo_stroke(cr);
201 cairo_destroy(cr);
202 cairo_surface_destroy(surface);
205 void
206 draw_circle(DrawCtx *ctx, int x, int y, int r, Bool filled, XColor color)
208 cairo_surface_t *surface;
209 cairo_t *cr;
211 surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
212 cr = cairo_create (surface);
213 cairo_set_line_width(cr, 1.0);
214 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
215 if(filled)
217 cairo_arc (cr, x + r, y + r, r, 0, 2 * M_PI);
218 cairo_fill(cr);
220 else
221 cairo_arc (cr, x + r, y + r, r - 1, 0, 2 * M_PI);
223 cairo_stroke(cr);
225 cairo_destroy(cr);
226 cairo_surface_destroy(surface);
229 void draw_image_from_argb_data(DrawCtx *ctx, int x, int y, int w, int h,
230 int wanted_h, unsigned char *data)
232 double ratio;
233 cairo_surface_t *surface, *source;
234 cairo_t *cr;
236 surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
237 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h, 0);
238 cr = cairo_create (surface);
239 if(wanted_h > 0 && h > 0)
241 ratio = (double) wanted_h / (double) h;
242 cairo_scale(cr, ratio, ratio);
243 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
245 else
246 cairo_set_source_surface(cr, source, x, y);
247 cairo_paint(cr);
249 cairo_destroy(cr);
250 cairo_surface_destroy(source);
251 cairo_surface_destroy(surface);
254 void
255 draw_image(DrawCtx *ctx, int x, int y, int wanted_h, const char *filename)
257 double ratio;
258 int h;
259 cairo_surface_t *surface, *source;
260 cairo_t *cr;
262 source = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
263 surface = cairo_image_surface_create_from_png(filename);
264 cr = cairo_create (source);
265 if(wanted_h > 0 && (h = cairo_image_surface_get_height(surface)) > 0)
267 ratio = (double) wanted_h / (double) h;
268 cairo_scale(cr, ratio, ratio);
269 cairo_set_source_surface(cr, surface, x / ratio, y / ratio);
271 else
272 cairo_set_source_surface(cr, surface, x, y);
273 cairo_paint(cr);
275 cairo_destroy(cr);
276 cairo_surface_destroy(source);
277 cairo_surface_destroy(surface);
282 Area
283 draw_get_image_size(const char *filename)
285 Area size;
286 cairo_surface_t *surface;
288 surface = cairo_image_surface_create_from_png(filename);
289 cairo_image_surface_get_width(surface);
290 size.x = 0;
291 size.y = 0;
292 size.width = cairo_image_surface_get_width(surface);
293 size.height = cairo_image_surface_get_height(surface);
294 cairo_surface_destroy(surface);
296 return size;
299 Drawable
300 draw_rotate(DrawCtx *ctx, int screen, double angle, int tx, int ty)
302 cairo_surface_t *surface, *source;
303 cairo_t *cr;
304 Drawable newdrawable;
306 newdrawable = XCreatePixmap(globalconf.display,
307 RootWindow(globalconf.display, screen),
308 ctx->height, ctx->width,
309 ctx->depth);
310 surface = cairo_xlib_surface_create(globalconf.display, newdrawable, ctx->visual, ctx->height, ctx->width);
311 source = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
312 cr = cairo_create (surface);
314 cairo_translate(cr, tx, ty);
315 cairo_rotate(cr, angle);
317 cairo_set_source_surface(cr, source, 0.0, 0.0);
318 cairo_paint(cr);
320 cairo_destroy(cr);
321 cairo_surface_destroy(source);
322 cairo_surface_destroy(surface);
324 return newdrawable;
327 unsigned short
328 textwidth(XftFont *font, char *text)
330 cairo_surface_t *surface;
331 cairo_t *cr;
332 cairo_font_face_t *font_face;
333 cairo_text_extents_t te;
335 if (!a_strlen(text))
336 return 0;
338 surface = cairo_xlib_surface_create(globalconf.display, DefaultScreen(globalconf.display),
339 DefaultVisual(globalconf.display, DefaultScreen(globalconf.display)),
340 DisplayWidth(globalconf.display, DefaultScreen(globalconf.display)),
341 DisplayHeight(globalconf.display, DefaultScreen(globalconf.display)));
342 cr = cairo_create(surface);
343 font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
344 cairo_set_font_face(cr, font_face);
345 cairo_set_font_size(cr, font->height);
346 cairo_text_extents(cr, text, &te);
347 cairo_destroy(cr);
348 cairo_surface_destroy(surface);
349 cairo_font_face_destroy(font_face);
351 return MAX(te.x_advance, te.width);
354 Alignment
355 draw_get_align(const char *align)
357 if(!a_strncmp(align, "center", 6))
358 return AlignCenter;
359 else if(!a_strncmp(align, "right", 5))
360 return AlignRight;
362 return AlignLeft;
365 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80