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.

225 lines
4.7 KiB

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