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