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);
25 drawtext(const char *text, Bool invert)
29 unsigned int len, olen;
32 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
34 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
35 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
36 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
37 XSetForeground(dpy, dc.gc, dc.border);
40 points[1].x = dc.w - 1;
43 points[2].y = dc.h - 1;
44 points[3].x = -(dc.w - 1);
47 points[4].y = -(dc.h - 1);
48 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
54 olen = len = strlen(text);
55 if(len >= sizeof(buf))
56 len = sizeof(buf) - 1;
57 memcpy(buf, text, len);
60 h = dc.font.ascent + dc.font.descent;
61 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
64 /* shorten text if necessary */
65 while(len && (w = textnw(buf, len)) > dc.w - h)
77 return; /* too long */
79 gcv.foreground = invert ? dc.bg : dc.fg;
80 gcv.background = invert ? dc.fg : dc.bg;
82 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
83 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
87 gcv.font = dc.font.xfont->fid;
88 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
89 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
100 for(c = clients; c; c = getnext(c->next))
109 Bool istile = arrange == dotile;
113 drawtext(NULL, !istile);
116 for(i = 0; i < ntags; i++) {
118 dc.w = textw(tags[i]);
120 drawtext(tags[i], seltag[i]);
122 drawtext(tags[i], !seltag[i]);
126 dc.x = bx + bw - dc.w;
127 drawtext(stext, !istile);
128 if(sel && ((dc.w = dc.x - x) >= bh)) {
130 drawtext(sel->name, istile);
132 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
140 Bool istile = arrange == dotile;
142 if(c == sel && issel) {
144 XUnmapWindow(dpy, c->title);
145 XSetWindowBorder(dpy, c->win, dc.fg);
149 XSetWindowBorder(dpy, c->win, dc.bg);
150 XMapWindow(dpy, c->title);
155 for(i = 0; i < ntags; i++) {
158 dc.w = textw(tags[i]);
159 drawtext(tags[i], !istile);
164 drawtext(c->name, !istile);
165 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
170 getcolor(const char *colstr)
172 Colormap cmap = DefaultColormap(dpy, screen);
175 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
180 setfont(const char *fontstr)
182 char **missing, *def;
186 setlocale(LC_ALL, "");
188 XFreeFontSet(dpy, dc.font.set);
189 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
192 fprintf(stderr, "missing fontset: %s\n", missing[n]);
193 XFreeStringList(missing);
195 XFreeFontSet(dpy, dc.font.set);
200 XFontSetExtents *font_extents;
201 XFontStruct **xfonts;
204 dc.font.ascent = dc.font.descent = 0;
205 font_extents = XExtentsOfFontSet(dc.font.set);
206 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
207 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
208 if(dc.font.ascent < (*xfonts)->ascent)
209 dc.font.ascent = (*xfonts)->ascent;
210 if(dc.font.descent < (*xfonts)->descent)
211 dc.font.descent = (*xfonts)->descent;
217 XFreeFont(dpy, dc.font.xfont);
218 dc.font.xfont = NULL;
219 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
221 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
223 eprint("error, cannot init 'fixed' font\n");
224 dc.font.ascent = dc.font.xfont->ascent;
225 dc.font.descent = dc.font.xfont->descent;
227 dc.font.height = dc.font.ascent + dc.font.descent;
231 textw(const char *text)
233 return textnw(text, strlen(text)) + dc.font.height;