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