Xinqi Bao's Git

9142905b8ecd20cf1a12f3321fe2552dabb50a95
[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 const char *progname;
18
19 void
20 cleanupdraw(DC *dc) {
21 if(dc->font.set)
22 XFreeFontSet(dc->dpy, dc->font.set);
23 else
24 XFreeFont(dc->dpy, dc->font.xfont);
25 XFreePixmap(dc->dpy, dc->drawable);
26 XFreeGC(dc->dpy, dc->gc);
27 }
28
29 void
30 setupdraw(DC *dc, Window w) {
31 XWindowAttributes wa;
32
33 XGetWindowAttributes(dc->dpy, w, &wa);
34 dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height,
35 DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
36 dc->gc = XCreateGC(dc->dpy, w, 0, NULL);
37 XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
38 if(!dc->font.set)
39 XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
40 }
41
42 void
43 drawtext(DC *dc, 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(dc->dpy, dc->gc, col[ColBG]);
49 XFillRectangles(dc->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(dc, 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(dc->dpy, dc->gc, col[ColFG]);
64 if(dc->font.set)
65 XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len);
66 else
67 XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len);
68 }
69
70 void
71 eprint(const char *fmt, ...) {
72 va_list ap;
73
74 fprintf(stderr, "%s: ", progname);
75 va_start(ap, fmt);
76 vfprintf(stderr, fmt, ap);
77 va_end(ap);
78 exit(EXIT_FAILURE);
79 }
80
81 unsigned long
82 getcolor(DC *dc, const char *colstr) {
83 Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
84 XColor color;
85
86 if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
87 eprint("cannot allocate color '%s'\n", colstr);
88 return color.pixel;
89 }
90
91 void
92 initfont(DC *dc, const char *fontstr) {
93 char *def, **missing = NULL;
94 int i, n;
95
96 if(!fontstr || !*fontstr)
97 eprint("cannot load null font\n");
98 dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def);
99 if(missing)
100 XFreeStringList(missing);
101 if(dc->font.set) {
102 XFontStruct **xfonts;
103 char **font_names;
104 dc->font.ascent = dc->font.descent = 0;
105 n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names);
106 for(i = 0; i < n; i++) {
107 dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent);
108 dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent);
109 xfonts++;
110 }
111 }
112 else {
113 if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))
114 && !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed")))
115 eprint("cannot load font '%s'\n", fontstr);
116 dc->font.ascent = dc->font.xfont->ascent;
117 dc->font.descent = dc->font.xfont->descent;
118 }
119 dc->font.height = dc->font.ascent + dc->font.descent;
120 }
121
122 int
123 textnw(DC *dc, const char *text, unsigned int len) {
124 XRectangle r;
125
126 if(dc->font.set) {
127 XmbTextExtents(dc->font.set, text, len, NULL, &r);
128 return r.width;
129 }
130 return XTextWidth(dc->font.xfont, text, len);
131 }
132
133 int
134 textw(DC *dc, const char *text) {
135 return textnw(dc, text, strlen(text)) + dc->font.height;
136 }