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.

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