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