dmenu for lunch applications in dwm
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.

142 lines
3.5 KiB

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