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.

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