aewl

annotate draw.c @ 244:2d4f2bacbbc4

applied grammar correction by ILF
author Anselm R.Garbe <arg@10ksloc.org>
date Thu, 10 Aug 2006 15:35:02 +0200
parents 7f8f7f14e9cd
children 32dc3acfffc0
rev   line source
garbeam@2 1 /*
garbeam@2 2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@2 3 * See LICENSE file for license details.
garbeam@2 4 */
garbeam@76 5 #include "dwm.h"
garbeam@2 6 #include <stdio.h>
garbeam@2 7 #include <string.h>
garbeam@32 8 #include <X11/Xlocale.h>
garbeam@32 9
garbeam@84 10 /* static */
garbeam@76 11
arg@237 12 static void
arg@237 13 drawborder(void)
arg@237 14 {
arg@237 15 XPoint points[5];
arg@237 16
arg@237 17 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
arg@237 18 XSetForeground(dpy, dc.gc, dc.border);
arg@237 19 points[0].x = dc.x;
arg@237 20 points[0].y = dc.y;
arg@237 21 points[1].x = dc.w - 1;
arg@237 22 points[1].y = 0;
arg@237 23 points[2].x = 0;
arg@237 24 points[2].y = dc.h - 1;
arg@237 25 points[3].x = -(dc.w - 1);
arg@237 26 points[3].y = 0;
arg@237 27 points[4].x = 0;
arg@237 28 points[4].y = -(dc.h - 1);
arg@237 29 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
arg@237 30 }
arg@237 31
garbeam@77 32 static unsigned int
arg@173 33 textnw(const char *text, unsigned int len)
garbeam@75 34 {
garbeam@77 35 XRectangle r;
arg@123 36
garbeam@77 37 if(dc.font.set) {
garbeam@77 38 XmbTextExtents(dc.font.set, text, len, NULL, &r);
garbeam@77 39 return r.width;
garbeam@77 40 }
garbeam@77 41 return XTextWidth(dc.font.xfont, text, len);
garbeam@75 42 }
garbeam@75 43
garbeam@77 44 static void
arg@218 45 drawtext(const char *text, Bool invert)
garbeam@2 46 {
garbeam@33 47 int x, y, w, h;
arg@123 48 static char buf[256];
garbeam@33 49 unsigned int len;
garbeam@2 50 XGCValues gcv;
garbeam@34 51 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
garbeam@2 52
garbeam@66 53 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
garbeam@34 54 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
arg@237 55 drawborder();
arg@237 56
garbeam@3 57 if(!text)
garbeam@2 58 return;
garbeam@2 59
arg@218 60 w = 0;
garbeam@3 61 len = strlen(text);
garbeam@2 62 if(len >= sizeof(buf))
garbeam@2 63 len = sizeof(buf) - 1;
garbeam@3 64 memcpy(buf, text, len);
garbeam@2 65 buf[len] = 0;
garbeam@2 66
garbeam@34 67 h = dc.font.ascent + dc.font.descent;
garbeam@34 68 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
garbeam@34 69 x = dc.x + (h / 2);
garbeam@2 70
garbeam@2 71 /* shorten text if necessary */
garbeam@43 72 while(len && (w = textnw(buf, len)) > dc.w - h)
garbeam@2 73 buf[--len] = 0;
garbeam@2 74
garbeam@34 75 if(w > dc.w)
garbeam@2 76 return; /* too long */
garbeam@2 77
garbeam@66 78 gcv.foreground = invert ? dc.bg : dc.fg;
garbeam@66 79 gcv.background = invert ? dc.fg : dc.bg;
garbeam@34 80 if(dc.font.set) {
garbeam@34 81 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
garbeam@34 82 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
garbeam@2 83 x, y, buf, len);
garbeam@2 84 }
garbeam@2 85 else {
garbeam@34 86 gcv.font = dc.font.xfont->fid;
garbeam@34 87 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
garbeam@34 88 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
garbeam@2 89 }
garbeam@2 90 }
garbeam@2 91
garbeam@84 92 /* extern */
garbeam@77 93
garbeam@77 94 void
garbeam@77 95 drawall()
garbeam@77 96 {
garbeam@77 97 Client *c;
garbeam@77 98
arg@142 99 for(c = clients; c; c = getnext(c->next))
garbeam@77 100 drawtitle(c);
garbeam@77 101 drawstatus();
garbeam@77 102 }
garbeam@77 103
garbeam@77 104 void
garbeam@77 105 drawstatus()
garbeam@77 106 {
arg@124 107 int i, x;
garbeam@77 108 Bool istile = arrange == dotile;
garbeam@77 109
garbeam@77 110 dc.x = dc.y = 0;
garbeam@77 111 dc.w = bw;
arg@218 112 drawtext(NULL, !istile);
garbeam@77 113
garbeam@77 114 dc.w = 0;
arg@178 115 for(i = 0; i < ntags; i++) {
garbeam@77 116 dc.x += dc.w;
garbeam@77 117 dc.w = textw(tags[i]);
garbeam@77 118 if(istile)
arg@218 119 drawtext(tags[i], (i == tsel));
garbeam@77 120 else
arg@218 121 drawtext(tags[i], (i != tsel));
garbeam@77 122 }
arg@124 123 x = dc.x + dc.w;
garbeam@77 124 dc.w = textw(stext);
garbeam@77 125 dc.x = bx + bw - dc.w;
arg@218 126 drawtext(stext, !istile);
arg@124 127 if(sel && ((dc.w = dc.x - x) >= bh)) {
arg@124 128 dc.x = x;
arg@218 129 drawtext(sel->name, istile);
arg@124 130 }
garbeam@77 131 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
garbeam@79 132 XSync(dpy, False);
garbeam@77 133 }
garbeam@77 134
garbeam@76 135 void
garbeam@76 136 drawtitle(Client *c)
garbeam@76 137 {
garbeam@76 138 int i;
garbeam@76 139 Bool istile = arrange == dotile;
garbeam@76 140
arg@239 141 if(c == sel && issel) {
garbeam@76 142 drawstatus();
garbeam@76 143 XUnmapWindow(dpy, c->title);
garbeam@76 144 XSetWindowBorder(dpy, c->win, dc.fg);
garbeam@76 145 return;
garbeam@76 146 }
garbeam@76 147
garbeam@76 148 XSetWindowBorder(dpy, c->win, dc.bg);
garbeam@76 149 XMapWindow(dpy, c->title);
garbeam@76 150
garbeam@76 151 dc.x = dc.y = 0;
garbeam@76 152
garbeam@76 153 dc.w = 0;
arg@178 154 for(i = 0; i < ntags; i++) {
garbeam@76 155 if(c->tags[i]) {
garbeam@76 156 dc.x += dc.w;
arg@173 157 dc.w = textw(tags[i]);
arg@218 158 drawtext(tags[i], !istile);
garbeam@76 159 }
garbeam@76 160 }
garbeam@76 161 dc.x += dc.w;
garbeam@76 162 dc.w = textw(c->name);
arg@218 163 drawtext(c->name, !istile);
garbeam@79 164 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
garbeam@79 165 XSync(dpy, False);
garbeam@76 166 }
garbeam@76 167
garbeam@43 168 unsigned long
garbeam@74 169 getcolor(const char *colstr)
garbeam@2 170 {
arg@123 171 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@2 172 XColor color;
garbeam@43 173
garbeam@2 174 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
garbeam@2 175 return color.pixel;
garbeam@2 176 }
garbeam@2 177
garbeam@2 178 void
garbeam@74 179 setfont(const char *fontstr)
garbeam@2 180 {
garbeam@2 181 char **missing, *def;
garbeam@33 182 int i, n;
garbeam@2 183
garbeam@7 184 missing = NULL;
garbeam@2 185 setlocale(LC_ALL, "");
garbeam@43 186 if(dc.font.set)
garbeam@43 187 XFreeFontSet(dpy, dc.font.set);
garbeam@43 188 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 189 if(missing) {
garbeam@2 190 while(n--)
garbeam@2 191 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 192 XFreeStringList(missing);
garbeam@43 193 if(dc.font.set) {
garbeam@43 194 XFreeFontSet(dpy, dc.font.set);
garbeam@43 195 dc.font.set = NULL;
garbeam@2 196 }
garbeam@2 197 }
garbeam@43 198 if(dc.font.set) {
garbeam@2 199 XFontSetExtents *font_extents;
garbeam@2 200 XFontStruct **xfonts;
garbeam@2 201 char **font_names;
garbeam@2 202
garbeam@43 203 dc.font.ascent = dc.font.descent = 0;
garbeam@43 204 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 205 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 206 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 207 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 208 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 209 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 210 dc.font.descent = (*xfonts)->descent;
garbeam@2 211 xfonts++;
garbeam@2 212 }
garbeam@2 213 }
garbeam@2 214 else {
garbeam@43 215 if(dc.font.xfont)
garbeam@43 216 XFreeFont(dpy, dc.font.xfont);
garbeam@43 217 dc.font.xfont = NULL;
garbeam@43 218 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
garbeam@43 219 if (!dc.font.xfont)
garbeam@43 220 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
garbeam@43 221 if (!dc.font.xfont)
garbeam@75 222 eprint("error, cannot init 'fixed' font\n");
garbeam@43 223 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 224 dc.font.descent = dc.font.xfont->descent;
garbeam@2 225 }
garbeam@43 226 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 227 }
garbeam@76 228
garbeam@76 229 unsigned int
arg@173 230 textw(const char *text)
garbeam@76 231 {
garbeam@76 232 return textnw(text, strlen(text)) + dc.font.height;
garbeam@76 233 }