JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removing the -1 on final indicators, looks cleaner
[dwm.git] / draw.c
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>
8
9 /* static */
10
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 }
20
21 static unsigned int
22 textnw(const char *text, unsigned int len) {
23         XRectangle r;
24
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 }
31
32 static void
33 drawtext(const char *text, unsigned long col[ColLast], Bool dot, Bool border) {
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 };
39         XPoint pt[3];
40
41         XSetForeground(dpy, dc.gc, col[ColBG]);
42         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
43         if(!text)
44                 return;
45         w = 0;
46         olen = len = strlen(text);
47         if(len >= sizeof buf)
48                 len = sizeof buf - 1;
49         memcpy(buf, text, len);
50         buf[len] = 0;
51         h = dc.font.ascent + dc.font.descent;
52         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
53         x = dc.x + (h / 2);
54         /* shorten text if necessary */
55         while(len && (w = textnw(buf, len)) > dc.w - h)
56                 buf[--len] = 0;
57         if(len < olen) {
58                 if(len > 1)
59                         buf[len - 1] = '.';
60                 if(len > 2)
61                         buf[len - 2] = '.';
62                 if(len > 3)
63                         buf[len - 3] = '.';
64         }
65         if(w > dc.w)
66                 return; /* too long */
67         gcv.foreground = col[ColFG];
68         if(dc.font.set) {
69                 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
70                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
71         }
72         else {
73                 gcv.font = dc.font.xfont->fid;
74                 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
75                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
76         }
77         if(dot) {
78                 r.x = dc.x + 2;
79                 r.y = dc.y + 2;
80                 r.width = r.height = (h + 2) / 4;
81                 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
82         }
83         if(border) {
84                 pt[0].x = dc.x;
85                 pt[0].y = dc.y + dc.h;
86                 pt[1].x = 0;
87                 pt[1].y = -dc.h;
88                 pt[2].x = dc.w;
89                 pt[2].y = 0;
90                 XDrawLines(dpy, dc.drawable, dc.gc, pt, 3, CoordModePrevious);
91         }
92 }
93
94 /* extern */
95
96 void
97 drawall(void) {
98         Client *c;
99
100         for(c = clients; c; c = getnext(c->next))
101                 drawtitle(c);
102         drawstatus();
103 }
104
105 void
106 drawstatus(void) {
107         int i, x;
108
109         dc.x = dc.y = 0;
110         for(i = 0; i < ntags; i++) {
111                 dc.w = textw(tags[i]);
112                 if(seltag[i])
113                         drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
114                 else
115                         drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
116                 dc.x += dc.w;
117         }
118         dc.w = bmw;
119         drawtext(arrange == dofloat ?  FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
120         x = dc.x + dc.w;
121         dc.w = textw(stext);
122         dc.x = bw - dc.w;
123         if(dc.x < x) {
124                 dc.x = x;
125                 dc.w = bw - x;
126         }
127         drawtext(stext, dc.status, False, False);
128         if((dc.w = dc.x - x) > bh) {
129                 dc.x = x;
130                 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
131         }
132         XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
133         XSync(dpy, False);
134 }
135
136 void
137 drawtitle(Client *c) {
138         if(c == sel && issel) {
139                 drawstatus();
140                 XUnmapWindow(dpy, c->twin);
141                 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
142                 return;
143         }
144         XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
145         XMapWindow(dpy, c->twin);
146         dc.x = dc.y = 0;
147         dc.w = c->tw;
148         drawtext(c->name, dc.norm, False,False);
149         XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
150         XSync(dpy, False);
151 }
152
153 unsigned long
154 getcolor(const char *colstr) {
155         Colormap cmap = DefaultColormap(dpy, screen);
156         XColor color;
157
158         if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
159                 eprint("error, cannot allocate color '%s'\n", colstr);
160         return color.pixel;
161 }
162
163 void
164 setfont(const char *fontstr) {
165         char **missing, *def;
166         int i, n;
167
168         missing = NULL;
169         setlocale(LC_ALL, "");
170         if(dc.font.set)
171                 XFreeFontSet(dpy, dc.font.set);
172         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
173         if(missing) {
174                 while(n--)
175                         fprintf(stderr, "missing fontset: %s\n", missing[n]);
176                 XFreeStringList(missing);
177                 if(dc.font.set) {
178                         XFreeFontSet(dpy, dc.font.set);
179                         dc.font.set = NULL;
180                 }
181         }
182         if(dc.font.set) {
183                 XFontSetExtents *font_extents;
184                 XFontStruct **xfonts;
185                 char **font_names;
186                 dc.font.ascent = dc.font.descent = 0;
187                 font_extents = XExtentsOfFontSet(dc.font.set);
188                 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
189                 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
190                         if(dc.font.ascent < (*xfonts)->ascent)
191                                 dc.font.ascent = (*xfonts)->ascent;
192                         if(dc.font.descent < (*xfonts)->descent)
193                                 dc.font.descent = (*xfonts)->descent;
194                         xfonts++;
195                 }
196         }
197         else {
198                 if(dc.font.xfont)
199                         XFreeFont(dpy, dc.font.xfont);
200                 dc.font.xfont = NULL;
201                 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
202                 if (!dc.font.xfont)
203                         dc.font.xfont = XLoadQueryFont(dpy, "fixed");
204                 if (!dc.font.xfont)
205                         eprint("error, cannot init 'fixed' font\n");
206                 dc.font.ascent = dc.font.xfont->ascent;
207                 dc.font.descent = dc.font.xfont->descent;
208         }
209         dc.font.height = dc.font.ascent + dc.font.descent;
210 }
211
212 unsigned int
213 textw(const char *text) {
214         return textnw(text, strlen(text)) + dc.font.height;
215 }