removed unnecessary border color
[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)
28 {
29         int x, y, w, h;
30         static char buf[256];
31         unsigned int len;
32         XGCValues gcv;
33         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
34
35         XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
36         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
37         w = 0;
38
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 = invert ? dc.bg : dc.fg;
60         gcv.background = invert ? 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 }
72
73 unsigned long
74 getcolor(const char *colstr)
75 {
76         Colormap cmap = DefaultColormap(dpy, screen);
77         XColor color;
78
79         XAllocNamedColor(dpy, cmap, colstr, &color, &color);
80         return color.pixel;
81 }
82
83 void
84 setfont(const char *fontstr)
85 {
86         char **missing, *def;
87         int i, n;
88
89         missing = NULL;
90         setlocale(LC_ALL, "");
91         if(dc.font.set)
92                 XFreeFontSet(dpy, dc.font.set);
93         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
94         if(missing) {
95                 XFreeStringList(missing);
96                 if(dc.font.set) {
97                         XFreeFontSet(dpy, dc.font.set);
98                         dc.font.set = NULL;
99                 }
100         }
101         if(dc.font.set) {
102                 XFontSetExtents *font_extents;
103                 XFontStruct **xfonts;
104                 char **font_names;
105
106                 dc.font.ascent = dc.font.descent = 0;
107                 font_extents = XExtentsOfFontSet(dc.font.set);
108                 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
109                 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
110                         if(dc.font.ascent < (*xfonts)->ascent)
111                                 dc.font.ascent = (*xfonts)->ascent;
112                         if(dc.font.descent < (*xfonts)->descent)
113                                 dc.font.descent = (*xfonts)->descent;
114                         xfonts++;
115                 }
116         }
117         else {
118                 if(dc.font.xfont)
119                         XFreeFont(dpy, dc.font.xfont);
120                 dc.font.xfont = NULL;
121                 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
122                 if (!dc.font.xfont)
123                         dc.font.xfont = XLoadQueryFont(dpy, "fixed");
124                 if (!dc.font.xfont)
125                         eprint("error, cannot init 'fixed' font\n");
126                 dc.font.ascent = dc.font.xfont->ascent;
127                 dc.font.descent = dc.font.xfont->descent;
128         }
129         dc.font.height = dc.font.ascent + dc.font.descent;
130 }
131
132 unsigned int
133 textw(const char *text)
134 {
135         return textnw(text, strlen(text)) + dc.font.height;
136 }