1
0

backported drawtext() simplifications to dmenu

This commit is contained in:
Anselm R Garbe 2008-08-18 10:03:28 +01:00
parent c43c692a4d
commit cf7e4c15a9
2 changed files with 11 additions and 22 deletions

View File

@ -1,5 +1,5 @@
# dmenu version # dmenu version
VERSION = 3.8 VERSION = 3.9
# Customize below to fit your system # Customize below to fit your system

31
dmenu.c
View File

@ -17,6 +17,7 @@
/* macros */ /* macros */
#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
#define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH)) #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
/* enums */ /* enums */
enum { ColFG, ColBG, ColLast }; enum { ColFG, ColBG, ColLast };
@ -212,37 +213,25 @@ drawmenu(void) {
void void
drawtext(const char *text, unsigned long col[ColLast]) { drawtext(const char *text, unsigned long col[ColLast]) {
int x, y, w, h; int i, x, y, h, len, olen;
static char buf[256]; char buf[256];
unsigned int len, olen;
XRectangle r = { dc.x, dc.y, dc.w, dc.h }; XRectangle r = { dc.x, dc.y, dc.w, dc.h };
XSetForeground(dpy, dc.gc, col[ColBG]); XSetForeground(dpy, dc.gc, col[ColBG]);
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
if(!text) if(!text)
return; return;
w = 0; olen = strlen(text);
olen = len = strlen(text);
if(len >= sizeof buf)
len = sizeof buf - 1;
memcpy(buf, text, len);
buf[len] = 0;
h = dc.font.ascent + dc.font.descent; h = dc.font.ascent + dc.font.descent;
y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
x = dc.x + (h / 2); x = dc.x + (h / 2);
/* shorten text if necessary */ /* shorten text if necessary */
while(len && (w = textnw(buf, len)) > dc.w - h) for(len = MIN(olen, sizeof buf); len && (i = textnw(buf, len)) > dc.w - h; len--);
buf[--len] = 0; if(!len)
if(len < olen) { return;
if(len > 1) memcpy(buf, text, len);
buf[len - 1] = '.'; if(len < olen)
if(len > 2) for(i = len; i && i > len - 3; buf[--i] = '.');
buf[len - 2] = '.';
if(len > 3)
buf[len - 3] = '.';
}
if(w > dc.w)
return; /* too long */
XSetForeground(dpy, dc.gc, col[ColFG]); XSetForeground(dpy, dc.gc, col[ColFG]);
if(dc.font.set) if(dc.font.set)
XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);