diff draw.c @ 0:7024076fa948

initial add
author paulo@localhost
date Sun, 22 Mar 2009 23:26:35 -0700
parents
children faa4cb9d7bd6
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/draw.c	Sun Mar 22 23:26:35 2009 -0700
     1.3 @@ -0,0 +1,154 @@
     1.4 +/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
     1.5 + * See LICENSE file for license details.
     1.6 + */
     1.7 +#include "dwm.h"
     1.8 +#include <string.h>
     1.9 +
    1.10 +/* static */
    1.11 +
    1.12 +static void
    1.13 +drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
    1.14 +	int x;
    1.15 +	XGCValues gcv;
    1.16 +	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
    1.17 +
    1.18 +	gcv.foreground = col[ColFG];
    1.19 +	XChangeGC(dpy, dc.gc, GCForeground, &gcv);
    1.20 +	x = (dc.font.ascent + dc.font.descent + 2) / 4;
    1.21 +	r.x = dc.x + 1;
    1.22 +	r.y = dc.y + 1;
    1.23 +	if(filled) {
    1.24 +		r.width = r.height = x + 1;
    1.25 +		XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
    1.26 +	}
    1.27 +	else if(empty) {
    1.28 +		r.width = r.height = x;
    1.29 +		XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
    1.30 +	}
    1.31 +}
    1.32 +
    1.33 +static Bool
    1.34 +isoccupied(unsigned int t) {
    1.35 +	Client *c;
    1.36 +
    1.37 +	for(c = clients; c; c = c->next)
    1.38 +		if(c->tags[t])
    1.39 +			return True;
    1.40 +	return False;
    1.41 +}
    1.42 +
    1.43 +static unsigned int
    1.44 +textnw(const char *text, unsigned int len) {
    1.45 +	XRectangle r;
    1.46 +
    1.47 +	if(dc.font.set) {
    1.48 +		XmbTextExtents(dc.font.set, text, len, NULL, &r);
    1.49 +		return r.width;
    1.50 +	}
    1.51 +	return XTextWidth(dc.font.xfont, text, len);
    1.52 +}
    1.53 +
    1.54 +/* extern */
    1.55 +
    1.56 +void
    1.57 +drawstatus(void) {
    1.58 +	Client *c;
    1.59 +	int i, j, x;
    1.60 +
    1.61 +	dc.x = dc.y = 0;
    1.62 +	for(i = 0; i < ntags; i++) {
    1.63 +		dc.w = textw(tags[i]);
    1.64 +		if(seltag[i]) {
    1.65 +			drawtext(tags[i], dc.sel);
    1.66 +			drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
    1.67 +		}
    1.68 +		else {
    1.69 +			drawtext(tags[i], dc.norm);
    1.70 +			drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
    1.71 +		}
    1.72 +		dc.x += dc.w;
    1.73 +	}
    1.74 +	dc.w = blw;
    1.75 +	drawtext(lt->symbol, dc.norm);
    1.76 +	x = dc.x + dc.w;
    1.77 +	dc.w = textw(stext);
    1.78 +	dc.x = sw - dc.w;
    1.79 +	if(dc.x < x) {
    1.80 +		dc.x = x;
    1.81 +		dc.w = sw - x;
    1.82 +	}
    1.83 +	drawtext(stext, dc.norm);
    1.84 +	if((dc.w = dc.x - x) > bh) {
    1.85 +		dc.x = x;
    1.86 +		for(j=0, c = clients; c; c = c->next) {
    1.87 +			if(isvisible(c))
    1.88 +				j++;
    1.89 +		}
    1.90 +		if(j && j < MAX_TASKS)
    1.91 +			dc.w /= j;
    1.92 +		else {
    1.93 +			drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm);
    1.94 +			j = 0;
    1.95 +		}
    1.96 +		for(c = clients; j && c; c = c->next) {
    1.97 +			if(isvisible(c)) {
    1.98 +				drawtext(c->name, (c == sel) ? dc.sel : dc.norm);
    1.99 +				dc.x += dc.w;
   1.100 +				j--;
   1.101 +			}
   1.102 +		}
   1.103 +	}
   1.104 +	XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
   1.105 +	XSync(dpy, False);
   1.106 +}
   1.107 +
   1.108 +void
   1.109 +drawtext(const char *text, unsigned long col[ColLast]) {
   1.110 +	int x, y, w, h;
   1.111 +	static char buf[256];
   1.112 +	unsigned int len, olen;
   1.113 +	XGCValues gcv;
   1.114 +	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
   1.115 +
   1.116 +	XSetForeground(dpy, dc.gc, col[ColBG]);
   1.117 +	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
   1.118 +	if(!text)
   1.119 +		return;
   1.120 +	w = 0;
   1.121 +	olen = len = strlen(text);
   1.122 +	if(len >= sizeof buf)
   1.123 +		len = sizeof buf - 1;
   1.124 +	memcpy(buf, text, len);
   1.125 +	buf[len] = 0;
   1.126 +	h = dc.font.ascent + dc.font.descent;
   1.127 +	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
   1.128 +	x = dc.x + (h / 2);
   1.129 +	/* shorten text if necessary */
   1.130 +	while(len && (w = textnw(buf, len)) > dc.w - h)
   1.131 +		buf[--len] = 0;
   1.132 +	if(len < olen) {
   1.133 +		if(len > 1)
   1.134 +			buf[len - 1] = '.';
   1.135 +		if(len > 2)
   1.136 +			buf[len - 2] = '.';
   1.137 +		if(len > 3)
   1.138 +			buf[len - 3] = '.';
   1.139 +	}
   1.140 +	if(w > dc.w)
   1.141 +		return; /* too long */
   1.142 +	gcv.foreground = col[ColFG];
   1.143 +	if(dc.font.set) {
   1.144 +		XChangeGC(dpy, dc.gc, GCForeground, &gcv);
   1.145 +		XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
   1.146 +	}
   1.147 +	else {
   1.148 +		gcv.font = dc.font.xfont->fid;
   1.149 +		XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
   1.150 +		XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
   1.151 +	}
   1.152 +}
   1.153 +
   1.154 +unsigned int
   1.155 +textw(const char *text) {
   1.156 +	return textnw(text, strlen(text)) + dc.font.height;
   1.157 +}