dwm-meillo

view draw.c @ 512:aca04c3022c1

fixed the z-layer issue described on mailinglist
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 29 Sep 2006 17:12:57 +0200
parents be4f90c03582
children e5f8d6ac509c
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 XRectangle r;
16 if(dc.font.set) {
17 XmbTextExtents(dc.font.set, text, len, NULL, &r);
18 return r.width;
19 }
20 return XTextWidth(dc.font.xfont, text, len);
21 }
23 static void
24 drawtext(const char *text, unsigned long col[ColLast], Bool highlight) {
25 int x, y, w, h;
26 static char buf[256];
27 unsigned int len, olen;
28 XGCValues gcv;
29 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
31 XSetForeground(dpy, dc.gc, col[ColBG]);
32 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
34 if(!text)
35 return;
37 w = 0;
38 olen = len = strlen(text);
39 if(len >= sizeof(buf))
40 len = sizeof(buf) - 1;
41 memcpy(buf, text, len);
42 buf[len] = 0;
44 h = dc.font.ascent + dc.font.descent;
45 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
46 x = dc.x + (h / 2);
48 /* shorten text if necessary */
49 while(len && (w = textnw(buf, len)) > dc.w - h)
50 buf[--len] = 0;
51 if(len < olen) {
52 if(len > 1)
53 buf[len - 1] = '.';
54 if(len > 2)
55 buf[len - 2] = '.';
56 if(len > 3)
57 buf[len - 3] = '.';
58 }
60 if(w > dc.w)
61 return; /* too long */
62 gcv.foreground = col[ColFG];
63 if(dc.font.set) {
64 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
65 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
66 }
67 else {
68 gcv.font = dc.font.xfont->fid;
69 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
70 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
71 }
72 if(highlight) {
73 r.x = dc.x + 2;
74 r.y = dc.y + 2;
75 r.width = r.height = 3;
76 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
77 }
78 }
80 /* extern */
82 void
83 drawall(void) {
84 Client *c;
86 for(c = clients; c; c = getnext(c->next))
87 drawtitle(c);
88 drawstatus();
89 }
91 void
92 drawstatus(void) {
93 int i, x;
95 dc.x = dc.y = 0;
97 for(i = 0; i < ntags; i++) {
98 dc.w = textw(tags[i]);
99 if(seltag[i])
100 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
101 else
102 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
103 dc.x += dc.w;
104 }
106 dc.w = bmw;
107 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False);
109 x = dc.x + dc.w;
110 dc.w = textw(stext);
111 dc.x = bx + bw - dc.w;
112 if(dc.x < x) {
113 dc.x = x;
114 dc.w = bw - x;
115 }
116 drawtext(stext, dc.status, False);
118 if((dc.w = dc.x - x) > bh) {
119 dc.x = x;
120 if(sel)
121 drawtext(sel->name, dc.sel, False);
122 else
123 drawtext(NULL, dc.norm, False);
124 }
125 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
126 XSync(dpy, False);
127 }
129 void
130 drawtitle(Client *c) {
131 if(c == sel && issel) {
132 drawstatus();
133 XUnmapWindow(dpy, c->twin);
134 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
135 return;
136 }
138 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
139 XMapWindow(dpy, c->twin);
140 dc.x = dc.y = 0;
141 dc.w = c->tw;
142 drawtext(c->name, dc.norm, False);
143 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
144 XSync(dpy, False);
145 }
147 unsigned long
148 getcolor(const char *colstr) {
149 Colormap cmap = DefaultColormap(dpy, screen);
150 XColor color;
152 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
153 eprint("error, cannot allocate color '%s'\n", colstr);
154 return color.pixel;
155 }
157 void
158 setfont(const char *fontstr) {
159 char **missing, *def;
160 int i, n;
162 missing = NULL;
163 setlocale(LC_ALL, "");
164 if(dc.font.set)
165 XFreeFontSet(dpy, dc.font.set);
166 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
167 if(missing) {
168 while(n--)
169 fprintf(stderr, "missing fontset: %s\n", missing[n]);
170 XFreeStringList(missing);
171 if(dc.font.set) {
172 XFreeFontSet(dpy, dc.font.set);
173 dc.font.set = NULL;
174 }
175 }
176 if(dc.font.set) {
177 XFontSetExtents *font_extents;
178 XFontStruct **xfonts;
179 char **font_names;
181 dc.font.ascent = dc.font.descent = 0;
182 font_extents = XExtentsOfFontSet(dc.font.set);
183 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
184 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
185 if(dc.font.ascent < (*xfonts)->ascent)
186 dc.font.ascent = (*xfonts)->ascent;
187 if(dc.font.descent < (*xfonts)->descent)
188 dc.font.descent = (*xfonts)->descent;
189 xfonts++;
190 }
191 }
192 else {
193 if(dc.font.xfont)
194 XFreeFont(dpy, dc.font.xfont);
195 dc.font.xfont = NULL;
196 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
197 if (!dc.font.xfont)
198 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
199 if (!dc.font.xfont)
200 eprint("error, cannot init 'fixed' font\n");
201 dc.font.ascent = dc.font.xfont->ascent;
202 dc.font.descent = dc.font.xfont->descent;
203 }
204 dc.font.height = dc.font.ascent + dc.font.descent;
205 }
207 unsigned int
208 textw(const char *text) {
209 return textnw(text, strlen(text)) + dc.font.height;
210 }