dwm-meillo
view draw.c @ 516:e5f8d6ac509c
added symbols for different modes
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Fri, 29 Sep 2006 18:08:20 +0200 |
parents | 3a812750f983 |
children | 6cad48068e4c |
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 ?
108 FLOATSYMBOL : stackpos == StackBottom ?
109 BSTACKSYMBOL : VSTACKSYMBOL, dc.status, False);
111 x = dc.x + dc.w;
112 dc.w = textw(stext);
113 dc.x = bx + bw - dc.w;
114 if(dc.x < x) {
115 dc.x = x;
116 dc.w = bw - x;
117 }
118 drawtext(stext, dc.status, False);
120 if((dc.w = dc.x - x) > bh) {
121 dc.x = x;
122 if(sel)
123 drawtext(sel->name, dc.sel, False);
124 else
125 drawtext(NULL, dc.norm, False);
126 }
127 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
128 XSync(dpy, False);
129 }
131 void
132 drawtitle(Client *c) {
133 if(c == sel && issel) {
134 drawstatus();
135 XUnmapWindow(dpy, c->twin);
136 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
137 return;
138 }
140 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
141 XMapWindow(dpy, c->twin);
142 dc.x = dc.y = 0;
143 dc.w = c->tw;
144 drawtext(c->name, dc.norm, False);
145 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
146 XSync(dpy, False);
147 }
149 unsigned long
150 getcolor(const char *colstr) {
151 Colormap cmap = DefaultColormap(dpy, screen);
152 XColor color;
154 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
155 eprint("error, cannot allocate color '%s'\n", colstr);
156 return color.pixel;
157 }
159 void
160 setfont(const char *fontstr) {
161 char **missing, *def;
162 int i, n;
164 missing = NULL;
165 setlocale(LC_ALL, "");
166 if(dc.font.set)
167 XFreeFontSet(dpy, dc.font.set);
168 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
169 if(missing) {
170 while(n--)
171 fprintf(stderr, "missing fontset: %s\n", missing[n]);
172 XFreeStringList(missing);
173 if(dc.font.set) {
174 XFreeFontSet(dpy, dc.font.set);
175 dc.font.set = NULL;
176 }
177 }
178 if(dc.font.set) {
179 XFontSetExtents *font_extents;
180 XFontStruct **xfonts;
181 char **font_names;
183 dc.font.ascent = dc.font.descent = 0;
184 font_extents = XExtentsOfFontSet(dc.font.set);
185 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
186 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
187 if(dc.font.ascent < (*xfonts)->ascent)
188 dc.font.ascent = (*xfonts)->ascent;
189 if(dc.font.descent < (*xfonts)->descent)
190 dc.font.descent = (*xfonts)->descent;
191 xfonts++;
192 }
193 }
194 else {
195 if(dc.font.xfont)
196 XFreeFont(dpy, dc.font.xfont);
197 dc.font.xfont = NULL;
198 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
199 if (!dc.font.xfont)
200 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
201 if (!dc.font.xfont)
202 eprint("error, cannot init 'fixed' font\n");
203 dc.font.ascent = dc.font.xfont->ascent;
204 dc.font.descent = dc.font.xfont->descent;
205 }
206 dc.font.height = dc.font.ascent + dc.font.descent;
207 }
209 unsigned int
210 textw(const char *text) {
211 return textnw(text, strlen(text)) + dc.font.height;
212 }