all repos — dwm @ 0045ad87dfb32f35fc17b5b8942049cfe84d623c

fork of suckless dynamic window manager

draw.c (view raw)

  1/* (C)opyright MMIV-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2 * See LICENSE file for license details.
  3 */
  4#include "dwm.h"
  5#include <stdio.h>
  6#include <string.h>
  7
  8/* static */
  9
 10static Bool
 11isoccupied(unsigned int t)
 12{
 13	Client *c;
 14	for(c = clients; c; c = c->next)
 15		if(c->tags[t])
 16			return True;
 17	return False;
 18}
 19
 20static unsigned int
 21textnw(const char *text, unsigned int len) {
 22	XRectangle r;
 23
 24	if(dc.font.set) {
 25		XmbTextExtents(dc.font.set, text, len, NULL, &r);
 26		return r.width;
 27	}
 28	return XTextWidth(dc.font.xfont, text, len);
 29}
 30
 31static void
 32drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
 33	int x, y, w, h;
 34	static char buf[256];
 35	unsigned int len, olen;
 36	XGCValues gcv;
 37	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
 38	XPoint pt[5];
 39
 40	XSetForeground(dpy, dc.gc, col[ColBG]);
 41	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
 42	if(!text)
 43		return;
 44	w = 0;
 45	olen = len = strlen(text);
 46	if(len >= sizeof buf)
 47		len = sizeof buf - 1;
 48	memcpy(buf, text, len);
 49	buf[len] = 0;
 50	h = dc.font.ascent + dc.font.descent;
 51	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
 52	x = dc.x + (h / 2);
 53	/* shorten text if necessary */
 54	while(len && (w = textnw(buf, len)) > dc.w - h)
 55		buf[--len] = 0;
 56	if(len < olen) {
 57		if(len > 1)
 58			buf[len - 1] = '.';
 59		if(len > 2)
 60			buf[len - 2] = '.';
 61		if(len > 3)
 62			buf[len - 3] = '.';
 63	}
 64	if(w > dc.w)
 65		return; /* too long */
 66	gcv.foreground = col[ColFG];
 67	if(dc.font.set) {
 68		XChangeGC(dpy, dc.gc, GCForeground, &gcv);
 69		XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
 70	}
 71	else {
 72		gcv.font = dc.font.xfont->fid;
 73		XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
 74		XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
 75	}
 76	x = (h + 2) / 4;
 77	if(filledsquare) {
 78		r.x = dc.x + 1;
 79		r.y = dc.y + 1;
 80		r.width = r.height = x + 1;
 81		XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
 82	}
 83	else if(emptysquare) {
 84		pt[0].x = dc.x + 1;
 85		pt[0].y = dc.y + 1;
 86		pt[1].x = x;
 87		pt[1].y = 0;
 88		pt[2].x = 0;
 89		pt[2].y = x;
 90		pt[3].x = -x;
 91		pt[3].y = 0;
 92		pt[4].x = 0;
 93		pt[4].y = -x;
 94		XDrawLines(dpy, dc.drawable, dc.gc, pt, 5, CoordModePrevious);
 95	}
 96}
 97
 98/* extern */
 99
100void
101drawall(void) {
102	Client *c;
103
104	for(c = clients; c; c = getnext(c->next))
105		drawclient(c);
106	drawstatus();
107}
108
109void
110drawstatus(void) {
111	int i, x;
112
113	dc.x = dc.y = 0;
114	for(i = 0; i < ntags; i++) {
115		dc.w = textw(tags[i]);
116		if(seltag[i])
117			drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
118		else
119			drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
120		dc.x += dc.w;
121	}
122	dc.w = bmw;
123	drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False);
124	x = dc.x + dc.w;
125	dc.w = textw(stext);
126	dc.x = bw - dc.w;
127	if(dc.x < x) {
128		dc.x = x;
129		dc.w = bw - x;
130	}
131	drawtext(stext, dc.norm, False, False);
132	if((dc.w = dc.x - x) > bh) {
133		dc.x = x;
134		drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
135	}
136	XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
137	XSync(dpy, False);
138}
139
140void
141drawclient(Client *c) {
142	if(c == sel && issel) {
143		drawstatus();
144		XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
145		return;
146	}
147	XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
148	XSync(dpy, False);
149}
150
151unsigned long
152getcolor(const char *colstr) {
153	Colormap cmap = DefaultColormap(dpy, screen);
154	XColor color;
155
156	if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
157		eprint("error, cannot allocate color '%s'\n", colstr);
158	return color.pixel;
159}
160
161void
162setfont(const char *fontstr) {
163	char *def, **missing;
164	int i, n;
165
166	missing = NULL;
167	if(dc.font.set)
168		XFreeFontSet(dpy, dc.font.set);
169	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
170	if(missing) {
171		while(n--)
172			fprintf(stderr, "missing fontset: %s\n", missing[n]);
173		XFreeStringList(missing);
174	}
175	if(dc.font.set) {
176		XFontSetExtents *font_extents;
177		XFontStruct **xfonts;
178		char **font_names;
179		dc.font.ascent = dc.font.descent = 0;
180		font_extents = XExtentsOfFontSet(dc.font.set);
181		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
182		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
183			if(dc.font.ascent < (*xfonts)->ascent)
184				dc.font.ascent = (*xfonts)->ascent;
185			if(dc.font.descent < (*xfonts)->descent)
186				dc.font.descent = (*xfonts)->descent;
187			xfonts++;
188		}
189	}
190	else {
191		if(dc.font.xfont)
192			XFreeFont(dpy, dc.font.xfont);
193		dc.font.xfont = NULL;
194		if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
195			eprint("error, cannot load font: '%s'\n", fontstr);
196		dc.font.ascent = dc.font.xfont->ascent;
197		dc.font.descent = dc.font.xfont->descent;
198	}
199	dc.font.height = dc.font.ascent + dc.font.descent;
200}
201
202unsigned int
203textw(const char *text) {
204	return textnw(text, strlen(text)) + dc.font.height;
205}