My dmenu build
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

144 lignes
3.5 KiB

  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. initfont(font);
  45. /* pixmap */
  46. dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
  47. dc.gc = XCreateGC(dpy, parent, 0, NULL);
  48. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  49. if(!dc.font.set)
  50. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  51. }
  52. void
  53. drawtext(const char *text, unsigned long col[ColLast]) {
  54. char buf[256];
  55. int i, x, y, h, len, olen;
  56. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  57. XSetForeground(dpy, dc.gc, col[ColBG]);
  58. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  59. if(!text)
  60. return;
  61. olen = strlen(text);
  62. h = dc.font.height;
  63. y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent;
  64. x = dc.x + (h / 2);
  65. /* shorten text if necessary */
  66. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  67. if(!len)
  68. return;
  69. memcpy(buf, text, len);
  70. if(len < olen)
  71. for(i = len; i && i > len - 3; buf[--i] = '.');
  72. XSetForeground(dpy, dc.gc, col[ColFG]);
  73. if(dc.font.set)
  74. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  75. else
  76. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  77. }
  78. unsigned long
  79. getcolor(const char *colstr) {
  80. Colormap cmap = DefaultColormap(dpy, screen);
  81. XColor color;
  82. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  83. eprint("drawtext: cannot allocate color '%s'\n", colstr);
  84. return color.pixel;
  85. }
  86. void
  87. initfont(const char *fontstr) {
  88. char *def, **missing = NULL;
  89. int i, n;
  90. if(!fontstr || fontstr[0] == '\0')
  91. eprint("drawtext: cannot load font: '%s'\n", fontstr);
  92. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  93. if(missing)
  94. XFreeStringList(missing);
  95. if(dc.font.set) {
  96. XFontStruct **xfonts;
  97. char **font_names;
  98. dc.font.ascent = dc.font.descent = 0;
  99. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  100. for(i = 0; i < n; i++) {
  101. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  102. dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
  103. xfonts++;
  104. }
  105. }
  106. else {
  107. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  108. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  109. eprint("drawtext: cannot load font: '%s'\n", fontstr);
  110. dc.font.ascent = dc.font.xfont->ascent;
  111. dc.font.descent = dc.font.xfont->descent;
  112. }
  113. dc.font.height = dc.font.ascent + dc.font.descent;
  114. }
  115. int
  116. textnw(const char *text, unsigned int len) {
  117. XRectangle r;
  118. if(dc.font.set) {
  119. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  120. return r.width;
  121. }
  122. return XTextWidth(dc.font.xfont, text, len);
  123. }
  124. int
  125. textw(const char *text) {
  126. return textnw(text, strlen(text)) + dc.font.height;
  127. }