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.

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