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 isoccupied(unsigned int t
)
15 for(c
= clients
; c
; c
= c
->next
)
22 textnw(const char *text
, unsigned int len
) {
26 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
29 return XTextWidth(dc
.font
.xfont
, text
, len
);
33 drawtext(const char *text
, unsigned long col
[ColLast
], Bool filledsquare
, Bool emptysquare
) {
36 unsigned int len
, olen
;
38 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
41 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
42 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
46 olen
= len
= strlen(text
);
49 memcpy(buf
, text
, len
);
51 h
= dc
.font
.ascent
+ dc
.font
.descent
;
52 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
54 /* shorten text if necessary */
55 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
66 return; /* too long */
67 gcv
.foreground
= col
[ColFG
];
69 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
70 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
73 gcv
.font
= dc
.font
.xfont
->fid
;
74 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCFont
, &gcv
);
75 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
81 r
.width
= r
.height
= x
+ 1;
82 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
84 else if(emptysquare
) {
95 XDrawLines(dpy
, dc
.drawable
, dc
.gc
, pt
, 5, CoordModePrevious
);
105 for(c
= clients
; c
; c
= getnext(c
->next
))
115 for(i
= 0; i
< ntags
; i
++) {
116 dc
.w
= textw(tags
[i
]);
118 drawtext(tags
[i
], dc
.sel
, sel
&& sel
->tags
[i
], isoccupied(i
));
120 drawtext(tags
[i
], dc
.norm
, sel
&& sel
->tags
[i
], isoccupied(i
));
124 drawtext(arrange
== dofloat
? FLOATSYMBOL
: TILESYMBOL
, dc
.status
, False
, False
);
132 drawtext(stext
, dc
.status
, False
, False
);
133 if((dc
.w
= dc
.x
- x
) > bh
) {
135 drawtext(sel
? sel
->name
: NULL
, sel
? dc
.sel
: dc
.norm
, False
, False
);
137 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0);
142 drawtitle(Client
*c
) {
143 if(c
== sel
&& issel
) {
145 XUnmapWindow(dpy
, c
->twin
);
146 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBG
]);
149 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBG
]);
150 XMapWindow(dpy
, c
->twin
);
153 drawtext(c
->name
, dc
.norm
, False
,False
);
154 XCopyArea(dpy
, dc
.drawable
, c
->twin
, dc
.gc
, 0, 0, c
->tw
, c
->th
, 0, 0);
159 getcolor(const char *colstr
) {
160 Colormap cmap
= DefaultColormap(dpy
, screen
);
163 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
164 eprint("error, cannot allocate color '%s'\n", colstr
);
169 setfont(const char *fontstr
) {
170 char *def
, *lc
, **missing
;
173 lc
= setlocale(LC_CTYPE
, NULL
);
174 setlocale(LC_CTYPE
, "UTF-8");
177 XFreeFontSet(dpy
, dc
.font
.set
);
178 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
179 setlocale(LC_CTYPE
, lc
);
182 fprintf(stderr
, "missing fontset: %s\n", missing
[n
]);
183 XFreeStringList(missing
);
186 XFontSetExtents
*font_extents
;
187 XFontStruct
**xfonts
;
189 dc
.font
.ascent
= dc
.font
.descent
= 0;
190 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
191 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
192 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
193 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
194 dc
.font
.ascent
= (*xfonts
)->ascent
;
195 if(dc
.font
.descent
< (*xfonts
)->descent
)
196 dc
.font
.descent
= (*xfonts
)->descent
;
202 XFreeFont(dpy
, dc
.font
.xfont
);
203 dc
.font
.xfont
= NULL
;
204 dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
);
206 dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed");
208 eprint("error, cannot init 'fixed' font\n");
209 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
210 dc
.font
.descent
= dc
.font
.xfont
->descent
;
212 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
216 textw(const char *text
) {
217 return textnw(text
, strlen(text
)) + dc
.font
.height
;