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.

232 lines
4.9 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
18 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
18 years ago
19 years ago
18 years ago
18 years ago
19 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
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 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 "dwm.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. static void
  21. drawtext(const char *text, unsigned int colidx, Bool highlight)
  22. {
  23. int x, y, w, h;
  24. static char buf[256];
  25. unsigned int len, olen;
  26. XPoint points[5];
  27. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  28. XSetForeground(dpy, dc.gc, dc.bg[colidx]);
  29. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  30. points[0].x = dc.x;
  31. points[0].y = dc.y;
  32. points[1].x = dc.w - 1;
  33. points[1].y = 0;
  34. points[2].x = 0;
  35. points[2].y = dc.h - 1;
  36. points[3].x = -(dc.w - 1);
  37. points[3].y = 0;
  38. points[4].x = 0;
  39. points[4].y = -(dc.h - 1);
  40. XSetForeground(dpy, dc.gc, dc.fg[colidx]);
  41. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  42. if(!text)
  43. return;
  44. w = 0;
  45. olen = len = strlen(text);
  46. if(len >= sizeof(buf))
  47. len = sizeof(buf) - 1;
  48. memcpy(buf, text, len);
  49. buf[len] = 0;
  50. h = dc.font.ascent + dc.font.descent;
  51. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  52. x = dc.x + (h / 2);
  53. /* shorten text if necessary */
  54. while(len && (w = textnw(buf, len)) > dc.w - h)
  55. buf[--len] = 0;
  56. if(len < olen) {
  57. if(len > 1)
  58. buf[len - 1] = '.';
  59. if(len > 2)
  60. buf[len - 2] = '.';
  61. if(len > 3)
  62. buf[len - 3] = '.';
  63. }
  64. if(w > dc.w)
  65. return; /* too long */
  66. if(dc.font.set)
  67. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  68. else {
  69. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  70. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  71. }
  72. if(highlight) {
  73. points[0].x = dc.x + 1;
  74. points[0].y = dc.y + 1;
  75. points[1].x = dc.w - 3;
  76. points[1].y = 0;
  77. points[2].x = 0;
  78. points[2].y = dc.h - 3;
  79. points[3].x = -(dc.w - 3);
  80. points[3].y = 0;
  81. points[4].x = 0;
  82. points[4].y = -(dc.h - 3);
  83. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  84. }
  85. }
  86. /* extern */
  87. void
  88. drawall()
  89. {
  90. Client *c;
  91. for(c = clients; c; c = getnext(c->next))
  92. drawtitle(c);
  93. drawstatus();
  94. }
  95. void
  96. drawstatus()
  97. {
  98. int i, x;
  99. Bool istile = arrange == dotile;
  100. dc.x = dc.y = 0;
  101. dc.w = bw;
  102. drawtext(NULL, istile ? 1 : 0, False);
  103. dc.w = 0;
  104. for(i = 0; i < ntags; i++) {
  105. dc.x += dc.w;
  106. dc.w = textw(tags[i]);
  107. if(istile)
  108. drawtext(tags[i], seltag[i] ? 0 : 1, sel && sel->tags[i]);
  109. else
  110. drawtext(tags[i], seltag[i] ? 1 : 0, sel && sel->tags[i]);
  111. }
  112. x = dc.x + dc.w;
  113. dc.w = textw(stext);
  114. dc.x = bx + bw - dc.w;
  115. if(dc.x < x) {
  116. dc.x = x;
  117. dc.w = bw - x;
  118. }
  119. drawtext(stext, istile ? 1 : 0, False);
  120. if(sel && ((dc.w = dc.x - x) > bh)) {
  121. dc.x = x;
  122. drawtext(sel->name, istile ? 0 : 1, False);
  123. }
  124. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  125. XSync(dpy, False);
  126. }
  127. void
  128. drawtitle(Client *c)
  129. {
  130. int i;
  131. Bool istile = arrange == dotile;
  132. if(c == sel && issel) {
  133. drawstatus();
  134. XUnmapWindow(dpy, c->twin);
  135. XSetWindowBorder(dpy, c->win, dc.fg[1]);
  136. return;
  137. }
  138. XSetWindowBorder(dpy, c->win, dc.bg[0]);
  139. XMapWindow(dpy, c->twin);
  140. dc.x = dc.y = 0;
  141. dc.w = c->tw;
  142. drawtext(c->name, istile ? 1 : 0, False);
  143. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  144. XSync(dpy, False);
  145. }
  146. unsigned long
  147. getcolor(const char *colstr)
  148. {
  149. Colormap cmap = DefaultColormap(dpy, screen);
  150. XColor color;
  151. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  152. return color.pixel;
  153. }
  154. void
  155. setfont(const char *fontstr)
  156. {
  157. char **missing, *def;
  158. int i, n;
  159. missing = NULL;
  160. setlocale(LC_ALL, "");
  161. if(dc.font.set)
  162. XFreeFontSet(dpy, dc.font.set);
  163. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  164. if(missing) {
  165. while(n--)
  166. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  167. XFreeStringList(missing);
  168. if(dc.font.set) {
  169. XFreeFontSet(dpy, dc.font.set);
  170. dc.font.set = NULL;
  171. }
  172. }
  173. if(dc.font.set) {
  174. XFontSetExtents *font_extents;
  175. XFontStruct **xfonts;
  176. char **font_names;
  177. dc.font.ascent = dc.font.descent = 0;
  178. font_extents = XExtentsOfFontSet(dc.font.set);
  179. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  180. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  181. if(dc.font.ascent < (*xfonts)->ascent)
  182. dc.font.ascent = (*xfonts)->ascent;
  183. if(dc.font.descent < (*xfonts)->descent)
  184. dc.font.descent = (*xfonts)->descent;
  185. xfonts++;
  186. }
  187. }
  188. else {
  189. if(dc.font.xfont)
  190. XFreeFont(dpy, dc.font.xfont);
  191. dc.font.xfont = NULL;
  192. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  193. if (!dc.font.xfont)
  194. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  195. if (!dc.font.xfont)
  196. eprint("error, cannot init 'fixed' font\n");
  197. dc.font.ascent = dc.font.xfont->ascent;
  198. dc.font.descent = dc.font.xfont->descent;
  199. }
  200. dc.font.height = dc.font.ascent + dc.font.descent;
  201. }
  202. unsigned int
  203. textw(const char *text)
  204. {
  205. return textnw(text, strlen(text)) + dc.font.height;
  206. }