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.

draw.c 3.1 KiB

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