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.
 
 
 
 
 
 

154 lines
3.3 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. {
  13. XRectangle r;
  14. if(dc.font.set) {
  15. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  16. return r.width;
  17. }
  18. return XTextWidth(dc.font.xfont, text, len);
  19. }
  20. /* extern */
  21. void
  22. drawtext(const char *text, unsigned int colidx, Bool border)
  23. {
  24. int x, y, w, h;
  25. static char buf[256];
  26. unsigned int len, olen;
  27. XPoint points[5];
  28. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  29. XSetForeground(dpy, dc.gc, dc.bg[colidx]);
  30. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  31. w = 0;
  32. XSetForeground(dpy, dc.gc, dc.fg[colidx]);
  33. if(border) {
  34. points[0].x = dc.x;
  35. points[0].y = dc.y;
  36. points[1].x = dc.w - 1;
  37. points[1].y = 0;
  38. points[2].x = 0;
  39. points[2].y = dc.h - 1;
  40. points[3].x = -(dc.w - 1);
  41. points[3].y = 0;
  42. points[4].x = 0;
  43. points[4].y = -(dc.h - 1);
  44. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  45. }
  46. if(!text)
  47. return;
  48. olen = len = strlen(text);
  49. if(len >= sizeof(buf))
  50. len = sizeof(buf) - 1;
  51. memcpy(buf, text, len);
  52. buf[len] = 0;
  53. h = dc.font.ascent + dc.font.descent;
  54. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  55. x = dc.x + (h / 2);
  56. /* shorten text if necessary */
  57. while(len && (w = textnw(buf, len)) > dc.w - h)
  58. buf[--len] = 0;
  59. if(len < olen) {
  60. if(len > 1)
  61. buf[len - 1] = '.';
  62. if(len > 2)
  63. buf[len - 2] = '.';
  64. if(len > 3)
  65. buf[len - 3] = '.';
  66. }
  67. if(w > dc.w)
  68. return; /* too long */
  69. if(dc.font.set)
  70. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  71. else {
  72. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  73. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  74. }
  75. }
  76. unsigned long
  77. getcolor(const char *colstr)
  78. {
  79. Colormap cmap = DefaultColormap(dpy, screen);
  80. XColor color;
  81. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  82. return color.pixel;
  83. }
  84. void
  85. setfont(const char *fontstr)
  86. {
  87. char **missing, *def;
  88. int i, n;
  89. missing = NULL;
  90. setlocale(LC_ALL, "");
  91. if(dc.font.set)
  92. XFreeFontSet(dpy, dc.font.set);
  93. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  94. if(missing) {
  95. XFreeStringList(missing);
  96. if(dc.font.set) {
  97. XFreeFontSet(dpy, dc.font.set);
  98. dc.font.set = NULL;
  99. }
  100. }
  101. if(dc.font.set) {
  102. XFontSetExtents *font_extents;
  103. XFontStruct **xfonts;
  104. char **font_names;
  105. dc.font.ascent = dc.font.descent = 0;
  106. font_extents = XExtentsOfFontSet(dc.font.set);
  107. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  108. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  109. if(dc.font.ascent < (*xfonts)->ascent)
  110. dc.font.ascent = (*xfonts)->ascent;
  111. if(dc.font.descent < (*xfonts)->descent)
  112. dc.font.descent = (*xfonts)->descent;
  113. xfonts++;
  114. }
  115. }
  116. else {
  117. if(dc.font.xfont)
  118. XFreeFont(dpy, dc.font.xfont);
  119. dc.font.xfont = NULL;
  120. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  121. if (!dc.font.xfont)
  122. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  123. if (!dc.font.xfont)
  124. eprint("error, cannot init 'fixed' font\n");
  125. dc.font.ascent = dc.font.xfont->ascent;
  126. dc.font.descent = dc.font.xfont->descent;
  127. }
  128. dc.font.height = dc.font.ascent + dc.font.descent;
  129. }
  130. unsigned int
  131. textw(const char *text)
  132. {
  133. return textnw(text, strlen(text)) + dc.font.height;
  134. }