dwm-meillo

annotate draw.c @ 731:29c9b557ed95

implemented aspect ratio support of windows
author Anselm R. Garbe <arg@suckless.org>
date Tue, 06 Feb 2007 15:22:13 +0100
parents 4d12382fef8e
children 61821891835c
rev   line source
arg@643 1 /* (C)opyright MMIV-MMVII 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
garbeam@84 8 /* static */
garbeam@76 9
arg@572 10 static Bool
arg@572 11 isoccupied(unsigned int t)
arg@572 12 {
arg@572 13 Client *c;
arg@572 14 for(c = clients; c; c = c->next)
arg@572 15 if(c->tags[t])
arg@572 16 return True;
arg@572 17 return False;
arg@572 18 }
arg@572 19
garbeam@77 20 static unsigned int
arg@461 21 textnw(const char *text, unsigned int len) {
garbeam@77 22 XRectangle r;
arg@123 23
garbeam@77 24 if(dc.font.set) {
garbeam@77 25 XmbTextExtents(dc.font.set, text, len, NULL, &r);
garbeam@77 26 return r.width;
garbeam@77 27 }
garbeam@77 28 return XTextWidth(dc.font.xfont, text, len);
garbeam@75 29 }
garbeam@75 30
garbeam@77 31 static void
arg@607 32 drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
arg@344 33 int x, y, w, h;
arg@344 34 static char buf[256];
arg@344 35 unsigned int len, olen;
arg@352 36 XGCValues gcv;
arg@344 37 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
arg@344 38
arg@353 39 XSetForeground(dpy, dc.gc, col[ColBG]);
arg@344 40 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@3 41 if(!text)
garbeam@2 42 return;
arg@218 43 w = 0;
arg@269 44 olen = len = strlen(text);
arg@581 45 if(len >= sizeof buf)
arg@581 46 len = sizeof buf - 1;
garbeam@3 47 memcpy(buf, text, len);
garbeam@2 48 buf[len] = 0;
garbeam@34 49 h = dc.font.ascent + dc.font.descent;
garbeam@34 50 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
garbeam@34 51 x = dc.x + (h / 2);
garbeam@2 52 /* shorten text if necessary */
garbeam@43 53 while(len && (w = textnw(buf, len)) > dc.w - h)
garbeam@2 54 buf[--len] = 0;
arg@269 55 if(len < olen) {
arg@273 56 if(len > 1)
arg@273 57 buf[len - 1] = '.';
arg@273 58 if(len > 2)
arg@273 59 buf[len - 2] = '.';
arg@269 60 if(len > 3)
arg@273 61 buf[len - 3] = '.';
arg@269 62 }
garbeam@34 63 if(w > dc.w)
garbeam@2 64 return; /* too long */
arg@353 65 gcv.foreground = col[ColFG];
arg@352 66 if(dc.font.set) {
arg@353 67 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
arg@342 68 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
arg@352 69 }
garbeam@2 70 else {
arg@352 71 gcv.font = dc.font.xfont->fid;
arg@353 72 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
arg@342 73 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
arg@342 74 }
arg@606 75 x = (h + 2) / 4;
arg@728 76 r.x = dc.x + 1;
arg@728 77 r.y = dc.y + 1;
arg@607 78 if(filledsquare) {
arg@608 79 r.width = r.height = x + 1;
arg@605 80 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@2 81 }
arg@607 82 else if(emptysquare) {
arg@728 83 r.width = r.height = x;
arg@728 84 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
arg@604 85 }
arg@604 86 }
garbeam@2 87
garbeam@84 88 /* extern */
garbeam@77 89
garbeam@77 90 void
arg@487 91 drawstatus(void) {
arg@124 92 int i, x;
garbeam@77 93
garbeam@77 94 dc.x = dc.y = 0;
arg@178 95 for(i = 0; i < ntags; i++) {
garbeam@77 96 dc.w = textw(tags[i]);
arg@353 97 if(seltag[i])
arg@572 98 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
garbeam@77 99 else
arg@572 100 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
arg@362 101 dc.x += dc.w;
garbeam@77 102 }
arg@362 103 dc.w = bmw;
arg@689 104 drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False);
arg@361 105 x = dc.x + dc.w;
garbeam@77 106 dc.w = textw(stext);
arg@697 107 dc.x = sw - dc.w;
arg@342 108 if(dc.x < x) {
arg@342 109 dc.x = x;
arg@697 110 dc.w = sw - x;
arg@342 111 }
arg@689 112 drawtext(stext, dc.norm, False, False);
arg@371 113 if((dc.w = dc.x - x) > bh) {
arg@342 114 dc.x = x;
arg@572 115 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
arg@124 116 }
arg@697 117 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
garbeam@79 118 XSync(dpy, False);
garbeam@77 119 }
garbeam@77 120
garbeam@43 121 unsigned long
arg@461 122 getcolor(const char *colstr) {
arg@123 123 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@2 124 XColor color;
garbeam@43 125
arg@495 126 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
arg@495 127 eprint("error, cannot allocate color '%s'\n", colstr);
garbeam@2 128 return color.pixel;
garbeam@2 129 }
garbeam@2 130
garbeam@2 131 void
arg@461 132 setfont(const char *fontstr) {
arg@619 133 char *def, **missing;
garbeam@33 134 int i, n;
garbeam@2 135
garbeam@7 136 missing = NULL;
garbeam@43 137 if(dc.font.set)
garbeam@43 138 XFreeFontSet(dpy, dc.font.set);
garbeam@43 139 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 140 if(missing) {
garbeam@2 141 while(n--)
garbeam@2 142 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 143 XFreeStringList(missing);
garbeam@2 144 }
garbeam@43 145 if(dc.font.set) {
garbeam@2 146 XFontSetExtents *font_extents;
garbeam@2 147 XFontStruct **xfonts;
garbeam@2 148 char **font_names;
garbeam@43 149 dc.font.ascent = dc.font.descent = 0;
garbeam@43 150 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 151 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 152 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 153 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 154 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 155 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 156 dc.font.descent = (*xfonts)->descent;
garbeam@2 157 xfonts++;
garbeam@2 158 }
garbeam@2 159 }
garbeam@2 160 else {
garbeam@43 161 if(dc.font.xfont)
garbeam@43 162 XFreeFont(dpy, dc.font.xfont);
garbeam@43 163 dc.font.xfont = NULL;
arg@626 164 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
arg@627 165 eprint("error, cannot load font: '%s'\n", fontstr);
garbeam@43 166 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 167 dc.font.descent = dc.font.xfont->descent;
garbeam@2 168 }
garbeam@43 169 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 170 }
garbeam@76 171
garbeam@76 172 unsigned int
arg@461 173 textw(const char *text) {
garbeam@76 174 return textnw(text, strlen(text)) + dc.font.height;
garbeam@76 175 }