all repos — dwm @ 2e0c767d74da024c3cd4dbd524e1364039704451

fork of suckless dynamic window manager

draw.c (view raw)

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