dwm-meillo

annotate draw.c @ 101:17c526ee321c

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