JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added nsz to copyright holders as well, because he did a lot recently
[dwm.git] / draw.c
1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2  * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3  * © 2007 Premysl Hruby <dfenze at gmail dot com>
4  * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
5  * See LICENSE file for license details. */
6 #include "dwm.h"
7 #include <string.h>
8
9 /* static */
10
11 static void
12 drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
13         int x;
14         XGCValues gcv;
15         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
16
17         gcv.foreground = col[ColFG];
18         XChangeGC(dpy, dc.gc, GCForeground, &gcv);
19         x = (dc.font.ascent + dc.font.descent + 2) / 4;
20         r.x = dc.x + 1;
21         r.y = dc.y + 1;
22         if(filled) {
23                 r.width = r.height = x + 1;
24                 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
25         }
26         else if(empty) {
27                 r.width = r.height = x;
28                 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
29         }
30 }
31
32 static Bool
33 isoccupied(unsigned int t) {
34         Client *c;
35
36         for(c = clients; c; c = c->next)
37                 if(c->tags[t])
38                         return True;
39         return False;
40 }
41
42 static unsigned int
43 textnw(const char *text, unsigned int len) {
44         XRectangle r;
45
46         if(dc.font.set) {
47                 XmbTextExtents(dc.font.set, text, len, NULL, &r);
48                 return r.width;
49         }
50         return XTextWidth(dc.font.xfont, text, len);
51 }
52
53 /* extern */
54
55 void
56 drawstatus(void) {
57         int i, x;
58
59         dc.x = dc.y = 0;
60         for(i = 0; i < ntags; i++) {
61                 dc.w = textw(tags[i]);
62                 if(seltag[i]) {
63                         drawtext(tags[i], dc.sel);
64                         drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
65                 }
66                 else {
67                         drawtext(tags[i], dc.norm);
68                         drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
69                 }
70                 dc.x += dc.w;
71         }
72         dc.w = blw;
73         drawtext(lt->symbol, dc.norm);
74         x = dc.x + dc.w;
75         dc.w = textw(stext);
76         dc.x = sw - dc.w;
77         if(dc.x < x) {
78                 dc.x = x;
79                 dc.w = sw - x;
80         }
81         drawtext(stext, dc.norm);
82         if((dc.w = dc.x - x) > bh) {
83                 dc.x = x;
84                 if(sel) {
85                         drawtext(sel->name, dc.sel);
86                         drawsquare(sel->ismax, sel->isfloating, dc.sel);
87                 }
88                 else
89                         drawtext(NULL, dc.norm);
90         }
91         XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
92         XSync(dpy, False);
93 }
94
95 void
96 drawtext(const char *text, unsigned long col[ColLast]) {
97         int x, y, w, h;
98         static char buf[256];
99         unsigned int len, olen;
100         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
101
102         XSetForeground(dpy, dc.gc, col[ColBG]);
103         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
104         if(!text)
105                 return;
106         w = 0;
107         olen = len = strlen(text);
108         if(len >= sizeof buf)
109                 len = sizeof buf - 1;
110         memcpy(buf, text, len);
111         buf[len] = 0;
112         h = dc.font.ascent + dc.font.descent;
113         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
114         x = dc.x + (h / 2);
115         /* shorten text if necessary */
116         while(len && (w = textnw(buf, len)) > dc.w - h)
117                 buf[--len] = 0;
118         if(len < olen) {
119                 if(len > 1)
120                         buf[len - 1] = '.';
121                 if(len > 2)
122                         buf[len - 2] = '.';
123                 if(len > 3)
124                         buf[len - 3] = '.';
125         }
126         if(w > dc.w)
127                 return; /* too long */
128         XSetForeground(dpy, dc.gc, col[ColFG]);
129         if(dc.font.set)
130                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
131         else
132                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
133 }
134
135 unsigned int
136 textw(const char *text) {
137         return textnw(text, strlen(text)) + dc.font.height;
138 }