My dmenu build
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

35 lines
1.0 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <string.h>
  3. #include <X11/Xlib.h>
  4. #include "draw.h"
  5. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  6. void
  7. drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert) {
  8. char buf[256];
  9. int i, x, y, h, len, olen;
  10. XRectangle r = { dc->x, dc->y, dc->w, dc->h };
  11. XSetForeground(dc->dpy, dc->gc, col[invert ? ColFG : ColBG]);
  12. XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
  13. if(!text)
  14. return;
  15. olen = strlen(text);
  16. h = dc->font.height;
  17. y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent;
  18. x = dc->x + (h / 2);
  19. /* shorten text if necessary */
  20. for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--);
  21. if(!len)
  22. return;
  23. memcpy(buf, text, len);
  24. if(len < olen)
  25. for(i = len; i && i > len - 3; buf[--i] = '.');
  26. XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]);
  27. if(dc->font.set)
  28. XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len);
  29. else
  30. XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len);
  31. }