JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
1046322c7bc003acc3673c843afcb105bad25c54
[dwm.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
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <X11/Xlocale.h>
10
11 #include "wm.h"
12
13 static void
14 drawborder(Brush *b)
15 {
16         XPoint points[5];
17         XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
18         XSetForeground(dpy, b->gc, b->border);
19         points[0].x = b->x;
20         points[0].y = b->y;
21         points[1].x = b->w - 1;
22         points[1].y = 0;
23         points[2].x = 0;
24         points[2].y = b->h - 1;
25         points[3].x = -(b->w - 1);
26         points[3].y = 0;
27         points[4].x = 0;
28         points[4].y = -(b->h - 1);
29         XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
30 }
31
32 void
33 draw(Brush *b, Bool border, const char *text)
34 {
35         int x, y, w, h;
36         unsigned int len;
37         static char buf[256];
38         XGCValues gcv;
39         XRectangle r = { b->x, b->y, b->w, b->h };
40
41         XSetForeground(dpy, b->gc, b->bg);
42         XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
43
44         w = 0;
45         if(border)
46                 drawborder(b);
47
48         if(!text)
49                 return;
50
51         len = strlen(text);
52         if(len >= sizeof(buf))
53                 len = sizeof(buf) - 1;
54         memcpy(buf, text, len);
55         buf[len] = 0;
56
57         h = b->font.ascent + b->font.descent;
58         y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
59         x = b->x + (h / 2);
60
61         /* shorten text if necessary */
62         while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
63                 buf[--len] = 0;
64
65         if(w > b->w)
66                 return; /* too long */
67
68         gcv.foreground = b->fg;
69         gcv.background = b->bg;
70         if(b->font.set) {
71                 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
72                 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
73                                 x, y, buf, len);
74         }
75         else {
76                 gcv.font = b->font.xfont->fid;
77                 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
78                 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
79         }
80 }
81
82 static unsigned long
83 xloadcolors(Colormap cmap, const char *colstr)
84 {
85         XColor color;
86         XAllocNamedColor(dpy, cmap, colstr, &color, &color);
87         return color.pixel;
88 }
89
90 void
91 loadcolors(int scr, Brush *b,
92                 const char *bg, const char *fg, const char *border)
93 {
94         Colormap cmap = DefaultColormap(dpy, scr);
95         b->bg = xloadcolors(cmap, bg);
96         b->fg = xloadcolors(cmap, fg);
97         b->border = xloadcolors(cmap, border);
98 }
99
100 unsigned int
101 textnw(Fnt *font, char *text, unsigned int len)
102 {
103         XRectangle r;
104         if(font->set) {
105                 XmbTextExtents(font->set, text, len, NULL, &r);
106                 return r.width;
107         }
108         return XTextWidth(font->xfont, text, len);
109 }
110
111 unsigned int
112 textw(Fnt *font, char *text)
113 {
114         return textnw(font, text, strlen(text));
115 }
116
117 unsigned int
118 texth(Fnt *font)
119 {
120         return font->height + 4;
121 }
122
123 void
124 loadfont(Fnt *font, const char *fontstr)
125 {
126         char **missing, *def;
127         int i, n;
128
129         missing = NULL;
130         setlocale(LC_ALL, "");
131         if(font->set)
132                 XFreeFontSet(dpy, font->set);
133         font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
134         if(missing) {
135                 while(n--)
136                         fprintf(stderr, "missing fontset: %s\n", missing[n]);
137                 XFreeStringList(missing);
138                 if(font->set) {
139                         XFreeFontSet(dpy, font->set);
140                         font->set = NULL;
141                 }
142         }
143         if(font->set) {
144                 XFontSetExtents *font_extents;
145                 XFontStruct **xfonts;
146                 char **font_names;
147
148                 font->ascent = font->descent = 0;
149                 font_extents = XExtentsOfFontSet(font->set);
150                 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
151                 for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
152                         if(font->ascent < (*xfonts)->ascent)
153                                 font->ascent = (*xfonts)->ascent;
154                         if(font->descent < (*xfonts)->descent)
155                                 font->descent = (*xfonts)->descent;
156                         xfonts++;
157                 }
158         }
159         else {
160                 if(font->xfont)
161                         XFreeFont(dpy, font->xfont);
162                 font->xfont = NULL;
163                 font->xfont = XLoadQueryFont(dpy, fontstr);
164                 if (!font->xfont)
165                         font->xfont = XLoadQueryFont(dpy, "fixed");
166                 if (!font->xfont)
167                         error("error, cannot load 'fixed' font\n");
168                 font->ascent = font->xfont->ascent;
169                 font->descent = font->xfont->descent;
170         }
171         font->height = font->ascent + font->descent;
172 }