Mercurial > hg > index.fcgi > dwm > dwm-3.6.1-10pba
view draw.c @ 0:7024076fa948
initial add
author | paulo@localhost |
---|---|
date | Sun, 22 Mar 2009 23:26:35 -0700 |
parents | |
children | faa4cb9d7bd6 |
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 for(j=0, c = clients; c; c = c->next) {
84 if(isvisible(c))
85 j++;
86 }
87 if(j && j < MAX_TASKS)
88 dc.w /= j;
89 else {
90 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm);
91 j = 0;
92 }
93 for(c = clients; j && c; c = c->next) {
94 if(isvisible(c)) {
95 drawtext(c->name, (c == sel) ? dc.sel : dc.norm);
96 dc.x += dc.w;
97 j--;
98 }
99 }
100 }
101 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
102 XSync(dpy, False);
103 }
105 void
106 drawtext(const char *text, unsigned long col[ColLast]) {
107 int x, y, w, h;
108 static char buf[256];
109 unsigned int len, olen;
110 XGCValues gcv;
111 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
113 XSetForeground(dpy, dc.gc, col[ColBG]);
114 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
115 if(!text)
116 return;
117 w = 0;
118 olen = len = strlen(text);
119 if(len >= sizeof buf)
120 len = sizeof buf - 1;
121 memcpy(buf, text, len);
122 buf[len] = 0;
123 h = dc.font.ascent + dc.font.descent;
124 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
125 x = dc.x + (h / 2);
126 /* shorten text if necessary */
127 while(len && (w = textnw(buf, len)) > dc.w - h)
128 buf[--len] = 0;
129 if(len < olen) {
130 if(len > 1)
131 buf[len - 1] = '.';
132 if(len > 2)
133 buf[len - 2] = '.';
134 if(len > 3)
135 buf[len - 3] = '.';
136 }
137 if(w > dc.w)
138 return; /* too long */
139 gcv.foreground = col[ColFG];
140 if(dc.font.set) {
141 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
142 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
143 }
144 else {
145 gcv.font = dc.font.xfont->fid;
146 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
147 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
148 }
149 }
151 unsigned int
152 textw(const char *text) {
153 return textnw(text, strlen(text)) + dc.font.height;
154 }