draw.c (view raw)
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
13static void
14drawborder(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
33static unsigned int
34textnw(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
45static void
46drawtext(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
97void
98drawall()
99{
100 Client *c;
101
102 for(c = clients; c; c = getnext(c->next, tsel))
103 drawtitle(c);
104 drawstatus();
105}
106
107void
108drawstatus()
109{
110 int i;
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 if(sel) {
127 dc.x += dc.w;
128 dc.w = textw(sel->name);
129 drawtext(sel->name, istile, True);
130 }
131 dc.w = textw(stext);
132 dc.x = bx + bw - dc.w;
133 drawtext(stext, !istile, False);
134
135 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
136 XSync(dpy, False);
137}
138
139void
140drawtitle(Client *c)
141{
142 int i;
143 Bool istile = arrange == dotile;
144
145 if(c == sel) {
146 drawstatus();
147 XUnmapWindow(dpy, c->title);
148 XSetWindowBorder(dpy, c->win, dc.fg);
149 return;
150 }
151
152 XSetWindowBorder(dpy, c->win, dc.bg);
153 XMapWindow(dpy, c->title);
154
155 dc.x = dc.y = 0;
156
157 dc.w = 0;
158 for(i = 0; i < TLast; i++) {
159 if(c->tags[i]) {
160 dc.x += dc.w;
161 dc.w = textw(c->tags[i]);
162 drawtext(c->tags[i], !istile, True);
163 }
164 }
165 dc.x += dc.w;
166 dc.w = textw(c->name);
167 drawtext(c->name, !istile, True);
168 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
169 XSync(dpy, False);
170}
171
172unsigned long
173getcolor(const char *colstr)
174{
175 Colormap cmap = DefaultColormap(dpy, screen);
176 XColor color;
177
178 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
179 return color.pixel;
180}
181
182void
183setfont(const char *fontstr)
184{
185 char **missing, *def;
186 int i, n;
187
188 missing = NULL;
189 setlocale(LC_ALL, "");
190 if(dc.font.set)
191 XFreeFontSet(dpy, dc.font.set);
192 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
193 if(missing) {
194 while(n--)
195 fprintf(stderr, "missing fontset: %s\n", missing[n]);
196 XFreeStringList(missing);
197 if(dc.font.set) {
198 XFreeFontSet(dpy, dc.font.set);
199 dc.font.set = NULL;
200 }
201 }
202 if(dc.font.set) {
203 XFontSetExtents *font_extents;
204 XFontStruct **xfonts;
205 char **font_names;
206
207 dc.font.ascent = dc.font.descent = 0;
208 font_extents = XExtentsOfFontSet(dc.font.set);
209 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
210 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
211 if(dc.font.ascent < (*xfonts)->ascent)
212 dc.font.ascent = (*xfonts)->ascent;
213 if(dc.font.descent < (*xfonts)->descent)
214 dc.font.descent = (*xfonts)->descent;
215 xfonts++;
216 }
217 }
218 else {
219 if(dc.font.xfont)
220 XFreeFont(dpy, dc.font.xfont);
221 dc.font.xfont = NULL;
222 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
223 if (!dc.font.xfont)
224 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
225 if (!dc.font.xfont)
226 eprint("error, cannot init 'fixed' font\n");
227 dc.font.ascent = dc.font.xfont->ascent;
228 dc.font.descent = dc.font.xfont->descent;
229 }
230 dc.font.height = dc.font.ascent + dc.font.descent;
231}
232
233unsigned int
234textw(char *text)
235{
236 return textnw(text, strlen(text)) + dc.font.height;
237}