2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
8 #include <X11/Xlocale.h>
13 textnw(const char *text, unsigned int len)
18 XmbTextExtents(dc.font.set, text, len, NULL, &r);
21 return XTextWidth(dc.font.xfont, text, len);
27 drawtext(const char *text, unsigned int colidx, Bool border)
31 unsigned int len, olen;
34 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
36 XSetForeground(dpy, dc.gc, dc.bg[colidx]);
37 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
40 XSetForeground(dpy, dc.gc, dc.fg[colidx]);
44 points[1].x = dc.w - 1;
47 points[2].y = dc.h - 1;
48 points[3].x = -(dc.w - 1);
51 points[4].y = -(dc.h - 1);
52 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
58 olen = len = strlen(text);
59 if(len >= sizeof(buf))
60 len = sizeof(buf) - 1;
61 memcpy(buf, text, len);
64 h = dc.font.ascent + dc.font.descent;
65 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
68 /* shorten text if necessary */
69 while(len && (w = textnw(buf, len)) > dc.w - h)
81 return; /* too long */
84 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
86 gcv.font = dc.font.xfont->fid;
87 XChangeGC(dpy, dc.gc, GCFont, &gcv);
88 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
93 getcolor(const char *colstr)
95 Colormap cmap = DefaultColormap(dpy, screen);
98 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
103 setfont(const char *fontstr)
105 char **missing, *def;
109 setlocale(LC_ALL, "");
111 XFreeFontSet(dpy, dc.font.set);
112 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
114 XFreeStringList(missing);
116 XFreeFontSet(dpy, dc.font.set);
121 XFontSetExtents *font_extents;
122 XFontStruct **xfonts;
125 dc.font.ascent = dc.font.descent = 0;
126 font_extents = XExtentsOfFontSet(dc.font.set);
127 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
128 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
129 if(dc.font.ascent < (*xfonts)->ascent)
130 dc.font.ascent = (*xfonts)->ascent;
131 if(dc.font.descent < (*xfonts)->descent)
132 dc.font.descent = (*xfonts)->descent;
138 XFreeFont(dpy, dc.font.xfont);
139 dc.font.xfont = NULL;
140 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
142 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
144 eprint("error, cannot init 'fixed' font\n");
145 dc.font.ascent = dc.font.xfont->ascent;
146 dc.font.descent = dc.font.xfont->descent;
148 dc.font.height = dc.font.ascent + dc.font.descent;
152 textw(const char *text)
154 return textnw(text, strlen(text)) + dc.font.height;