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.

219 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
19 years ago
18 years ago
19 years ago
18 years ago
19 years ago
18 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
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 dot, Bool border) {
  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. if(dot) {
  72. r.x = dc.x + 2;
  73. r.y = dc.y + 2;
  74. r.width = r.height = (h + 2) / 4;
  75. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  76. }
  77. if(border) {
  78. pt[0].x = dc.x + 1;
  79. pt[0].y = dc.y + 1;
  80. pt[1].x = dc.w - 2;
  81. pt[1].y = 0;
  82. pt[2].x = 0;
  83. pt[2].y = dc.h - 2;
  84. pt[3].x = -(dc.w - 2);
  85. pt[3].y = 0;
  86. pt[4].x = 0;
  87. pt[4].y = -(dc.h - 2);
  88. XDrawLines(dpy, dc.drawable, dc.gc, pt, 5, CoordModePrevious);
  89. }
  90. }
  91. /* extern */
  92. void
  93. drawall(void) {
  94. Client *c;
  95. for(c = clients; c; c = getnext(c->next))
  96. drawtitle(c);
  97. drawstatus();
  98. }
  99. void
  100. drawstatus(void) {
  101. int i, x;
  102. dc.x = dc.y = 0;
  103. for(i = 0; i < ntags; i++) {
  104. dc.w = textw(tags[i]);
  105. if(seltag[i])
  106. drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
  107. else
  108. drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
  109. dc.x += dc.w;
  110. }
  111. dc.w = bmw;
  112. drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
  113. x = dc.x + dc.w;
  114. dc.w = textw(stext);
  115. dc.x = bw - dc.w;
  116. if(dc.x < x) {
  117. dc.x = x;
  118. dc.w = bw - x;
  119. }
  120. drawtext(stext, dc.status, False, False);
  121. if((dc.w = dc.x - x) > bh) {
  122. dc.x = x;
  123. drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
  124. }
  125. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  126. XSync(dpy, False);
  127. }
  128. void
  129. drawtitle(Client *c) {
  130. if(c == sel && issel) {
  131. drawstatus();
  132. XUnmapWindow(dpy, c->twin);
  133. XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
  134. return;
  135. }
  136. XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
  137. XMapWindow(dpy, c->twin);
  138. dc.x = dc.y = 0;
  139. dc.w = c->tw;
  140. drawtext(c->name, dc.norm, False,False);
  141. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  142. XSync(dpy, False);
  143. }
  144. unsigned long
  145. getcolor(const char *colstr) {
  146. Colormap cmap = DefaultColormap(dpy, screen);
  147. XColor color;
  148. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  149. eprint("error, cannot allocate color '%s'\n", colstr);
  150. return color.pixel;
  151. }
  152. void
  153. setfont(const char *fontstr) {
  154. char **missing, *def;
  155. int i, n;
  156. missing = NULL;
  157. setlocale(LC_ALL, "");
  158. if(dc.font.set)
  159. XFreeFontSet(dpy, dc.font.set);
  160. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  161. if(missing) {
  162. while(n--)
  163. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  164. XFreeStringList(missing);
  165. if(dc.font.set) {
  166. XFreeFontSet(dpy, dc.font.set);
  167. dc.font.set = NULL;
  168. }
  169. }
  170. if(dc.font.set) {
  171. XFontSetExtents *font_extents;
  172. XFontStruct **xfonts;
  173. char **font_names;
  174. dc.font.ascent = dc.font.descent = 0;
  175. font_extents = XExtentsOfFontSet(dc.font.set);
  176. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  177. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  178. if(dc.font.ascent < (*xfonts)->ascent)
  179. dc.font.ascent = (*xfonts)->ascent;
  180. if(dc.font.descent < (*xfonts)->descent)
  181. dc.font.descent = (*xfonts)->descent;
  182. xfonts++;
  183. }
  184. }
  185. else {
  186. if(dc.font.xfont)
  187. XFreeFont(dpy, dc.font.xfont);
  188. dc.font.xfont = NULL;
  189. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  190. if (!dc.font.xfont)
  191. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  192. if (!dc.font.xfont)
  193. eprint("error, cannot init 'fixed' font\n");
  194. dc.font.ascent = dc.font.xfont->ascent;
  195. dc.font.descent = dc.font.xfont->descent;
  196. }
  197. dc.font.height = dc.font.ascent + dc.font.descent;
  198. }
  199. unsigned int
  200. textw(const char *text) {
  201. return textnw(text, strlen(text)) + dc.font.height;
  202. }