dwm-meillo

annotate draw.c @ 340:ae0affabdc02

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