dwm-meillo

view draw.c @ 334:5cfa63564a0f

tags appear in the left again
author Anselm R. Garbe <arg@10kloc.org>
date Wed, 23 Aug 2006 11:33:50 +0200
parents 0ed2de01e9f7
children ae0affabdc02
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;
153 dc.w = 0;
154 for(i = 0; i < ntags; i++) {
155 if(c->tags[i]) {
156 dc.x += dc.w;
157 dc.w = textw(tags[i]);
158 drawtext(tags[i], !istile);
159 }
160 }
161 dc.x += dc.w;
162 dc.w = c->tw - dc.x;
163 drawtext(c->name, !istile);
164 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
165 XSync(dpy, False);
166 }
168 unsigned long
169 getcolor(const char *colstr)
170 {
171 Colormap cmap = DefaultColormap(dpy, screen);
172 XColor color;
174 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
175 return color.pixel;
176 }
178 void
179 setfont(const char *fontstr)
180 {
181 char **missing, *def;
182 int i, n;
184 missing = NULL;
185 setlocale(LC_ALL, "");
186 if(dc.font.set)
187 XFreeFontSet(dpy, dc.font.set);
188 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
189 if(missing) {
190 while(n--)
191 fprintf(stderr, "missing fontset: %s\n", missing[n]);
192 XFreeStringList(missing);
193 if(dc.font.set) {
194 XFreeFontSet(dpy, dc.font.set);
195 dc.font.set = NULL;
196 }
197 }
198 if(dc.font.set) {
199 XFontSetExtents *font_extents;
200 XFontStruct **xfonts;
201 char **font_names;
203 dc.font.ascent = dc.font.descent = 0;
204 font_extents = XExtentsOfFontSet(dc.font.set);
205 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
206 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
207 if(dc.font.ascent < (*xfonts)->ascent)
208 dc.font.ascent = (*xfonts)->ascent;
209 if(dc.font.descent < (*xfonts)->descent)
210 dc.font.descent = (*xfonts)->descent;
211 xfonts++;
212 }
213 }
214 else {
215 if(dc.font.xfont)
216 XFreeFont(dpy, dc.font.xfont);
217 dc.font.xfont = NULL;
218 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
219 if (!dc.font.xfont)
220 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
221 if (!dc.font.xfont)
222 eprint("error, cannot init 'fixed' font\n");
223 dc.font.ascent = dc.font.xfont->ascent;
224 dc.font.descent = dc.font.xfont->descent;
225 }
226 dc.font.height = dc.font.ascent + dc.font.descent;
227 }
229 unsigned int
230 textw(const char *text)
231 {
232 return textnw(text, strlen(text)) + dc.font.height;
233 }