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.
 
 
 
 
 
 

137 lines
3.4 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <locale.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <strings.h>
  9. #include <X11/Xlib.h>
  10. #include "draw.h"
  11. /* macros */
  12. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  13. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  14. /* variables */
  15. const char *progname;
  16. void
  17. cleanupdraw(DC *dc) {
  18. if(dc->font.set)
  19. XFreeFontSet(dc->dpy, dc->font.set);
  20. else
  21. XFreeFont(dc->dpy, dc->font.xfont);
  22. XFreePixmap(dc->dpy, dc->drawable);
  23. XFreeGC(dc->dpy, dc->gc);
  24. }
  25. void
  26. setupdraw(DC *dc, Window w) {
  27. XWindowAttributes wa;
  28. XGetWindowAttributes(dc->dpy, w, &wa);
  29. dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height,
  30. DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
  31. dc->gc = XCreateGC(dc->dpy, w, 0, NULL);
  32. XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
  33. if(!dc->font.set)
  34. XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
  35. }
  36. void
  37. drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
  38. char buf[256];
  39. int i, x, y, h, len, olen;
  40. XRectangle r = { dc->x, dc->y, dc->w, dc->h };
  41. XSetForeground(dc->dpy, dc->gc, col[ColBG]);
  42. XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
  43. if(!text)
  44. return;
  45. olen = strlen(text);
  46. h = dc->font.height;
  47. y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent;
  48. x = dc->x + (h / 2);
  49. /* shorten text if necessary */
  50. for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--);
  51. if(!len)
  52. return;
  53. memcpy(buf, text, len);
  54. if(len < olen)
  55. for(i = len; i && i > len - 3; buf[--i] = '.');
  56. XSetForeground(dc->dpy, dc->gc, col[ColFG]);
  57. if(dc->font.set)
  58. XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len);
  59. else
  60. XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len);
  61. }
  62. void
  63. eprint(const char *fmt, ...) {
  64. va_list ap;
  65. fprintf(stderr, "%s: ", progname);
  66. va_start(ap, fmt);
  67. vfprintf(stderr, fmt, ap);
  68. va_end(ap);
  69. exit(EXIT_FAILURE);
  70. }
  71. unsigned long
  72. getcolor(DC *dc, const char *colstr) {
  73. Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
  74. XColor color;
  75. if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
  76. eprint("cannot allocate color '%s'\n", colstr);
  77. return color.pixel;
  78. }
  79. void
  80. initfont(DC *dc, const char *fontstr) {
  81. char *def, **missing = NULL;
  82. int i, n;
  83. if(!fontstr || !*fontstr)
  84. eprint("cannot load null font\n");
  85. dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def);
  86. if(missing)
  87. XFreeStringList(missing);
  88. if(dc->font.set) {
  89. XFontStruct **xfonts;
  90. char **font_names;
  91. dc->font.ascent = dc->font.descent = 0;
  92. n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names);
  93. for(i = 0; i < n; i++) {
  94. dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent);
  95. dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent);
  96. xfonts++;
  97. }
  98. }
  99. else {
  100. if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))
  101. && !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed")))
  102. eprint("cannot load font '%s'\n", fontstr);
  103. dc->font.ascent = dc->font.xfont->ascent;
  104. dc->font.descent = dc->font.xfont->descent;
  105. }
  106. dc->font.height = dc->font.ascent + dc->font.descent;
  107. }
  108. int
  109. textnw(DC *dc, const char *text, unsigned int len) {
  110. XRectangle r;
  111. if(dc->font.set) {
  112. XmbTextExtents(dc->font.set, text, len, NULL, &r);
  113. return r.width;
  114. }
  115. return XTextWidth(dc->font.xfont, text, len);
  116. }
  117. int
  118. textw(DC *dc, const char *text) {
  119. return textnw(dc, text, strlen(text)) + dc->font.height;
  120. }