aewl
view draw.c @ 584:37281ebc1b5b
added togglefloat to hg tip (i consider this useful for some cases), using MODKEY-Shift-space as shortcut
author | arg@mig29 |
---|---|
date | Mon, 27 Nov 2006 10:57:37 +0100 |
parents | 427708bf5f36 |
children | 7a48d1f06223 |
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 Bool
12 isoccupied(unsigned int t)
13 {
14 Client *c;
15 for(c = clients; c; c = c->next)
16 if(c->tags[t])
17 return True;
18 return False;
19 }
21 static unsigned int
22 textnw(const char *text, unsigned int len) {
23 XRectangle r;
25 if(dc.font.set) {
26 XmbTextExtents(dc.font.set, text, len, NULL, &r);
27 return r.width;
28 }
29 return XTextWidth(dc.font.xfont, text, len);
30 }
32 static void
33 drawtext(const char *text, unsigned long col[ColLast], Bool ldot, Bool rdot) {
34 int x, y, w, h;
35 static char buf[256];
36 unsigned int len, olen;
37 XGCValues gcv;
38 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
40 XSetForeground(dpy, dc.gc, col[ColBG]);
41 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
42 if(!text)
43 return;
44 w = 0;
45 olen = len = strlen(text);
46 if(len >= sizeof buf)
47 len = sizeof buf - 1;
48 memcpy(buf, text, len);
49 buf[len] = 0;
50 h = dc.font.ascent + dc.font.descent;
51 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
52 x = dc.x + (h / 2);
53 /* shorten text if necessary */
54 while(len && (w = textnw(buf, len)) > dc.w - h)
55 buf[--len] = 0;
56 if(len < olen) {
57 if(len > 1)
58 buf[len - 1] = '.';
59 if(len > 2)
60 buf[len - 2] = '.';
61 if(len > 3)
62 buf[len - 3] = '.';
63 }
64 if(w > dc.w)
65 return; /* too long */
66 gcv.foreground = col[ColFG];
67 if(dc.font.set) {
68 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
69 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
70 }
71 else {
72 gcv.font = dc.font.xfont->fid;
73 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
74 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
75 }
76 if(ldot) {
77 r.x = dc.x + 2;
78 r.y = dc.y + 2;
79 r.width = r.height = (h + 2) / 4;
80 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
81 }
82 if(rdot) {
83 r.width = r.height = (h + 2) / 4;
84 r.x = dc.x + dc.w - r.width - 2;
85 r.y = dc.y + dc.h - r.height - 2;
86 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
87 }
88 }
90 /* extern */
92 void
93 drawall(void) {
94 Client *c;
96 for(c = clients; c; c = getnext(c->next))
97 drawtitle(c);
98 drawstatus();
99 }
101 void
102 drawstatus(void) {
103 int i, x;
105 dc.x = dc.y = 0;
106 for(i = 0; i < ntags; i++) {
107 dc.w = textw(tags[i]);
108 if(seltag[i])
109 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
110 else
111 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
112 dc.x += dc.w;
113 }
114 dc.w = bmw;
115 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
116 x = dc.x + dc.w;
117 dc.w = textw(stext);
118 dc.x = bw - dc.w;
119 if(dc.x < x) {
120 dc.x = x;
121 dc.w = bw - x;
122 }
123 drawtext(stext, dc.status, False, False);
124 if((dc.w = dc.x - x) > bh) {
125 dc.x = x;
126 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
127 }
128 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
129 XSync(dpy, False);
130 }
132 void
133 drawtitle(Client *c) {
134 if(c == sel && issel) {
135 drawstatus();
136 XUnmapWindow(dpy, c->twin);
137 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
138 return;
139 }
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,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;
182 dc.font.ascent = dc.font.descent = 0;
183 font_extents = XExtentsOfFontSet(dc.font.set);
184 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
185 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
186 if(dc.font.ascent < (*xfonts)->ascent)
187 dc.font.ascent = (*xfonts)->ascent;
188 if(dc.font.descent < (*xfonts)->descent)
189 dc.font.descent = (*xfonts)->descent;
190 xfonts++;
191 }
192 }
193 else {
194 if(dc.font.xfont)
195 XFreeFont(dpy, dc.font.xfont);
196 dc.font.xfont = NULL;
197 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
198 if (!dc.font.xfont)
199 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
200 if (!dc.font.xfont)
201 eprint("error, cannot init 'fixed' font\n");
202 dc.font.ascent = dc.font.xfont->ascent;
203 dc.font.descent = dc.font.xfont->descent;
204 }
205 dc.font.height = dc.font.ascent + dc.font.descent;
206 }
208 unsigned int
209 textw(const char *text) {
210 return textnw(text, strlen(text)) + dc.font.height;
211 }