titlebar: emit signal when changing titlebar property
[awesome.git] / draw.c
blobe94215b2289e5940036b9fe189d2733d060d3192
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 valign Vertical text alignment.
204 * \param area Area to draw to.
206 void
207 draw_text(draw_context_t *ctx, draw_text_context_t *data,
208 PangoEllipsizeMode ellip, PangoWrapMode wrap,
209 alignment_t align, alignment_t valign, area_t area)
211 pango_layout_set_text(ctx->layout, data->text, data->len);
212 pango_layout_set_width(ctx->layout,
213 pango_units_from_double(area.width));
214 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
215 pango_layout_set_ellipsize(ctx->layout, ellip);
216 pango_layout_set_wrap(ctx->layout, wrap);
217 pango_layout_set_attributes(ctx->layout, data->attr_list);
218 pango_layout_set_font_description(ctx->layout, globalconf.font->desc);
220 PangoRectangle ext;
221 pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
223 /* Not enough space, draw nothing */
224 if(ext.width > area.width || ext.height > area.height)
225 return;
227 switch(align)
229 case AlignCenter:
230 area.x += (area.width - ext.width) / 2;
231 break;
232 case AlignRight:
233 area.x += area.width - ext.width;
234 break;
235 default:
236 break;
239 switch(valign)
241 case AlignCenter:
242 area.y += (area.height - ext.height) / 2;
243 break;
244 case AlignBottom:
245 area.y += area.height - ext.height;
246 break;
247 default:
248 break;
251 cairo_move_to(ctx->cr, area.x, area.y);
253 cairo_set_source_rgba(ctx->cr,
254 ctx->fg.red / 65535.0,
255 ctx->fg.green / 65535.0,
256 ctx->fg.blue / 65535.0,
257 ctx->fg.alpha / 65535.0);
258 pango_cairo_update_layout(ctx->cr, ctx->layout);
259 pango_cairo_show_layout(ctx->cr, ctx->layout);
262 /** Setup color-source for cairo (gradient or mono).
263 * \param ctx Draw context.
264 * \param gradient_vector x, y to x + x_offset, y + y_offset.
265 * \param pcolor Color to use at start of gradient_vector.
266 * \param pcolor_center Color at center of gradient_vector.
267 * \param pcolor_end Color at end of gradient_vector.
268 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
270 static cairo_pattern_t *
271 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
272 const color_t *pcolor, const color_t *pcolor_center,
273 const color_t *pcolor_end)
275 cairo_pattern_t *pat = NULL;
276 bool has_center = pcolor_center->initialized;
277 bool has_end = pcolor_end->initialized;
279 /* no need for a real pattern: */
280 if(!has_end && !has_center)
281 cairo_set_source_rgba(ctx->cr,
282 pcolor->red / 255.0,
283 pcolor->green / 255.0,
284 pcolor->blue / 255.0,
285 pcolor->alpha / 255.0);
286 else
288 pat = cairo_pattern_create_linear(gradient_vector.x,
289 gradient_vector.y,
290 gradient_vector.x + gradient_vector.x_offset,
291 gradient_vector.y + gradient_vector.y_offset);
293 /* pcolor is always set (so far in awesome) */
294 cairo_pattern_add_color_stop_rgba(pat, 0.0,
295 pcolor->red / 255.0,
296 pcolor->green / 255.0,
297 pcolor->blue / 255.0,
298 pcolor->alpha / 255.0);
300 if(has_center)
301 cairo_pattern_add_color_stop_rgba(pat, 0.5,
302 pcolor_center->red / 255.0,
303 pcolor_center->green / 255.0,
304 pcolor_center->blue / 255.0,
305 pcolor_center->alpha / 255.0);
307 if(has_end)
308 cairo_pattern_add_color_stop_rgba(pat, 1.0,
309 pcolor_end->red / 255.0,
310 pcolor_end->green / 255.0,
311 pcolor_end->blue / 255.0,
312 pcolor_end->alpha / 255.0);
313 else
314 cairo_pattern_add_color_stop_rgba(pat, 1.0,
315 pcolor->red / 255.0,
316 pcolor->green / 255.0,
317 pcolor->blue / 255.0,
318 pcolor->alpha / 255.0);
319 cairo_set_source(ctx->cr, pat);
321 return pat;
324 /** Draw rectangle inside the coordinates
325 * \param ctx Draw context
326 * \param geometry geometry
327 * \param line_width line width
328 * \param filled fill rectangle?
329 * \param color color to use
331 void
332 draw_rectangle(draw_context_t *ctx, area_t geometry,
333 float line_width, bool filled, const color_t *color)
335 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
336 cairo_set_line_width(ctx->cr, line_width);
337 cairo_set_miter_limit(ctx->cr, 10.0);
338 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
339 cairo_set_source_rgba(ctx->cr,
340 color->red / 255.0,
341 color->green / 255.0,
342 color->blue / 255.0,
343 color->alpha / 255.0);
344 if(filled)
346 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
347 geometry.width, geometry.height);
348 cairo_fill(ctx->cr);
350 else
352 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
353 geometry.width - line_width, geometry.height - line_width);
354 cairo_stroke(ctx->cr);
358 /** Draw rectangle with gradient colors
359 * \param ctx Draw context.
360 * \param geometry Geometry.
361 * \param line_width Line width.
362 * \param filled Filled rectangle?
363 * \param gradient_vector Color-gradient course.
364 * \param pcolor Color at start of gradient_vector.
365 * \param pcolor_center Color in the center.
366 * \param pcolor_end Color at end of gradient_vector.
368 void
369 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
370 vector_t gradient_vector, const color_t *pcolor,
371 const color_t *pcolor_center, const color_t *pcolor_end)
373 cairo_pattern_t *pat;
375 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
376 cairo_set_line_width(ctx->cr, line_width);
377 cairo_set_miter_limit(ctx->cr, 10.0);
378 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
380 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
382 if(filled)
384 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
385 cairo_fill(ctx->cr);
387 else
389 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
390 cairo_stroke(ctx->cr);
393 if(pat)
394 cairo_pattern_destroy(pat);
397 /** Setup some cairo-things for drawing a graph
398 * \param ctx Draw context
400 void
401 draw_graph_setup(draw_context_t *ctx)
403 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
404 cairo_set_line_width(ctx->cr, 1.0);
405 /* without it, it can draw over the path on sharp angles
406 * ...too long lines * (...graph_line) */
407 cairo_set_miter_limit(ctx->cr, 0.0);
408 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
411 /** Draw a graph.
412 * \param ctx Draw context.
413 * \param rect The area to draw into.
414 * \param from Array of starting-point offsets to draw a graph lines.
415 * \param to Array of end-point offsets to draw a graph lines.
416 * \param cur_index Current position in data-array (cycles around).
417 * \param grow Put new values to the left or to the right.
418 * \param gradient_vector Color-Gradient course.
419 * \param pcolor Color at start of gradient_vector.
420 * \param pcolor_center Color in the center.
421 * \param pcolor_end Color at end of gradient_vector.
423 void
424 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
425 position_t grow, vector_t gradient_vector, const color_t *pcolor,
426 const color_t *pcolor_center, const color_t *pcolor_end)
428 int i = -1;
429 float x = rect.x + 0.5; /* middle of a pixel */
430 cairo_pattern_t *pat;
432 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
433 pcolor, pcolor_center, pcolor_end);
435 if(grow == Right) /* draw from right to left */
437 x += rect.width - 1;
438 while(++i < rect.width)
440 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
441 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
442 x -= 1.0;
444 if(--cur_index < 0)
445 cur_index = rect.width - 1;
448 else /* draw from left to right */
449 while(++i < rect.width)
451 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
452 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
453 x += 1.0;
455 if(--cur_index < 0)
456 cur_index = rect.width - 1;
459 cairo_stroke(ctx->cr);
461 if(pat)
462 cairo_pattern_destroy(pat);
465 /** Draw a line into a graph-widget.
466 * \param ctx Draw context.
467 * \param rect The area to draw into.
468 * \param to array of offsets to draw the line through...
469 * \param cur_index current position in data-array (cycles around)
470 * \param grow put new values to the left or to the right
471 * \param gradient_vector Color-gradient course.
472 * \param pcolor Color at start of gradient_vector.
473 * \param pcolor_center Color in the center.
474 * \param pcolor_end Color at end of gradient_vector.
476 void
477 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
478 position_t grow, vector_t gradient_vector, const color_t *pcolor,
479 const color_t *pcolor_center, const color_t *pcolor_end)
481 int i, w;
482 float x, y;
483 cairo_pattern_t *pat;
485 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
486 * ... it won't fill some pixels! It also looks much nicer so.
487 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
488 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
489 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
490 /* a nicer, better visible line compared to 1.0 */
491 cairo_set_line_width(ctx->cr, 1.25);
493 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
495 /* path through the centers of pixels */
496 x = rect.x + 0.5;
497 y = rect.y + 0.5;
498 w = rect.width;
500 if(grow == Right)
502 /* go through the values from old to new. Begin with the oldest. */
503 if(++cur_index > w - 1)
504 cur_index = 0;
506 cairo_move_to(ctx->cr, x, y - to[cur_index]);
508 else
509 /* on the left border: fills a pixel also when there's only one value */
510 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
512 for(i = 0; i < w; i++)
514 cairo_line_to(ctx->cr, x, y - to[cur_index]);
515 x += 1.0;
517 /* cycles around the index */
518 if(grow == Right)
520 if(++cur_index > w - 1)
521 cur_index = 0;
523 else
525 if(--cur_index < 0)
526 cur_index = w - 1;
530 /* onto the right border: fills a pixel also when there's only one value */
531 if(grow == Right)
532 cairo_line_to(ctx->cr, x, y - to[(cur_index + (w - 1)) % w]);
534 cairo_stroke(ctx->cr);
536 if(pat)
537 cairo_pattern_destroy(pat);
538 /* reset line-width */
539 cairo_set_line_width(ctx->cr, 1.0);
542 /** Draw an image from ARGB data to a draw context.
543 * Data should be stored as an array of alpha, red, blue, green for each pixel
544 * and the array size should be w * h elements long.
545 * \param ctx Draw context to draw to.
546 * \param x X coordinate.
547 * \param y Y coordinate.
548 * \param w Width.
549 * \param h Height.
550 * \param ratio The ratio to apply to the image.
551 * \param data The image pixels array.
553 static void
554 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
555 double ratio, unsigned char *data)
557 cairo_t *cr;
558 cairo_surface_t *source;
560 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
561 #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)
562 sizeof(unsigned char) * 4 * w);
563 #else
564 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
565 #endif
566 cr = cairo_create(ctx->surface);
567 cairo_scale(cr, ratio, ratio);
568 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
570 cairo_paint(cr);
572 cairo_destroy(cr);
573 cairo_surface_destroy(source);
576 /** Draw an image to a draw context.
577 * \param ctx Draw context to draw to.
578 * \param x X coordinate.
579 * \param y Y coordinate.
580 * \param ratio The ratio to apply to the image.
581 * \param image The image to draw.
583 void
584 draw_image(draw_context_t *ctx, int x, int y, double ratio, image_t *image)
586 draw_image_from_argb_data(ctx, x, y, image_getwidth(image), image_getheight(image), ratio, image_getdata(image));
589 /** Rotate a pixmap.
590 * \param ctx Draw context to draw with.
591 * \param src Drawable to draw from.
592 * \param dest Drawable to draw to.
593 * \param src_w Drawable width.
594 * \param src_h Drawable height.
595 * \param dest_w Drawable width.
596 * \param dest_h Drawable height.
597 * \param angle angle to rotate.
598 * \param tx Translate to this x coordinate.
599 * \param ty Translate to this y coordinate.
601 void
602 draw_rotate(draw_context_t *ctx,
603 xcb_pixmap_t src, xcb_pixmap_t dest,
604 int src_w, int src_h,
605 int dest_w, int dest_h,
606 double angle, int tx, int ty)
608 cairo_surface_t *surface, *source;
609 cairo_t *cr;
611 surface = cairo_xcb_surface_create(globalconf.connection, dest,
612 globalconf.screens.tab[ctx->phys_screen].visual,
613 dest_w, dest_h);
614 source = cairo_xcb_surface_create(globalconf.connection, src,
615 globalconf.screens.tab[ctx->phys_screen].visual,
616 src_w, src_h);
617 cr = cairo_create (surface);
619 cairo_translate(cr, tx, ty);
620 cairo_rotate(cr, angle);
622 cairo_set_source_surface(cr, source, 0.0, 0.0);
623 cairo_paint(cr);
625 cairo_destroy(cr);
626 cairo_surface_destroy(source);
627 cairo_surface_destroy(surface);
630 /** Return the width and height of a text in pixel.
631 * \param data The draw context text data.
632 * \return Text height and width.
634 area_t
635 draw_text_extents(draw_text_context_t *data)
637 cairo_surface_t *surface;
638 cairo_t *cr;
639 PangoLayout *layout;
640 PangoRectangle ext;
641 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
642 area_t geom = { 0, 0, 0, 0 };
644 if(data->len <= 0)
645 return geom;
647 surface = cairo_xcb_surface_create(globalconf.connection,
648 globalconf.default_screen,
649 globalconf.screens.tab[0].visual,
650 s->width_in_pixels,
651 s->height_in_pixels);
653 cr = cairo_create(surface);
654 layout = pango_cairo_create_layout(cr);
655 pango_layout_set_text(layout, data->text, data->len);
656 pango_layout_set_attributes(layout, data->attr_list);
657 pango_layout_set_font_description(layout, globalconf.font->desc);
658 pango_layout_get_pixel_extents(layout, NULL, &ext);
659 g_object_unref(layout);
660 cairo_destroy(cr);
661 cairo_surface_destroy(surface);
663 geom.width = ext.width;
664 geom.height = ext.height;
666 return geom;
669 /** Transform a string to a alignment_t type.
670 * Recognized string are flex, fixed, left, center, middle or right.
671 * \param align Atring with align text.
672 * \param len The string length.
673 * \return An alignment_t type.
675 alignment_t
676 draw_align_fromstr(const char *align, ssize_t len)
678 switch(a_tokenize(align, len))
680 case A_TK_CENTER: return AlignCenter;
681 case A_TK_RIGHT: return AlignRight;
682 case A_TK_TOP: return AlignTop;
683 case A_TK_BOTTOM: return AlignBottom;
684 case A_TK_MIDDLE: return AlignMiddle;
685 default: return AlignLeft;
689 /** Transform an alignment to a string.
690 * \param a The alignment.
691 * \return A string which must not be freed.
693 const char *
694 draw_align_tostr(alignment_t a)
696 switch(a)
698 case AlignLeft: return "left";
699 case AlignCenter: return "center";
700 case AlignRight: return "right";
701 case AlignBottom: return "bottom";
702 case AlignTop: return "top";
703 case AlignMiddle: return "middle";
704 default: return NULL;
708 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80