aewl

view draw.c @ 30:2e0fb4130bfb

new stuff, fixed several issues
author Anselm R. Garbe <garbeam@wmii.de>
date Wed, 12 Jul 2006 17:50:31 +0200
parents e8f627998d6f
children 082c75b937b5
line source
1 /*
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include <stdio.h>
7 #include <string.h>
9 #include "draw.h"
10 #include "util.h"
12 static void
13 drawborder(Display *dpy, Brush *b)
14 {
15 XPoint points[5];
16 XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
17 XSetForeground(dpy, b->gc, b->border);
18 points[0].x = b->x;
19 points[0].y = b->y;
20 points[1].x = b->w - 1;
21 points[1].y = 0;
22 points[2].x = 0;
23 points[2].y = b->h - 1;
24 points[3].x = -(b->w - 1);
25 points[3].y = 0;
26 points[4].x = 0;
27 points[4].y = -(b->h - 1);
28 XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
29 }
31 void
32 draw(Display *dpy, Brush *b, Bool border, const char *text)
33 {
34 unsigned int x, y, w, h, len;
35 static char buf[256];
36 XGCValues gcv;
37 XRectangle r = { b->x, b->y, b->w, b->h };
39 XSetForeground(dpy, b->gc, b->bg);
40 XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
42 w = 0;
43 if(border)
44 drawborder(dpy, b);
46 if(!text)
47 return;
49 len = strlen(text);
50 if(len >= sizeof(buf))
51 len = sizeof(buf) - 1;
52 memcpy(buf, text, len);
53 buf[len] = 0;
55 h = b->font.ascent + b->font.descent;
56 y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
57 x = b->x + (h / 2);
59 /* shorten text if necessary */
60 while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
61 buf[--len] = 0;
63 if(w > b->w)
64 return; /* too long */
66 gcv.foreground = b->fg;
67 gcv.background = b->bg;
68 if(b->font.set) {
69 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
70 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
71 x, y, buf, len);
72 }
73 else {
74 gcv.font = b->font.xfont->fid;
75 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
76 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
77 }
78 }
80 static unsigned long
81 xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
82 {
83 XColor color;
84 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
85 return color.pixel;
86 }
88 void
89 loadcolors(Display *dpy, int screen, Brush *b,
90 const char *bg, const char *fg, const char *border)
91 {
92 Colormap cmap = DefaultColormap(dpy, screen);
93 b->bg = xloadcolors(dpy, cmap, bg);
94 b->fg = xloadcolors(dpy, cmap, fg);
95 b->border = xloadcolors(dpy, cmap, border);
96 }
98 unsigned int
99 textnw(Fnt *font, char *text, unsigned int len)
100 {
101 XRectangle r;
102 if(font->set) {
103 XmbTextExtents(font->set, text, len, NULL, &r);
104 return r.width;
105 }
106 return XTextWidth(font->xfont, text, len);
107 }
109 unsigned int
110 textw(Fnt *font, char *text)
111 {
112 return textnw(font, text, strlen(text));
113 }
115 unsigned int
116 texth(Fnt *font)
117 {
118 return font->height + 4;
119 }
121 void
122 loadfont(Display *dpy, Fnt *font, const char *fontstr)
123 {
124 char **missing, *def;
125 int n;
127 missing = NULL;
128 def = "?";
129 setlocale(LC_ALL, "");
130 if(font->set)
131 XFreeFontSet(dpy, font->set);
132 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
133 if(missing) {
134 while(n--)
135 fprintf(stderr, "missing fontset: %s\n", missing[n]);
136 XFreeStringList(missing);
137 if(font->set) {
138 XFreeFontSet(dpy, font->set);
139 font->set = NULL;
140 }
141 }
142 if(font->set) {
143 XFontSetExtents *font_extents;
144 XFontStruct **xfonts;
145 char **font_names;
146 unsigned int i;
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 }