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.
 
 
 
 
 
 

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