all repos — dwm @ 30561a0161629b15abe1beeb97eec3fc0768efaf

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, Bool invert)
 26{
 27	int x, y, w, h;
 28	static char buf[256];
 29	unsigned int len;
 30	XGCValues gcv;
 31	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
 32
 33	XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
 34	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
 35
 36	if(!text)
 37		return;
 38
 39	w = 0;
 40	len = strlen(text);
 41	if(len >= sizeof(buf))
 42		len = sizeof(buf) - 1;
 43	memcpy(buf, text, len);
 44	buf[len] = 0;
 45
 46	h = dc.font.ascent + dc.font.descent;
 47	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
 48	x = dc.x + (h / 2);
 49
 50	/* shorten text if necessary */
 51	while(len && (w = textnw(buf, len)) > dc.w - h)
 52		buf[--len] = 0;
 53
 54	if(w > dc.w)
 55		return; /* too long */
 56
 57	gcv.foreground = invert ? dc.bg : dc.fg;
 58	gcv.background = invert ? dc.fg : dc.bg;
 59	if(dc.font.set) {
 60		XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
 61		XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
 62				x, y, buf, len);
 63	}
 64	else {
 65		gcv.font = dc.font.xfont->fid;
 66		XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
 67		XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
 68	}
 69}
 70
 71/* extern */
 72
 73void
 74drawall()
 75{
 76	Client *c;
 77
 78	for(c = clients; c; c = getnext(c->next))
 79		drawtitle(c);
 80	drawstatus();
 81}
 82
 83void
 84drawstatus()
 85{
 86	int i, x;
 87	Bool istile = arrange == dotile;
 88
 89	dc.x = dc.y = 0;
 90	dc.w = bw;
 91	drawtext(NULL, !istile);
 92
 93	dc.w = 0;
 94	for(i = 0; i < ntags; i++) {
 95		dc.x += dc.w;
 96		dc.w = textw(tags[i]);
 97		if(istile)
 98			drawtext(tags[i], (i == tsel));
 99		else
100			drawtext(tags[i], (i != tsel));
101	}
102	x = dc.x + dc.w;
103	dc.w = textw(stext);
104	dc.x = bx + bw - dc.w;
105	drawtext(stext, !istile);
106	if(sel && ((dc.w = dc.x - x) >= bh)) {
107		dc.x = x;
108		drawtext(sel->name, istile);
109	}
110	XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
111	XSync(dpy, False);
112}
113
114void
115drawtitle(Client *c)
116{
117	int i;
118	Bool istile = arrange == dotile;
119
120	if(c == sel) {
121		drawstatus();
122		XUnmapWindow(dpy, c->title);
123		XSetWindowBorder(dpy, c->win, dc.fg);
124		return;
125	}
126
127	XSetWindowBorder(dpy, c->win, dc.bg);
128	XMapWindow(dpy, c->title);
129
130	dc.x = dc.y = 0;
131
132	dc.w = 0;
133	for(i = 0; i < ntags; i++) {
134		if(c->tags[i]) {
135			dc.x += dc.w;
136			dc.w = textw(tags[i]);
137			drawtext(tags[i], !istile);
138		}
139	}
140	dc.x += dc.w;
141	dc.w = textw(c->name);
142	drawtext(c->name, !istile);
143	XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
144	XSync(dpy, False);
145}
146
147unsigned long
148getcolor(const char *colstr)
149{
150	Colormap cmap = DefaultColormap(dpy, screen);
151	XColor color;
152
153	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
154	return color.pixel;
155}
156
157void
158setfont(const char *fontstr)
159{
160	char **missing, *def;
161	int i, n;
162
163	missing = NULL;
164	setlocale(LC_ALL, "");
165	if(dc.font.set)
166		XFreeFontSet(dpy, dc.font.set);
167	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
168	if(missing) {
169		while(n--)
170			fprintf(stderr, "missing fontset: %s\n", missing[n]);
171		XFreeStringList(missing);
172		if(dc.font.set) {
173			XFreeFontSet(dpy, dc.font.set);
174			dc.font.set = NULL;
175		}
176	}
177	if(dc.font.set) {
178		XFontSetExtents *font_extents;
179		XFontStruct **xfonts;
180		char **font_names;
181
182		dc.font.ascent = dc.font.descent = 0;
183		font_extents = XExtentsOfFontSet(dc.font.set);
184		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
185		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
186			if(dc.font.ascent < (*xfonts)->ascent)
187				dc.font.ascent = (*xfonts)->ascent;
188			if(dc.font.descent < (*xfonts)->descent)
189				dc.font.descent = (*xfonts)->descent;
190			xfonts++;
191		}
192	}
193	else {
194		if(dc.font.xfont)
195			XFreeFont(dpy, dc.font.xfont);
196		dc.font.xfont = NULL;
197		dc.font.xfont = XLoadQueryFont(dpy, fontstr);
198		if (!dc.font.xfont)
199			dc.font.xfont = XLoadQueryFont(dpy, "fixed");
200		if (!dc.font.xfont)
201			eprint("error, cannot init 'fixed' font\n");
202		dc.font.ascent = dc.font.xfont->ascent;
203		dc.font.descent = dc.font.xfont->descent;
204	}
205	dc.font.height = dc.font.ascent + dc.font.descent;
206}
207
208unsigned int
209textw(const char *text)
210{
211	return textnw(text, strlen(text)) + dc.font.height;
212}