dmenu for lunch applications in dwm
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.

138 lines
3.1 KiB

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