dwm-meillo

view draw.c @ 609:dcbbfabc8ecc

applied Manuel's regex patch
author arg@mig29
date Sun, 03 Dec 2006 11:21:25 +0100
parents 16762f7414de
children 5f4bb0a58f32
line source
1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdio.h>
6 #include <string.h>
7 #include <X11/Xlocale.h>
9 /* static */
11 static Bool
12 isoccupied(unsigned int t)
13 {
14 Client *c;
15 for(c = clients; c; c = c->next)
16 if(c->tags[t])
17 return True;
18 return False;
19 }
21 static unsigned int
22 textnw(const char *text, unsigned int len) {
23 XRectangle r;
25 if(dc.font.set) {
26 XmbTextExtents(dc.font.set, text, len, NULL, &r);
27 return r.width;
28 }
29 return XTextWidth(dc.font.xfont, text, len);
30 }
32 static void
33 drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
34 int x, y, w, h;
35 static char buf[256];
36 unsigned int len, olen;
37 XGCValues gcv;
38 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
39 XPoint pt[5];
41 XSetForeground(dpy, dc.gc, col[ColBG]);
42 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
43 if(!text)
44 return;
45 w = 0;
46 olen = len = strlen(text);
47 if(len >= sizeof buf)
48 len = sizeof buf - 1;
49 memcpy(buf, text, len);
50 buf[len] = 0;
51 h = dc.font.ascent + dc.font.descent;
52 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
53 x = dc.x + (h / 2);
54 /* shorten text if necessary */
55 while(len && (w = textnw(buf, len)) > dc.w - h)
56 buf[--len] = 0;
57 if(len < olen) {
58 if(len > 1)
59 buf[len - 1] = '.';
60 if(len > 2)
61 buf[len - 2] = '.';
62 if(len > 3)
63 buf[len - 3] = '.';
64 }
65 if(w > dc.w)
66 return; /* too long */
67 gcv.foreground = col[ColFG];
68 if(dc.font.set) {
69 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
70 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
71 }
72 else {
73 gcv.font = dc.font.xfont->fid;
74 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
75 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
76 }
77 x = (h + 2) / 4;
78 if(filledsquare) {
79 r.x = dc.x + 1;
80 r.y = dc.y + 1;
81 r.width = r.height = x + 1;
82 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
83 }
84 else if(emptysquare) {
85 pt[0].x = dc.x + 1;
86 pt[0].y = dc.y + 1;
87 pt[1].x = x;
88 pt[1].y = 0;
89 pt[2].x = 0;
90 pt[2].y = x;
91 pt[3].x = -x;
92 pt[3].y = 0;
93 pt[4].x = 0;
94 pt[4].y = -x;
95 XDrawLines(dpy, dc.drawable, dc.gc, pt, 5, CoordModePrevious);
96 }
97 }
99 /* extern */
101 void
102 drawall(void) {
103 Client *c;
105 for(c = clients; c; c = getnext(c->next))
106 drawtitle(c);
107 drawstatus();
108 }
110 void
111 drawstatus(void) {
112 int i, x;
114 dc.x = dc.y = 0;
115 for(i = 0; i < ntags; i++) {
116 dc.w = textw(tags[i]);
117 if(seltag[i])
118 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
119 else
120 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
121 dc.x += dc.w;
122 }
123 dc.w = bmw;
124 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
125 x = dc.x + dc.w;
126 dc.w = textw(stext);
127 dc.x = bw - dc.w;
128 if(dc.x < x) {
129 dc.x = x;
130 dc.w = bw - x;
131 }
132 drawtext(stext, dc.status, False, False);
133 if((dc.w = dc.x - x) > bh) {
134 dc.x = x;
135 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
136 }
137 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
138 XSync(dpy, False);
139 }
141 void
142 drawtitle(Client *c) {
143 if(c == sel && issel) {
144 drawstatus();
145 XUnmapWindow(dpy, c->twin);
146 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
147 return;
148 }
149 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
150 XMapWindow(dpy, c->twin);
151 dc.x = dc.y = 0;
152 dc.w = c->tw;
153 drawtext(c->name, dc.norm, False,False);
154 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
155 XSync(dpy, False);
156 }
158 unsigned long
159 getcolor(const char *colstr) {
160 Colormap cmap = DefaultColormap(dpy, screen);
161 XColor color;
163 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
164 eprint("error, cannot allocate color '%s'\n", colstr);
165 return color.pixel;
166 }
168 void
169 setfont(const char *fontstr) {
170 char **missing, *def;
171 int i, n;
173 missing = NULL;
174 setlocale(LC_ALL, "");
175 if(dc.font.set)
176 XFreeFontSet(dpy, dc.font.set);
177 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
178 if(missing) {
179 while(n--)
180 fprintf(stderr, "missing fontset: %s\n", missing[n]);
181 XFreeStringList(missing);
182 if(dc.font.set) {
183 XFreeFontSet(dpy, dc.font.set);
184 dc.font.set = NULL;
185 }
186 }
187 if(dc.font.set) {
188 XFontSetExtents *font_extents;
189 XFontStruct **xfonts;
190 char **font_names;
191 dc.font.ascent = dc.font.descent = 0;
192 font_extents = XExtentsOfFontSet(dc.font.set);
193 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
194 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
195 if(dc.font.ascent < (*xfonts)->ascent)
196 dc.font.ascent = (*xfonts)->ascent;
197 if(dc.font.descent < (*xfonts)->descent)
198 dc.font.descent = (*xfonts)->descent;
199 xfonts++;
200 }
201 }
202 else {
203 if(dc.font.xfont)
204 XFreeFont(dpy, dc.font.xfont);
205 dc.font.xfont = NULL;
206 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
207 if (!dc.font.xfont)
208 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
209 if (!dc.font.xfont)
210 eprint("error, cannot init 'fixed' font\n");
211 dc.font.ascent = dc.font.xfont->ascent;
212 dc.font.descent = dc.font.xfont->descent;
213 }
214 dc.font.height = dc.font.ascent + dc.font.descent;
215 }
217 unsigned int
218 textw(const char *text) {
219 return textnw(text, strlen(text)) + dc.font.height;
220 }