aewl

view draw.c @ 477:bf3de99b7c0c

small man page fix
author arg@mmvi
date Fri, 22 Sep 2006 10:01:51 +0200
parents b1159a638d0a
children 8d564b9e3cd4
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 XRectangle r;
16 if(dc.font.set) {
17 XmbTextExtents(dc.font.set, text, len, NULL, &r);
18 return r.width;
19 }
20 return XTextWidth(dc.font.xfont, text, len);
21 }
23 static void
24 drawtext(const char *text, unsigned long col[ColLast], Bool highlight) {
25 int x, y, w, h;
26 static char buf[256];
27 unsigned int len, olen;
28 XGCValues gcv;
29 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
31 XSetForeground(dpy, dc.gc, col[ColBG]);
32 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
34 if(!text)
35 return;
37 w = 0;
38 olen = len = strlen(text);
39 if(len >= sizeof(buf))
40 len = sizeof(buf) - 1;
41 memcpy(buf, text, len);
42 buf[len] = 0;
44 h = dc.font.ascent + dc.font.descent;
45 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
46 x = dc.x + (h / 2);
48 /* shorten text if necessary */
49 while(len && (w = textnw(buf, len)) > dc.w - h)
50 buf[--len] = 0;
51 if(len < olen) {
52 if(len > 1)
53 buf[len - 1] = '.';
54 if(len > 2)
55 buf[len - 2] = '.';
56 if(len > 3)
57 buf[len - 3] = '.';
58 }
60 if(w > dc.w)
61 return; /* too long */
62 gcv.foreground = col[ColFG];
63 if(dc.font.set) {
64 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
65 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
66 }
67 else {
68 gcv.font = dc.font.xfont->fid;
69 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
70 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
71 }
72 if(highlight) {
73 r.x = dc.x + 2;
74 r.y = dc.y + 2;
75 r.width = r.height = 3;
76 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
77 }
78 }
80 /* extern */
82 void
83 drawall() {
84 Client *c;
86 for(c = clients; c; c = getnext(c->next))
87 drawtitle(c);
88 drawstatus();
89 }
91 void
92 drawstatus() {
93 int i, x;
95 dc.x = dc.y = 0;
97 for(i = 0; i < ntags; i++) {
98 dc.w = textw(tags[i]);
99 if(seltag[i])
100 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
101 else
102 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
103 dc.x += dc.w;
104 }
106 dc.w = bmw;
107 drawtext(arrange == dotile ? TILESYMBOL : FLOATSYMBOL, dc.status, False);
109 x = dc.x + dc.w;
110 dc.w = textw(stext);
111 dc.x = bx + bw - dc.w;
112 if(dc.x < x) {
113 dc.x = x;
114 dc.w = bw - x;
115 }
116 drawtext(stext, dc.status, False);
118 if((dc.w = dc.x - x) > bh) {
119 dc.x = x;
120 if(sel)
121 drawtext(sel->name, dc.sel, False);
122 else
123 drawtext(NULL, dc.norm, False);
124 }
125 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
126 XSync(dpy, False);
127 }
129 void
130 drawtitle(Client *c) {
131 if(c == sel && issel) {
132 drawstatus();
133 XUnmapWindow(dpy, c->twin);
134 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
135 return;
136 }
138 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
139 XMapWindow(dpy, c->twin);
140 dc.x = dc.y = 0;
141 dc.w = c->tw;
142 drawtext(c->name, dc.norm, False);
143 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
144 XSync(dpy, False);
145 }
147 unsigned long
148 getcolor(const char *colstr) {
149 Colormap cmap = DefaultColormap(dpy, screen);
150 XColor color;
152 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
153 return color.pixel;
154 }
156 void
157 setfont(const char *fontstr) {
158 char **missing, *def;
159 int i, n;
161 missing = NULL;
162 setlocale(LC_ALL, "");
163 if(dc.font.set)
164 XFreeFontSet(dpy, dc.font.set);
165 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
166 if(missing) {
167 while(n--)
168 fprintf(stderr, "missing fontset: %s\n", missing[n]);
169 XFreeStringList(missing);
170 if(dc.font.set) {
171 XFreeFontSet(dpy, dc.font.set);
172 dc.font.set = NULL;
173 }
174 }
175 if(dc.font.set) {
176 XFontSetExtents *font_extents;
177 XFontStruct **xfonts;
178 char **font_names;
180 dc.font.ascent = dc.font.descent = 0;
181 font_extents = XExtentsOfFontSet(dc.font.set);
182 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
183 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
184 if(dc.font.ascent < (*xfonts)->ascent)
185 dc.font.ascent = (*xfonts)->ascent;
186 if(dc.font.descent < (*xfonts)->descent)
187 dc.font.descent = (*xfonts)->descent;
188 xfonts++;
189 }
190 }
191 else {
192 if(dc.font.xfont)
193 XFreeFont(dpy, dc.font.xfont);
194 dc.font.xfont = NULL;
195 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
196 if (!dc.font.xfont)
197 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
198 if (!dc.font.xfont)
199 eprint("error, cannot init 'fixed' font\n");
200 dc.font.ascent = dc.font.xfont->ascent;
201 dc.font.descent = dc.font.xfont->descent;
202 }
203 dc.font.height = dc.font.ascent + dc.font.descent;
204 }
206 unsigned int
207 textw(const char *text) {
208 return textnw(text, strlen(text)) + dc.font.height;
209 }