aewl

annotate draw.c @ 76:4bd49f404f10

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