dwm-meillo

view 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
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 unsigned int
13 textnw(const char *text, unsigned int len)
14 {
15 XRectangle r;
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);
22 }
24 static void
25 drawtext(const char *text, Bool invert)
26 {
27 int x, y, w, h;
28 static char buf[256];
29 unsigned int len;
30 XGCValues gcv;
31 XPoint points[5];
32 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
34 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
35 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
36 if(!text)
37 return;
39 w = 0;
40 len = strlen(text);
41 if(len >= sizeof(buf))
42 len = sizeof(buf) - 1;
43 memcpy(buf, text, len);
44 buf[len] = 0;
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);
50 /* shorten text if necessary */
51 while(len && (w = textnw(buf, len)) > dc.w - h)
52 buf[--len] = 0;
54 if(w > dc.w)
55 return; /* too long */
57 gcv.foreground = invert ? dc.bg : dc.fg;
58 gcv.background = invert ? dc.fg : dc.bg;
59 if(dc.font.set) {
60 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
61 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
62 x, y, buf, len);
63 }
64 else {
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);
68 }
70 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
71 points[0].x = dc.x;
72 points[0].y = dc.y;
73 points[1].x = dc.w - 1;
74 points[1].y = 0;
75 points[2].x = 0;
76 points[2].y = dc.h - 1;
77 points[3].x = -(dc.w - 1);
78 points[3].y = 0;
79 points[4].x = 0;
80 points[4].y = -(dc.h - 1);
81 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
82 }
84 /* extern */
86 void
87 drawall()
88 {
89 Client *c;
91 for(c = clients; c; c = getnext(c->next))
92 drawtitle(c);
93 drawstatus();
94 }
96 void
97 drawstatus()
98 {
99 int i, x;
100 Bool istile = arrange == dotile;
102 dc.x = dc.y = 0;
103 dc.w = bw;
104 drawtext(NULL, !istile);
106 dc.w = 0;
107 for(i = 0; i < ntags; i++) {
108 dc.x += dc.w;
109 dc.w = textw(tags[i]);
110 if(istile)
111 drawtext(tags[i], (i == tsel));
112 else
113 drawtext(tags[i], (i != tsel));
114 }
115 x = dc.x + dc.w;
116 dc.w = textw(stext);
117 dc.x = bx + bw - dc.w;
118 drawtext(stext, !istile);
119 if(sel && ((dc.w = dc.x - x) >= bh)) {
120 dc.x = x;
121 drawtext(sel->name, istile);
122 }
123 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
124 XSync(dpy, False);
125 }
127 void
128 drawtitle(Client *c)
129 {
130 int i;
131 Bool istile = arrange == dotile;
133 if(c == sel) {
134 drawstatus();
135 XUnmapWindow(dpy, c->title);
136 XSetWindowBorder(dpy, c->win, dc.fg);
137 return;
138 }
140 XSetWindowBorder(dpy, c->win, dc.bg);
141 XMapWindow(dpy, c->title);
143 dc.x = dc.y = 0;
145 dc.w = 0;
146 for(i = 0; i < ntags; i++) {
147 if(c->tags[i]) {
148 dc.x += dc.w;
149 dc.w = textw(tags[i]);
150 drawtext(tags[i], !istile);
151 }
152 }
153 dc.x += dc.w;
154 dc.w = textw(c->name);
155 drawtext(c->name, !istile);
156 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
157 XSync(dpy, False);
158 }
160 unsigned long
161 getcolor(const char *colstr)
162 {
163 Colormap cmap = DefaultColormap(dpy, screen);
164 XColor color;
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 textw(const char *text)
223 {
224 return textnw(text, strlen(text)) + dc.font.height;
225 }