Xinqi Bao's Git
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
8 #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 textnw(const char *text
, unsigned int len
)
38 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
41 return XTextWidth(dc
.font
.xfont
, text
, len
);
47 drawtext(const char *text
, Bool invert
, Bool border
)
53 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
55 XSetForeground(dpy
, dc
.gc
, invert
? dc
.fg
: dc
.bg
);
56 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
66 if(len
>= sizeof(buf
))
67 len
= sizeof(buf
) - 1;
68 memcpy(buf
, text
, len
);
71 h
= dc
.font
.ascent
+ dc
.font
.descent
;
72 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
75 /* shorten text if necessary */
76 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
80 return; /* too long */
82 gcv
.foreground
= invert
? dc
.bg
: dc
.fg
;
83 gcv
.background
= invert
? dc
.fg
: dc
.bg
;
85 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCBackground
, &gcv
);
86 XmbDrawImageString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
,
90 gcv
.font
= dc
.font
.xfont
->fid
;
91 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCBackground
| GCFont
, &gcv
);
92 XDrawImageString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
97 getcolor(const char *colstr
)
99 Colormap cmap
= DefaultColormap(dpy
, screen
);
102 XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
);
107 setfont(const char *fontstr
)
109 char **missing
, *def
;
113 setlocale(LC_ALL
, "");
115 XFreeFontSet(dpy
, dc
.font
.set
);
116 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
118 XFreeStringList(missing
);
120 XFreeFontSet(dpy
, dc
.font
.set
);
125 XFontSetExtents
*font_extents
;
126 XFontStruct
**xfonts
;
129 dc
.font
.ascent
= dc
.font
.descent
= 0;
130 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
131 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
132 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
133 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
134 dc
.font
.ascent
= (*xfonts
)->ascent
;
135 if(dc
.font
.descent
< (*xfonts
)->descent
)
136 dc
.font
.descent
= (*xfonts
)->descent
;
142 XFreeFont(dpy
, dc
.font
.xfont
);
143 dc
.font
.xfont
= NULL
;
144 dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
);
146 dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed");
148 eprint("error, cannot init 'fixed' font\n");
149 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
150 dc
.font
.descent
= dc
.font
.xfont
->descent
;
152 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
156 textw(const char *text
)
158 return textnw(text
, strlen(text
)) + dc
.font
.height
;