Xinqi Bao's Git

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