]> git.armaanb.net Git - dmenu.git/blob - draw.c
Added tag 4.2.1 for changeset abb6579a324f
[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
29 void
30 drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
31         char buf[256];
32         size_t n, mn;
33
34         /* shorten text if necessary */
35         n = strlen(text);
36         for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) > dc->w - dc->font.height/2; mn--)
37                 if(mn == 0)
38                         return;
39         memcpy(buf, text, mn);
40         if(mn < n)
41                 for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
42
43         drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
44         drawtextn(dc, buf, mn, col);
45 }
46
47 void
48 drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
49         int x, y;
50
51         x = dc->x + dc->font.height/2;
52         y = dc->y + dc->font.ascent+1;
53
54         XSetForeground(dc->dpy, dc->gc, FG(dc, col));
55         if(dc->font.set)
56                 XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
57         else {
58                 XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
59                 XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
60         }
61 }
62
63 void
64 eprintf(const char *fmt, ...) {
65         va_list ap;
66
67         fprintf(stderr, "%s: ", progname);
68         va_start(ap, fmt);
69         vfprintf(stderr, fmt, ap);
70         va_end(ap);
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                 weprintf("no locale support\n");
103         if(!(dc = malloc(sizeof *dc)))
104                 eprintf("cannot malloc %u bytes\n", 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         dc->font.xfont = NULL;
111         dc->font.set = NULL;
112         dc->canvas = None;
113         return dc;
114 }
115
116 void
117 initfont(DC *dc, const char *fontstr) {
118         if(!loadfont(dc, fontstr ? fontstr : DEFFONT)) {
119                 if(fontstr != NULL)
120                         weprintf("cannot load font '%s'\n", fontstr);
121                 if(fontstr == NULL || !loadfont(dc, DEFFONT))
122                         eprintf("cannot load font '%s'\n", DEFFONT);
123         }
124         dc->font.height = dc->font.ascent + dc->font.descent;
125 }
126
127 Bool
128 loadfont(DC *dc, const char *fontstr) {
129         char *def, **missing;
130         int i, n;
131
132         if(!*fontstr)
133                 return False;
134         if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
135                 char **names;
136                 XFontStruct **xfonts;
137
138                 n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
139                 for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
140                         dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
141                         dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
142                 }
143         }
144         else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
145                 dc->font.ascent = dc->font.xfont->ascent;
146                 dc->font.descent = dc->font.xfont->descent;
147         }
148         if(missing)
149                 XFreeStringList(missing);
150         return (dc->font.set || dc->font.xfont);
151 }
152
153 void
154 mapdc(DC *dc, Window win, unsigned int w, unsigned int h) {
155         XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
156 }
157
158 void
159 resizedc(DC *dc, unsigned int w, unsigned int h) {
160         if(dc->canvas)
161                 XFreePixmap(dc->dpy, dc->canvas);
162         dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
163                                    DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
164         dc->x = dc->y = 0;
165         dc->w = w;
166         dc->h = h;
167         dc->invert = False;
168 }
169
170 int
171 textnw(DC *dc, const char *text, size_t len) {
172         if(dc->font.set) {
173                 XRectangle r;
174
175                 XmbTextExtents(dc->font.set, text, len, NULL, &r);
176                 return r.width;
177         }
178         return XTextWidth(dc->font.xfont, text, len);
179 }
180
181 int
182 textw(DC *dc, const char *text) {
183         return textnw(dc, text, strlen(text)) + dc->font.height;
184 }
185
186 void
187 weprintf(const char *fmt, ...) {
188         va_list ap;
189
190         fprintf(stderr, "%s: warning: ", progname);
191         va_start(ap, fmt);
192         vfprintf(stderr, fmt, ap);
193         va_end(ap);
194 }