Xinqi Bao's Git
1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
7 #include <X11/Xlocale.h>
12 textnw(const char *text
, unsigned int len
) {
16 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
19 return XTextWidth(dc
.font
.xfont
, text
, len
);
23 drawtext(const char *text
, unsigned long col
[ColLast
], Bool highlight
) {
26 unsigned int len
, olen
;
28 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
30 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
31 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
35 olen
= len
= strlen(text
);
36 if(len
>= sizeof(buf
))
37 len
= sizeof(buf
) - 1;
38 memcpy(buf
, text
, len
);
40 h
= dc
.font
.ascent
+ dc
.font
.descent
;
41 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
43 /* shorten text if necessary */
44 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
55 return; /* too long */
56 gcv
.foreground
= col
[ColFG
];
58 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
59 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
62 gcv
.font
= dc
.font
.xfont
->fid
;
63 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCFont
, &gcv
);
64 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
69 r
.width
= r
.height
= (h
+ 2) / 4;
70 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
80 for(c
= clients
; c
; c
= getnext(c
->next
))
90 for(i
= 0; i
< ntags
; i
++) {
91 dc
.w
= textw(tags
[i
]);
93 drawtext(tags
[i
], dc
.sel
, sel
&& sel
->tags
[i
]);
95 drawtext(tags
[i
], dc
.norm
, sel
&& sel
->tags
[i
]);
99 drawtext(arrange
== dofloat
? FLOATSYMBOL
: TILESYMBOL
, dc
.status
, False
);
102 dc
.x
= bx
+ bw
- dc
.w
;
107 drawtext(stext
, dc
.status
, False
);
108 if((dc
.w
= dc
.x
- x
) > bh
) {
111 drawtext(sel
->name
, dc
.sel
, False
);
113 drawtext(NULL
, dc
.norm
, False
);
115 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0);
120 drawtitle(Client
*c
) {
121 if(c
== sel
&& issel
) {
123 XUnmapWindow(dpy
, c
->twin
);
124 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBG
]);
127 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBG
]);
128 XMapWindow(dpy
, c
->twin
);
131 drawtext(c
->name
, dc
.norm
, False
);
132 XCopyArea(dpy
, dc
.drawable
, c
->twin
, dc
.gc
, 0, 0, c
->tw
, c
->th
, 0, 0);
137 getcolor(const char *colstr
) {
138 Colormap cmap
= DefaultColormap(dpy
, screen
);
141 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
142 eprint("error, cannot allocate color '%s'\n", colstr
);
147 setfont(const char *fontstr
) {
148 char **missing
, *def
;
152 setlocale(LC_ALL
, "");
154 XFreeFontSet(dpy
, dc
.font
.set
);
155 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
158 fprintf(stderr
, "missing fontset: %s\n", missing
[n
]);
159 XFreeStringList(missing
);
161 XFreeFontSet(dpy
, dc
.font
.set
);
166 XFontSetExtents
*font_extents
;
167 XFontStruct
**xfonts
;
169 dc
.font
.ascent
= dc
.font
.descent
= 0;
170 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
171 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
172 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
173 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
174 dc
.font
.ascent
= (*xfonts
)->ascent
;
175 if(dc
.font
.descent
< (*xfonts
)->descent
)
176 dc
.font
.descent
= (*xfonts
)->descent
;
182 XFreeFont(dpy
, dc
.font
.xfont
);
183 dc
.font
.xfont
= NULL
;
184 dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
);
186 dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed");
188 eprint("error, cannot init 'fixed' font\n");
189 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
190 dc
.font
.descent
= dc
.font
.xfont
->descent
;
192 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
196 textw(const char *text
) {
197 return textnw(text
, strlen(text
)) + dc
.font
.height
;