Xinqi Bao's Git

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