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.

242 lines
5.0 KiB

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