image: Remove some code duplication
[awesome.git] / draw.h
blobea74a9edb669bd7a9870fa1fd4009491da44c930
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 "color.h"
32 #include "common/array.h"
34 /** Padding type */
35 typedef struct
37 /** Padding at top */
38 int top;
39 /** Padding at bottom */
40 int bottom;
41 /** Padding at left */
42 int left;
43 /** Padding at right */
44 int right;
45 } padding_t;
47 typedef enum
49 AlignLeft = (0),
50 AlignRight = (1),
51 AlignCenter = (1 << 1),
52 AlignTop = (1 << 2),
53 AlignBottom = (1 << 3),
54 AlignFlex = (1 << 4),
55 AlignFixed = (1 << 5),
56 AlignMiddle = (1 << 6)
57 } alignment_t;
59 typedef struct vector_t vector_t;
60 struct vector_t
62 /** Co-ords of starting point */
63 int16_t x;
64 int16_t y;
65 /** Offset to starting point */
66 int16_t x_offset;
67 int16_t y_offset;
70 typedef struct area_t area_t;
71 struct area_t
73 /** Co-ords of upper left corner */
74 int16_t x;
75 int16_t y;
76 uint16_t width;
77 uint16_t height;
80 #define AREA_LEFT(a) ((a).x)
81 #define AREA_TOP(a) ((a).y)
82 #define AREA_RIGHT(a) ((a).x + (a).width)
83 #define AREA_BOTTOM(a) ((a).y + (a).height)
85 typedef struct
87 PangoFontDescription *desc;
88 int height;
89 } font_t;
91 typedef struct
93 xcb_pixmap_t pixmap;
94 xcb_visualtype_t *visual;
95 uint16_t width;
96 uint16_t height;
97 int phys_screen;
98 uint8_t depth;
99 cairo_t *cr;
100 cairo_surface_t *surface;
101 PangoLayout *layout;
102 xcolor_t fg;
103 xcolor_t bg;
104 } draw_context_t;
106 void draw_context_init(draw_context_t *, int, int, int,
107 xcb_pixmap_t, const xcolor_t *, const xcolor_t *);
109 /** Wipe a draw context.
110 * \param ctx The draw_context_t to wipe.
112 static inline void
113 draw_context_wipe(draw_context_t *ctx)
115 if(ctx->layout)
117 g_object_unref(ctx->layout);
118 ctx->layout = NULL;
120 if(ctx->surface)
122 cairo_surface_destroy(ctx->surface);
123 ctx->surface = NULL;
125 if(ctx->cr)
127 cairo_destroy(ctx->cr);
128 ctx->cr = NULL;
132 font_t *draw_font_new(const char *);
133 void draw_font_delete(font_t **);
135 bool draw_iso2utf8(const char *, size_t, char **, ssize_t *);
137 /** Convert a string to UTF-8.
138 * \param str The string to convert.
139 * \param len The string length.
140 * \param dest The destination string that will be allocated.
141 * \param dlen The destination string length allocated, can be NULL.
142 * \return True if the conversion happened, false otherwise. In both case, dest
143 * and dlen will have value and dest have to be free().
145 static inline bool
146 a_iso2utf8(const char *str, ssize_t len, char **dest, ssize_t *dlen)
148 if(draw_iso2utf8(str, len, dest, dlen))
149 return true;
151 *dest = a_strdup(str);
152 if(dlen)
153 *dlen = len;
155 return false;
158 typedef struct
160 PangoAttrList *attr_list;
161 char *text;
162 ssize_t len;
163 } draw_text_context_t;
165 bool draw_text_context_init(draw_text_context_t *, const char *, ssize_t);
166 void draw_text(draw_context_t *, draw_text_context_t *, PangoEllipsizeMode, PangoWrapMode, alignment_t, padding_t *, area_t, area_t *);
167 void draw_rectangle(draw_context_t *, area_t, float, bool, const color_t *);
168 void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t,
169 const color_t *, const color_t *, const color_t *);
171 void draw_graph_setup(draw_context_t *);
172 void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, vector_t,
173 const color_t *, const color_t *, const color_t *);
174 void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, vector_t,
175 const color_t *, const color_t *, const color_t *);
176 void draw_image(draw_context_t *, int, int, double, image_t *);
177 void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int);
178 area_t draw_text_extents(draw_text_context_t *);
179 alignment_t draw_align_fromstr(const char *, ssize_t);
180 const char *draw_align_tostr(alignment_t);
182 static inline void
183 draw_text_context_wipe(draw_text_context_t *pdata)
185 if(pdata)
187 if(pdata->attr_list)
188 pango_attr_list_unref(pdata->attr_list);
189 p_delete(&pdata->text);
193 #endif
194 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80