aewl

annotate draw.c @ 607:16762f7414de

if client is focused, the emptysquare don't needs to be drawed
author arg@mig29
date Fri, 01 Dec 2006 17:51:05 +0100
parents 44b5617c78b3
children ecfc43176b2d
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
arg@572 11 static Bool
arg@572 12 isoccupied(unsigned int t)
arg@572 13 {
arg@572 14 Client *c;
arg@572 15 for(c = clients; c; c = c->next)
arg@572 16 if(c->tags[t])
arg@572 17 return True;
arg@572 18 return False;
arg@572 19 }
arg@572 20
garbeam@77 21 static unsigned int
arg@461 22 textnw(const char *text, unsigned int len) {
garbeam@77 23 XRectangle r;
arg@123 24
garbeam@77 25 if(dc.font.set) {
garbeam@77 26 XmbTextExtents(dc.font.set, text, len, NULL, &r);
garbeam@77 27 return r.width;
garbeam@77 28 }
garbeam@77 29 return XTextWidth(dc.font.xfont, text, len);
garbeam@75 30 }
garbeam@75 31
garbeam@77 32 static void
arg@607 33 drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
arg@344 34 int x, y, w, h;
arg@344 35 static char buf[256];
arg@344 36 unsigned int len, olen;
arg@352 37 XGCValues gcv;
arg@344 38 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
arg@606 39 XPoint pt[5];
arg@344 40
arg@353 41 XSetForeground(dpy, dc.gc, col[ColBG]);
arg@344 42 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@3 43 if(!text)
garbeam@2 44 return;
arg@218 45 w = 0;
arg@269 46 olen = len = strlen(text);
arg@581 47 if(len >= sizeof buf)
arg@581 48 len = sizeof buf - 1;
garbeam@3 49 memcpy(buf, text, len);
garbeam@2 50 buf[len] = 0;
garbeam@34 51 h = dc.font.ascent + dc.font.descent;
garbeam@34 52 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
garbeam@34 53 x = dc.x + (h / 2);
garbeam@2 54 /* shorten text if necessary */
garbeam@43 55 while(len && (w = textnw(buf, len)) > dc.w - h)
garbeam@2 56 buf[--len] = 0;
arg@269 57 if(len < olen) {
arg@273 58 if(len > 1)
arg@273 59 buf[len - 1] = '.';
arg@273 60 if(len > 2)
arg@273 61 buf[len - 2] = '.';
arg@269 62 if(len > 3)
arg@273 63 buf[len - 3] = '.';
arg@269 64 }
garbeam@34 65 if(w > dc.w)
garbeam@2 66 return; /* too long */
arg@353 67 gcv.foreground = col[ColFG];
arg@352 68 if(dc.font.set) {
arg@353 69 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
arg@342 70 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
arg@352 71 }
garbeam@2 72 else {
arg@352 73 gcv.font = dc.font.xfont->fid;
arg@353 74 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
arg@342 75 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
arg@342 76 }
arg@606 77 x = (h + 2) / 4;
arg@607 78 if(filledsquare) {
arg@605 79 r.x = dc.x + 2;
arg@605 80 r.y = dc.y + 2;
arg@606 81 r.width = r.height = x;
arg@605 82 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@2 83 }
arg@607 84 else if(emptysquare) {
arg@606 85 pt[0].x = dc.x + 2;
arg@606 86 pt[0].y = dc.y + 2;
arg@606 87 pt[1].x = x;
arg@606 88 pt[1].y = 0;
arg@606 89 pt[2].x = 0;
arg@606 90 pt[2].y = x;
arg@606 91 pt[3].x = -x;
arg@606 92 pt[3].y = 0;
arg@606 93 pt[4].x = 0;
arg@606 94 pt[4].y = -x;
arg@606 95 XDrawLines(dpy, dc.drawable, dc.gc, pt, 5, CoordModePrevious);
arg@604 96 }
arg@604 97 }
garbeam@2 98
garbeam@84 99 /* extern */
garbeam@77 100
garbeam@77 101 void
arg@487 102 drawall(void) {
garbeam@77 103 Client *c;
garbeam@77 104
arg@142 105 for(c = clients; c; c = getnext(c->next))
garbeam@77 106 drawtitle(c);
garbeam@77 107 drawstatus();
garbeam@77 108 }
garbeam@77 109
garbeam@77 110 void
arg@487 111 drawstatus(void) {
arg@124 112 int i, x;
garbeam@77 113
garbeam@77 114 dc.x = dc.y = 0;
arg@178 115 for(i = 0; i < ntags; i++) {
garbeam@77 116 dc.w = textw(tags[i]);
arg@353 117 if(seltag[i])
arg@572 118 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
garbeam@77 119 else
arg@572 120 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
arg@362 121 dc.x += dc.w;
garbeam@77 122 }
arg@362 123 dc.w = bmw;
arg@572 124 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
arg@361 125 x = dc.x + dc.w;
garbeam@77 126 dc.w = textw(stext);
arg@570 127 dc.x = bw - dc.w;
arg@342 128 if(dc.x < x) {
arg@342 129 dc.x = x;
arg@342 130 dc.w = bw - x;
arg@342 131 }
arg@572 132 drawtext(stext, dc.status, False, False);
arg@371 133 if((dc.w = dc.x - x) > bh) {
arg@342 134 dc.x = x;
arg@572 135 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
arg@124 136 }
garbeam@77 137 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
garbeam@79 138 XSync(dpy, False);
garbeam@77 139 }
garbeam@77 140
garbeam@76 141 void
arg@461 142 drawtitle(Client *c) {
arg@239 143 if(c == sel && issel) {
garbeam@76 144 drawstatus();
arg@342 145 XUnmapWindow(dpy, c->twin);
arg@353 146 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
garbeam@76 147 return;
garbeam@76 148 }
arg@353 149 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
arg@342 150 XMapWindow(dpy, c->twin);
arg@342 151 dc.x = dc.y = 0;
arg@342 152 dc.w = c->tw;
arg@572 153 drawtext(c->name, dc.norm, False,False);
arg@342 154 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
garbeam@79 155 XSync(dpy, False);
garbeam@76 156 }
garbeam@76 157
garbeam@43 158 unsigned long
arg@461 159 getcolor(const char *colstr) {
arg@123 160 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@2 161 XColor color;
garbeam@43 162
arg@495 163 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
arg@495 164 eprint("error, cannot allocate color '%s'\n", colstr);
garbeam@2 165 return color.pixel;
garbeam@2 166 }
garbeam@2 167
garbeam@2 168 void
arg@461 169 setfont(const char *fontstr) {
garbeam@2 170 char **missing, *def;
garbeam@33 171 int i, n;
garbeam@2 172
garbeam@7 173 missing = NULL;
garbeam@2 174 setlocale(LC_ALL, "");
garbeam@43 175 if(dc.font.set)
garbeam@43 176 XFreeFontSet(dpy, dc.font.set);
garbeam@43 177 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 178 if(missing) {
garbeam@2 179 while(n--)
garbeam@2 180 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 181 XFreeStringList(missing);
garbeam@43 182 if(dc.font.set) {
garbeam@43 183 XFreeFontSet(dpy, dc.font.set);
garbeam@43 184 dc.font.set = NULL;
garbeam@2 185 }
garbeam@2 186 }
garbeam@43 187 if(dc.font.set) {
garbeam@2 188 XFontSetExtents *font_extents;
garbeam@2 189 XFontStruct **xfonts;
garbeam@2 190 char **font_names;
garbeam@43 191 dc.font.ascent = dc.font.descent = 0;
garbeam@43 192 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 193 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 194 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 195 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 196 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 197 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 198 dc.font.descent = (*xfonts)->descent;
garbeam@2 199 xfonts++;
garbeam@2 200 }
garbeam@2 201 }
garbeam@2 202 else {
garbeam@43 203 if(dc.font.xfont)
garbeam@43 204 XFreeFont(dpy, dc.font.xfont);
garbeam@43 205 dc.font.xfont = NULL;
garbeam@43 206 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
garbeam@43 207 if (!dc.font.xfont)
garbeam@43 208 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
garbeam@43 209 if (!dc.font.xfont)
garbeam@75 210 eprint("error, cannot init 'fixed' font\n");
garbeam@43 211 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 212 dc.font.descent = dc.font.xfont->descent;
garbeam@2 213 }
garbeam@43 214 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 215 }
garbeam@76 216
garbeam@76 217 unsigned int
arg@461 218 textw(const char *text) {
garbeam@76 219 return textnw(text, strlen(text)) + dc.font.height;
garbeam@76 220 }