aewl

view draw.c @ 66:50450aa24a46

removed a bunch of lines through swap removal
author Anselm R. Garbe <garbeam@wmii.de>
date Fri, 14 Jul 2006 17:43:56 +0200
parents f005d46462e8
children c2ddb9dbbd10
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 "dwm.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 drawtext(const char *text, Bool invert, Bool border)
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, invert ? dc.fg : 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(buf, len)) > dc.w - h)
63 buf[--len] = 0;
65 if(w > dc.w)
66 return; /* too long */
68 gcv.foreground = invert ? dc.bg : dc.fg;
69 gcv.background = invert ? dc.fg : 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 unsigned long
83 initcolor(const char *colstr)
84 {
85 XColor color;
86 Colormap cmap = DefaultColormap(dpy, screen);
88 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
89 return color.pixel;
90 }
92 unsigned int
93 textnw(char *text, unsigned int len)
94 {
95 XRectangle r;
96 if(dc.font.set) {
97 XmbTextExtents(dc.font.set, text, len, NULL, &r);
98 return r.width;
99 }
100 return XTextWidth(dc.font.xfont, text, len);
101 }
103 unsigned int
104 textw(char *text)
105 {
106 return textnw(text, strlen(text));
107 }
109 void
110 initfont(const char *fontstr)
111 {
112 char **missing, *def;
113 int i, n;
115 missing = NULL;
116 setlocale(LC_ALL, "");
117 if(dc.font.set)
118 XFreeFontSet(dpy, dc.font.set);
119 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
120 if(missing) {
121 while(n--)
122 fprintf(stderr, "missing fontset: %s\n", missing[n]);
123 XFreeStringList(missing);
124 if(dc.font.set) {
125 XFreeFontSet(dpy, dc.font.set);
126 dc.font.set = NULL;
127 }
128 }
129 if(dc.font.set) {
130 XFontSetExtents *font_extents;
131 XFontStruct **xfonts;
132 char **font_names;
134 dc.font.ascent = dc.font.descent = 0;
135 font_extents = XExtentsOfFontSet(dc.font.set);
136 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
137 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
138 if(dc.font.ascent < (*xfonts)->ascent)
139 dc.font.ascent = (*xfonts)->ascent;
140 if(dc.font.descent < (*xfonts)->descent)
141 dc.font.descent = (*xfonts)->descent;
142 xfonts++;
143 }
144 }
145 else {
146 if(dc.font.xfont)
147 XFreeFont(dpy, dc.font.xfont);
148 dc.font.xfont = NULL;
149 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
150 if (!dc.font.xfont)
151 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
152 if (!dc.font.xfont)
153 error("error, cannot init 'fixed' font\n");
154 dc.font.ascent = dc.font.xfont->ascent;
155 dc.font.descent = dc.font.xfont->descent;
156 }
157 dc.font.height = dc.font.ascent + dc.font.descent;
158 }