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