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.

220 lines
5.0 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
18 years ago
18 years ago
19 years ago
18 years ago
18 years ago
18 years ago
18 years ago
19 years ago
18 years ago
18 years ago
19 years ago
18 years ago
18 years ago
18 years ago
18 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
19 years ago
  1. /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <X11/Xlocale.h>
  8. /* static */
  9. static Bool
  10. isoccupied(unsigned int t)
  11. {
  12. Client *c;
  13. for(c = clients; c; c = c->next)
  14. if(c->tags[t])
  15. return True;
  16. return False;
  17. }
  18. static unsigned int
  19. textnw(const char *text, unsigned int len) {
  20. XRectangle r;
  21. if(dc.font.set) {
  22. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  23. return r.width;
  24. }
  25. return XTextWidth(dc.font.xfont, text, len);
  26. }
  27. static void
  28. drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
  29. int x, y, w, h;
  30. static char buf[256];
  31. unsigned int len, olen;
  32. XGCValues gcv;
  33. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  34. XPoint pt[5];
  35. XSetForeground(dpy, dc.gc, col[ColBG]);
  36. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  37. if(!text)
  38. return;
  39. w = 0;
  40. olen = len = strlen(text);
  41. if(len >= sizeof buf)
  42. len = sizeof buf - 1;
  43. memcpy(buf, text, len);
  44. buf[len] = 0;
  45. h = dc.font.ascent + dc.font.descent;
  46. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  47. x = dc.x + (h / 2);
  48. /* shorten text if necessary */
  49. while(len && (w = textnw(buf, len)) > dc.w - h)
  50. buf[--len] = 0;
  51. if(len < olen) {
  52. if(len > 1)
  53. buf[len - 1] = '.';
  54. if(len > 2)
  55. buf[len - 2] = '.';
  56. if(len > 3)
  57. buf[len - 3] = '.';
  58. }
  59. if(w > dc.w)
  60. return; /* too long */
  61. gcv.foreground = col[ColFG];
  62. if(dc.font.set) {
  63. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  64. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  65. }
  66. else {
  67. gcv.font = dc.font.xfont->fid;
  68. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  69. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  70. }
  71. x = (h + 2) / 4;
  72. if(filledsquare) {
  73. r.x = dc.x + 1;
  74. r.y = dc.y + 1;
  75. r.width = r.height = x + 1;
  76. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  77. }
  78. else if(emptysquare) {
  79. pt[0].x = dc.x + 1;
  80. pt[0].y = dc.y + 1;
  81. pt[1].x = x;
  82. pt[1].y = 0;
  83. pt[2].x = 0;
  84. pt[2].y = x;
  85. pt[3].x = -x;
  86. pt[3].y = 0;
  87. pt[4].x = 0;
  88. pt[4].y = -x;
  89. XDrawLines(dpy, dc.drawable, dc.gc, pt, 5, CoordModePrevious);
  90. }
  91. }
  92. /* extern */
  93. void
  94. drawall(void) {
  95. Client *c;
  96. for(c = clients; c; c = getnext(c->next))
  97. drawtitle(c);
  98. drawstatus();
  99. }
  100. void
  101. drawstatus(void) {
  102. int i, x;
  103. dc.x = dc.y = 0;
  104. for(i = 0; i < ntags; i++) {
  105. dc.w = textw(tags[i]);
  106. if(seltag[i])
  107. drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
  108. else
  109. drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
  110. dc.x += dc.w;
  111. }
  112. dc.w = bmw;
  113. drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
  114. x = dc.x + dc.w;
  115. dc.w = textw(stext);
  116. dc.x = bw - dc.w;
  117. if(dc.x < x) {
  118. dc.x = x;
  119. dc.w = bw - x;
  120. }
  121. drawtext(stext, dc.status, False, False);
  122. if((dc.w = dc.x - x) > bh) {
  123. dc.x = x;
  124. drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
  125. }
  126. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  127. XSync(dpy, False);
  128. }
  129. void
  130. drawtitle(Client *c) {
  131. if(c == sel && issel) {
  132. drawstatus();
  133. XUnmapWindow(dpy, c->twin);
  134. XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
  135. return;
  136. }
  137. XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
  138. XMapWindow(dpy, c->twin);
  139. dc.x = dc.y = 0;
  140. dc.w = c->tw;
  141. drawtext(c->name, dc.norm, False,False);
  142. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  143. XSync(dpy, False);
  144. }
  145. unsigned long
  146. getcolor(const char *colstr) {
  147. Colormap cmap = DefaultColormap(dpy, screen);
  148. XColor color;
  149. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  150. eprint("error, cannot allocate color '%s'\n", colstr);
  151. return color.pixel;
  152. }
  153. void
  154. setfont(const char *fontstr) {
  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(const char *text) {
  202. return textnw(text, strlen(text)) + dc.font.height;
  203. }