JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
s/TILED/TILE/g
[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, unsigned long col[ColLast], Bool highlight)
26 {
27         int x, y, w, h;
28         static char buf[256];
29         unsigned int len, olen;
30         XGCValues gcv;
31         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
32
33         XSetForeground(dpy, dc.gc, col[ColBG]);
34         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
35
36         if(!text)
37                 return;
38
39         w = 0;
40         olen = len = strlen(text);
41         if(len >= sizeof(buf))
42                 len = sizeof(buf) - 1;
43         memcpy(buf, text, len);
44         buf[len] = 0;
45
46         h = dc.font.ascent + dc.font.descent;
47         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
48         x = dc.x + (h / 2);
49
50         /* shorten text if necessary */
51         while(len && (w = textnw(buf, len)) > dc.w - h)
52                 buf[--len] = 0;
53         if(len < olen) {
54                 if(len > 1)
55                         buf[len - 1] = '.';
56                 if(len > 2)
57                         buf[len - 2] = '.';
58                 if(len > 3)
59                         buf[len - 3] = '.';
60         }
61
62         if(w > dc.w)
63                 return; /* too long */
64         gcv.foreground = col[ColFG];
65         if(dc.font.set) {
66                 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
67                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
68         }
69         else {
70                 gcv.font = dc.font.xfont->fid;
71                 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
72                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
73         }
74         if(highlight) {
75                 r.x = dc.x + 2;
76                 r.y = dc.y + 2;
77                 r.width = r.height = 3;
78                 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
79         }
80 }
81
82 /* extern */
83
84 void
85 drawall()
86 {
87         Client *c;
88
89         for(c = clients; c; c = getnext(c->next))
90                 drawtitle(c);
91         drawstatus();
92 }
93
94 void
95 drawstatus()
96 {
97         int i, x;
98
99         dc.x = dc.y = 0;
100         dc.w = bw;
101
102         drawtext(arrange == dotile ? TILESYMBOL : FLOATSYMBOL, dc.status, False);
103         dc.w = modew;
104         for(i = 0; i < ntags; i++) {
105                 dc.x += dc.w;
106                 dc.w = textw(tags[i]);
107                 if(seltag[i])
108                         drawtext(tags[i], dc.sel, sel && sel->tags[i]);
109                 else
110                         drawtext(tags[i], dc.norm, sel && sel->tags[i]);
111         }
112         x = dc.x + dc.w + 1;
113         dc.w = textw(stext);
114         dc.x = bx + bw - dc.w;
115         if(dc.x < x) {
116                 dc.x = x;
117                 dc.w = bw - x;
118         }
119         drawtext(stext, dc.status, False);
120
121         if(sel && ((dc.w = dc.x - x) > bh)) {
122                 dc.x = x;
123                 drawtext(sel->name, dc.sel, False);
124         }
125         XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
126         XSync(dpy, False);
127 }
128
129 void
130 drawtitle(Client *c)
131 {
132         int i;
133
134         if(c == sel && issel) {
135                 drawstatus();
136                 XUnmapWindow(dpy, c->twin);
137                 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
138                 return;
139         }
140
141         XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
142         XMapWindow(dpy, c->twin);
143         dc.x = dc.y = 0;
144         dc.w = c->tw;
145         drawtext(c->name, dc.norm, False);
146         XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
147         XSync(dpy, False);
148 }
149
150 unsigned long
151 getcolor(const char *colstr)
152 {
153         Colormap cmap = DefaultColormap(dpy, screen);
154         XColor color;
155
156         XAllocNamedColor(dpy, cmap, colstr, &color, &color);
157         return color.pixel;
158 }
159
160 void
161 setfont(const char *fontstr)
162 {
163         char **missing, *def;
164         int i, n;
165
166         missing = NULL;
167         setlocale(LC_ALL, "");
168         if(dc.font.set)
169                 XFreeFontSet(dpy, dc.font.set);
170         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
171         if(missing) {
172                 while(n--)
173                         fprintf(stderr, "missing fontset: %s\n", missing[n]);
174                 XFreeStringList(missing);
175                 if(dc.font.set) {
176                         XFreeFontSet(dpy, dc.font.set);
177                         dc.font.set = NULL;
178                 }
179         }
180         if(dc.font.set) {
181                 XFontSetExtents *font_extents;
182                 XFontStruct **xfonts;
183                 char **font_names;
184
185                 dc.font.ascent = dc.font.descent = 0;
186                 font_extents = XExtentsOfFontSet(dc.font.set);
187                 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
188                 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
189                         if(dc.font.ascent < (*xfonts)->ascent)
190                                 dc.font.ascent = (*xfonts)->ascent;
191                         if(dc.font.descent < (*xfonts)->descent)
192                                 dc.font.descent = (*xfonts)->descent;
193                         xfonts++;
194                 }
195         }
196         else {
197                 if(dc.font.xfont)
198                         XFreeFont(dpy, dc.font.xfont);
199                 dc.font.xfont = NULL;
200                 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
201                 if (!dc.font.xfont)
202                         dc.font.xfont = XLoadQueryFont(dpy, "fixed");
203                 if (!dc.font.xfont)
204                         eprint("error, cannot init 'fixed' font\n");
205                 dc.font.ascent = dc.font.xfont->ascent;
206                 dc.font.descent = dc.font.xfont->descent;
207         }
208         dc.font.height = dc.font.ascent + dc.font.descent;
209 }
210
211 unsigned int
212 textw(const char *text)
213 {
214         return textnw(text, strlen(text)) + dc.font.height;
215 }