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.

236 lines
4.8 KiB

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
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
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 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 functions */
  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. unsigned int len;
  43. static char buf[256];
  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 functions */
  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;
  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. if(sel) {
  106. dc.x += dc.w;
  107. dc.w = textw(sel->name);
  108. drawtext(sel->name, istile, True);
  109. }
  110. dc.w = textw(stext);
  111. dc.x = bx + bw - dc.w;
  112. drawtext(stext, !istile, False);
  113. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  114. XFlush(dpy);
  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,
  142. 0, 0, c->tw, c->th, 0, 0);
  143. XFlush(dpy);
  144. }
  145. unsigned long
  146. getcolor(const char *colstr)
  147. {
  148. XColor color;
  149. Colormap cmap = DefaultColormap(dpy, screen);
  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(char *text)
  203. {
  204. return textnw(text, strlen(text)) + dc.font.height;
  205. }