JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
shut up about deprecated Xlib functions
[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 win, unsigned int w, unsigned int h) {
12         Drw *drw = (Drw *)calloc(1, sizeof(Drw));
13         drw->dpy = dpy;
14         drw->screen = screen;
15         drw->win = win;
16         drw->w = w;
17         drw->h = h;
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);
21         return drw;
22 }
23
24 void
25 drw_resize(Drw *drw, unsigned int w, unsigned int h) {
26         if(!drw)
27                 return;
28         drw->w = w;
29         drw->h = h;
30         XFreePixmap(drw->dpy, drw->drwable);
31         drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen));
32 }
33
34 void
35 drw_free(Drw *drw) {
36         XFreePixmap(drw->dpy, drw->drwable);
37         XFreeGC(drw->dpy, drw->gc);
38         free(drw);
39 }
40
41 Fnt *
42 drw_font_create(Drw *drw, const char *fontname) {
43         Fnt *font;
44         char *def, **missing;
45         int n;
46
47         if(!drw)
48                 return NULL;
49         font = (Fnt *)calloc(1, sizeof(Fnt));
50         font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def);
51         if(missing) {
52                 while(n--)
53                         fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
54                 XFreeStringList(missing);
55         }
56         if(font->set) {
57                 XFontStruct **xfonts;
58                 char **font_names;
59                 XExtentsOfFontSet(font->set);
60                 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
61                 while(n--) {
62                         font->ascent = MAX(font->ascent, (*xfonts)->ascent);
63                         font->descent = MAX(font->descent,(*xfonts)->descent);
64                         xfonts++;
65                 }
66         }
67         else {
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;
73         }
74         font->h = font->ascent + font->descent;
75         return font;
76 }
77
78 void
79 drw_font_free(Drw *drw, Fnt *font) {
80         if(!drw || !font)
81                 return;
82         if(font->set)
83                 XFreeFontSet(drw->dpy, font->set);
84         else
85                 XFreeFont(drw->dpy, font->xfont);
86         free(font);
87 }
88
89 Clr *
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);
93         XColor color;
94
95         if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
96                 die("error, cannot allocate color '%s'\n", clrname);
97         clr->rgb = color.pixel;
98         return clr;
99 }
100
101 void
102 drw_clr_free(Drw *drw, Clr *clr) {
103         if(!clr)
104                 return;
105         free(clr);
106 }
107
108 void
109 drw_setfont(Drw *drw, Fnt *font) {
110         if(!drw)
111                 return;
112         drw->font = font;
113 }
114
115 void
116 drw_setfg(Drw *drw, Clr *clr) {
117         if(!drw) 
118                 return;
119         drw->fg = clr;
120 }
121
122 void
123 drw_setbg(Drw *drw, Clr *clr) {
124         if(!drw)
125                 return;
126         drw->bg = clr;
127 }
128
129 void
130 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
131         int dx;
132
133         if(!drw || !drw->font || !drw->fg || !drw->bg)
134                 return;
135         XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
136         dx = (drw->font->ascent + drw->font->descent + 2) / 4;
137         if(filled)
138                 XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1);
139         else if(empty)
140                 XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx);
141 }
142
143 void
144 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
145         char buf[256];
146         int i, tx, ty, len, olen;
147         Extnts tex;
148
149         if(!drw || !drw->fg || !drw->bg)
150                 return;
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)
154                 return;
155         olen = strlen(text);
156         drw_getexts(drw, text, olen, &tex);
157         ty = y + (h / 2) - tex.yOff;
158         tx = x + tex.xOff;
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);
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->bg->rgb : drw->fg->rgb);
168         if(drw->font->set)
169                 XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len);
170         else
171                 XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len);
172 }
173
174 void
175 drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) {
176         if(!drw)
177                 return;
178         XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y);
179         XSync(drw->dpy, False);
180 }
181
182
183 void
184 drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) {
185         XRectangle r;
186
187         if(!drw || !drw->font || !text)
188                 return;
189         if(drw->font->set) {
190                 XmbTextExtents(drw->font->set, text, len, NULL, &r);
191                 tex->xOff = r.x;
192                 tex->yOff = r.y;
193                 tex->w = r.width;
194                 tex->h = r.height;
195         }
196         else {
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;
201         }
202 }