1 /* See LICENSE file for copyright and license details. */
11 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) {
12 Drw *drw = (Drw *)calloc(1, sizeof(Drw));
20 drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
21 drw->gc = XCreateGC(dpy, root, 0, NULL);
22 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
27 drw_resize(Drw *drw, unsigned int w, unsigned int h) {
32 if(drw->drawable != 0)
33 XFreePixmap(drw->dpy, drw->drawable);
34 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
39 XFreePixmap(drw->dpy, drw->drawable);
40 XFreeGC(drw->dpy, drw->gc);
45 drw_font_create(Display *dpy, const char *fontname) {
50 font = (Fnt *)calloc(1, sizeof(Fnt));
53 font->set = XCreateFontSet(dpy, fontname, &missing, &n, &def);
56 fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
57 XFreeStringList(missing);
62 XExtentsOfFontSet(font->set);
63 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
65 font->ascent = MAX(font->ascent, (*xfonts)->ascent);
66 font->descent = MAX(font->descent,(*xfonts)->descent);
71 if(!(font->xfont = XLoadQueryFont(dpy, fontname))
72 && !(font->xfont = XLoadQueryFont(dpy, "fixed")))
73 die("error, cannot load font: '%s'\n", fontname);
74 font->ascent = font->xfont->ascent;
75 font->descent = font->xfont->descent;
77 font->h = font->ascent + font->descent;
82 drw_font_free(Display *dpy, Fnt *font) {
86 XFreeFontSet(dpy, font->set);
88 XFreeFont(dpy, font->xfont);
93 drw_clr_create(Drw *drw, const char *clrname) {
100 clr = (Clr *)calloc(1, sizeof(Clr));
103 cmap = DefaultColormap(drw->dpy, drw->screen);
104 if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
105 die("error, cannot allocate color '%s'\n", clrname);
106 clr->rgb = color.pixel;
111 drw_clr_free(Clr *clr) {
118 drw_setfont(Drw *drw, Fnt *font) {
124 drw_settheme(Drw *drw, Theme *theme) {
131 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) {
134 if(!drw || !drw->font || !drw->theme)
136 XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb);
137 dx = (drw->font->ascent + drw->font->descent + 2) / 4;
139 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1);
141 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx, dx);
145 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) {
147 int i, tx, ty, th, len, olen;
150 if(!drw || !drw->theme)
152 XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->fg->rgb : drw->theme->bg->rgb);
153 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
154 if(!text || !drw->font)
157 drw_font_getexts(drw->font, text, olen, &tex);
158 th = drw->font->ascent + drw->font->descent;
159 ty = y + (h / 2) - (th / 2) + drw->font->ascent;
161 /* shorten text if necessary */
162 for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
163 drw_font_getexts(drw->font, text, len, &tex);
166 memcpy(buf, text, len);
168 for(i = len; i && i > len - 3; buf[--i] = '.');
169 XSetForeground(drw->dpy, drw->gc, invert ? drw->theme->bg->rgb : drw->theme->fg->rgb);
171 XmbDrawString(drw->dpy, drw->drawable, drw->font->set, drw->gc, tx, ty, buf, len);
173 XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len);
177 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
180 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
181 XSync(drw->dpy, False);
186 drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) {
192 XmbTextExtents(font->set, text, len, NULL, &r);
197 tex->h = font->ascent + font->descent;
198 tex->w = XTextWidth(font->xfont, text, len);
203 drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) {
208 drw_font_getexts(font, text, len, &tex);
213 drw_cur_create(Drw *drw, int shape) {
214 Cur *cur = (Cur *)calloc(1, sizeof(Cur));
218 cur->cursor = XCreateFontCursor(drw->dpy, shape);
223 drw_cur_free(Drw *drw, Cur *cursor) {
226 XFreeCursor(drw->dpy, cursor->cursor);