rev |
line source |
arg@643
|
1 /* (C)opyright MMIV-MMVII Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@2
|
2 * See LICENSE file for license details.
|
garbeam@2
|
3 */
|
garbeam@76
|
4 #include "dwm.h"
|
garbeam@2
|
5 #include <stdio.h>
|
garbeam@2
|
6 #include <string.h>
|
garbeam@32
|
7
|
garbeam@84
|
8 /* static */
|
garbeam@76
|
9
|
arg@572
|
10 static Bool
|
arg@572
|
11 isoccupied(unsigned int t)
|
arg@572
|
12 {
|
arg@572
|
13 Client *c;
|
arg@572
|
14 for(c = clients; c; c = c->next)
|
arg@572
|
15 if(c->tags[t])
|
arg@572
|
16 return True;
|
arg@572
|
17 return False;
|
arg@572
|
18 }
|
arg@572
|
19
|
garbeam@77
|
20 static unsigned int
|
arg@461
|
21 textnw(const char *text, unsigned int len) {
|
garbeam@77
|
22 XRectangle r;
|
arg@123
|
23
|
garbeam@77
|
24 if(dc.font.set) {
|
garbeam@77
|
25 XmbTextExtents(dc.font.set, text, len, NULL, &r);
|
garbeam@77
|
26 return r.width;
|
garbeam@77
|
27 }
|
garbeam@77
|
28 return XTextWidth(dc.font.xfont, text, len);
|
garbeam@75
|
29 }
|
garbeam@75
|
30
|
garbeam@77
|
31 static void
|
arg@607
|
32 drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
|
arg@344
|
33 int x, y, w, h;
|
arg@344
|
34 static char buf[256];
|
arg@344
|
35 unsigned int len, olen;
|
arg@352
|
36 XGCValues gcv;
|
arg@344
|
37 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
|
arg@606
|
38 XPoint pt[5];
|
arg@344
|
39
|
arg@353
|
40 XSetForeground(dpy, dc.gc, col[ColBG]);
|
arg@344
|
41 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
|
garbeam@3
|
42 if(!text)
|
garbeam@2
|
43 return;
|
arg@218
|
44 w = 0;
|
arg@269
|
45 olen = len = strlen(text);
|
arg@581
|
46 if(len >= sizeof buf)
|
arg@581
|
47 len = sizeof buf - 1;
|
garbeam@3
|
48 memcpy(buf, text, len);
|
garbeam@2
|
49 buf[len] = 0;
|
garbeam@34
|
50 h = dc.font.ascent + dc.font.descent;
|
garbeam@34
|
51 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
|
garbeam@34
|
52 x = dc.x + (h / 2);
|
garbeam@2
|
53 /* shorten text if necessary */
|
garbeam@43
|
54 while(len && (w = textnw(buf, len)) > dc.w - h)
|
garbeam@2
|
55 buf[--len] = 0;
|
arg@269
|
56 if(len < olen) {
|
arg@273
|
57 if(len > 1)
|
arg@273
|
58 buf[len - 1] = '.';
|
arg@273
|
59 if(len > 2)
|
arg@273
|
60 buf[len - 2] = '.';
|
arg@269
|
61 if(len > 3)
|
arg@273
|
62 buf[len - 3] = '.';
|
arg@269
|
63 }
|
garbeam@34
|
64 if(w > dc.w)
|
garbeam@2
|
65 return; /* too long */
|
arg@353
|
66 gcv.foreground = col[ColFG];
|
arg@352
|
67 if(dc.font.set) {
|
arg@353
|
68 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
|
arg@342
|
69 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
|
arg@352
|
70 }
|
garbeam@2
|
71 else {
|
arg@352
|
72 gcv.font = dc.font.xfont->fid;
|
arg@353
|
73 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
|
arg@342
|
74 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
|
arg@342
|
75 }
|
arg@606
|
76 x = (h + 2) / 4;
|
arg@607
|
77 if(filledsquare) {
|
arg@608
|
78 r.x = dc.x + 1;
|
arg@608
|
79 r.y = dc.y + 1;
|
arg@608
|
80 r.width = r.height = x + 1;
|
arg@605
|
81 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
|
garbeam@2
|
82 }
|
arg@607
|
83 else if(emptysquare) {
|
arg@608
|
84 pt[0].x = dc.x + 1;
|
arg@608
|
85 pt[0].y = dc.y + 1;
|
arg@606
|
86 pt[1].x = x;
|
arg@606
|
87 pt[1].y = 0;
|
arg@606
|
88 pt[2].x = 0;
|
arg@606
|
89 pt[2].y = x;
|
arg@606
|
90 pt[3].x = -x;
|
arg@606
|
91 pt[3].y = 0;
|
arg@606
|
92 pt[4].x = 0;
|
arg@606
|
93 pt[4].y = -x;
|
arg@606
|
94 XDrawLines(dpy, dc.drawable, dc.gc, pt, 5, CoordModePrevious);
|
arg@604
|
95 }
|
arg@604
|
96 }
|
garbeam@2
|
97
|
garbeam@84
|
98 /* extern */
|
garbeam@77
|
99
|
garbeam@77
|
100 void
|
arg@487
|
101 drawstatus(void) {
|
arg@124
|
102 int i, x;
|
garbeam@77
|
103
|
garbeam@77
|
104 dc.x = dc.y = 0;
|
arg@178
|
105 for(i = 0; i < ntags; i++) {
|
garbeam@77
|
106 dc.w = textw(tags[i]);
|
arg@353
|
107 if(seltag[i])
|
arg@572
|
108 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
|
garbeam@77
|
109 else
|
arg@572
|
110 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
|
arg@362
|
111 dc.x += dc.w;
|
garbeam@77
|
112 }
|
arg@362
|
113 dc.w = bmw;
|
arg@689
|
114 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False);
|
arg@361
|
115 x = dc.x + dc.w;
|
garbeam@77
|
116 dc.w = textw(stext);
|
arg@570
|
117 dc.x = bw - dc.w;
|
arg@342
|
118 if(dc.x < x) {
|
arg@342
|
119 dc.x = x;
|
arg@342
|
120 dc.w = bw - x;
|
arg@342
|
121 }
|
arg@689
|
122 drawtext(stext, dc.norm, False, False);
|
arg@371
|
123 if((dc.w = dc.x - x) > bh) {
|
arg@342
|
124 dc.x = x;
|
arg@572
|
125 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
|
arg@124
|
126 }
|
garbeam@77
|
127 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
|
garbeam@79
|
128 XSync(dpy, False);
|
garbeam@77
|
129 }
|
garbeam@77
|
130
|
garbeam@43
|
131 unsigned long
|
arg@461
|
132 getcolor(const char *colstr) {
|
arg@123
|
133 Colormap cmap = DefaultColormap(dpy, screen);
|
garbeam@2
|
134 XColor color;
|
garbeam@43
|
135
|
arg@495
|
136 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
|
arg@495
|
137 eprint("error, cannot allocate color '%s'\n", colstr);
|
garbeam@2
|
138 return color.pixel;
|
garbeam@2
|
139 }
|
garbeam@2
|
140
|
garbeam@2
|
141 void
|
arg@461
|
142 setfont(const char *fontstr) {
|
arg@619
|
143 char *def, **missing;
|
garbeam@33
|
144 int i, n;
|
garbeam@2
|
145
|
garbeam@7
|
146 missing = NULL;
|
garbeam@43
|
147 if(dc.font.set)
|
garbeam@43
|
148 XFreeFontSet(dpy, dc.font.set);
|
garbeam@43
|
149 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
|
garbeam@2
|
150 if(missing) {
|
garbeam@2
|
151 while(n--)
|
garbeam@2
|
152 fprintf(stderr, "missing fontset: %s\n", missing[n]);
|
garbeam@2
|
153 XFreeStringList(missing);
|
garbeam@2
|
154 }
|
garbeam@43
|
155 if(dc.font.set) {
|
garbeam@2
|
156 XFontSetExtents *font_extents;
|
garbeam@2
|
157 XFontStruct **xfonts;
|
garbeam@2
|
158 char **font_names;
|
garbeam@43
|
159 dc.font.ascent = dc.font.descent = 0;
|
garbeam@43
|
160 font_extents = XExtentsOfFontSet(dc.font.set);
|
garbeam@43
|
161 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
|
garbeam@43
|
162 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
|
garbeam@43
|
163 if(dc.font.ascent < (*xfonts)->ascent)
|
garbeam@43
|
164 dc.font.ascent = (*xfonts)->ascent;
|
garbeam@43
|
165 if(dc.font.descent < (*xfonts)->descent)
|
garbeam@43
|
166 dc.font.descent = (*xfonts)->descent;
|
garbeam@2
|
167 xfonts++;
|
garbeam@2
|
168 }
|
garbeam@2
|
169 }
|
garbeam@2
|
170 else {
|
garbeam@43
|
171 if(dc.font.xfont)
|
garbeam@43
|
172 XFreeFont(dpy, dc.font.xfont);
|
garbeam@43
|
173 dc.font.xfont = NULL;
|
arg@626
|
174 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
|
arg@627
|
175 eprint("error, cannot load font: '%s'\n", fontstr);
|
garbeam@43
|
176 dc.font.ascent = dc.font.xfont->ascent;
|
garbeam@43
|
177 dc.font.descent = dc.font.xfont->descent;
|
garbeam@2
|
178 }
|
garbeam@43
|
179 dc.font.height = dc.font.ascent + dc.font.descent;
|
garbeam@2
|
180 }
|
garbeam@76
|
181
|
garbeam@76
|
182 unsigned int
|
arg@461
|
183 textw(const char *text) {
|
garbeam@76
|
184 return textnw(text, strlen(text)) + dc.font.height;
|
garbeam@76
|
185 }
|