view draw.c @ 10:ed4f1bf80e57

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