all repos — dwm @ aafeaf731701e528f4ae9d7c7432b3e92fee4392

fork of suckless dynamic window manager

draw.c (view raw)

  1/* See LICENSE file for copyright and license details. */
  2#include <stdio.h>
  3#include <stdlib.h>
  4#include <string.h>
  5#include <X11/Xlib.h>
  6
  7#include "draw.h"
  8#include "util.h"
  9
 10Draw *
 11draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
 12	Draw *draw = (Draw *)calloc(1, sizeof(Draw));
 13	draw->dpy = dpy;
 14	draw->screen = screen;
 15	draw->win = win;
 16	draw->w = w;
 17	draw->h = h;
 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);
 21	return draw;
 22}
 23
 24void
 25draw_resize(Draw *draw, unsigned int w, unsigned int h) {
 26	if(!draw)
 27		return;
 28	draw->w = w;
 29	draw->h = h;
 30	XFreePixmap(draw->dpy, draw->drawable);
 31	draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
 32}
 33
 34void
 35draw_free(Draw *draw) {
 36	XFreePixmap(draw->dpy, draw->drawable);
 37	XFreeGC(draw->dpy, draw->gc);
 38	free(draw);
 39}
 40
 41Fnt *
 42draw_font_create(Draw *draw, const char *fontname) {
 43	Fnt *font;
 44	char *def, **missing;
 45	int n;
 46
 47	if(!draw)
 48		return NULL;
 49	font = (Fnt *)calloc(1, sizeof(Fnt));
 50	font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
 51	if(missing) {
 52		while(n--)
 53			fprintf(stderr, "draw: missing fontset: %s\n", missing[n]);
 54		XFreeStringList(missing);
 55	}
 56	if(font->set) {
 57		XFontStruct **xfonts;
 58		char **font_names;
 59		XExtentsOfFontSet(font->set);
 60		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
 61		while(n--) {
 62			font->ascent = MAX(font->ascent, (*xfonts)->ascent);
 63			font->descent = MAX(font->descent,(*xfonts)->descent);
 64			xfonts++;
 65		}
 66	}
 67	else {
 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;
 73	}
 74	font->h = font->ascent + font->descent;
 75	return font;
 76}
 77
 78void
 79draw_font_free(Draw *draw, Fnt *font) {
 80	if(!draw || !font)
 81		return;
 82	if(font->set)
 83		XFreeFontSet(draw->dpy, font->set);
 84	else
 85		XFreeFont(draw->dpy, font->xfont);
 86	free(font);
 87}
 88
 89Col *
 90draw_col_create(Draw *draw, const char *colname) {
 91	Col *col = (Col *)calloc(1, sizeof(Col));
 92	Colormap cmap = DefaultColormap(draw->dpy, draw->screen);
 93	XColor color;
 94
 95	if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color))
 96		die("error, cannot allocate color '%s'\n", colname);
 97	col->rgb = color.pixel;
 98	return col;
 99}
100
101void
102draw_col_free(Draw *draw, Col *col) {
103	if(!col)
104		return;
105	free(col);
106}
107
108void
109draw_setfont(Draw *draw, Fnt *font) {
110	if(!draw)
111		return;
112	draw->font = font;
113}
114
115void
116draw_setfg(Draw *draw, Col *col) {
117	if(!draw) 
118		return;
119	draw->fg = col;
120}
121
122void
123draw_setbg(Draw *draw, Col *col) {
124	if(!draw)
125		return;
126	draw->bg = col;
127}
128
129void
130draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
131	int dx;
132
133	if(!draw || !draw->font || !draw->fg || !draw->bg)
134		return;
135	XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
136	dx = (draw->font->ascent + draw->font->descent + 2) / 4;
137	if(filled)
138		XFillRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx+1, dx+1);
139	else if(empty)
140		XDrawRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx, dx);
141}
142
143void
144draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
145	char buf[256];
146	int i, tx, ty, len, olen;
147	TextExtents tex;
148
149	if(!draw || !draw->fg || !draw->bg)
150		return;
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)
154		return;
155	olen = strlen(text);
156	draw_getextents(draw, text, olen, &tex);
157	ty = y + (h / 2) - tex.yOff;
158	tx = x + tex.xOff;
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);
162	if(!len)
163		return;
164	memcpy(buf, text, len);
165	if(len < olen)
166		for(i = len; i && i > len - 3; buf[--i] = '.');
167	XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
168	if(draw->font->set)
169		XmbDrawString(draw->dpy, draw->drawable, draw->font->set, draw->gc, tx, ty, buf, len);
170	else
171		XDrawString(draw->dpy, draw->drawable, draw->gc, tx, ty, buf, len);
172}
173
174void
175draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
176	if(!draw)
177		return;
178	XCopyArea(draw->dpy, draw->drawable, draw->win, draw->gc, x, y, w, h, x, y);
179	XSync(draw->dpy, False);
180}
181
182
183void
184draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents) {
185	XRectangle r;
186
187	if(!draw || !draw->font || !text)
188		return;
189	if(draw->font->set) {
190		XmbTextExtents(draw->font->set, text, len, NULL, &r);
191		extents->xOff = r.x;
192		extents->yOff = r.y;
193		extents->w = r.width;
194		extents->h = r.height;
195	}
196	else {
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;
201	}
202}