small fix for maximize() in floating layout
[awesome.git] / draw.c
blob61c4d71cfda63276deaa344c03e14d05ede54fe9
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007 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.h>
23 #include <cairo-ft.h>
24 #include <cairo-xlib.h>
25 #include <math.h>
26 #include "layout.h"
27 #include "util.h"
28 #include "draw.h"
30 void
31 drawtext(Display *disp, int screen, int x, int y, int w, int h, Drawable drawable, int dw, int dh, XftFont *font, const char *text, XColor color[ColLast])
33 int nw = 0;
34 static char buf[256];
35 size_t len, olen;
36 cairo_font_face_t *font_face;
37 cairo_surface_t *surface;
38 cairo_t *cr;
40 drawrectangle(disp, screen, x, y, w, h, drawable, dw, dh, True, color[ColBG]);
41 if(!a_strlen(text))
42 return;
44 surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
45 cr = cairo_create(surface);
46 font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
47 cairo_set_font_face(cr, font_face);
48 cairo_set_font_size(cr, font->height);
49 cairo_set_source_rgb(cr, color[ColFG].red / 65535.0, color[ColFG].green / 65535.0, color[ColFG].blue / 65535.0);
51 olen = len = a_strlen(text);
52 if(len >= sizeof(buf))
53 len = sizeof(buf) - 1;
54 memcpy(buf, text, len);
55 buf[len] = 0;
56 while(len && (nw = textwidth(disp, font, buf)) > w)
57 buf[--len] = 0;
58 if(nw > w)
59 return; /* too long */
60 if(len < olen)
62 if(len > 1)
63 buf[len - 1] = '.';
64 if(len > 2)
65 buf[len - 2] = '.';
66 if(len > 3)
67 buf[len - 3] = '.';
70 cairo_move_to(cr, x + font->height / 2, y + font->ascent + (dh - font->height) / 2);
71 cairo_show_text(cr, buf);
73 cairo_font_face_destroy(font_face);
74 cairo_destroy(cr);
75 cairo_surface_destroy(surface);
78 void
79 drawrectangle(Display *disp, int screen, int x, int y, int w, int h, Drawable drawable, int dw, int dh, Bool filled, XColor color)
81 cairo_surface_t *surface;
82 cairo_t *cr;
84 surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
85 cr = cairo_create (surface);
87 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
88 cairo_set_line_width(cr, 1.0);
89 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
90 if(filled)
92 cairo_rectangle(cr, x, y, w + 1, h + 1);
93 cairo_fill(cr);
95 else
96 cairo_rectangle(cr, x + 1, y, w, h);
98 cairo_stroke(cr);
100 cairo_destroy(cr);
101 cairo_surface_destroy(surface);
104 void
105 drawcircle(Display *disp, int screen, int x, int y, int r, Drawable drawable, int dw, int dh, Bool filled, XColor color)
107 cairo_surface_t *surface;
108 cairo_t *cr;
110 surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
111 cr = cairo_create (surface);
112 cairo_set_line_width(cr, 1.0);
113 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
114 if(filled)
116 cairo_arc (cr, x + r, y + r, r, 0, 2 * M_PI);
117 cairo_fill(cr);
119 else
120 cairo_arc (cr, x + r, y + r, r - 1, 0, 2 * M_PI);
122 cairo_stroke(cr);
124 cairo_destroy(cr);
125 cairo_surface_destroy(surface);
128 Drawable
129 draw_rotate(Display *disp, int screen, Drawable drawable, int dw, int dh, double angle, int tx, int ty)
131 cairo_surface_t *surface, *source;
132 cairo_t *cr;
133 Drawable newdrawable;
135 newdrawable = XCreatePixmap(disp,
136 RootWindow(disp, screen),
137 dh, dw,
138 DefaultDepth(disp, screen));
139 surface = cairo_xlib_surface_create(disp, newdrawable, DefaultVisual(disp, screen), dh, dw);
140 source = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
141 cr = cairo_create (surface);
143 cairo_translate(cr, tx, ty);
144 cairo_rotate(cr, angle);
146 cairo_set_source_surface(cr, source, 0.0, 0.0);
147 cairo_paint(cr);
149 cairo_destroy(cr);
150 cairo_surface_destroy(source);
151 cairo_surface_destroy(surface);
153 return newdrawable;
156 unsigned short
157 textwidth(Display *disp, XftFont *font, char *text)
159 cairo_surface_t *surface;
160 cairo_t *cr;
161 cairo_font_face_t *font_face;
162 cairo_text_extents_t te;
164 surface = cairo_xlib_surface_create(disp, DefaultScreen(disp),
165 DefaultVisual(disp, DefaultScreen(disp)),
166 DisplayWidth(disp, DefaultScreen(disp)),
167 DisplayHeight(disp, DefaultScreen(disp)));
168 cr = cairo_create(surface);
169 font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
170 cairo_set_font_face(cr, font_face);
171 cairo_set_font_size(cr, font->height);
172 cairo_text_extents(cr, text, &te);
173 cairo_destroy(cr);
174 cairo_surface_destroy(surface);
175 cairo_font_face_destroy(font_face);
177 return MAX(te.x_advance, te.width) + font->height;
180 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99