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@32: #include garbeam@32: garbeam@43: #include "dwm.h" garbeam@2: garbeam@73: void garbeam@75: drawall() garbeam@75: { garbeam@75: Client *c; garbeam@75: garbeam@75: for(c = clients; c; c = getnext(c->next)) garbeam@75: drawtitle(c); garbeam@75: drawstatus(); garbeam@75: } garbeam@75: garbeam@75: void garbeam@74: drawstatus() garbeam@73: { garbeam@73: int i; garbeam@75: Bool istile = arrange == dotile; garbeam@73: garbeam@73: dc.x = dc.y = 0; garbeam@73: dc.w = bw; garbeam@75: drawtext(NULL, !istile, False); garbeam@73: garbeam@75: dc.w = 0; garbeam@73: for(i = 0; i < TLast; i++) { garbeam@73: dc.x += dc.w; garbeam@73: dc.w = textw(tags[i]); garbeam@75: if(istile) garbeam@75: drawtext(tags[i], (i == tsel), True); garbeam@75: else garbeam@75: drawtext(tags[i], (i != tsel), True); garbeam@73: } garbeam@73: if(sel) { garbeam@73: dc.x += dc.w; garbeam@73: dc.w = textw(sel->name); garbeam@75: drawtext(sel->name, istile, True); garbeam@73: } garbeam@73: dc.w = textw(stext); garbeam@73: dc.x = bx + bw - dc.w; garbeam@75: drawtext(stext, !istile, False); garbeam@73: garbeam@73: XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0); garbeam@73: XFlush(dpy); garbeam@73: } garbeam@73: garbeam@73: void garbeam@74: drawtitle(Client *c) garbeam@73: { garbeam@73: int i; garbeam@75: Bool istile = arrange == dotile; garbeam@75: garbeam@73: if(c == sel) { garbeam@74: drawstatus(); garbeam@73: XUnmapWindow(dpy, c->title); garbeam@73: XSetWindowBorder(dpy, c->win, dc.fg); garbeam@73: return; garbeam@73: } garbeam@73: garbeam@73: XSetWindowBorder(dpy, c->win, dc.bg); garbeam@73: XMapWindow(dpy, c->title); garbeam@73: garbeam@73: dc.x = dc.y = 0; garbeam@73: garbeam@73: dc.w = 0; garbeam@73: for(i = 0; i < TLast; i++) { garbeam@73: if(c->tags[i]) { garbeam@73: dc.x += dc.w; garbeam@73: dc.w = textw(c->tags[i]); garbeam@75: drawtext(c->tags[i], !istile, True); garbeam@73: } garbeam@73: } garbeam@73: dc.x += dc.w; garbeam@73: dc.w = textw(c->name); garbeam@75: drawtext(c->name, !istile, True); garbeam@73: XCopyArea(dpy, dc.drawable, c->title, dc.gc, garbeam@73: 0, 0, c->tw, c->th, 0, 0); garbeam@73: XFlush(dpy); garbeam@73: } garbeam@73: garbeam@2: static void garbeam@34: drawborder(void) garbeam@2: { garbeam@2: XPoint points[5]; garbeam@34: XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); garbeam@34: XSetForeground(dpy, dc.gc, dc.border); garbeam@34: points[0].x = dc.x; garbeam@34: points[0].y = dc.y; garbeam@34: points[1].x = dc.w - 1; garbeam@2: points[1].y = 0; garbeam@2: points[2].x = 0; garbeam@34: points[2].y = dc.h - 1; garbeam@34: points[3].x = -(dc.w - 1); garbeam@2: points[3].y = 0; garbeam@2: points[4].x = 0; garbeam@34: points[4].y = -(dc.h - 1); garbeam@34: XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious); garbeam@2: } garbeam@2: garbeam@2: void garbeam@66: drawtext(const char *text, Bool invert, Bool border) garbeam@2: { garbeam@33: int x, y, w, h; garbeam@33: unsigned int len; garbeam@2: static char buf[256]; garbeam@2: XGCValues gcv; garbeam@34: XRectangle r = { dc.x, dc.y, dc.w, dc.h }; garbeam@2: garbeam@66: XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg); garbeam@34: XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); garbeam@2: garbeam@30: w = 0; garbeam@3: if(border) garbeam@34: drawborder(); garbeam@2: garbeam@3: if(!text) garbeam@2: return; garbeam@2: garbeam@3: len = strlen(text); garbeam@2: if(len >= sizeof(buf)) garbeam@2: len = sizeof(buf) - 1; garbeam@3: memcpy(buf, text, len); garbeam@2: buf[len] = 0; garbeam@2: garbeam@34: h = dc.font.ascent + dc.font.descent; garbeam@34: y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; garbeam@34: x = dc.x + (h / 2); garbeam@2: garbeam@2: /* shorten text if necessary */ garbeam@43: while(len && (w = textnw(buf, len)) > dc.w - h) garbeam@2: buf[--len] = 0; garbeam@2: garbeam@34: if(w > dc.w) garbeam@2: return; /* too long */ garbeam@2: garbeam@66: gcv.foreground = invert ? dc.bg : dc.fg; garbeam@66: gcv.background = invert ? dc.fg : dc.bg; garbeam@34: if(dc.font.set) { garbeam@34: XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv); garbeam@34: XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc, garbeam@2: x, y, buf, len); garbeam@2: } garbeam@2: else { garbeam@34: gcv.font = dc.font.xfont->fid; garbeam@34: XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv); garbeam@34: XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len); garbeam@2: } garbeam@2: } garbeam@2: garbeam@43: unsigned long garbeam@74: getcolor(const char *colstr) garbeam@2: { garbeam@2: XColor color; garbeam@43: Colormap cmap = DefaultColormap(dpy, screen); garbeam@43: garbeam@2: XAllocNamedColor(dpy, cmap, colstr, &color, &color); garbeam@2: return color.pixel; garbeam@2: } garbeam@2: garbeam@43: unsigned int garbeam@43: textnw(char *text, unsigned int len) garbeam@2: { garbeam@43: XRectangle r; garbeam@43: if(dc.font.set) { garbeam@43: XmbTextExtents(dc.font.set, text, len, NULL, &r); garbeam@43: return r.width; garbeam@43: } garbeam@43: return XTextWidth(dc.font.xfont, text, len); garbeam@2: } garbeam@2: garbeam@2: unsigned int garbeam@43: textw(char *text) garbeam@2: { garbeam@73: return textnw(text, strlen(text)) + dc.font.height; garbeam@2: } garbeam@2: garbeam@2: void garbeam@74: setfont(const char *fontstr) garbeam@2: { garbeam@2: char **missing, *def; garbeam@33: int i, n; garbeam@2: garbeam@7: missing = NULL; garbeam@2: setlocale(LC_ALL, ""); garbeam@43: if(dc.font.set) garbeam@43: XFreeFontSet(dpy, dc.font.set); garbeam@43: dc.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@43: if(dc.font.set) { garbeam@43: XFreeFontSet(dpy, dc.font.set); garbeam@43: dc.font.set = NULL; garbeam@2: } garbeam@2: } garbeam@43: if(dc.font.set) { garbeam@2: XFontSetExtents *font_extents; garbeam@2: XFontStruct **xfonts; garbeam@2: char **font_names; garbeam@2: garbeam@43: dc.font.ascent = dc.font.descent = 0; garbeam@43: font_extents = XExtentsOfFontSet(dc.font.set); garbeam@43: n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); garbeam@43: for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { garbeam@43: if(dc.font.ascent < (*xfonts)->ascent) garbeam@43: dc.font.ascent = (*xfonts)->ascent; garbeam@43: if(dc.font.descent < (*xfonts)->descent) garbeam@43: dc.font.descent = (*xfonts)->descent; garbeam@2: xfonts++; garbeam@2: } garbeam@2: } garbeam@2: else { garbeam@43: if(dc.font.xfont) garbeam@43: XFreeFont(dpy, dc.font.xfont); garbeam@43: dc.font.xfont = NULL; garbeam@43: dc.font.xfont = XLoadQueryFont(dpy, fontstr); garbeam@43: if (!dc.font.xfont) garbeam@43: dc.font.xfont = XLoadQueryFont(dpy, "fixed"); garbeam@43: if (!dc.font.xfont) garbeam@75: eprint("error, cannot init 'fixed' font\n"); garbeam@43: dc.font.ascent = dc.font.xfont->ascent; garbeam@43: dc.font.descent = dc.font.xfont->descent; garbeam@2: } garbeam@43: dc.font.height = dc.font.ascent + dc.font.descent; garbeam@2: }