Mercurial > hg > index.fcgi > dwm > dwm-3.6.1-11pba
view draw.c @ 5:e060ab82b136
gapless grid
author | paulo@thepaulopc |
---|---|
date | Wed, 27 Jan 2010 23:28:29 -0800 |
parents | 7024076fa948 |
children | 0968b3739b8d |
line source
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>
7 /* static */
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 };
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 }
30 static Bool
31 isoccupied(unsigned int t) {
32 Client *c;
34 for(c = clients; c; c = c->next)
35 if(c->tags[t])
36 return True;
37 return False;
38 }
40 static unsigned int
41 textnw(const char *text, unsigned int len) {
42 XRectangle r;
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 }
51 /* extern */
53 void
54 drawstatus(void) {
55 Client *c;
56 int i, j, x;
58 dc.x = dc.y = 0;
59 for(i = 0; i < ntags; i++) {
60 dc.w = textw(tags[i]);
61 if(seltag[i]) {
62 drawtext(tags[i], dc.sel);
63 drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
64 }
65 else {
66 drawtext(tags[i], dc.norm);
67 drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
68 }
69 dc.x += dc.w;
70 }
71 dc.w = blw;
72 drawtext(lt->symbol, dc.norm);
73 x = dc.x + dc.w;
74 dc.w = textw(stext);
75 dc.x = sw - dc.w;
76 if(dc.x < x) {
77 dc.x = x;
78 dc.w = sw - x;
79 }
80 drawtext(stext, dc.norm);
81 if((dc.w = dc.x - x) > bh) {
82 dc.x = x;
83 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm);
84 }
85 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
87 if (TASKBAR) {
88 dc.x = dc.y = 0;
89 dc.w = sw;
90 for(j=0, c = clients; c; c = c->next) {
91 if(isvisible(c))
92 j++;
93 }
94 if(j && j < MAX_TASKS)
95 dc.w /= j;
96 else
97 drawtext("", dc.norm);
98 for(c = clients; j && c && dc.x < sw; c = c->next) {
99 if(isvisible(c)) {
100 drawtext(c->name, (c == sel) ? dc.sel : dc.norm);
101 dc.x += dc.w;
102 j--;
103 }
104 }
105 XCopyArea(dpy, dc.drawable, tbarwin, dc.gc, 0, 0, sw, bh, 0, 0);
106 }
108 XSync(dpy, False);
109 }
111 void
112 drawtext(const char *text, unsigned long col[ColLast]) {
113 int x, y, w, h;
114 static char buf[256];
115 unsigned int len, olen;
116 XGCValues gcv;
117 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
119 XSetForeground(dpy, dc.gc, col[ColBG]);
120 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
121 if(!text)
122 return;
123 w = 0;
124 olen = len = strlen(text);
125 if(len >= sizeof buf)
126 len = sizeof buf - 1;
127 memcpy(buf, text, len);
128 buf[len] = 0;
129 h = dc.font.ascent + dc.font.descent;
130 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
131 x = dc.x + (h / 2);
132 /* shorten text if necessary */
133 while(len && (w = textnw(buf, len)) > dc.w - h)
134 buf[--len] = 0;
135 if(len < olen) {
136 if(len > 1)
137 buf[len - 1] = '.';
138 if(len > 2)
139 buf[len - 2] = '.';
140 if(len > 3)
141 buf[len - 3] = '.';
142 }
143 if(w > dc.w)
144 return; /* too long */
145 gcv.foreground = col[ColFG];
146 if(dc.font.set) {
147 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
148 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
149 }
150 else {
151 gcv.font = dc.font.xfont->fid;
152 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
153 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
154 }
155 }
157 unsigned int
158 textw(const char *text) {
159 return textnw(text, strlen(text)) + dc.font.height;
160 }