rearranged several stuff
[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 void
13 drawborder(void)
14 {
15         XPoint points[5];
16
17         XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
18         XSetForeground(dpy, dc.gc, dc.border);
19         points[0].x = dc.x;
20         points[0].y = dc.y;
21         points[1].x = dc.w - 1;
22         points[1].y = 0;
23         points[2].x = 0;
24         points[2].y = dc.h - 1;
25         points[3].x = -(dc.w - 1);
26         points[3].y = 0;
27         points[4].x = 0;
28         points[4].y = -(dc.h - 1);
29         XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
30 }
31
32 static unsigned int
33 textnw(const char *text, unsigned int len)
34 {
35         XRectangle r;
36
37         if(dc.font.set) {
38                 XmbTextExtents(dc.font.set, text, len, NULL, &r);
39                 return r.width;
40         }
41         return XTextWidth(dc.font.xfont, text, len);
42 }
43
44 /* extern */
45
46 void
47 drawtext(const char *text, Bool invert, Bool border)
48 {
49         int x, y, w, h;
50         static char buf[256];
51         unsigned int len;
52         XGCValues gcv;
53         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
54
55         XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
56         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
57
58         w = 0;
59         if(border)
60                 drawborder();
61
62         if(!text)
63                 return;
64
65         len = strlen(text);
66         if(len >= sizeof(buf))
67                 len = sizeof(buf) - 1;
68         memcpy(buf, text, len);
69         buf[len] = 0;
70
71         h = dc.font.ascent + dc.font.descent;
72         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
73         x = dc.x + (h / 2);
74
75         /* shorten text if necessary */
76         while(len && (w = textnw(buf, len)) > dc.w - h)
77                 buf[--len] = 0;
78
79         if(w > dc.w)
80                 return; /* too long */
81
82         gcv.foreground = invert ? dc.bg : dc.fg;
83         gcv.background = invert ? dc.fg : dc.bg;
84         if(dc.font.set) {
85                 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
86                 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
87                                 x, y, buf, len);
88         }
89         else {
90                 gcv.font = dc.font.xfont->fid;
91                 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
92                 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
93         }
94 }
95
96 unsigned long
97 getcolor(const char *colstr)
98 {
99         Colormap cmap = DefaultColormap(dpy, screen);
100         XColor color;
101
102         XAllocNamedColor(dpy, cmap, colstr, &color, &color);
103         return color.pixel;
104 }
105
106 void
107 setfont(const char *fontstr)
108 {
109         char **missing, *def;
110         int i, n;
111
112         missing = NULL;
113         setlocale(LC_ALL, "");
114         if(dc.font.set)
115                 XFreeFontSet(dpy, dc.font.set);
116         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
117         if(missing) {
118                 while(n--)
119                         fprintf(stderr, "missing fontset: %s\n", missing[n]);
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 }