removed useless newlines
[dmenu.git] / draw.c
1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2  * See LICENSE file for license details.
3  */
4 #include "dmenu.h"
5 #include <stdio.h>
6 #include <string.h>
7 #include <X11/Xlocale.h>
8
9 /* static */
10
11 static unsigned int
12 textnw(const char *text, unsigned int len) {
13         XRectangle r;
14
15         if(dc.font.set) {
16                 XmbTextExtents(dc.font.set, text, len, NULL, &r);
17                 return r.width;
18         }
19         return XTextWidth(dc.font.xfont, text, len);
20 }
21
22 /* extern */
23
24 void
25 drawtext(const char *text, unsigned long col[ColLast]) {
26         int x, y, w, h;
27         static char buf[256];
28         unsigned int len, olen;
29         XGCValues gcv;
30         XRectangle r = { dc.x, dc.y, dc.w, dc.h };
31
32         XSetForeground(dpy, dc.gc, col[ColBG]);
33         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
34         if(!text)
35                 return;
36         w = 0;
37         olen = len = strlen(text);
38         if(len >= sizeof(buf))
39                 len = sizeof(buf) - 1;
40         memcpy(buf, text, len);
41         buf[len] = 0;
42         h = dc.font.ascent + dc.font.descent;
43         y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
44         x = dc.x + (h / 2);
45         /* shorten text if necessary */
46         while(len && (w = textnw(buf, len)) > dc.w - h)
47                 buf[--len] = 0;
48         if(len < olen) {
49                 if(len > 1)
50                         buf[len - 1] = '.';
51                 if(len > 2)
52                         buf[len - 2] = '.';
53                 if(len > 3)
54                         buf[len - 3] = '.';
55         }
56         if(w > dc.w)
57                 return; /* too long */
58         gcv.foreground = col[ColFG];
59         if(dc.font.set) {
60                 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
61                 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
62                                 x, y, buf, len);
63         }
64         else {
65                 gcv.font = dc.font.xfont->fid;
66                 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
67                 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
68         }
69 }
70
71 unsigned long
72 getcolor(const char *colstr) {
73         Colormap cmap = DefaultColormap(dpy, screen);
74         XColor color;
75
76         if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
77                 eprint("error, cannot allocate color '%s'\n", colstr);
78         return color.pixel;
79 }
80
81 void
82 setfont(const char *fontstr) {
83         char **missing, *def;
84         int i, n;
85
86         missing = NULL;
87         setlocale(LC_ALL, "");
88         if(dc.font.set)
89                 XFreeFontSet(dpy, dc.font.set);
90         dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
91         if(missing) {
92                 XFreeStringList(missing);
93                 if(dc.font.set) {
94                         XFreeFontSet(dpy, dc.font.set);
95                         dc.font.set = NULL;
96                 }
97         }
98         if(dc.font.set) {
99                 XFontSetExtents *font_extents;
100                 XFontStruct **xfonts;
101                 char **font_names;
102                 dc.font.ascent = dc.font.descent = 0;
103                 font_extents = XExtentsOfFontSet(dc.font.set);
104                 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
105                 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
106                         if(dc.font.ascent < (*xfonts)->ascent)
107                                 dc.font.ascent = (*xfonts)->ascent;
108                         if(dc.font.descent < (*xfonts)->descent)
109                                 dc.font.descent = (*xfonts)->descent;
110                         xfonts++;
111                 }
112         }
113         else {
114                 if(dc.font.xfont)
115                         XFreeFont(dpy, dc.font.xfont);
116                 dc.font.xfont = NULL;
117                 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
118                 if (!dc.font.xfont)
119                         dc.font.xfont = XLoadQueryFont(dpy, "fixed");
120                 if (!dc.font.xfont)
121                         eprint("error, cannot init 'fixed' font\n");
122                 dc.font.ascent = dc.font.xfont->ascent;
123                 dc.font.descent = dc.font.xfont->descent;
124         }
125         dc.font.height = dc.font.ascent + dc.font.descent;
126 }
127
128 unsigned int
129 textw(const char *text) {
130         return textnw(text, strlen(text)) + dc.font.height;
131 }