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) {
17 XmbTextExtents(dc.font.set, text, len, NULL, &r);
20 return XTextWidth(dc.font.xfont, text, len);
24 drawtext(const char *text, unsigned long col[ColLast], Bool highlight) {
27 unsigned int len, olen;
29 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
31 XSetForeground(dpy, dc.gc, col[ColBG]);
32 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
38 olen = len = strlen(text);
39 if(len >= sizeof(buf))
40 len = sizeof(buf) - 1;
41 memcpy(buf, text, len);
44 h = dc.font.ascent + dc.font.descent;
45 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
48 /* shorten text if necessary */
49 while(len && (w = textnw(buf, len)) > dc.w - h)
61 return; /* too long */
62 gcv.foreground = col[ColFG];
64 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
65 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
68 gcv.font = dc.font.xfont->fid;
69 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
70 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
75 r.width = r.height = 3;
76 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
86 for(c = clients; c; c = getnext(c->next))
97 for(i = 0; i < ntags; i++) {
98 dc.w = textw(tags[i]);
100 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
102 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
107 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False);
111 dc.x = bx + bw - dc.w;
116 drawtext(stext, dc.status, False);
118 if((dc.w = dc.x - x) > bh) {
121 drawtext(sel->name, dc.sel, False);
123 drawtext(NULL, dc.norm, False);
125 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
130 drawtitle(Client *c) {
131 if(c == sel && issel) {
133 XUnmapWindow(dpy, c->twin);
134 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
138 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
139 XMapWindow(dpy, c->twin);
142 drawtext(c->name, dc.norm, False);
143 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
148 getcolor(const char *colstr) {
149 Colormap cmap = DefaultColormap(dpy, screen);
152 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
153 eprint("error, cannot allocate color '%s'\n", colstr);
158 setfont(const char *fontstr) {
159 char **missing, *def;
163 setlocale(LC_ALL, "");
165 XFreeFontSet(dpy, dc.font.set);
166 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
169 fprintf(stderr, "missing fontset: %s\n", missing[n]);
170 XFreeStringList(missing);
172 XFreeFontSet(dpy, dc.font.set);
177 XFontSetExtents *font_extents;
178 XFontStruct **xfonts;
181 dc.font.ascent = dc.font.descent = 0;
182 font_extents = XExtentsOfFontSet(dc.font.set);
183 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
184 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
185 if(dc.font.ascent < (*xfonts)->ascent)
186 dc.font.ascent = (*xfonts)->ascent;
187 if(dc.font.descent < (*xfonts)->descent)
188 dc.font.descent = (*xfonts)->descent;
194 XFreeFont(dpy, dc.font.xfont);
195 dc.font.xfont = NULL;
196 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
198 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
200 eprint("error, cannot init 'fixed' font\n");
201 dc.font.ascent = dc.font.xfont->ascent;
202 dc.font.descent = dc.font.xfont->descent;
204 dc.font.height = dc.font.ascent + dc.font.descent;
208 textw(const char *text) {
209 return textnw(text, strlen(text)) + dc.font.height;