aewl
view draw.c @ 604:2c366b252717
dood
author | arg@mig29 |
---|---|
date | Fri, 01 Dec 2006 16:55:42 +0100 |
parents | 3c765cc4bce4 |
children | 755d8dcb8a05 |
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 hborder, Bool vborder) {
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(vborder) {
77 XDrawLine(dpy, dc.drawable, dc.gc, dc.x + 1, dc.y + 1, dc.x + 1, dc.y + dc.h - 1);
78 XDrawLine(dpy, dc.drawable, dc.gc, dc.x + dc.w - 2, dc.y + 1, dc.x + dc.w - 2, dc.y + dc.h - 1);
79 }
80 if(hborder) {
81 XDrawLine(dpy, dc.drawable, dc.gc, dc.x + 1, dc.y + 1, dc.x + dc.w - 2, dc.y + 1);
82 XDrawLine(dpy, dc.drawable, dc.gc, dc.x + 1, dc.y + dc.h - 2, dc.x + dc.w - 2, dc.y + dc.h - 2);
83 }
84 }
86 /* extern */
88 void
89 drawall(void) {
90 Client *c;
92 for(c = clients; c; c = getnext(c->next))
93 drawtitle(c);
94 drawstatus();
95 }
97 void
98 drawstatus(void) {
99 int i, x;
101 dc.x = dc.y = 0;
102 for(i = 0; i < ntags; i++) {
103 dc.w = textw(tags[i]);
104 if(seltag[i])
105 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
106 else
107 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
108 dc.x += dc.w;
109 }
110 dc.w = bmw;
111 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
112 x = dc.x + dc.w;
113 dc.w = textw(stext);
114 dc.x = bw - dc.w;
115 if(dc.x < x) {
116 dc.x = x;
117 dc.w = bw - x;
118 }
119 drawtext(stext, dc.status, False, False);
120 if((dc.w = dc.x - x) > bh) {
121 dc.x = x;
122 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
123 }
124 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
125 XSync(dpy, False);
126 }
128 void
129 drawtitle(Client *c) {
130 if(c == sel && issel) {
131 drawstatus();
132 XUnmapWindow(dpy, c->twin);
133 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
134 return;
135 }
136 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
137 XMapWindow(dpy, c->twin);
138 dc.x = dc.y = 0;
139 dc.w = c->tw;
140 drawtext(c->name, dc.norm, False,False);
141 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
142 XSync(dpy, False);
143 }
145 unsigned long
146 getcolor(const char *colstr) {
147 Colormap cmap = DefaultColormap(dpy, screen);
148 XColor color;
150 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
151 eprint("error, cannot allocate color '%s'\n", colstr);
152 return color.pixel;
153 }
155 void
156 setfont(const char *fontstr) {
157 char **missing, *def;
158 int i, n;
160 missing = NULL;
161 setlocale(LC_ALL, "");
162 if(dc.font.set)
163 XFreeFontSet(dpy, dc.font.set);
164 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
165 if(missing) {
166 while(n--)
167 fprintf(stderr, "missing fontset: %s\n", missing[n]);
168 XFreeStringList(missing);
169 if(dc.font.set) {
170 XFreeFontSet(dpy, dc.font.set);
171 dc.font.set = NULL;
172 }
173 }
174 if(dc.font.set) {
175 XFontSetExtents *font_extents;
176 XFontStruct **xfonts;
177 char **font_names;
178 dc.font.ascent = dc.font.descent = 0;
179 font_extents = XExtentsOfFontSet(dc.font.set);
180 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
181 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
182 if(dc.font.ascent < (*xfonts)->ascent)
183 dc.font.ascent = (*xfonts)->ascent;
184 if(dc.font.descent < (*xfonts)->descent)
185 dc.font.descent = (*xfonts)->descent;
186 xfonts++;
187 }
188 }
189 else {
190 if(dc.font.xfont)
191 XFreeFont(dpy, dc.font.xfont);
192 dc.font.xfont = NULL;
193 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
194 if (!dc.font.xfont)
195 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
196 if (!dc.font.xfont)
197 eprint("error, cannot init 'fixed' font\n");
198 dc.font.ascent = dc.font.xfont->ascent;
199 dc.font.descent = dc.font.xfont->descent;
200 }
201 dc.font.height = dc.font.ascent + dc.font.descent;
202 }
204 unsigned int
205 textw(const char *text) {
206 return textnw(text, strlen(text)) + dc.font.height;
207 }