Xinqi Bao's Git

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