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