all repos — dwm @ 0f3acce0429bc5d2906eaf9279abc5565b189d33

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