932b2f659029b61b7135b8fc3679fce8fbd57b23
[dmenu.git] / draw.c
1 /* See LICENSE file for copyright and license details. */
2
3 /* enums */
4 enum { ColFG, ColBG, ColLast };
5
6 /* typedefs */
7 typedef struct {
8         int x, y, w, h;
9         unsigned long norm[ColLast];
10         unsigned long sel[ColLast];
11         Drawable drawable;
12         GC gc;
13         struct {
14                 XFontStruct *xfont;
15                 XFontSet set;
16                 int ascent;
17                 int descent;
18                 int height;
19         } font;
20 } DC; /* draw context */
21
22 /* forward declarations */
23 static void dccleanup(void);
24 static void dcsetup(void);
25 static void drawtext(const char *text, unsigned long col[ColLast]);
26 static unsigned long getcolor(const char *colstr);
27 static void initfont(const char *fontstr);
28 static int textnw(const char *text, unsigned int len);
29 static int textw(const char *text);
30
31 static DC dc;
32
33 void
34 dccleanup(void) {
35         if(dc.font.set)
36                 XFreeFontSet(dpy, dc.font.set);
37         else
38                 XFreeFont(dpy, dc.font.xfont);
39         XFreePixmap(dpy, dc.drawable);
40         XFreeGC(dpy, dc.gc);
41 }
42
43 void
44 dcsetup(void) {
45         /* style */
46         dc.norm[ColBG] = getcolor(normbgcolor);
47         dc.norm[ColFG] = getcolor(normfgcolor);
48         dc.sel[ColBG] = getcolor(selbgcolor);
49         dc.sel[ColFG] = getcolor(selfgcolor);
50
51         /* pixmap */
52         dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
53         dc.gc = XCreateGC(dpy, parent, 0, NULL);
54         XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
55         if(!dc.font.set)
56                 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
57 }
58
59 void
60 drawtext(const char *text, unsigned long col[ColLast]) {
61         char buf[256];
62         int i, x, y, h, len, olen;
63         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
64
65         XSetForeground(dpy, dc.gc, col[ColBG]);
66         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
67         if(!text)
68                 return;
69         olen = strlen(text);
70         h = dc.font.height;
71         y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent;
72         x = dc.x + (h / 2);
73         /* shorten text if necessary */
74         for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
75         if(!len)
76                 return;
77         memcpy(buf, text, len);
78         if(len < olen)
79                 for(i = len; i && i > len - 3; buf[--i] = '.');
80         XSetForeground(dpy, dc.gc, col[ColFG]);
81         if(dc.font.set)
82                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
83         else
84                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
85 }
86
87 unsigned long
88 getcolor(const char *colstr) {
89         Colormap cmap = DefaultColormap(dpy, screen);
90         XColor color;
91
92         if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
93                 eprint("drawtext: cannot allocate color '%s'\n", colstr);
94         return color.pixel;
95 }
96
97 void
98 initfont(const char *fontstr) {
99         char *def, **missing = NULL;
100         int i, n;
101
102         if(!fontstr || fontstr[0] == '\0')
103                 eprint("drawtext: cannot load font: '%s'\n", fontstr);
104         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
105         if(missing)
106                 XFreeStringList(missing);
107         if(dc.font.set) {
108                 XFontStruct **xfonts;
109                 char **font_names;
110                 dc.font.ascent = dc.font.descent = 0;
111                 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
112                 for(i = 0; i < n; i++) {
113                         dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
114                         dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
115                         xfonts++;
116                 }
117         }
118         else {
119                 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
120                 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
121                         eprint("drawtext: cannot load font: '%s'\n", fontstr);
122                 dc.font.ascent = dc.font.xfont->ascent;
123                 dc.font.descent = dc.font.xfont->descent;
124         }
125         dc.font.height = dc.font.ascent + dc.font.descent;
126 }
127
128 int
129 textnw(const char *text, unsigned int len) {
130         XRectangle r;
131
132         if(dc.font.set) {
133                 XmbTextExtents(dc.font.set, text, len, NULL, &r);
134                 return r.width;
135         }
136         return XTextWidth(dc.font.xfont, text, len);
137 }
138
139 int
140 textw(const char *text) {
141         return textnw(text, strlen(text)) + dc.font.height;
142 }