Xinqi Bao's Git

6802403f8742ef446848a9cde9ff79744322e329
[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 int colidx, Bool border)
28 {
29 int x, y, w, h;
30 static char buf[256];
31 unsigned int len, olen;
32 XGCValues gcv;
33 XPoint points[5];
34 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
35
36 XSetForeground(dpy, dc.gc, dc.bg[colidx]);
37 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
38
39 w = 0;
40 XSetForeground(dpy, dc.gc, dc.fg[colidx]);
41 if(border) {
42 points[0].x = dc.x;
43 points[0].y = dc.y;
44 points[1].x = dc.w - 1;
45 points[1].y = 0;
46 points[2].x = 0;
47 points[2].y = dc.h - 1;
48 points[3].x = -(dc.w - 1);
49 points[3].y = 0;
50 points[4].x = 0;
51 points[4].y = -(dc.h - 1);
52 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
53 }
54
55 if(!text)
56 return;
57
58 olen = len = strlen(text);
59 if(len >= sizeof(buf))
60 len = sizeof(buf) - 1;
61 memcpy(buf, text, len);
62 buf[len] = 0;
63
64 h = dc.font.ascent + dc.font.descent;
65 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
66 x = dc.x + (h / 2);
67
68 /* shorten text if necessary */
69 while(len && (w = textnw(buf, len)) > dc.w - h)
70 buf[--len] = 0;
71 if(len < olen) {
72 if(len > 1)
73 buf[len - 1] = '.';
74 if(len > 2)
75 buf[len - 2] = '.';
76 if(len > 3)
77 buf[len - 3] = '.';
78 }
79
80 if(w > dc.w)
81 return; /* too long */
82
83 if(dc.font.set)
84 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
85 else {
86 gcv.font = dc.font.xfont->fid;
87 XChangeGC(dpy, dc.gc, GCFont, &gcv);
88 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
89 }
90 }
91
92 unsigned long
93 getcolor(const char *colstr)
94 {
95 Colormap cmap = DefaultColormap(dpy, screen);
96 XColor color;
97
98 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
99 return color.pixel;
100 }
101
102 void
103 setfont(const char *fontstr)
104 {
105 char **missing, *def;
106 int i, n;
107
108 missing = NULL;
109 setlocale(LC_ALL, "");
110 if(dc.font.set)
111 XFreeFontSet(dpy, dc.font.set);
112 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
113 if(missing) {
114 XFreeStringList(missing);
115 if(dc.font.set) {
116 XFreeFontSet(dpy, dc.font.set);
117 dc.font.set = NULL;
118 }
119 }
120 if(dc.font.set) {
121 XFontSetExtents *font_extents;
122 XFontStruct **xfonts;
123 char **font_names;
124
125 dc.font.ascent = dc.font.descent = 0;
126 font_extents = XExtentsOfFontSet(dc.font.set);
127 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
128 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
129 if(dc.font.ascent < (*xfonts)->ascent)
130 dc.font.ascent = (*xfonts)->ascent;
131 if(dc.font.descent < (*xfonts)->descent)
132 dc.font.descent = (*xfonts)->descent;
133 xfonts++;
134 }
135 }
136 else {
137 if(dc.font.xfont)
138 XFreeFont(dpy, dc.font.xfont);
139 dc.font.xfont = NULL;
140 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
141 if (!dc.font.xfont)
142 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
143 if (!dc.font.xfont)
144 eprint("error, cannot init 'fixed' font\n");
145 dc.font.ascent = dc.font.xfont->ascent;
146 dc.font.descent = dc.font.xfont->descent;
147 }
148 dc.font.height = dc.font.ascent + dc.font.descent;
149 }
150
151 unsigned int
152 textw(const char *text)
153 {
154 return textnw(text, strlen(text)) + dc.font.height;
155 }