1 /* See LICENSE file for copyright and license details. */
11 draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
12 Draw *draw = (Draw *)calloc(1, sizeof(Draw));
14 draw->screen = screen;
18 draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
19 draw->gc = XCreateGC(dpy, win, 0, NULL);
20 XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter);
25 draw_resize(Draw *draw, unsigned int w, unsigned int h) {
30 XFreePixmap(draw->dpy, draw->drawable);
31 draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
35 draw_free(Draw *draw) {
36 XFreePixmap(draw->dpy, draw->drawable);
37 XFreeGC(draw->dpy, draw->gc);
42 draw_font_create(Draw *draw, const char *fontname) {
49 font = (Fnt *)calloc(1, sizeof(Fnt));
50 font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
53 fprintf(stderr, "draw: missing fontset: %s\n", missing[n]);
54 XFreeStringList(missing);
59 XExtentsOfFontSet(font->set);
60 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
62 font->ascent = MAX(font->ascent, (*xfonts)->ascent);
63 font->descent = MAX(font->descent,(*xfonts)->descent);
68 if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname))
69 && !(font->xfont = XLoadQueryFont(draw->dpy, "fixed")))
70 die("error, cannot load font: '%s'\n", fontname);
71 font->ascent = font->xfont->ascent;
72 font->descent = font->xfont->descent;
74 font->h = font->ascent + font->descent;
79 draw_font_free(Draw *draw, Fnt *font) {
83 XFreeFontSet(draw->dpy, font->set);
85 XFreeFont(draw->dpy, font->xfont);
90 draw_col_create(Draw *draw, const char *colname) {
91 Col *col = (Col *)calloc(1, sizeof(Col));
92 Colormap cmap = DefaultColormap(draw->dpy, draw->screen);
95 if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color))
96 die("error, cannot allocate color '%s'\n", colname);
97 col->rgb = color.pixel;
102 draw_col_free(Draw *draw, Col *col) {
109 draw_setfont(Draw *draw, Fnt *font) {
116 draw_setfg(Draw *draw, Col *col) {
123 draw_setbg(Draw *draw, Col *col) {
130 draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
133 if(!draw || !draw->font || !draw->fg || !draw->bg)
135 XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
136 dx = (draw->font->ascent + draw->font->descent + 2) / 4;
138 XFillRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx+1, dx+1);
140 XDrawRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx, dx);
144 draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
146 int i, tx, ty, len, olen;
149 if(!draw || !draw->fg || !draw->bg)
151 XSetForeground(draw->dpy, draw->gc, invert ? draw->fg->rgb : draw->bg->rgb);
152 XFillRectangle(draw->dpy, draw->drawable, draw->gc, x, y, w, h);
153 if(!text || !draw->font)
156 draw_getextents(draw, text, olen, &tex);
157 ty = y + (h / 2) - tex.yOff;
159 /* shorten text if necessary */
160 for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
161 draw_getextents(draw, text, len, &tex);
164 memcpy(buf, text, len);
166 for(i = len; i && i > len - 3; buf[--i] = '.');
167 XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
169 XmbDrawString(draw->dpy, draw->drawable, draw->font->set, draw->gc, tx, ty, buf, len);
171 XDrawString(draw->dpy, draw->drawable, draw->gc, tx, ty, buf, len);
175 draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
178 XCopyArea(draw->dpy, draw->drawable, draw->win, draw->gc, x, y, w, h, x, y);
179 XSync(draw->dpy, False);
184 draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents) {
187 if(!draw || !draw->font || !text)
189 if(draw->font->set) {
190 XmbTextExtents(draw->font->set, text, len, NULL, &r);
193 extents->w = r.width;
194 extents->h = r.height;
197 extents->h = draw->font->ascent + draw->font->descent;
198 extents->w = XTextWidth(draw->font->xfont, text, len);
199 extents->xOff = extents->h / 2;
200 extents->yOff = (extents->h / 2) + draw->font->ascent;