dmenu/draw.c

137 lines
3.4 KiB
C
Raw Normal View History

2010-06-23 08:04:54 -03:00
/* See LICENSE file for copyright and license details. */
2010-06-23 09:29:15 -03:00
#include <ctype.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <X11/Xlib.h>
#include "draw.h"
2010-06-23 08:04:54 -03:00
2010-06-23 09:29:15 -03:00
/* macros */
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
2010-06-23 08:04:54 -03:00
/* variables */
2010-06-24 12:18:18 -03:00
const char *progname;
2010-06-23 08:04:54 -03:00
void
2010-06-24 12:18:18 -03:00
cleanupdraw(DC *dc) {
if(dc->font.set)
XFreeFontSet(dc->dpy, dc->font.set);
2010-06-23 08:04:54 -03:00
else
2010-06-24 12:18:18 -03:00
XFreeFont(dc->dpy, dc->font.xfont);
XFreePixmap(dc->dpy, dc->drawable);
XFreeGC(dc->dpy, dc->gc);
2010-06-23 08:04:54 -03:00
}
void
2010-06-24 12:18:18 -03:00
setupdraw(DC *dc, Window w) {
XWindowAttributes wa;
XGetWindowAttributes(dc->dpy, w, &wa);
dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height,
DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
dc->gc = XCreateGC(dc->dpy, w, 0, NULL);
XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
if(!dc->font.set)
XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
2010-06-23 08:04:54 -03:00
}
void
2010-06-24 12:18:18 -03:00
drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
2010-06-23 08:04:54 -03:00
char buf[256];
int i, x, y, h, len, olen;
2010-06-24 12:18:18 -03:00
XRectangle r = { dc->x, dc->y, dc->w, dc->h };
2010-06-23 08:04:54 -03:00
2010-06-24 12:18:18 -03:00
XSetForeground(dc->dpy, dc->gc, col[ColBG]);
XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
2010-06-23 08:04:54 -03:00
if(!text)
return;
olen = strlen(text);
2010-06-24 12:18:18 -03:00
h = dc->font.height;
y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent;
x = dc->x + (h / 2);
2010-06-23 08:04:54 -03:00
/* shorten text if necessary */
2010-06-24 12:18:18 -03:00
for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--);
2010-06-23 08:04:54 -03:00
if(!len)
return;
memcpy(buf, text, len);
if(len < olen)
for(i = len; i && i > len - 3; buf[--i] = '.');
2010-06-24 12:18:18 -03:00
XSetForeground(dc->dpy, dc->gc, col[ColFG]);
if(dc->font.set)
XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len);
2010-06-23 08:04:54 -03:00
else
2010-06-24 12:18:18 -03:00
XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len);
2010-06-23 08:04:54 -03:00
}
2010-06-23 09:29:15 -03:00
void
2010-06-24 12:18:18 -03:00
eprint(const char *fmt, ...) {
2010-06-23 09:29:15 -03:00
va_list ap;
fprintf(stderr, "%s: ", progname);
2010-06-24 12:18:18 -03:00
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
2010-06-23 09:29:15 -03:00
va_end(ap);
exit(EXIT_FAILURE);
}
2010-06-23 08:04:54 -03:00
unsigned long
2010-06-24 12:18:18 -03:00
getcolor(DC *dc, const char *colstr) {
Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
2010-06-23 08:04:54 -03:00
XColor color;
2010-06-24 12:18:18 -03:00
if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
eprint("cannot allocate color '%s'\n", colstr);
2010-06-23 08:04:54 -03:00
return color.pixel;
}
void
2010-06-24 12:18:18 -03:00
initfont(DC *dc, const char *fontstr) {
2010-06-23 08:04:54 -03:00
char *def, **missing = NULL;
int i, n;
if(!fontstr || !*fontstr)
eprint("cannot load null font\n");
2010-06-24 12:18:18 -03:00
dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def);
2010-06-23 08:04:54 -03:00
if(missing)
XFreeStringList(missing);
2010-06-24 12:18:18 -03:00
if(dc->font.set) {
2010-06-23 08:04:54 -03:00
XFontStruct **xfonts;
char **font_names;
2010-06-24 12:18:18 -03:00
dc->font.ascent = dc->font.descent = 0;
n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names);
2010-06-23 08:04:54 -03:00
for(i = 0; i < n; i++) {
2010-06-24 12:18:18 -03:00
dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent);
dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent);
2010-06-23 08:04:54 -03:00
xfonts++;
}
}
else {
2010-06-24 12:18:18 -03:00
if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))
&& !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed")))
eprint("cannot load font '%s'\n", fontstr);
2010-06-24 12:18:18 -03:00
dc->font.ascent = dc->font.xfont->ascent;
dc->font.descent = dc->font.xfont->descent;
2010-06-23 08:04:54 -03:00
}
2010-06-24 12:18:18 -03:00
dc->font.height = dc->font.ascent + dc->font.descent;
2010-06-23 08:04:54 -03:00
}
int
2010-06-24 12:18:18 -03:00
textnw(DC *dc, const char *text, unsigned int len) {
2010-06-23 08:04:54 -03:00
XRectangle r;
2010-06-24 12:18:18 -03:00
if(dc->font.set) {
XmbTextExtents(dc->font.set, text, len, NULL, &r);
2010-06-23 08:04:54 -03:00
return r.width;
}
2010-06-24 12:18:18 -03:00
return XTextWidth(dc->font.xfont, text, len);
2010-06-23 08:04:54 -03:00
}
int
2010-06-24 12:18:18 -03:00
textw(DC *dc, const char *text) {
return textnw(dc, text, strlen(text)) + dc->font.height;
2010-06-23 08:04:54 -03:00
}