dwm-meillo
view draw.c @ 340:ae0affabdc02
implemented right tag drawing in the status bar and titlebars
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Wed, 23 Aug 2006 14:38:25 +0200 |
parents | 5cfa63564a0f |
children | a1901753deef |
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);
129 if(sel) {
130 for(i = 0; i < ntags; i++)
131 if(sel->tags[i]) {
132 dc.w = textw(tags[i]);
133 dc.x -= dc.w;
134 if(dc.x < x)
135 break;
136 drawtext(tags[i], istile);
137 }
138 if(dc.x > x && (dc.x - x) > bh) {
139 dc.w = dc.x - x;
140 dc.x = x;
141 drawtext(sel->name, istile);
142 }
143 }
144 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
145 XSync(dpy, False);
146 }
148 void
149 drawtitle(Client *c)
150 {
151 int i;
152 Bool istile = arrange == dotile;
154 if(c == sel && issel) {
155 drawstatus();
156 XUnmapWindow(dpy, c->title);
157 XSetWindowBorder(dpy, c->win, dc.fg);
158 return;
159 }
161 XSetWindowBorder(dpy, c->win, dc.bg);
162 XMapWindow(dpy, c->title);
164 dc.y = dc.w = 0;
165 dc.x = c->tw;
166 for(i = 0; i < ntags; i++)
167 if(c->tags[i]) {
168 dc.w = textw(tags[i]);
169 dc.x -= dc.w;
170 drawtext(tags[i], !istile);
171 }
172 dc.w = dc.x;
173 dc.x = 0;
174 drawtext(c->name, !istile);
175 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
176 XSync(dpy, False);
177 }
179 unsigned long
180 getcolor(const char *colstr)
181 {
182 Colormap cmap = DefaultColormap(dpy, screen);
183 XColor color;
185 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
186 return color.pixel;
187 }
189 void
190 setfont(const char *fontstr)
191 {
192 char **missing, *def;
193 int i, n;
195 missing = NULL;
196 setlocale(LC_ALL, "");
197 if(dc.font.set)
198 XFreeFontSet(dpy, dc.font.set);
199 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
200 if(missing) {
201 while(n--)
202 fprintf(stderr, "missing fontset: %s\n", missing[n]);
203 XFreeStringList(missing);
204 if(dc.font.set) {
205 XFreeFontSet(dpy, dc.font.set);
206 dc.font.set = NULL;
207 }
208 }
209 if(dc.font.set) {
210 XFontSetExtents *font_extents;
211 XFontStruct **xfonts;
212 char **font_names;
214 dc.font.ascent = dc.font.descent = 0;
215 font_extents = XExtentsOfFontSet(dc.font.set);
216 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
217 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
218 if(dc.font.ascent < (*xfonts)->ascent)
219 dc.font.ascent = (*xfonts)->ascent;
220 if(dc.font.descent < (*xfonts)->descent)
221 dc.font.descent = (*xfonts)->descent;
222 xfonts++;
223 }
224 }
225 else {
226 if(dc.font.xfont)
227 XFreeFont(dpy, dc.font.xfont);
228 dc.font.xfont = NULL;
229 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
230 if (!dc.font.xfont)
231 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
232 if (!dc.font.xfont)
233 eprint("error, cannot init 'fixed' font\n");
234 dc.font.ascent = dc.font.xfont->ascent;
235 dc.font.descent = dc.font.xfont->descent;
236 }
237 dc.font.height = dc.font.ascent + dc.font.descent;
238 }
240 unsigned int
241 textw(const char *text)
242 {
243 return textnw(text, strlen(text)) + dc.font.height;
244 }