Xinqi Bao's Git
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(dpy
, dc
.font
.set
);
24 XFreeFont(dpy
, dc
.font
.xfont
);
25 XFreePixmap(dpy
, dc
.drawable
);
32 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
33 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
34 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
35 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
38 dc
.drawable
= XCreatePixmap(dpy
, parent
, mw
, mh
, DefaultDepth(dpy
, screen
));
39 dc
.gc
= XCreateGC(dpy
, parent
, 0, NULL
);
40 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
42 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
46 drawtext(const char *text
, unsigned long col
[ColLast
]) {
48 int i
, x
, y
, h
, len
, olen
;
49 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
51 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
52 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
57 y
= dc
.y
+ ((h
+2) / 2) - (h
/ 2) + dc
.font
.ascent
;
59 /* shorten text if necessary */
60 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
63 memcpy(buf
, text
, len
);
65 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
66 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
68 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
70 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
74 eprint(const char *errstr
, ...) {
77 fprintf(stderr
, "%s: ", progname
);
79 vfprintf(stderr
, errstr
, ap
);
85 getcolor(const char *colstr
) {
86 Colormap cmap
= DefaultColormap(dpy
, screen
);
89 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
90 eprint("cannot allocate color '%s'\n", colstr
);
95 initfont(const char *fontstr
) {
96 char *def
, **missing
= NULL
;
99 if(!fontstr
|| !*fontstr
)
100 eprint("cannot load null font\n");
101 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
103 XFreeStringList(missing
);
105 XFontStruct
**xfonts
;
107 dc
.font
.ascent
= dc
.font
.descent
= 0;
108 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
109 for(i
= 0; i
< n
; i
++) {
110 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
111 dc
.font
.descent
= MAX(dc
.font
.descent
, (*xfonts
)->descent
);
116 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
117 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
118 eprint("cannot load font '%s'\n", fontstr
);
119 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
120 dc
.font
.descent
= dc
.font
.xfont
->descent
;
122 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
126 textnw(const char *text
, unsigned int len
) {
130 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
133 return XTextWidth(dc
.font
.xfont
, text
, len
);
137 textw(const char *text
) {
138 return textnw(text
, strlen(text
)) + dc
.font
.height
;