JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
renamed untiled into floating, keeping tiled instead of tiling (afaik tiled sounds...
[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         XGCValues gcv;
99         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
100
101         XSetForeground(dpy, dc.gc, col[ColBG]);
102         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
103         if(!text)
104                 return;
105         w = 0;
106         olen = len = strlen(text);
107         if(len >= sizeof buf)
108                 len = sizeof buf - 1;
109         memcpy(buf, text, len);
110         buf[len] = 0;
111         h = dc.font.ascent + dc.font.descent;
112         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
113         x = dc.x + (h / 2);
114         /* shorten text if necessary */
115         while(len && (w = textnw(buf, len)) > dc.w - h)
116                 buf[--len] = 0;
117         if(len < olen) {
118                 if(len > 1)
119                         buf[len - 1] = '.';
120                 if(len > 2)
121                         buf[len - 2] = '.';
122                 if(len > 3)
123                         buf[len - 3] = '.';
124         }
125         if(w > dc.w)
126                 return; /* too long */
127         gcv.foreground = col[ColFG];
128         if(dc.font.set) {
129                 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
130                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
131         }
132         else {
133                 gcv.font = dc.font.xfont->fid;
134                 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
135                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
136         }
137 }
138
139 unsigned int
140 textw(const char *text) {
141         return textnw(text, strlen(text)) + dc.font.height;
142 }