dwm-meillo
view draw.c @ 116:eca09aac3ccc
makefile now sets permissions for executables and man pages
author | arg@10ksloc.org |
---|---|
date | Thu, 20 Jul 2006 09:45:47 +0200 |
parents | 052fe7498930 |
children | 61490330e90a |
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"
7 #include <stdio.h>
8 #include <string.h>
9 #include <X11/Xlocale.h>
11 /* static */
13 static void
14 drawborder(void)
15 {
16 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(char *text, unsigned int len)
34 {
35 XRectangle r;
36 if(dc.font.set) {
37 XmbTextExtents(dc.font.set, text, len, NULL, &r);
38 return r.width;
39 }
40 return XTextWidth(dc.font.xfont, text, len);
41 }
43 static void
44 drawtext(const char *text, Bool invert, Bool border)
45 {
46 int x, y, w, h;
47 unsigned int len;
48 static char buf[256];
49 XGCValues gcv;
50 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
52 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
53 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
55 w = 0;
56 if(border)
57 drawborder();
59 if(!text)
60 return;
62 len = strlen(text);
63 if(len >= sizeof(buf))
64 len = sizeof(buf) - 1;
65 memcpy(buf, text, len);
66 buf[len] = 0;
68 h = dc.font.ascent + dc.font.descent;
69 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
70 x = dc.x + (h / 2);
72 /* shorten text if necessary */
73 while(len && (w = textnw(buf, len)) > dc.w - h)
74 buf[--len] = 0;
76 if(w > dc.w)
77 return; /* too long */
79 gcv.foreground = invert ? dc.bg : dc.fg;
80 gcv.background = invert ? dc.fg : dc.bg;
81 if(dc.font.set) {
82 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
83 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
84 x, y, buf, len);
85 }
86 else {
87 gcv.font = dc.font.xfont->fid;
88 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
89 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
90 }
91 }
93 /* extern */
95 void
96 drawall()
97 {
98 Client *c;
100 for(c = clients; c; c = getnext(c->next, tsel))
101 drawtitle(c);
102 drawstatus();
103 }
105 void
106 drawstatus()
107 {
108 int i;
109 Bool istile = arrange == dotile;
111 dc.x = dc.y = 0;
112 dc.w = bw;
113 drawtext(NULL, !istile, False);
115 dc.w = 0;
116 for(i = 0; i < TLast; i++) {
117 dc.x += dc.w;
118 dc.w = textw(tags[i]);
119 if(istile)
120 drawtext(tags[i], (i == tsel), True);
121 else
122 drawtext(tags[i], (i != tsel), True);
123 }
124 if(sel) {
125 dc.x += dc.w;
126 dc.w = textw(sel->name);
127 drawtext(sel->name, istile, True);
128 }
129 dc.w = textw(stext);
130 dc.x = bx + bw - dc.w;
131 drawtext(stext, !istile, False);
133 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
134 XSync(dpy, False);
135 }
137 void
138 drawtitle(Client *c)
139 {
140 int i;
141 Bool istile = arrange == dotile;
143 if(c == sel) {
144 drawstatus();
145 XUnmapWindow(dpy, c->title);
146 XSetWindowBorder(dpy, c->win, dc.fg);
147 return;
148 }
150 XSetWindowBorder(dpy, c->win, dc.bg);
151 XMapWindow(dpy, c->title);
153 dc.x = dc.y = 0;
155 dc.w = 0;
156 for(i = 0; i < TLast; i++) {
157 if(c->tags[i]) {
158 dc.x += dc.w;
159 dc.w = textw(c->tags[i]);
160 drawtext(c->tags[i], !istile, True);
161 }
162 }
163 dc.x += dc.w;
164 dc.w = textw(c->name);
165 drawtext(c->name, !istile, True);
166 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
167 XSync(dpy, False);
168 }
170 unsigned long
171 getcolor(const char *colstr)
172 {
173 XColor color;
174 Colormap cmap = DefaultColormap(dpy, screen);
176 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
177 return color.pixel;
178 }
180 void
181 setfont(const char *fontstr)
182 {
183 char **missing, *def;
184 int i, n;
186 missing = NULL;
187 setlocale(LC_ALL, "");
188 if(dc.font.set)
189 XFreeFontSet(dpy, dc.font.set);
190 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
191 if(missing) {
192 while(n--)
193 fprintf(stderr, "missing fontset: %s\n", missing[n]);
194 XFreeStringList(missing);
195 if(dc.font.set) {
196 XFreeFontSet(dpy, dc.font.set);
197 dc.font.set = NULL;
198 }
199 }
200 if(dc.font.set) {
201 XFontSetExtents *font_extents;
202 XFontStruct **xfonts;
203 char **font_names;
205 dc.font.ascent = dc.font.descent = 0;
206 font_extents = XExtentsOfFontSet(dc.font.set);
207 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
208 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
209 if(dc.font.ascent < (*xfonts)->ascent)
210 dc.font.ascent = (*xfonts)->ascent;
211 if(dc.font.descent < (*xfonts)->descent)
212 dc.font.descent = (*xfonts)->descent;
213 xfonts++;
214 }
215 }
216 else {
217 if(dc.font.xfont)
218 XFreeFont(dpy, dc.font.xfont);
219 dc.font.xfont = NULL;
220 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
221 if (!dc.font.xfont)
222 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
223 if (!dc.font.xfont)
224 eprint("error, cannot init 'fixed' font\n");
225 dc.font.ascent = dc.font.xfont->ascent;
226 dc.font.descent = dc.font.xfont->descent;
227 }
228 dc.font.height = dc.font.ascent + dc.font.descent;
229 }
231 unsigned int
232 textw(char *text)
233 {
234 return textnw(text, strlen(text)) + dc.font.height;
235 }