Mercurial > hg > index.fcgi > dwm > dwm-3.6.1-11pba
view draw.c @ 6:0968b3739b8d
apply keymodes patch
author | paulo@thepaulopc |
---|---|
date | Sun, 12 Jun 2011 14:32:54 -0700 |
parents | faa4cb9d7bd6 |
children | bc03b37b37ba |
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;
75 char *_stext = stext;
76 if (getkeymode() == COMMANDMODE) {
77 strcpy(cmtext, "-- CMD -- ");
78 strncat(cmtext, stext, sizeof cmtext - 1 - strlen(cmtext));
79 _stext = cmtext;
80 }
81 dc.w = textw(_stext);
82 dc.x = sw - dc.w;
83 if(dc.x < x) {
84 dc.x = x;
85 dc.w = sw - x;
86 }
87 drawtext(_stext, dc.norm);
89 if((dc.w = dc.x - x) > bh) {
90 dc.x = x;
91 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm);
92 }
93 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
95 if (TASKBAR) {
96 dc.x = dc.y = 0;
97 dc.w = sw;
98 for(j=0, c = clients; c; c = c->next) {
99 if(isvisible(c))
100 j++;
101 }
102 if(j && j < MAX_TASKS)
103 dc.w /= j;
104 else
105 drawtext("", dc.norm);
106 for(c = clients; j && c && dc.x < sw; c = c->next) {
107 if(isvisible(c)) {
108 drawtext(c->name, (c == sel) ? dc.sel : dc.norm);
109 dc.x += dc.w;
110 j--;
111 }
112 }
113 XCopyArea(dpy, dc.drawable, tbarwin, dc.gc, 0, 0, sw, bh, 0, 0);
114 }
116 XSync(dpy, False);
117 }
119 void
120 drawtext(const char *text, unsigned long col[ColLast]) {
121 int x, y, w, h;
122 static char buf[256];
123 unsigned int len, olen;
124 XGCValues gcv;
125 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
127 XSetForeground(dpy, dc.gc, col[ColBG]);
128 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
129 if(!text)
130 return;
131 w = 0;
132 olen = len = strlen(text);
133 if(len >= sizeof buf)
134 len = sizeof buf - 1;
135 memcpy(buf, text, len);
136 buf[len] = 0;
137 h = dc.font.ascent + dc.font.descent;
138 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
139 x = dc.x + (h / 2);
140 /* shorten text if necessary */
141 while(len && (w = textnw(buf, len)) > dc.w - h)
142 buf[--len] = 0;
143 if(len < olen) {
144 if(len > 1)
145 buf[len - 1] = '.';
146 if(len > 2)
147 buf[len - 2] = '.';
148 if(len > 3)
149 buf[len - 3] = '.';
150 }
151 if(w > dc.w)
152 return; /* too long */
153 gcv.foreground = col[ColFG];
154 if(dc.font.set) {
155 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
156 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
157 }
158 else {
159 gcv.font = dc.font.xfont->fid;
160 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
161 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
162 }
163 }
165 unsigned int
166 textw(const char *text) {
167 return textnw(text, strlen(text)) + dc.font.height;
168 }