aewl
view draw.c @ 266:e8aa8f6e3481
supplying NULL args in select
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Mon, 14 Aug 2006 07:40:20 +0200 |
parents | d6fd632d861c |
children | bf6792e3e700 |
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 drawtext(const char *text, Bool invert)
26 {
27 int x, y, w, h;
28 static char buf[256];
29 unsigned int len;
30 XGCValues gcv;
31 XPoint points[5];
32 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
34 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
35 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
36 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
37 XSetForeground(dpy, dc.gc, dc.border);
38 points[0].x = dc.x;
39 points[0].y = dc.y;
40 points[1].x = dc.w - 1;
41 points[1].y = 0;
42 points[2].x = 0;
43 points[2].y = dc.h - 1;
44 points[3].x = -(dc.w - 1);
45 points[3].y = 0;
46 points[4].x = 0;
47 points[4].y = -(dc.h - 1);
48 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
50 if(!text)
51 return;
53 w = 0;
54 len = strlen(text);
55 if(len >= sizeof(buf))
56 len = sizeof(buf) - 1;
57 memcpy(buf, text, len);
58 buf[len] = 0;
60 h = dc.font.ascent + dc.font.descent;
61 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
62 x = dc.x + (h / 2);
64 /* shorten text if necessary */
65 while(len && (w = textnw(buf, len)) > dc.w - h)
66 buf[--len] = 0;
68 if(w > dc.w)
69 return; /* too long */
71 gcv.foreground = invert ? dc.bg : dc.fg;
72 gcv.background = invert ? dc.fg : dc.bg;
73 if(dc.font.set) {
74 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
75 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
76 x, y, buf, len);
77 }
78 else {
79 gcv.font = dc.font.xfont->fid;
80 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
81 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
82 }
83 }
85 /* extern */
87 void
88 drawall()
89 {
90 Client *c;
92 for(c = clients; c; c = getnext(c->next))
93 drawtitle(c);
94 drawstatus();
95 }
97 void
98 drawstatus()
99 {
100 int i, x;
101 Bool istile = arrange == dotile;
103 dc.x = dc.y = 0;
104 dc.w = bw;
105 drawtext(NULL, !istile);
107 dc.w = 0;
108 for(i = 0; i < ntags; i++) {
109 dc.x += dc.w;
110 dc.w = textw(tags[i]);
111 if(istile)
112 drawtext(tags[i], seltag[i]);
113 else
114 drawtext(tags[i], !seltag[i]);
115 }
116 x = dc.x + dc.w;
117 dc.w = textw(stext);
118 dc.x = bx + bw - dc.w;
119 drawtext(stext, !istile);
120 if(sel && ((dc.w = dc.x - x) >= bh)) {
121 dc.x = x;
122 drawtext(sel->name, istile);
123 }
124 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
125 XSync(dpy, False);
126 }
128 void
129 drawtitle(Client *c)
130 {
131 int i;
132 Bool istile = arrange == dotile;
134 if(c == sel && issel) {
135 drawstatus();
136 XUnmapWindow(dpy, c->title);
137 XSetWindowBorder(dpy, c->win, dc.fg);
138 return;
139 }
141 XSetWindowBorder(dpy, c->win, dc.bg);
142 XMapWindow(dpy, c->title);
144 dc.x = dc.y = 0;
146 dc.w = 0;
147 for(i = 0; i < ntags; i++) {
148 if(c->tags[i]) {
149 dc.x += dc.w;
150 dc.w = textw(tags[i]);
151 drawtext(tags[i], !istile);
152 }
153 }
154 dc.x += dc.w;
155 dc.w = textw(c->name);
156 drawtext(c->name, !istile);
157 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
158 XSync(dpy, False);
159 }
161 unsigned long
162 getcolor(const char *colstr)
163 {
164 Colormap cmap = DefaultColormap(dpy, screen);
165 XColor color;
167 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
168 return color.pixel;
169 }
171 void
172 setfont(const char *fontstr)
173 {
174 char **missing, *def;
175 int i, n;
177 missing = NULL;
178 setlocale(LC_ALL, "");
179 if(dc.font.set)
180 XFreeFontSet(dpy, dc.font.set);
181 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
182 if(missing) {
183 while(n--)
184 fprintf(stderr, "missing fontset: %s\n", missing[n]);
185 XFreeStringList(missing);
186 if(dc.font.set) {
187 XFreeFontSet(dpy, dc.font.set);
188 dc.font.set = NULL;
189 }
190 }
191 if(dc.font.set) {
192 XFontSetExtents *font_extents;
193 XFontStruct **xfonts;
194 char **font_names;
196 dc.font.ascent = dc.font.descent = 0;
197 font_extents = XExtentsOfFontSet(dc.font.set);
198 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
199 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
200 if(dc.font.ascent < (*xfonts)->ascent)
201 dc.font.ascent = (*xfonts)->ascent;
202 if(dc.font.descent < (*xfonts)->descent)
203 dc.font.descent = (*xfonts)->descent;
204 xfonts++;
205 }
206 }
207 else {
208 if(dc.font.xfont)
209 XFreeFont(dpy, dc.font.xfont);
210 dc.font.xfont = NULL;
211 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
212 if (!dc.font.xfont)
213 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
214 if (!dc.font.xfont)
215 eprint("error, cannot init 'fixed' font\n");
216 dc.font.ascent = dc.font.xfont->ascent;
217 dc.font.descent = dc.font.xfont->descent;
218 }
219 dc.font.height = dc.font.ascent + dc.font.descent;
220 }
222 unsigned int
223 textw(const char *text)
224 {
225 return textnw(text, strlen(text)) + dc.font.height;
226 }