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
);
107 drawtext(stext
, dc
.status
, False
);
108 if((dc
.w
= dc
.x
- x
) > bh
) {
110 drawtext(sel
? sel
->name
: NULL
, dc
.sel
, False
);
112 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0);
117 drawtitle(Client
*c
) {
118 if(c
== sel
&& issel
) {
120 XUnmapWindow(dpy
, c
->twin
);
121 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBG
]);
124 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBG
]);
125 XMapWindow(dpy
, c
->twin
);
128 drawtext(c
->name
, dc
.norm
, False
);
129 XCopyArea(dpy
, dc
.drawable
, c
->twin
, dc
.gc
, 0, 0, c
->tw
, c
->th
, 0, 0);
134 getcolor(const char *colstr
) {
135 Colormap cmap
= DefaultColormap(dpy
, screen
);
138 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
139 eprint("error, cannot allocate color '%s'\n", colstr
);
144 setfont(const char *fontstr
) {
145 char **missing
, *def
;
149 setlocale(LC_ALL
, "");
151 XFreeFontSet(dpy
, dc
.font
.set
);
152 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
155 fprintf(stderr
, "missing fontset: %s\n", missing
[n
]);
156 XFreeStringList(missing
);
158 XFreeFontSet(dpy
, dc
.font
.set
);
163 XFontSetExtents
*font_extents
;
164 XFontStruct
**xfonts
;
166 dc
.font
.ascent
= dc
.font
.descent
= 0;
167 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
168 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
169 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
170 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
171 dc
.font
.ascent
= (*xfonts
)->ascent
;
172 if(dc
.font
.descent
< (*xfonts
)->descent
)
173 dc
.font
.descent
= (*xfonts
)->descent
;
179 XFreeFont(dpy
, dc
.font
.xfont
);
180 dc
.font
.xfont
= NULL
;
181 dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
);
183 dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed");
185 eprint("error, cannot init 'fixed' font\n");
186 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
187 dc
.font
.descent
= dc
.font
.xfont
->descent
;
189 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
193 textw(const char *text
) {
194 return textnw(text
, strlen(text
)) + dc
.font
.height
;