aewl

view draw.c @ 343:336d3a2c3f80

black on light is really better, I notice this after all
author Anselm R. Garbe <arg@10kloc.org>
date Wed, 23 Aug 2006 19:05:03 +0200
parents ae0affabdc02
children 93192711a36a
line source
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>
10 /* static */
12 static unsigned int
13 textnw(const char *text, unsigned int len)
14 {
15 XRectangle r;
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 }
24 static void
25 drawborder()
26 {
27 XPoint points[5];
28 points[0].x = dc.x;
29 points[0].y = dc.y;
30 points[1].x = dc.w - 1;
31 points[1].y = 0;
32 points[2].x = 0;
33 points[2].y = dc.h - 1;
34 points[3].x = -(dc.w - 1);
35 points[3].y = 0;
36 points[4].x = 0;
37 points[4].y = -(dc.h - 1);
38 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
39 }
41 static void
42 drawtext(const char *text, Bool invert, Bool highlight)
43 {
44 int x, y, w, h;
45 static char buf[256];
46 unsigned int len, olen;
47 DC tmp;
48 XGCValues gcv;
49 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
51 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
52 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
53 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
54 XSetForeground(dpy, dc.gc, dc.border);
55 drawborder();
57 if(!text)
58 return;
60 w = 0;
61 olen = len = strlen(text);
62 if(len >= sizeof(buf))
63 len = sizeof(buf) - 1;
64 memcpy(buf, text, len);
65 buf[len] = 0;
67 h = dc.font.ascent + dc.font.descent;
68 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
69 x = dc.x + (h / 2);
71 /* shorten text if necessary */
72 while(len && (w = textnw(buf, len)) > dc.w - h)
73 buf[--len] = 0;
74 if(len < olen) {
75 if(len > 1)
76 buf[len - 1] = '.';
77 if(len > 2)
78 buf[len - 2] = '.';
79 if(len > 3)
80 buf[len - 3] = '.';
81 }
83 if(w > dc.w)
84 return; /* too long */
85 gcv.foreground = invert ? dc.bg : dc.fg;
86 gcv.background = invert ? dc.fg : dc.bg;
87 if(dc.font.set) {
88 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
89 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
90 }
91 else {
92 gcv.font = dc.font.xfont->fid;
93 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
94 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
95 }
96 if(highlight) {
97 tmp = dc;
98 dc.x += 2;
99 dc.y += 2;
100 dc.w -= 4;
101 dc.h -= 4;
102 drawborder();
103 dc = tmp;
104 }
105 }
107 /* extern */
109 void
110 drawall()
111 {
112 Client *c;
114 for(c = clients; c; c = getnext(c->next))
115 drawtitle(c);
116 drawstatus();
117 }
119 void
120 drawstatus()
121 {
122 int i, x;
123 Bool istile = arrange == dotile;
125 dc.x = dc.y = 0;
126 dc.w = bw;
127 drawtext(NULL, !istile, False);
129 dc.w = 0;
130 for(i = 0; i < ntags; i++) {
131 dc.x += dc.w;
132 dc.w = textw(tags[i]);
133 if(istile)
134 drawtext(tags[i], seltag[i], sel && sel->tags[i]);
135 else
136 drawtext(tags[i], !seltag[i], sel && sel->tags[i]);
137 }
138 x = dc.x + dc.w;
139 dc.w = textw(stext);
140 dc.x = bx + bw - dc.w;
141 if(dc.x < x) {
142 dc.x = x;
143 dc.w = bw - x;
144 }
145 drawtext(stext, !istile, False);
147 if(sel && ((dc.w = dc.x - x) > bh)) {
148 dc.x = x;
149 drawtext(sel->name, istile, False);
150 }
151 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
152 XSync(dpy, False);
153 }
155 void
156 drawtitle(Client *c)
157 {
158 int i;
159 Bool istile = arrange == dotile;
161 if(c == sel && issel) {
162 drawstatus();
163 XUnmapWindow(dpy, c->twin);
164 XSetWindowBorder(dpy, c->win, dc.fg);
165 return;
166 }
168 XSetWindowBorder(dpy, c->win, dc.bg);
169 XMapWindow(dpy, c->twin);
170 dc.x = dc.y = 0;
171 dc.w = c->tw;
172 drawtext(c->name, !istile, False);
173 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
174 XSync(dpy, False);
175 }
177 unsigned long
178 getcolor(const char *colstr)
179 {
180 Colormap cmap = DefaultColormap(dpy, screen);
181 XColor color;
183 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
184 return color.pixel;
185 }
187 void
188 setfont(const char *fontstr)
189 {
190 char **missing, *def;
191 int i, n;
193 missing = NULL;
194 setlocale(LC_ALL, "");
195 if(dc.font.set)
196 XFreeFontSet(dpy, dc.font.set);
197 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
198 if(missing) {
199 while(n--)
200 fprintf(stderr, "missing fontset: %s\n", missing[n]);
201 XFreeStringList(missing);
202 if(dc.font.set) {
203 XFreeFontSet(dpy, dc.font.set);
204 dc.font.set = NULL;
205 }
206 }
207 if(dc.font.set) {
208 XFontSetExtents *font_extents;
209 XFontStruct **xfonts;
210 char **font_names;
212 dc.font.ascent = dc.font.descent = 0;
213 font_extents = XExtentsOfFontSet(dc.font.set);
214 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
215 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
216 if(dc.font.ascent < (*xfonts)->ascent)
217 dc.font.ascent = (*xfonts)->ascent;
218 if(dc.font.descent < (*xfonts)->descent)
219 dc.font.descent = (*xfonts)->descent;
220 xfonts++;
221 }
222 }
223 else {
224 if(dc.font.xfont)
225 XFreeFont(dpy, dc.font.xfont);
226 dc.font.xfont = NULL;
227 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
228 if (!dc.font.xfont)
229 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
230 if (!dc.font.xfont)
231 eprint("error, cannot init 'fixed' font\n");
232 dc.font.ascent = dc.font.xfont->ascent;
233 dc.font.descent = dc.font.xfont->descent;
234 }
235 dc.font.height = dc.font.ascent + dc.font.descent;
236 }
238 unsigned int
239 textw(const char *text)
240 {
241 return textnw(text, strlen(text)) + dc.font.height;
242 }