dwm-meillo

view draw.c @ 76:4bd49f404f10

proceeded with cleaning up, sorting functions, etc
author Anselm R. Garbe <garbeam@wmii.de>
date Sat, 15 Jul 2006 17:00:56 +0200
parents f08271b7cb20
children 38c8f7f7d401
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 functions */
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 /* extern functions */
34 void
35 drawall()
36 {
37 Client *c;
39 for(c = clients; c; c = getnext(c->next))
40 drawtitle(c);
41 drawstatus();
42 }
44 void
45 drawstatus()
46 {
47 int i;
48 Bool istile = arrange == dotile;
50 dc.x = dc.y = 0;
51 dc.w = bw;
52 drawtext(NULL, !istile, False);
54 dc.w = 0;
55 for(i = 0; i < TLast; i++) {
56 dc.x += dc.w;
57 dc.w = textw(tags[i]);
58 if(istile)
59 drawtext(tags[i], (i == tsel), True);
60 else
61 drawtext(tags[i], (i != tsel), True);
62 }
63 if(sel) {
64 dc.x += dc.w;
65 dc.w = textw(sel->name);
66 drawtext(sel->name, istile, True);
67 }
68 dc.w = textw(stext);
69 dc.x = bx + bw - dc.w;
70 drawtext(stext, !istile, False);
72 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
73 XFlush(dpy);
74 }
76 void
77 drawtext(const char *text, Bool invert, Bool border)
78 {
79 int x, y, w, h;
80 unsigned int len;
81 static char buf[256];
82 XGCValues gcv;
83 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
85 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
86 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
88 w = 0;
89 if(border)
90 drawborder();
92 if(!text)
93 return;
95 len = strlen(text);
96 if(len >= sizeof(buf))
97 len = sizeof(buf) - 1;
98 memcpy(buf, text, len);
99 buf[len] = 0;
101 h = dc.font.ascent + dc.font.descent;
102 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
103 x = dc.x + (h / 2);
105 /* shorten text if necessary */
106 while(len && (w = textnw(buf, len)) > dc.w - h)
107 buf[--len] = 0;
109 if(w > dc.w)
110 return; /* too long */
112 gcv.foreground = invert ? dc.bg : dc.fg;
113 gcv.background = invert ? dc.fg : dc.bg;
114 if(dc.font.set) {
115 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
116 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
117 x, y, buf, len);
118 }
119 else {
120 gcv.font = dc.font.xfont->fid;
121 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
122 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
123 }
124 }
126 void
127 drawtitle(Client *c)
128 {
129 int i;
130 Bool istile = arrange == dotile;
132 if(c == sel) {
133 drawstatus();
134 XUnmapWindow(dpy, c->title);
135 XSetWindowBorder(dpy, c->win, dc.fg);
136 return;
137 }
139 XSetWindowBorder(dpy, c->win, dc.bg);
140 XMapWindow(dpy, c->title);
142 dc.x = dc.y = 0;
144 dc.w = 0;
145 for(i = 0; i < TLast; i++) {
146 if(c->tags[i]) {
147 dc.x += dc.w;
148 dc.w = textw(c->tags[i]);
149 drawtext(c->tags[i], !istile, True);
150 }
151 }
152 dc.x += dc.w;
153 dc.w = textw(c->name);
154 drawtext(c->name, !istile, True);
155 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
156 0, 0, c->tw, c->th, 0, 0);
157 XFlush(dpy);
158 }
160 unsigned long
161 getcolor(const char *colstr)
162 {
163 XColor color;
164 Colormap cmap = DefaultColormap(dpy, screen);
166 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
167 return color.pixel;
168 }
170 void
171 setfont(const char *fontstr)
172 {
173 char **missing, *def;
174 int i, n;
176 missing = NULL;
177 setlocale(LC_ALL, "");
178 if(dc.font.set)
179 XFreeFontSet(dpy, dc.font.set);
180 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
181 if(missing) {
182 while(n--)
183 fprintf(stderr, "missing fontset: %s\n", missing[n]);
184 XFreeStringList(missing);
185 if(dc.font.set) {
186 XFreeFontSet(dpy, dc.font.set);
187 dc.font.set = NULL;
188 }
189 }
190 if(dc.font.set) {
191 XFontSetExtents *font_extents;
192 XFontStruct **xfonts;
193 char **font_names;
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;
203 xfonts++;
204 }
205 }
206 else {
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)
214 eprint("error, cannot init 'fixed' font\n");
215 dc.font.ascent = dc.font.xfont->ascent;
216 dc.font.descent = dc.font.xfont->descent;
217 }
218 dc.font.height = dc.font.ascent + dc.font.descent;
219 }
221 unsigned int
222 textnw(char *text, unsigned int len)
223 {
224 XRectangle r;
225 if(dc.font.set) {
226 XmbTextExtents(dc.font.set, text, len, NULL, &r);
227 return r.width;
228 }
229 return XTextWidth(dc.font.xfont, text, len);
230 }
232 unsigned int
233 textw(char *text)
234 {
235 return textnw(text, strlen(text)) + dc.font.height;
236 }