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.

238 lines
5.1 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 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
19 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
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. points[0].x = dc.x + 1;
  80. points[0].y = dc.y + 1;
  81. points[1].x = dc.w - 3;
  82. points[1].y = 0;
  83. points[2].x = 0;
  84. points[2].y = dc.h - 3;
  85. points[3].x = -(dc.w - 3);
  86. points[3].y = 0;
  87. points[4].x = 0;
  88. points[4].y = -(dc.h - 3);
  89. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  90. }
  91. }
  92. /* extern */
  93. void
  94. drawall()
  95. {
  96. Client *c;
  97. for(c = clients; c; c = getnext(c->next))
  98. drawtitle(c);
  99. drawstatus();
  100. }
  101. void
  102. drawstatus()
  103. {
  104. int i, x;
  105. Bool istile = arrange == dotile;
  106. dc.x = dc.y = 0;
  107. dc.w = bw;
  108. drawtext(NULL, !istile, False);
  109. dc.w = 0;
  110. for(i = 0; i < ntags; i++) {
  111. dc.x += dc.w;
  112. dc.w = textw(tags[i]);
  113. if(istile)
  114. drawtext(tags[i], seltag[i], sel && sel->tags[i]);
  115. else
  116. drawtext(tags[i], !seltag[i], sel && sel->tags[i]);
  117. }
  118. x = dc.x + dc.w;
  119. dc.w = textw(stext);
  120. dc.x = bx + bw - dc.w;
  121. if(dc.x < x) {
  122. dc.x = x;
  123. dc.w = bw - x;
  124. }
  125. drawtext(stext, !istile, False);
  126. if(sel && ((dc.w = dc.x - x) > bh)) {
  127. dc.x = x;
  128. drawtext(sel->name, istile, False);
  129. }
  130. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  131. XSync(dpy, False);
  132. }
  133. void
  134. drawtitle(Client *c)
  135. {
  136. int i;
  137. Bool istile = arrange == dotile;
  138. if(c == sel && issel) {
  139. drawstatus();
  140. XUnmapWindow(dpy, c->twin);
  141. XSetWindowBorder(dpy, c->win, dc.fg);
  142. return;
  143. }
  144. XSetWindowBorder(dpy, c->win, dc.bg);
  145. XMapWindow(dpy, c->twin);
  146. dc.x = dc.y = 0;
  147. dc.w = c->tw;
  148. drawtext(c->name, !istile, False);
  149. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  150. XSync(dpy, False);
  151. }
  152. unsigned long
  153. getcolor(const char *colstr)
  154. {
  155. Colormap cmap = DefaultColormap(dpy, screen);
  156. XColor color;
  157. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  158. return color.pixel;
  159. }
  160. void
  161. setfont(const char *fontstr)
  162. {
  163. char **missing, *def;
  164. int i, n;
  165. missing = NULL;
  166. setlocale(LC_ALL, "");
  167. if(dc.font.set)
  168. XFreeFontSet(dpy, dc.font.set);
  169. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  170. if(missing) {
  171. while(n--)
  172. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  173. XFreeStringList(missing);
  174. if(dc.font.set) {
  175. XFreeFontSet(dpy, dc.font.set);
  176. dc.font.set = NULL;
  177. }
  178. }
  179. if(dc.font.set) {
  180. XFontSetExtents *font_extents;
  181. XFontStruct **xfonts;
  182. char **font_names;
  183. dc.font.ascent = dc.font.descent = 0;
  184. font_extents = XExtentsOfFontSet(dc.font.set);
  185. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  186. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  187. if(dc.font.ascent < (*xfonts)->ascent)
  188. dc.font.ascent = (*xfonts)->ascent;
  189. if(dc.font.descent < (*xfonts)->descent)
  190. dc.font.descent = (*xfonts)->descent;
  191. xfonts++;
  192. }
  193. }
  194. else {
  195. if(dc.font.xfont)
  196. XFreeFont(dpy, dc.font.xfont);
  197. dc.font.xfont = NULL;
  198. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  199. if (!dc.font.xfont)
  200. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  201. if (!dc.font.xfont)
  202. eprint("error, cannot init 'fixed' font\n");
  203. dc.font.ascent = dc.font.xfont->ascent;
  204. dc.font.descent = dc.font.xfont->descent;
  205. }
  206. dc.font.height = dc.font.ascent + dc.font.descent;
  207. }
  208. unsigned int
  209. textw(const char *text)
  210. {
  211. return textnw(text, strlen(text)) + dc.font.height;
  212. }