Xinqi Bao's Git
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
13 drawborder(Display
*dpy
, Brush
*b
)
16 XSetLineAttributes(dpy
, b
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
17 XSetForeground(dpy
, b
->gc
, b
->border
);
20 points
[1].x
= b
->w
- 1;
23 points
[2].y
= b
->h
- 1;
24 points
[3].x
= -(b
->w
- 1);
27 points
[4].y
= -(b
->h
- 1);
28 XDrawLines(dpy
, b
->drawable
, b
->gc
, points
, 5, CoordModePrevious
);
32 draw(Display
*dpy
, Brush
*b
, Bool border
, const char *text
)
34 unsigned int x
, y
, w
, h
, len
;
37 XRectangle r
= { b
->x
, b
->y
, b
->w
, b
->h
};
39 XSetForeground(dpy
, b
->gc
, b
->bg
);
40 XFillRectangles(dpy
, b
->drawable
, b
->gc
, &r
, 1);
50 if(len
>= sizeof(buf
))
51 len
= sizeof(buf
) - 1;
52 memcpy(buf
, text
, len
);
55 h
= b
->font
.ascent
+ b
->font
.descent
;
56 y
= b
->y
+ (b
->h
/ 2) - (h
/ 2) + b
->font
.ascent
;
59 /* shorten text if necessary */
60 while(len
&& (w
= textnw(&b
->font
, buf
, len
)) > b
->w
- h
)
64 return; /* too long */
66 gcv
.foreground
= b
->fg
;
67 gcv
.background
= b
->bg
;
69 XChangeGC(dpy
, b
->gc
, GCForeground
| GCBackground
, &gcv
);
70 XmbDrawImageString(dpy
, b
->drawable
, b
->font
.set
, b
->gc
,
74 gcv
.font
= b
->font
.xfont
->fid
;
75 XChangeGC(dpy
, b
->gc
, GCForeground
| GCBackground
| GCFont
, &gcv
);
76 XDrawImageString(dpy
, b
->drawable
, b
->gc
, x
, y
, buf
, len
);
81 xloadcolors(Display
*dpy
, Colormap cmap
, const char *colstr
)
84 XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
);
89 loadcolors(Display
*dpy
, int screen
, Brush
*b
,
90 const char *bg
, const char *fg
, const char *border
)
92 Colormap cmap
= DefaultColormap(dpy
, screen
);
93 b
->bg
= xloadcolors(dpy
, cmap
, bg
);
94 b
->fg
= xloadcolors(dpy
, cmap
, fg
);
95 b
->border
= xloadcolors(dpy
, cmap
, border
);
99 textnw(Fnt
*font
, char *text
, unsigned int len
)
103 XmbTextExtents(font
->set
, text
, len
, NULL
, &r
);
106 return XTextWidth(font
->xfont
, text
, len
);
110 textw(Fnt
*font
, char *text
)
112 return textnw(font
, text
, strlen(text
));
118 return font
->height
+ 4;
122 loadfont(Display
*dpy
, Fnt
*font
, const char *fontstr
)
124 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
;
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
;