awful: add generic completion wrapper for awful.prompt.run()
[awesome.git] / draw.c
blobe21e415c2e49a6e20cc1f8d7a25c1513c6ffb773
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-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"
34 #include "common/tokenize.h"
35 #include "common/markup.h"
37 extern awesome_t globalconf;
39 /** Convert text from any charset to UTF-8 using iconv.
40 * \param iso The ISO string to convert.
41 * \param len The string size.
42 * \return NULL if error, otherwise pointer to the new converted string.
44 char *
45 draw_iso2utf8(const char *iso, size_t len)
47 size_t utf8len;
48 char *utf8, *utf8p;
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 NULL;
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 NULL;
73 utf8len = 2 * len + 1;
74 utf8 = utf8p = p_new(char, utf8len);
76 if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
78 warn("text conversion failed: %s", strerror(errno));
79 p_delete(&utf8p);
82 return utf8p;
85 static xcb_visualtype_t *
86 draw_screen_default_visual(xcb_screen_t *s)
88 xcb_depth_iterator_t depth_iter;
89 xcb_visualtype_iterator_t visual_iter;
91 if(!s)
92 return NULL;
94 for(depth_iter = xcb_screen_allowed_depths_iterator(s);
95 depth_iter.rem; xcb_depth_next (&depth_iter))
96 for(visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
97 visual_iter.rem; xcb_visualtype_next (&visual_iter))
98 if (s->root_visual == visual_iter.data->visual_id)
99 return visual_iter.data;
101 return NULL;
104 /** Create a new Pango font.
105 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
106 * \return A new font.
108 font_t *
109 draw_font_new(const char *fontname)
111 cairo_surface_t *surface;
112 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
113 cairo_t *cr;
114 PangoLayout *layout;
115 font_t *font = p_new(font_t, 1);
117 /* Create a dummy cairo surface, cairo context and pango layout in
118 * order to get font informations */
119 surface = cairo_xcb_surface_create(globalconf.connection,
120 globalconf.default_screen,
121 draw_screen_default_visual(s),
122 s->width_in_pixels,
123 s->height_in_pixels);
125 cr = cairo_create(surface);
126 layout = pango_cairo_create_layout(cr);
128 /* Get the font description used to set text on a PangoLayout */
129 font->desc = pango_font_description_from_string(fontname);
130 pango_layout_set_font_description(layout, font->desc);
132 /* Get height */
133 pango_layout_get_pixel_size(layout, NULL, &font->height);
135 g_object_unref(layout);
136 cairo_destroy(cr);
137 cairo_surface_destroy(surface);
139 return font;
142 /** Delete a font.
143 * \param font Font to delete.
145 void
146 draw_font_delete(font_t **font)
148 if(*font)
150 pango_font_description_free((*font)->desc);
151 p_delete(font);
155 static void
156 draw_markup_on_element(markup_parser_data_t *p, const char *elem,
157 const char **names, const char **values)
159 draw_parser_data_t *data = p->priv;
161 xcolor_init_request_t reqs[3];
162 int8_t i, bg_color_nbr = -1, reqs_nbr = -1;
164 /* hack: markup.c validates tags so we can avoid strcmps here */
165 switch (*elem) {
166 case 'b':
167 if(elem[1] == 'g') /* bg? */
169 if(elem[2] == '_') /* bg_margin */
170 for(; *names; names++, values++)
171 switch(a_tokenize(*names, -1))
173 case A_TK_LEFT:
174 data->bg_margin.left = atoi(*values);
175 break;
176 case A_TK_TOP:
177 data->bg_margin.top = atoi(*values);
178 default:
179 break;
181 else /* bg */
182 for(; *names; names++, values++)
183 switch(a_tokenize(*names, -1))
185 case A_TK_COLOR:
186 reqs[++reqs_nbr] = xcolor_init_unchecked(&data->bg_color,
187 *values,
188 a_strlen(*values));
190 bg_color_nbr = reqs_nbr;
191 break;
192 case A_TK_IMAGE:
193 if(data->bg_image)
194 image_delete(&data->bg_image);
195 data->bg_image = image_new_from_file(*values);
196 break;
197 case A_TK_ALIGN:
198 data->bg_align = draw_align_fromstr(*values, -1);
199 break;
200 case A_TK_RESIZE:
201 data->bg_resize = a_strtobool(*values, -1);
202 default:
203 break;
207 else /* border */
208 for(; *names; names++, values++)
209 switch(a_tokenize(*names, -1))
211 case A_TK_COLOR:
212 reqs[++reqs_nbr] = xcolor_init_unchecked(&data->border.color,
213 *values,
214 a_strlen(*values));
215 break;
216 case A_TK_WIDTH:
217 data->border.width = atoi(*values);
218 break;
219 default:
220 break;
222 break;
223 case 't': /* text */
224 for(; *names; names++, values++)
225 switch(a_tokenize(*names, -1))
227 case A_TK_ALIGN:
228 data->align = draw_align_fromstr(*values, -1);
229 break;
230 case A_TK_SHADOW:
231 reqs[++reqs_nbr] = xcolor_init_unchecked(&data->shadow.color,
232 *values,
233 a_strlen(*values));
234 break;
235 case A_TK_SHADOW_OFFSET:
236 data->shadow.offset = atoi(*values);
237 break;
238 default:
239 break;
241 break;
242 case 'm': /* margin */
243 for (; *names; names++, values++)
244 switch(a_tokenize(*names, -1))
246 case A_TK_LEFT:
247 data->margin.left = atoi(*values);
248 break;
249 case A_TK_RIGHT:
250 data->margin.right = atoi(*values);
251 break;
252 case A_TK_TOP:
253 data->margin.top = atoi(*values);
254 default:
255 break;
257 break;
260 for(i = 0; i <= reqs_nbr; i++)
261 if(i == bg_color_nbr)
262 data->has_bg_color = xcolor_init_reply(reqs[i]);
263 else
264 xcolor_init_reply(reqs[i]);
267 static bool
268 draw_text_markup_expand(draw_parser_data_t *data,
269 const char *str, ssize_t slen)
271 static char const * const elements[] = { "bg", "bg_margin", "text", "margin", "border", NULL };
272 markup_parser_data_t p =
274 .elements = elements,
275 .priv = data,
276 .on_element = &draw_markup_on_element,
278 GError *error = NULL;
279 bool ret = false;
281 markup_parser_data_init(&p);
283 if(!markup_parse(&p, str, slen))
284 goto bailout;
286 if(!pango_parse_markup(p.text.s, p.text.len, 0, &data->attr_list, &data->text, NULL, &error))
288 warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
289 if(error)
290 g_error_free(error);
291 goto bailout;
294 data->len = a_strlen(data->text);
295 ret = true;
297 bailout:
298 markup_parser_data_wipe(&p);
299 return ret;
302 /** Initialize a new draw context.
303 * \param d The draw context to initialize.
304 * \param phys_screen Physical screen id.
305 * \param width Width.
306 * \param height Height.
307 * \param px Pixmap object to store.
308 * \param fg Foreground color.
309 * \param bg Background color.
311 void
312 draw_context_init(draw_context_t *d, int phys_screen,
313 int width, int height, xcb_pixmap_t px,
314 const xcolor_t *fg, const xcolor_t *bg)
316 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
318 d->phys_screen = phys_screen;
319 d->width = width;
320 d->height = height;
321 d->depth = s->root_depth;
322 d->visual = draw_screen_default_visual(s);
323 d->pixmap = px;
324 d->surface = cairo_xcb_surface_create(globalconf.connection, px, d->visual, width, height);
325 d->cr = cairo_create(d->surface);
326 d->layout = pango_cairo_create_layout(d->cr);
327 d->fg = *fg;
328 d->bg = *bg;
331 /** Draw text into a draw context.
332 * \param ctx Draw context to draw to.
333 * \param font The font to use.
334 * \param elip Ellipsize mode.
335 * \param wrap Wrap mode.
336 * \param area Area to draw to.
337 * \param text Text to draw.
338 * \param len Text to draw length.
339 * \param data Optional parser data.
341 void
342 draw_text(draw_context_t *ctx, font_t *font, PangoEllipsizeMode ellip, PangoWrapMode wrap,
343 area_t area, const char *text, ssize_t len, draw_parser_data_t *pdata)
345 int x, y;
346 PangoRectangle ext;
347 draw_parser_data_t parser_data;
349 if(!pdata)
351 draw_parser_data_init(&parser_data);
352 if(draw_text_markup_expand(&parser_data, text, len))
354 text = parser_data.text;
355 len = parser_data.len;
357 pdata = &parser_data;
359 else
361 text = pdata->text;
362 len = pdata->len;
365 if(pdata->has_bg_color)
366 draw_rectangle(ctx, area, 1.0, true, &pdata->bg_color);
368 if(pdata->border.width > 0)
369 draw_rectangle(ctx, area, pdata->border.width, false, &pdata->border.color);
371 if(pdata->bg_image)
373 x = area.x;
374 y = area.y;
375 switch(pdata->bg_align)
377 case AlignCenter:
378 x += (area.width - pdata->bg_image->width) / 2;
379 break;
380 case AlignRight:
381 x += area.width- pdata->bg_image->width;
382 break;
383 default:
384 break;
386 draw_image(ctx,
387 x + pdata->bg_margin.left,
388 y + pdata->bg_margin.top,
389 pdata->bg_resize ? area.height : 0, pdata->bg_image);
392 pango_layout_set_text(ctx->layout, pdata->text, pdata->len);
393 pango_layout_set_width(ctx->layout,
394 pango_units_from_double(area.width
395 - (pdata->margin.left
396 + pdata->margin.right)));
397 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height) - pdata->margin.top);
398 pango_layout_set_ellipsize(ctx->layout, ellip);
399 pango_layout_set_wrap(ctx->layout, wrap);
400 pango_layout_set_attributes(ctx->layout, pdata->attr_list);
401 pango_layout_set_font_description(ctx->layout, font->desc);
402 pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
404 x = area.x + pdata->margin.left;
405 /* + 1 is added for rounding, so that in any case of doubt we rather draw
406 * the text 1px lower than too high which usually results in a better type
407 * face */
408 y = area.y + (ctx->height - ext.height + 1) / 2 + pdata->margin.top;
410 /* only honors alignment if enough space */
411 if(ext.width < area.width)
412 switch(pdata->align)
414 case AlignCenter:
415 x += (area.width - ext.width) / 2;
416 break;
417 case AlignRight:
418 x += area.width - ext.width;
419 break;
420 default:
421 break;
424 if(pdata->shadow.offset)
426 cairo_set_source_rgba(ctx->cr,
427 pdata->shadow.color.red / 65535.0,
428 pdata->shadow.color.green / 65535.0,
429 pdata->shadow.color.blue / 65535.0,
430 pdata->shadow.color.alpha / 65535.0);
431 cairo_move_to(ctx->cr, x + pdata->shadow.offset, y + pdata->shadow.offset);
432 pango_cairo_layout_path(ctx->cr, ctx->layout);
433 cairo_stroke(ctx->cr);
436 cairo_move_to(ctx->cr, x, y);
438 cairo_set_source_rgba(ctx->cr,
439 ctx->fg.red / 65535.0,
440 ctx->fg.green / 65535.0,
441 ctx->fg.blue / 65535.0,
442 ctx->fg.alpha / 65535.0);
443 pango_cairo_update_layout(ctx->cr, ctx->layout);
444 pango_cairo_show_layout(ctx->cr, ctx->layout);
446 if (pdata == &parser_data)
447 draw_parser_data_wipe(&parser_data);
450 /** Setup color-source for cairo (gradient or mono).
451 * \param ctx Draw context.
452 * \param gradient_vector x, y to x + x_offset, y + y_offset.
453 * \param pcolor Color to use at start of gradient_vector.
454 * \param pcolor_center Color at center of gradient_vector.
455 * \param pcolor_end Color at end of gradient_vector.
456 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
458 static cairo_pattern_t *
459 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
460 const xcolor_t *pcolor, const xcolor_t *pcolor_center,
461 const xcolor_t *pcolor_end)
463 cairo_pattern_t *pat = NULL;
464 bool has_center = pcolor_center->initialized;
465 bool has_end = pcolor_end->initialized;
467 /* no need for a real pattern: */
468 if(!has_end && !has_center)
469 cairo_set_source_rgba(ctx->cr,
470 pcolor->red / 65535.0,
471 pcolor->green / 65535.0,
472 pcolor->blue / 65535.0,
473 pcolor->alpha / 65535.0);
474 else
476 pat = cairo_pattern_create_linear(gradient_vector.x,
477 gradient_vector.y,
478 gradient_vector.x + gradient_vector.x_offset,
479 gradient_vector.y + gradient_vector.y_offset);
481 /* pcolor is always set (so far in awesome) */
482 cairo_pattern_add_color_stop_rgba(pat, 0.0,
483 pcolor->red / 65535.0,
484 pcolor->green / 65535.0,
485 pcolor->blue / 65535.0,
486 pcolor->alpha / 65535.0);
488 if(has_center)
489 cairo_pattern_add_color_stop_rgba(pat, 0.5,
490 pcolor_center->red / 65535.0,
491 pcolor_center->green / 65535.0,
492 pcolor_center->blue / 65535.0,
493 pcolor_center->alpha / 65535.0);
495 if(has_end)
496 cairo_pattern_add_color_stop_rgba(pat, 1.0,
497 pcolor_end->red / 65535.0,
498 pcolor_end->green / 65535.0,
499 pcolor_end->blue / 65535.0,
500 pcolor_end->alpha / 65535.0);
501 else
502 cairo_pattern_add_color_stop_rgba(pat, 1.0,
503 pcolor->red / 65535.0,
504 pcolor->green / 65535.0,
505 pcolor->blue / 65535.0,
506 pcolor->alpha / 65535.0);
507 cairo_set_source(ctx->cr, pat);
509 return pat;
512 /** Draw rectangle inside the coordinates
513 * \param ctx Draw context
514 * \param geometry geometry
515 * \param line_width line width
516 * \param filled fill rectangle?
517 * \param color color to use
519 void
520 draw_rectangle(draw_context_t *ctx, area_t geometry,
521 float line_width, bool filled, const xcolor_t *color)
523 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
524 cairo_set_line_width(ctx->cr, line_width);
525 cairo_set_miter_limit(ctx->cr, 10.0);
526 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
527 cairo_set_source_rgba(ctx->cr,
528 color->red / 65535.0,
529 color->green / 65535.0,
530 color->blue / 65535.0,
531 color->alpha / 65535.0);
532 if(filled)
534 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
535 geometry.width, geometry.height);
536 cairo_fill(ctx->cr);
538 else
540 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
541 geometry.width - line_width, geometry.height - line_width);
542 cairo_stroke(ctx->cr);
546 /** Draw rectangle with gradient colors
547 * \param ctx Draw context.
548 * \param geometry Geometry.
549 * \param line_width Line width.
550 * \param filled Filled rectangle?
551 * \param gradient_vector Color-gradient course.
552 * \param pcolor Color at start of gradient_vector.
553 * \param pcolor_center Color in the center.
554 * \param pcolor_end Color at end of gradient_vector.
556 void
557 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
558 vector_t gradient_vector, const xcolor_t *pcolor,
559 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
561 cairo_pattern_t *pat;
563 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
564 cairo_set_line_width(ctx->cr, line_width);
565 cairo_set_miter_limit(ctx->cr, 10.0);
566 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
568 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
570 if(filled)
572 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
573 cairo_fill(ctx->cr);
575 else
577 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
578 cairo_stroke(ctx->cr);
581 if(pat)
582 cairo_pattern_destroy(pat);
585 /** Setup some cairo-things for drawing a graph
586 * \param ctx Draw context
588 void
589 draw_graph_setup(draw_context_t *ctx)
591 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
592 cairo_set_line_width(ctx->cr, 1.0);
593 /* without it, it can draw over the path on sharp angles
594 * ...too long lines * (...graph_line) */
595 cairo_set_miter_limit(ctx->cr, 0.0);
596 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
599 /** Draw a graph.
600 * \param ctx Draw context.
601 * \param rect The area to draw into.
602 * \param from Array of starting-point offsets to draw a graph lines.
603 * \param to Array of end-point offsets to draw a graph lines.
604 * \param cur_index Current position in data-array (cycles around).
605 * \param grow Put new values to the left or to the right.
606 * \param gradient_vector Color-Gradient course.
607 * \param pcolor Color at start of gradient_vector.
608 * \param pcolor_center Color in the center.
609 * \param pcolor_end Color at end of gradient_vector.
611 void
612 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
613 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
614 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
616 int i = -1;
617 float x = rect.x + 0.5; /* middle of a pixel */
618 cairo_pattern_t *pat;
620 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
621 pcolor, pcolor_center, pcolor_end);
623 if(grow == Right) /* draw from right to left */
625 x += rect.width - 1;
626 while(++i < rect.width)
628 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
629 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
630 x -= 1.0;
632 if (--cur_index < 0)
633 cur_index = rect.width - 1;
636 else /* draw from left to right */
637 while(++i < rect.width)
639 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
640 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
641 x += 1.0;
643 if (--cur_index < 0)
644 cur_index = rect.width - 1;
647 cairo_stroke(ctx->cr);
649 if(pat)
650 cairo_pattern_destroy(pat);
653 /** Draw a line into a graph-widget.
654 * \param ctx Draw context.
655 * \param rect The area to draw into.
656 * \param to array of offsets to draw the line through...
657 * \param cur_index current position in data-array (cycles around)
658 * \param grow put new values to the left or to the right
659 * \param gradient_vector Color-gradient course.
660 * \param pcolor Color at start of gradient_vector.
661 * \param pcolor_center Color in the center.
662 * \param pcolor_end Color at end of gradient_vector.
664 void
665 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
666 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
667 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
669 int i, w;
670 float x, y;
671 cairo_pattern_t *pat;
673 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
674 * ... it won't fill some pixels! It also looks much nicer so.
675 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
676 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
677 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
678 /* a nicer, better visible line compared to 1.0 */
679 cairo_set_line_width(ctx->cr, 1.25);
681 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
683 /* path through the centers of pixels */
684 x = rect.x + 0.5;
685 y = rect.y + 0.5;
686 w = rect.width;
688 if(grow == Right)
690 /* go through the values from old to new. Begin with the oldest. */
691 if(++cur_index > w - 1)
692 cur_index = 0;
694 cairo_move_to(ctx->cr, x, y - to[cur_index]);
696 else
697 /* on the left border: fills a pixel also when there's only one value */
698 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
700 for(i = 0; i < w; i++)
702 cairo_line_to(ctx->cr, x, y - to[cur_index]);
703 x += 1.0;
705 /* cycles around the index */
706 if(grow == Right)
708 if (++cur_index > w - 1)
709 cur_index = 0;
711 else
713 if(--cur_index < 0)
714 cur_index = w - 1;
718 /* onto the right border: fills a pixel also when there's only one value */
719 if(grow == Right)
720 cairo_line_to(ctx->cr, x, y - to[cur_index]);
722 cairo_stroke(ctx->cr);
724 if(pat)
725 cairo_pattern_destroy(pat);
726 /* reset line-width */
727 cairo_set_line_width(ctx->cr, 1.0);
730 /** Draw an image from ARGB data to a draw context.
731 * Data should be stored as an array of alpha, red, blue, green for each pixel
732 * and the array size should be w * h elements long.
733 * \param ctx Draw context to draw to.
734 * \param x X coordinate.
735 * \param y Y coordinate.
736 * \param w Width.
737 * \param h Height.
738 * \param wanted_h Wanted height: if > 0, image will be resized.
739 * \param data The image pixels array.
741 static void
742 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
743 int wanted_h, unsigned char *data)
745 double ratio;
746 cairo_t *cr;
747 cairo_surface_t *source;
749 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
750 #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)
751 sizeof(unsigned char) * 4 * w);
752 #else
753 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
754 #endif
755 cr = cairo_create(ctx->surface);
756 if(wanted_h > 0 && h > 0)
758 ratio = (double) wanted_h / (double) h;
759 cairo_scale(cr, ratio, ratio);
760 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
762 else
763 cairo_set_source_surface(cr, source, x, y);
765 cairo_paint(cr);
767 cairo_destroy(cr);
768 cairo_surface_destroy(source);
771 /** Draw an image to a draw context.
772 * \param ctx Draw context to draw to.
773 * \param x X coordinate.
774 * \param y Y coordinate.
775 * \param wanted_h Wanted height: if > 0, image will be resized.
776 * \param image The image to draw.
778 void
779 draw_image(draw_context_t *ctx, int x, int y, int wanted_h, image_t *image)
781 draw_image_from_argb_data(ctx, x, y, image->width, image->height, wanted_h, image->data);
784 /** Rotate a pixmap.
785 * \param ctx Draw context to draw with.
786 * \param src Drawable to draw from.
787 * \param dest Drawable to draw to.
788 * \param src_w Drawable width.
789 * \param src_h Drawable height.
790 * \param dest_w Drawable width.
791 * \param dest_h Drawable height.
792 * \param angle angle to rotate.
793 * \param tx Translate to this x coordinate.
794 * \param ty Translate to this y coordinate.
796 void
797 draw_rotate(draw_context_t *ctx,
798 xcb_pixmap_t src, xcb_pixmap_t dest,
799 int src_w, int src_h,
800 int dest_w, int dest_h,
801 double angle, int tx, int ty)
803 cairo_surface_t *surface, *source;
804 cairo_t *cr;
806 surface = cairo_xcb_surface_create(globalconf.connection, dest,
807 ctx->visual, dest_w, dest_h);
808 source = cairo_xcb_surface_create(globalconf.connection, src,
809 ctx->visual, src_w, src_h);
810 cr = cairo_create (surface);
812 cairo_translate(cr, tx, ty);
813 cairo_rotate(cr, angle);
815 cairo_set_source_surface(cr, source, 0.0, 0.0);
816 cairo_paint(cr);
818 cairo_destroy(cr);
819 cairo_surface_destroy(source);
820 cairo_surface_destroy(surface);
823 /** Return the width and height of a text in pixel.
824 * \param font Font to use.
825 * \param text The text.
826 * \param len The text length.
827 * \param pdata The parser data to fill.
828 * \return Text height and width.
830 area_t
831 draw_text_extents(font_t *font, const char *text, ssize_t len, draw_parser_data_t *parser_data)
833 cairo_surface_t *surface;
834 cairo_t *cr;
835 PangoLayout *layout;
836 PangoRectangle ext;
837 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
838 area_t geom = { 0, 0, 0, 0 };
840 if(!len)
841 return geom;
843 if(!draw_text_markup_expand(parser_data, text, len))
844 return geom;
846 surface = cairo_xcb_surface_create(globalconf.connection,
847 globalconf.default_screen,
848 draw_screen_default_visual(s),
849 s->width_in_pixels,
850 s->height_in_pixels);
852 cr = cairo_create(surface);
853 layout = pango_cairo_create_layout(cr);
854 pango_layout_set_text(layout, parser_data->text, parser_data->len);
855 pango_layout_set_attributes(layout, parser_data->attr_list);
856 pango_layout_set_font_description(layout, font->desc);
857 pango_layout_get_pixel_extents(layout, NULL, &ext);
858 g_object_unref(layout);
859 cairo_destroy(cr);
860 cairo_surface_destroy(surface);
862 geom.width = ext.width;
863 geom.height = ext.height * 1.5;
865 return geom;
868 /** Transform a string to a alignment_t type.
869 * Recognized string are flex, left, center or right. Everything else will be
870 * recognized as AlignLeft.
871 * \param align Atring with align text.
872 * \param len The string length.
873 * \return An alignment_t type.
875 alignment_t
876 draw_align_fromstr(const char *align, ssize_t len)
878 switch (a_tokenize(align, len))
880 case A_TK_CENTER: return AlignCenter;
881 case A_TK_RIGHT: return AlignRight;
882 case A_TK_FLEX: return AlignFlex;
883 case A_TK_TOP: return AlignTop;
884 case A_TK_BOTTOM: return AlignBottom;
885 default: return AlignLeft;
889 /** Transform an alignment to a string.
890 * \param a The alignment.
891 * \return A string which must not be freed.
893 const char *
894 draw_align_tostr(alignment_t a)
896 switch(a)
898 case AlignLeft: return "left";
899 case AlignCenter: return "center";
900 case AlignRight: return "right";
901 case AlignFlex: return "flex";
902 case AlignBottom: return "bottom";
903 case AlignTop: return "top";
904 default: return NULL;
908 #define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
910 /** Send a request to initialize a X color.
911 * \param color xcolor_t struct to store color into.
912 * \param colstr Color specification.
913 * \return request informations.
915 xcolor_init_request_t
916 xcolor_init_unchecked(xcolor_t *color, const char *colstr, ssize_t len)
918 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
919 xcolor_init_request_t req;
920 unsigned long colnum;
921 uint16_t red, green, blue;
923 p_clear(&req, 1);
925 if(!len)
927 req.has_error = true;
928 return req;
931 req.alpha = 0xffff;
932 req.color = color;
934 /* The color is given in RGB value */
935 if(colstr[0] == '#')
937 char *p;
939 if(len == 7)
941 colnum = strtoul(colstr + 1, &p, 16);
942 if(p - colstr != 7)
943 goto invalid;
945 /* we have alpha */
946 else if(len == 9)
948 colnum = strtoul(colstr + 1, &p, 16);
949 if(p - colstr != 9)
950 goto invalid;
951 req.alpha = RGB_COLOR_8_TO_16(colnum);
952 colnum >>= 8;
954 else
956 invalid:
957 warn("awesome: error, invalid color '%s'", colstr);
958 req.has_error = true;
959 return req;
962 red = RGB_COLOR_8_TO_16(colnum >> 16);
963 green = RGB_COLOR_8_TO_16(colnum >> 8);
964 blue = RGB_COLOR_8_TO_16(colnum);
966 req.is_hexa = true;
967 req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
968 s->default_colormap,
969 red, green, blue);
971 else
973 req.is_hexa = false;
974 req.cookie_named = xcb_alloc_named_color_unchecked(globalconf.connection,
975 s->default_colormap, len,
976 colstr);
979 req.has_error = false;
980 req.colstr = colstr;
982 return req;
985 /** Initialize a X color.
986 * \param req xcolor_init request.
987 * \return True if color allocation was successfull.
989 bool
990 xcolor_init_reply(xcolor_init_request_t req)
992 if(req.has_error)
993 return false;
995 if(req.is_hexa)
997 xcb_alloc_color_reply_t *hexa_color;
999 if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
1000 req.cookie_hexa, NULL)))
1002 req.color->pixel = hexa_color->pixel;
1003 req.color->red = hexa_color->red;
1004 req.color->green = hexa_color->green;
1005 req.color->blue = hexa_color->blue;
1006 req.color->alpha = req.alpha;
1007 req.color->initialized = true;
1008 p_delete(&hexa_color);
1009 return true;
1012 else
1014 xcb_alloc_named_color_reply_t *named_color;
1016 if((named_color = xcb_alloc_named_color_reply(globalconf.connection,
1017 req.cookie_named, NULL)))
1019 req.color->pixel = named_color->pixel;
1020 req.color->red = named_color->visual_red;
1021 req.color->green = named_color->visual_green;
1022 req.color->blue = named_color->visual_blue;
1023 req.color->alpha = req.alpha;
1024 req.color->initialized = true;
1025 p_delete(&named_color);
1026 return true;
1030 warn("awesome: error, cannot allocate color '%s'", req.colstr);
1031 return false;
1034 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80