Xinqi Bao's Git
9142905b8ecd20cf1a12f3321fe2552dabb50a95
1 /* See LICENSE file for copyright and license details. */
13 #define MIN(a, b) ((a) < (b) ? (a) : (b))
14 #define MAX(a, b) ((a) > (b) ? (a) : (b))
22 XFreeFontSet(dc
->dpy
, dc
->font
.set
);
24 XFreeFont(dc
->dpy
, dc
->font
.xfont
);
25 XFreePixmap(dc
->dpy
, dc
->drawable
);
26 XFreeGC(dc
->dpy
, dc
->gc
);
30 setupdraw(DC
*dc
, Window w
) {
33 XGetWindowAttributes(dc
->dpy
, w
, &wa
);
34 dc
->drawable
= XCreatePixmap(dc
->dpy
, w
, wa
.width
, wa
.height
,
35 DefaultDepth(dc
->dpy
, DefaultScreen(dc
->dpy
)));
36 dc
->gc
= XCreateGC(dc
->dpy
, w
, 0, NULL
);
37 XSetLineAttributes(dc
->dpy
, dc
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
39 XSetFont(dc
->dpy
, dc
->gc
, dc
->font
.xfont
->fid
);
43 drawtext(DC
*dc
, const char *text
, unsigned long col
[ColLast
]) {
45 int i
, x
, y
, h
, len
, olen
;
46 XRectangle r
= { dc
->x
, dc
->y
, dc
->w
, dc
->h
};
48 XSetForeground(dc
->dpy
, dc
->gc
, col
[ColBG
]);
49 XFillRectangles(dc
->dpy
, dc
->drawable
, dc
->gc
, &r
, 1);
54 y
= dc
->y
+ ((h
+2) / 2) - (h
/ 2) + dc
->font
.ascent
;
56 /* shorten text if necessary */
57 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(dc
, text
, len
) > dc
->w
- h
; len
--);
60 memcpy(buf
, text
, len
);
62 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
63 XSetForeground(dc
->dpy
, dc
->gc
, col
[ColFG
]);
65 XmbDrawString(dc
->dpy
, dc
->drawable
, dc
->font
.set
, dc
->gc
, x
, y
, buf
, len
);
67 XDrawString(dc
->dpy
, dc
->drawable
, dc
->gc
, x
, y
, buf
, len
);
71 eprint(const char *fmt
, ...) {
74 fprintf(stderr
, "%s: ", progname
);
76 vfprintf(stderr
, fmt
, ap
);
82 getcolor(DC
*dc
, const char *colstr
) {
83 Colormap cmap
= DefaultColormap(dc
->dpy
, DefaultScreen(dc
->dpy
));
86 if(!XAllocNamedColor(dc
->dpy
, cmap
, colstr
, &color
, &color
))
87 eprint("cannot allocate color '%s'\n", colstr
);
92 initfont(DC
*dc
, const char *fontstr
) {
93 char *def
, **missing
= NULL
;
96 if(!fontstr
|| !*fontstr
)
97 eprint("cannot load null font\n");
98 dc
->font
.set
= XCreateFontSet(dc
->dpy
, fontstr
, &missing
, &n
, &def
);
100 XFreeStringList(missing
);
102 XFontStruct
**xfonts
;
104 dc
->font
.ascent
= dc
->font
.descent
= 0;
105 n
= XFontsOfFontSet(dc
->font
.set
, &xfonts
, &font_names
);
106 for(i
= 0; i
< n
; i
++) {
107 dc
->font
.ascent
= MAX(dc
->font
.ascent
, (*xfonts
)->ascent
);
108 dc
->font
.descent
= MAX(dc
->font
.descent
, (*xfonts
)->descent
);
113 if(!(dc
->font
.xfont
= XLoadQueryFont(dc
->dpy
, fontstr
))
114 && !(dc
->font
.xfont
= XLoadQueryFont(dc
->dpy
, "fixed")))
115 eprint("cannot load font '%s'\n", fontstr
);
116 dc
->font
.ascent
= dc
->font
.xfont
->ascent
;
117 dc
->font
.descent
= dc
->font
.xfont
->descent
;
119 dc
->font
.height
= dc
->font
.ascent
+ dc
->font
.descent
;
123 textnw(DC
*dc
, const char *text
, unsigned int len
) {
127 XmbTextExtents(dc
->font
.set
, text
, len
, NULL
, &r
);
130 return XTextWidth(dc
->font
.xfont
, text
, len
);
134 textw(DC
*dc
, const char *text
) {
135 return textnw(dc
, text
, strlen(text
)) + dc
->font
.height
;