Xinqi Bao's Git

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