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.
 
 
 
 
 
 

136 lines
3.3 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. void
  15. drawcleanup(void) {
  16. if(dc.font.set)
  17. XFreeFontSet(dpy, dc.font.set);
  18. else
  19. XFreeFont(dpy, dc.font.xfont);
  20. XFreePixmap(dpy, dc.drawable);
  21. XFreeGC(dpy, dc.gc);
  22. }
  23. void
  24. drawsetup(void) {
  25. /* style */
  26. dc.norm[ColBG] = getcolor(normbgcolor);
  27. dc.norm[ColFG] = getcolor(normfgcolor);
  28. dc.sel[ColBG] = getcolor(selbgcolor);
  29. dc.sel[ColFG] = getcolor(selfgcolor);
  30. /* pixmap */
  31. dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
  32. dc.gc = XCreateGC(dpy, parent, 0, NULL);
  33. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  34. if(!dc.font.set)
  35. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  36. }
  37. void
  38. drawtext(const char *text, unsigned long col[ColLast]) {
  39. char buf[256];
  40. int i, x, y, h, len, olen;
  41. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  42. XSetForeground(dpy, dc.gc, col[ColBG]);
  43. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  44. if(!text)
  45. return;
  46. olen = strlen(text);
  47. h = dc.font.height;
  48. y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent;
  49. x = dc.x + (h / 2);
  50. /* shorten text if necessary */
  51. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  52. if(!len)
  53. return;
  54. memcpy(buf, text, len);
  55. if(len < olen)
  56. for(i = len; i && i > len - 3; buf[--i] = '.');
  57. XSetForeground(dpy, dc.gc, col[ColFG]);
  58. if(dc.font.set)
  59. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  60. else
  61. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  62. }
  63. void
  64. eprint(const char *errstr, ...) {
  65. va_list ap;
  66. va_start(ap, errstr);
  67. vfprintf(stderr, errstr, ap);
  68. va_end(ap);
  69. exit(EXIT_FAILURE);
  70. }
  71. unsigned long
  72. getcolor(const char *colstr) {
  73. Colormap cmap = DefaultColormap(dpy, screen);
  74. XColor color;
  75. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  76. eprint("drawtext: cannot allocate color '%s'\n", colstr);
  77. return color.pixel;
  78. }
  79. void
  80. initfont(const char *fontstr) {
  81. char *def, **missing = NULL;
  82. int i, n;
  83. if(!fontstr || fontstr[0] == '\0')
  84. eprint("drawtext: cannot load font: '%s'\n", fontstr);
  85. dc.font.set = XCreateFontSet(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(dpy, fontstr))
  101. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  102. eprint("drawtext: 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(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(const char *text) {
  119. return textnw(text, strlen(text)) + dc.font.height;
  120. }