X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=draw.c;h=3fbc85c7426b8e551d8a4f8b938b5701eeb51c24;hb=e7fa504c3e095db65853afd39d2d1324d439dcf4;hp=455c137e5b64aa128c2e4fb9d0264ea67cd18f9f;hpb=650a1fb4e1a798aca48a53739f5bb2649191bc1c;p=dwm.git diff --git a/draw.c b/draw.c index 455c137..3fbc85c 100644 --- a/draw.c +++ b/draw.c @@ -8,7 +8,7 @@ #include -#include "wm.h" +#include "dwm.h" static void drawborder(void) @@ -30,7 +30,7 @@ drawborder(void) } void -draw(Bool border, const char *text) +drawtext(const char *text, Bool invert, Bool border) { int x, y, w, h; unsigned int len; @@ -38,7 +38,7 @@ draw(Bool border, const char *text) XGCValues gcv; XRectangle r = { dc.x, dc.y, dc.w, dc.h }; - XSetForeground(dpy, dc.gc, dc.bg); + XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg); XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); w = 0; @@ -59,14 +59,14 @@ draw(Bool border, const char *text) x = dc.x + (h / 2); /* shorten text if necessary */ - while(len && (w = textnw(&dc.font, buf, len)) > dc.w - h) + while(len && (w = textnw(buf, len)) > dc.w - h) buf[--len] = 0; if(w > dc.w) return; /* too long */ - gcv.foreground = dc.fg; - gcv.background = dc.bg; + gcv.foreground = invert ? dc.bg : dc.fg; + gcv.background = invert ? dc.fg : dc.bg; if(dc.font.set) { XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv); XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc, @@ -79,93 +79,80 @@ draw(Bool border, const char *text) } } -static unsigned long -xinitcolors(Colormap cmap, const char *colstr) +unsigned long +initcolor(const char *colstr) { XColor color; + Colormap cmap = DefaultColormap(dpy, screen); + XAllocNamedColor(dpy, cmap, colstr, &color, &color); return color.pixel; } -void -initcolors(const char *bg, const char *fg, const char *border) -{ - Colormap cmap = DefaultColormap(dpy, screen); - dc.bg = xinitcolors(cmap, bg); - dc.fg = xinitcolors(cmap, fg); - dc.border = xinitcolors(cmap, border); -} - unsigned int -textnw(Fnt *font, char *text, unsigned int len) +textnw(char *text, unsigned int len) { XRectangle r; - if(font->set) { - XmbTextExtents(font->set, text, len, NULL, &r); + if(dc.font.set) { + XmbTextExtents(dc.font.set, text, len, NULL, &r); return r.width; } - return XTextWidth(font->xfont, text, len); -} - -unsigned int -textw(Fnt *font, char *text) -{ - return textnw(font, text, strlen(text)); + return XTextWidth(dc.font.xfont, text, len); } unsigned int -texth(Fnt *font) +textw(char *text) { - return font->height + 4; + return textnw(text, strlen(text)); } void -initfont(Fnt *font, const char *fontstr) +initfont(const char *fontstr) { char **missing, *def; int i, n; missing = NULL; setlocale(LC_ALL, ""); - if(font->set) - XFreeFontSet(dpy, font->set); - font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); + if(dc.font.set) + XFreeFontSet(dpy, dc.font.set); + dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); if(missing) { while(n--) fprintf(stderr, "missing fontset: %s\n", missing[n]); XFreeStringList(missing); - if(font->set) { - XFreeFontSet(dpy, font->set); - font->set = NULL; + if(dc.font.set) { + XFreeFontSet(dpy, dc.font.set); + dc.font.set = NULL; } } - if(font->set) { + if(dc.font.set) { XFontSetExtents *font_extents; XFontStruct **xfonts; char **font_names; - font->ascent = font->descent = 0; - font_extents = XExtentsOfFontSet(font->set); - n = XFontsOfFontSet(font->set, &xfonts, &font_names); - for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) { - if(font->ascent < (*xfonts)->ascent) - font->ascent = (*xfonts)->ascent; - if(font->descent < (*xfonts)->descent) - font->descent = (*xfonts)->descent; + dc.font.ascent = dc.font.descent = 0; + font_extents = XExtentsOfFontSet(dc.font.set); + n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); + for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { + if(dc.font.ascent < (*xfonts)->ascent) + dc.font.ascent = (*xfonts)->ascent; + if(dc.font.descent < (*xfonts)->descent) + dc.font.descent = (*xfonts)->descent; xfonts++; } } else { - if(font->xfont) - XFreeFont(dpy, font->xfont); - font->xfont = NULL; - font->xfont = XLoadQueryFont(dpy, fontstr); - if (!font->xfont) - font->xfont = XLoadQueryFont(dpy, "fixed"); - if (!font->xfont) + if(dc.font.xfont) + XFreeFont(dpy, dc.font.xfont); + dc.font.xfont = NULL; + dc.font.xfont = XLoadQueryFont(dpy, fontstr); + if (!dc.font.xfont) + dc.font.xfont = XLoadQueryFont(dpy, "fixed"); + if (!dc.font.xfont) error("error, cannot init 'fixed' font\n"); - font->ascent = font->xfont->ascent; - font->descent = font->xfont->descent; + dc.font.ascent = dc.font.xfont->ascent; + dc.font.descent = dc.font.xfont->descent; } - font->height = font->ascent + font->descent; + dc.font.height = dc.font.ascent + dc.font.descent; }