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(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 initcolor(const char *colstr
)
86 Colormap cmap
= DefaultColormap(dpy
, screen
);
88 XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
);
93 textnw(char *text
, unsigned int len
)
97 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
100 return XTextWidth(dc
.font
.xfont
, text
, len
);
106 return textnw(text
, strlen(text
));
110 initfont(const char *fontstr
)
112 char **missing
, *def
;
116 setlocale(LC_ALL
, "");
118 XFreeFontSet(dpy
, dc
.font
.set
);
119 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
122 fprintf(stderr
, "missing fontset: %s\n", missing
[n
]);
123 XFreeStringList(missing
);
125 XFreeFontSet(dpy
, dc
.font
.set
);
130 XFontSetExtents
*font_extents
;
131 XFontStruct
**xfonts
;
134 dc
.font
.ascent
= dc
.font
.descent
= 0;
135 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
136 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
137 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
138 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
139 dc
.font
.ascent
= (*xfonts
)->ascent
;
140 if(dc
.font
.descent
< (*xfonts
)->descent
)
141 dc
.font
.descent
= (*xfonts
)->descent
;
147 XFreeFont(dpy
, dc
.font
.xfont
);
148 dc
.font
.xfont
= NULL
;
149 dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
);
151 dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed");
153 error("error, cannot init 'fixed' font\n");
154 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
155 dc
.font
.descent
= dc
.font
.xfont
->descent
;
157 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;