Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #define MAX(a, b) ((a) > (b) ? (a) : (b))
11 #define MIN(a, b) ((a) < (b) ? (a) : (b))
12 #define DEFFONT "fixed"
14 static Bool
loadfont(DC
*dc
, const char *fontstr
);
17 drawrect(DC
*dc
, int x
, int y
, unsigned int w
, unsigned int h
, Bool fill
, unsigned long color
) {
22 r
.width
= fill
? w
: w
-1;
23 r
.height
= fill
? h
: h
-1;
25 XSetForeground(dc
->dpy
, dc
->gc
, color
);
26 (fill
? XFillRectangles
: XDrawRectangles
)(dc
->dpy
, dc
->canvas
, dc
->gc
, &r
, 1);
30 drawtext(DC
*dc
, const char *text
, unsigned long col
[ColLast
]) {
32 size_t mn
, n
= strlen(text
);
34 /* shorten text if necessary */
35 for(mn
= MIN(n
, sizeof buf
); textnw(dc
, text
, mn
) + dc
->font
.height
/2 > dc
->w
; mn
--)
38 memcpy(buf
, text
, mn
);
40 for(n
= MAX(mn
-3, 0); n
< mn
; buf
[n
++] = '.');
42 drawrect(dc
, 0, 0, dc
->w
, dc
->h
, True
, BG(dc
, col
));
43 drawtextn(dc
, buf
, mn
, col
);
47 drawtextn(DC
*dc
, const char *text
, size_t n
, unsigned long col
[ColLast
]) {
48 int x
= dc
->x
+ dc
->font
.height
/2;
49 int y
= dc
->y
+ dc
->font
.ascent
+1;
51 XSetForeground(dc
->dpy
, dc
->gc
, FG(dc
, col
));
53 XmbDrawString(dc
->dpy
, dc
->canvas
, dc
->font
.set
, dc
->gc
, x
, y
, text
, n
);
55 XSetFont(dc
->dpy
, dc
->gc
, dc
->font
.xfont
->fid
);
56 XDrawString(dc
->dpy
, dc
->canvas
, dc
->gc
, x
, y
, text
, n
);
61 eprintf(const char *fmt
, ...) {
65 vfprintf(stderr
, fmt
, ap
);
68 if(fmt
[strlen(fmt
)-1] == ':') {
78 XFreeFontSet(dc
->dpy
, dc
->font
.set
);
80 XFreeFont(dc
->dpy
, dc
->font
.xfont
);
82 XFreePixmap(dc
->dpy
, dc
->canvas
);
83 XFreeGC(dc
->dpy
, dc
->gc
);
84 XCloseDisplay(dc
->dpy
);
89 getcolor(DC
*dc
, const char *colstr
) {
90 Colormap cmap
= DefaultColormap(dc
->dpy
, DefaultScreen(dc
->dpy
));
93 if(!XAllocNamedColor(dc
->dpy
, cmap
, colstr
, &color
, &color
))
94 eprintf("cannot allocate color '%s'\n", colstr
);
102 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
103 fprintf(stderr
, "no locale support\n");
104 if(!(dc
= calloc(1, sizeof *dc
)))
105 eprintf("cannot malloc %u bytes:", sizeof *dc
);
106 if(!(dc
->dpy
= XOpenDisplay(NULL
)))
107 eprintf("cannot open display\n");
109 dc
->gc
= XCreateGC(dc
->dpy
, DefaultRootWindow(dc
->dpy
), 0, NULL
);
110 XSetLineAttributes(dc
->dpy
, dc
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
115 initfont(DC
*dc
, const char *fontstr
) {
116 if(!loadfont(dc
, fontstr
? fontstr
: DEFFONT
)) {
118 fprintf(stderr
, "cannot load font '%s'\n", fontstr
);
119 if(fontstr
== NULL
|| !loadfont(dc
, DEFFONT
))
120 eprintf("cannot load font '%s'\n", DEFFONT
);
122 dc
->font
.height
= dc
->font
.ascent
+ dc
->font
.descent
;
126 loadfont(DC
*dc
, const char *fontstr
) {
127 char *def
, **missing
;
132 if((dc
->font
.set
= XCreateFontSet(dc
->dpy
, fontstr
, &missing
, &n
, &def
))) {
134 XFontStruct
**xfonts
;
136 n
= XFontsOfFontSet(dc
->font
.set
, &xfonts
, &names
);
137 for(i
= dc
->font
.ascent
= dc
->font
.descent
= 0; i
< n
; i
++) {
138 dc
->font
.ascent
= MAX(dc
->font
.ascent
, xfonts
[i
]->ascent
);
139 dc
->font
.descent
= MAX(dc
->font
.descent
, xfonts
[i
]->descent
);
142 else if((dc
->font
.xfont
= XLoadQueryFont(dc
->dpy
, fontstr
))) {
143 dc
->font
.ascent
= dc
->font
.xfont
->ascent
;
144 dc
->font
.descent
= dc
->font
.xfont
->descent
;
147 XFreeStringList(missing
);
148 return (dc
->font
.set
|| dc
->font
.xfont
);
152 mapdc(DC
*dc
, Window win
, unsigned int w
, unsigned int h
) {
153 XCopyArea(dc
->dpy
, dc
->canvas
, win
, dc
->gc
, 0, 0, w
, h
, 0, 0);
157 resizedc(DC
*dc
, unsigned int w
, unsigned int h
) {
159 XFreePixmap(dc
->dpy
, dc
->canvas
);
161 dc
->canvas
= XCreatePixmap(dc
->dpy
, DefaultRootWindow(dc
->dpy
), w
, h
,
162 DefaultDepth(dc
->dpy
, DefaultScreen(dc
->dpy
)));
168 textnw(DC
*dc
, const char *text
, size_t len
) {
172 XmbTextExtents(dc
->font
.set
, text
, len
, NULL
, &r
);
175 return XTextWidth(dc
->font
.xfont
, text
, len
);
179 textw(DC
*dc
, const char *text
) {
180 return textnw(dc
, text
, strlen(text
)) + dc
->font
.height
;