dwm-meillo

diff draw.c @ 2:a79188fe4a40

added new stuff
author Anselm R. Garbe <garbeam@wmii.de>
date Mon, 10 Jul 2006 18:35:39 +0200
parents
children e969f3575b7a
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/draw.c	Mon Jul 10 18:35:39 2006 +0200
     1.3 @@ -0,0 +1,163 @@
     1.4 +/*
     1.5 + * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
     1.6 + * See LICENSE file for license details.
     1.7 + */
     1.8 +
     1.9 +#include <stdio.h>
    1.10 +#include <string.h>
    1.11 +
    1.12 +#include "draw.h"
    1.13 +#include "util.h"
    1.14 +
    1.15 +static void
    1.16 +drawborder(Display *dpy, Brush *b)
    1.17 +{
    1.18 +	XPoint points[5];
    1.19 +	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
    1.20 +	XSetForeground(dpy, b->gc, b->color.border);
    1.21 +	points[0].x = b->rect.x;
    1.22 +	points[0].y = b->rect.y;
    1.23 +	points[1].x = b->rect.width - 1;
    1.24 +	points[1].y = 0;
    1.25 +	points[2].x = 0;
    1.26 +	points[2].y = b->rect.height - 1;
    1.27 +	points[3].x = -(b->rect.width - 1);
    1.28 +	points[3].y = 0;
    1.29 +	points[4].x = 0;
    1.30 +	points[4].y = -(b->rect.height - 1);
    1.31 +	XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
    1.32 +}
    1.33 +
    1.34 +void
    1.35 +draw(Display *dpy, Brush *b)
    1.36 +{
    1.37 +	unsigned int x, y, w, h, len;
    1.38 +	static char buf[256];
    1.39 +	XGCValues gcv;
    1.40 +
    1.41 +	XSetForeground(dpy, b->gc, b->color.bg);
    1.42 +	XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
    1.43 +
    1.44 +	if(b->border)
    1.45 +		drawborder(dpy, b);
    1.46 +
    1.47 +	if(!b->text)
    1.48 +		return;
    1.49 +
    1.50 +	len = strlen(b->text);
    1.51 +	if(len >= sizeof(buf))
    1.52 +		len = sizeof(buf) - 1;
    1.53 +	memcpy(buf, b->text, len);
    1.54 +	buf[len] = 0;
    1.55 +
    1.56 +	h = b->font->ascent + b->font->descent;
    1.57 +	y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font->ascent;
    1.58 +	x = b->rect.x + (h / 2);
    1.59 +
    1.60 +	/* shorten text if necessary */
    1.61 +	while(len && (w = textwidth_l(b->font, buf, len)) > b->rect.width - h)
    1.62 +		buf[--len] = 0;
    1.63 +
    1.64 +	if(w > b->rect.width)
    1.65 +		return; /* too long */
    1.66 +
    1.67 +	gcv.foreground = b->color.fg;
    1.68 +	gcv.background = b->color.bg;
    1.69 +	if(b->font->set) {
    1.70 +		XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
    1.71 +		XmbDrawImageString(dpy, b->drawable, b->font->set, b->gc,
    1.72 +				x, y, buf, len);
    1.73 +	}
    1.74 +	else {
    1.75 +		gcv.font = b->font->xfont->fid;
    1.76 +		XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
    1.77 +		XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
    1.78 +	}
    1.79 +}
    1.80 +
    1.81 +static unsigned long
    1.82 +xloadcolor(Display *dpy, Colormap cmap, const char *colstr)
    1.83 +{
    1.84 +	XColor color;
    1.85 +	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    1.86 +	return color.pixel;
    1.87 +}
    1.88 +
    1.89 +void
    1.90 +loadcolor(Display *dpy, int screen, Color *c,
    1.91 +		const char *bg, const char *fg, const char *border)
    1.92 +{
    1.93 +	Colormap cmap = DefaultColormap(dpy, screen);
    1.94 +	c->bg = xloadcolor(dpy, cmap, bg);
    1.95 +	c->fg = xloadcolor(dpy, cmap, fg);
    1.96 +	c->border = xloadcolor(dpy, cmap, border);
    1.97 +}
    1.98 +
    1.99 +unsigned int
   1.100 +textwidth_l(Fnt *font, char *text, unsigned int len)
   1.101 +{
   1.102 +	if(font->set) {
   1.103 +		XRectangle r;
   1.104 +		XmbTextExtents(font->set, text, len, 0, &r);
   1.105 +		return r.width;
   1.106 +	}
   1.107 +	return XTextWidth(font->xfont, text, len);
   1.108 +}
   1.109 +
   1.110 +unsigned int
   1.111 +textwidth(Fnt *font, char *text)
   1.112 +{
   1.113 +	return textwidth_l(font, text, strlen(text));
   1.114 +}
   1.115 +
   1.116 +void
   1.117 +loadfont(Display *dpy, Fnt *font, const char *fontstr)
   1.118 +{
   1.119 +	char **missing, *def;
   1.120 +	int n;
   1.121 +
   1.122 +	missing = 0;
   1.123 +	def = "?";
   1.124 +	setlocale(LC_ALL, "");
   1.125 +	if(font->set)
   1.126 +		XFreeFontSet(dpy, font->set);
   1.127 +	font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
   1.128 +	if(missing) {
   1.129 +		while(n--)
   1.130 +			fprintf(stderr, "missing fontset: %s\n", missing[n]);
   1.131 +		XFreeStringList(missing);
   1.132 +		if(font->set) {
   1.133 +			XFreeFontSet(dpy, font->set);
   1.134 +			font->set = 0;
   1.135 +		}
   1.136 +	}
   1.137 +	if(font->set) {
   1.138 +		XFontSetExtents *font_extents;
   1.139 +		XFontStruct **xfonts;
   1.140 +		char **font_names;
   1.141 +		unsigned int i;
   1.142 +
   1.143 +		font->ascent = font->descent = 0;
   1.144 +		font_extents = XExtentsOfFontSet(font->set);
   1.145 +		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
   1.146 +		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
   1.147 +			if(font->ascent < (*xfonts)->ascent)
   1.148 +				font->ascent = (*xfonts)->ascent;
   1.149 +			if(font->descent < (*xfonts)->descent)
   1.150 +				font->descent = (*xfonts)->descent;
   1.151 +			xfonts++;
   1.152 +		}
   1.153 +	}
   1.154 +	else {
   1.155 +		if(font->xfont)
   1.156 +			XFreeFont(dpy, font->xfont);
   1.157 +		font->xfont = 0;
   1.158 +		font->xfont = XLoadQueryFont(dpy, fontstr);
   1.159 +		if (!font->xfont)
   1.160 +			font->xfont = XLoadQueryFont(dpy, "fixed");
   1.161 +		if (!font->xfont)
   1.162 +			error("error, cannot load 'fixed' font\n");
   1.163 +		font->ascent = font->xfont->ascent;
   1.164 +		font->descent = font->xfont->descent;
   1.165 +	}
   1.166 +}