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.

131 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
  1. /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dmenu.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <X11/Xlocale.h>
  8. /* static */
  9. static unsigned int
  10. textnw(const char *text, unsigned int len) {
  11. XRectangle r;
  12. if(dc.font.set) {
  13. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  14. return r.width;
  15. }
  16. return XTextWidth(dc.font.xfont, text, len);
  17. }
  18. /* extern */
  19. void
  20. drawtext(const char *text, unsigned long col[ColLast]) {
  21. int x, y, w, h;
  22. static char buf[256];
  23. unsigned int len, olen;
  24. XGCValues gcv;
  25. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  26. XSetForeground(dpy, dc.gc, col[ColBG]);
  27. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  28. if(!text)
  29. return;
  30. w = 0;
  31. olen = len = strlen(text);
  32. if(len >= sizeof(buf))
  33. len = sizeof(buf) - 1;
  34. memcpy(buf, text, len);
  35. buf[len] = 0;
  36. h = dc.font.ascent + dc.font.descent;
  37. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  38. x = dc.x + (h / 2);
  39. /* shorten text if necessary */
  40. while(len && (w = textnw(buf, len)) > dc.w - h)
  41. buf[--len] = 0;
  42. if(len < olen) {
  43. if(len > 1)
  44. buf[len - 1] = '.';
  45. if(len > 2)
  46. buf[len - 2] = '.';
  47. if(len > 3)
  48. buf[len - 3] = '.';
  49. }
  50. if(w > dc.w)
  51. return; /* too long */
  52. gcv.foreground = col[ColFG];
  53. if(dc.font.set) {
  54. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  55. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
  56. x, y, buf, len);
  57. }
  58. else {
  59. gcv.font = dc.font.xfont->fid;
  60. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  61. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  62. }
  63. }
  64. unsigned long
  65. getcolor(const char *colstr) {
  66. Colormap cmap = DefaultColormap(dpy, screen);
  67. XColor color;
  68. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  69. eprint("error, cannot allocate color '%s'\n", colstr);
  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. }