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 sel)
34 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
36 XSetForeground(dpy, dc.gc, sel ? dc.fg : dc.bg);
37 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
43 if(len >= sizeof(buf))
44 len = sizeof(buf) - 1;
45 memcpy(buf, text, len);
48 h = dc.font.ascent + dc.font.descent;
49 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
52 /* shorten text if necessary */
53 while(len && (w = textnw(buf, len)) > dc.w - h)
57 return; /* too long */
59 gcv.foreground = sel ? dc.bg : dc.fg;
60 gcv.background = sel ? dc.fg : dc.bg;
62 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
63 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
67 gcv.font = dc.font.xfont->fid;
68 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
69 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
72 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
75 points[1].x = dc.w - 1;
78 points[2].y = dc.h - 1;
79 points[3].x = -(dc.w - 1);
82 points[4].y = -(dc.h - 1);
83 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
89 getcolor(const char *colstr)
91 Colormap cmap = DefaultColormap(dpy, screen);
94 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
99 setfont(const char *fontstr)
101 char **missing, *def;
105 setlocale(LC_ALL, "");
107 XFreeFontSet(dpy, dc.font.set);
108 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
110 XFreeStringList(missing);
112 XFreeFontSet(dpy, dc.font.set);
117 XFontSetExtents *font_extents;
118 XFontStruct **xfonts;
121 dc.font.ascent = dc.font.descent = 0;
122 font_extents = XExtentsOfFontSet(dc.font.set);
123 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
124 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
125 if(dc.font.ascent < (*xfonts)->ascent)
126 dc.font.ascent = (*xfonts)->ascent;
127 if(dc.font.descent < (*xfonts)->descent)
128 dc.font.descent = (*xfonts)->descent;
134 XFreeFont(dpy, dc.font.xfont);
135 dc.font.xfont = NULL;
136 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
138 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
140 eprint("error, cannot init 'fixed' font\n");
141 dc.font.ascent = dc.font.xfont->ascent;
142 dc.font.descent = dc.font.xfont->descent;
144 dc.font.height = dc.font.ascent + dc.font.descent;
148 textw(const char *text)
150 return textnw(text, strlen(text)) + dc.font.height;