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.

205 lines
4.6 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
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-MMVII 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. /* static */
  8. static Bool
  9. isoccupied(unsigned int t)
  10. {
  11. Client *c;
  12. for(c = clients; c; c = c->next)
  13. if(c->tags[t])
  14. return True;
  15. return False;
  16. }
  17. static unsigned int
  18. textnw(const char *text, unsigned int len) {
  19. XRectangle r;
  20. if(dc.font.set) {
  21. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  22. return r.width;
  23. }
  24. return XTextWidth(dc.font.xfont, text, len);
  25. }
  26. static void
  27. drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
  28. int x, y, w, h;
  29. static char buf[256];
  30. unsigned int len, olen;
  31. XGCValues gcv;
  32. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  33. XPoint pt[5];
  34. XSetForeground(dpy, dc.gc, col[ColBG]);
  35. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  36. if(!text)
  37. return;
  38. w = 0;
  39. olen = len = strlen(text);
  40. if(len >= sizeof buf)
  41. len = sizeof buf - 1;
  42. memcpy(buf, text, len);
  43. buf[len] = 0;
  44. h = dc.font.ascent + dc.font.descent;
  45. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  46. x = dc.x + (h / 2);
  47. /* shorten text if necessary */
  48. while(len && (w = textnw(buf, len)) > dc.w - h)
  49. buf[--len] = 0;
  50. if(len < olen) {
  51. if(len > 1)
  52. buf[len - 1] = '.';
  53. if(len > 2)
  54. buf[len - 2] = '.';
  55. if(len > 3)
  56. buf[len - 3] = '.';
  57. }
  58. if(w > dc.w)
  59. return; /* too long */
  60. gcv.foreground = col[ColFG];
  61. if(dc.font.set) {
  62. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  63. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  64. }
  65. else {
  66. gcv.font = dc.font.xfont->fid;
  67. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  68. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  69. }
  70. x = (h + 2) / 4;
  71. if(filledsquare) {
  72. r.x = dc.x + 1;
  73. r.y = dc.y + 1;
  74. r.width = r.height = x + 1;
  75. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  76. }
  77. else if(emptysquare) {
  78. pt[0].x = dc.x + 1;
  79. pt[0].y = dc.y + 1;
  80. pt[1].x = x;
  81. pt[1].y = 0;
  82. pt[2].x = 0;
  83. pt[2].y = x;
  84. pt[3].x = -x;
  85. pt[3].y = 0;
  86. pt[4].x = 0;
  87. pt[4].y = -x;
  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. drawclient(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(mtext, 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. drawclient(Client *c) {
  130. if(c == sel && issel) {
  131. drawstatus();
  132. XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
  133. return;
  134. }
  135. XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
  136. XSync(dpy, False);
  137. }
  138. unsigned long
  139. getcolor(const char *colstr) {
  140. Colormap cmap = DefaultColormap(dpy, screen);
  141. XColor color;
  142. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  143. eprint("error, cannot allocate color '%s'\n", colstr);
  144. return color.pixel;
  145. }
  146. void
  147. setfont(const char *fontstr) {
  148. char *def, **missing;
  149. int i, n;
  150. missing = NULL;
  151. if(dc.font.set)
  152. XFreeFontSet(dpy, dc.font.set);
  153. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  154. if(missing) {
  155. while(n--)
  156. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  157. XFreeStringList(missing);
  158. }
  159. if(dc.font.set) {
  160. XFontSetExtents *font_extents;
  161. XFontStruct **xfonts;
  162. char **font_names;
  163. dc.font.ascent = dc.font.descent = 0;
  164. font_extents = XExtentsOfFontSet(dc.font.set);
  165. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  166. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  167. if(dc.font.ascent < (*xfonts)->ascent)
  168. dc.font.ascent = (*xfonts)->ascent;
  169. if(dc.font.descent < (*xfonts)->descent)
  170. dc.font.descent = (*xfonts)->descent;
  171. xfonts++;
  172. }
  173. }
  174. else {
  175. if(dc.font.xfont)
  176. XFreeFont(dpy, dc.font.xfont);
  177. dc.font.xfont = NULL;
  178. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  179. eprint("error, cannot load font: '%s'\n", fontstr);
  180. dc.font.ascent = dc.font.xfont->ascent;
  181. dc.font.descent = dc.font.xfont->descent;
  182. }
  183. dc.font.height = dc.font.ascent + dc.font.descent;
  184. }
  185. unsigned int
  186. textw(const char *text) {
  187. return textnw(text, strlen(text)) + dc.font.height;
  188. }