awful.menu: align text when item doesn't have icon
[awesome.git] / draw.h
blob6f0b09afaef6416301d49b7a026bf935c192ff8e
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"
32 #include "common/list.h"
33 #include "common/buffer.h"
34 #include "common/xutil.h"
36 typedef struct
38 unsigned initialized : 1;
39 uint32_t pixel;
40 uint16_t red;
41 uint16_t green;
42 uint16_t blue;
43 uint16_t alpha;
44 } xcolor_t;
46 typedef enum
48 AlignLeft = 0,
49 AlignRight,
50 AlignCenter,
51 AlignFlex,
52 AlignAuto
53 } alignment_t;
55 typedef struct vector_t vector_t;
56 struct vector_t
58 /** Co-ords of starting point */
59 int16_t x;
60 int16_t y;
61 /** Offset to starting point */
62 int16_t x_offset;
63 int16_t y_offset;
66 typedef struct area_t area_t;
67 struct area_t
69 /** Co-ords of upper left corner */
70 int16_t x;
71 int16_t y;
72 uint16_t width;
73 uint16_t height;
76 DO_ARRAY(area_t, area, DO_NOTHING)
78 #define AREA_LEFT(a) ((a).x)
79 #define AREA_TOP(a) ((a).y)
80 #define AREA_RIGHT(a) ((a).x + (a).width)
81 #define AREA_BOTTOM(a) ((a).y + (a).height)
83 typedef struct
85 PangoFontDescription *desc;
86 int height;
87 } font_t;
89 typedef struct
91 xcb_pixmap_t pixmap;
92 xcb_visualtype_t *visual;
93 uint16_t width;
94 uint16_t height;
95 int phys_screen;
96 uint8_t depth;
97 cairo_t *cr;
98 cairo_surface_t *surface;
99 PangoLayout *layout;
100 xcolor_t fg;
101 xcolor_t bg;
102 } draw_context_t;
104 void draw_context_init(draw_context_t *, int, int, int,
105 xcb_pixmap_t, const xcolor_t *, const xcolor_t *);
107 /** Wipe a draw context.
108 * \param ctx The draw_context_t to wipe.
110 static inline void
111 draw_context_wipe(draw_context_t *ctx)
113 if(ctx->layout)
115 g_object_unref(ctx->layout);
116 ctx->layout = NULL;
118 if(ctx->surface)
120 cairo_surface_destroy(ctx->surface);
121 ctx->surface = NULL;
123 if(ctx->cr)
125 cairo_destroy(ctx->cr);
126 ctx->cr = NULL;
130 font_t *draw_font_new(int, const char *);
131 void draw_font_delete(font_t **);
133 char * draw_iso2utf8(const char *, size_t);
135 static inline bool
136 a_iso2utf8(char **dest, const char *str, ssize_t len)
138 char *utf8;
139 if((utf8 = draw_iso2utf8(str, len)))
141 *dest = utf8;
142 return true;
144 *dest = a_strdup(str);
145 return false;
148 typedef struct
150 PangoAttrList *attr_list;
151 char *text;
152 ssize_t len;
153 alignment_t align;
154 struct
156 int left, right, top;
157 } margin;
158 struct
160 int top, left;
161 } bg_margin;
162 bool has_bg_color;
163 xcolor_t bg_color;
164 image_t *bg_image;
165 alignment_t bg_align;
166 bool bg_resize;
167 struct
169 int offset;
170 xcolor_t color;
171 } shadow;
172 struct
174 int width;
175 xcolor_t color;
176 } border;
177 } draw_parser_data_t;
179 bool draw_text_markup_expand(draw_parser_data_t *, const char *, ssize_t);
181 void draw_text(draw_context_t *, font_t *, area_t, const char *, ssize_t len, draw_parser_data_t *);
182 void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *);
183 void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t,
184 const xcolor_t *, const xcolor_t *, const xcolor_t *);
186 void draw_graph_setup(draw_context_t *);
187 void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, vector_t,
188 const xcolor_t *, const xcolor_t *, const xcolor_t *);
189 void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, vector_t,
190 const xcolor_t *, const xcolor_t *, const xcolor_t *);
191 void draw_image(draw_context_t *, int, int, int, image_t *);
192 void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int);
193 area_t draw_text_extents(int, font_t *, const char *, ssize_t, draw_parser_data_t *);
194 alignment_t draw_align_fromstr(const char *, ssize_t);
195 const char *draw_align_tostr(alignment_t);
197 typedef struct
199 union
201 xcb_alloc_color_cookie_t cookie_hexa;
202 xcb_alloc_named_color_cookie_t cookie_named;
205 uint16_t alpha;
206 xcolor_t *color;
207 bool is_hexa, has_error;
208 const char *colstr;
209 } xcolor_init_request_t;
211 xcolor_init_request_t xcolor_init_unchecked(xcolor_t *, const char *, ssize_t);
212 bool xcolor_init_reply(xcolor_init_request_t);
214 static inline void
215 draw_parser_data_init(draw_parser_data_t *pdata)
217 p_clear(pdata, 1);
220 static inline void
221 draw_parser_data_wipe(draw_parser_data_t *pdata)
223 if(pdata)
225 pango_attr_list_unref(pdata->attr_list);
226 p_delete(&pdata->text);
227 image_unref(&pdata->bg_image);
231 #endif
232 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80