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