Xinqi Bao's Git

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