applied Julian's enum approach,
[dwm.git] / drw.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <X11/Xlib.h>
6
7 #include "drw.h"
8 #include "util.h"
9
10 Drw *
11 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) {
12 Drw *drw = (Drw *)calloc(1, sizeof(Drw));
13 if(!drw)
14 return NULL;
15 drw->dpy = dpy;
16 drw->screen = screen;
17 drw->root = root;
18 drw->w = w;
19 drw->h = h;
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);
23 return drw;
24 }
25
26 void
27 drw_resize(Drw *drw, unsigned int w, unsigned int h) {
28 if(!drw)
29 return;
30 drw->w = w;
31 drw->h = 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));
35 }
36
37 void
38 drw_free(Drw *drw) {
39 XFreePixmap(drw->dpy, drw->drawable);
40 XFreeGC(drw->dpy, drw->gc);
41 free(drw);
42 }
43
44 Fnt *
45 drw_font_create(Display *dpy, const char *fontname) {
46 Fnt *font;
47 char *def, **missing;
48 int n;
49
50 font = (Fnt *)calloc(1, sizeof(Fnt));
51 if(!font)
52 return NULL;
53 font->set = XCreateFontSet(dpy, fontname, &missing, &n, &def);
54 if(missing) {
55 while(n--)
56 fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
57 XFreeStringList(missing);
58 }
59 if(font->set) {
60 XFontStruct **xfonts;
61 char **font_names;
62 XExtentsOfFontSet(font->set);
63 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
64 while(n--) {
65 font->ascent = MAX(font->ascent, (*xfonts)->ascent);
66 font->descent = MAX(font->descent,(*xfonts)->descent);
67 xfonts++;
68 }
69 }
70 else {
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;
76 }
77 font->h = font->ascent + font->descent;
78 return font;
79 }
80
81 void
82 drw_font_free(Display *dpy, Fnt *font) {
83 if(!font)
84 return;
85 if(font->set)
86 XFreeFontSet(dpy, font->set);
87 else
88 XFreeFont(dpy, font->xfont);
89 free(font);
90 }
91
92 Clr *
93 drw_clr_create(Drw *drw, const char *clrname) {
94 Clr *clr;
95 Colormap cmap;
96 XColor color;
97
98 if(!drw)
99 return NULL;
100 clr = (Clr *)calloc(1, sizeof(Clr));
101 if(!clr)
102 return NULL;
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;
107 return clr;
108 }
109
110 void
111 drw_clr_free(Clr *clr) {
112 if(clr)
113 free(clr);
114 }
115
116 void
117 drw_setfont(Drw *drw, Fnt *font) {
118 if(drw)
119 drw->font = font;
120 }
121
122 void
123 drw_setscheme(Drw *drw, ClrScheme *scheme) {
124 if(drw && scheme)
125 drw->scheme = scheme;
126 }
127
128 void
129 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) {
130 int dx;
131
132 if(!drw || !drw->font || !drw->scheme)
133 return;
134 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->rgb : drw->scheme->fg->rgb);
135 dx = (drw->font->ascent + drw->font->descent + 2) / 4;
136 if(filled)
137 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1);
138 else if(empty)
139 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx, dx);
140 }
141
142 void
143 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) {
144 char buf[256];
145 int i, tx, ty, th, len, olen;
146 Extnts tex;
147
148 if(!drw || !drw->scheme)
149 return;
150 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg->rgb : drw->scheme->bg->rgb);
151 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
152 if(!text || !drw->font)
153 return;
154 olen = strlen(text);
155 drw_font_getexts(drw->font, text, olen, &tex);
156 th = drw->font->ascent + drw->font->descent;
157 ty = y + (h / 2) - (th / 2) + drw->font->ascent;
158 tx = x + (h / 2);
159 /* shorten text if necessary */
160 for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
161 drw_font_getexts(drw->font, text, len, &tex);
162 if(!len)
163 return;
164 memcpy(buf, text, len);
165 if(len < olen)
166 for(i = len; i && i > len - 3; buf[--i] = '.');
167 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->rgb : drw->scheme->fg->rgb);
168 if(drw->font->set)
169 XmbDrawString(drw->dpy, drw->drawable, drw->font->set, drw->gc, tx, ty, buf, len);
170 else
171 XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len);
172 }
173
174 void
175 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
176 if(!drw)
177 return;
178 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
179 XSync(drw->dpy, False);
180 }
181
182
183 void
184 drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) {
185 XRectangle r;
186
187 if(!font || !text)
188 return;
189 if(font->set) {
190 XmbTextExtents(font->set, text, len, NULL, &r);
191 tex->w = r.width;
192 tex->h = r.height;
193 }
194 else {
195 tex->h = font->ascent + font->descent;
196 tex->w = XTextWidth(font->xfont, text, len);
197 }
198 }
199
200 unsigned int
201 drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) {
202 Extnts tex;
203
204 if(!font)
205 return -1;
206 drw_font_getexts(font, text, len, &tex);
207 return tex.w;
208 }
209
210 Cur *
211 drw_cur_create(Drw *drw, int shape) {
212 Cur *cur = (Cur *)calloc(1, sizeof(Cur));
213
214 if(!drw || !cur)
215 return NULL;
216 cur->cursor = XCreateFontCursor(drw->dpy, shape);
217 return cur;
218 }
219
220 void
221 drw_cur_free(Drw *drw, Cur *cursor) {
222 if(!drw || !cursor)
223 return;
224 XFreeCursor(drw->dpy, cursor->cursor);
225 free(cursor);
226 }