aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnselm R Garbe <garbeam@gmail.com>2008-08-18 10:03:28 +0100
committerAnselm R Garbe <garbeam@gmail.com>2008-08-18 10:03:28 +0100
commitcf7e4c15a97fa4e0921985d8e31bf9d6d4414b9a (patch)
tree03480def1eeaee3e02bc5a07b9136687fb5f71e1
parentc43c692a4db35c1527d1beb0edadef671588cb04 (diff)
backported drawtext() simplifications to dmenu
-rw-r--r--config.mk2
-rw-r--r--dmenu.c31
2 files changed, 11 insertions, 22 deletions
diff --git a/config.mk b/config.mk
index 9206a44..bd1ee6b 100644
--- a/config.mk
+++ b/config.mk
@@ -1,5 +1,5 @@
1# dmenu version 1# dmenu version
2VERSION = 3.8 2VERSION = 3.9
3 3
4# Customize below to fit your system 4# Customize below to fit your system
5 5
diff --git a/dmenu.c b/dmenu.c
index 895bdc5..a9c0f60 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -17,6 +17,7 @@
17/* macros */ 17/* macros */
18#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) 18#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
19#define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH)) 19#define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
20#define MIN(a, b) ((a) < (b) ? (a) : (b))
20 21
21/* enums */ 22/* enums */
22enum { ColFG, ColBG, ColLast }; 23enum { ColFG, ColBG, ColLast };
@@ -212,37 +213,25 @@ drawmenu(void) {
212 213
213void 214void
214drawtext(const char *text, unsigned long col[ColLast]) { 215drawtext(const char *text, unsigned long col[ColLast]) {
215 int x, y, w, h; 216 int i, x, y, h, len, olen;
216 static char buf[256]; 217 char buf[256];
217 unsigned int len, olen;
218 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; 218 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
219 219
220 XSetForeground(dpy, dc.gc, col[ColBG]); 220 XSetForeground(dpy, dc.gc, col[ColBG]);
221 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); 221 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
222 if(!text) 222 if(!text)
223 return; 223 return;
224 w = 0; 224 olen = strlen(text);
225 olen = len = strlen(text);
226 if(len >= sizeof buf)
227 len = sizeof buf - 1;
228 memcpy(buf, text, len);
229 buf[len] = 0;
230 h = dc.font.ascent + dc.font.descent; 225 h = dc.font.ascent + dc.font.descent;
231 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; 226 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
232 x = dc.x + (h / 2); 227 x = dc.x + (h / 2);
233 /* shorten text if necessary */ 228 /* shorten text if necessary */
234 while(len && (w = textnw(buf, len)) > dc.w - h) 229 for(len = MIN(olen, sizeof buf); len && (i = textnw(buf, len)) > dc.w - h; len--);
235 buf[--len] = 0; 230 if(!len)
236 if(len < olen) { 231 return;
237 if(len > 1) 232 memcpy(buf, text, len);
238 buf[len - 1] = '.'; 233 if(len < olen)
239 if(len > 2) 234 for(i = len; i && i > len - 3; buf[--i] = '.');
240 buf[len - 2] = '.';
241 if(len > 3)
242 buf[len - 3] = '.';
243 }
244 if(w > dc.w)
245 return; /* too long */
246 XSetForeground(dpy, dc.gc, col[ColFG]); 235 XSetForeground(dpy, dc.gc, col[ColFG]);
247 if(dc.font.set) 236 if(dc.font.set)
248 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); 237 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);