2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
13 drawborder(Display *dpy, Brush *b)
16 XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
17 XSetForeground(dpy, b->gc, b->border);
20 points[1].x = b->w - 1;
23 points[2].y = b->h - 1;
24 points[3].x = -(b->w - 1);
27 points[4].y = -(b->h - 1);
28 XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
32 draw(Display *dpy, Brush *b, Bool border, const char *text)
34 unsigned int x, y, w, h, len;
37 XRectangle r = { b->x, b->y, b->w, b->h };
39 XSetForeground(dpy, b->gc, b->bg);
40 XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
49 if(len >= sizeof(buf))
50 len = sizeof(buf) - 1;
51 memcpy(buf, text, len);
54 h = b->font.ascent + b->font.descent;
55 y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
58 /* shorten text if necessary */
59 while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
63 return; /* too long */
65 gcv.foreground = b->fg;
66 gcv.background = b->bg;
68 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
69 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
73 gcv.font = b->font.xfont->fid;
74 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
75 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
80 xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
83 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
88 loadcolors(Display *dpy, int screen, Brush *b,
89 const char *bg, const char *fg, const char *border)
91 Colormap cmap = DefaultColormap(dpy, screen);
92 b->bg = xloadcolors(dpy, cmap, bg);
93 b->fg = xloadcolors(dpy, cmap, fg);
94 b->border = xloadcolors(dpy, cmap, border);
98 textnw(Fnt *font, char *text, unsigned int len)
102 XmbTextExtents(font->set, text, len, NULL, &r);
105 return XTextWidth(font->xfont, text, len);
109 textw(Fnt *font, char *text)
111 return textnw(font, text, strlen(text));
117 return font->height + 4;
121 loadfont(Display *dpy, Fnt *font, const char *fontstr)
123 char **missing, *def;
128 setlocale(LC_ALL, "");
130 XFreeFontSet(dpy, font->set);
131 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
134 fprintf(stderr, "missing fontset: %s\n", missing[n]);
135 XFreeStringList(missing);
137 XFreeFontSet(dpy, font->set);
142 XFontSetExtents *font_extents;
143 XFontStruct **xfonts;
147 font->ascent = font->descent = 0;
148 font_extents = XExtentsOfFontSet(font->set);
149 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
150 for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
151 if(font->ascent < (*xfonts)->ascent)
152 font->ascent = (*xfonts)->ascent;
153 if(font->descent < (*xfonts)->descent)
154 font->descent = (*xfonts)->descent;
160 XFreeFont(dpy, font->xfont);
162 font->xfont = XLoadQueryFont(dpy, fontstr);
164 font->xfont = XLoadQueryFont(dpy, "fixed");
166 error("error, cannot load 'fixed' font\n");
167 font->ascent = font->xfont->ascent;
168 font->descent = font->xfont->descent;
170 font->height = font->ascent + font->descent;