Xinqi Bao's Git

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