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