all repos — dwm @ 8b59083eb13c0712e04d9a5b6d7bf4af5913c442

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