rev |
line source |
garbeam@2
|
1 /*
|
garbeam@2
|
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@2
|
3 * See LICENSE file for license details.
|
garbeam@2
|
4 */
|
garbeam@76
|
5 #include "dwm.h"
|
garbeam@2
|
6 #include <stdio.h>
|
garbeam@2
|
7 #include <string.h>
|
garbeam@32
|
8 #include <X11/Xlocale.h>
|
garbeam@32
|
9
|
garbeam@84
|
10 /* static */
|
garbeam@76
|
11
|
garbeam@77
|
12 static unsigned int
|
arg@173
|
13 textnw(const char *text, unsigned int len)
|
garbeam@75
|
14 {
|
garbeam@77
|
15 XRectangle r;
|
arg@123
|
16
|
garbeam@77
|
17 if(dc.font.set) {
|
garbeam@77
|
18 XmbTextExtents(dc.font.set, text, len, NULL, &r);
|
garbeam@77
|
19 return r.width;
|
garbeam@77
|
20 }
|
garbeam@77
|
21 return XTextWidth(dc.font.xfont, text, len);
|
garbeam@75
|
22 }
|
garbeam@75
|
23
|
garbeam@77
|
24 static void
|
arg@345
|
25 drawtext(const char *text, Bool invert, Bool highlight)
|
garbeam@2
|
26 {
|
arg@344
|
27 int x, y, w, h;
|
arg@344
|
28 static char buf[256];
|
arg@344
|
29 unsigned int len, olen;
|
arg@344
|
30 XGCValues gcv;
|
arg@257
|
31 XPoint points[5];
|
arg@344
|
32 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
|
arg@344
|
33
|
arg@344
|
34 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
|
arg@344
|
35 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
|
arg@257
|
36 points[0].x = dc.x;
|
arg@257
|
37 points[0].y = dc.y;
|
arg@257
|
38 points[1].x = dc.w - 1;
|
arg@257
|
39 points[1].y = 0;
|
arg@257
|
40 points[2].x = 0;
|
arg@257
|
41 points[2].y = dc.h - 1;
|
arg@257
|
42 points[3].x = -(dc.w - 1);
|
arg@257
|
43 points[3].y = 0;
|
arg@257
|
44 points[4].x = 0;
|
arg@257
|
45 points[4].y = -(dc.h - 1);
|
arg@344
|
46 XSetForeground(dpy, dc.gc, dc.border);
|
arg@257
|
47 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
|
arg@237
|
48
|
garbeam@3
|
49 if(!text)
|
garbeam@2
|
50 return;
|
garbeam@2
|
51
|
arg@218
|
52 w = 0;
|
arg@269
|
53 olen = len = strlen(text);
|
garbeam@2
|
54 if(len >= sizeof(buf))
|
garbeam@2
|
55 len = sizeof(buf) - 1;
|
garbeam@3
|
56 memcpy(buf, text, len);
|
garbeam@2
|
57 buf[len] = 0;
|
garbeam@2
|
58
|
garbeam@34
|
59 h = dc.font.ascent + dc.font.descent;
|
garbeam@34
|
60 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
|
garbeam@34
|
61 x = dc.x + (h / 2);
|
garbeam@2
|
62
|
garbeam@2
|
63 /* shorten text if necessary */
|
garbeam@43
|
64 while(len && (w = textnw(buf, len)) > dc.w - h)
|
garbeam@2
|
65 buf[--len] = 0;
|
arg@269
|
66 if(len < olen) {
|
arg@273
|
67 if(len > 1)
|
arg@273
|
68 buf[len - 1] = '.';
|
arg@273
|
69 if(len > 2)
|
arg@273
|
70 buf[len - 2] = '.';
|
arg@269
|
71 if(len > 3)
|
arg@273
|
72 buf[len - 3] = '.';
|
arg@269
|
73 }
|
garbeam@2
|
74
|
garbeam@34
|
75 if(w > dc.w)
|
garbeam@2
|
76 return; /* too long */
|
garbeam@66
|
77 gcv.foreground = invert ? dc.bg : dc.fg;
|
garbeam@66
|
78 gcv.background = invert ? dc.fg : dc.bg;
|
garbeam@34
|
79 if(dc.font.set) {
|
garbeam@34
|
80 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
|
arg@342
|
81 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
|
garbeam@2
|
82 }
|
garbeam@2
|
83 else {
|
garbeam@34
|
84 gcv.font = dc.font.xfont->fid;
|
garbeam@34
|
85 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
|
arg@342
|
86 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
|
arg@342
|
87 }
|
arg@345
|
88 if(highlight) {
|
arg@345
|
89 points[0].x = dc.x + 1;
|
arg@345
|
90 points[0].y = dc.y + 1;
|
arg@345
|
91 points[1].x = dc.w - 3;
|
arg@344
|
92 points[1].y = 0;
|
arg@345
|
93 points[2].x = 0;
|
arg@345
|
94 points[2].y = dc.h - 3;
|
arg@345
|
95 points[3].x = -(dc.w - 3);
|
arg@345
|
96 points[3].y = 0;
|
arg@345
|
97 points[4].x = 0;
|
arg@345
|
98 points[4].y = -(dc.h - 3);
|
arg@345
|
99 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
|
garbeam@2
|
100 }
|
garbeam@2
|
101 }
|
garbeam@2
|
102
|
garbeam@84
|
103 /* extern */
|
garbeam@77
|
104
|
garbeam@77
|
105 void
|
garbeam@77
|
106 drawall()
|
garbeam@77
|
107 {
|
garbeam@77
|
108 Client *c;
|
garbeam@77
|
109
|
arg@142
|
110 for(c = clients; c; c = getnext(c->next))
|
garbeam@77
|
111 drawtitle(c);
|
garbeam@77
|
112 drawstatus();
|
garbeam@77
|
113 }
|
garbeam@77
|
114
|
garbeam@77
|
115 void
|
garbeam@77
|
116 drawstatus()
|
garbeam@77
|
117 {
|
arg@124
|
118 int i, x;
|
garbeam@77
|
119 Bool istile = arrange == dotile;
|
garbeam@77
|
120
|
garbeam@77
|
121 dc.x = dc.y = 0;
|
garbeam@77
|
122 dc.w = bw;
|
arg@342
|
123 drawtext(NULL, !istile, False);
|
garbeam@77
|
124
|
garbeam@77
|
125 dc.w = 0;
|
arg@178
|
126 for(i = 0; i < ntags; i++) {
|
garbeam@77
|
127 dc.x += dc.w;
|
garbeam@77
|
128 dc.w = textw(tags[i]);
|
garbeam@77
|
129 if(istile)
|
arg@342
|
130 drawtext(tags[i], seltag[i], sel && sel->tags[i]);
|
garbeam@77
|
131 else
|
arg@342
|
132 drawtext(tags[i], !seltag[i], sel && sel->tags[i]);
|
garbeam@77
|
133 }
|
arg@124
|
134 x = dc.x + dc.w;
|
garbeam@77
|
135 dc.w = textw(stext);
|
garbeam@77
|
136 dc.x = bx + bw - dc.w;
|
arg@342
|
137 if(dc.x < x) {
|
arg@342
|
138 dc.x = x;
|
arg@342
|
139 dc.w = bw - x;
|
arg@342
|
140 }
|
arg@342
|
141 drawtext(stext, !istile, False);
|
arg@340
|
142
|
arg@342
|
143 if(sel && ((dc.w = dc.x - x) > bh)) {
|
arg@342
|
144 dc.x = x;
|
arg@342
|
145 drawtext(sel->name, istile, False);
|
arg@124
|
146 }
|
garbeam@77
|
147 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
|
garbeam@79
|
148 XSync(dpy, False);
|
garbeam@77
|
149 }
|
garbeam@77
|
150
|
garbeam@76
|
151 void
|
garbeam@76
|
152 drawtitle(Client *c)
|
garbeam@76
|
153 {
|
garbeam@76
|
154 int i;
|
garbeam@76
|
155 Bool istile = arrange == dotile;
|
garbeam@76
|
156
|
arg@239
|
157 if(c == sel && issel) {
|
garbeam@76
|
158 drawstatus();
|
arg@342
|
159 XUnmapWindow(dpy, c->twin);
|
garbeam@76
|
160 XSetWindowBorder(dpy, c->win, dc.fg);
|
garbeam@76
|
161 return;
|
garbeam@76
|
162 }
|
garbeam@76
|
163
|
garbeam@76
|
164 XSetWindowBorder(dpy, c->win, dc.bg);
|
arg@342
|
165 XMapWindow(dpy, c->twin);
|
arg@342
|
166 dc.x = dc.y = 0;
|
arg@342
|
167 dc.w = c->tw;
|
arg@342
|
168 drawtext(c->name, !istile, False);
|
arg@342
|
169 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
|
garbeam@79
|
170 XSync(dpy, False);
|
garbeam@76
|
171 }
|
garbeam@76
|
172
|
garbeam@43
|
173 unsigned long
|
garbeam@74
|
174 getcolor(const char *colstr)
|
garbeam@2
|
175 {
|
arg@123
|
176 Colormap cmap = DefaultColormap(dpy, screen);
|
garbeam@2
|
177 XColor color;
|
garbeam@43
|
178
|
garbeam@2
|
179 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
|
garbeam@2
|
180 return color.pixel;
|
garbeam@2
|
181 }
|
garbeam@2
|
182
|
garbeam@2
|
183 void
|
garbeam@74
|
184 setfont(const char *fontstr)
|
garbeam@2
|
185 {
|
garbeam@2
|
186 char **missing, *def;
|
garbeam@33
|
187 int i, n;
|
garbeam@2
|
188
|
garbeam@7
|
189 missing = NULL;
|
garbeam@2
|
190 setlocale(LC_ALL, "");
|
garbeam@43
|
191 if(dc.font.set)
|
garbeam@43
|
192 XFreeFontSet(dpy, dc.font.set);
|
garbeam@43
|
193 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
|
garbeam@2
|
194 if(missing) {
|
garbeam@2
|
195 while(n--)
|
garbeam@2
|
196 fprintf(stderr, "missing fontset: %s\n", missing[n]);
|
garbeam@2
|
197 XFreeStringList(missing);
|
garbeam@43
|
198 if(dc.font.set) {
|
garbeam@43
|
199 XFreeFontSet(dpy, dc.font.set);
|
garbeam@43
|
200 dc.font.set = NULL;
|
garbeam@2
|
201 }
|
garbeam@2
|
202 }
|
garbeam@43
|
203 if(dc.font.set) {
|
garbeam@2
|
204 XFontSetExtents *font_extents;
|
garbeam@2
|
205 XFontStruct **xfonts;
|
garbeam@2
|
206 char **font_names;
|
garbeam@2
|
207
|
garbeam@43
|
208 dc.font.ascent = dc.font.descent = 0;
|
garbeam@43
|
209 font_extents = XExtentsOfFontSet(dc.font.set);
|
garbeam@43
|
210 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
|
garbeam@43
|
211 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
|
garbeam@43
|
212 if(dc.font.ascent < (*xfonts)->ascent)
|
garbeam@43
|
213 dc.font.ascent = (*xfonts)->ascent;
|
garbeam@43
|
214 if(dc.font.descent < (*xfonts)->descent)
|
garbeam@43
|
215 dc.font.descent = (*xfonts)->descent;
|
garbeam@2
|
216 xfonts++;
|
garbeam@2
|
217 }
|
garbeam@2
|
218 }
|
garbeam@2
|
219 else {
|
garbeam@43
|
220 if(dc.font.xfont)
|
garbeam@43
|
221 XFreeFont(dpy, dc.font.xfont);
|
garbeam@43
|
222 dc.font.xfont = NULL;
|
garbeam@43
|
223 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
|
garbeam@43
|
224 if (!dc.font.xfont)
|
garbeam@43
|
225 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
|
garbeam@43
|
226 if (!dc.font.xfont)
|
garbeam@75
|
227 eprint("error, cannot init 'fixed' font\n");
|
garbeam@43
|
228 dc.font.ascent = dc.font.xfont->ascent;
|
garbeam@43
|
229 dc.font.descent = dc.font.xfont->descent;
|
garbeam@2
|
230 }
|
garbeam@43
|
231 dc.font.height = dc.font.ascent + dc.font.descent;
|
garbeam@2
|
232 }
|
garbeam@76
|
233
|
garbeam@76
|
234 unsigned int
|
arg@173
|
235 textw(const char *text)
|
garbeam@76
|
236 {
|
garbeam@76
|
237 return textnw(text, strlen(text)) + dc.font.height;
|
garbeam@76
|
238 }
|