My dmenu build
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

156 rader
3.4 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. XGCValues gcv;
  28. XPoint points[5];
  29. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  30. XSetForeground(dpy, dc.gc, dc.bg[colidx]);
  31. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  32. w = 0;
  33. XSetForeground(dpy, dc.gc, dc.fg[colidx]);
  34. if(border) {
  35. points[0].x = dc.x;
  36. points[0].y = dc.y;
  37. points[1].x = dc.w - 1;
  38. points[1].y = 0;
  39. points[2].x = 0;
  40. points[2].y = dc.h - 1;
  41. points[3].x = -(dc.w - 1);
  42. points[3].y = 0;
  43. points[4].x = 0;
  44. points[4].y = -(dc.h - 1);
  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. if(dc.font.set)
  71. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  72. else {
  73. gcv.font = dc.font.xfont->fid;
  74. XChangeGC(dpy, dc.gc, GCFont, &gcv);
  75. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  76. }
  77. }
  78. unsigned long
  79. getcolor(const char *colstr)
  80. {
  81. Colormap cmap = DefaultColormap(dpy, screen);
  82. XColor color;
  83. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  84. return color.pixel;
  85. }
  86. void
  87. setfont(const char *fontstr)
  88. {
  89. char **missing, *def;
  90. int i, n;
  91. missing = NULL;
  92. setlocale(LC_ALL, "");
  93. if(dc.font.set)
  94. XFreeFontSet(dpy, dc.font.set);
  95. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  96. if(missing) {
  97. XFreeStringList(missing);
  98. if(dc.font.set) {
  99. XFreeFontSet(dpy, dc.font.set);
  100. dc.font.set = NULL;
  101. }
  102. }
  103. if(dc.font.set) {
  104. XFontSetExtents *font_extents;
  105. XFontStruct **xfonts;
  106. char **font_names;
  107. dc.font.ascent = dc.font.descent = 0;
  108. font_extents = XExtentsOfFontSet(dc.font.set);
  109. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  110. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  111. if(dc.font.ascent < (*xfonts)->ascent)
  112. dc.font.ascent = (*xfonts)->ascent;
  113. if(dc.font.descent < (*xfonts)->descent)
  114. dc.font.descent = (*xfonts)->descent;
  115. xfonts++;
  116. }
  117. }
  118. else {
  119. if(dc.font.xfont)
  120. XFreeFont(dpy, dc.font.xfont);
  121. dc.font.xfont = NULL;
  122. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  123. if (!dc.font.xfont)
  124. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  125. if (!dc.font.xfont)
  126. eprint("error, cannot init 'fixed' font\n");
  127. dc.font.ascent = dc.font.xfont->ascent;
  128. dc.font.descent = dc.font.xfont->descent;
  129. }
  130. dc.font.height = dc.font.ascent + dc.font.descent;
  131. }
  132. unsigned int
  133. textw(const char *text)
  134. {
  135. return textnw(text, strlen(text)) + dc.font.height;
  136. }