garbeam@2: /* garbeam@2: * (C)opyright MMIV-MMVI Anselm R. Garbe garbeam@2: * See LICENSE file for license details. garbeam@2: */ garbeam@2: garbeam@2: #include garbeam@2: #include garbeam@2: garbeam@2: #include "draw.h" garbeam@2: #include "util.h" garbeam@2: garbeam@2: static void garbeam@2: drawborder(Display *dpy, Brush *b) garbeam@2: { garbeam@2: XPoint points[5]; garbeam@2: XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter); garbeam@2: XSetForeground(dpy, b->gc, b->color.border); garbeam@2: points[0].x = b->rect.x; garbeam@2: points[0].y = b->rect.y; garbeam@2: points[1].x = b->rect.width - 1; garbeam@2: points[1].y = 0; garbeam@2: points[2].x = 0; garbeam@2: points[2].y = b->rect.height - 1; garbeam@2: points[3].x = -(b->rect.width - 1); garbeam@2: points[3].y = 0; garbeam@2: points[4].x = 0; garbeam@2: points[4].y = -(b->rect.height - 1); garbeam@2: XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious); garbeam@2: } garbeam@2: garbeam@2: void garbeam@2: draw(Display *dpy, Brush *b) garbeam@2: { garbeam@2: unsigned int x, y, w, h, len; garbeam@2: static char buf[256]; garbeam@2: XGCValues gcv; garbeam@2: garbeam@2: XSetForeground(dpy, b->gc, b->color.bg); garbeam@2: XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1); garbeam@2: garbeam@2: if(b->border) garbeam@2: drawborder(dpy, b); garbeam@2: garbeam@2: if(!b->text) garbeam@2: return; garbeam@2: garbeam@2: len = strlen(b->text); garbeam@2: if(len >= sizeof(buf)) garbeam@2: len = sizeof(buf) - 1; garbeam@2: memcpy(buf, b->text, len); garbeam@2: buf[len] = 0; garbeam@2: garbeam@2: h = b->font->ascent + b->font->descent; garbeam@2: y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font->ascent; garbeam@2: x = b->rect.x + (h / 2); garbeam@2: garbeam@2: /* shorten text if necessary */ garbeam@2: while(len && (w = textwidth_l(b->font, buf, len)) > b->rect.width - h) garbeam@2: buf[--len] = 0; garbeam@2: garbeam@2: if(w > b->rect.width) garbeam@2: return; /* too long */ garbeam@2: garbeam@2: gcv.foreground = b->color.fg; garbeam@2: gcv.background = b->color.bg; garbeam@2: if(b->font->set) { garbeam@2: XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv); garbeam@2: XmbDrawImageString(dpy, b->drawable, b->font->set, b->gc, garbeam@2: x, y, buf, len); garbeam@2: } garbeam@2: else { garbeam@2: gcv.font = b->font->xfont->fid; garbeam@2: XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv); garbeam@2: XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len); garbeam@2: } garbeam@2: } garbeam@2: garbeam@2: static unsigned long garbeam@2: xloadcolor(Display *dpy, Colormap cmap, const char *colstr) garbeam@2: { garbeam@2: XColor color; garbeam@2: XAllocNamedColor(dpy, cmap, colstr, &color, &color); garbeam@2: return color.pixel; garbeam@2: } garbeam@2: garbeam@2: void garbeam@2: loadcolor(Display *dpy, int screen, Color *c, garbeam@2: const char *bg, const char *fg, const char *border) garbeam@2: { garbeam@2: Colormap cmap = DefaultColormap(dpy, screen); garbeam@2: c->bg = xloadcolor(dpy, cmap, bg); garbeam@2: c->fg = xloadcolor(dpy, cmap, fg); garbeam@2: c->border = xloadcolor(dpy, cmap, border); garbeam@2: } garbeam@2: garbeam@2: unsigned int garbeam@2: textwidth_l(Fnt *font, char *text, unsigned int len) garbeam@2: { garbeam@2: if(font->set) { garbeam@2: XRectangle r; garbeam@2: XmbTextExtents(font->set, text, len, 0, &r); garbeam@2: return r.width; garbeam@2: } garbeam@2: return XTextWidth(font->xfont, text, len); garbeam@2: } garbeam@2: garbeam@2: unsigned int garbeam@2: textwidth(Fnt *font, char *text) garbeam@2: { garbeam@2: return textwidth_l(font, text, strlen(text)); garbeam@2: } garbeam@2: garbeam@2: void garbeam@2: loadfont(Display *dpy, Fnt *font, const char *fontstr) garbeam@2: { garbeam@2: char **missing, *def; garbeam@2: int n; garbeam@2: garbeam@2: missing = 0; garbeam@2: def = "?"; garbeam@2: setlocale(LC_ALL, ""); garbeam@2: if(font->set) garbeam@2: XFreeFontSet(dpy, font->set); garbeam@2: font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); garbeam@2: if(missing) { garbeam@2: while(n--) garbeam@2: fprintf(stderr, "missing fontset: %s\n", missing[n]); garbeam@2: XFreeStringList(missing); garbeam@2: if(font->set) { garbeam@2: XFreeFontSet(dpy, font->set); garbeam@2: font->set = 0; garbeam@2: } garbeam@2: } garbeam@2: if(font->set) { garbeam@2: XFontSetExtents *font_extents; garbeam@2: XFontStruct **xfonts; garbeam@2: char **font_names; garbeam@2: unsigned int i; garbeam@2: garbeam@2: font->ascent = font->descent = 0; garbeam@2: font_extents = XExtentsOfFontSet(font->set); garbeam@2: n = XFontsOfFontSet(font->set, &xfonts, &font_names); garbeam@2: for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) { garbeam@2: if(font->ascent < (*xfonts)->ascent) garbeam@2: font->ascent = (*xfonts)->ascent; garbeam@2: if(font->descent < (*xfonts)->descent) garbeam@2: font->descent = (*xfonts)->descent; garbeam@2: xfonts++; garbeam@2: } garbeam@2: } garbeam@2: else { garbeam@2: if(font->xfont) garbeam@2: XFreeFont(dpy, font->xfont); garbeam@2: font->xfont = 0; garbeam@2: font->xfont = XLoadQueryFont(dpy, fontstr); garbeam@2: if (!font->xfont) garbeam@2: font->xfont = XLoadQueryFont(dpy, "fixed"); garbeam@2: if (!font->xfont) garbeam@2: error("error, cannot load 'fixed' font\n"); garbeam@2: font->ascent = font->xfont->ascent; garbeam@2: font->descent = font->xfont->descent; garbeam@2: } garbeam@2: }