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, Bool invert, Bool border)
34 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
36 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
37 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
41 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
42 XSetForeground(dpy, dc.gc, dc.border);
45 points[1].x = dc.w - 1;
48 points[2].y = dc.h - 1;
49 points[3].x = -(dc.w - 1);
52 points[4].y = -(dc.h - 1);
53 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
60 if(len >= sizeof(buf))
61 len = sizeof(buf) - 1;
62 memcpy(buf, text, len);
65 h = dc.font.ascent + dc.font.descent;
66 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
69 /* shorten text if necessary */
70 while(len && (w = textnw(buf, len)) > dc.w - h)
74 return; /* too long */
76 gcv.foreground = invert ? dc.bg : dc.fg;
77 gcv.background = invert ? dc.fg : dc.bg;
79 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
80 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
84 gcv.font = dc.font.xfont->fid;
85 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
86 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
91 getcolor(const char *colstr)
93 Colormap cmap = DefaultColormap(dpy, screen);
96 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
101 setfont(const char *fontstr)
103 char **missing, *def;
107 setlocale(LC_ALL, "");
109 XFreeFontSet(dpy, dc.font.set);
110 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
112 XFreeStringList(missing);
114 XFreeFontSet(dpy, dc.font.set);
119 XFontSetExtents *font_extents;
120 XFontStruct **xfonts;
123 dc.font.ascent = dc.font.descent = 0;
124 font_extents = XExtentsOfFontSet(dc.font.set);
125 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
126 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
127 if(dc.font.ascent < (*xfonts)->ascent)
128 dc.font.ascent = (*xfonts)->ascent;
129 if(dc.font.descent < (*xfonts)->descent)
130 dc.font.descent = (*xfonts)->descent;
136 XFreeFont(dpy, dc.font.xfont);
137 dc.font.xfont = NULL;
138 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
140 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
142 eprint("error, cannot init 'fixed' font\n");
143 dc.font.ascent = dc.font.xfont->ascent;
144 dc.font.descent = dc.font.xfont->descent;
146 dc.font.height = dc.font.ascent + dc.font.descent;
150 textw(const char *text)
152 return textnw(text, strlen(text)) + dc.font.height;