aewl

annotate draw.c @ 123:61490330e90a

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