JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fdb36d70a521c358bf907970e3ecf0aaa9aef7f0
[dwm.git] / draw.c
1 /*
2  * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5 #include "dwm.h"
6 #include <stdio.h>
7 #include <string.h>
8 #include <X11/Xlocale.h>
9
10 /* static */
11
12 static unsigned int
13 textnw(const char *text, unsigned int len)
14 {
15         XRectangle r;
16
17         if(dc.font.set) {
18                 XmbTextExtents(dc.font.set, text, len, NULL, &r);
19                 return r.width;
20         }
21         return XTextWidth(dc.font.xfont, text, len);
22 }
23
24 static void
25 drawtext(const char *text, Bool invert, Bool highlight)
26 {
27         int x, y, w, h;
28         static char buf[256];
29         unsigned int len, olen;
30         XGCValues gcv;
31         XPoint points[5];
32         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
33
34         XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
35         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
36         points[0].x = dc.x;
37         points[0].y = dc.y;
38         points[1].x = dc.w - 1;
39         points[1].y = 0;
40         points[2].x = 0;
41         points[2].y = dc.h - 1;
42         points[3].x = -(dc.w - 1);
43         points[3].y = 0;
44         points[4].x = 0;
45         points[4].y = -(dc.h - 1);
46         XSetForeground(dpy, dc.gc, dc.border);
47         XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
48
49         if(!text)
50                 return;
51
52         w = 0;
53         olen = len = strlen(text);
54         if(len >= sizeof(buf))
55                 len = sizeof(buf) - 1;
56         memcpy(buf, text, len);
57         buf[len] = 0;
58
59         h = dc.font.ascent + dc.font.descent;
60         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
61         x = dc.x + (h / 2);
62
63         /* shorten text if necessary */
64         while(len && (w = textnw(buf, len)) > dc.w - h)
65                 buf[--len] = 0;
66         if(len < olen) {
67                 if(len > 1)
68                         buf[len - 1] = '.';
69                 if(len > 2)
70                         buf[len - 2] = '.';
71                 if(len > 3)
72                         buf[len - 3] = '.';
73         }
74
75         if(w > dc.w)
76                 return; /* too long */
77         gcv.foreground = invert ? dc.bg : dc.fg;
78         gcv.background = invert ? dc.fg : dc.bg;
79         if(dc.font.set) {
80                 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
81                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
82         }
83         else {
84                 gcv.font = dc.font.xfont->fid;
85                 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
86                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
87         }
88         if(highlight) {
89                 r.x = dc.x + 2;
90                 r.y = dc.y + 2;
91                 r.width = r.height = 3;
92                 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
93         }
94 }
95
96 /* extern */
97
98 void
99 drawall()
100 {
101         Client *c;
102
103         for(c = clients; c; c = getnext(c->next))
104                 drawtitle(c);
105         drawstatus();
106 }
107
108 void
109 drawstatus()
110 {
111         int i, x;
112         Bool istile = arrange == dotile;
113
114         dc.x = dc.y = 0;
115         dc.w = bw;
116         drawtext(NULL, !istile, False);
117
118         dc.w = 0;
119         for(i = 0; i < ntags; i++) {
120                 dc.x += dc.w;
121                 dc.w = textw(tags[i]);
122                 if(istile)
123                         drawtext(tags[i], seltag[i], sel && sel->tags[i]);
124                 else
125                         drawtext(tags[i], !seltag[i], sel && sel->tags[i]);
126         }
127         x = dc.x + dc.w;
128         dc.w = textw(stext);
129         dc.x = bx + bw - dc.w;
130         if(dc.x < x) {
131                 dc.x = x;
132                 dc.w = bw - x;
133         }
134         drawtext(stext, !istile, False);
135
136         if(sel && ((dc.w = dc.x - x) > bh)) {
137                 dc.x = x;
138                 drawtext(sel->name, istile, False);
139         }
140         XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
141         XSync(dpy, False);
142 }
143
144 void
145 drawtitle(Client *c)
146 {
147         int i;
148         Bool istile = arrange == dotile;
149
150         if(c == sel && issel) {
151                 drawstatus();
152                 XUnmapWindow(dpy, c->twin);
153                 XSetWindowBorder(dpy, c->win, dc.fg);
154                 return;
155         }
156
157         XSetWindowBorder(dpy, c->win, dc.bg);
158         XMapWindow(dpy, c->twin);
159         dc.x = dc.y = 0;
160         dc.w = c->tw;
161         drawtext(c->name, !istile, False);
162         XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
163         XSync(dpy, False);
164 }
165
166 unsigned long
167 getcolor(const char *colstr)
168 {
169         Colormap cmap = DefaultColormap(dpy, screen);
170         XColor color;
171
172         XAllocNamedColor(dpy, cmap, colstr, &color, &color);
173         return color.pixel;
174 }
175
176 void
177 setfont(const char *fontstr)
178 {
179         char **missing, *def;
180         int i, n;
181
182         missing = NULL;
183         setlocale(LC_ALL, "");
184         if(dc.font.set)
185                 XFreeFontSet(dpy, dc.font.set);
186         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
187         if(missing) {
188                 while(n--)
189                         fprintf(stderr, "missing fontset: %s\n", missing[n]);
190                 XFreeStringList(missing);
191                 if(dc.font.set) {
192                         XFreeFontSet(dpy, dc.font.set);
193                         dc.font.set = NULL;
194                 }
195         }
196         if(dc.font.set) {
197                 XFontSetExtents *font_extents;
198                 XFontStruct **xfonts;
199                 char **font_names;
200
201                 dc.font.ascent = dc.font.descent = 0;
202                 font_extents = XExtentsOfFontSet(dc.font.set);
203                 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
204                 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
205                         if(dc.font.ascent < (*xfonts)->ascent)
206                                 dc.font.ascent = (*xfonts)->ascent;
207                         if(dc.font.descent < (*xfonts)->descent)
208                                 dc.font.descent = (*xfonts)->descent;
209                         xfonts++;
210                 }
211         }
212         else {
213                 if(dc.font.xfont)
214                         XFreeFont(dpy, dc.font.xfont);
215                 dc.font.xfont = NULL;
216                 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
217                 if (!dc.font.xfont)
218                         dc.font.xfont = XLoadQueryFont(dpy, "fixed");
219                 if (!dc.font.xfont)
220                         eprint("error, cannot init 'fixed' font\n");
221                 dc.font.ascent = dc.font.xfont->ascent;
222                 dc.font.descent = dc.font.xfont->descent;
223         }
224         dc.font.height = dc.font.ascent + dc.font.descent;
225 }
226
227 unsigned int
228 textw(const char *text)
229 {
230         return textnw(text, strlen(text)) + dc.font.height;
231 }