diff options
author | Anselm R Garbe <garbeam@gmail.com> | 2008-08-18 10:03:28 +0100 |
---|---|---|
committer | Anselm R Garbe <garbeam@gmail.com> | 2008-08-18 10:03:28 +0100 |
commit | cf7e4c15a97fa4e0921985d8e31bf9d6d4414b9a (patch) | |
tree | 03480def1eeaee3e02bc5a07b9136687fb5f71e1 | |
parent | c43c692a4db35c1527d1beb0edadef671588cb04 (diff) | |
download | dmenu-cf7e4c15a97fa4e0921985d8e31bf9d6d4414b9a.tar.gz dmenu-cf7e4c15a97fa4e0921985d8e31bf9d6d4414b9a.tar.bz2 dmenu-cf7e4c15a97fa4e0921985d8e31bf9d6d4414b9a.zip |
backported drawtext() simplifications to dmenu
-rw-r--r-- | config.mk | 2 | ||||
-rw-r--r-- | dmenu.c | 31 |
2 files changed, 11 insertions, 22 deletions
@@ -1,5 +1,5 @@ # dmenu version -VERSION = 3.8 +VERSION = 3.9 # Customize below to fit your system @@ -17,6 +17,7 @@ /* macros */ #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 MIN(a, b) ((a) < (b) ? (a) : (b)) /* enums */ enum { ColFG, ColBG, ColLast }; @@ -212,37 +213,25 @@ drawmenu(void) { void drawtext(const char *text, unsigned long col[ColLast]) { - int x, y, w, h; - static char buf[256]; - unsigned int len, olen; + int i, x, y, h, len, olen; + char buf[256]; XRectangle r = { dc.x, dc.y, dc.w, dc.h }; XSetForeground(dpy, dc.gc, col[ColBG]); XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); if(!text) return; - w = 0; - olen = len = strlen(text); - if(len >= sizeof buf) - len = sizeof buf - 1; - memcpy(buf, text, len); - buf[len] = 0; + olen = strlen(text); h = dc.font.ascent + dc.font.descent; y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; x = dc.x + (h / 2); /* shorten text if necessary */ - while(len && (w = textnw(buf, len)) > dc.w - h) - buf[--len] = 0; - if(len < olen) { - if(len > 1) - buf[len - 1] = '.'; - if(len > 2) - buf[len - 2] = '.'; - if(len > 3) - buf[len - 3] = '.'; - } - if(w > dc.w) - return; /* too long */ + for(len = MIN(olen, sizeof buf); len && (i = textnw(buf, len)) > dc.w - h; len--); + if(!len) + return; + memcpy(buf, text, len); + if(len < olen) + for(i = len; i && i > len - 3; buf[--i] = '.'); XSetForeground(dpy, dc.gc, col[ColFG]); if(dc.font.set) XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |