dwm-meillo

annotate draw.c @ 536:d5aa5a4be560

using lsx instead of Jukka's shell construct
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 13 Oct 2006 18:47:24 +0200
parents 451f19d48845
children f05cfa7d5128
rev   line source
arg@532 1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@2 2 * See LICENSE file for license details.
garbeam@2 3 */
garbeam@76 4 #include "dwm.h"
garbeam@2 5 #include <stdio.h>
garbeam@2 6 #include <string.h>
garbeam@32 7 #include <X11/Xlocale.h>
garbeam@32 8
garbeam@84 9 /* static */
garbeam@76 10
garbeam@77 11 static unsigned int
arg@461 12 textnw(const char *text, unsigned int len) {
garbeam@77 13 XRectangle r;
arg@123 14
garbeam@77 15 if(dc.font.set) {
garbeam@77 16 XmbTextExtents(dc.font.set, text, len, NULL, &r);
garbeam@77 17 return r.width;
garbeam@77 18 }
garbeam@77 19 return XTextWidth(dc.font.xfont, text, len);
garbeam@75 20 }
garbeam@75 21
garbeam@77 22 static void
arg@461 23 drawtext(const char *text, unsigned long col[ColLast], Bool highlight) {
arg@344 24 int x, y, w, h;
arg@344 25 static char buf[256];
arg@344 26 unsigned int len, olen;
arg@352 27 XGCValues gcv;
arg@344 28 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
arg@344 29
arg@353 30 XSetForeground(dpy, dc.gc, col[ColBG]);
arg@344 31 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@3 32 if(!text)
garbeam@2 33 return;
arg@218 34 w = 0;
arg@269 35 olen = len = strlen(text);
garbeam@2 36 if(len >= sizeof(buf))
garbeam@2 37 len = sizeof(buf) - 1;
garbeam@3 38 memcpy(buf, text, len);
garbeam@2 39 buf[len] = 0;
garbeam@34 40 h = dc.font.ascent + dc.font.descent;
garbeam@34 41 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
garbeam@34 42 x = dc.x + (h / 2);
garbeam@2 43 /* shorten text if necessary */
garbeam@43 44 while(len && (w = textnw(buf, len)) > dc.w - h)
garbeam@2 45 buf[--len] = 0;
arg@269 46 if(len < olen) {
arg@273 47 if(len > 1)
arg@273 48 buf[len - 1] = '.';
arg@273 49 if(len > 2)
arg@273 50 buf[len - 2] = '.';
arg@269 51 if(len > 3)
arg@273 52 buf[len - 3] = '.';
arg@269 53 }
garbeam@34 54 if(w > dc.w)
garbeam@2 55 return; /* too long */
arg@353 56 gcv.foreground = col[ColFG];
arg@352 57 if(dc.font.set) {
arg@353 58 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
arg@342 59 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
arg@352 60 }
garbeam@2 61 else {
arg@352 62 gcv.font = dc.font.xfont->fid;
arg@353 63 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
arg@342 64 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
arg@342 65 }
arg@345 66 if(highlight) {
arg@352 67 r.x = dc.x + 2;
arg@352 68 r.y = dc.y + 2;
arg@519 69 r.width = r.height = (h + 2) / 4;
arg@352 70 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@2 71 }
garbeam@2 72 }
garbeam@2 73
garbeam@84 74 /* extern */
garbeam@77 75
garbeam@77 76 void
arg@487 77 drawall(void) {
garbeam@77 78 Client *c;
garbeam@77 79
arg@142 80 for(c = clients; c; c = getnext(c->next))
garbeam@77 81 drawtitle(c);
garbeam@77 82 drawstatus();
garbeam@77 83 }
garbeam@77 84
garbeam@77 85 void
arg@487 86 drawstatus(void) {
arg@124 87 int i, x;
garbeam@77 88
garbeam@77 89 dc.x = dc.y = 0;
arg@178 90 for(i = 0; i < ntags; i++) {
garbeam@77 91 dc.w = textw(tags[i]);
arg@353 92 if(seltag[i])
arg@353 93 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
garbeam@77 94 else
arg@353 95 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
arg@362 96 dc.x += dc.w;
garbeam@77 97 }
arg@362 98 dc.w = bmw;
arg@530 99 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False);
arg@361 100 x = dc.x + dc.w;
garbeam@77 101 dc.w = textw(stext);
garbeam@77 102 dc.x = bx + bw - dc.w;
arg@342 103 if(dc.x < x) {
arg@342 104 dc.x = x;
arg@342 105 dc.w = bw - x;
arg@342 106 }
arg@353 107 drawtext(stext, dc.status, False);
arg@371 108 if((dc.w = dc.x - x) > bh) {
arg@342 109 dc.x = x;
arg@371 110 if(sel)
arg@371 111 drawtext(sel->name, dc.sel, False);
arg@371 112 else
arg@371 113 drawtext(NULL, dc.norm, False);
arg@124 114 }
garbeam@77 115 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
garbeam@79 116 XSync(dpy, False);
garbeam@77 117 }
garbeam@77 118
garbeam@76 119 void
arg@461 120 drawtitle(Client *c) {
arg@239 121 if(c == sel && issel) {
garbeam@76 122 drawstatus();
arg@342 123 XUnmapWindow(dpy, c->twin);
arg@353 124 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
garbeam@76 125 return;
garbeam@76 126 }
arg@353 127 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
arg@342 128 XMapWindow(dpy, c->twin);
arg@342 129 dc.x = dc.y = 0;
arg@342 130 dc.w = c->tw;
arg@353 131 drawtext(c->name, dc.norm, False);
arg@342 132 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
garbeam@79 133 XSync(dpy, False);
garbeam@76 134 }
garbeam@76 135
garbeam@43 136 unsigned long
arg@461 137 getcolor(const char *colstr) {
arg@123 138 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@2 139 XColor color;
garbeam@43 140
arg@495 141 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
arg@495 142 eprint("error, cannot allocate color '%s'\n", colstr);
garbeam@2 143 return color.pixel;
garbeam@2 144 }
garbeam@2 145
garbeam@2 146 void
arg@461 147 setfont(const char *fontstr) {
garbeam@2 148 char **missing, *def;
garbeam@33 149 int i, n;
garbeam@2 150
garbeam@7 151 missing = NULL;
garbeam@2 152 setlocale(LC_ALL, "");
garbeam@43 153 if(dc.font.set)
garbeam@43 154 XFreeFontSet(dpy, dc.font.set);
garbeam@43 155 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 156 if(missing) {
garbeam@2 157 while(n--)
garbeam@2 158 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 159 XFreeStringList(missing);
garbeam@43 160 if(dc.font.set) {
garbeam@43 161 XFreeFontSet(dpy, dc.font.set);
garbeam@43 162 dc.font.set = NULL;
garbeam@2 163 }
garbeam@2 164 }
garbeam@43 165 if(dc.font.set) {
garbeam@2 166 XFontSetExtents *font_extents;
garbeam@2 167 XFontStruct **xfonts;
garbeam@2 168 char **font_names;
garbeam@43 169 dc.font.ascent = dc.font.descent = 0;
garbeam@43 170 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 171 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 172 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 173 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 174 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 175 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 176 dc.font.descent = (*xfonts)->descent;
garbeam@2 177 xfonts++;
garbeam@2 178 }
garbeam@2 179 }
garbeam@2 180 else {
garbeam@43 181 if(dc.font.xfont)
garbeam@43 182 XFreeFont(dpy, dc.font.xfont);
garbeam@43 183 dc.font.xfont = NULL;
garbeam@43 184 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
garbeam@43 185 if (!dc.font.xfont)
garbeam@43 186 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
garbeam@43 187 if (!dc.font.xfont)
garbeam@75 188 eprint("error, cannot init 'fixed' font\n");
garbeam@43 189 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 190 dc.font.descent = dc.font.xfont->descent;
garbeam@2 191 }
garbeam@43 192 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 193 }
garbeam@76 194
garbeam@76 195 unsigned int
arg@461 196 textw(const char *text) {
garbeam@76 197 return textnw(text, strlen(text)) + dc.font.height;
garbeam@76 198 }