dwm-meillo

view draw.c @ 356:c2a65784cb71

changed symbols for float/tiled mode, added mouse-driven mode toggle to buttonpress()
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 25 Aug 2006 15:06:38 +0200
parents f9543a4af9e5
children 27082985e51e
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, unsigned long col[ColLast], Bool highlight)
26 {
27 int x, y, w, h;
28 static char buf[256];
29 unsigned int len, olen;
30 XGCValues gcv;
31 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
33 XSetForeground(dpy, dc.gc, col[ColBG]);
34 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
36 if(!text)
37 return;
39 w = 0;
40 olen = 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;
53 if(len < olen) {
54 if(len > 1)
55 buf[len - 1] = '.';
56 if(len > 2)
57 buf[len - 2] = '.';
58 if(len > 3)
59 buf[len - 3] = '.';
60 }
62 if(w > dc.w)
63 return; /* too long */
64 gcv.foreground = col[ColFG];
65 if(dc.font.set) {
66 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
67 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
68 }
69 else {
70 gcv.font = dc.font.xfont->fid;
71 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
72 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
73 }
74 if(highlight) {
75 r.x = dc.x + 2;
76 r.y = dc.y + 2;
77 r.width = r.height = 3;
78 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
79 }
80 }
82 /* extern */
84 void
85 drawall()
86 {
87 Client *c;
89 for(c = clients; c; c = getnext(c->next))
90 drawtitle(c);
91 drawstatus();
92 }
94 void
95 drawstatus()
96 {
97 static const char *mode[] = { "><", "|=" };
98 int i, x;
100 dc.x = dc.y = 0;
101 dc.w = bw;
103 if(!modew)
104 modew = textw(mode[0]) > textw(mode[1]) ? textw(mode[0]) : textw(mode[1]);
105 drawtext(mode[arrange == dotile ? 1 : 0], dc.status, False);
107 dc.w = 0;
108 dc.x = modew;
109 for(i = 0; i < ntags; i++) {
110 dc.x += dc.w;
111 dc.w = textw(tags[i]);
112 if(seltag[i])
113 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
114 else
115 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
116 }
117 x = dc.x + dc.w + 1;
118 dc.w = textw(stext);
119 dc.x = bx + bw - dc.w;
120 if(dc.x < x) {
121 dc.x = x;
122 dc.w = bw - x;
123 }
124 drawtext(stext, dc.status, False);
126 if(sel && ((dc.w = dc.x - x) > bh)) {
127 dc.x = x;
128 drawtext(sel->name, dc.sel, False);
129 }
130 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
131 XSync(dpy, False);
132 }
134 void
135 drawtitle(Client *c)
136 {
137 int i;
139 if(c == sel && issel) {
140 drawstatus();
141 XUnmapWindow(dpy, c->twin);
142 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
143 return;
144 }
146 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
147 XMapWindow(dpy, c->twin);
148 dc.x = dc.y = 0;
149 dc.w = c->tw;
150 drawtext(c->name, dc.norm, False);
151 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
152 XSync(dpy, False);
153 }
155 unsigned long
156 getcolor(const char *colstr)
157 {
158 Colormap cmap = DefaultColormap(dpy, screen);
159 XColor color;
161 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
162 return color.pixel;
163 }
165 void
166 setfont(const char *fontstr)
167 {
168 char **missing, *def;
169 int i, n;
171 missing = NULL;
172 setlocale(LC_ALL, "");
173 if(dc.font.set)
174 XFreeFontSet(dpy, dc.font.set);
175 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
176 if(missing) {
177 while(n--)
178 fprintf(stderr, "missing fontset: %s\n", missing[n]);
179 XFreeStringList(missing);
180 if(dc.font.set) {
181 XFreeFontSet(dpy, dc.font.set);
182 dc.font.set = NULL;
183 }
184 }
185 if(dc.font.set) {
186 XFontSetExtents *font_extents;
187 XFontStruct **xfonts;
188 char **font_names;
190 dc.font.ascent = dc.font.descent = 0;
191 font_extents = XExtentsOfFontSet(dc.font.set);
192 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
193 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
194 if(dc.font.ascent < (*xfonts)->ascent)
195 dc.font.ascent = (*xfonts)->ascent;
196 if(dc.font.descent < (*xfonts)->descent)
197 dc.font.descent = (*xfonts)->descent;
198 xfonts++;
199 }
200 }
201 else {
202 if(dc.font.xfont)
203 XFreeFont(dpy, dc.font.xfont);
204 dc.font.xfont = NULL;
205 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
206 if (!dc.font.xfont)
207 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
208 if (!dc.font.xfont)
209 eprint("error, cannot init 'fixed' font\n");
210 dc.font.ascent = dc.font.xfont->ascent;
211 dc.font.descent = dc.font.xfont->descent;
212 }
213 dc.font.height = dc.font.ascent + dc.font.descent;
214 }
216 unsigned int
217 textw(const char *text)
218 {
219 return textnw(text, strlen(text)) + dc.font.height;
220 }