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.
 
 
 
 
 
 

122 lines
2.9 KiB

  1. /* (C)opyright MMIV-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dmenu.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. /* static */
  8. static unsigned int
  9. textnw(const char *text, unsigned int len) {
  10. XRectangle r;
  11. if(dc.font.set) {
  12. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  13. return r.width;
  14. }
  15. return XTextWidth(dc.font.xfont, text, len);
  16. }
  17. /* extern */
  18. void
  19. drawtext(const char *text, unsigned long col[ColLast]) {
  20. int x, y, w, h;
  21. static char buf[256];
  22. unsigned int len, olen;
  23. XGCValues gcv;
  24. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  25. XSetForeground(dpy, dc.gc, col[ColBG]);
  26. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  27. if(!text)
  28. return;
  29. w = 0;
  30. olen = len = strlen(text);
  31. if(len >= sizeof buf)
  32. len = sizeof buf - 1;
  33. memcpy(buf, text, len);
  34. buf[len] = 0;
  35. h = dc.font.ascent + dc.font.descent;
  36. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  37. x = dc.x + (h / 2);
  38. /* shorten text if necessary */
  39. while(len && (w = textnw(buf, len)) > dc.w - h)
  40. buf[--len] = 0;
  41. if(len < olen) {
  42. if(len > 1)
  43. buf[len - 1] = '.';
  44. if(len > 2)
  45. buf[len - 2] = '.';
  46. if(len > 3)
  47. buf[len - 3] = '.';
  48. }
  49. if(w > dc.w)
  50. return; /* too long */
  51. gcv.foreground = col[ColFG];
  52. if(dc.font.set) {
  53. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  54. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
  55. x, y, buf, len);
  56. }
  57. else {
  58. gcv.font = dc.font.xfont->fid;
  59. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  60. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  61. }
  62. }
  63. unsigned long
  64. getcolor(const char *colstr) {
  65. Colormap cmap = DefaultColormap(dpy, screen);
  66. XColor color;
  67. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  68. eprint("error, cannot allocate color '%s'\n", colstr);
  69. return color.pixel;
  70. }
  71. void
  72. setfont(const char *fontstr) {
  73. char *def, **missing;
  74. int i, n;
  75. missing = NULL;
  76. if(dc.font.set)
  77. XFreeFontSet(dpy, dc.font.set);
  78. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  79. if(missing)
  80. XFreeStringList(missing);
  81. if(dc.font.set) {
  82. XFontSetExtents *font_extents;
  83. XFontStruct **xfonts;
  84. char **font_names;
  85. dc.font.ascent = dc.font.descent = 0;
  86. font_extents = XExtentsOfFontSet(dc.font.set);
  87. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  88. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  89. if(dc.font.ascent < (*xfonts)->ascent)
  90. dc.font.ascent = (*xfonts)->ascent;
  91. if(dc.font.descent < (*xfonts)->descent)
  92. dc.font.descent = (*xfonts)->descent;
  93. xfonts++;
  94. }
  95. }
  96. else {
  97. if(dc.font.xfont)
  98. XFreeFont(dpy, dc.font.xfont);
  99. dc.font.xfont = NULL;
  100. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  101. eprint("error, cannot load font: '%s'\n", fontstr);
  102. dc.font.ascent = dc.font.xfont->ascent;
  103. dc.font.descent = dc.font.xfont->descent;
  104. }
  105. dc.font.height = dc.font.ascent + dc.font.descent;
  106. }
  107. unsigned int
  108. textw(const char *text) {
  109. return textnw(text, strlen(text)) + dc.font.height;
  110. }