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.

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