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.

235 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
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(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, Bool border)
  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. w = 0;
  49. if(border)
  50. drawborder();
  51. if(!text)
  52. return;
  53. 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(w > dc.w)
  65. return; /* too long */
  66. gcv.foreground = invert ? dc.bg : dc.fg;
  67. gcv.background = invert ? dc.fg : dc.bg;
  68. if(dc.font.set) {
  69. XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
  70. XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
  71. x, y, buf, len);
  72. }
  73. else {
  74. gcv.font = dc.font.xfont->fid;
  75. XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
  76. XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  77. }
  78. }
  79. /* extern */
  80. void
  81. drawall()
  82. {
  83. Client *c;
  84. for(c = clients; c; c = getnext(c->next))
  85. drawtitle(c);
  86. drawstatus();
  87. }
  88. void
  89. drawstatus()
  90. {
  91. int i, x;
  92. Bool istile = arrange == dotile;
  93. dc.x = dc.y = 0;
  94. dc.w = bw;
  95. drawtext(NULL, !istile, False);
  96. dc.w = 0;
  97. for(i = 0; i < TLast; i++) {
  98. dc.x += dc.w;
  99. dc.w = textw(tags[i]);
  100. if(istile)
  101. drawtext(tags[i], (i == tsel), True);
  102. else
  103. drawtext(tags[i], (i != tsel), True);
  104. }
  105. x = dc.x + dc.w;
  106. dc.w = textw(stext);
  107. dc.x = bx + bw - dc.w;
  108. drawtext(stext, !istile, False);
  109. if(sel && ((dc.w = dc.x - x) >= bh)) {
  110. dc.x = x;
  111. drawtext(sel->name, istile, True);
  112. }
  113. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  114. XSync(dpy, False);
  115. }
  116. void
  117. drawtitle(Client *c)
  118. {
  119. int i;
  120. Bool istile = arrange == dotile;
  121. if(c == sel) {
  122. drawstatus();
  123. XUnmapWindow(dpy, c->title);
  124. XSetWindowBorder(dpy, c->win, dc.fg);
  125. return;
  126. }
  127. XSetWindowBorder(dpy, c->win, dc.bg);
  128. XMapWindow(dpy, c->title);
  129. dc.x = dc.y = 0;
  130. dc.w = 0;
  131. for(i = 0; i < TLast; i++) {
  132. if(c->tags[i]) {
  133. dc.x += dc.w;
  134. dc.w = textw(c->tags[i]);
  135. drawtext(c->tags[i], !istile, True);
  136. }
  137. }
  138. dc.x += dc.w;
  139. dc.w = textw(c->name);
  140. drawtext(c->name, !istile, True);
  141. XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  142. XSync(dpy, False);
  143. }
  144. unsigned long
  145. getcolor(const char *colstr)
  146. {
  147. Colormap cmap = DefaultColormap(dpy, screen);
  148. XColor color;
  149. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  150. return color.pixel;
  151. }
  152. void
  153. setfont(const char *fontstr)
  154. {
  155. char **missing, *def;
  156. int i, n;
  157. missing = NULL;
  158. setlocale(LC_ALL, "");
  159. if(dc.font.set)
  160. XFreeFontSet(dpy, dc.font.set);
  161. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  162. if(missing) {
  163. while(n--)
  164. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  165. XFreeStringList(missing);
  166. if(dc.font.set) {
  167. XFreeFontSet(dpy, dc.font.set);
  168. dc.font.set = NULL;
  169. }
  170. }
  171. if(dc.font.set) {
  172. XFontSetExtents *font_extents;
  173. XFontStruct **xfonts;
  174. char **font_names;
  175. dc.font.ascent = dc.font.descent = 0;
  176. font_extents = XExtentsOfFontSet(dc.font.set);
  177. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  178. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  179. if(dc.font.ascent < (*xfonts)->ascent)
  180. dc.font.ascent = (*xfonts)->ascent;
  181. if(dc.font.descent < (*xfonts)->descent)
  182. dc.font.descent = (*xfonts)->descent;
  183. xfonts++;
  184. }
  185. }
  186. else {
  187. if(dc.font.xfont)
  188. XFreeFont(dpy, dc.font.xfont);
  189. dc.font.xfont = NULL;
  190. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  191. if (!dc.font.xfont)
  192. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  193. if (!dc.font.xfont)
  194. eprint("error, cannot init 'fixed' font\n");
  195. dc.font.ascent = dc.font.xfont->ascent;
  196. dc.font.descent = dc.font.xfont->descent;
  197. }
  198. dc.font.height = dc.font.ascent + dc.font.descent;
  199. }
  200. unsigned int
  201. textw(char *text)
  202. {
  203. return textnw(text, strlen(text)) + dc.font.height;
  204. }