awful.prompt: add support for Shift+Insert to paste
[awesome.git] / draw.h
blobfde0e760bc1014d5dfaddd65fa06668b366b809e
1 /*
2 * draw.h - draw functions header
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 #ifndef AWESOME_COMMON_DRAW_H
23 #define AWESOME_COMMON_DRAW_H
25 #include <cairo.h>
26 #include <pango/pangocairo.h>
28 #include <xcb/xcb.h>
30 #include "image.h"
31 #include "common/array.h"
33 /** Padding type */
34 typedef struct
36 /** Padding at top */
37 int top;
38 /** Padding at bottom */
39 int bottom;
40 /** Padding at left */
41 int left;
42 /** Padding at right */
43 int right;
44 } padding_t;
46 typedef struct
48 unsigned initialized : 1;
49 uint32_t pixel;
50 uint16_t red;
51 uint16_t green;
52 uint16_t blue;
53 uint16_t alpha;
54 } xcolor_t;
56 typedef enum
58 AlignLeft = (0),
59 AlignRight = (1),
60 AlignCenter = (1 << 1),
61 AlignTop = (1 << 2),
62 AlignBottom = (1 << 3),
63 AlignFlex = (1 << 4),
64 AlignFixed = (1 << 5),
65 } alignment_t;
67 typedef struct vector_t vector_t;
68 struct vector_t
70 /** Co-ords of starting point */
71 int16_t x;
72 int16_t y;
73 /** Offset to starting point */
74 int16_t x_offset;
75 int16_t y_offset;
78 typedef struct area_t area_t;
79 struct area_t
81 /** Co-ords of upper left corner */
82 int16_t x;
83 int16_t y;
84 uint16_t width;
85 uint16_t height;
88 DO_ARRAY(area_t, area, DO_NOTHING)
90 #define AREA_LEFT(a) ((a).x)
91 #define AREA_TOP(a) ((a).y)
92 #define AREA_RIGHT(a) ((a).x + (a).width)
93 #define AREA_BOTTOM(a) ((a).y + (a).height)
95 typedef struct
97 PangoFontDescription *desc;
98 int height;
99 } font_t;
101 typedef struct
103 xcb_pixmap_t pixmap;
104 xcb_visualtype_t *visual;
105 uint16_t width;
106 uint16_t height;
107 int phys_screen;
108 uint8_t depth;
109 cairo_t *cr;
110 cairo_surface_t *surface;
111 PangoLayout *layout;
112 xcolor_t fg;
113 xcolor_t bg;
114 } draw_context_t;
116 void draw_context_init(draw_context_t *, int, int, int,
117 xcb_pixmap_t, const xcolor_t *, const xcolor_t *);
119 /** Wipe a draw context.
120 * \param ctx The draw_context_t to wipe.
122 static inline void
123 draw_context_wipe(draw_context_t *ctx)
125 if(ctx->layout)
127 g_object_unref(ctx->layout);
128 ctx->layout = NULL;
130 if(ctx->surface)
132 cairo_surface_destroy(ctx->surface);
133 ctx->surface = NULL;
135 if(ctx->cr)
137 cairo_destroy(ctx->cr);
138 ctx->cr = NULL;
142 font_t *draw_font_new(const char *);
143 void draw_font_delete(font_t **);
145 bool draw_iso2utf8(const char *, size_t, char **, ssize_t *);
147 /** Convert a string to UTF-8.
148 * \param str The string to convert.
149 * \param len The string length.
150 * \param dest The destination string that will be allocated.
151 * \param dlen The destination string length allocated, can be NULL.
152 * \return True if the conversion happened, false otherwise. In both case, dest
153 * and dlen will have value and dest have to be free().
155 static inline bool
156 a_iso2utf8(const char *str, ssize_t len, char **dest, ssize_t *dlen)
158 if(draw_iso2utf8(str, len, dest, dlen))
159 return true;
161 *dest = a_strdup(str);
162 if(dlen)
163 *dlen = len;
165 return false;
168 typedef struct
170 PangoAttrList *attr_list;
171 char *text;
172 ssize_t len;
173 } draw_text_context_t;
175 bool draw_text_context_init(draw_text_context_t *, const char *, ssize_t);
176 void draw_text(draw_context_t *, draw_text_context_t *, PangoEllipsizeMode, PangoWrapMode, alignment_t, padding_t *, area_t, area_t *);
177 void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *);
178 void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t,
179 const xcolor_t *, const xcolor_t *, const xcolor_t *);
181 void draw_graph_setup(draw_context_t *);
182 void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, vector_t,
183 const xcolor_t *, const xcolor_t *, const xcolor_t *);
184 void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, vector_t,
185 const xcolor_t *, const xcolor_t *, const xcolor_t *);
186 void draw_image(draw_context_t *, int, int, double, image_t *);
187 void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int);
188 area_t draw_text_extents(draw_text_context_t *);
189 alignment_t draw_align_fromstr(const char *, ssize_t);
190 const char *draw_align_tostr(alignment_t);
192 typedef struct
194 union
196 xcb_alloc_color_cookie_t cookie_hexa;
197 xcb_alloc_named_color_cookie_t cookie_named;
200 uint16_t alpha;
201 xcolor_t *color;
202 bool is_hexa, has_error;
203 const char *colstr;
204 } xcolor_init_request_t;
206 xcolor_init_request_t xcolor_init_unchecked(xcolor_t *, const char *, ssize_t);
207 bool xcolor_init_reply(xcolor_init_request_t);
209 static inline void
210 draw_text_context_wipe(draw_text_context_t *pdata)
212 if(pdata)
214 if(pdata->attr_list)
215 pango_attr_list_unref(pdata->attr_list);
216 p_delete(&pdata->text);
220 #endif
221 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80