Xinqi Bao's Git
932b2f659029b61b7135b8fc3679fce8fbd57b23
1 /* See LICENSE file for copyright and license details. */
4 enum { ColFG
, ColBG
, ColLast
};
9 unsigned long norm
[ColLast
];
10 unsigned long sel
[ColLast
];
20 } DC
; /* draw context */
22 /* forward declarations */
23 static void dccleanup(void);
24 static void dcsetup(void);
25 static void drawtext(const char *text
, unsigned long col
[ColLast
]);
26 static unsigned long getcolor(const char *colstr
);
27 static void initfont(const char *fontstr
);
28 static int textnw(const char *text
, unsigned int len
);
29 static int textw(const char *text
);
36 XFreeFontSet(dpy
, dc
.font
.set
);
38 XFreeFont(dpy
, dc
.font
.xfont
);
39 XFreePixmap(dpy
, dc
.drawable
);
46 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
47 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
48 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
49 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
52 dc
.drawable
= XCreatePixmap(dpy
, parent
, mw
, mh
, DefaultDepth(dpy
, screen
));
53 dc
.gc
= XCreateGC(dpy
, parent
, 0, NULL
);
54 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
56 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
60 drawtext(const char *text
, unsigned long col
[ColLast
]) {
62 int i
, x
, y
, h
, len
, olen
;
63 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
65 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
66 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
71 y
= dc
.y
+ ((h
+2) / 2) - (h
/ 2) + dc
.font
.ascent
;
73 /* shorten text if necessary */
74 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
77 memcpy(buf
, text
, len
);
79 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
80 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
82 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
84 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
88 getcolor(const char *colstr
) {
89 Colormap cmap
= DefaultColormap(dpy
, screen
);
92 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
93 eprint("drawtext: cannot allocate color '%s'\n", colstr
);
98 initfont(const char *fontstr
) {
99 char *def
, **missing
= NULL
;
102 if(!fontstr
|| fontstr
[0] == '\0')
103 eprint("drawtext: cannot load font: '%s'\n", fontstr
);
104 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
106 XFreeStringList(missing
);
108 XFontStruct
**xfonts
;
110 dc
.font
.ascent
= dc
.font
.descent
= 0;
111 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
112 for(i
= 0; i
< n
; i
++) {
113 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
114 dc
.font
.descent
= MAX(dc
.font
.descent
, (*xfonts
)->descent
);
119 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
120 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
121 eprint("drawtext: cannot load font: '%s'\n", fontstr
);
122 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
123 dc
.font
.descent
= dc
.font
.xfont
->descent
;
125 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
129 textnw(const char *text
, unsigned int len
) {
133 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
136 return XTextWidth(dc
.font
.xfont
, text
, len
);
140 textw(const char *text
) {
141 return textnw(text
, strlen(text
)) + dc
.font
.height
;