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.

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