dwm-meillo

view draw.c @ 362:ba6c55e1b9b2

trying a different configuration
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 25 Aug 2006 15:48:44 +0200
parents 728e74820e1d
children fc9d35252ab4
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, unsigned long col[ColLast], Bool highlight)
26 {
27 int x, y, w, h;
28 static char buf[256];
29 unsigned int len, olen;
30 XGCValues gcv;
31 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
33 XSetForeground(dpy, dc.gc, col[ColBG]);
34 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
36 if(!text)
37 return;
39 w = 0;
40 olen = len = strlen(text);
41 if(len >= sizeof(buf))
42 len = sizeof(buf) - 1;
43 memcpy(buf, text, len);
44 buf[len] = 0;
46 h = dc.font.ascent + dc.font.descent;
47 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
48 x = dc.x + (h / 2);
50 /* shorten text if necessary */
51 while(len && (w = textnw(buf, len)) > dc.w - h)
52 buf[--len] = 0;
53 if(len < olen) {
54 if(len > 1)
55 buf[len - 1] = '.';
56 if(len > 2)
57 buf[len - 2] = '.';
58 if(len > 3)
59 buf[len - 3] = '.';
60 }
62 if(w > dc.w)
63 return; /* too long */
64 gcv.foreground = col[ColFG];
65 if(dc.font.set) {
66 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
67 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
68 }
69 else {
70 gcv.font = dc.font.xfont->fid;
71 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
72 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
73 }
74 if(highlight) {
75 r.x = dc.x + 2;
76 r.y = dc.y + 2;
77 r.width = r.height = 3;
78 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
79 }
80 }
82 /* extern */
84 void
85 drawall()
86 {
87 Client *c;
89 for(c = clients; c; c = getnext(c->next))
90 drawtitle(c);
91 drawstatus();
92 }
94 void
95 drawstatus()
96 {
97 int i, x;
99 dc.x = dc.y = 0;
100 dc.w = bw;
102 drawtext(NULL, dc.status, False);
103 for(i = 0; i < ntags; i++) {
104 dc.w = textw(tags[i]);
105 if(seltag[i])
106 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
107 else
108 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
109 dc.x += dc.w;
110 }
112 dc.w = bmw;
113 drawtext(arrange == dotile ? TILESYMBOL : FLOATSYMBOL, dc.status, False);
115 x = dc.x + dc.w;
116 dc.w = textw(stext);
117 dc.x = bx + bw - dc.w;
118 if(dc.x < x) {
119 dc.x = x;
120 dc.w = bw - x;
121 }
122 drawtext(stext, dc.status, False);
124 if(sel && ((dc.w = dc.x - x) > bh)) {
125 dc.x = x;
126 drawtext(sel->name, dc.sel, False);
127 }
128 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
129 XSync(dpy, False);
130 }
132 void
133 drawtitle(Client *c)
134 {
135 int i;
137 if(c == sel && issel) {
138 drawstatus();
139 XUnmapWindow(dpy, c->twin);
140 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
141 return;
142 }
144 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
145 XMapWindow(dpy, c->twin);
146 dc.x = dc.y = 0;
147 dc.w = c->tw;
148 drawtext(c->name, dc.norm, False);
149 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
150 XSync(dpy, False);
151 }
153 unsigned long
154 getcolor(const char *colstr)
155 {
156 Colormap cmap = DefaultColormap(dpy, screen);
157 XColor color;
159 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
160 return color.pixel;
161 }
163 void
164 setfont(const char *fontstr)
165 {
166 char **missing, *def;
167 int i, n;
169 missing = NULL;
170 setlocale(LC_ALL, "");
171 if(dc.font.set)
172 XFreeFontSet(dpy, dc.font.set);
173 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
174 if(missing) {
175 while(n--)
176 fprintf(stderr, "missing fontset: %s\n", missing[n]);
177 XFreeStringList(missing);
178 if(dc.font.set) {
179 XFreeFontSet(dpy, dc.font.set);
180 dc.font.set = NULL;
181 }
182 }
183 if(dc.font.set) {
184 XFontSetExtents *font_extents;
185 XFontStruct **xfonts;
186 char **font_names;
188 dc.font.ascent = dc.font.descent = 0;
189 font_extents = XExtentsOfFontSet(dc.font.set);
190 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
191 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
192 if(dc.font.ascent < (*xfonts)->ascent)
193 dc.font.ascent = (*xfonts)->ascent;
194 if(dc.font.descent < (*xfonts)->descent)
195 dc.font.descent = (*xfonts)->descent;
196 xfonts++;
197 }
198 }
199 else {
200 if(dc.font.xfont)
201 XFreeFont(dpy, dc.font.xfont);
202 dc.font.xfont = NULL;
203 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
204 if (!dc.font.xfont)
205 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
206 if (!dc.font.xfont)
207 eprint("error, cannot init 'fixed' font\n");
208 dc.font.ascent = dc.font.xfont->ascent;
209 dc.font.descent = dc.font.xfont->descent;
210 }
211 dc.font.height = dc.font.ascent + dc.font.descent;
212 }
214 unsigned int
215 textw(const char *text)
216 {
217 return textnw(text, strlen(text)) + dc.font.height;
218 }