dwm-meillo

view draw.c @ 34:cd30cce52b78

added logo+description
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 09:32:22 +0200
parents e90449e03167
children 989178822938
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(void)
15 {
16 XPoint points[5];
17 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
18 XSetForeground(dpy, dc.gc, dc.border);
19 points[0].x = dc.x;
20 points[0].y = dc.y;
21 points[1].x = dc.w - 1;
22 points[1].y = 0;
23 points[2].x = 0;
24 points[2].y = dc.h - 1;
25 points[3].x = -(dc.w - 1);
26 points[3].y = 0;
27 points[4].x = 0;
28 points[4].y = -(dc.h - 1);
29 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
30 }
32 void
33 draw(Bool border, const char *text)
34 {
35 int x, y, w, h;
36 unsigned int len;
37 static char buf[256];
38 XGCValues gcv;
39 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
41 XSetForeground(dpy, dc.gc, dc.bg);
42 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
44 w = 0;
45 if(border)
46 drawborder();
48 if(!text)
49 return;
51 len = strlen(text);
52 if(len >= sizeof(buf))
53 len = sizeof(buf) - 1;
54 memcpy(buf, text, len);
55 buf[len] = 0;
57 h = dc.font.ascent + dc.font.descent;
58 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
59 x = dc.x + (h / 2);
61 /* shorten text if necessary */
62 while(len && (w = textnw(&dc.font, buf, len)) > dc.w - h)
63 buf[--len] = 0;
65 if(w > dc.w)
66 return; /* too long */
68 gcv.foreground = dc.fg;
69 gcv.background = dc.bg;
70 if(dc.font.set) {
71 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
72 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
73 x, y, buf, len);
74 }
75 else {
76 gcv.font = dc.font.xfont->fid;
77 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
78 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
79 }
80 }
82 static unsigned long
83 xinitcolors(Colormap cmap, const char *colstr)
84 {
85 XColor color;
86 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
87 return color.pixel;
88 }
90 void
91 initcolors(const char *bg, const char *fg, const char *border)
92 {
93 Colormap cmap = DefaultColormap(dpy, screen);
94 dc.bg = xinitcolors(cmap, bg);
95 dc.fg = xinitcolors(cmap, fg);
96 dc.border = xinitcolors(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 initfont(Fnt *font, const char *fontstr)
124 {
125 char **missing, *def;
126 int i, n;
128 missing = NULL;
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;
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 init 'fixed' font\n");
167 font->ascent = font->xfont->ascent;
168 font->descent = font->xfont->descent;
169 }
170 font->height = font->ascent + font->descent;
171 }