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