Xinqi Bao's Git

95ff072a9a0cee5329ba8066d05a11a6cd60a69e
[dmenu.git] / draw.c
1 /* See LICENSE file for copyright and license details. */
2 #include <locale.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <X11/Xlib.h>
8 #include "draw.h"
9
10 #define MAX(a, b) ((a) > (b) ? (a) : (b))
11 #define MIN(a, b) ((a) < (b) ? (a) : (b))
12 #define DEFFONT "fixed"
13
14 static Bool loadfont(DC *dc, const char *fontstr);
15
16 void
17 drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
18 XRectangle r = { dc->x + x, dc->y + y, w, h };
19
20 if(!fill) {
21 r.width -= 1;
22 r.height -= 1;
23 }
24 XSetForeground(dc->dpy, dc->gc, color);
25 (fill ? XFillRectangles : XDrawRectangles)(dc->dpy, dc->canvas, dc->gc, &r, 1);
26 }
27
28 void
29 drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
30 char buf[BUFSIZ];
31 size_t mn, n = strlen(text);
32
33 /* shorten text if necessary */
34 for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--)
35 if(mn == 0)
36 return;
37 memcpy(buf, text, mn);
38 if(mn < n)
39 for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
40
41 drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
42 drawtextn(dc, buf, mn, col);
43 }
44
45 void
46 drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
47 int x = dc->x + dc->font.height/2;
48 int y = dc->y + dc->font.ascent+1;
49
50 XSetForeground(dc->dpy, dc->gc, FG(dc, col));
51 if(dc->font.set)
52 XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
53 else {
54 XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
55 XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
56 }
57 }
58
59 void
60 eprintf(const char *fmt, ...) {
61 va_list ap;
62
63 va_start(ap, fmt);
64 vfprintf(stderr, fmt, ap);
65 va_end(ap);
66
67 if(fmt[strlen(fmt)-1] == ':') {
68 fputc(' ', stderr);
69 perror(NULL);
70 }
71 exit(EXIT_FAILURE);
72 }
73
74 void
75 freedc(DC *dc) {
76 if(dc->font.set)
77 XFreeFontSet(dc->dpy, dc->font.set);
78 if(dc->font.xfont)
79 XFreeFont(dc->dpy, dc->font.xfont);
80 if(dc->canvas)
81 XFreePixmap(dc->dpy, dc->canvas);
82 XFreeGC(dc->dpy, dc->gc);
83 XCloseDisplay(dc->dpy);
84 free(dc);
85 }
86
87 unsigned long
88 getcolor(DC *dc, const char *colstr) {
89 Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
90 XColor color;
91
92 if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
93 eprintf("cannot allocate color '%s'\n", colstr);
94 return color.pixel;
95 }
96
97 DC *
98 initdc(void) {
99 DC *dc;
100
101 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
102 fprintf(stderr, "no locale support\n");
103 if(!(dc = calloc(1, sizeof *dc)))
104 eprintf("cannot malloc %u bytes:", sizeof *dc);
105 if(!(dc->dpy = XOpenDisplay(NULL)))
106 eprintf("cannot open display\n");
107
108 dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
109 XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
110 return dc;
111 }
112
113 void
114 initfont(DC *dc, const char *fontstr) {
115 if(!loadfont(dc, fontstr ? fontstr : DEFFONT)) {
116 if(fontstr != NULL)
117 fprintf(stderr, "cannot load font '%s'\n", fontstr);
118 if(fontstr == NULL || !loadfont(dc, DEFFONT))
119 eprintf("cannot load font '%s'\n", DEFFONT);
120 }
121 dc->font.height = dc->font.ascent + dc->font.descent;
122 }
123
124 Bool
125 loadfont(DC *dc, const char *fontstr) {
126 char *def, **missing;
127 int i, n;
128
129 if(!*fontstr)
130 return False;
131 if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
132 char **names;
133 XFontStruct **xfonts;
134
135 n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
136 for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
137 dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
138 dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
139 }
140 }
141 else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
142 dc->font.ascent = dc->font.xfont->ascent;
143 dc->font.descent = dc->font.xfont->descent;
144 }
145 if(missing)
146 XFreeStringList(missing);
147 return (dc->font.set || dc->font.xfont);
148 }
149
150 void
151 mapdc(DC *dc, Window win, unsigned int w, unsigned int h) {
152 XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
153 }
154
155 void
156 resizedc(DC *dc, unsigned int w, unsigned int h) {
157 if(dc->canvas)
158 XFreePixmap(dc->dpy, dc->canvas);
159
160 dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
161 DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
162 dc->w = w;
163 dc->h = h;
164 }
165
166 int
167 textnw(DC *dc, const char *text, size_t len) {
168 if(dc->font.set) {
169 XRectangle r;
170
171 XmbTextExtents(dc->font.set, text, len, NULL, &r);
172 return r.width;
173 }
174 return XTextWidth(dc->font.xfont, text, len);
175 }
176
177 int
178 textw(DC *dc, const char *text) {
179 return textnw(dc, text, strlen(text)) + dc->font.height;
180 }