all repos — dwm @ 0e5c8198bc5a69e87b0114b81d6569188828edfa

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
  6#include <stdio.h>
  7#include <string.h>
  8
  9#include <X11/Xlocale.h>
 10
 11#include "dwm.h"
 12
 13static void
 14drawborder(void)
 15{
 16	XPoint points[5];
 17	XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
 18	XSetForeground(dpy, dc.gc, dc.border);
 19	points[0].x = dc.x;
 20	points[0].y = dc.y;
 21	points[1].x = dc.w - 1;
 22	points[1].y = 0;
 23	points[2].x = 0;
 24	points[2].y = dc.h - 1;
 25	points[3].x = -(dc.w - 1);
 26	points[3].y = 0;
 27	points[4].x = 0;
 28	points[4].y = -(dc.h - 1);
 29	XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
 30}
 31
 32void
 33drawtext(const char *text, Bool border)
 34{
 35	int x, y, w, h;
 36	unsigned int len;
 37	static char buf[256];
 38	XGCValues gcv;
 39	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
 40
 41	XSetForeground(dpy, dc.gc, dc.bg);
 42	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
 43
 44	w = 0;
 45	if(border)
 46		drawborder();
 47
 48	if(!text)
 49		return;
 50
 51	len = strlen(text);
 52	if(len >= sizeof(buf))
 53		len = sizeof(buf) - 1;
 54	memcpy(buf, text, len);
 55	buf[len] = 0;
 56
 57	h = dc.font.ascent + dc.font.descent;
 58	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
 59	x = dc.x + (h / 2);
 60
 61	/* shorten text if necessary */
 62	while(len && (w = textnw(buf, len)) > dc.w - h)
 63		buf[--len] = 0;
 64
 65	if(w > dc.w)
 66		return; /* too long */
 67
 68	gcv.foreground = dc.fg;
 69	gcv.background = dc.bg;
 70	if(dc.font.set) {
 71		XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
 72		XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
 73				x, y, buf, len);
 74	}
 75	else {
 76		gcv.font = dc.font.xfont->fid;
 77		XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
 78		XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
 79	}
 80}
 81
 82unsigned long
 83initcolor(const char *colstr)
 84{
 85	XColor color;
 86	Colormap cmap = DefaultColormap(dpy, screen);
 87
 88	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
 89	return color.pixel;
 90}
 91
 92unsigned int
 93textnw(char *text, unsigned int len)
 94{
 95	XRectangle r;
 96	if(dc.font.set) {
 97		XmbTextExtents(dc.font.set, text, len, NULL, &r);
 98		return r.width;
 99	}
100	return XTextWidth(dc.font.xfont, text, len);
101}
102
103unsigned int
104textw(char *text)
105{
106	return textnw(text, strlen(text));
107}
108
109void
110initfont(const char *fontstr)
111{
112	char **missing, *def;
113	int i, n;
114
115	missing = NULL;
116	setlocale(LC_ALL, "");
117	if(dc.font.set)
118		XFreeFontSet(dpy, dc.font.set);
119	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
120	if(missing) {
121		while(n--)
122			fprintf(stderr, "missing fontset: %s\n", missing[n]);
123		XFreeStringList(missing);
124		if(dc.font.set) {
125			XFreeFontSet(dpy, dc.font.set);
126			dc.font.set = NULL;
127		}
128	}
129	if(dc.font.set) {
130		XFontSetExtents *font_extents;
131		XFontStruct **xfonts;
132		char **font_names;
133
134		dc.font.ascent = dc.font.descent = 0;
135		font_extents = XExtentsOfFontSet(dc.font.set);
136		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
137		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
138			if(dc.font.ascent < (*xfonts)->ascent)
139				dc.font.ascent = (*xfonts)->ascent;
140			if(dc.font.descent < (*xfonts)->descent)
141				dc.font.descent = (*xfonts)->descent;
142			xfonts++;
143		}
144	}
145	else {
146		if(dc.font.xfont)
147			XFreeFont(dpy, dc.font.xfont);
148		dc.font.xfont = NULL;
149		dc.font.xfont = XLoadQueryFont(dpy, fontstr);
150		if (!dc.font.xfont)
151			dc.font.xfont = XLoadQueryFont(dpy, "fixed");
152		if (!dc.font.xfont)
153			error("error, cannot init 'fixed' font\n");
154		dc.font.ascent = dc.font.xfont->ascent;
155		dc.font.descent = dc.font.xfont->descent;
156	}
157	dc.font.height = dc.font.ascent + dc.font.descent;
158}