549 } |
549 } |
550 } |
550 } |
551 |
551 |
552 void |
552 void |
553 drawtext(const char *text, ulong col[ColLast], Bool invert) { |
553 drawtext(const char *text, ulong col[ColLast], Bool invert) { |
554 int x, y, w, h; |
554 int i, x, y, h; |
555 uint len, olen; |
555 uint len, olen; |
556 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
556 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
557 char buf[256]; |
557 char buf[256]; |
558 |
558 |
559 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]); |
559 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]); |
561 if(!text) |
561 if(!text) |
562 return; |
562 return; |
563 olen = strlen(text); |
563 olen = strlen(text); |
564 len = MIN(olen, sizeof buf); |
564 len = MIN(olen, sizeof buf); |
565 memcpy(buf, text, len); |
565 memcpy(buf, text, len); |
566 w = 0; |
|
567 h = dc.font.ascent + dc.font.descent; |
566 h = dc.font.ascent + dc.font.descent; |
568 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
567 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
569 x = dc.x + (h / 2); |
568 x = dc.x + (h / 2); |
570 /* shorten text if necessary */ |
569 /* shorten text if necessary */ |
571 for(; len && (w = textnw(buf, len)) > dc.w - h; len--); |
570 for(; len && (i = textnw(buf, len)) > dc.w - h; len--); |
572 if(!len) |
571 if(!len) |
573 return; |
572 return; |
574 if(len < olen) |
573 if(len < olen) |
575 memcpy(&buf[MAX(0, len - 3)], "...", 3); |
574 for(i = len; i >= MAX(0, len - 3); buf[i--] = '.'); |
576 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]); |
575 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]); |
577 if(dc.font.set) |
576 if(dc.font.set) |
578 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |
577 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |
579 else |
578 else |
580 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); |
579 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); |