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
, b
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
18 XSetForeground(dpy
, b
->gc
, b
->border
);
21 points
[1].x
= b
->w
- 1;
24 points
[2].y
= b
->h
- 1;
25 points
[3].x
= -(b
->w
- 1);
28 points
[4].y
= -(b
->h
- 1);
29 XDrawLines(dpy
, b
->drawable
, b
->gc
, points
, 5, CoordModePrevious
);
33 draw(Brush
*b
, Bool border
, const char *text
)
39 XRectangle r
= { b
->x
, b
->y
, b
->w
, b
->h
};
41 XSetForeground(dpy
, b
->gc
, b
->bg
);
42 XFillRectangles(dpy
, b
->drawable
, b
->gc
, &r
, 1);
52 if(len
>= sizeof(buf
))
53 len
= sizeof(buf
) - 1;
54 memcpy(buf
, text
, len
);
57 h
= b
->font
.ascent
+ b
->font
.descent
;
58 y
= b
->y
+ (b
->h
/ 2) - (h
/ 2) + b
->font
.ascent
;
61 /* shorten text if necessary */
62 while(len
&& (w
= textnw(&b
->font
, buf
, len
)) > b
->w
- h
)
66 return; /* too long */
68 gcv
.foreground
= b
->fg
;
69 gcv
.background
= b
->bg
;
71 XChangeGC(dpy
, b
->gc
, GCForeground
| GCBackground
, &gcv
);
72 XmbDrawImageString(dpy
, b
->drawable
, b
->font
.set
, b
->gc
,
76 gcv
.font
= b
->font
.xfont
->fid
;
77 XChangeGC(dpy
, b
->gc
, GCForeground
| GCBackground
| GCFont
, &gcv
);
78 XDrawImageString(dpy
, b
->drawable
, b
->gc
, x
, y
, buf
, len
);
83 xloadcolors(Colormap cmap
, const char *colstr
)
86 XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
);
91 loadcolors(int scr
, Brush
*b
,
92 const char *bg
, const char *fg
, const char *border
)
94 Colormap cmap
= DefaultColormap(dpy
, scr
);
95 b
->bg
= xloadcolors(cmap
, bg
);
96 b
->fg
= xloadcolors(cmap
, fg
);
97 b
->border
= xloadcolors(cmap
, border
);
101 textnw(Fnt
*font
, char *text
, unsigned int len
)
105 XmbTextExtents(font
->set
, text
, len
, NULL
, &r
);
108 return XTextWidth(font
->xfont
, text
, len
);
112 textw(Fnt
*font
, char *text
)
114 return textnw(font
, text
, strlen(text
));
120 return font
->height
+ 4;
124 loadfont(Fnt
*font
, const char *fontstr
)
126 char **missing
, *def
;
130 setlocale(LC_ALL
, "");
132 XFreeFontSet(dpy
, font
->set
);
133 font
->set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
136 fprintf(stderr
, "missing fontset: %s\n", missing
[n
]);
137 XFreeStringList(missing
);
139 XFreeFontSet(dpy
, font
->set
);
144 XFontSetExtents
*font_extents
;
145 XFontStruct
**xfonts
;
148 font
->ascent
= font
->descent
= 0;
149 font_extents
= XExtentsOfFontSet(font
->set
);
150 n
= XFontsOfFontSet(font
->set
, &xfonts
, &font_names
);
151 for(i
= 0, font
->ascent
= 0, font
->descent
= 0; i
< n
; i
++) {
152 if(font
->ascent
< (*xfonts
)->ascent
)
153 font
->ascent
= (*xfonts
)->ascent
;
154 if(font
->descent
< (*xfonts
)->descent
)
155 font
->descent
= (*xfonts
)->descent
;
161 XFreeFont(dpy
, font
->xfont
);
163 font
->xfont
= XLoadQueryFont(dpy
, fontstr
);
165 font
->xfont
= XLoadQueryFont(dpy
, "fixed");
167 error("error, cannot load 'fixed' font\n");
168 font
->ascent
= font
->xfont
->ascent
;
169 font
->descent
= font
->xfont
->descent
;
171 font
->height
= font
->ascent
+ font
->descent
;