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);
18 points[0].x = b->rect.x;
19 points[0].y = b->rect.y;
20 points[1].x = b->rect.width - 1;
23 points[2].y = b->rect.height - 1;
24 points[3].x = -(b->rect.width - 1);
27 points[4].y = -(b->rect.height - 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;
38 XSetForeground(dpy, b->gc, b->bg);
39 XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
48 if(len >= sizeof(buf))
49 len = sizeof(buf) - 1;
50 memcpy(buf, text, len);
53 h = b->font.ascent + b->font.descent;
54 y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent;
55 x = b->rect.x + (h / 2);
57 /* shorten text if necessary */
58 while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h)
62 return; /* too long */
64 gcv.foreground = b->fg;
65 gcv.background = b->bg;
67 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
68 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
72 gcv.font = b->font.xfont->fid;
73 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
74 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
79 xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
82 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
87 loadcolors(Display *dpy, int screen, Brush *b,
88 const char *bg, const char *fg, const char *border)
90 Colormap cmap = DefaultColormap(dpy, screen);
91 b->bg = xloadcolors(dpy, cmap, bg);
92 b->fg = xloadcolors(dpy, cmap, fg);
93 b->border = xloadcolors(dpy, cmap, border);
97 textwidth_l(Fnt *font, char *text, unsigned int len)
101 XmbTextExtents(font->set, text, len, 0, &r);
104 return XTextWidth(font->xfont, text, len);
108 textwidth(Fnt *font, char *text)
110 return textwidth_l(font, text, strlen(text));
114 loadfont(Display *dpy, Fnt *font, const char *fontstr)
116 char **missing, *def;
121 setlocale(LC_ALL, "");
123 XFreeFontSet(dpy, font->set);
124 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
127 fprintf(stderr, "missing fontset: %s\n", missing[n]);
128 XFreeStringList(missing);
130 XFreeFontSet(dpy, font->set);
135 XFontSetExtents *font_extents;
136 XFontStruct **xfonts;
140 font->ascent = font->descent = 0;
141 font_extents = XExtentsOfFontSet(font->set);
142 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
143 for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
144 if(font->ascent < (*xfonts)->ascent)
145 font->ascent = (*xfonts)->ascent;
146 if(font->descent < (*xfonts)->descent)
147 font->descent = (*xfonts)->descent;
153 XFreeFont(dpy, font->xfont);
155 font->xfont = XLoadQueryFont(dpy, fontstr);
157 font->xfont = XLoadQueryFont(dpy, "fixed");
159 error("error, cannot load 'fixed' font\n");
160 font->ascent = font->xfont->ascent;
161 font->descent = font->xfont->descent;
163 font->height = font->ascent + font->descent;
167 labelheight(Fnt *font)
169 return font->height + 4;