1 /* See LICENSE file for copyright and license details. */
11 drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
12 Drw *drw = (Drw *)calloc(1, sizeof(Drw));
18 drw->drwable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
19 drw->gc = XCreateGC(dpy, win, 0, NULL);
20 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
25 drw_resize(Drw *drw, unsigned int w, unsigned int h) {
30 XFreePixmap(drw->dpy, drw->drwable);
31 drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen));
36 XFreePixmap(drw->dpy, drw->drwable);
37 XFreeGC(drw->dpy, drw->gc);
42 drw_font_create(Drw *drw, const char *fontname) {
49 font = (Fnt *)calloc(1, sizeof(Fnt));
50 font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def);
53 fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
54 XFreeStringList(missing);
59 XExtentsOfFontSet(font->set);
60 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
62 font->ascent = MAX(font->ascent, (*xfonts)->ascent);
63 font->descent = MAX(font->descent,(*xfonts)->descent);
68 if(!(font->xfont = XLoadQueryFont(drw->dpy, fontname))
69 && !(font->xfont = XLoadQueryFont(drw->dpy, "fixed")))
70 die("error, cannot load font: '%s'\n", fontname);
71 font->ascent = font->xfont->ascent;
72 font->descent = font->xfont->descent;
74 font->h = font->ascent + font->descent;
79 drw_font_free(Drw *drw, Fnt *font) {
83 XFreeFontSet(drw->dpy, font->set);
85 XFreeFont(drw->dpy, font->xfont);
90 drw_clr_create(Drw *drw, const char *clrname) {
91 Clr *clr = (Clr *)calloc(1, sizeof(Clr));
92 Colormap cmap = DefaultColormap(drw->dpy, drw->screen);
95 if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
96 die("error, cannot allocate color '%s'\n", clrname);
97 clr->rgb = color.pixel;
102 drw_clr_free(Drw *drw, Clr *clr) {
109 drw_setfont(Drw *drw, Fnt *font) {
116 drw_setfg(Drw *drw, Clr *clr) {
123 drw_setbg(Drw *drw, Clr *clr) {
130 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
133 if(!drw || !drw->font || !drw->fg || !drw->bg)
135 XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
136 dx = (drw->font->ascent + drw->font->descent + 2) / 4;
138 XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1);
140 XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx);
144 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
146 int i, tx, ty, len, olen;
149 if(!drw || !drw->fg || !drw->bg)
151 XSetForeground(drw->dpy, drw->gc, invert ? drw->fg->rgb : drw->bg->rgb);
152 XFillRectangle(drw->dpy, drw->drwable, drw->gc, x, y, w, h);
153 if(!text || !drw->font)
156 drw_getexts(drw, text, olen, &tex);
157 ty = y + (h / 2) - tex.yOff;
159 /* shorten text if necessary */
160 for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
161 drw_getexts(drw, text, len, &tex);
164 memcpy(buf, text, len);
166 for(i = len; i && i > len - 3; buf[--i] = '.');
167 XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
169 XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len);
171 XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len);
175 drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) {
178 XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y);
179 XSync(drw->dpy, False);
184 drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) {
187 if(!drw || !drw->font || !text)
190 XmbTextExtents(drw->font->set, text, len, NULL, &r);
197 tex->h = drw->font->ascent + drw->font->descent;
198 tex->w = XTextWidth(drw->font->xfont, text, len);
199 tex->xOff = tex->h / 2;
200 tex->yOff = (tex->h / 2) + drw->font->ascent;