typo fixes
[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         initfont(font);
51
52         /* pixmap */
53         dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
54         dc.gc = XCreateGC(dpy, parent, 0, NULL);
55         XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
56         if(!dc.font.set)
57                 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
58 }
59
60 void
61 drawtext(const char *text, unsigned long col[ColLast]) {
62         char buf[256];
63         int i, x, y, h, len, olen;
64         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
65
66         XSetForeground(dpy, dc.gc, col[ColBG]);
67         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
68         if(!text)
69                 return;
70         olen = strlen(text);
71         h = dc.font.height;
72         y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent;
73         x = dc.x + (h / 2);
74         /* shorten text if necessary */
75         for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
76         if(!len)
77                 return;
78         memcpy(buf, text, len);
79         if(len < olen)
80                 for(i = len; i && i > len - 3; buf[--i] = '.');
81         XSetForeground(dpy, dc.gc, col[ColFG]);
82         if(dc.font.set)
83                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
84         else
85                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
86 }
87
88 unsigned long
89 getcolor(const char *colstr) {
90         Colormap cmap = DefaultColormap(dpy, screen);
91         XColor color;
92
93         if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
94                 eprint("drawtext: cannot allocate color '%s'\n", colstr);
95         return color.pixel;
96 }
97
98 void
99 initfont(const char *fontstr) {
100         char *def, **missing = NULL;
101         int i, n;
102
103         if(!fontstr || fontstr[0] == '\0')
104                 eprint("drawtext: cannot load font: '%s'\n", fontstr);
105         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
106         if(missing)
107                 XFreeStringList(missing);
108         if(dc.font.set) {
109                 XFontStruct **xfonts;
110                 char **font_names;
111                 dc.font.ascent = dc.font.descent = 0;
112                 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
113                 for(i = 0; i < n; i++) {
114                         dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
115                         dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
116                         xfonts++;
117                 }
118         }
119         else {
120                 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
121                 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
122                         eprint("drawtext: cannot load font: '%s'\n", fontstr);
123                 dc.font.ascent = dc.font.xfont->ascent;
124                 dc.font.descent = dc.font.xfont->descent;
125         }
126         dc.font.height = dc.font.ascent + dc.font.descent;
127 }
128
129 int
130 textnw(const char *text, unsigned int len) {
131         XRectangle r;
132
133         if(dc.font.set) {
134                 XmbTextExtents(dc.font.set, text, len, NULL, &r);
135                 return r.width;
136         }
137         return XTextWidth(dc.font.xfont, text, len);
138 }
139
140 int
141 textw(const char *text) {
142         return textnw(text, strlen(text)) + dc.font.height;
143 }