aewl

view draw.c @ 329:cea0c98495bc

rearranged getproto
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 22 Aug 2006 17:40:21 +0200
parents bf6792e3e700
children 0ed2de01e9f7
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, olen;
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 olen = 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;
67 if(len < olen) {
68 if(len > 1)
69 buf[len - 1] = '.';
70 if(len > 2)
71 buf[len - 2] = '.';
72 if(len > 3)
73 buf[len - 3] = '.';
74 }
76 if(w > dc.w)
77 return; /* too long */
79 gcv.foreground = invert ? dc.bg : dc.fg;
80 gcv.background = invert ? dc.fg : dc.bg;
81 if(dc.font.set) {
82 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
83 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
84 x, y, buf, len);
85 }
86 else {
87 gcv.font = dc.font.xfont->fid;
88 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
89 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
90 }
91 }
93 /* extern */
95 void
96 drawall()
97 {
98 Client *c;
100 for(c = clients; c; c = getnext(c->next))
101 drawtitle(c);
102 drawstatus();
103 }
105 void
106 drawstatus()
107 {
108 int i, x;
109 Bool istile = arrange == dotile;
111 dc.x = dc.y = 0;
112 dc.w = bw;
113 drawtext(NULL, !istile);
115 dc.w = 0;
116 for(i = 0; i < ntags; i++) {
117 dc.x += dc.w;
118 dc.w = textw(tags[i]);
119 if(istile)
120 drawtext(tags[i], seltag[i]);
121 else
122 drawtext(tags[i], !seltag[i]);
123 }
124 x = dc.x + dc.w;
125 dc.w = textw(stext);
126 dc.x = bx + bw - dc.w;
127 drawtext(stext, !istile);
128 if(sel && ((dc.w = dc.x - x) >= bh)) {
129 dc.x = x;
130 drawtext(sel->name, istile);
131 }
132 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
133 XSync(dpy, False);
134 }
136 void
137 drawtitle(Client *c)
138 {
139 int i;
140 Bool istile = arrange == dotile;
142 if(c == sel && issel) {
143 drawstatus();
144 XUnmapWindow(dpy, c->title);
145 XSetWindowBorder(dpy, c->win, dc.fg);
146 return;
147 }
149 XSetWindowBorder(dpy, c->win, dc.bg);
150 XMapWindow(dpy, c->title);
152 dc.x = dc.y = 0;
154 dc.w = 0;
155 for(i = 0; i < ntags; i++) {
156 if(c->tags[i]) {
157 dc.x += dc.w;
158 dc.w = textw(tags[i]);
159 drawtext(tags[i], !istile);
160 }
161 }
162 dc.x += dc.w;
163 dc.w = c->tw - dc.x;
164 drawtext(c->name, !istile);
165 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
166 XSync(dpy, False);
167 }
169 unsigned long
170 getcolor(const char *colstr)
171 {
172 Colormap cmap = DefaultColormap(dpy, screen);
173 XColor color;
175 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
176 return color.pixel;
177 }
179 void
180 setfont(const char *fontstr)
181 {
182 char **missing, *def;
183 int i, n;
185 missing = NULL;
186 setlocale(LC_ALL, "");
187 if(dc.font.set)
188 XFreeFontSet(dpy, dc.font.set);
189 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
190 if(missing) {
191 while(n--)
192 fprintf(stderr, "missing fontset: %s\n", missing[n]);
193 XFreeStringList(missing);
194 if(dc.font.set) {
195 XFreeFontSet(dpy, dc.font.set);
196 dc.font.set = NULL;
197 }
198 }
199 if(dc.font.set) {
200 XFontSetExtents *font_extents;
201 XFontStruct **xfonts;
202 char **font_names;
204 dc.font.ascent = dc.font.descent = 0;
205 font_extents = XExtentsOfFontSet(dc.font.set);
206 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
207 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
208 if(dc.font.ascent < (*xfonts)->ascent)
209 dc.font.ascent = (*xfonts)->ascent;
210 if(dc.font.descent < (*xfonts)->descent)
211 dc.font.descent = (*xfonts)->descent;
212 xfonts++;
213 }
214 }
215 else {
216 if(dc.font.xfont)
217 XFreeFont(dpy, dc.font.xfont);
218 dc.font.xfont = NULL;
219 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
220 if (!dc.font.xfont)
221 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
222 if (!dc.font.xfont)
223 eprint("error, cannot init 'fixed' font\n");
224 dc.font.ascent = dc.font.xfont->ascent;
225 dc.font.descent = dc.font.xfont->descent;
226 }
227 dc.font.height = dc.font.ascent + dc.font.descent;
228 }
230 unsigned int
231 textw(const char *text)
232 {
233 return textnw(text, strlen(text)) + dc.font.height;
234 }