aewl

annotate draw.c @ 350:b10852dbbffe

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