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.1 KiB

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