dwm-meillo
view draw.c @ 202:f5b952e566da
applied endless loop prevention on zoom()
author | arg@10ksloc.org |
---|---|
date | Mon, 07 Aug 2006 08:05:04 +0200 |
parents | 1db04019684e |
children | e4bfe46c7910 |
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, Bool border)
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);
56 w = 0;
57 if(border)
58 drawborder();
60 if(!text)
61 return;
63 len = strlen(text);
64 if(len >= sizeof(buf))
65 len = sizeof(buf) - 1;
66 memcpy(buf, text, len);
67 buf[len] = 0;
69 h = dc.font.ascent + dc.font.descent;
70 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
71 x = dc.x + (h / 2);
73 /* shorten text if necessary */
74 while(len && (w = textnw(buf, len)) > dc.w - h)
75 buf[--len] = 0;
77 if(w > dc.w)
78 return; /* too long */
80 gcv.foreground = invert ? dc.bg : dc.fg;
81 gcv.background = invert ? dc.fg : dc.bg;
82 if(dc.font.set) {
83 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
84 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
85 x, y, buf, len);
86 }
87 else {
88 gcv.font = dc.font.xfont->fid;
89 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
90 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
91 }
92 }
94 /* extern */
96 void
97 drawall()
98 {
99 Client *c;
101 for(c = clients; c; c = getnext(c->next))
102 drawtitle(c);
103 drawstatus();
104 }
106 void
107 drawstatus()
108 {
109 int i, x;
110 Bool istile = arrange == dotile;
112 dc.x = dc.y = 0;
113 dc.w = bw;
114 drawtext(NULL, !istile, False);
116 dc.w = 0;
117 for(i = 0; i < ntags; i++) {
118 dc.x += dc.w;
119 dc.w = textw(tags[i]);
120 if(istile)
121 drawtext(tags[i], (i == tsel), True);
122 else
123 drawtext(tags[i], (i != tsel), True);
124 }
125 x = dc.x + dc.w;
126 dc.w = textw(stext);
127 dc.x = bx + bw - dc.w;
128 drawtext(stext, !istile, False);
129 if(sel && ((dc.w = dc.x - x) >= bh)) {
130 dc.x = x;
131 drawtext(sel->name, istile, True);
132 }
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 < ntags; i++) {
157 if(c->tags[i]) {
158 dc.x += dc.w;
159 dc.w = textw(tags[i]);
160 drawtext(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 Colormap cmap = DefaultColormap(dpy, screen);
174 XColor color;
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(const char *text)
233 {
234 return textnw(text, strlen(text)) + dc.font.height;
235 }