JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed XSync handling and finished man page
[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
7 #include <stdio.h>
8 #include <string.h>
9 #include <X11/Xlocale.h>
10
11 /* static functions */
12
13 static void
14 drawborder(void)
15 {
16         XPoint points[5];
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(char *text, unsigned int len)
34 {
35         XRectangle r;
36         if(dc.font.set) {
37                 XmbTextExtents(dc.font.set, text, len, NULL, &r);
38                 return r.width;
39         }
40         return XTextWidth(dc.font.xfont, text, len);
41 }
42
43 static void
44 drawtext(const char *text, Bool invert, Bool border)
45 {
46         int x, y, w, h;
47         unsigned int len;
48         static char buf[256];
49         XGCValues gcv;
50         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
51
52         XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
53         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
54
55         w = 0;
56         if(border)
57                 drawborder();
58
59         if(!text)
60                 return;
61
62         len = strlen(text);
63         if(len >= sizeof(buf))
64                 len = sizeof(buf) - 1;
65         memcpy(buf, text, len);
66         buf[len] = 0;
67
68         h = dc.font.ascent + dc.font.descent;
69         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
70         x = dc.x + (h / 2);
71
72         /* shorten text if necessary */
73         while(len && (w = textnw(buf, len)) > dc.w - h)
74                 buf[--len] = 0;
75
76         if(w > dc.w)
77                 return; /* too long */
78
79         gcv.foreground = invert ? dc.bg : dc.fg;
80         gcv.background = invert ? dc.fg : dc.bg;
81         if(dc.font.set) {
82                 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
83                 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
84                                 x, y, buf, len);
85         }
86         else {
87                 gcv.font = dc.font.xfont->fid;
88                 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
89                 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
90         }
91 }
92
93 /* extern functions */
94
95 void
96 drawall()
97 {
98         Client *c;
99
100         for(c = clients; c; c = getnext(c->next))
101                 drawtitle(c);
102         drawstatus();
103 }
104
105 void
106 drawstatus()
107 {
108         int i;
109         Bool istile = arrange == dotile;
110
111         dc.x = dc.y = 0;
112         dc.w = bw;
113         drawtext(NULL, !istile, False);
114
115         dc.w = 0;
116         for(i = 0; i < TLast; i++) {
117                 dc.x += dc.w;
118                 dc.w = textw(tags[i]);
119                 if(istile)
120                         drawtext(tags[i], (i == tsel), True);
121                 else
122                         drawtext(tags[i], (i != tsel), True);
123         }
124         if(sel) {
125                 dc.x += dc.w;
126                 dc.w = textw(sel->name);
127                 drawtext(sel->name, istile, True);
128         }
129         dc.w = textw(stext);
130         dc.x = bx + bw - dc.w;
131         drawtext(stext, !istile, False);
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(c->tags[i]);
160                         drawtext(c->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         XColor color;
174         Colormap cmap = DefaultColormap(dpy, screen);
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(char *text)
233 {
234         return textnw(text, strlen(text)) + dc.font.height;
235 }