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.
 
 
 
 
 
 

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