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.

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