titlebar: fix modkey for buttons
[awesome.git] / draw.c
blobf79e9937b3412e0588753aca4c446f64ec963c86
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007-2009 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-xcb.h>
24 #include "config.h"
26 #include <langinfo.h>
27 #include <iconv.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <math.h>
32 #include "structs.h"
33 #include "screen.h"
35 #include "common/tokenize.h"
36 #include "common/xutil.h"
38 /** Convert text from any charset to UTF-8 using iconv.
39 * \param iso The ISO string to convert.
40 * \param len The string size.
41 * \param dest The destination pointer. Memory will be allocated, up to you to
42 * free, like any char *.
43 * \param dlen The destination length, can be NULL.
44 * \return True if conversion was done.
46 bool
47 draw_iso2utf8(const char *iso, size_t len, char **dest, ssize_t *dlen)
49 static iconv_t iso2utf8 = (iconv_t) -1;
50 static int8_t dont_need_convert = -1;
52 if(dont_need_convert == -1)
53 dont_need_convert = !a_strcmp(nl_langinfo(CODESET), "UTF-8");
55 if(!len || dont_need_convert)
56 return false;
58 if(iso2utf8 == (iconv_t) -1)
60 iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
61 if(iso2utf8 == (iconv_t) -1)
63 if(errno == EINVAL)
64 warn("unable to convert text from %s to UTF-8, not available",
65 nl_langinfo(CODESET));
66 else
67 warn("unable to convert text: %s", strerror(errno));
69 return false;
73 size_t orig_utf8len, utf8len;
74 char *utf8;
76 orig_utf8len = utf8len = 2 * len + 1;
77 utf8 = *dest = p_new(char, utf8len);
79 if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
81 warn("text conversion failed: %s", strerror(errno));
82 p_delete(dest);
83 return false;
86 if(dlen)
87 *dlen = orig_utf8len - utf8len;
89 return true;
92 /** Create a new Pango font.
93 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
94 * \return A new font.
96 font_t *
97 draw_font_new(const char *fontname)
99 cairo_surface_t *surface;
100 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
101 cairo_t *cr;
102 PangoLayout *layout;
103 font_t *font = p_new(font_t, 1);
105 /* Create a dummy cairo surface, cairo context and pango layout in
106 * order to get font informations */
107 surface = cairo_xcb_surface_create(globalconf.connection,
108 globalconf.default_screen,
109 globalconf.screens.tab[0].visual,
110 s->width_in_pixels,
111 s->height_in_pixels);
113 cr = cairo_create(surface);
114 layout = pango_cairo_create_layout(cr);
116 /* Get the font description used to set text on a PangoLayout */
117 font->desc = pango_font_description_from_string(fontname);
118 pango_layout_set_font_description(layout, font->desc);
120 /* Get height */
121 pango_layout_get_pixel_size(layout, NULL, &font->height);
123 g_object_unref(layout);
124 cairo_destroy(cr);
125 cairo_surface_destroy(surface);
127 return font;
130 /** Delete a font.
131 * \param font Font to delete.
133 void
134 draw_font_delete(font_t **font)
136 if(*font)
138 pango_font_description_free((*font)->desc);
139 p_delete(font);
143 /** Initialize a draw_text_context_t with text data.
144 * \param data The draw text context to init.
145 * \param str The text string to render.
146 * \param slen The text string length.
147 * \return True if everything is ok, false otherwise.
149 bool
150 draw_text_context_init(draw_text_context_t *data, const char *str, ssize_t slen)
152 GError *error = NULL;
154 if(!str)
155 return false;
157 if(!pango_parse_markup(str, slen, 0, &data->attr_list, &data->text, NULL, &error))
159 warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
160 if(error)
161 g_error_free(error);
162 return false;
165 data->len = a_strlen(data->text);
167 return true;
170 /** Initialize a new draw context.
171 * \param d The draw context to initialize.
172 * \param phys_screen Physical screen id.
173 * \param width Width.
174 * \param height Height.
175 * \param px Pixmap object to store.
176 * \param fg Foreground color.
177 * \param bg Background color.
179 void
180 draw_context_init(draw_context_t *d, int phys_screen,
181 int width, int height, xcb_pixmap_t px,
182 const xcolor_t *fg, const xcolor_t *bg)
184 d->phys_screen = phys_screen;
185 d->width = width;
186 d->height = height;
187 d->pixmap = px;
188 d->surface = cairo_xcb_surface_create(globalconf.connection,
189 px, globalconf.screens.tab[phys_screen].visual,
190 width, height);
191 d->cr = cairo_create(d->surface);
192 d->layout = pango_cairo_create_layout(d->cr);
193 d->fg = *fg;
194 d->bg = *bg;
197 /** Draw text into a draw context.
198 * \param ctx Draw context to draw to.
199 * \param data Draw text context data.
200 * \param ellip Ellipsize mode.
201 * \param wrap Wrap mode.
202 * \param align Text alignment.
203 * \param margin Margin to respect when drawing text.
204 * \param area Area to draw to.
205 * \param ext Text extents.
207 void
208 draw_text(draw_context_t *ctx, draw_text_context_t *data,
209 PangoEllipsizeMode ellip, PangoWrapMode wrap,
210 alignment_t align, padding_t *margin, area_t area, area_t *ext)
212 int x, y;
214 pango_layout_set_text(ctx->layout, data->text, data->len);
215 pango_layout_set_width(ctx->layout,
216 pango_units_from_double(area.width
217 - (margin->left
218 + margin->right)));
219 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height)
220 - (margin->top + margin->bottom));
221 pango_layout_set_ellipsize(ctx->layout, ellip);
222 pango_layout_set_wrap(ctx->layout, wrap);
223 pango_layout_set_attributes(ctx->layout, data->attr_list);
224 pango_layout_set_font_description(ctx->layout, globalconf.font->desc);
226 x = area.x + margin->left;
227 y = area.y + margin->top;
229 /* only honors alignment if enough space */
230 if(ext->width < area.width)
231 switch(align)
233 case AlignCenter:
234 x += (area.width - ext->width) / 2;
235 break;
236 case AlignRight:
237 x += area.width - ext->width;
238 break;
239 default:
240 break;
243 cairo_move_to(ctx->cr, x, y);
245 cairo_set_source_rgba(ctx->cr,
246 ctx->fg.red / 65535.0,
247 ctx->fg.green / 65535.0,
248 ctx->fg.blue / 65535.0,
249 ctx->fg.alpha / 65535.0);
250 pango_cairo_update_layout(ctx->cr, ctx->layout);
251 pango_cairo_show_layout(ctx->cr, ctx->layout);
254 /** Setup color-source for cairo (gradient or mono).
255 * \param ctx Draw context.
256 * \param gradient_vector x, y to x + x_offset, y + y_offset.
257 * \param pcolor Color to use at start of gradient_vector.
258 * \param pcolor_center Color at center of gradient_vector.
259 * \param pcolor_end Color at end of gradient_vector.
260 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
262 static cairo_pattern_t *
263 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
264 const color_t *pcolor, const color_t *pcolor_center,
265 const color_t *pcolor_end)
267 cairo_pattern_t *pat = NULL;
268 bool has_center = pcolor_center->initialized;
269 bool has_end = pcolor_end->initialized;
271 /* no need for a real pattern: */
272 if(!has_end && !has_center)
273 cairo_set_source_rgba(ctx->cr,
274 pcolor->red / 255.0,
275 pcolor->green / 255.0,
276 pcolor->blue / 255.0,
277 pcolor->alpha / 255.0);
278 else
280 pat = cairo_pattern_create_linear(gradient_vector.x,
281 gradient_vector.y,
282 gradient_vector.x + gradient_vector.x_offset,
283 gradient_vector.y + gradient_vector.y_offset);
285 /* pcolor is always set (so far in awesome) */
286 cairo_pattern_add_color_stop_rgba(pat, 0.0,
287 pcolor->red / 255.0,
288 pcolor->green / 255.0,
289 pcolor->blue / 255.0,
290 pcolor->alpha / 255.0);
292 if(has_center)
293 cairo_pattern_add_color_stop_rgba(pat, 0.5,
294 pcolor_center->red / 255.0,
295 pcolor_center->green / 255.0,
296 pcolor_center->blue / 255.0,
297 pcolor_center->alpha / 255.0);
299 if(has_end)
300 cairo_pattern_add_color_stop_rgba(pat, 1.0,
301 pcolor_end->red / 255.0,
302 pcolor_end->green / 255.0,
303 pcolor_end->blue / 255.0,
304 pcolor_end->alpha / 255.0);
305 else
306 cairo_pattern_add_color_stop_rgba(pat, 1.0,
307 pcolor->red / 255.0,
308 pcolor->green / 255.0,
309 pcolor->blue / 255.0,
310 pcolor->alpha / 255.0);
311 cairo_set_source(ctx->cr, pat);
313 return pat;
316 /** Draw rectangle inside the coordinates
317 * \param ctx Draw context
318 * \param geometry geometry
319 * \param line_width line width
320 * \param filled fill rectangle?
321 * \param color color to use
323 void
324 draw_rectangle(draw_context_t *ctx, area_t geometry,
325 float line_width, bool filled, const color_t *color)
327 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
328 cairo_set_line_width(ctx->cr, line_width);
329 cairo_set_miter_limit(ctx->cr, 10.0);
330 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
331 cairo_set_source_rgba(ctx->cr,
332 color->red / 255.0,
333 color->green / 255.0,
334 color->blue / 255.0,
335 color->alpha / 255.0);
336 if(filled)
338 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
339 geometry.width, geometry.height);
340 cairo_fill(ctx->cr);
342 else
344 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
345 geometry.width - line_width, geometry.height - line_width);
346 cairo_stroke(ctx->cr);
350 /** Draw rectangle with gradient colors
351 * \param ctx Draw context.
352 * \param geometry Geometry.
353 * \param line_width Line width.
354 * \param filled Filled rectangle?
355 * \param gradient_vector Color-gradient course.
356 * \param pcolor Color at start of gradient_vector.
357 * \param pcolor_center Color in the center.
358 * \param pcolor_end Color at end of gradient_vector.
360 void
361 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
362 vector_t gradient_vector, const color_t *pcolor,
363 const color_t *pcolor_center, const color_t *pcolor_end)
365 cairo_pattern_t *pat;
367 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
368 cairo_set_line_width(ctx->cr, line_width);
369 cairo_set_miter_limit(ctx->cr, 10.0);
370 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
372 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
374 if(filled)
376 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
377 cairo_fill(ctx->cr);
379 else
381 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
382 cairo_stroke(ctx->cr);
385 if(pat)
386 cairo_pattern_destroy(pat);
389 /** Setup some cairo-things for drawing a graph
390 * \param ctx Draw context
392 void
393 draw_graph_setup(draw_context_t *ctx)
395 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
396 cairo_set_line_width(ctx->cr, 1.0);
397 /* without it, it can draw over the path on sharp angles
398 * ...too long lines * (...graph_line) */
399 cairo_set_miter_limit(ctx->cr, 0.0);
400 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
403 /** Draw a graph.
404 * \param ctx Draw context.
405 * \param rect The area to draw into.
406 * \param from Array of starting-point offsets to draw a graph lines.
407 * \param to Array of end-point offsets to draw a graph lines.
408 * \param cur_index Current position in data-array (cycles around).
409 * \param grow Put new values to the left or to the right.
410 * \param gradient_vector Color-Gradient course.
411 * \param pcolor Color at start of gradient_vector.
412 * \param pcolor_center Color in the center.
413 * \param pcolor_end Color at end of gradient_vector.
415 void
416 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
417 position_t grow, vector_t gradient_vector, const color_t *pcolor,
418 const color_t *pcolor_center, const color_t *pcolor_end)
420 int i = -1;
421 float x = rect.x + 0.5; /* middle of a pixel */
422 cairo_pattern_t *pat;
424 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
425 pcolor, pcolor_center, pcolor_end);
427 if(grow == Right) /* draw from right to left */
429 x += rect.width - 1;
430 while(++i < rect.width)
432 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
433 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
434 x -= 1.0;
436 if(--cur_index < 0)
437 cur_index = rect.width - 1;
440 else /* draw from left to right */
441 while(++i < rect.width)
443 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
444 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
445 x += 1.0;
447 if(--cur_index < 0)
448 cur_index = rect.width - 1;
451 cairo_stroke(ctx->cr);
453 if(pat)
454 cairo_pattern_destroy(pat);
457 /** Draw a line into a graph-widget.
458 * \param ctx Draw context.
459 * \param rect The area to draw into.
460 * \param to array of offsets to draw the line through...
461 * \param cur_index current position in data-array (cycles around)
462 * \param grow put new values to the left or to the right
463 * \param gradient_vector Color-gradient course.
464 * \param pcolor Color at start of gradient_vector.
465 * \param pcolor_center Color in the center.
466 * \param pcolor_end Color at end of gradient_vector.
468 void
469 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
470 position_t grow, vector_t gradient_vector, const color_t *pcolor,
471 const color_t *pcolor_center, const color_t *pcolor_end)
473 int i, w;
474 float x, y;
475 cairo_pattern_t *pat;
477 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
478 * ... it won't fill some pixels! It also looks much nicer so.
479 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
480 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
481 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
482 /* a nicer, better visible line compared to 1.0 */
483 cairo_set_line_width(ctx->cr, 1.25);
485 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
487 /* path through the centers of pixels */
488 x = rect.x + 0.5;
489 y = rect.y + 0.5;
490 w = rect.width;
492 if(grow == Right)
494 /* go through the values from old to new. Begin with the oldest. */
495 if(++cur_index > w - 1)
496 cur_index = 0;
498 cairo_move_to(ctx->cr, x, y - to[cur_index]);
500 else
501 /* on the left border: fills a pixel also when there's only one value */
502 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
504 for(i = 0; i < w; i++)
506 cairo_line_to(ctx->cr, x, y - to[cur_index]);
507 x += 1.0;
509 /* cycles around the index */
510 if(grow == Right)
512 if(++cur_index > w - 1)
513 cur_index = 0;
515 else
517 if(--cur_index < 0)
518 cur_index = w - 1;
522 /* onto the right border: fills a pixel also when there's only one value */
523 if(grow == Right)
524 cairo_line_to(ctx->cr, x, y - to[(cur_index + (w - 1)) % w]);
526 cairo_stroke(ctx->cr);
528 if(pat)
529 cairo_pattern_destroy(pat);
530 /* reset line-width */
531 cairo_set_line_width(ctx->cr, 1.0);
534 /** Draw an image from ARGB data to a draw context.
535 * Data should be stored as an array of alpha, red, blue, green for each pixel
536 * and the array size should be w * h elements long.
537 * \param ctx Draw context to draw to.
538 * \param x X coordinate.
539 * \param y Y coordinate.
540 * \param w Width.
541 * \param h Height.
542 * \param ratio The ratio to apply to the image.
543 * \param data The image pixels array.
545 static void
546 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
547 double ratio, unsigned char *data)
549 cairo_t *cr;
550 cairo_surface_t *source;
552 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
553 #if CAIRO_VERSION_MAJOR < 1 || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR < 5) || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR == 5 && CAIRO_VERSION_MICRO < 8)
554 sizeof(unsigned char) * 4 * w);
555 #else
556 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
557 #endif
558 cr = cairo_create(ctx->surface);
559 cairo_scale(cr, ratio, ratio);
560 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
562 cairo_paint(cr);
564 cairo_destroy(cr);
565 cairo_surface_destroy(source);
568 /** Draw an image to a draw context.
569 * \param ctx Draw context to draw to.
570 * \param x X coordinate.
571 * \param y Y coordinate.
572 * \param ratio The ratio to apply to the image.
573 * \param image The image to draw.
575 void
576 draw_image(draw_context_t *ctx, int x, int y, double ratio, image_t *image)
578 draw_image_from_argb_data(ctx, x, y, image_getwidth(image), image_getheight(image), ratio, image_getdata(image));
581 /** Rotate a pixmap.
582 * \param ctx Draw context to draw with.
583 * \param src Drawable to draw from.
584 * \param dest Drawable to draw to.
585 * \param src_w Drawable width.
586 * \param src_h Drawable height.
587 * \param dest_w Drawable width.
588 * \param dest_h Drawable height.
589 * \param angle angle to rotate.
590 * \param tx Translate to this x coordinate.
591 * \param ty Translate to this y coordinate.
593 void
594 draw_rotate(draw_context_t *ctx,
595 xcb_pixmap_t src, xcb_pixmap_t dest,
596 int src_w, int src_h,
597 int dest_w, int dest_h,
598 double angle, int tx, int ty)
600 cairo_surface_t *surface, *source;
601 cairo_t *cr;
603 surface = cairo_xcb_surface_create(globalconf.connection, dest,
604 globalconf.screens.tab[ctx->phys_screen].visual,
605 dest_w, dest_h);
606 source = cairo_xcb_surface_create(globalconf.connection, src,
607 globalconf.screens.tab[ctx->phys_screen].visual,
608 src_w, src_h);
609 cr = cairo_create (surface);
611 cairo_translate(cr, tx, ty);
612 cairo_rotate(cr, angle);
614 cairo_set_source_surface(cr, source, 0.0, 0.0);
615 cairo_paint(cr);
617 cairo_destroy(cr);
618 cairo_surface_destroy(source);
619 cairo_surface_destroy(surface);
622 /** Return the width and height of a text in pixel.
623 * \param data The draw context text data.
624 * \return Text height and width.
626 area_t
627 draw_text_extents(draw_text_context_t *data)
629 cairo_surface_t *surface;
630 cairo_t *cr;
631 PangoLayout *layout;
632 PangoRectangle ext;
633 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
634 area_t geom = { 0, 0, 0, 0 };
636 if(data->len <= 0)
637 return geom;
639 surface = cairo_xcb_surface_create(globalconf.connection,
640 globalconf.default_screen,
641 globalconf.screens.tab[0].visual,
642 s->width_in_pixels,
643 s->height_in_pixels);
645 cr = cairo_create(surface);
646 layout = pango_cairo_create_layout(cr);
647 pango_layout_set_text(layout, data->text, data->len);
648 pango_layout_set_attributes(layout, data->attr_list);
649 pango_layout_set_font_description(layout, globalconf.font->desc);
650 pango_layout_get_pixel_extents(layout, NULL, &ext);
651 g_object_unref(layout);
652 cairo_destroy(cr);
653 cairo_surface_destroy(surface);
655 geom.width = ext.width;
656 geom.height = ext.height;
658 return geom;
661 /** Transform a string to a alignment_t type.
662 * Recognized string are flex, fixed, left, center, middle or right.
663 * \param align Atring with align text.
664 * \param len The string length.
665 * \return An alignment_t type.
667 alignment_t
668 draw_align_fromstr(const char *align, ssize_t len)
670 switch(a_tokenize(align, len))
672 case A_TK_CENTER: return AlignCenter;
673 case A_TK_RIGHT: return AlignRight;
674 case A_TK_TOP: return AlignTop;
675 case A_TK_BOTTOM: return AlignBottom;
676 case A_TK_MIDDLE: return AlignMiddle;
677 default: return AlignLeft;
681 /** Transform an alignment to a string.
682 * \param a The alignment.
683 * \return A string which must not be freed.
685 const char *
686 draw_align_tostr(alignment_t a)
688 switch(a)
690 case AlignLeft: return "left";
691 case AlignCenter: return "center";
692 case AlignRight: return "right";
693 case AlignBottom: return "bottom";
694 case AlignTop: return "top";
695 case AlignMiddle: return "middle";
696 default: return NULL;
700 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80