arg@532: /* (C)opyright MMIV-MMVI Anselm R. Garbe garbeam@2: * See LICENSE file for license details. garbeam@2: */ garbeam@76: #include "dwm.h" garbeam@2: #include garbeam@2: #include garbeam@32: #include garbeam@32: garbeam@84: /* static */ garbeam@76: arg@572: static Bool arg@572: isoccupied(unsigned int t) arg@572: { arg@572: Client *c; arg@572: for(c = clients; c; c = c->next) arg@572: if(c->tags[t]) arg@572: return True; arg@572: return False; arg@572: } arg@572: garbeam@77: static unsigned int arg@461: textnw(const char *text, unsigned int len) { garbeam@77: XRectangle r; arg@123: garbeam@77: if(dc.font.set) { garbeam@77: XmbTextExtents(dc.font.set, text, len, NULL, &r); garbeam@77: return r.width; garbeam@77: } garbeam@77: return XTextWidth(dc.font.xfont, text, len); garbeam@75: } garbeam@75: garbeam@77: static void arg@607: drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) { arg@344: int x, y, w, h; arg@344: static char buf[256]; arg@344: unsigned int len, olen; arg@352: XGCValues gcv; arg@344: XRectangle r = { dc.x, dc.y, dc.w, dc.h }; arg@606: XPoint pt[5]; arg@344: arg@353: XSetForeground(dpy, dc.gc, col[ColBG]); arg@344: XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); garbeam@3: if(!text) garbeam@2: return; arg@218: w = 0; arg@269: olen = len = strlen(text); arg@581: if(len >= sizeof buf) arg@581: len = sizeof buf - 1; garbeam@3: memcpy(buf, text, len); garbeam@2: buf[len] = 0; 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: /* shorten text if necessary */ garbeam@43: while(len && (w = textnw(buf, len)) > dc.w - h) garbeam@2: buf[--len] = 0; arg@269: if(len < olen) { arg@273: if(len > 1) arg@273: buf[len - 1] = '.'; arg@273: if(len > 2) arg@273: buf[len - 2] = '.'; arg@269: if(len > 3) arg@273: buf[len - 3] = '.'; arg@269: } garbeam@34: if(w > dc.w) garbeam@2: return; /* too long */ arg@353: gcv.foreground = col[ColFG]; arg@352: if(dc.font.set) { arg@353: XChangeGC(dpy, dc.gc, GCForeground, &gcv); arg@342: XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); arg@352: } garbeam@2: else { arg@352: gcv.font = dc.font.xfont->fid; arg@353: XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); arg@342: XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); arg@342: } arg@606: x = (h + 2) / 4; arg@607: if(filledsquare) { arg@608: r.x = dc.x + 1; arg@608: r.y = dc.y + 1; arg@608: r.width = r.height = x + 1; arg@605: XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); garbeam@2: } arg@607: else if(emptysquare) { arg@608: pt[0].x = dc.x + 1; arg@608: pt[0].y = dc.y + 1; arg@606: pt[1].x = x; arg@606: pt[1].y = 0; arg@606: pt[2].x = 0; arg@606: pt[2].y = x; arg@606: pt[3].x = -x; arg@606: pt[3].y = 0; arg@606: pt[4].x = 0; arg@606: pt[4].y = -x; arg@606: XDrawLines(dpy, dc.drawable, dc.gc, pt, 5, CoordModePrevious); arg@604: } arg@604: } garbeam@2: garbeam@84: /* extern */ garbeam@77: garbeam@77: void arg@487: drawall(void) { garbeam@77: Client *c; garbeam@77: arg@142: for(c = clients; c; c = getnext(c->next)) garbeam@77: drawtitle(c); garbeam@77: drawstatus(); garbeam@77: } garbeam@77: garbeam@77: void arg@487: drawstatus(void) { arg@124: int i, x; garbeam@77: garbeam@77: dc.x = dc.y = 0; arg@178: for(i = 0; i < ntags; i++) { garbeam@77: dc.w = textw(tags[i]); arg@353: if(seltag[i]) arg@572: drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i)); garbeam@77: else arg@572: drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i)); arg@362: dc.x += dc.w; garbeam@77: } arg@362: dc.w = bmw; arg@572: drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False); arg@361: x = dc.x + dc.w; garbeam@77: dc.w = textw(stext); arg@570: dc.x = bw - dc.w; arg@342: if(dc.x < x) { arg@342: dc.x = x; arg@342: dc.w = bw - x; arg@342: } arg@572: drawtext(stext, dc.status, False, False); arg@371: if((dc.w = dc.x - x) > bh) { arg@342: dc.x = x; arg@572: drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False); arg@124: } garbeam@77: XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0); garbeam@79: XSync(dpy, False); garbeam@77: } garbeam@77: garbeam@76: void arg@461: drawtitle(Client *c) { arg@239: if(c == sel && issel) { garbeam@76: drawstatus(); arg@342: XUnmapWindow(dpy, c->twin); arg@353: XSetWindowBorder(dpy, c->win, dc.sel[ColBG]); garbeam@76: return; garbeam@76: } arg@353: XSetWindowBorder(dpy, c->win, dc.norm[ColBG]); arg@342: XMapWindow(dpy, c->twin); arg@342: dc.x = dc.y = 0; arg@342: dc.w = c->tw; arg@572: drawtext(c->name, dc.norm, False,False); arg@342: XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0); garbeam@79: XSync(dpy, False); garbeam@76: } garbeam@76: garbeam@43: unsigned long arg@461: getcolor(const char *colstr) { arg@123: Colormap cmap = DefaultColormap(dpy, screen); garbeam@2: XColor color; garbeam@43: arg@495: if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) arg@495: eprint("error, cannot allocate color '%s'\n", colstr); garbeam@2: return color.pixel; garbeam@2: } garbeam@2: garbeam@2: void arg@461: setfont(const char *fontstr) { 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@2: } garbeam@43: if(dc.font.set) { garbeam@2: XFontSetExtents *font_extents; garbeam@2: XFontStruct **xfonts; garbeam@2: char **font_names; 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: } garbeam@76: garbeam@76: unsigned int arg@461: textw(const char *text) { garbeam@76: return textnw(text, strlen(text)) + dc.font.height; garbeam@76: }