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