dwm-meillo

annotate draw.c @ 154:a011f447af88

simplified README
author arg@10ksloc.org
date Tue, 01 Aug 2006 16:29:25 +0200
parents 75576e44c1d8
children 21071ae1fe68
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
arg@142 102 for(c = clients; c; c = getnext(c->next))
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 {
arg@124 110 int i, x;
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 }
arg@124 126 x = dc.x + dc.w;
garbeam@77 127 dc.w = textw(stext);
garbeam@77 128 dc.x = bx + bw - dc.w;
garbeam@77 129 drawtext(stext, !istile, False);
arg@124 130 if(sel && ((dc.w = dc.x - x) >= bh)) {
arg@124 131 dc.x = x;
arg@124 132 drawtext(sel->name, istile, True);
arg@124 133 }
garbeam@77 134 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
garbeam@79 135 XSync(dpy, False);
garbeam@77 136 }
garbeam@77 137
garbeam@76 138 void
garbeam@76 139 drawtitle(Client *c)
garbeam@76 140 {
garbeam@76 141 int i;
garbeam@76 142 Bool istile = arrange == dotile;
garbeam@76 143
garbeam@76 144 if(c == sel) {
garbeam@76 145 drawstatus();
garbeam@76 146 XUnmapWindow(dpy, c->title);
garbeam@76 147 XSetWindowBorder(dpy, c->win, dc.fg);
garbeam@76 148 return;
garbeam@76 149 }
garbeam@76 150
garbeam@76 151 XSetWindowBorder(dpy, c->win, dc.bg);
garbeam@76 152 XMapWindow(dpy, c->title);
garbeam@76 153
garbeam@76 154 dc.x = dc.y = 0;
garbeam@76 155
garbeam@76 156 dc.w = 0;
garbeam@76 157 for(i = 0; i < TLast; i++) {
garbeam@76 158 if(c->tags[i]) {
garbeam@76 159 dc.x += dc.w;
garbeam@76 160 dc.w = textw(c->tags[i]);
garbeam@76 161 drawtext(c->tags[i], !istile, True);
garbeam@76 162 }
garbeam@76 163 }
garbeam@76 164 dc.x += dc.w;
garbeam@76 165 dc.w = textw(c->name);
garbeam@76 166 drawtext(c->name, !istile, True);
garbeam@79 167 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
garbeam@79 168 XSync(dpy, False);
garbeam@76 169 }
garbeam@76 170
garbeam@43 171 unsigned long
garbeam@74 172 getcolor(const char *colstr)
garbeam@2 173 {
arg@123 174 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@2 175 XColor color;
garbeam@43 176
garbeam@2 177 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
garbeam@2 178 return color.pixel;
garbeam@2 179 }
garbeam@2 180
garbeam@2 181 void
garbeam@74 182 setfont(const char *fontstr)
garbeam@2 183 {
garbeam@2 184 char **missing, *def;
garbeam@33 185 int i, n;
garbeam@2 186
garbeam@7 187 missing = NULL;
garbeam@2 188 setlocale(LC_ALL, "");
garbeam@43 189 if(dc.font.set)
garbeam@43 190 XFreeFontSet(dpy, dc.font.set);
garbeam@43 191 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 192 if(missing) {
garbeam@2 193 while(n--)
garbeam@2 194 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 195 XFreeStringList(missing);
garbeam@43 196 if(dc.font.set) {
garbeam@43 197 XFreeFontSet(dpy, dc.font.set);
garbeam@43 198 dc.font.set = NULL;
garbeam@2 199 }
garbeam@2 200 }
garbeam@43 201 if(dc.font.set) {
garbeam@2 202 XFontSetExtents *font_extents;
garbeam@2 203 XFontStruct **xfonts;
garbeam@2 204 char **font_names;
garbeam@2 205
garbeam@43 206 dc.font.ascent = dc.font.descent = 0;
garbeam@43 207 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 208 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 209 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 210 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 211 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 212 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 213 dc.font.descent = (*xfonts)->descent;
garbeam@2 214 xfonts++;
garbeam@2 215 }
garbeam@2 216 }
garbeam@2 217 else {
garbeam@43 218 if(dc.font.xfont)
garbeam@43 219 XFreeFont(dpy, dc.font.xfont);
garbeam@43 220 dc.font.xfont = NULL;
garbeam@43 221 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
garbeam@43 222 if (!dc.font.xfont)
garbeam@43 223 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
garbeam@43 224 if (!dc.font.xfont)
garbeam@75 225 eprint("error, cannot init 'fixed' font\n");
garbeam@43 226 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 227 dc.font.descent = dc.font.xfont->descent;
garbeam@2 228 }
garbeam@43 229 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 230 }
garbeam@76 231
garbeam@76 232 unsigned int
garbeam@76 233 textw(char *text)
garbeam@76 234 {
garbeam@76 235 return textnw(text, strlen(text)) + dc.font.height;
garbeam@76 236 }