Mercurial > aewl
annotate draw.c @ 236:ebecb98a1c29
drawing border with fg color
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Thu, 10 Aug 2006 11:07:27 +0200 |
parents | 60e73ebaab27 |
children | 7f8f7f14e9cd |
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 */ | |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
5 #include "dwm.h" |
2 | 6 #include <stdio.h> |
7 #include <string.h> | |
32 | 8 #include <X11/Xlocale.h> |
9 | |
84
052fe7498930
ordered variables in structs and source files alphabetically
Anselm R. Garbe <garbeam@wmii.de>
parents:
79
diff
changeset
|
10 /* static */ |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
11 |
77 | 12 static unsigned int |
173
1db04019684e
changed Client->tags and Rule->tags to be Bool (I'll also try to remove the TLast enum)
arg@10ksloc.org
parents:
164
diff
changeset
|
13 textnw(const char *text, unsigned int len) |
75 | 14 { |
77 | 15 XRectangle r; |
123 | 16 |
77 | 17 if(dc.font.set) { |
18 XmbTextExtents(dc.font.set, text, len, NULL, &r); | |
19 return r.width; | |
20 } | |
21 return XTextWidth(dc.font.xfont, text, len); | |
75 | 22 } |
23 | |
77 | 24 static void |
218 | 25 drawtext(const char *text, Bool invert) |
2 | 26 { |
33
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
27 int x, y, w, h; |
123 | 28 static char buf[256]; |
33
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
29 unsigned int len; |
2 | 30 XGCValues gcv; |
236
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
31 XPoint points[5]; |
34 | 32 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
2 | 33 |
66
50450aa24a46
removed a bunch of lines through swap removal
Anselm R. Garbe <garbeam@wmii.de>
parents:
57
diff
changeset
|
34 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg); |
34 | 35 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
36 if(!text) |
2 | 37 return; |
38 | |
218 | 39 w = 0; |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
40 len = strlen(text); |
2 | 41 if(len >= sizeof(buf)) |
42 len = sizeof(buf) - 1; | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
43 memcpy(buf, text, len); |
2 | 44 buf[len] = 0; |
45 | |
34 | 46 h = dc.font.ascent + dc.font.descent; |
47 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | |
48 x = dc.x + (h / 2); | |
2 | 49 |
50 /* shorten text if necessary */ | |
43 | 51 while(len && (w = textnw(buf, len)) > dc.w - h) |
2 | 52 buf[--len] = 0; |
53 | |
34 | 54 if(w > dc.w) |
2 | 55 return; /* too long */ |
56 | |
66
50450aa24a46
removed a bunch of lines through swap removal
Anselm R. Garbe <garbeam@wmii.de>
parents:
57
diff
changeset
|
57 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
|
58 gcv.background = invert ? dc.fg : dc.bg; |
34 | 59 if(dc.font.set) { |
60 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv); | |
61 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc, | |
2 | 62 x, y, buf, len); |
63 } | |
64 else { | |
34 | 65 gcv.font = dc.font.xfont->fid; |
66 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv); | |
67 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len); | |
2 | 68 } |
236
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
69 |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
70 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
71 points[0].x = dc.x; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
72 points[0].y = dc.y; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
73 points[1].x = dc.w - 1; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
74 points[1].y = 0; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
75 points[2].x = 0; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
76 points[2].y = dc.h - 1; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
77 points[3].x = -(dc.w - 1); |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
78 points[3].y = 0; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
79 points[4].x = 0; |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
80 points[4].y = -(dc.h - 1); |
ebecb98a1c29
drawing border with fg color
Anselm R.Garbe <arg@10ksloc.org>
parents:
235
diff
changeset
|
81 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious); |
2 | 82 } |
83 | |
84
052fe7498930
ordered variables in structs and source files alphabetically
Anselm R. Garbe <garbeam@wmii.de>
parents:
79
diff
changeset
|
84 /* extern */ |
77 | 85 |
86 void | |
87 drawall() | |
88 { | |
89 Client *c; | |
90 | |
142
9b9deafa0508
committed a patch which fixes the hints of Jukka
arg@10ksloc.org
parents:
124
diff
changeset
|
91 for(c = clients; c; c = getnext(c->next)) |
77 | 92 drawtitle(c); |
93 drawstatus(); | |
94 } | |
95 | |
96 void | |
97 drawstatus() | |
98 { | |
124
75576e44c1d8
made status bar drawing more robust, implemented togglemax and togglemode, works quite well
arg@10ksloc.org
parents:
123
diff
changeset
|
99 int i, x; |
77 | 100 Bool istile = arrange == dotile; |
101 | |
102 dc.x = dc.y = 0; | |
103 dc.w = bw; | |
218 | 104 drawtext(NULL, !istile); |
77 | 105 |
106 dc.w = 0; | |
178
e848966a1ac6
removed TLast tag enum, now tags is simple defined as char *[] array, the rest is calculated correctly, rules take an int array for the tags
arg@10ksloc.org
parents:
173
diff
changeset
|
107 for(i = 0; i < ntags; i++) { |
77 | 108 dc.x += dc.w; |
109 dc.w = textw(tags[i]); | |
110 if(istile) | |
218 | 111 drawtext(tags[i], (i == tsel)); |
77 | 112 else |
218 | 113 drawtext(tags[i], (i != tsel)); |
77 | 114 } |
124
75576e44c1d8
made status bar drawing more robust, implemented togglemax and togglemode, works quite well
arg@10ksloc.org
parents:
123
diff
changeset
|
115 x = dc.x + dc.w; |
77 | 116 dc.w = textw(stext); |
117 dc.x = bx + bw - dc.w; | |
218 | 118 drawtext(stext, !istile); |
124
75576e44c1d8
made status bar drawing more robust, implemented togglemax and togglemode, works quite well
arg@10ksloc.org
parents:
123
diff
changeset
|
119 if(sel && ((dc.w = dc.x - x) >= bh)) { |
75576e44c1d8
made status bar drawing more robust, implemented togglemax and togglemode, works quite well
arg@10ksloc.org
parents:
123
diff
changeset
|
120 dc.x = x; |
218 | 121 drawtext(sel->name, istile); |
124
75576e44c1d8
made status bar drawing more robust, implemented togglemax and togglemode, works quite well
arg@10ksloc.org
parents:
123
diff
changeset
|
122 } |
77 | 123 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0); |
79
aabebd6e61f3
fixed XSync handling and finished man page
Anselm R. Garbe <garbeam@wmii.de>
parents:
77
diff
changeset
|
124 XSync(dpy, False); |
77 | 125 } |
126 | |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
127 void |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
128 drawtitle(Client *c) |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
129 { |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
130 int i; |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
131 Bool istile = arrange == dotile; |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
132 |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
133 if(c == sel) { |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
134 drawstatus(); |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
135 XUnmapWindow(dpy, c->title); |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
136 XSetWindowBorder(dpy, c->win, dc.fg); |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
137 return; |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
138 } |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
139 |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
140 XSetWindowBorder(dpy, c->win, dc.bg); |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
141 XMapWindow(dpy, c->title); |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
142 |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
143 dc.x = dc.y = 0; |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
144 |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
145 dc.w = 0; |
178
e848966a1ac6
removed TLast tag enum, now tags is simple defined as char *[] array, the rest is calculated correctly, rules take an int array for the tags
arg@10ksloc.org
parents:
173
diff
changeset
|
146 for(i = 0; i < ntags; i++) { |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
147 if(c->tags[i]) { |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
148 dc.x += dc.w; |
173
1db04019684e
changed Client->tags and Rule->tags to be Bool (I'll also try to remove the TLast enum)
arg@10ksloc.org
parents:
164
diff
changeset
|
149 dc.w = textw(tags[i]); |
218 | 150 drawtext(tags[i], !istile); |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
151 } |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
152 } |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
153 dc.x += dc.w; |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
154 dc.w = textw(c->name); |
218 | 155 drawtext(c->name, !istile); |
79
aabebd6e61f3
fixed XSync handling and finished man page
Anselm R. Garbe <garbeam@wmii.de>
parents:
77
diff
changeset
|
156 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0); |
aabebd6e61f3
fixed XSync handling and finished man page
Anselm R. Garbe <garbeam@wmii.de>
parents:
77
diff
changeset
|
157 XSync(dpy, False); |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
158 } |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
159 |
43 | 160 unsigned long |
74 | 161 getcolor(const char *colstr) |
2 | 162 { |
123 | 163 Colormap cmap = DefaultColormap(dpy, screen); |
2 | 164 XColor color; |
43 | 165 |
2 | 166 XAllocNamedColor(dpy, cmap, colstr, &color, &color); |
167 return color.pixel; | |
168 } | |
169 | |
170 void | |
74 | 171 setfont(const char *fontstr) |
2 | 172 { |
173 char **missing, *def; | |
33
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
174 int i, n; |
2 | 175 |
7 | 176 missing = NULL; |
2 | 177 setlocale(LC_ALL, ""); |
43 | 178 if(dc.font.set) |
179 XFreeFontSet(dpy, dc.font.set); | |
180 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); | |
2 | 181 if(missing) { |
182 while(n--) | |
183 fprintf(stderr, "missing fontset: %s\n", missing[n]); | |
184 XFreeStringList(missing); | |
43 | 185 if(dc.font.set) { |
186 XFreeFontSet(dpy, dc.font.set); | |
187 dc.font.set = NULL; | |
2 | 188 } |
189 } | |
43 | 190 if(dc.font.set) { |
2 | 191 XFontSetExtents *font_extents; |
192 XFontStruct **xfonts; | |
193 char **font_names; | |
194 | |
43 | 195 dc.font.ascent = dc.font.descent = 0; |
196 font_extents = XExtentsOfFontSet(dc.font.set); | |
197 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); | |
198 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { | |
199 if(dc.font.ascent < (*xfonts)->ascent) | |
200 dc.font.ascent = (*xfonts)->ascent; | |
201 if(dc.font.descent < (*xfonts)->descent) | |
202 dc.font.descent = (*xfonts)->descent; | |
2 | 203 xfonts++; |
204 } | |
205 } | |
206 else { | |
43 | 207 if(dc.font.xfont) |
208 XFreeFont(dpy, dc.font.xfont); | |
209 dc.font.xfont = NULL; | |
210 dc.font.xfont = XLoadQueryFont(dpy, fontstr); | |
211 if (!dc.font.xfont) | |
212 dc.font.xfont = XLoadQueryFont(dpy, "fixed"); | |
213 if (!dc.font.xfont) | |
75 | 214 eprint("error, cannot init 'fixed' font\n"); |
43 | 215 dc.font.ascent = dc.font.xfont->ascent; |
216 dc.font.descent = dc.font.xfont->descent; | |
2 | 217 } |
43 | 218 dc.font.height = dc.font.ascent + dc.font.descent; |
2 | 219 } |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
220 |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
221 unsigned int |
173
1db04019684e
changed Client->tags and Rule->tags to be Bool (I'll also try to remove the TLast enum)
arg@10ksloc.org
parents:
164
diff
changeset
|
222 textw(const char *text) |
76
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
223 { |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
224 return textnw(text, strlen(text)) + dc.font.height; |
4bd49f404f10
proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents:
75
diff
changeset
|
225 } |