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