aewl

annotate draw.c @ 343:336d3a2c3f80

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