1
0
Fork 0
dmenu/draw/drawtext.c

35 lines
1 KiB
C
Raw Normal View History

2010-06-24 19:44:35 +03:00
/* See LICENSE file for copyright and license details. */
#include <string.h>
#include <X11/Xlib.h>
#include "draw.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void
2010-06-28 08:09:34 +03:00
drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert) {
2010-06-24 19:44:35 +03:00
char buf[256];
int i, x, y, h, len, olen;
XRectangle r = { dc->x, dc->y, dc->w, dc->h };
2010-06-28 08:09:34 +03:00
XSetForeground(dc->dpy, dc->gc, col[invert ? ColFG : ColBG]);
2010-06-24 19:44:35 +03:00
XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
if(!text)
return;
olen = strlen(text);
h = dc->font.height;
y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent;
x = dc->x + (h / 2);
/* shorten text if necessary */
for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--);
if(!len)
return;
memcpy(buf, text, len);
if(len < olen)
for(i = len; i && i > len - 3; buf[--i] = '.');
2010-06-28 08:09:34 +03:00
XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]);
2010-06-24 19:44:35 +03:00
if(dc->font.set)
XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len);
else
XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len);
}