aewl

view draw.c @ 602:3c765cc4bce4

less obtrusive indicator (only a top line)
author arg@mig29
date Fri, 01 Dec 2006 15:03:42 +0100
parents bfea29f1e4a4
children 2c366b252717
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 dot, Bool line) {
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 };
40 XSetForeground(dpy, dc.gc, col[ColBG]);
41 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
42 if(!text)
43 return;
44 w = 0;
45 olen = len = strlen(text);
46 if(len >= sizeof buf)
47 len = sizeof buf - 1;
48 memcpy(buf, text, len);
49 buf[len] = 0;
50 h = dc.font.ascent + dc.font.descent;
51 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
52 x = dc.x + (h / 2);
53 /* shorten text if necessary */
54 while(len && (w = textnw(buf, len)) > dc.w - h)
55 buf[--len] = 0;
56 if(len < olen) {
57 if(len > 1)
58 buf[len - 1] = '.';
59 if(len > 2)
60 buf[len - 2] = '.';
61 if(len > 3)
62 buf[len - 3] = '.';
63 }
64 if(w > dc.w)
65 return; /* too long */
66 gcv.foreground = col[ColFG];
67 if(dc.font.set) {
68 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
69 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
70 }
71 else {
72 gcv.font = dc.font.xfont->fid;
73 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
74 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
75 }
76 if(dot) {
77 r.x = dc.x + 1;
78 r.y = dc.y + 2;
79 r.width = r.height = (h + 2) / 4;
80 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
81 }
82 if(line)
83 XDrawLine(dpy, dc.drawable, dc.gc, dc.x + 1, dc.y + 1, dc.x + dc.w - 1, dc.y + 1); }
85 /* extern */
87 void
88 drawall(void) {
89 Client *c;
91 for(c = clients; c; c = getnext(c->next))
92 drawtitle(c);
93 drawstatus();
94 }
96 void
97 drawstatus(void) {
98 int i, x;
100 dc.x = dc.y = 0;
101 for(i = 0; i < ntags; i++) {
102 dc.w = textw(tags[i]);
103 if(seltag[i])
104 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
105 else
106 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
107 dc.x += dc.w;
108 }
109 dc.w = bmw;
110 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
111 x = dc.x + dc.w;
112 dc.w = textw(stext);
113 dc.x = bw - dc.w;
114 if(dc.x < x) {
115 dc.x = x;
116 dc.w = bw - x;
117 }
118 drawtext(stext, dc.status, False, False);
119 if((dc.w = dc.x - x) > bh) {
120 dc.x = x;
121 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
122 }
123 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
124 XSync(dpy, False);
125 }
127 void
128 drawtitle(Client *c) {
129 if(c == sel && issel) {
130 drawstatus();
131 XUnmapWindow(dpy, c->twin);
132 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
133 return;
134 }
135 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
136 XMapWindow(dpy, c->twin);
137 dc.x = dc.y = 0;
138 dc.w = c->tw;
139 drawtext(c->name, dc.norm, False,False);
140 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
141 XSync(dpy, False);
142 }
144 unsigned long
145 getcolor(const char *colstr) {
146 Colormap cmap = DefaultColormap(dpy, screen);
147 XColor color;
149 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
150 eprint("error, cannot allocate color '%s'\n", colstr);
151 return color.pixel;
152 }
154 void
155 setfont(const char *fontstr) {
156 char **missing, *def;
157 int i, n;
159 missing = NULL;
160 setlocale(LC_ALL, "");
161 if(dc.font.set)
162 XFreeFontSet(dpy, dc.font.set);
163 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
164 if(missing) {
165 while(n--)
166 fprintf(stderr, "missing fontset: %s\n", missing[n]);
167 XFreeStringList(missing);
168 if(dc.font.set) {
169 XFreeFontSet(dpy, dc.font.set);
170 dc.font.set = NULL;
171 }
172 }
173 if(dc.font.set) {
174 XFontSetExtents *font_extents;
175 XFontStruct **xfonts;
176 char **font_names;
177 dc.font.ascent = dc.font.descent = 0;
178 font_extents = XExtentsOfFontSet(dc.font.set);
179 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
180 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
181 if(dc.font.ascent < (*xfonts)->ascent)
182 dc.font.ascent = (*xfonts)->ascent;
183 if(dc.font.descent < (*xfonts)->descent)
184 dc.font.descent = (*xfonts)->descent;
185 xfonts++;
186 }
187 }
188 else {
189 if(dc.font.xfont)
190 XFreeFont(dpy, dc.font.xfont);
191 dc.font.xfont = NULL;
192 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
193 if (!dc.font.xfont)
194 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
195 if (!dc.font.xfont)
196 eprint("error, cannot init 'fixed' font\n");
197 dc.font.ascent = dc.font.xfont->ascent;
198 dc.font.descent = dc.font.xfont->descent;
199 }
200 dc.font.height = dc.font.ascent + dc.font.descent;
201 }
203 unsigned int
204 textw(const char *text) {
205 return textnw(text, strlen(text)) + dc.font.height;
206 }