Mercurial > dwm-meillo
comparison draw.c @ 269:bf6792e3e700
fixed string cutting in draw.c
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Mon, 14 Aug 2006 08:52:15 +0200 |
parents | d659a2dce2b5 |
children | ffc73b32084a |
comparison
equal
deleted
inserted
replaced
268:a47b3b0d7bf4 | 269:bf6792e3e700 |
---|---|
24 static void | 24 static void |
25 drawtext(const char *text, Bool invert) | 25 drawtext(const char *text, Bool invert) |
26 { | 26 { |
27 int x, y, w, h; | 27 int x, y, w, h; |
28 static char buf[256]; | 28 static char buf[256]; |
29 unsigned int len; | 29 unsigned int len, olen; |
30 XGCValues gcv; | 30 XGCValues gcv; |
31 XPoint points[5]; | 31 XPoint points[5]; |
32 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; | 32 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
33 | 33 |
34 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg); | 34 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg); |
49 | 49 |
50 if(!text) | 50 if(!text) |
51 return; | 51 return; |
52 | 52 |
53 w = 0; | 53 w = 0; |
54 len = strlen(text); | 54 olen = len = strlen(text); |
55 if(len >= sizeof(buf)) | 55 if(len >= sizeof(buf)) |
56 len = sizeof(buf) - 1; | 56 len = sizeof(buf) - 1; |
57 memcpy(buf, text, len); | 57 memcpy(buf, text, len); |
58 buf[len] = 0; | 58 buf[len] = 0; |
59 | 59 |
62 x = dc.x + (h / 2); | 62 x = dc.x + (h / 2); |
63 | 63 |
64 /* shorten text if necessary */ | 64 /* shorten text if necessary */ |
65 while(len && (w = textnw(buf, len)) > dc.w - h) | 65 while(len && (w = textnw(buf, len)) > dc.w - h) |
66 buf[--len] = 0; | 66 buf[--len] = 0; |
67 if(len < olen) { | |
68 if(len > 3) | |
69 memcpy(buf + len - 4, "...\0", 4); | |
70 else if(len > 2) | |
71 memcpy(buf + len - 3, "..\0", 3); | |
72 else if(len > 1) | |
73 memcpy(buf + len - 2, ".\0", 2); | |
74 } | |
67 | 75 |
68 if(w > dc.w) | 76 if(w > dc.w) |
69 return; /* too long */ | 77 return; /* too long */ |
70 | 78 |
71 gcv.foreground = invert ? dc.bg : dc.fg; | 79 gcv.foreground = invert ? dc.bg : dc.fg; |