all repos — dwm @ 1.1

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