dwm-meillo

view draw.c @ 26:e8f627998d6f

simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
author Anselm R. Garbe <garbeam@wmii.de>
date Wed, 12 Jul 2006 15:17:22 +0200
parents 49e2fc9fb94f
children 2e0fb4130bfb
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 if(border)
43 drawborder(dpy, b);
45 if(!text)
46 return;
48 len = strlen(text);
49 if(len >= sizeof(buf))
50 len = sizeof(buf) - 1;
51 memcpy(buf, text, len);
52 buf[len] = 0;
54 h = b->font.ascent + b->font.descent;
55 y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
56 x = b->x + (h / 2);
58 /* shorten text if necessary */
59 while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
60 buf[--len] = 0;
62 if(w > b->w)
63 return; /* too long */
65 gcv.foreground = b->fg;
66 gcv.background = b->bg;
67 if(b->font.set) {
68 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
69 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
70 x, y, buf, len);
71 }
72 else {
73 gcv.font = b->font.xfont->fid;
74 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
75 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
76 }
77 }
79 static unsigned long
80 xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
81 {
82 XColor color;
83 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
84 return color.pixel;
85 }
87 void
88 loadcolors(Display *dpy, int screen, Brush *b,
89 const char *bg, const char *fg, const char *border)
90 {
91 Colormap cmap = DefaultColormap(dpy, screen);
92 b->bg = xloadcolors(dpy, cmap, bg);
93 b->fg = xloadcolors(dpy, cmap, fg);
94 b->border = xloadcolors(dpy, cmap, border);
95 }
97 unsigned int
98 textnw(Fnt *font, char *text, unsigned int len)
99 {
100 XRectangle r;
101 if(font->set) {
102 XmbTextExtents(font->set, text, len, NULL, &r);
103 return r.width;
104 }
105 return XTextWidth(font->xfont, text, len);
106 }
108 unsigned int
109 textw(Fnt *font, char *text)
110 {
111 return textnw(font, text, strlen(text));
112 }
114 unsigned int
115 texth(Fnt *font)
116 {
117 return font->height + 4;
118 }
120 void
121 loadfont(Display *dpy, Fnt *font, const char *fontstr)
122 {
123 char **missing, *def;
124 int n;
126 missing = NULL;
127 def = "?";
128 setlocale(LC_ALL, "");
129 if(font->set)
130 XFreeFontSet(dpy, font->set);
131 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
132 if(missing) {
133 while(n--)
134 fprintf(stderr, "missing fontset: %s\n", missing[n]);
135 XFreeStringList(missing);
136 if(font->set) {
137 XFreeFontSet(dpy, font->set);
138 font->set = NULL;
139 }
140 }
141 if(font->set) {
142 XFontSetExtents *font_extents;
143 XFontStruct **xfonts;
144 char **font_names;
145 unsigned int i;
147 font->ascent = font->descent = 0;
148 font_extents = XExtentsOfFontSet(font->set);
149 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
150 for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
151 if(font->ascent < (*xfonts)->ascent)
152 font->ascent = (*xfonts)->ascent;
153 if(font->descent < (*xfonts)->descent)
154 font->descent = (*xfonts)->descent;
155 xfonts++;
156 }
157 }
158 else {
159 if(font->xfont)
160 XFreeFont(dpy, font->xfont);
161 font->xfont = NULL;
162 font->xfont = XLoadQueryFont(dpy, fontstr);
163 if (!font->xfont)
164 font->xfont = XLoadQueryFont(dpy, "fixed");
165 if (!font->xfont)
166 error("error, cannot load 'fixed' font\n");
167 font->ascent = font->xfont->ascent;
168 font->descent = font->xfont->descent;
169 }
170 font->height = font->ascent + font->descent;
171 }