1 /* See LICENSE file for copyright and license details. */
4 enum { ColFG, ColBG, ColLast };
9 unsigned long norm[ColLast];
10 unsigned long sel[ColLast];
20 } DC; /* draw context */
22 /* forward declarations */
23 static void dccleanup(void);
24 static void dcsetup(void);
25 static void drawtext(const char *text, unsigned long col[ColLast]);
26 static unsigned long getcolor(const char *colstr);
27 static void initfont(const char *fontstr);
28 static int textnw(const char *text, unsigned int len);
29 static int textw(const char *text);
36 XFreeFontSet(dpy, dc.font.set);
38 XFreeFont(dpy, dc.font.xfont);
39 XFreePixmap(dpy, dc.drawable);
46 dc.norm[ColBG] = getcolor(normbgcolor);
47 dc.norm[ColFG] = getcolor(normfgcolor);
48 dc.sel[ColBG] = getcolor(selbgcolor);
49 dc.sel[ColFG] = getcolor(selfgcolor);
53 dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
54 dc.gc = XCreateGC(dpy, parent, 0, NULL);
55 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
57 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
61 drawtext(const char *text, unsigned long col[ColLast]) {
63 int i, x, y, h, len, olen;
64 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
66 XSetForeground(dpy, dc.gc, col[ColBG]);
67 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
72 y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent;
74 /* shorten text if necessary */
75 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
78 memcpy(buf, text, len);
80 for(i = len; i && i > len - 3; buf[--i] = '.');
81 XSetForeground(dpy, dc.gc, col[ColFG]);
83 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
85 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
89 getcolor(const char *colstr) {
90 Colormap cmap = DefaultColormap(dpy, screen);
93 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
94 eprint("drawtext: cannot allocate color '%s'\n", colstr);
99 initfont(const char *fontstr) {
100 char *def, **missing = NULL;
103 if(!fontstr || fontstr[0] == '\0')
104 eprint("drawtext: cannot load font: '%s'\n", fontstr);
105 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
107 XFreeStringList(missing);
109 XFontStruct **xfonts;
111 dc.font.ascent = dc.font.descent = 0;
112 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
113 for(i = 0; i < n; i++) {
114 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
115 dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
120 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
121 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
122 eprint("drawtext: cannot load font: '%s'\n", fontstr);
123 dc.font.ascent = dc.font.xfont->ascent;
124 dc.font.descent = dc.font.xfont->descent;
126 dc.font.height = dc.font.ascent + dc.font.descent;
130 textnw(const char *text, unsigned int len) {
134 XmbTextExtents(dc.font.set, text, len, NULL, &r);
137 return XTextWidth(dc.font.xfont, text, len);
141 textw(const char *text) {
142 return textnw(text, strlen(text)) + dc.font.height;