aewl

view draw.c @ 239:e5390f8e06b9

applied sumik's multihead patch
author Anselm R.Garbe <arg@10ksloc.org>
date Thu, 10 Aug 2006 11:26:32 +0200
parents 7f8f7f14e9cd
children 32dc3acfffc0
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 void
13 drawborder(void)
14 {
15 XPoint points[5];
17 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
18 XSetForeground(dpy, dc.gc, dc.border);
19 points[0].x = dc.x;
20 points[0].y = dc.y;
21 points[1].x = dc.w - 1;
22 points[1].y = 0;
23 points[2].x = 0;
24 points[2].y = dc.h - 1;
25 points[3].x = -(dc.w - 1);
26 points[3].y = 0;
27 points[4].x = 0;
28 points[4].y = -(dc.h - 1);
29 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
30 }
32 static unsigned int
33 textnw(const char *text, unsigned int len)
34 {
35 XRectangle r;
37 if(dc.font.set) {
38 XmbTextExtents(dc.font.set, text, len, NULL, &r);
39 return r.width;
40 }
41 return XTextWidth(dc.font.xfont, text, len);
42 }
44 static void
45 drawtext(const char *text, Bool invert)
46 {
47 int x, y, w, h;
48 static char buf[256];
49 unsigned int len;
50 XGCValues gcv;
51 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
53 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
54 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
55 drawborder();
57 if(!text)
58 return;
60 w = 0;
61 len = strlen(text);
62 if(len >= sizeof(buf))
63 len = sizeof(buf) - 1;
64 memcpy(buf, text, len);
65 buf[len] = 0;
67 h = dc.font.ascent + dc.font.descent;
68 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
69 x = dc.x + (h / 2);
71 /* shorten text if necessary */
72 while(len && (w = textnw(buf, len)) > dc.w - h)
73 buf[--len] = 0;
75 if(w > dc.w)
76 return; /* too long */
78 gcv.foreground = invert ? dc.bg : dc.fg;
79 gcv.background = invert ? dc.fg : dc.bg;
80 if(dc.font.set) {
81 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
82 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
83 x, y, buf, len);
84 }
85 else {
86 gcv.font = dc.font.xfont->fid;
87 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
88 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
89 }
90 }
92 /* extern */
94 void
95 drawall()
96 {
97 Client *c;
99 for(c = clients; c; c = getnext(c->next))
100 drawtitle(c);
101 drawstatus();
102 }
104 void
105 drawstatus()
106 {
107 int i, x;
108 Bool istile = arrange == dotile;
110 dc.x = dc.y = 0;
111 dc.w = bw;
112 drawtext(NULL, !istile);
114 dc.w = 0;
115 for(i = 0; i < ntags; i++) {
116 dc.x += dc.w;
117 dc.w = textw(tags[i]);
118 if(istile)
119 drawtext(tags[i], (i == tsel));
120 else
121 drawtext(tags[i], (i != tsel));
122 }
123 x = dc.x + dc.w;
124 dc.w = textw(stext);
125 dc.x = bx + bw - dc.w;
126 drawtext(stext, !istile);
127 if(sel && ((dc.w = dc.x - x) >= bh)) {
128 dc.x = x;
129 drawtext(sel->name, istile);
130 }
131 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
132 XSync(dpy, False);
133 }
135 void
136 drawtitle(Client *c)
137 {
138 int i;
139 Bool istile = arrange == dotile;
141 if(c == sel && issel) {
142 drawstatus();
143 XUnmapWindow(dpy, c->title);
144 XSetWindowBorder(dpy, c->win, dc.fg);
145 return;
146 }
148 XSetWindowBorder(dpy, c->win, dc.bg);
149 XMapWindow(dpy, c->title);
151 dc.x = dc.y = 0;
153 dc.w = 0;
154 for(i = 0; i < ntags; i++) {
155 if(c->tags[i]) {
156 dc.x += dc.w;
157 dc.w = textw(tags[i]);
158 drawtext(tags[i], !istile);
159 }
160 }
161 dc.x += dc.w;
162 dc.w = textw(c->name);
163 drawtext(c->name, !istile);
164 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
165 XSync(dpy, False);
166 }
168 unsigned long
169 getcolor(const char *colstr)
170 {
171 Colormap cmap = DefaultColormap(dpy, screen);
172 XColor color;
174 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
175 return color.pixel;
176 }
178 void
179 setfont(const char *fontstr)
180 {
181 char **missing, *def;
182 int i, n;
184 missing = NULL;
185 setlocale(LC_ALL, "");
186 if(dc.font.set)
187 XFreeFontSet(dpy, dc.font.set);
188 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
189 if(missing) {
190 while(n--)
191 fprintf(stderr, "missing fontset: %s\n", missing[n]);
192 XFreeStringList(missing);
193 if(dc.font.set) {
194 XFreeFontSet(dpy, dc.font.set);
195 dc.font.set = NULL;
196 }
197 }
198 if(dc.font.set) {
199 XFontSetExtents *font_extents;
200 XFontStruct **xfonts;
201 char **font_names;
203 dc.font.ascent = dc.font.descent = 0;
204 font_extents = XExtentsOfFontSet(dc.font.set);
205 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
206 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
207 if(dc.font.ascent < (*xfonts)->ascent)
208 dc.font.ascent = (*xfonts)->ascent;
209 if(dc.font.descent < (*xfonts)->descent)
210 dc.font.descent = (*xfonts)->descent;
211 xfonts++;
212 }
213 }
214 else {
215 if(dc.font.xfont)
216 XFreeFont(dpy, dc.font.xfont);
217 dc.font.xfont = NULL;
218 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
219 if (!dc.font.xfont)
220 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
221 if (!dc.font.xfont)
222 eprint("error, cannot init 'fixed' font\n");
223 dc.font.ascent = dc.font.xfont->ascent;
224 dc.font.descent = dc.font.xfont->descent;
225 }
226 dc.font.height = dc.font.ascent + dc.font.descent;
227 }
229 unsigned int
230 textw(const char *text)
231 {
232 return textnw(text, strlen(text)) + dc.font.height;
233 }