Xinqi Bao's Git

Added tag 0.6 for changeset 25f679fb19686140a907684ffcb423b9e9d44b53
[dmenu.git] / draw.c
1 /*
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
5 #include "dmenu.h"
6 #include <stdio.h>
7 #include <string.h>
8 #include <X11/Xlocale.h>
9
10 /* static */
11
12 static unsigned int
13 textnw(const char *text, unsigned int len)
14 {
15 XRectangle r;
16
17 if(dc.font.set) {
18 XmbTextExtents(dc.font.set, text, len, NULL, &r);
19 return r.width;
20 }
21 return XTextWidth(dc.font.xfont, text, len);
22 }
23
24 /* extern */
25
26 void
27 drawtext(const char *text, unsigned long col[ColLast])
28 {
29 int x, y, w, h;
30 static char buf[256];
31 unsigned int len, olen;
32 XGCValues gcv;
33 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
34
35 XSetForeground(dpy, dc.gc, col[ColBG]);
36 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
37
38 if(!text)
39 return;
40
41 w = 0;
42 olen = len = strlen(text);
43 if(len >= sizeof(buf))
44 len = sizeof(buf) - 1;
45 memcpy(buf, text, len);
46 buf[len] = 0;
47
48 h = dc.font.ascent + dc.font.descent;
49 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
50 x = dc.x + (h / 2);
51
52 /* shorten text if necessary */
53 while(len && (w = textnw(buf, len)) > dc.w - h)
54 buf[--len] = 0;
55 if(len < olen) {
56 if(len > 1)
57 buf[len - 1] = '.';
58 if(len > 2)
59 buf[len - 2] = '.';
60 if(len > 3)
61 buf[len - 3] = '.';
62 }
63
64 if(w > dc.w)
65 return; /* too long */
66
67 gcv.foreground = col[ColFG];
68 if(dc.font.set) {
69 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
70 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
71 x, y, buf, len);
72 }
73 else {
74 gcv.font = dc.font.xfont->fid;
75 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
76 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
77 }
78 }
79
80 unsigned long
81 getcolor(const char *colstr)
82 {
83 Colormap cmap = DefaultColormap(dpy, screen);
84 XColor color;
85
86 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
87 return color.pixel;
88 }
89
90 void
91 setfont(const char *fontstr)
92 {
93 char **missing, *def;
94 int i, n;
95
96 missing = NULL;
97 setlocale(LC_ALL, "");
98 if(dc.font.set)
99 XFreeFontSet(dpy, dc.font.set);
100 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
101 if(missing) {
102 XFreeStringList(missing);
103 if(dc.font.set) {
104 XFreeFontSet(dpy, dc.font.set);
105 dc.font.set = NULL;
106 }
107 }
108 if(dc.font.set) {
109 XFontSetExtents *font_extents;
110 XFontStruct **xfonts;
111 char **font_names;
112
113 dc.font.ascent = dc.font.descent = 0;
114 font_extents = XExtentsOfFontSet(dc.font.set);
115 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
116 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
117 if(dc.font.ascent < (*xfonts)->ascent)
118 dc.font.ascent = (*xfonts)->ascent;
119 if(dc.font.descent < (*xfonts)->descent)
120 dc.font.descent = (*xfonts)->descent;
121 xfonts++;
122 }
123 }
124 else {
125 if(dc.font.xfont)
126 XFreeFont(dpy, dc.font.xfont);
127 dc.font.xfont = NULL;
128 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
129 if (!dc.font.xfont)
130 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
131 if (!dc.font.xfont)
132 eprint("error, cannot init 'fixed' font\n");
133 dc.font.ascent = dc.font.xfont->ascent;
134 dc.font.descent = dc.font.xfont->descent;
135 }
136 dc.font.height = dc.font.ascent + dc.font.descent;
137 }
138
139 unsigned int
140 textw(const char *text)
141 {
142 return textnw(text, strlen(text)) + dc.font.height;
143 }