dwm-meillo

view draw.c @ 32:082c75b937b5

removed unnecessary crap
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 01:30:55 +0200
parents 2e0fb4130bfb
children e90449e03167
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 <X11/Xlocale.h>
11 #include "wm.h"
13 static void
14 drawborder(Display *dpy, 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 }
32 void
33 draw(Display *dpy, Brush *b, Bool border, const char *text)
34 {
35 unsigned int x, y, w, h, len;
36 static char buf[256];
37 XGCValues gcv;
38 XRectangle r = { b->x, b->y, b->w, b->h };
40 XSetForeground(dpy, b->gc, b->bg);
41 XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
43 w = 0;
44 if(border)
45 drawborder(dpy, b);
47 if(!text)
48 return;
50 len = strlen(text);
51 if(len >= sizeof(buf))
52 len = sizeof(buf) - 1;
53 memcpy(buf, text, len);
54 buf[len] = 0;
56 h = b->font.ascent + b->font.descent;
57 y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
58 x = b->x + (h / 2);
60 /* shorten text if necessary */
61 while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
62 buf[--len] = 0;
64 if(w > b->w)
65 return; /* too long */
67 gcv.foreground = b->fg;
68 gcv.background = b->bg;
69 if(b->font.set) {
70 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
71 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
72 x, y, buf, len);
73 }
74 else {
75 gcv.font = b->font.xfont->fid;
76 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
77 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
78 }
79 }
81 static unsigned long
82 xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
83 {
84 XColor color;
85 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
86 return color.pixel;
87 }
89 void
90 loadcolors(Display *dpy, int screen, Brush *b,
91 const char *bg, const char *fg, const char *border)
92 {
93 Colormap cmap = DefaultColormap(dpy, screen);
94 b->bg = xloadcolors(dpy, cmap, bg);
95 b->fg = xloadcolors(dpy, cmap, fg);
96 b->border = xloadcolors(dpy, cmap, border);
97 }
99 unsigned int
100 textnw(Fnt *font, char *text, unsigned int len)
101 {
102 XRectangle r;
103 if(font->set) {
104 XmbTextExtents(font->set, text, len, NULL, &r);
105 return r.width;
106 }
107 return XTextWidth(font->xfont, text, len);
108 }
110 unsigned int
111 textw(Fnt *font, char *text)
112 {
113 return textnw(font, text, strlen(text));
114 }
116 unsigned int
117 texth(Fnt *font)
118 {
119 return font->height + 4;
120 }
122 void
123 loadfont(Display *dpy, Fnt *font, const char *fontstr)
124 {
125 char **missing, *def;
126 int n;
128 missing = NULL;
129 def = "?";
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 unsigned int i;
149 font->ascent = font->descent = 0;
150 font_extents = XExtentsOfFontSet(font->set);
151 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
152 for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
153 if(font->ascent < (*xfonts)->ascent)
154 font->ascent = (*xfonts)->ascent;
155 if(font->descent < (*xfonts)->descent)
156 font->descent = (*xfonts)->descent;
157 xfonts++;
158 }
159 }
160 else {
161 if(font->xfont)
162 XFreeFont(dpy, font->xfont);
163 font->xfont = NULL;
164 font->xfont = XLoadQueryFont(dpy, fontstr);
165 if (!font->xfont)
166 font->xfont = XLoadQueryFont(dpy, "fixed");
167 if (!font->xfont)
168 error("error, cannot load 'fixed' font\n");
169 font->ascent = font->xfont->ascent;
170 font->descent = font->xfont->descent;
171 }
172 font->height = font->ascent + font->descent;
173 }