Xinqi Bao's Git

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