Xinqi Bao's Git

removed unnecessary Xlib call
[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 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 XSetForeground(dpy, dc.gc, dc.border);
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 gcv.foreground = invert ? dc.bg : dc.fg;
84 gcv.background = invert ? dc.fg : dc.bg;
85 if(dc.font.set) {
86 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
87 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
88 x, y, buf, len);
89 }
90 else {
91 gcv.font = dc.font.xfont->fid;
92 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
93 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
94 }
95 }
96
97 unsigned long
98 getcolor(const char *colstr)
99 {
100 Colormap cmap = DefaultColormap(dpy, screen);
101 XColor color;
102
103 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
104 return color.pixel;
105 }
106
107 void
108 setfont(const char *fontstr)
109 {
110 char **missing, *def;
111 int i, n;
112
113 missing = NULL;
114 setlocale(LC_ALL, "");
115 if(dc.font.set)
116 XFreeFontSet(dpy, dc.font.set);
117 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
118 if(missing) {
119 XFreeStringList(missing);
120 if(dc.font.set) {
121 XFreeFontSet(dpy, dc.font.set);
122 dc.font.set = NULL;
123 }
124 }
125 if(dc.font.set) {
126 XFontSetExtents *font_extents;
127 XFontStruct **xfonts;
128 char **font_names;
129
130 dc.font.ascent = dc.font.descent = 0;
131 font_extents = XExtentsOfFontSet(dc.font.set);
132 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
133 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
134 if(dc.font.ascent < (*xfonts)->ascent)
135 dc.font.ascent = (*xfonts)->ascent;
136 if(dc.font.descent < (*xfonts)->descent)
137 dc.font.descent = (*xfonts)->descent;
138 xfonts++;
139 }
140 }
141 else {
142 if(dc.font.xfont)
143 XFreeFont(dpy, dc.font.xfont);
144 dc.font.xfont = NULL;
145 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
146 if (!dc.font.xfont)
147 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
148 if (!dc.font.xfont)
149 eprint("error, cannot init 'fixed' font\n");
150 dc.font.ascent = dc.font.xfont->ascent;
151 dc.font.descent = dc.font.xfont->descent;
152 }
153 dc.font.height = dc.font.ascent + dc.font.descent;
154 }
155
156 unsigned int
157 textw(const char *text)
158 {
159 return textnw(text, strlen(text)) + dc.font.height;
160 }