Xinqi Bao's Git
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
9 #include <X11/Xlocale.h>
17 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
18 XSetForeground(dpy
, dc
.gc
, dc
.border
);
21 points
[1].x
= dc
.w
- 1;
24 points
[2].y
= dc
.h
- 1;
25 points
[3].x
= -(dc
.w
- 1);
28 points
[4].y
= -(dc
.h
- 1);
29 XDrawLines(dpy
, dc
.drawable
, dc
.gc
, points
, 5, CoordModePrevious
);
33 draw(Bool border
, const char *text
)
39 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
41 XSetForeground(dpy
, dc
.gc
, dc
.bg
);
42 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
52 if(len
>= sizeof(buf
))
53 len
= sizeof(buf
) - 1;
54 memcpy(buf
, text
, len
);
57 h
= dc
.font
.ascent
+ dc
.font
.descent
;
58 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
61 /* shorten text if necessary */
62 while(len
&& (w
= textnw(&dc
.font
, buf
, len
)) > dc
.w
- h
)
66 return; /* too long */
68 gcv
.foreground
= dc
.fg
;
69 gcv
.background
= dc
.bg
;
71 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCBackground
, &gcv
);
72 XmbDrawImageString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
,
76 gcv
.font
= dc
.font
.xfont
->fid
;
77 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCBackground
| GCFont
, &gcv
);
78 XDrawImageString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
83 xinitcolors(Colormap cmap
, const char *colstr
)
86 XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
);
91 initcolors(const char *bg
, const char *fg
, const char *border
)
93 Colormap cmap
= DefaultColormap(dpy
, screen
);
94 dc
.bg
= xinitcolors(cmap
, bg
);
95 dc
.fg
= xinitcolors(cmap
, fg
);
96 dc
.border
= xinitcolors(cmap
, border
);
100 textnw(Fnt
*font
, char *text
, unsigned int len
)
104 XmbTextExtents(font
->set
, text
, len
, NULL
, &r
);
107 return XTextWidth(font
->xfont
, text
, len
);
111 textw(Fnt
*font
, char *text
)
113 return textnw(font
, text
, strlen(text
));
119 return font
->height
+ 4;
123 initfont(Fnt
*font
, const char *fontstr
)
125 char **missing
, *def
;
129 setlocale(LC_ALL
, "");
131 XFreeFontSet(dpy
, font
->set
);
132 font
->set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
135 fprintf(stderr
, "missing fontset: %s\n", missing
[n
]);
136 XFreeStringList(missing
);
138 XFreeFontSet(dpy
, font
->set
);
143 XFontSetExtents
*font_extents
;
144 XFontStruct
**xfonts
;
147 font
->ascent
= font
->descent
= 0;
148 font_extents
= XExtentsOfFontSet(font
->set
);
149 n
= XFontsOfFontSet(font
->set
, &xfonts
, &font_names
);
150 for(i
= 0, font
->ascent
= 0, font
->descent
= 0; i
< n
; i
++) {
151 if(font
->ascent
< (*xfonts
)->ascent
)
152 font
->ascent
= (*xfonts
)->ascent
;
153 if(font
->descent
< (*xfonts
)->descent
)
154 font
->descent
= (*xfonts
)->descent
;
160 XFreeFont(dpy
, font
->xfont
);
162 font
->xfont
= XLoadQueryFont(dpy
, fontstr
);
164 font
->xfont
= XLoadQueryFont(dpy
, "fixed");
166 error("error, cannot init 'fixed' font\n");
167 font
->ascent
= font
->xfont
->ascent
;
168 font
->descent
= font
->xfont
->descent
;
170 font
->height
= font
->ascent
+ font
->descent
;