Mercurial > aewl
annotate draw.c @ 75:f08271b7cb20
rearranged several stuff
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Sat, 15 Jul 2006 16:30:50 +0200 |
parents | 5370ef170cc9 |
children | 4bd49f404f10 |
rev | line source |
---|---|
2 | 1 /* |
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> | |
3 * See LICENSE file for license details. | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <string.h> | |
8 | |
32 | 9 #include <X11/Xlocale.h> |
10 | |
43 | 11 #include "dwm.h" |
2 | 12 |
73 | 13 void |
75 | 14 drawall() |
15 { | |
16 Client *c; | |
17 | |
18 for(c = clients; c; c = getnext(c->next)) | |
19 drawtitle(c); | |
20 drawstatus(); | |
21 } | |
22 | |
23 void | |
74 | 24 drawstatus() |
73 | 25 { |
26 int i; | |
75 | 27 Bool istile = arrange == dotile; |
73 | 28 |
29 dc.x = dc.y = 0; | |
30 dc.w = bw; | |
75 | 31 drawtext(NULL, !istile, False); |
73 | 32 |
75 | 33 dc.w = 0; |
73 | 34 for(i = 0; i < TLast; i++) { |
35 dc.x += dc.w; | |
36 dc.w = textw(tags[i]); | |
75 | 37 if(istile) |
38 drawtext(tags[i], (i == tsel), True); | |
39 else | |
40 drawtext(tags[i], (i != tsel), True); | |
73 | 41 } |
42 if(sel) { | |
43 dc.x += dc.w; | |
44 dc.w = textw(sel->name); | |
75 | 45 drawtext(sel->name, istile, True); |
73 | 46 } |
47 dc.w = textw(stext); | |
48 dc.x = bx + bw - dc.w; | |
75 | 49 drawtext(stext, !istile, False); |
73 | 50 |
51 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0); | |
52 XFlush(dpy); | |
53 } | |
54 | |
55 void | |
74 | 56 drawtitle(Client *c) |
73 | 57 { |
58 int i; | |
75 | 59 Bool istile = arrange == dotile; |
60 | |
73 | 61 if(c == sel) { |
74 | 62 drawstatus(); |
73 | 63 XUnmapWindow(dpy, c->title); |
64 XSetWindowBorder(dpy, c->win, dc.fg); | |
65 return; | |
66 } | |
67 | |
68 XSetWindowBorder(dpy, c->win, dc.bg); | |
69 XMapWindow(dpy, c->title); | |
70 | |
71 dc.x = dc.y = 0; | |
72 | |
73 dc.w = 0; | |
74 for(i = 0; i < TLast; i++) { | |
75 if(c->tags[i]) { | |
76 dc.x += dc.w; | |
77 dc.w = textw(c->tags[i]); | |
75 | 78 drawtext(c->tags[i], !istile, True); |
73 | 79 } |
80 } | |
81 dc.x += dc.w; | |
82 dc.w = textw(c->name); | |
75 | 83 drawtext(c->name, !istile, True); |
73 | 84 XCopyArea(dpy, dc.drawable, c->title, dc.gc, |
85 0, 0, c->tw, c->th, 0, 0); | |
86 XFlush(dpy); | |
87 } | |
88 | |
2 | 89 static void |
34 | 90 drawborder(void) |
2 | 91 { |
92 XPoint points[5]; | |
34 | 93 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); |
94 XSetForeground(dpy, dc.gc, dc.border); | |
95 points[0].x = dc.x; | |
96 points[0].y = dc.y; | |
97 points[1].x = dc.w - 1; | |
2 | 98 points[1].y = 0; |
99 points[2].x = 0; | |
34 | 100 points[2].y = dc.h - 1; |
101 points[3].x = -(dc.w - 1); | |
2 | 102 points[3].y = 0; |
103 points[4].x = 0; | |
34 | 104 points[4].y = -(dc.h - 1); |
105 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious); | |
2 | 106 } |
107 | |
108 void | |
66
50450aa24a46
removed a bunch of lines through swap removal
Anselm R. Garbe <garbeam@wmii.de>
parents:
57
diff
changeset
|
109 drawtext(const char *text, Bool invert, Bool border) |
2 | 110 { |
33
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
111 int x, y, w, h; |
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
112 unsigned int len; |
2 | 113 static char buf[256]; |
114 XGCValues gcv; | |
34 | 115 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
2 | 116 |
66
50450aa24a46
removed a bunch of lines through swap removal
Anselm R. Garbe <garbeam@wmii.de>
parents:
57
diff
changeset
|
117 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg); |
34 | 118 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
2 | 119 |
30
2e0fb4130bfb
new stuff, fixed several issues
Anselm R. Garbe <garbeam@wmii.de>
parents:
26
diff
changeset
|
120 w = 0; |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
121 if(border) |
34 | 122 drawborder(); |
2 | 123 |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
124 if(!text) |
2 | 125 return; |
126 | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
127 len = strlen(text); |
2 | 128 if(len >= sizeof(buf)) |
129 len = sizeof(buf) - 1; | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
130 memcpy(buf, text, len); |
2 | 131 buf[len] = 0; |
132 | |
34 | 133 h = dc.font.ascent + dc.font.descent; |
134 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | |
135 x = dc.x + (h / 2); | |
2 | 136 |
137 /* shorten text if necessary */ | |
43 | 138 while(len && (w = textnw(buf, len)) > dc.w - h) |
2 | 139 buf[--len] = 0; |
140 | |
34 | 141 if(w > dc.w) |
2 | 142 return; /* too long */ |
143 | |
66
50450aa24a46
removed a bunch of lines through swap removal
Anselm R. Garbe <garbeam@wmii.de>
parents:
57
diff
changeset
|
144 gcv.foreground = invert ? dc.bg : dc.fg; |
50450aa24a46
removed a bunch of lines through swap removal
Anselm R. Garbe <garbeam@wmii.de>
parents:
57
diff
changeset
|
145 gcv.background = invert ? dc.fg : dc.bg; |
34 | 146 if(dc.font.set) { |
147 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv); | |
148 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc, | |
2 | 149 x, y, buf, len); |
150 } | |
151 else { | |
34 | 152 gcv.font = dc.font.xfont->fid; |
153 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv); | |
154 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len); | |
2 | 155 } |
156 } | |
157 | |
43 | 158 unsigned long |
74 | 159 getcolor(const char *colstr) |
2 | 160 { |
161 XColor color; | |
43 | 162 Colormap cmap = DefaultColormap(dpy, screen); |
163 | |
2 | 164 XAllocNamedColor(dpy, cmap, colstr, &color, &color); |
165 return color.pixel; | |
166 } | |
167 | |
43 | 168 unsigned int |
169 textnw(char *text, unsigned int len) | |
2 | 170 { |
43 | 171 XRectangle r; |
172 if(dc.font.set) { | |
173 XmbTextExtents(dc.font.set, text, len, NULL, &r); | |
174 return r.width; | |
175 } | |
176 return XTextWidth(dc.font.xfont, text, len); | |
2 | 177 } |
178 | |
179 unsigned int | |
43 | 180 textw(char *text) |
2 | 181 { |
73 | 182 return textnw(text, strlen(text)) + dc.font.height; |
2 | 183 } |
184 | |
185 void | |
74 | 186 setfont(const char *fontstr) |
2 | 187 { |
188 char **missing, *def; | |
33
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
189 int i, n; |
2 | 190 |
7 | 191 missing = NULL; |
2 | 192 setlocale(LC_ALL, ""); |
43 | 193 if(dc.font.set) |
194 XFreeFontSet(dpy, dc.font.set); | |
195 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); | |
2 | 196 if(missing) { |
197 while(n--) | |
198 fprintf(stderr, "missing fontset: %s\n", missing[n]); | |
199 XFreeStringList(missing); | |
43 | 200 if(dc.font.set) { |
201 XFreeFontSet(dpy, dc.font.set); | |
202 dc.font.set = NULL; | |
2 | 203 } |
204 } | |
43 | 205 if(dc.font.set) { |
2 | 206 XFontSetExtents *font_extents; |
207 XFontStruct **xfonts; | |
208 char **font_names; | |
209 | |
43 | 210 dc.font.ascent = dc.font.descent = 0; |
211 font_extents = XExtentsOfFontSet(dc.font.set); | |
212 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); | |
213 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { | |
214 if(dc.font.ascent < (*xfonts)->ascent) | |
215 dc.font.ascent = (*xfonts)->ascent; | |
216 if(dc.font.descent < (*xfonts)->descent) | |
217 dc.font.descent = (*xfonts)->descent; | |
2 | 218 xfonts++; |
219 } | |
220 } | |
221 else { | |
43 | 222 if(dc.font.xfont) |
223 XFreeFont(dpy, dc.font.xfont); | |
224 dc.font.xfont = NULL; | |
225 dc.font.xfont = XLoadQueryFont(dpy, fontstr); | |
226 if (!dc.font.xfont) | |
227 dc.font.xfont = XLoadQueryFont(dpy, "fixed"); | |
228 if (!dc.font.xfont) | |
75 | 229 eprint("error, cannot init 'fixed' font\n"); |
43 | 230 dc.font.ascent = dc.font.xfont->ascent; |
231 dc.font.descent = dc.font.xfont->descent; | |
2 | 232 } |
43 | 233 dc.font.height = dc.font.ascent + dc.font.descent; |
2 | 234 } |