]> git.sur5r.net Git - i3/i3/blob - i3bar/include/cairo_util.h
37eaa6e24c30af05319dbd77e833389208d3b1b7
[i3/i3] / i3bar / include / cairo_util.h
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * © 2015 Ingo Bürk and contributors (see also: LICENSE)
5  *
6  * cairo_util.h: Utility for operations using cairo.
7  *
8  */
9 #pragma once
10
11 #include <cairo/cairo-xcb.h>
12
13 /* Represents a color split by color channel. */
14 typedef struct color_t {
15     double red;
16     double green;
17     double blue;
18
19     /* For compatibility, we also store the colorpixel for now. */
20     uint32_t colorpixel;
21 } color_t;
22
23 /* A wrapper grouping an XCB drawable and both a graphics context
24  * and the corresponding cairo objects representing it. */
25 typedef struct surface_t {
26     /* The drawable which is being represented. */
27     xcb_drawable_t id;
28
29     // TODO remove this once i3 uses solely cairo for drawing operations
30     /* A classic XCB graphics context. This should not be used for
31      * drawing operations. */
32     xcb_gcontext_t gc;
33
34     /* A cairo surface representing the drawable. */
35     cairo_surface_t *surface;
36
37     /* The cairo object representing the drawale. In general,
38      * this is what one should use for any drawing operation. */
39     cairo_t *cr;
40 } surface_t;
41
42 /**
43  * Initialize the cairo surface to represent the given drawable.
44  *
45  */
46 void cairo_surface_init(surface_t *surface, xcb_drawable_t drawable, int width, int height);
47
48 /**
49  * Destroys the surface.
50  *
51  */
52 void cairo_surface_free(surface_t *surface);
53
54 /**
55  * Parses the given color in hex format to an internal color representation.
56  * Note that the input must begin with a hash sign, e.g., "#3fbc59".
57  *
58  */
59 color_t cairo_hex_to_color(const char *color);
60
61 /**
62  * Set the given color as the source color on the surface.
63  *
64  */
65 void cairo_set_source_color(surface_t *surface, color_t color);
66
67 /**
68  * Draw the given text using libi3.
69  * This function also marks the surface dirty which is needed if other means of
70  * drawing are used. This will be the case when using XCB to draw text.
71  *
72  */
73 void cairo_draw_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width);
74
75 /**
76  * Draws a filled rectangle.
77  * This function is a convenience wrapper and takes care of flushing the
78  * surface as well as restoring the cairo state.
79  * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
80  *
81  */
82 void cairo_draw_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h);
83
84 /**
85  * Copies a surface onto another surface.
86  * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
87  *
88  */
89 void cairo_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y,
90                         double dest_x, double dest_y, double dest_w, double dest_h);