dwm-meillo

view draw.c @ 571:f0688a69f17f

returning to old bar colorization behavior, like sander proposed for consistency reasons
author arg@mig29
date Tue, 21 Nov 2006 15:03:08 +0100
parents f05cfa7d5128
children 427708bf5f36
line source
1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdio.h>
6 #include <string.h>
7 #include <X11/Xlocale.h>
9 /* static */
11 static unsigned int
12 textnw(const char *text, unsigned int len) {
13 XRectangle r;
15 if(dc.font.set) {
16 XmbTextExtents(dc.font.set, text, len, NULL, &r);
17 return r.width;
18 }
19 return XTextWidth(dc.font.xfont, text, len);
20 }
22 static void
23 drawtext(const char *text, unsigned long col[ColLast], Bool highlight) {
24 int x, y, w, h;
25 static char buf[256];
26 unsigned int len, olen;
27 XGCValues gcv;
28 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
30 XSetForeground(dpy, dc.gc, col[ColBG]);
31 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
32 if(!text)
33 return;
34 w = 0;
35 olen = len = strlen(text);
36 if(len >= sizeof(buf))
37 len = sizeof(buf) - 1;
38 memcpy(buf, text, len);
39 buf[len] = 0;
40 h = dc.font.ascent + dc.font.descent;
41 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
42 x = dc.x + (h / 2);
43 /* shorten text if necessary */
44 while(len && (w = textnw(buf, len)) > dc.w - h)
45 buf[--len] = 0;
46 if(len < olen) {
47 if(len > 1)
48 buf[len - 1] = '.';
49 if(len > 2)
50 buf[len - 2] = '.';
51 if(len > 3)
52 buf[len - 3] = '.';
53 }
54 if(w > dc.w)
55 return; /* too long */
56 gcv.foreground = col[ColFG];
57 if(dc.font.set) {
58 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
59 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
60 }
61 else {
62 gcv.font = dc.font.xfont->fid;
63 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
64 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
65 }
66 if(highlight) {
67 r.x = dc.x + 2;
68 r.y = dc.y + 2;
69 r.width = r.height = (h + 2) / 4;
70 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
71 }
72 }
74 /* extern */
76 void
77 drawall(void) {
78 Client *c;
80 for(c = clients; c; c = getnext(c->next))
81 drawtitle(c);
82 drawstatus();
83 }
85 void
86 drawstatus(void) {
87 int i, x;
89 dc.x = dc.y = 0;
90 for(i = 0; i < ntags; i++) {
91 dc.w = textw(tags[i]);
92 if(seltag[i])
93 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
94 else
95 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
96 dc.x += dc.w;
97 }
98 dc.w = bmw;
99 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False);
100 x = dc.x + dc.w;
101 dc.w = textw(stext);
102 dc.x = bw - dc.w;
103 if(dc.x < x) {
104 dc.x = x;
105 dc.w = bw - x;
106 }
107 drawtext(stext, dc.status, False);
108 if((dc.w = dc.x - x) > bh) {
109 dc.x = x;
110 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False);
111 }
112 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
113 XSync(dpy, False);
114 }
116 void
117 drawtitle(Client *c) {
118 if(c == sel && issel) {
119 drawstatus();
120 XUnmapWindow(dpy, c->twin);
121 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
122 return;
123 }
124 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
125 XMapWindow(dpy, c->twin);
126 dc.x = dc.y = 0;
127 dc.w = c->tw;
128 drawtext(c->name, dc.norm, False);
129 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
130 XSync(dpy, False);
131 }
133 unsigned long
134 getcolor(const char *colstr) {
135 Colormap cmap = DefaultColormap(dpy, screen);
136 XColor color;
138 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
139 eprint("error, cannot allocate color '%s'\n", colstr);
140 return color.pixel;
141 }
143 void
144 setfont(const char *fontstr) {
145 char **missing, *def;
146 int i, n;
148 missing = NULL;
149 setlocale(LC_ALL, "");
150 if(dc.font.set)
151 XFreeFontSet(dpy, dc.font.set);
152 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
153 if(missing) {
154 while(n--)
155 fprintf(stderr, "missing fontset: %s\n", missing[n]);
156 XFreeStringList(missing);
157 if(dc.font.set) {
158 XFreeFontSet(dpy, dc.font.set);
159 dc.font.set = NULL;
160 }
161 }
162 if(dc.font.set) {
163 XFontSetExtents *font_extents;
164 XFontStruct **xfonts;
165 char **font_names;
166 dc.font.ascent = dc.font.descent = 0;
167 font_extents = XExtentsOfFontSet(dc.font.set);
168 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
169 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
170 if(dc.font.ascent < (*xfonts)->ascent)
171 dc.font.ascent = (*xfonts)->ascent;
172 if(dc.font.descent < (*xfonts)->descent)
173 dc.font.descent = (*xfonts)->descent;
174 xfonts++;
175 }
176 }
177 else {
178 if(dc.font.xfont)
179 XFreeFont(dpy, dc.font.xfont);
180 dc.font.xfont = NULL;
181 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
182 if (!dc.font.xfont)
183 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
184 if (!dc.font.xfont)
185 eprint("error, cannot init 'fixed' font\n");
186 dc.font.ascent = dc.font.xfont->ascent;
187 dc.font.descent = dc.font.xfont->descent;
188 }
189 dc.font.height = dc.font.ascent + dc.font.descent;
190 }
192 unsigned int
193 textw(const char *text) {
194 return textnw(text, strlen(text)) + dc.font.height;
195 }