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.
 
 
 
 
 
 

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