aewl
view draw.c @ 123:61490330e90a
cleaned up code
author | arg@10ksloc.org |
---|---|
date | Thu, 20 Jul 2006 12:18:06 +0200 |
parents | c498da7520c7 |
children | 75576e44c1d8 |
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"
7 #include <stdio.h>
8 #include <string.h>
9 #include <X11/Xlocale.h>
11 /* static */
13 static void
14 drawborder(void)
15 {
16 XPoint points[5];
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 }
33 static unsigned int
34 textnw(char *text, unsigned int len)
35 {
36 XRectangle r;
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 }
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 };
54 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
55 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
57 w = 0;
58 if(border)
59 drawborder();
61 if(!text)
62 return;
64 len = strlen(text);
65 if(len >= sizeof(buf))
66 len = sizeof(buf) - 1;
67 memcpy(buf, text, len);
68 buf[len] = 0;
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);
74 /* shorten text if necessary */
75 while(len && (w = textnw(buf, len)) > dc.w - h)
76 buf[--len] = 0;
78 if(w > dc.w)
79 return; /* too long */
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 }
95 /* extern */
97 void
98 drawall()
99 {
100 Client *c;
102 for(c = clients; c; c = getnext(c->next, tsel))
103 drawtitle(c);
104 drawstatus();
105 }
107 void
108 drawstatus()
109 {
110 int i;
111 Bool istile = arrange == dotile;
113 dc.x = dc.y = 0;
114 dc.w = bw;
115 drawtext(NULL, !istile, False);
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);
135 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
136 XSync(dpy, False);
137 }
139 void
140 drawtitle(Client *c)
141 {
142 int i;
143 Bool istile = arrange == dotile;
145 if(c == sel) {
146 drawstatus();
147 XUnmapWindow(dpy, c->title);
148 XSetWindowBorder(dpy, c->win, dc.fg);
149 return;
150 }
152 XSetWindowBorder(dpy, c->win, dc.bg);
153 XMapWindow(dpy, c->title);
155 dc.x = dc.y = 0;
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 }
172 unsigned long
173 getcolor(const char *colstr)
174 {
175 Colormap cmap = DefaultColormap(dpy, screen);
176 XColor color;
178 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
179 return color.pixel;
180 }
182 void
183 setfont(const char *fontstr)
184 {
185 char **missing, *def;
186 int i, n;
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;
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 }
233 unsigned int
234 textw(char *text)
235 {
236 return textnw(text, strlen(text)) + dc.font.height;
237 }